OSDN Git Service

DR 325
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR,
110   /* a transaction expression */
111   NIC_TRANSACTION
112 } non_integral_constant;
113
114 /* The various kinds of errors about name-lookup failing. */
115 typedef enum name_lookup_error {
116   /* NULL */
117   NLE_NULL,
118   /* is not a type */
119   NLE_TYPE,
120   /* is not a class or namespace */
121   NLE_CXX98,
122   /* is not a class, namespace, or enumeration */
123   NLE_NOT_CXX98
124 } name_lookup_error;
125
126 /* The various kinds of required token */
127 typedef enum required_token {
128   RT_NONE,
129   RT_SEMICOLON,  /* ';' */
130   RT_OPEN_PAREN, /* '(' */
131   RT_CLOSE_BRACE, /* '}' */
132   RT_OPEN_BRACE,  /* '{' */
133   RT_CLOSE_SQUARE, /* ']' */
134   RT_OPEN_SQUARE,  /* '[' */
135   RT_COMMA, /* ',' */
136   RT_SCOPE, /* '::' */
137   RT_LESS, /* '<' */
138   RT_GREATER, /* '>' */
139   RT_EQ, /* '=' */
140   RT_ELLIPSIS, /* '...' */
141   RT_MULT, /* '*' */
142   RT_COMPL, /* '~' */
143   RT_COLON, /* ':' */
144   RT_COLON_SCOPE, /* ':' or '::' */
145   RT_CLOSE_PAREN, /* ')' */
146   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147   RT_PRAGMA_EOL, /* end of line */
148   RT_NAME, /* identifier */
149
150   /* The type is CPP_KEYWORD */
151   RT_NEW, /* new */
152   RT_DELETE, /* delete */
153   RT_RETURN, /* return */
154   RT_WHILE, /* while */
155   RT_EXTERN, /* extern */
156   RT_STATIC_ASSERT, /* static_assert */
157   RT_DECLTYPE, /* decltype */
158   RT_OPERATOR, /* operator */
159   RT_CLASS, /* class */
160   RT_TEMPLATE, /* template */
161   RT_NAMESPACE, /* namespace */
162   RT_USING, /* using */
163   RT_ASM, /* asm */
164   RT_TRY, /* try */
165   RT_CATCH, /* catch */
166   RT_THROW, /* throw */
167   RT_LABEL, /* __label__ */
168   RT_AT_TRY, /* @try */
169   RT_AT_SYNCHRONIZED, /* @synchronized */
170   RT_AT_THROW, /* @throw */
171
172   RT_SELECT,  /* selection-statement */
173   RT_INTERATION, /* iteration-statement */
174   RT_JUMP, /* jump-statement */
175   RT_CLASS_KEY, /* class-key */
176   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179   RT_TRANSACTION_CANCEL /* __transaction_cancel */
180 } required_token;
181
182 /* Prototypes.  */
183
184 static cp_lexer *cp_lexer_new_main
185   (void);
186 static cp_lexer *cp_lexer_new_from_tokens
187   (cp_token_cache *tokens);
188 static void cp_lexer_destroy
189   (cp_lexer *);
190 static int cp_lexer_saving_tokens
191   (const cp_lexer *);
192 static cp_token *cp_lexer_token_at
193   (cp_lexer *, cp_token_position);
194 static void cp_lexer_get_preprocessor_token
195   (cp_lexer *, cp_token *);
196 static inline cp_token *cp_lexer_peek_token
197   (cp_lexer *);
198 static cp_token *cp_lexer_peek_nth_token
199   (cp_lexer *, size_t);
200 static inline bool cp_lexer_next_token_is
201   (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_not
203   (cp_lexer *, enum cpp_ttype);
204 static bool cp_lexer_next_token_is_keyword
205   (cp_lexer *, enum rid);
206 static cp_token *cp_lexer_consume_token
207   (cp_lexer *);
208 static void cp_lexer_purge_token
209   (cp_lexer *);
210 static void cp_lexer_purge_tokens_after
211   (cp_lexer *, cp_token_position);
212 static void cp_lexer_save_tokens
213   (cp_lexer *);
214 static void cp_lexer_commit_tokens
215   (cp_lexer *);
216 static void cp_lexer_rollback_tokens
217   (cp_lexer *);
218 static void cp_lexer_print_token
219   (FILE *, cp_token *);
220 static inline bool cp_lexer_debugging_p
221   (cp_lexer *);
222 static void cp_lexer_start_debugging
223   (cp_lexer *) ATTRIBUTE_UNUSED;
224 static void cp_lexer_stop_debugging
225   (cp_lexer *) ATTRIBUTE_UNUSED;
226
227 static cp_token_cache *cp_token_cache_new
228   (cp_token *, cp_token *);
229
230 static void cp_parser_initial_pragma
231   (cp_token *);
232
233 static tree cp_literal_operator_id
234   (const char *);
235
236 /* Manifest constants.  */
237 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
238 #define CP_SAVED_TOKEN_STACK 5
239
240 /* Variables.  */
241
242 /* The stream to which debugging output should be written.  */
243 static FILE *cp_lexer_debug_stream;
244
245 /* Nonzero if we are parsing an unevaluated operand: an operand to
246    sizeof, typeof, or alignof.  */
247 int cp_unevaluated_operand;
248
249 /* Dump up to NUM tokens in BUFFER to FILE starting with token
250    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
251    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
252    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
253    highlighted by surrounding it in [[ ]].  */
254
255 static void
256 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
257                       cp_token *start_token, unsigned num,
258                       cp_token *curr_token)
259 {
260   unsigned i, nprinted;
261   cp_token *token;
262   bool do_print;
263
264   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265
266   if (buffer == NULL)
267     return;
268
269   if (num == 0)
270     num = VEC_length (cp_token, buffer);
271
272   if (start_token == NULL)
273     start_token = VEC_address (cp_token, buffer);
274
275   if (start_token > VEC_address (cp_token, buffer))
276     {
277       cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
278       fprintf (file, " ... ");
279     }
280
281   do_print = false;
282   nprinted = 0;
283   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284     {
285       if (token == start_token)
286         do_print = true;
287
288       if (!do_print)
289         continue;
290
291       nprinted++;
292       if (token == curr_token)
293         fprintf (file, "[[");
294
295       cp_lexer_print_token (file, token);
296
297       if (token == curr_token)
298         fprintf (file, "]]");
299
300       switch (token->type)
301         {
302           case CPP_SEMICOLON:
303           case CPP_OPEN_BRACE:
304           case CPP_CLOSE_BRACE:
305           case CPP_EOF:
306             fputc ('\n', file);
307             break;
308
309           default:
310             fputc (' ', file);
311         }
312     }
313
314   if (i == num && i < VEC_length (cp_token, buffer))
315     {
316       fprintf (file, " ... ");
317       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
318                             VEC_length (cp_token, buffer) - 1));
319     }
320
321   fprintf (file, "\n");
322 }
323
324
325 /* Dump all tokens in BUFFER to stderr.  */
326
327 void
328 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
329 {
330   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
331 }
332
333
334 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
335    description for T.  */
336
337 static void
338 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
339 {
340   if (t)
341     {
342       fprintf (file, "%s: ", desc);
343       print_node_brief (file, "", t, 0);
344     }
345 }
346
347
348 /* Dump parser context C to FILE.  */
349
350 static void
351 cp_debug_print_context (FILE *file, cp_parser_context *c)
352 {
353   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
354   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
355   print_node_brief (file, "", c->object_type, 0);
356   fprintf (file, "}\n");
357 }
358
359
360 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
361
362 static void
363 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
364 {
365   unsigned i;
366   cp_parser_context *c;
367
368   fprintf (file, "Parsing context stack:\n");
369   for (i = 0, c = first; c; c = c->next, i++)
370     {
371       fprintf (file, "\t#%u: ", i);
372       cp_debug_print_context (file, c);
373     }
374 }
375
376
377 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
378
379 static void
380 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
381 {
382   if (flag)
383     fprintf (file, "%s: true\n", desc);
384 }
385
386
387 /* Print an unparsed function entry UF to FILE.  */
388
389 static void
390 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
391 {
392   unsigned i;
393   cp_default_arg_entry *default_arg_fn;
394   tree fn;
395
396   fprintf (file, "\tFunctions with default args:\n");
397   for (i = 0;
398        VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
399                     default_arg_fn);
400        i++)
401     {
402       fprintf (file, "\t\tClass type: ");
403       print_node_brief (file, "", default_arg_fn->class_type, 0);
404       fprintf (file, "\t\tDeclaration: ");
405       print_node_brief (file, "", default_arg_fn->decl, 0);
406       fprintf (file, "\n");
407     }
408
409   fprintf (file, "\n\tFunctions with definitions that require "
410            "post-processing\n\t\t");
411   for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
412     {
413       print_node_brief (file, "", fn, 0);
414       fprintf (file, " ");
415     }
416   fprintf (file, "\n");
417
418   fprintf (file, "\n\tNon-static data members with initializers that require "
419            "post-processing\n\t\t");
420   for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
421     {
422       print_node_brief (file, "", fn, 0);
423       fprintf (file, " ");
424     }
425   fprintf (file, "\n");
426 }
427
428
429 /* Print the stack of unparsed member functions S to FILE.  */
430
431 static void
432 cp_debug_print_unparsed_queues (FILE *file,
433                                 VEC(cp_unparsed_functions_entry, gc) *s)
434 {
435   unsigned i;
436   cp_unparsed_functions_entry *uf;
437
438   fprintf (file, "Unparsed functions\n");
439   for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
440     {
441       fprintf (file, "#%u:\n", i);
442       cp_debug_print_unparsed_function (file, uf);
443     }
444 }
445
446
447 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
448    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
449
450 static void
451 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
452 {
453   cp_token *next_token, *first_token, *start_token;
454
455   if (file == NULL)
456     file = stderr;
457
458   next_token = parser->lexer->next_token;
459   first_token = VEC_address (cp_token, parser->lexer->buffer);
460   start_token = (next_token > first_token + window_size / 2)
461                 ? next_token - window_size / 2
462                 : first_token;
463   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
464                         next_token);
465 }
466
467
468 /* Dump debugging information for the given PARSER.  If FILE is NULL,
469    the output is printed on stderr.  */
470
471 void
472 cp_debug_parser (FILE *file, cp_parser *parser)
473 {
474   const size_t window_size = 20;
475   cp_token *token;
476   expanded_location eloc;
477
478   if (file == NULL)
479     file = stderr;
480
481   fprintf (file, "Parser state\n\n");
482   fprintf (file, "Number of tokens: %u\n",
483            VEC_length (cp_token, parser->lexer->buffer));
484   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
485   cp_debug_print_tree_if_set (file, "Object scope",
486                                      parser->object_scope);
487   cp_debug_print_tree_if_set (file, "Qualifying scope",
488                                      parser->qualifying_scope);
489   cp_debug_print_context_stack (file, parser->context);
490   cp_debug_print_flag (file, "Allow GNU extensions",
491                               parser->allow_gnu_extensions_p);
492   cp_debug_print_flag (file, "'>' token is greater-than",
493                               parser->greater_than_is_operator_p);
494   cp_debug_print_flag (file, "Default args allowed in current "
495                               "parameter list", parser->default_arg_ok_p);
496   cp_debug_print_flag (file, "Parsing integral constant-expression",
497                               parser->integral_constant_expression_p);
498   cp_debug_print_flag (file, "Allow non-constant expression in current "
499                               "constant-expression",
500                               parser->allow_non_integral_constant_expression_p);
501   cp_debug_print_flag (file, "Seen non-constant expression",
502                               parser->non_integral_constant_expression_p);
503   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
504                               "current context",
505                               parser->local_variables_forbidden_p);
506   cp_debug_print_flag (file, "In unbraced linkage specification",
507                               parser->in_unbraced_linkage_specification_p);
508   cp_debug_print_flag (file, "Parsing a declarator",
509                               parser->in_declarator_p);
510   cp_debug_print_flag (file, "In template argument list",
511                               parser->in_template_argument_list_p);
512   cp_debug_print_flag (file, "Parsing an iteration statement",
513                               parser->in_statement & IN_ITERATION_STMT);
514   cp_debug_print_flag (file, "Parsing a switch statement",
515                               parser->in_statement & IN_SWITCH_STMT);
516   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
517                               parser->in_statement & IN_OMP_BLOCK);
518   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
519                               parser->in_statement & IN_OMP_FOR);
520   cp_debug_print_flag (file, "Parsing an if statement",
521                               parser->in_statement & IN_IF_STMT);
522   cp_debug_print_flag (file, "Parsing a type-id in an expression "
523                               "context", parser->in_type_id_in_expr_p);
524   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
525                               parser->implicit_extern_c);
526   cp_debug_print_flag (file, "String expressions should be translated "
527                               "to execution character set",
528                               parser->translate_strings_p);
529   cp_debug_print_flag (file, "Parsing function body outside of a "
530                               "local class", parser->in_function_body);
531   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
532                               parser->colon_corrects_to_scope_p);
533   if (parser->type_definition_forbidden_message)
534     fprintf (file, "Error message for forbidden type definitions: %s\n",
535              parser->type_definition_forbidden_message);
536   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
537   fprintf (file, "Number of class definitions in progress: %u\n",
538            parser->num_classes_being_defined);
539   fprintf (file, "Number of template parameter lists for the current "
540            "declaration: %u\n", parser->num_template_parameter_lists);
541   cp_debug_parser_tokens (file, parser, window_size);
542   token = parser->lexer->next_token;
543   fprintf (file, "Next token to parse:\n");
544   fprintf (file, "\tToken:  ");
545   cp_lexer_print_token (file, token);
546   eloc = expand_location (token->location);
547   fprintf (file, "\n\tFile:   %s\n", eloc.file);
548   fprintf (file, "\tLine:   %d\n", eloc.line);
549   fprintf (file, "\tColumn: %d\n", eloc.column);
550 }
551
552
553 /* Allocate memory for a new lexer object and return it.  */
554
555 static cp_lexer *
556 cp_lexer_alloc (void)
557 {
558   cp_lexer *lexer;
559
560   c_common_no_more_pch ();
561
562   /* Allocate the memory.  */
563   lexer = ggc_alloc_cleared_cp_lexer ();
564
565   /* Initially we are not debugging.  */
566   lexer->debugging_p = false;
567
568   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
569                                    CP_SAVED_TOKEN_STACK);
570
571   /* Create the buffer.  */
572   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
573
574   return lexer;
575 }
576
577
578 /* Create a new main C++ lexer, the lexer that gets tokens from the
579    preprocessor.  */
580
581 static cp_lexer *
582 cp_lexer_new_main (void)
583 {
584   cp_lexer *lexer;
585   cp_token token;
586
587   /* It's possible that parsing the first pragma will load a PCH file,
588      which is a GC collection point.  So we have to do that before
589      allocating any memory.  */
590   cp_parser_initial_pragma (&token);
591
592   lexer = cp_lexer_alloc ();
593
594   /* Put the first token in the buffer.  */
595   VEC_quick_push (cp_token, lexer->buffer, &token);
596
597   /* Get the remaining tokens from the preprocessor.  */
598   while (token.type != CPP_EOF)
599     {
600       cp_lexer_get_preprocessor_token (lexer, &token);
601       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
602     }
603
604   lexer->last_token = VEC_address (cp_token, lexer->buffer)
605                       + VEC_length (cp_token, lexer->buffer)
606                       - 1;
607   lexer->next_token = VEC_length (cp_token, lexer->buffer)
608                       ? VEC_address (cp_token, lexer->buffer)
609                       : &eof_token;
610
611   /* Subsequent preprocessor diagnostics should use compiler
612      diagnostic functions to get the compiler source location.  */
613   done_lexing = true;
614
615   gcc_assert (!lexer->next_token->purged_p);
616   return lexer;
617 }
618
619 /* Create a new lexer whose token stream is primed with the tokens in
620    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
621
622 static cp_lexer *
623 cp_lexer_new_from_tokens (cp_token_cache *cache)
624 {
625   cp_token *first = cache->first;
626   cp_token *last = cache->last;
627   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
628
629   /* We do not own the buffer.  */
630   lexer->buffer = NULL;
631   lexer->next_token = first == last ? &eof_token : first;
632   lexer->last_token = last;
633
634   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
635                                    CP_SAVED_TOKEN_STACK);
636
637   /* Initially we are not debugging.  */
638   lexer->debugging_p = false;
639
640   gcc_assert (!lexer->next_token->purged_p);
641   return lexer;
642 }
643
644 /* Frees all resources associated with LEXER.  */
645
646 static void
647 cp_lexer_destroy (cp_lexer *lexer)
648 {
649   VEC_free (cp_token, gc, lexer->buffer);
650   VEC_free (cp_token_position, heap, lexer->saved_tokens);
651   ggc_free (lexer);
652 }
653
654 /* Returns nonzero if debugging information should be output.  */
655
656 static inline bool
657 cp_lexer_debugging_p (cp_lexer *lexer)
658 {
659   return lexer->debugging_p;
660 }
661
662
663 static inline cp_token_position
664 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
665 {
666   gcc_assert (!previous_p || lexer->next_token != &eof_token);
667
668   return lexer->next_token - previous_p;
669 }
670
671 static inline cp_token *
672 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
673 {
674   return pos;
675 }
676
677 static inline void
678 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
679 {
680   lexer->next_token = cp_lexer_token_at (lexer, pos);
681 }
682
683 static inline cp_token_position
684 cp_lexer_previous_token_position (cp_lexer *lexer)
685 {
686   if (lexer->next_token == &eof_token)
687     return lexer->last_token - 1;
688   else
689     return cp_lexer_token_position (lexer, true);
690 }
691
692 static inline cp_token *
693 cp_lexer_previous_token (cp_lexer *lexer)
694 {
695   cp_token_position tp = cp_lexer_previous_token_position (lexer);
696
697   return cp_lexer_token_at (lexer, tp);
698 }
699
700 /* nonzero if we are presently saving tokens.  */
701
702 static inline int
703 cp_lexer_saving_tokens (const cp_lexer* lexer)
704 {
705   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
706 }
707
708 /* Store the next token from the preprocessor in *TOKEN.  Return true
709    if we reach EOF.  If LEXER is NULL, assume we are handling an
710    initial #pragma pch_preprocess, and thus want the lexer to return
711    processed strings.  */
712
713 static void
714 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
715 {
716   static int is_extern_c = 0;
717
718    /* Get a new token from the preprocessor.  */
719   token->type
720     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
721                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
722   token->keyword = RID_MAX;
723   token->pragma_kind = PRAGMA_NONE;
724   token->purged_p = false;
725
726   /* On some systems, some header files are surrounded by an
727      implicit extern "C" block.  Set a flag in the token if it
728      comes from such a header.  */
729   is_extern_c += pending_lang_change;
730   pending_lang_change = 0;
731   token->implicit_extern_c = is_extern_c > 0;
732
733   /* Check to see if this token is a keyword.  */
734   if (token->type == CPP_NAME)
735     {
736       if (C_IS_RESERVED_WORD (token->u.value))
737         {
738           /* Mark this token as a keyword.  */
739           token->type = CPP_KEYWORD;
740           /* Record which keyword.  */
741           token->keyword = C_RID_CODE (token->u.value);
742         }
743       else
744         {
745           if (warn_cxx0x_compat
746               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
747               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
748             {
749               /* Warn about the C++0x keyword (but still treat it as
750                  an identifier).  */
751               warning (OPT_Wc__0x_compat, 
752                        "identifier %qE is a keyword in C++11",
753                        token->u.value);
754
755               /* Clear out the C_RID_CODE so we don't warn about this
756                  particular identifier-turned-keyword again.  */
757               C_SET_RID_CODE (token->u.value, RID_MAX);
758             }
759
760           token->ambiguous_p = false;
761           token->keyword = RID_MAX;
762         }
763     }
764   else if (token->type == CPP_AT_NAME)
765     {
766       /* This only happens in Objective-C++; it must be a keyword.  */
767       token->type = CPP_KEYWORD;
768       switch (C_RID_CODE (token->u.value))
769         {
770           /* Replace 'class' with '@class', 'private' with '@private',
771              etc.  This prevents confusion with the C++ keyword
772              'class', and makes the tokens consistent with other
773              Objective-C 'AT' keywords.  For example '@class' is
774              reported as RID_AT_CLASS which is consistent with
775              '@synchronized', which is reported as
776              RID_AT_SYNCHRONIZED.
777           */
778         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
779         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
780         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
781         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
782         case RID_THROW:     token->keyword = RID_AT_THROW; break;
783         case RID_TRY:       token->keyword = RID_AT_TRY; break;
784         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
785         default:            token->keyword = C_RID_CODE (token->u.value);
786         }
787     }
788   else if (token->type == CPP_PRAGMA)
789     {
790       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
791       token->pragma_kind = ((enum pragma_kind)
792                             TREE_INT_CST_LOW (token->u.value));
793       token->u.value = NULL_TREE;
794     }
795 }
796
797 /* Update the globals input_location and the input file stack from TOKEN.  */
798 static inline void
799 cp_lexer_set_source_position_from_token (cp_token *token)
800 {
801   if (token->type != CPP_EOF)
802     {
803       input_location = token->location;
804     }
805 }
806
807 /* Return a pointer to the next token in the token stream, but do not
808    consume it.  */
809
810 static inline cp_token *
811 cp_lexer_peek_token (cp_lexer *lexer)
812 {
813   if (cp_lexer_debugging_p (lexer))
814     {
815       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
816       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
817       putc ('\n', cp_lexer_debug_stream);
818     }
819   return lexer->next_token;
820 }
821
822 /* Return true if the next token has the indicated TYPE.  */
823
824 static inline bool
825 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
826 {
827   return cp_lexer_peek_token (lexer)->type == type;
828 }
829
830 /* Return true if the next token does not have the indicated TYPE.  */
831
832 static inline bool
833 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
834 {
835   return !cp_lexer_next_token_is (lexer, type);
836 }
837
838 /* Return true if the next token is the indicated KEYWORD.  */
839
840 static inline bool
841 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
842 {
843   return cp_lexer_peek_token (lexer)->keyword == keyword;
844 }
845
846 /* Return true if the next token is not the indicated KEYWORD.  */
847
848 static inline bool
849 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
850 {
851   return cp_lexer_peek_token (lexer)->keyword != keyword;
852 }
853
854 /* Return true if the next token is a keyword for a decl-specifier.  */
855
856 static bool
857 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
858 {
859   cp_token *token;
860
861   token = cp_lexer_peek_token (lexer);
862   switch (token->keyword) 
863     {
864       /* auto specifier: storage-class-specifier in C++,
865          simple-type-specifier in C++0x.  */
866     case RID_AUTO:
867       /* Storage classes.  */
868     case RID_REGISTER:
869     case RID_STATIC:
870     case RID_EXTERN:
871     case RID_MUTABLE:
872     case RID_THREAD:
873       /* Elaborated type specifiers.  */
874     case RID_ENUM:
875     case RID_CLASS:
876     case RID_STRUCT:
877     case RID_UNION:
878     case RID_TYPENAME:
879       /* Simple type specifiers.  */
880     case RID_CHAR:
881     case RID_CHAR16:
882     case RID_CHAR32:
883     case RID_WCHAR:
884     case RID_BOOL:
885     case RID_SHORT:
886     case RID_INT:
887     case RID_LONG:
888     case RID_INT128:
889     case RID_SIGNED:
890     case RID_UNSIGNED:
891     case RID_FLOAT:
892     case RID_DOUBLE:
893     case RID_VOID:
894       /* GNU extensions.  */ 
895     case RID_ATTRIBUTE:
896     case RID_TYPEOF:
897       /* C++0x extensions.  */
898     case RID_DECLTYPE:
899     case RID_UNDERLYING_TYPE:
900       return true;
901
902     default:
903       return false;
904     }
905 }
906
907 /* Returns TRUE iff the token T begins a decltype type.  */
908
909 static bool
910 token_is_decltype (cp_token *t)
911 {
912   return (t->keyword == RID_DECLTYPE
913           || t->type == CPP_DECLTYPE);
914 }
915
916 /* Returns TRUE iff the next token begins a decltype type.  */
917
918 static bool
919 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
920 {
921   cp_token *t = cp_lexer_peek_token (lexer);
922   return token_is_decltype (t);
923 }
924
925 /* Return a pointer to the Nth token in the token stream.  If N is 1,
926    then this is precisely equivalent to cp_lexer_peek_token (except
927    that it is not inline).  One would like to disallow that case, but
928    there is one case (cp_parser_nth_token_starts_template_id) where
929    the caller passes a variable for N and it might be 1.  */
930
931 static cp_token *
932 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
933 {
934   cp_token *token;
935
936   /* N is 1-based, not zero-based.  */
937   gcc_assert (n > 0);
938
939   if (cp_lexer_debugging_p (lexer))
940     fprintf (cp_lexer_debug_stream,
941              "cp_lexer: peeking ahead %ld at token: ", (long)n);
942
943   --n;
944   token = lexer->next_token;
945   gcc_assert (!n || token != &eof_token);
946   while (n != 0)
947     {
948       ++token;
949       if (token == lexer->last_token)
950         {
951           token = &eof_token;
952           break;
953         }
954
955       if (!token->purged_p)
956         --n;
957     }
958
959   if (cp_lexer_debugging_p (lexer))
960     {
961       cp_lexer_print_token (cp_lexer_debug_stream, token);
962       putc ('\n', cp_lexer_debug_stream);
963     }
964
965   return token;
966 }
967
968 /* Return the next token, and advance the lexer's next_token pointer
969    to point to the next non-purged token.  */
970
971 static cp_token *
972 cp_lexer_consume_token (cp_lexer* lexer)
973 {
974   cp_token *token = lexer->next_token;
975
976   gcc_assert (token != &eof_token);
977   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
978
979   do
980     {
981       lexer->next_token++;
982       if (lexer->next_token == lexer->last_token)
983         {
984           lexer->next_token = &eof_token;
985           break;
986         }
987
988     }
989   while (lexer->next_token->purged_p);
990
991   cp_lexer_set_source_position_from_token (token);
992
993   /* Provide debugging output.  */
994   if (cp_lexer_debugging_p (lexer))
995     {
996       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
997       cp_lexer_print_token (cp_lexer_debug_stream, token);
998       putc ('\n', cp_lexer_debug_stream);
999     }
1000
1001   return token;
1002 }
1003
1004 /* Permanently remove the next token from the token stream, and
1005    advance the next_token pointer to refer to the next non-purged
1006    token.  */
1007
1008 static void
1009 cp_lexer_purge_token (cp_lexer *lexer)
1010 {
1011   cp_token *tok = lexer->next_token;
1012
1013   gcc_assert (tok != &eof_token);
1014   tok->purged_p = true;
1015   tok->location = UNKNOWN_LOCATION;
1016   tok->u.value = NULL_TREE;
1017   tok->keyword = RID_MAX;
1018
1019   do
1020     {
1021       tok++;
1022       if (tok == lexer->last_token)
1023         {
1024           tok = &eof_token;
1025           break;
1026         }
1027     }
1028   while (tok->purged_p);
1029   lexer->next_token = tok;
1030 }
1031
1032 /* Permanently remove all tokens after TOK, up to, but not
1033    including, the token that will be returned next by
1034    cp_lexer_peek_token.  */
1035
1036 static void
1037 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1038 {
1039   cp_token *peek = lexer->next_token;
1040
1041   if (peek == &eof_token)
1042     peek = lexer->last_token;
1043
1044   gcc_assert (tok < peek);
1045
1046   for ( tok += 1; tok != peek; tok += 1)
1047     {
1048       tok->purged_p = true;
1049       tok->location = UNKNOWN_LOCATION;
1050       tok->u.value = NULL_TREE;
1051       tok->keyword = RID_MAX;
1052     }
1053 }
1054
1055 /* Begin saving tokens.  All tokens consumed after this point will be
1056    preserved.  */
1057
1058 static void
1059 cp_lexer_save_tokens (cp_lexer* lexer)
1060 {
1061   /* Provide debugging output.  */
1062   if (cp_lexer_debugging_p (lexer))
1063     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1064
1065   VEC_safe_push (cp_token_position, heap,
1066                  lexer->saved_tokens, lexer->next_token);
1067 }
1068
1069 /* Commit to the portion of the token stream most recently saved.  */
1070
1071 static void
1072 cp_lexer_commit_tokens (cp_lexer* lexer)
1073 {
1074   /* Provide debugging output.  */
1075   if (cp_lexer_debugging_p (lexer))
1076     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1077
1078   VEC_pop (cp_token_position, lexer->saved_tokens);
1079 }
1080
1081 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1082    to the token stream.  Stop saving tokens.  */
1083
1084 static void
1085 cp_lexer_rollback_tokens (cp_lexer* lexer)
1086 {
1087   /* Provide debugging output.  */
1088   if (cp_lexer_debugging_p (lexer))
1089     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1090
1091   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1092 }
1093
1094 /* Print a representation of the TOKEN on the STREAM.  */
1095
1096 static void
1097 cp_lexer_print_token (FILE * stream, cp_token *token)
1098 {
1099   /* We don't use cpp_type2name here because the parser defines
1100      a few tokens of its own.  */
1101   static const char *const token_names[] = {
1102     /* cpplib-defined token types */
1103 #define OP(e, s) #e,
1104 #define TK(e, s) #e,
1105     TTYPE_TABLE
1106 #undef OP
1107 #undef TK
1108     /* C++ parser token types - see "Manifest constants", above.  */
1109     "KEYWORD",
1110     "TEMPLATE_ID",
1111     "NESTED_NAME_SPECIFIER",
1112   };
1113
1114   /* For some tokens, print the associated data.  */
1115   switch (token->type)
1116     {
1117     case CPP_KEYWORD:
1118       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1119          For example, `struct' is mapped to an INTEGER_CST.  */
1120       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1121         break;
1122       /* else fall through */
1123     case CPP_NAME:
1124       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1125       break;
1126
1127     case CPP_STRING:
1128     case CPP_STRING16:
1129     case CPP_STRING32:
1130     case CPP_WSTRING:
1131     case CPP_UTF8STRING:
1132       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1133       break;
1134
1135     case CPP_NUMBER:
1136       print_generic_expr (stream, token->u.value, 0);
1137       break;
1138
1139     default:
1140       /* If we have a name for the token, print it out.  Otherwise, we
1141          simply give the numeric code.  */
1142       if (token->type < ARRAY_SIZE(token_names))
1143         fputs (token_names[token->type], stream);
1144       else
1145         fprintf (stream, "[%d]", token->type);
1146       break;
1147     }
1148 }
1149
1150 /* Start emitting debugging information.  */
1151
1152 static void
1153 cp_lexer_start_debugging (cp_lexer* lexer)
1154 {
1155   lexer->debugging_p = true;
1156   cp_lexer_debug_stream = stderr;
1157 }
1158
1159 /* Stop emitting debugging information.  */
1160
1161 static void
1162 cp_lexer_stop_debugging (cp_lexer* lexer)
1163 {
1164   lexer->debugging_p = false;
1165   cp_lexer_debug_stream = NULL;
1166 }
1167
1168 /* Create a new cp_token_cache, representing a range of tokens.  */
1169
1170 static cp_token_cache *
1171 cp_token_cache_new (cp_token *first, cp_token *last)
1172 {
1173   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1174   cache->first = first;
1175   cache->last = last;
1176   return cache;
1177 }
1178
1179 \f
1180 /* Decl-specifiers.  */
1181
1182 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1183
1184 static void
1185 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1186 {
1187   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1188 }
1189
1190 /* Declarators.  */
1191
1192 /* Nothing other than the parser should be creating declarators;
1193    declarators are a semi-syntactic representation of C++ entities.
1194    Other parts of the front end that need to create entities (like
1195    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1196
1197 static cp_declarator *make_call_declarator
1198   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1199 static cp_declarator *make_array_declarator
1200   (cp_declarator *, tree);
1201 static cp_declarator *make_pointer_declarator
1202   (cp_cv_quals, cp_declarator *);
1203 static cp_declarator *make_reference_declarator
1204   (cp_cv_quals, cp_declarator *, bool);
1205 static cp_parameter_declarator *make_parameter_declarator
1206   (cp_decl_specifier_seq *, cp_declarator *, tree);
1207 static cp_declarator *make_ptrmem_declarator
1208   (cp_cv_quals, tree, cp_declarator *);
1209
1210 /* An erroneous declarator.  */
1211 static cp_declarator *cp_error_declarator;
1212
1213 /* The obstack on which declarators and related data structures are
1214    allocated.  */
1215 static struct obstack declarator_obstack;
1216
1217 /* Alloc BYTES from the declarator memory pool.  */
1218
1219 static inline void *
1220 alloc_declarator (size_t bytes)
1221 {
1222   return obstack_alloc (&declarator_obstack, bytes);
1223 }
1224
1225 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1226    common to all declarators.  */
1227
1228 static cp_declarator *
1229 make_declarator (cp_declarator_kind kind)
1230 {
1231   cp_declarator *declarator;
1232
1233   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1234   declarator->kind = kind;
1235   declarator->attributes = NULL_TREE;
1236   declarator->declarator = NULL;
1237   declarator->parameter_pack_p = false;
1238   declarator->id_loc = UNKNOWN_LOCATION;
1239
1240   return declarator;
1241 }
1242
1243 /* Make a declarator for a generalized identifier.  If
1244    QUALIFYING_SCOPE is non-NULL, the identifier is
1245    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1246    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1247    is, if any.   */
1248
1249 static cp_declarator *
1250 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1251                     special_function_kind sfk)
1252 {
1253   cp_declarator *declarator;
1254
1255   /* It is valid to write:
1256
1257        class C { void f(); };
1258        typedef C D;
1259        void D::f();
1260
1261      The standard is not clear about whether `typedef const C D' is
1262      legal; as of 2002-09-15 the committee is considering that
1263      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1264      well.  */
1265   if (qualifying_scope && TYPE_P (qualifying_scope))
1266     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267
1268   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1269               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1270               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271
1272   declarator = make_declarator (cdk_id);
1273   declarator->u.id.qualifying_scope = qualifying_scope;
1274   declarator->u.id.unqualified_name = unqualified_name;
1275   declarator->u.id.sfk = sfk;
1276   
1277   return declarator;
1278 }
1279
1280 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1281    of modifiers such as const or volatile to apply to the pointer
1282    type, represented as identifiers.  */
1283
1284 cp_declarator *
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1286 {
1287   cp_declarator *declarator;
1288
1289   declarator = make_declarator (cdk_pointer);
1290   declarator->declarator = target;
1291   declarator->u.pointer.qualifiers = cv_qualifiers;
1292   declarator->u.pointer.class_type = NULL_TREE;
1293   if (target)
1294     {
1295       declarator->id_loc = target->id_loc;
1296       declarator->parameter_pack_p = target->parameter_pack_p;
1297       target->parameter_pack_p = false;
1298     }
1299   else
1300     declarator->parameter_pack_p = false;
1301
1302   return declarator;
1303 }
1304
1305 /* Like make_pointer_declarator -- but for references.  */
1306
1307 cp_declarator *
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309                            bool rvalue_ref)
1310 {
1311   cp_declarator *declarator;
1312
1313   declarator = make_declarator (cdk_reference);
1314   declarator->declarator = target;
1315   declarator->u.reference.qualifiers = cv_qualifiers;
1316   declarator->u.reference.rvalue_ref = rvalue_ref;
1317   if (target)
1318     {
1319       declarator->id_loc = target->id_loc;
1320       declarator->parameter_pack_p = target->parameter_pack_p;
1321       target->parameter_pack_p = false;
1322     }
1323   else
1324     declarator->parameter_pack_p = false;
1325
1326   return declarator;
1327 }
1328
1329 /* Like make_pointer_declarator -- but for a pointer to a non-static
1330    member of CLASS_TYPE.  */
1331
1332 cp_declarator *
1333 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1334                         cp_declarator *pointee)
1335 {
1336   cp_declarator *declarator;
1337
1338   declarator = make_declarator (cdk_ptrmem);
1339   declarator->declarator = pointee;
1340   declarator->u.pointer.qualifiers = cv_qualifiers;
1341   declarator->u.pointer.class_type = class_type;
1342
1343   if (pointee)
1344     {
1345       declarator->parameter_pack_p = pointee->parameter_pack_p;
1346       pointee->parameter_pack_p = false;
1347     }
1348   else
1349     declarator->parameter_pack_p = false;
1350
1351   return declarator;
1352 }
1353
1354 /* Make a declarator for the function given by TARGET, with the
1355    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1356    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1357    indicates what exceptions can be thrown.  */
1358
1359 cp_declarator *
1360 make_call_declarator (cp_declarator *target,
1361                       tree parms,
1362                       cp_cv_quals cv_qualifiers,
1363                       cp_virt_specifiers virt_specifiers,
1364                       tree exception_specification,
1365                       tree late_return_type)
1366 {
1367   cp_declarator *declarator;
1368
1369   declarator = make_declarator (cdk_function);
1370   declarator->declarator = target;
1371   declarator->u.function.parameters = parms;
1372   declarator->u.function.qualifiers = cv_qualifiers;
1373   declarator->u.function.virt_specifiers = virt_specifiers;
1374   declarator->u.function.exception_specification = exception_specification;
1375   declarator->u.function.late_return_type = late_return_type;
1376   if (target)
1377     {
1378       declarator->id_loc = target->id_loc;
1379       declarator->parameter_pack_p = target->parameter_pack_p;
1380       target->parameter_pack_p = false;
1381     }
1382   else
1383     declarator->parameter_pack_p = false;
1384
1385   return declarator;
1386 }
1387
1388 /* Make a declarator for an array of BOUNDS elements, each of which is
1389    defined by ELEMENT.  */
1390
1391 cp_declarator *
1392 make_array_declarator (cp_declarator *element, tree bounds)
1393 {
1394   cp_declarator *declarator;
1395
1396   declarator = make_declarator (cdk_array);
1397   declarator->declarator = element;
1398   declarator->u.array.bounds = bounds;
1399   if (element)
1400     {
1401       declarator->id_loc = element->id_loc;
1402       declarator->parameter_pack_p = element->parameter_pack_p;
1403       element->parameter_pack_p = false;
1404     }
1405   else
1406     declarator->parameter_pack_p = false;
1407
1408   return declarator;
1409 }
1410
1411 /* Determine whether the declarator we've seen so far can be a
1412    parameter pack, when followed by an ellipsis.  */
1413 static bool 
1414 declarator_can_be_parameter_pack (cp_declarator *declarator)
1415 {
1416   /* Search for a declarator name, or any other declarator that goes
1417      after the point where the ellipsis could appear in a parameter
1418      pack. If we find any of these, then this declarator can not be
1419      made into a parameter pack.  */
1420   bool found = false;
1421   while (declarator && !found)
1422     {
1423       switch ((int)declarator->kind)
1424         {
1425         case cdk_id:
1426         case cdk_array:
1427           found = true;
1428           break;
1429
1430         case cdk_error:
1431           return true;
1432
1433         default:
1434           declarator = declarator->declarator;
1435           break;
1436         }
1437     }
1438
1439   return !found;
1440 }
1441
1442 cp_parameter_declarator *no_parameters;
1443
1444 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1445    DECLARATOR and DEFAULT_ARGUMENT.  */
1446
1447 cp_parameter_declarator *
1448 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1449                            cp_declarator *declarator,
1450                            tree default_argument)
1451 {
1452   cp_parameter_declarator *parameter;
1453
1454   parameter = ((cp_parameter_declarator *)
1455                alloc_declarator (sizeof (cp_parameter_declarator)));
1456   parameter->next = NULL;
1457   if (decl_specifiers)
1458     parameter->decl_specifiers = *decl_specifiers;
1459   else
1460     clear_decl_specs (&parameter->decl_specifiers);
1461   parameter->declarator = declarator;
1462   parameter->default_argument = default_argument;
1463   parameter->ellipsis_p = false;
1464
1465   return parameter;
1466 }
1467
1468 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1469
1470 static bool
1471 function_declarator_p (const cp_declarator *declarator)
1472 {
1473   while (declarator)
1474     {
1475       if (declarator->kind == cdk_function
1476           && declarator->declarator->kind == cdk_id)
1477         return true;
1478       if (declarator->kind == cdk_id
1479           || declarator->kind == cdk_error)
1480         return false;
1481       declarator = declarator->declarator;
1482     }
1483   return false;
1484 }
1485  
1486 /* The parser.  */
1487
1488 /* Overview
1489    --------
1490
1491    A cp_parser parses the token stream as specified by the C++
1492    grammar.  Its job is purely parsing, not semantic analysis.  For
1493    example, the parser breaks the token stream into declarators,
1494    expressions, statements, and other similar syntactic constructs.
1495    It does not check that the types of the expressions on either side
1496    of an assignment-statement are compatible, or that a function is
1497    not declared with a parameter of type `void'.
1498
1499    The parser invokes routines elsewhere in the compiler to perform
1500    semantic analysis and to build up the abstract syntax tree for the
1501    code processed.
1502
1503    The parser (and the template instantiation code, which is, in a
1504    way, a close relative of parsing) are the only parts of the
1505    compiler that should be calling push_scope and pop_scope, or
1506    related functions.  The parser (and template instantiation code)
1507    keeps track of what scope is presently active; everything else
1508    should simply honor that.  (The code that generates static
1509    initializers may also need to set the scope, in order to check
1510    access control correctly when emitting the initializers.)
1511
1512    Methodology
1513    -----------
1514
1515    The parser is of the standard recursive-descent variety.  Upcoming
1516    tokens in the token stream are examined in order to determine which
1517    production to use when parsing a non-terminal.  Some C++ constructs
1518    require arbitrary look ahead to disambiguate.  For example, it is
1519    impossible, in the general case, to tell whether a statement is an
1520    expression or declaration without scanning the entire statement.
1521    Therefore, the parser is capable of "parsing tentatively."  When the
1522    parser is not sure what construct comes next, it enters this mode.
1523    Then, while we attempt to parse the construct, the parser queues up
1524    error messages, rather than issuing them immediately, and saves the
1525    tokens it consumes.  If the construct is parsed successfully, the
1526    parser "commits", i.e., it issues any queued error messages and
1527    the tokens that were being preserved are permanently discarded.
1528    If, however, the construct is not parsed successfully, the parser
1529    rolls back its state completely so that it can resume parsing using
1530    a different alternative.
1531
1532    Future Improvements
1533    -------------------
1534
1535    The performance of the parser could probably be improved substantially.
1536    We could often eliminate the need to parse tentatively by looking ahead
1537    a little bit.  In some places, this approach might not entirely eliminate
1538    the need to parse tentatively, but it might still speed up the average
1539    case.  */
1540
1541 /* Flags that are passed to some parsing functions.  These values can
1542    be bitwise-ored together.  */
1543
1544 enum
1545 {
1546   /* No flags.  */
1547   CP_PARSER_FLAGS_NONE = 0x0,
1548   /* The construct is optional.  If it is not present, then no error
1549      should be issued.  */
1550   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1551   /* When parsing a type-specifier, treat user-defined type-names
1552      as non-type identifiers.  */
1553   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1554   /* When parsing a type-specifier, do not try to parse a class-specifier
1555      or enum-specifier.  */
1556   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1557   /* When parsing a decl-specifier-seq, only allow type-specifier or
1558      constexpr.  */
1559   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1560 };
1561
1562 /* This type is used for parameters and variables which hold
1563    combinations of the above flags.  */
1564 typedef int cp_parser_flags;
1565
1566 /* The different kinds of declarators we want to parse.  */
1567
1568 typedef enum cp_parser_declarator_kind
1569 {
1570   /* We want an abstract declarator.  */
1571   CP_PARSER_DECLARATOR_ABSTRACT,
1572   /* We want a named declarator.  */
1573   CP_PARSER_DECLARATOR_NAMED,
1574   /* We don't mind, but the name must be an unqualified-id.  */
1575   CP_PARSER_DECLARATOR_EITHER
1576 } cp_parser_declarator_kind;
1577
1578 /* The precedence values used to parse binary expressions.  The minimum value
1579    of PREC must be 1, because zero is reserved to quickly discriminate
1580    binary operators from other tokens.  */
1581
1582 enum cp_parser_prec
1583 {
1584   PREC_NOT_OPERATOR,
1585   PREC_LOGICAL_OR_EXPRESSION,
1586   PREC_LOGICAL_AND_EXPRESSION,
1587   PREC_INCLUSIVE_OR_EXPRESSION,
1588   PREC_EXCLUSIVE_OR_EXPRESSION,
1589   PREC_AND_EXPRESSION,
1590   PREC_EQUALITY_EXPRESSION,
1591   PREC_RELATIONAL_EXPRESSION,
1592   PREC_SHIFT_EXPRESSION,
1593   PREC_ADDITIVE_EXPRESSION,
1594   PREC_MULTIPLICATIVE_EXPRESSION,
1595   PREC_PM_EXPRESSION,
1596   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1597 };
1598
1599 /* A mapping from a token type to a corresponding tree node type, with a
1600    precedence value.  */
1601
1602 typedef struct cp_parser_binary_operations_map_node
1603 {
1604   /* The token type.  */
1605   enum cpp_ttype token_type;
1606   /* The corresponding tree code.  */
1607   enum tree_code tree_type;
1608   /* The precedence of this operator.  */
1609   enum cp_parser_prec prec;
1610 } cp_parser_binary_operations_map_node;
1611
1612 typedef struct cp_parser_expression_stack_entry
1613 {
1614   /* Left hand side of the binary operation we are currently
1615      parsing.  */
1616   tree lhs;
1617   /* Original tree code for left hand side, if it was a binary
1618      expression itself (used for -Wparentheses).  */
1619   enum tree_code lhs_type;
1620   /* Tree code for the binary operation we are parsing.  */
1621   enum tree_code tree_type;
1622   /* Precedence of the binary operation we are parsing.  */
1623   enum cp_parser_prec prec;
1624 } cp_parser_expression_stack_entry;
1625
1626 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1627    entries because precedence levels on the stack are monotonically
1628    increasing.  */
1629 typedef struct cp_parser_expression_stack_entry
1630   cp_parser_expression_stack[NUM_PREC_VALUES];
1631
1632 /* Prototypes.  */
1633
1634 /* Constructors and destructors.  */
1635
1636 static cp_parser_context *cp_parser_context_new
1637   (cp_parser_context *);
1638
1639 /* Class variables.  */
1640
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1642
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644    Transformed into an associative array (binops_by_token) by
1645    cp_parser_new.  */
1646
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1650
1651   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654
1655   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657
1658   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660
1661   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665
1666   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1668
1669   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1670
1671   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1672
1673   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1674
1675   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1676
1677   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1678 };
1679
1680 /* The same as binops, but initialized by cp_parser_new so that
1681    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1682    for speed.  */
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1684
1685 /* Constructors and destructors.  */
1686
1687 /* Construct a new context.  The context below this one on the stack
1688    is given by NEXT.  */
1689
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1692 {
1693   cp_parser_context *context;
1694
1695   /* Allocate the storage.  */
1696   if (cp_parser_context_free_list != NULL)
1697     {
1698       /* Pull the first entry from the free list.  */
1699       context = cp_parser_context_free_list;
1700       cp_parser_context_free_list = context->next;
1701       memset (context, 0, sizeof (*context));
1702     }
1703   else
1704     context = ggc_alloc_cleared_cp_parser_context ();
1705
1706   /* No errors have occurred yet in this context.  */
1707   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708   /* If this is not the bottommost context, copy information that we
1709      need from the previous context.  */
1710   if (next)
1711     {
1712       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713          expression, then we are parsing one in this context, too.  */
1714       context->object_type = next->object_type;
1715       /* Thread the stack.  */
1716       context->next = next;
1717     }
1718
1719   return context;
1720 }
1721
1722 /* Managing the unparsed function queues.  */
1723
1724 #define unparsed_funs_with_default_args \
1725   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1728 #define unparsed_nsdmis \
1729   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1730
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1733 {
1734   VEC_safe_push (cp_unparsed_functions_entry, gc,
1735                  parser->unparsed_queues, NULL);
1736   unparsed_funs_with_default_args = NULL;
1737   unparsed_funs_with_definitions = make_tree_vector ();
1738   unparsed_nsdmis = NULL;
1739 }
1740
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1743 {
1744   release_tree_vector (unparsed_funs_with_definitions);
1745   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1746 }
1747
1748 /* Prototypes.  */
1749
1750 /* Constructors and destructors.  */
1751
1752 static cp_parser *cp_parser_new
1753   (void);
1754
1755 /* Routines to parse various constructs.
1756
1757    Those that return `tree' will return the error_mark_node (rather
1758    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759    Sometimes, they will return an ordinary node if error-recovery was
1760    attempted, even though a parse error occurred.  So, to check
1761    whether or not a parse error occurred, you should always use
1762    cp_parser_error_occurred.  If the construct is optional (indicated
1763    either by an `_opt' in the name of the function that does the
1764    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765    the construct is not present.  */
1766
1767 /* Lexical conventions [gram.lex]  */
1768
1769 static tree cp_parser_identifier
1770   (cp_parser *);
1771 static tree cp_parser_string_literal
1772   (cp_parser *, bool, bool);
1773 static tree cp_parser_userdef_char_literal
1774   (cp_parser *);
1775 static tree cp_parser_userdef_string_literal
1776   (cp_token *);
1777 static tree cp_parser_userdef_numeric_literal
1778   (cp_parser *);
1779
1780 /* Basic concepts [gram.basic]  */
1781
1782 static bool cp_parser_translation_unit
1783   (cp_parser *);
1784
1785 /* Expressions [gram.expr]  */
1786
1787 static tree cp_parser_primary_expression
1788   (cp_parser *, bool, bool, bool, cp_id_kind *);
1789 static tree cp_parser_id_expression
1790   (cp_parser *, bool, bool, bool *, bool, bool);
1791 static tree cp_parser_unqualified_id
1792   (cp_parser *, bool, bool, bool, bool);
1793 static tree cp_parser_nested_name_specifier_opt
1794   (cp_parser *, bool, bool, bool, bool);
1795 static tree cp_parser_nested_name_specifier
1796   (cp_parser *, bool, bool, bool, bool);
1797 static tree cp_parser_qualifying_entity
1798   (cp_parser *, bool, bool, bool, bool, bool);
1799 static tree cp_parser_postfix_expression
1800   (cp_parser *, bool, bool, bool, cp_id_kind *);
1801 static tree cp_parser_postfix_open_square_expression
1802   (cp_parser *, tree, bool);
1803 static tree cp_parser_postfix_dot_deref_expression
1804   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1805 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1806   (cp_parser *, int, bool, bool, bool *);
1807 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1808 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1809 static void cp_parser_pseudo_destructor_name
1810   (cp_parser *, tree *, tree *);
1811 static tree cp_parser_unary_expression
1812   (cp_parser *, bool, bool, cp_id_kind *);
1813 static enum tree_code cp_parser_unary_operator
1814   (cp_token *);
1815 static tree cp_parser_new_expression
1816   (cp_parser *);
1817 static VEC(tree,gc) *cp_parser_new_placement
1818   (cp_parser *);
1819 static tree cp_parser_new_type_id
1820   (cp_parser *, tree *);
1821 static cp_declarator *cp_parser_new_declarator_opt
1822   (cp_parser *);
1823 static cp_declarator *cp_parser_direct_new_declarator
1824   (cp_parser *);
1825 static VEC(tree,gc) *cp_parser_new_initializer
1826   (cp_parser *);
1827 static tree cp_parser_delete_expression
1828   (cp_parser *);
1829 static tree cp_parser_cast_expression
1830   (cp_parser *, bool, bool, cp_id_kind *);
1831 static tree cp_parser_binary_expression
1832   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1833 static tree cp_parser_question_colon_clause
1834   (cp_parser *, tree);
1835 static tree cp_parser_assignment_expression
1836   (cp_parser *, bool, cp_id_kind *);
1837 static enum tree_code cp_parser_assignment_operator_opt
1838   (cp_parser *);
1839 static tree cp_parser_expression
1840   (cp_parser *, bool, cp_id_kind *);
1841 static tree cp_parser_constant_expression
1842   (cp_parser *, bool, bool *);
1843 static tree cp_parser_builtin_offsetof
1844   (cp_parser *);
1845 static tree cp_parser_lambda_expression
1846   (cp_parser *);
1847 static void cp_parser_lambda_introducer
1848   (cp_parser *, tree);
1849 static bool cp_parser_lambda_declarator_opt
1850   (cp_parser *, tree);
1851 static void cp_parser_lambda_body
1852   (cp_parser *, tree);
1853
1854 /* Statements [gram.stmt.stmt]  */
1855
1856 static void cp_parser_statement
1857   (cp_parser *, tree, bool, bool *);
1858 static void cp_parser_label_for_labeled_statement
1859   (cp_parser *);
1860 static tree cp_parser_expression_statement
1861   (cp_parser *, tree);
1862 static tree cp_parser_compound_statement
1863   (cp_parser *, tree, bool, bool);
1864 static void cp_parser_statement_seq_opt
1865   (cp_parser *, tree);
1866 static tree cp_parser_selection_statement
1867   (cp_parser *, bool *);
1868 static tree cp_parser_condition
1869   (cp_parser *);
1870 static tree cp_parser_iteration_statement
1871   (cp_parser *);
1872 static bool cp_parser_for_init_statement
1873   (cp_parser *, tree *decl);
1874 static tree cp_parser_for
1875   (cp_parser *);
1876 static tree cp_parser_c_for
1877   (cp_parser *, tree, tree);
1878 static tree cp_parser_range_for
1879   (cp_parser *, tree, tree, tree);
1880 static void do_range_for_auto_deduction
1881   (tree, tree);
1882 static tree cp_parser_perform_range_for_lookup
1883   (tree, tree *, tree *);
1884 static tree cp_parser_range_for_member_function
1885   (tree, tree);
1886 static tree cp_parser_jump_statement
1887   (cp_parser *);
1888 static void cp_parser_declaration_statement
1889   (cp_parser *);
1890
1891 static tree cp_parser_implicitly_scoped_statement
1892   (cp_parser *, bool *);
1893 static void cp_parser_already_scoped_statement
1894   (cp_parser *);
1895
1896 /* Declarations [gram.dcl.dcl] */
1897
1898 static void cp_parser_declaration_seq_opt
1899   (cp_parser *);
1900 static void cp_parser_declaration
1901   (cp_parser *);
1902 static void cp_parser_block_declaration
1903   (cp_parser *, bool);
1904 static void cp_parser_simple_declaration
1905   (cp_parser *, bool, tree *);
1906 static void cp_parser_decl_specifier_seq
1907   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1908 static tree cp_parser_storage_class_specifier_opt
1909   (cp_parser *);
1910 static tree cp_parser_function_specifier_opt
1911   (cp_parser *, cp_decl_specifier_seq *);
1912 static tree cp_parser_type_specifier
1913   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1914    int *, bool *);
1915 static tree cp_parser_simple_type_specifier
1916   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1917 static tree cp_parser_type_name
1918   (cp_parser *);
1919 static tree cp_parser_nonclass_name 
1920   (cp_parser* parser);
1921 static tree cp_parser_elaborated_type_specifier
1922   (cp_parser *, bool, bool);
1923 static tree cp_parser_enum_specifier
1924   (cp_parser *);
1925 static void cp_parser_enumerator_list
1926   (cp_parser *, tree);
1927 static void cp_parser_enumerator_definition
1928   (cp_parser *, tree);
1929 static tree cp_parser_namespace_name
1930   (cp_parser *);
1931 static void cp_parser_namespace_definition
1932   (cp_parser *);
1933 static void cp_parser_namespace_body
1934   (cp_parser *);
1935 static tree cp_parser_qualified_namespace_specifier
1936   (cp_parser *);
1937 static void cp_parser_namespace_alias_definition
1938   (cp_parser *);
1939 static bool cp_parser_using_declaration
1940   (cp_parser *, bool);
1941 static void cp_parser_using_directive
1942   (cp_parser *);
1943 static tree cp_parser_alias_declaration
1944   (cp_parser *);
1945 static void cp_parser_asm_definition
1946   (cp_parser *);
1947 static void cp_parser_linkage_specification
1948   (cp_parser *);
1949 static void cp_parser_static_assert
1950   (cp_parser *, bool);
1951 static tree cp_parser_decltype
1952   (cp_parser *);
1953
1954 /* Declarators [gram.dcl.decl] */
1955
1956 static tree cp_parser_init_declarator
1957   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1958 static cp_declarator *cp_parser_declarator
1959   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1960 static cp_declarator *cp_parser_direct_declarator
1961   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1962 static enum tree_code cp_parser_ptr_operator
1963   (cp_parser *, tree *, cp_cv_quals *);
1964 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1965   (cp_parser *);
1966 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1967   (cp_parser *);
1968 static tree cp_parser_late_return_type_opt
1969   (cp_parser *, cp_cv_quals);
1970 static tree cp_parser_declarator_id
1971   (cp_parser *, bool);
1972 static tree cp_parser_type_id
1973   (cp_parser *);
1974 static tree cp_parser_template_type_arg
1975   (cp_parser *);
1976 static tree cp_parser_trailing_type_id (cp_parser *);
1977 static tree cp_parser_type_id_1
1978   (cp_parser *, bool, bool);
1979 static void cp_parser_type_specifier_seq
1980   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1981 static tree cp_parser_parameter_declaration_clause
1982   (cp_parser *);
1983 static tree cp_parser_parameter_declaration_list
1984   (cp_parser *, bool *);
1985 static cp_parameter_declarator *cp_parser_parameter_declaration
1986   (cp_parser *, bool, bool *);
1987 static tree cp_parser_default_argument 
1988   (cp_parser *, bool);
1989 static void cp_parser_function_body
1990   (cp_parser *);
1991 static tree cp_parser_initializer
1992   (cp_parser *, bool *, bool *);
1993 static tree cp_parser_initializer_clause
1994   (cp_parser *, bool *);
1995 static tree cp_parser_braced_list
1996   (cp_parser*, bool*);
1997 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1998   (cp_parser *, bool *);
1999
2000 static bool cp_parser_ctor_initializer_opt_and_function_body
2001   (cp_parser *);
2002
2003 /* Classes [gram.class] */
2004
2005 static tree cp_parser_class_name
2006   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2007 static tree cp_parser_class_specifier
2008   (cp_parser *);
2009 static tree cp_parser_class_head
2010   (cp_parser *, bool *, tree *, tree *);
2011 static enum tag_types cp_parser_class_key
2012   (cp_parser *);
2013 static void cp_parser_member_specification_opt
2014   (cp_parser *);
2015 static void cp_parser_member_declaration
2016   (cp_parser *);
2017 static tree cp_parser_pure_specifier
2018   (cp_parser *);
2019 static tree cp_parser_constant_initializer
2020   (cp_parser *);
2021
2022 /* Derived classes [gram.class.derived] */
2023
2024 static tree cp_parser_base_clause
2025   (cp_parser *);
2026 static tree cp_parser_base_specifier
2027   (cp_parser *);
2028
2029 /* Special member functions [gram.special] */
2030
2031 static tree cp_parser_conversion_function_id
2032   (cp_parser *);
2033 static tree cp_parser_conversion_type_id
2034   (cp_parser *);
2035 static cp_declarator *cp_parser_conversion_declarator_opt
2036   (cp_parser *);
2037 static bool cp_parser_ctor_initializer_opt
2038   (cp_parser *);
2039 static void cp_parser_mem_initializer_list
2040   (cp_parser *);
2041 static tree cp_parser_mem_initializer
2042   (cp_parser *);
2043 static tree cp_parser_mem_initializer_id
2044   (cp_parser *);
2045
2046 /* Overloading [gram.over] */
2047
2048 static tree cp_parser_operator_function_id
2049   (cp_parser *);
2050 static tree cp_parser_operator
2051   (cp_parser *);
2052
2053 /* Templates [gram.temp] */
2054
2055 static void cp_parser_template_declaration
2056   (cp_parser *, bool);
2057 static tree cp_parser_template_parameter_list
2058   (cp_parser *);
2059 static tree cp_parser_template_parameter
2060   (cp_parser *, bool *, bool *);
2061 static tree cp_parser_type_parameter
2062   (cp_parser *, bool *);
2063 static tree cp_parser_template_id
2064   (cp_parser *, bool, bool, bool);
2065 static tree cp_parser_template_name
2066   (cp_parser *, bool, bool, bool, bool *);
2067 static tree cp_parser_template_argument_list
2068   (cp_parser *);
2069 static tree cp_parser_template_argument
2070   (cp_parser *);
2071 static void cp_parser_explicit_instantiation
2072   (cp_parser *);
2073 static void cp_parser_explicit_specialization
2074   (cp_parser *);
2075
2076 /* Exception handling [gram.exception] */
2077
2078 static tree cp_parser_try_block
2079   (cp_parser *);
2080 static bool cp_parser_function_try_block
2081   (cp_parser *);
2082 static void cp_parser_handler_seq
2083   (cp_parser *);
2084 static void cp_parser_handler
2085   (cp_parser *);
2086 static tree cp_parser_exception_declaration
2087   (cp_parser *);
2088 static tree cp_parser_throw_expression
2089   (cp_parser *);
2090 static tree cp_parser_exception_specification_opt
2091   (cp_parser *);
2092 static tree cp_parser_type_id_list
2093   (cp_parser *);
2094
2095 /* GNU Extensions */
2096
2097 static tree cp_parser_asm_specification_opt
2098   (cp_parser *);
2099 static tree cp_parser_asm_operand_list
2100   (cp_parser *);
2101 static tree cp_parser_asm_clobber_list
2102   (cp_parser *);
2103 static tree cp_parser_asm_label_list
2104   (cp_parser *);
2105 static tree cp_parser_attributes_opt
2106   (cp_parser *);
2107 static tree cp_parser_attribute_list
2108   (cp_parser *);
2109 static bool cp_parser_extension_opt
2110   (cp_parser *, int *);
2111 static void cp_parser_label_declaration
2112   (cp_parser *);
2113
2114 /* Transactional Memory Extensions */
2115
2116 static tree cp_parser_transaction
2117   (cp_parser *, enum rid);
2118 static tree cp_parser_transaction_expression
2119   (cp_parser *, enum rid);
2120 static bool cp_parser_function_transaction
2121   (cp_parser *, enum rid);
2122 static tree cp_parser_transaction_cancel
2123   (cp_parser *);
2124
2125 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2126 static bool cp_parser_pragma
2127   (cp_parser *, enum pragma_context);
2128
2129 /* Objective-C++ Productions */
2130
2131 static tree cp_parser_objc_message_receiver
2132   (cp_parser *);
2133 static tree cp_parser_objc_message_args
2134   (cp_parser *);
2135 static tree cp_parser_objc_message_expression
2136   (cp_parser *);
2137 static tree cp_parser_objc_encode_expression
2138   (cp_parser *);
2139 static tree cp_parser_objc_defs_expression
2140   (cp_parser *);
2141 static tree cp_parser_objc_protocol_expression
2142   (cp_parser *);
2143 static tree cp_parser_objc_selector_expression
2144   (cp_parser *);
2145 static tree cp_parser_objc_expression
2146   (cp_parser *);
2147 static bool cp_parser_objc_selector_p
2148   (enum cpp_ttype);
2149 static tree cp_parser_objc_selector
2150   (cp_parser *);
2151 static tree cp_parser_objc_protocol_refs_opt
2152   (cp_parser *);
2153 static void cp_parser_objc_declaration
2154   (cp_parser *, tree);
2155 static tree cp_parser_objc_statement
2156   (cp_parser *);
2157 static bool cp_parser_objc_valid_prefix_attributes
2158   (cp_parser *, tree *);
2159 static void cp_parser_objc_at_property_declaration 
2160   (cp_parser *) ;
2161 static void cp_parser_objc_at_synthesize_declaration 
2162   (cp_parser *) ;
2163 static void cp_parser_objc_at_dynamic_declaration
2164   (cp_parser *) ;
2165 static tree cp_parser_objc_struct_declaration
2166   (cp_parser *) ;
2167
2168 /* Utility Routines */
2169
2170 static tree cp_parser_lookup_name
2171   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2172 static tree cp_parser_lookup_name_simple
2173   (cp_parser *, tree, location_t);
2174 static tree cp_parser_maybe_treat_template_as_class
2175   (tree, bool);
2176 static bool cp_parser_check_declarator_template_parameters
2177   (cp_parser *, cp_declarator *, location_t);
2178 static bool cp_parser_check_template_parameters
2179   (cp_parser *, unsigned, location_t, cp_declarator *);
2180 static tree cp_parser_simple_cast_expression
2181   (cp_parser *);
2182 static tree cp_parser_global_scope_opt
2183   (cp_parser *, bool);
2184 static bool cp_parser_constructor_declarator_p
2185   (cp_parser *, bool);
2186 static tree cp_parser_function_definition_from_specifiers_and_declarator
2187   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2188 static tree cp_parser_function_definition_after_declarator
2189   (cp_parser *, bool);
2190 static void cp_parser_template_declaration_after_export
2191   (cp_parser *, bool);
2192 static void cp_parser_perform_template_parameter_access_checks
2193   (VEC (deferred_access_check,gc)*);
2194 static tree cp_parser_single_declaration
2195   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2196 static tree cp_parser_functional_cast
2197   (cp_parser *, tree);
2198 static tree cp_parser_save_member_function_body
2199   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2200 static tree cp_parser_save_nsdmi
2201   (cp_parser *);
2202 static tree cp_parser_enclosed_template_argument_list
2203   (cp_parser *);
2204 static void cp_parser_save_default_args
2205   (cp_parser *, tree);
2206 static void cp_parser_late_parsing_for_member
2207   (cp_parser *, tree);
2208 static tree cp_parser_late_parse_one_default_arg
2209   (cp_parser *, tree, tree, tree);
2210 static void cp_parser_late_parsing_nsdmi
2211   (cp_parser *, tree);
2212 static void cp_parser_late_parsing_default_args
2213   (cp_parser *, tree);
2214 static tree cp_parser_sizeof_operand
2215   (cp_parser *, enum rid);
2216 static tree cp_parser_trait_expr
2217   (cp_parser *, enum rid);
2218 static bool cp_parser_declares_only_class_p
2219   (cp_parser *);
2220 static void cp_parser_set_storage_class
2221   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2222 static void cp_parser_set_decl_spec_type
2223   (cp_decl_specifier_seq *, tree, location_t, bool);
2224 static bool cp_parser_friend_p
2225   (const cp_decl_specifier_seq *);
2226 static void cp_parser_required_error
2227   (cp_parser *, required_token, bool);
2228 static cp_token *cp_parser_require
2229   (cp_parser *, enum cpp_ttype, required_token);
2230 static cp_token *cp_parser_require_keyword
2231   (cp_parser *, enum rid, required_token);
2232 static bool cp_parser_token_starts_function_definition_p
2233   (cp_token *);
2234 static bool cp_parser_next_token_starts_class_definition_p
2235   (cp_parser *);
2236 static bool cp_parser_next_token_ends_template_argument_p
2237   (cp_parser *);
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239   (cp_parser *, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2241   (cp_token *);
2242 static void cp_parser_check_class_key
2243   (enum tag_types, tree type);
2244 static void cp_parser_check_access_in_redeclaration
2245   (tree type, location_t location);
2246 static bool cp_parser_optional_template_keyword
2247   (cp_parser *);
2248 static void cp_parser_pre_parsed_nested_name_specifier
2249   (cp_parser *);
2250 static bool cp_parser_cache_group
2251   (cp_parser *, enum cpp_ttype, unsigned);
2252 static tree cp_parser_cache_defarg
2253   (cp_parser *parser, bool nsdmi);
2254 static void cp_parser_parse_tentatively
2255   (cp_parser *);
2256 static void cp_parser_commit_to_tentative_parse
2257   (cp_parser *);
2258 static void cp_parser_abort_tentative_parse
2259   (cp_parser *);
2260 static bool cp_parser_parse_definitely
2261   (cp_parser *);
2262 static inline bool cp_parser_parsing_tentatively
2263   (cp_parser *);
2264 static bool cp_parser_uncommitted_to_tentative_parse_p
2265   (cp_parser *);
2266 static void cp_parser_error
2267   (cp_parser *, const char *);
2268 static void cp_parser_name_lookup_error
2269   (cp_parser *, tree, tree, name_lookup_error, location_t);
2270 static bool cp_parser_simulate_error
2271   (cp_parser *);
2272 static bool cp_parser_check_type_definition
2273   (cp_parser *);
2274 static void cp_parser_check_for_definition_in_return_type
2275   (cp_declarator *, tree, location_t type_location);
2276 static void cp_parser_check_for_invalid_template_id
2277   (cp_parser *, tree, location_t location);
2278 static bool cp_parser_non_integral_constant_expression
2279   (cp_parser *, non_integral_constant);
2280 static void cp_parser_diagnose_invalid_type_name
2281   (cp_parser *, tree, tree, location_t);
2282 static bool cp_parser_parse_and_diagnose_invalid_type_name
2283   (cp_parser *);
2284 static int cp_parser_skip_to_closing_parenthesis
2285   (cp_parser *, bool, bool, bool);
2286 static void cp_parser_skip_to_end_of_statement
2287   (cp_parser *);
2288 static void cp_parser_consume_semicolon_at_end_of_statement
2289   (cp_parser *);
2290 static void cp_parser_skip_to_end_of_block_or_statement
2291   (cp_parser *);
2292 static bool cp_parser_skip_to_closing_brace
2293   (cp_parser *);
2294 static void cp_parser_skip_to_end_of_template_parameter_list
2295   (cp_parser *);
2296 static void cp_parser_skip_to_pragma_eol
2297   (cp_parser*, cp_token *);
2298 static bool cp_parser_error_occurred
2299   (cp_parser *);
2300 static bool cp_parser_allow_gnu_extensions_p
2301   (cp_parser *);
2302 static bool cp_parser_is_pure_string_literal
2303   (cp_token *);
2304 static bool cp_parser_is_string_literal
2305   (cp_token *);
2306 static bool cp_parser_is_keyword
2307   (cp_token *, enum rid);
2308 static tree cp_parser_make_typename_type
2309   (cp_parser *, tree, tree, location_t location);
2310 static cp_declarator * cp_parser_make_indirect_declarator
2311   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2312
2313 /* Returns nonzero if we are parsing tentatively.  */
2314
2315 static inline bool
2316 cp_parser_parsing_tentatively (cp_parser* parser)
2317 {
2318   return parser->context->next != NULL;
2319 }
2320
2321 /* Returns nonzero if TOKEN is a string literal.  */
2322
2323 static bool
2324 cp_parser_is_pure_string_literal (cp_token* token)
2325 {
2326   return (token->type == CPP_STRING ||
2327           token->type == CPP_STRING16 ||
2328           token->type == CPP_STRING32 ||
2329           token->type == CPP_WSTRING ||
2330           token->type == CPP_UTF8STRING);
2331 }
2332
2333 /* Returns nonzero if TOKEN is a string literal
2334    of a user-defined string literal.  */
2335
2336 static bool
2337 cp_parser_is_string_literal (cp_token* token)
2338 {
2339   return (cp_parser_is_pure_string_literal (token) ||
2340           token->type == CPP_STRING_USERDEF ||
2341           token->type == CPP_STRING16_USERDEF ||
2342           token->type == CPP_STRING32_USERDEF ||
2343           token->type == CPP_WSTRING_USERDEF ||
2344           token->type == CPP_UTF8STRING_USERDEF);
2345 }
2346
2347 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2348
2349 static bool
2350 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2351 {
2352   return token->keyword == keyword;
2353 }
2354
2355 /* If not parsing tentatively, issue a diagnostic of the form
2356       FILE:LINE: MESSAGE before TOKEN
2357    where TOKEN is the next token in the input stream.  MESSAGE
2358    (specified by the caller) is usually of the form "expected
2359    OTHER-TOKEN".  */
2360
2361 static void
2362 cp_parser_error (cp_parser* parser, const char* gmsgid)
2363 {
2364   if (!cp_parser_simulate_error (parser))
2365     {
2366       cp_token *token = cp_lexer_peek_token (parser->lexer);
2367       /* This diagnostic makes more sense if it is tagged to the line
2368          of the token we just peeked at.  */
2369       cp_lexer_set_source_position_from_token (token);
2370
2371       if (token->type == CPP_PRAGMA)
2372         {
2373           error_at (token->location,
2374                     "%<#pragma%> is not allowed here");
2375           cp_parser_skip_to_pragma_eol (parser, token);
2376           return;
2377         }
2378
2379       c_parse_error (gmsgid,
2380                      /* Because c_parser_error does not understand
2381                         CPP_KEYWORD, keywords are treated like
2382                         identifiers.  */
2383                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2384                      token->u.value, token->flags);
2385     }
2386 }
2387
2388 /* Issue an error about name-lookup failing.  NAME is the
2389    IDENTIFIER_NODE DECL is the result of
2390    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2391    the thing that we hoped to find.  */
2392
2393 static void
2394 cp_parser_name_lookup_error (cp_parser* parser,
2395                              tree name,
2396                              tree decl,
2397                              name_lookup_error desired,
2398                              location_t location)
2399 {
2400   /* If name lookup completely failed, tell the user that NAME was not
2401      declared.  */
2402   if (decl == error_mark_node)
2403     {
2404       if (parser->scope && parser->scope != global_namespace)
2405         error_at (location, "%<%E::%E%> has not been declared",
2406                   parser->scope, name);
2407       else if (parser->scope == global_namespace)
2408         error_at (location, "%<::%E%> has not been declared", name);
2409       else if (parser->object_scope
2410                && !CLASS_TYPE_P (parser->object_scope))
2411         error_at (location, "request for member %qE in non-class type %qT",
2412                   name, parser->object_scope);
2413       else if (parser->object_scope)
2414         error_at (location, "%<%T::%E%> has not been declared",
2415                   parser->object_scope, name);
2416       else
2417         error_at (location, "%qE has not been declared", name);
2418     }
2419   else if (parser->scope && parser->scope != global_namespace)
2420     {
2421       switch (desired)
2422         {
2423           case NLE_TYPE:
2424             error_at (location, "%<%E::%E%> is not a type",
2425                                 parser->scope, name);
2426             break;
2427           case NLE_CXX98:
2428             error_at (location, "%<%E::%E%> is not a class or namespace",
2429                                 parser->scope, name);
2430             break;
2431           case NLE_NOT_CXX98:
2432             error_at (location,
2433                       "%<%E::%E%> is not a class, namespace, or enumeration",
2434                       parser->scope, name);
2435             break;
2436           default:
2437             gcc_unreachable ();
2438             
2439         }
2440     }
2441   else if (parser->scope == global_namespace)
2442     {
2443       switch (desired)
2444         {
2445           case NLE_TYPE:
2446             error_at (location, "%<::%E%> is not a type", name);
2447             break;
2448           case NLE_CXX98:
2449             error_at (location, "%<::%E%> is not a class or namespace", name);
2450             break;
2451           case NLE_NOT_CXX98:
2452             error_at (location,
2453                       "%<::%E%> is not a class, namespace, or enumeration",
2454                       name);
2455             break;
2456           default:
2457             gcc_unreachable ();
2458         }
2459     }
2460   else
2461     {
2462       switch (desired)
2463         {
2464           case NLE_TYPE:
2465             error_at (location, "%qE is not a type", name);
2466             break;
2467           case NLE_CXX98:
2468             error_at (location, "%qE is not a class or namespace", name);
2469             break;
2470           case NLE_NOT_CXX98:
2471             error_at (location,
2472                       "%qE is not a class, namespace, or enumeration", name);
2473             break;
2474           default:
2475             gcc_unreachable ();
2476         }
2477     }
2478 }
2479
2480 /* If we are parsing tentatively, remember that an error has occurred
2481    during this tentative parse.  Returns true if the error was
2482    simulated; false if a message should be issued by the caller.  */
2483
2484 static bool
2485 cp_parser_simulate_error (cp_parser* parser)
2486 {
2487   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2488     {
2489       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2490       return true;
2491     }
2492   return false;
2493 }
2494
2495 /* Check for repeated decl-specifiers.  */
2496
2497 static void
2498 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2499                            location_t location)
2500 {
2501   int ds;
2502
2503   for (ds = ds_first; ds != ds_last; ++ds)
2504     {
2505       unsigned count = decl_specs->specs[ds];
2506       if (count < 2)
2507         continue;
2508       /* The "long" specifier is a special case because of "long long".  */
2509       if (ds == ds_long)
2510         {
2511           if (count > 2)
2512             error_at (location, "%<long long long%> is too long for GCC");
2513           else 
2514             pedwarn_cxx98 (location, OPT_Wlong_long, 
2515                            "ISO C++ 1998 does not support %<long long%>");
2516         }
2517       else if (count > 1)
2518         {
2519           static const char *const decl_spec_names[] = {
2520             "signed",
2521             "unsigned",
2522             "short",
2523             "long",
2524             "const",
2525             "volatile",
2526             "restrict",
2527             "inline",
2528             "virtual",
2529             "explicit",
2530             "friend",
2531             "typedef",
2532             "using",
2533             "constexpr",
2534             "__complex",
2535             "__thread"
2536           };
2537           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2538         }
2539     }
2540 }
2541
2542 /* This function is called when a type is defined.  If type
2543    definitions are forbidden at this point, an error message is
2544    issued.  */
2545
2546 static bool
2547 cp_parser_check_type_definition (cp_parser* parser)
2548 {
2549   /* If types are forbidden here, issue a message.  */
2550   if (parser->type_definition_forbidden_message)
2551     {
2552       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2553          in the message need to be interpreted.  */
2554       error (parser->type_definition_forbidden_message);
2555       return false;
2556     }
2557   return true;
2558 }
2559
2560 /* This function is called when the DECLARATOR is processed.  The TYPE
2561    was a type defined in the decl-specifiers.  If it is invalid to
2562    define a type in the decl-specifiers for DECLARATOR, an error is
2563    issued. TYPE_LOCATION is the location of TYPE and is used
2564    for error reporting.  */
2565
2566 static void
2567 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2568                                                tree type, location_t type_location)
2569 {
2570   /* [dcl.fct] forbids type definitions in return types.
2571      Unfortunately, it's not easy to know whether or not we are
2572      processing a return type until after the fact.  */
2573   while (declarator
2574          && (declarator->kind == cdk_pointer
2575              || declarator->kind == cdk_reference
2576              || declarator->kind == cdk_ptrmem))
2577     declarator = declarator->declarator;
2578   if (declarator
2579       && declarator->kind == cdk_function)
2580     {
2581       error_at (type_location,
2582                 "new types may not be defined in a return type");
2583       inform (type_location, 
2584               "(perhaps a semicolon is missing after the definition of %qT)",
2585               type);
2586     }
2587 }
2588
2589 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2590    "<" in any valid C++ program.  If the next token is indeed "<",
2591    issue a message warning the user about what appears to be an
2592    invalid attempt to form a template-id. LOCATION is the location
2593    of the type-specifier (TYPE) */
2594
2595 static void
2596 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2597                                          tree type, location_t location)
2598 {
2599   cp_token_position start = 0;
2600
2601   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2602     {
2603       if (TYPE_P (type))
2604         error_at (location, "%qT is not a template", type);
2605       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2606         error_at (location, "%qE is not a template", type);
2607       else
2608         error_at (location, "invalid template-id");
2609       /* Remember the location of the invalid "<".  */
2610       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2611         start = cp_lexer_token_position (parser->lexer, true);
2612       /* Consume the "<".  */
2613       cp_lexer_consume_token (parser->lexer);
2614       /* Parse the template arguments.  */
2615       cp_parser_enclosed_template_argument_list (parser);
2616       /* Permanently remove the invalid template arguments so that
2617          this error message is not issued again.  */
2618       if (start)
2619         cp_lexer_purge_tokens_after (parser->lexer, start);
2620     }
2621 }
2622
2623 /* If parsing an integral constant-expression, issue an error message
2624    about the fact that THING appeared and return true.  Otherwise,
2625    return false.  In either case, set
2626    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2627
2628 static bool
2629 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2630                                             non_integral_constant thing)
2631 {
2632   parser->non_integral_constant_expression_p = true;
2633   if (parser->integral_constant_expression_p)
2634     {
2635       if (!parser->allow_non_integral_constant_expression_p)
2636         {
2637           const char *msg = NULL;
2638           switch (thing)
2639             {
2640               case NIC_FLOAT:
2641                 error ("floating-point literal "
2642                        "cannot appear in a constant-expression");
2643                 return true;
2644               case NIC_CAST:
2645                 error ("a cast to a type other than an integral or "
2646                        "enumeration type cannot appear in a "
2647                        "constant-expression");
2648                 return true;
2649               case NIC_TYPEID:
2650                 error ("%<typeid%> operator "
2651                        "cannot appear in a constant-expression");
2652                 return true;
2653               case NIC_NCC:
2654                 error ("non-constant compound literals "
2655                        "cannot appear in a constant-expression");
2656                 return true;
2657               case NIC_FUNC_CALL:
2658                 error ("a function call "
2659                        "cannot appear in a constant-expression");
2660                 return true;
2661               case NIC_INC:
2662                 error ("an increment "
2663                        "cannot appear in a constant-expression");
2664                 return true;
2665               case NIC_DEC:
2666                 error ("an decrement "
2667                        "cannot appear in a constant-expression");
2668                 return true;
2669               case NIC_ARRAY_REF:
2670                 error ("an array reference "
2671                        "cannot appear in a constant-expression");
2672                 return true;
2673               case NIC_ADDR_LABEL:
2674                 error ("the address of a label "
2675                        "cannot appear in a constant-expression");
2676                 return true;
2677               case NIC_OVERLOADED:
2678                 error ("calls to overloaded operators "
2679                        "cannot appear in a constant-expression");
2680                 return true;
2681               case NIC_ASSIGNMENT:
2682                 error ("an assignment cannot appear in a constant-expression");
2683                 return true;
2684               case NIC_COMMA:
2685                 error ("a comma operator "
2686                        "cannot appear in a constant-expression");
2687                 return true;
2688               case NIC_CONSTRUCTOR:
2689                 error ("a call to a constructor "
2690                        "cannot appear in a constant-expression");
2691                 return true;
2692               case NIC_TRANSACTION:
2693                 error ("a transaction expression "
2694                        "cannot appear in a constant-expression");
2695                 return true;
2696               case NIC_THIS:
2697                 msg = "this";
2698                 break;
2699               case NIC_FUNC_NAME:
2700                 msg = "__FUNCTION__";
2701                 break;
2702               case NIC_PRETTY_FUNC:
2703                 msg = "__PRETTY_FUNCTION__";
2704                 break;
2705               case NIC_C99_FUNC:
2706                 msg = "__func__";
2707                 break;
2708               case NIC_VA_ARG:
2709                 msg = "va_arg";
2710                 break;
2711               case NIC_ARROW:
2712                 msg = "->";
2713                 break;
2714               case NIC_POINT:
2715                 msg = ".";
2716                 break;
2717               case NIC_STAR:
2718                 msg = "*";
2719                 break;
2720               case NIC_ADDR:
2721                 msg = "&";
2722                 break;
2723               case NIC_PREINCREMENT:
2724                 msg = "++";
2725                 break;
2726               case NIC_PREDECREMENT:
2727                 msg = "--";
2728                 break;
2729               case NIC_NEW:
2730                 msg = "new";
2731                 break;
2732               case NIC_DEL:
2733                 msg = "delete";
2734                 break;
2735               default:
2736                 gcc_unreachable ();
2737             }
2738           if (msg)
2739             error ("%qs cannot appear in a constant-expression", msg);
2740           return true;
2741         }
2742     }
2743   return false;
2744 }
2745
2746 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2747    qualifying scope (or NULL, if none) for ID.  This function commits
2748    to the current active tentative parse, if any.  (Otherwise, the
2749    problematic construct might be encountered again later, resulting
2750    in duplicate error messages.) LOCATION is the location of ID.  */
2751
2752 static void
2753 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2754                                       tree scope, tree id,
2755                                       location_t location)
2756 {
2757   tree decl, old_scope;
2758   cp_parser_commit_to_tentative_parse (parser);
2759   /* Try to lookup the identifier.  */
2760   old_scope = parser->scope;
2761   parser->scope = scope;
2762   decl = cp_parser_lookup_name_simple (parser, id, location);
2763   parser->scope = old_scope;
2764   /* If the lookup found a template-name, it means that the user forgot
2765   to specify an argument list. Emit a useful error message.  */
2766   if (TREE_CODE (decl) == TEMPLATE_DECL)
2767     error_at (location,
2768               "invalid use of template-name %qE without an argument list",
2769               decl);
2770   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2771     error_at (location, "invalid use of destructor %qD as a type", id);
2772   else if (TREE_CODE (decl) == TYPE_DECL)
2773     /* Something like 'unsigned A a;'  */
2774     error_at (location, "invalid combination of multiple type-specifiers");
2775   else if (!parser->scope)
2776     {
2777       /* Issue an error message.  */
2778       error_at (location, "%qE does not name a type", id);
2779       /* If we're in a template class, it's possible that the user was
2780          referring to a type from a base class.  For example:
2781
2782            template <typename T> struct A { typedef T X; };
2783            template <typename T> struct B : public A<T> { X x; };
2784
2785          The user should have said "typename A<T>::X".  */
2786       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2787         inform (location, "C++11 %<constexpr%> only available with "
2788                 "-std=c++11 or -std=gnu++11");
2789       else if (processing_template_decl && current_class_type
2790                && TYPE_BINFO (current_class_type))
2791         {
2792           tree b;
2793
2794           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2795                b;
2796                b = TREE_CHAIN (b))
2797             {
2798               tree base_type = BINFO_TYPE (b);
2799               if (CLASS_TYPE_P (base_type)
2800                   && dependent_type_p (base_type))
2801                 {
2802                   tree field;
2803                   /* Go from a particular instantiation of the
2804                      template (which will have an empty TYPE_FIELDs),
2805                      to the main version.  */
2806                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2807                   for (field = TYPE_FIELDS (base_type);
2808                        field;
2809                        field = DECL_CHAIN (field))
2810                     if (TREE_CODE (field) == TYPE_DECL
2811                         && DECL_NAME (field) == id)
2812                       {
2813                         inform (location, 
2814                                 "(perhaps %<typename %T::%E%> was intended)",
2815                                 BINFO_TYPE (b), id);
2816                         break;
2817                       }
2818                   if (field)
2819                     break;
2820                 }
2821             }
2822         }
2823     }
2824   /* Here we diagnose qualified-ids where the scope is actually correct,
2825      but the identifier does not resolve to a valid type name.  */
2826   else if (parser->scope != error_mark_node)
2827     {
2828       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2829         error_at (location, "%qE in namespace %qE does not name a type",
2830                   id, parser->scope);
2831       else if (CLASS_TYPE_P (parser->scope)
2832                && constructor_name_p (id, parser->scope))
2833         {
2834           /* A<T>::A<T>() */
2835           error_at (location, "%<%T::%E%> names the constructor, not"
2836                     " the type", parser->scope, id);
2837           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2838             error_at (location, "and %qT has no template constructors",
2839                       parser->scope);
2840         }
2841       else if (TYPE_P (parser->scope)
2842                && dependent_scope_p (parser->scope))
2843         error_at (location, "need %<typename%> before %<%T::%E%> because "
2844                   "%qT is a dependent scope",
2845                   parser->scope, id, parser->scope);
2846       else if (TYPE_P (parser->scope))
2847         error_at (location, "%qE in %q#T does not name a type",
2848                   id, parser->scope);
2849       else
2850         gcc_unreachable ();
2851     }
2852 }
2853
2854 /* Check for a common situation where a type-name should be present,
2855    but is not, and issue a sensible error message.  Returns true if an
2856    invalid type-name was detected.
2857
2858    The situation handled by this function are variable declarations of the
2859    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2860    Usually, `ID' should name a type, but if we got here it means that it
2861    does not. We try to emit the best possible error message depending on
2862    how exactly the id-expression looks like.  */
2863
2864 static bool
2865 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2866 {
2867   tree id;
2868   cp_token *token = cp_lexer_peek_token (parser->lexer);
2869
2870   /* Avoid duplicate error about ambiguous lookup.  */
2871   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2872     {
2873       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2874       if (next->type == CPP_NAME && next->ambiguous_p)
2875         goto out;
2876     }
2877
2878   cp_parser_parse_tentatively (parser);
2879   id = cp_parser_id_expression (parser,
2880                                 /*template_keyword_p=*/false,
2881                                 /*check_dependency_p=*/true,
2882                                 /*template_p=*/NULL,
2883                                 /*declarator_p=*/true,
2884                                 /*optional_p=*/false);
2885   /* If the next token is a (, this is a function with no explicit return
2886      type, i.e. constructor, destructor or conversion op.  */
2887   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2888       || TREE_CODE (id) == TYPE_DECL)
2889     {
2890       cp_parser_abort_tentative_parse (parser);
2891       return false;
2892     }
2893   if (!cp_parser_parse_definitely (parser))
2894     return false;
2895
2896   /* Emit a diagnostic for the invalid type.  */
2897   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2898                                         id, token->location);
2899  out:
2900   /* If we aren't in the middle of a declarator (i.e. in a
2901      parameter-declaration-clause), skip to the end of the declaration;
2902      there's no point in trying to process it.  */
2903   if (!parser->in_declarator_p)
2904     cp_parser_skip_to_end_of_block_or_statement (parser);
2905   return true;
2906 }
2907
2908 /* Consume tokens up to, and including, the next non-nested closing `)'.
2909    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2910    are doing error recovery. Returns -1 if OR_COMMA is true and we
2911    found an unnested comma.  */
2912
2913 static int
2914 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2915                                        bool recovering,
2916                                        bool or_comma,
2917                                        bool consume_paren)
2918 {
2919   unsigned paren_depth = 0;
2920   unsigned brace_depth = 0;
2921   unsigned square_depth = 0;
2922
2923   if (recovering && !or_comma
2924       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2925     return 0;
2926
2927   while (true)
2928     {
2929       cp_token * token = cp_lexer_peek_token (parser->lexer);
2930
2931       switch (token->type)
2932         {
2933         case CPP_EOF:
2934         case CPP_PRAGMA_EOL:
2935           /* If we've run out of tokens, then there is no closing `)'.  */
2936           return 0;
2937
2938         /* This is good for lambda expression capture-lists.  */
2939         case CPP_OPEN_SQUARE:
2940           ++square_depth;
2941           break;
2942         case CPP_CLOSE_SQUARE:
2943           if (!square_depth--)
2944             return 0;
2945           break;
2946
2947         case CPP_SEMICOLON:
2948           /* This matches the processing in skip_to_end_of_statement.  */
2949           if (!brace_depth)
2950             return 0;
2951           break;
2952
2953         case CPP_OPEN_BRACE:
2954           ++brace_depth;
2955           break;
2956         case CPP_CLOSE_BRACE:
2957           if (!brace_depth--)
2958             return 0;
2959           break;
2960
2961         case CPP_COMMA:
2962           if (recovering && or_comma && !brace_depth && !paren_depth
2963               && !square_depth)
2964             return -1;
2965           break;
2966
2967         case CPP_OPEN_PAREN:
2968           if (!brace_depth)
2969             ++paren_depth;
2970           break;
2971
2972         case CPP_CLOSE_PAREN:
2973           if (!brace_depth && !paren_depth--)
2974             {
2975               if (consume_paren)
2976                 cp_lexer_consume_token (parser->lexer);
2977               return 1;
2978             }
2979           break;
2980
2981         default:
2982           break;
2983         }
2984
2985       /* Consume the token.  */
2986       cp_lexer_consume_token (parser->lexer);
2987     }
2988 }
2989
2990 /* Consume tokens until we reach the end of the current statement.
2991    Normally, that will be just before consuming a `;'.  However, if a
2992    non-nested `}' comes first, then we stop before consuming that.  */
2993
2994 static void
2995 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2996 {
2997   unsigned nesting_depth = 0;
2998
2999   while (true)
3000     {
3001       cp_token *token = cp_lexer_peek_token (parser->lexer);
3002
3003       switch (token->type)
3004         {
3005         case CPP_EOF:
3006         case CPP_PRAGMA_EOL:
3007           /* If we've run out of tokens, stop.  */
3008           return;
3009
3010         case CPP_SEMICOLON:
3011           /* If the next token is a `;', we have reached the end of the
3012              statement.  */
3013           if (!nesting_depth)
3014             return;
3015           break;
3016
3017         case CPP_CLOSE_BRACE:
3018           /* If this is a non-nested '}', stop before consuming it.
3019              That way, when confronted with something like:
3020
3021                { 3 + }
3022
3023              we stop before consuming the closing '}', even though we
3024              have not yet reached a `;'.  */
3025           if (nesting_depth == 0)
3026             return;
3027
3028           /* If it is the closing '}' for a block that we have
3029              scanned, stop -- but only after consuming the token.
3030              That way given:
3031
3032                 void f g () { ... }
3033                 typedef int I;
3034
3035              we will stop after the body of the erroneously declared
3036              function, but before consuming the following `typedef'
3037              declaration.  */
3038           if (--nesting_depth == 0)
3039             {
3040               cp_lexer_consume_token (parser->lexer);
3041               return;
3042             }
3043
3044         case CPP_OPEN_BRACE:
3045           ++nesting_depth;
3046           break;
3047
3048         default:
3049           break;
3050         }
3051
3052       /* Consume the token.  */
3053       cp_lexer_consume_token (parser->lexer);
3054     }
3055 }
3056
3057 /* This function is called at the end of a statement or declaration.
3058    If the next token is a semicolon, it is consumed; otherwise, error
3059    recovery is attempted.  */
3060
3061 static void
3062 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3063 {
3064   /* Look for the trailing `;'.  */
3065   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3066     {
3067       /* If there is additional (erroneous) input, skip to the end of
3068          the statement.  */
3069       cp_parser_skip_to_end_of_statement (parser);
3070       /* If the next token is now a `;', consume it.  */
3071       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3072         cp_lexer_consume_token (parser->lexer);
3073     }
3074 }
3075
3076 /* Skip tokens until we have consumed an entire block, or until we
3077    have consumed a non-nested `;'.  */
3078
3079 static void
3080 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3081 {
3082   int nesting_depth = 0;
3083
3084   while (nesting_depth >= 0)
3085     {
3086       cp_token *token = cp_lexer_peek_token (parser->lexer);
3087
3088       switch (token->type)
3089         {
3090         case CPP_EOF:
3091         case CPP_PRAGMA_EOL:
3092           /* If we've run out of tokens, stop.  */
3093           return;
3094
3095         case CPP_SEMICOLON:
3096           /* Stop if this is an unnested ';'. */
3097           if (!nesting_depth)
3098             nesting_depth = -1;
3099           break;
3100
3101         case CPP_CLOSE_BRACE:
3102           /* Stop if this is an unnested '}', or closes the outermost
3103              nesting level.  */
3104           nesting_depth--;
3105           if (nesting_depth < 0)
3106             return;
3107           if (!nesting_depth)
3108             nesting_depth = -1;
3109           break;
3110
3111         case CPP_OPEN_BRACE:
3112           /* Nest. */
3113           nesting_depth++;
3114           break;
3115
3116         default:
3117           break;
3118         }
3119
3120       /* Consume the token.  */
3121       cp_lexer_consume_token (parser->lexer);
3122     }
3123 }
3124
3125 /* Skip tokens until a non-nested closing curly brace is the next
3126    token, or there are no more tokens. Return true in the first case,
3127    false otherwise.  */
3128
3129 static bool
3130 cp_parser_skip_to_closing_brace (cp_parser *parser)
3131 {
3132   unsigned nesting_depth = 0;
3133
3134   while (true)
3135     {
3136       cp_token *token = cp_lexer_peek_token (parser->lexer);
3137
3138       switch (token->type)
3139         {
3140         case CPP_EOF:
3141         case CPP_PRAGMA_EOL:
3142           /* If we've run out of tokens, stop.  */
3143           return false;
3144
3145         case CPP_CLOSE_BRACE:
3146           /* If the next token is a non-nested `}', then we have reached
3147              the end of the current block.  */
3148           if (nesting_depth-- == 0)
3149             return true;
3150           break;
3151
3152         case CPP_OPEN_BRACE:
3153           /* If it the next token is a `{', then we are entering a new
3154              block.  Consume the entire block.  */
3155           ++nesting_depth;
3156           break;
3157
3158         default:
3159           break;
3160         }
3161
3162       /* Consume the token.  */
3163       cp_lexer_consume_token (parser->lexer);
3164     }
3165 }
3166
3167 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3168    parameter is the PRAGMA token, allowing us to purge the entire pragma
3169    sequence.  */
3170
3171 static void
3172 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3173 {
3174   cp_token *token;
3175
3176   parser->lexer->in_pragma = false;
3177
3178   do
3179     token = cp_lexer_consume_token (parser->lexer);
3180   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3181
3182   /* Ensure that the pragma is not parsed again.  */
3183   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3184 }
3185
3186 /* Require pragma end of line, resyncing with it as necessary.  The
3187    arguments are as for cp_parser_skip_to_pragma_eol.  */
3188
3189 static void
3190 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3191 {
3192   parser->lexer->in_pragma = false;
3193   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3194     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3195 }
3196
3197 /* This is a simple wrapper around make_typename_type. When the id is
3198    an unresolved identifier node, we can provide a superior diagnostic
3199    using cp_parser_diagnose_invalid_type_name.  */
3200
3201 static tree
3202 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3203                               tree id, location_t id_location)
3204 {
3205   tree result;
3206   if (TREE_CODE (id) == IDENTIFIER_NODE)
3207     {
3208       result = make_typename_type (scope, id, typename_type,
3209                                    /*complain=*/tf_none);
3210       if (result == error_mark_node)
3211         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3212       return result;
3213     }
3214   return make_typename_type (scope, id, typename_type, tf_error);
3215 }
3216
3217 /* This is a wrapper around the
3218    make_{pointer,ptrmem,reference}_declarator functions that decides
3219    which one to call based on the CODE and CLASS_TYPE arguments. The
3220    CODE argument should be one of the values returned by
3221    cp_parser_ptr_operator. */
3222 static cp_declarator *
3223 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3224                                     cp_cv_quals cv_qualifiers,
3225                                     cp_declarator *target)
3226 {
3227   if (code == ERROR_MARK)
3228     return cp_error_declarator;
3229
3230   if (code == INDIRECT_REF)
3231     if (class_type == NULL_TREE)
3232       return make_pointer_declarator (cv_qualifiers, target);
3233     else
3234       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3235   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3236     return make_reference_declarator (cv_qualifiers, target, false);
3237   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3238     return make_reference_declarator (cv_qualifiers, target, true);
3239   gcc_unreachable ();
3240 }
3241
3242 /* Create a new C++ parser.  */
3243
3244 static cp_parser *
3245 cp_parser_new (void)
3246 {
3247   cp_parser *parser;
3248   cp_lexer *lexer;
3249   unsigned i;
3250
3251   /* cp_lexer_new_main is called before doing GC allocation because
3252      cp_lexer_new_main might load a PCH file.  */
3253   lexer = cp_lexer_new_main ();
3254
3255   /* Initialize the binops_by_token so that we can get the tree
3256      directly from the token.  */
3257   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3258     binops_by_token[binops[i].token_type] = binops[i];
3259
3260   parser = ggc_alloc_cleared_cp_parser ();
3261   parser->lexer = lexer;
3262   parser->context = cp_parser_context_new (NULL);
3263
3264   /* For now, we always accept GNU extensions.  */
3265   parser->allow_gnu_extensions_p = 1;
3266
3267   /* The `>' token is a greater-than operator, not the end of a
3268      template-id.  */
3269   parser->greater_than_is_operator_p = true;
3270
3271   parser->default_arg_ok_p = true;
3272
3273   /* We are not parsing a constant-expression.  */
3274   parser->integral_constant_expression_p = false;
3275   parser->allow_non_integral_constant_expression_p = false;
3276   parser->non_integral_constant_expression_p = false;
3277
3278   /* Local variable names are not forbidden.  */
3279   parser->local_variables_forbidden_p = false;
3280
3281   /* We are not processing an `extern "C"' declaration.  */
3282   parser->in_unbraced_linkage_specification_p = false;
3283
3284   /* We are not processing a declarator.  */
3285   parser->in_declarator_p = false;
3286
3287   /* We are not processing a template-argument-list.  */
3288   parser->in_template_argument_list_p = false;
3289
3290   /* We are not in an iteration statement.  */
3291   parser->in_statement = 0;
3292
3293   /* We are not in a switch statement.  */
3294   parser->in_switch_statement_p = false;
3295
3296   /* We are not parsing a type-id inside an expression.  */
3297   parser->in_type_id_in_expr_p = false;
3298
3299   /* Declarations aren't implicitly extern "C".  */
3300   parser->implicit_extern_c = false;
3301
3302   /* String literals should be translated to the execution character set.  */
3303   parser->translate_strings_p = true;
3304
3305   /* We are not parsing a function body.  */
3306   parser->in_function_body = false;
3307
3308   /* We can correct until told otherwise.  */
3309   parser->colon_corrects_to_scope_p = true;
3310
3311   /* The unparsed function queue is empty.  */
3312   push_unparsed_function_queues (parser);
3313
3314   /* There are no classes being defined.  */
3315   parser->num_classes_being_defined = 0;
3316
3317   /* No template parameters apply.  */
3318   parser->num_template_parameter_lists = 0;
3319
3320   return parser;
3321 }
3322
3323 /* Create a cp_lexer structure which will emit the tokens in CACHE
3324    and push it onto the parser's lexer stack.  This is used for delayed
3325    parsing of in-class method bodies and default arguments, and should
3326    not be confused with tentative parsing.  */
3327 static void
3328 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3329 {
3330   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3331   lexer->next = parser->lexer;
3332   parser->lexer = lexer;
3333
3334   /* Move the current source position to that of the first token in the
3335      new lexer.  */
3336   cp_lexer_set_source_position_from_token (lexer->next_token);
3337 }
3338
3339 /* Pop the top lexer off the parser stack.  This is never used for the
3340    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3341 static void
3342 cp_parser_pop_lexer (cp_parser *parser)
3343 {
3344   cp_lexer *lexer = parser->lexer;
3345   parser->lexer = lexer->next;
3346   cp_lexer_destroy (lexer);
3347
3348   /* Put the current source position back where it was before this
3349      lexer was pushed.  */
3350   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3351 }
3352
3353 /* Lexical conventions [gram.lex]  */
3354
3355 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3356    identifier.  */
3357
3358 static tree
3359 cp_parser_identifier (cp_parser* parser)
3360 {
3361   cp_token *token;
3362
3363   /* Look for the identifier.  */
3364   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3365   /* Return the value.  */
3366   return token ? token->u.value : error_mark_node;
3367 }
3368
3369 /* Parse a sequence of adjacent string constants.  Returns a
3370    TREE_STRING representing the combined, nul-terminated string
3371    constant.  If TRANSLATE is true, translate the string to the
3372    execution character set.  If WIDE_OK is true, a wide string is
3373    invalid here.
3374
3375    C++98 [lex.string] says that if a narrow string literal token is
3376    adjacent to a wide string literal token, the behavior is undefined.
3377    However, C99 6.4.5p4 says that this results in a wide string literal.
3378    We follow C99 here, for consistency with the C front end.
3379
3380    This code is largely lifted from lex_string() in c-lex.c.
3381
3382    FUTURE: ObjC++ will need to handle @-strings here.  */
3383 static tree
3384 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3385 {
3386   tree value;
3387   size_t count;
3388   struct obstack str_ob;
3389   cpp_string str, istr, *strs;
3390   cp_token *tok;
3391   enum cpp_ttype type, curr_type;
3392   int have_suffix_p = 0;
3393   tree string_tree;
3394   tree suffix_id = NULL_TREE;
3395   bool curr_tok_is_userdef_p = false;
3396
3397   tok = cp_lexer_peek_token (parser->lexer);
3398   if (!cp_parser_is_string_literal (tok))
3399     {
3400       cp_parser_error (parser, "expected string-literal");
3401       return error_mark_node;
3402     }
3403
3404   if (cpp_userdef_string_p (tok->type))
3405     {
3406       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3407       curr_type = cpp_userdef_string_remove_type (tok->type);
3408       curr_tok_is_userdef_p = true;
3409     }
3410   else
3411     {
3412       string_tree = tok->u.value;
3413       curr_type = tok->type;
3414     }
3415   type = curr_type;
3416
3417   /* Try to avoid the overhead of creating and destroying an obstack
3418      for the common case of just one string.  */
3419   if (!cp_parser_is_string_literal
3420       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3421     {
3422       cp_lexer_consume_token (parser->lexer);
3423
3424       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3425       str.len = TREE_STRING_LENGTH (string_tree);
3426       count = 1;
3427
3428       if (curr_tok_is_userdef_p)
3429         {
3430           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3431           have_suffix_p = 1;
3432           curr_type = cpp_userdef_string_remove_type (tok->type);
3433         }
3434       else
3435         curr_type = tok->type;
3436
3437       strs = &str;
3438     }
3439   else
3440     {
3441       gcc_obstack_init (&str_ob);
3442       count = 0;
3443
3444       do
3445         {
3446           cp_lexer_consume_token (parser->lexer);
3447           count++;
3448           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3449           str.len = TREE_STRING_LENGTH (string_tree);
3450
3451           if (curr_tok_is_userdef_p)
3452             {
3453               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3454               if (have_suffix_p == 0)
3455                 {
3456                   suffix_id = curr_suffix_id;
3457                   have_suffix_p = 1;
3458                 }
3459               else if (have_suffix_p == 1
3460                        && curr_suffix_id != suffix_id)
3461                 {
3462                   error ("inconsistent user-defined literal suffixes"
3463                          " %qD and %qD in string literal",
3464                          suffix_id, curr_suffix_id);
3465                   have_suffix_p = -1;
3466                 }
3467               curr_type = cpp_userdef_string_remove_type (tok->type);
3468             }
3469           else
3470             curr_type = tok->type;
3471
3472           if (type != curr_type)
3473             {
3474               if (type == CPP_STRING)
3475                 type = curr_type;
3476               else if (curr_type != CPP_STRING)
3477                 error_at (tok->location,
3478                           "unsupported non-standard concatenation "
3479                           "of string literals");
3480             }
3481
3482           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3483
3484           tok = cp_lexer_peek_token (parser->lexer);
3485           if (cpp_userdef_string_p (tok->type))
3486             {
3487               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3488               curr_type = cpp_userdef_string_remove_type (tok->type);
3489               curr_tok_is_userdef_p = true;
3490             }
3491           else
3492             {
3493               string_tree = tok->u.value;
3494               curr_type = tok->type;
3495               curr_tok_is_userdef_p = false;
3496             }
3497         }
3498       while (cp_parser_is_string_literal (tok));
3499
3500       strs = (cpp_string *) obstack_finish (&str_ob);
3501     }
3502
3503   if (type != CPP_STRING && !wide_ok)
3504     {
3505       cp_parser_error (parser, "a wide string is invalid in this context");
3506       type = CPP_STRING;
3507     }
3508
3509   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3510       (parse_in, strs, count, &istr, type))
3511     {
3512       value = build_string (istr.len, (const char *)istr.text);
3513       free (CONST_CAST (unsigned char *, istr.text));
3514
3515       switch (type)
3516         {
3517         default:
3518         case CPP_STRING:
3519         case CPP_UTF8STRING:
3520           TREE_TYPE (value) = char_array_type_node;
3521           break;
3522         case CPP_STRING16:
3523           TREE_TYPE (value) = char16_array_type_node;
3524           break;
3525         case CPP_STRING32:
3526           TREE_TYPE (value) = char32_array_type_node;
3527           break;
3528         case CPP_WSTRING:
3529           TREE_TYPE (value) = wchar_array_type_node;
3530           break;
3531         }
3532
3533       value = fix_string_type (value);
3534
3535       if (have_suffix_p)
3536         {
3537           tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3538           tok->u.value = literal;
3539           return cp_parser_userdef_string_literal (tok);
3540         }
3541     }
3542   else
3543     /* cpp_interpret_string has issued an error.  */
3544     value = error_mark_node;
3545
3546   if (count > 1)
3547     obstack_free (&str_ob, 0);
3548
3549   return value;
3550 }
3551
3552 /* Look up a literal operator with the name and the exact arguments.  */
3553
3554 static tree
3555 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3556 {
3557   tree decl, fns;
3558   decl = lookup_name (name);
3559   if (!decl || !is_overloaded_fn (decl))
3560     return error_mark_node;
3561
3562   for (fns = decl; fns; fns = OVL_NEXT (fns))
3563     {
3564       unsigned int ix;
3565       bool found = true;
3566       tree fn = OVL_CURRENT (fns);
3567       tree argtypes = NULL_TREE;
3568       argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3569       if (argtypes != NULL_TREE)
3570         {
3571           for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3572                ++ix, argtypes = TREE_CHAIN (argtypes))
3573             {
3574               tree targ = TREE_VALUE (argtypes);
3575               tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3576               bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3577               bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3578               if ((ptr || arr || !same_type_p (targ, tparm))
3579                   && (!ptr || !arr
3580                       || !same_type_p (TREE_TYPE (targ),
3581                                        TREE_TYPE (tparm))))
3582                 found = false;
3583             }
3584           if (found)
3585             return fn;
3586         }
3587     }
3588
3589   return error_mark_node;
3590 }
3591
3592 /* Parse a user-defined char constant.  Returns a call to a user-defined
3593    literal operator taking the character as an argument.  */
3594
3595 static tree
3596 cp_parser_userdef_char_literal (cp_parser *parser)
3597 {
3598   cp_token *token = cp_lexer_consume_token (parser->lexer);
3599   tree literal = token->u.value;
3600   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3601   tree value = USERDEF_LITERAL_VALUE (literal);
3602   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3603   tree decl, result;
3604
3605   /* Build up a call to the user-defined operator  */
3606   /* Lookup the name we got back from the id-expression.  */
3607   VEC(tree,gc) *args = make_tree_vector ();
3608   VEC_safe_push (tree, gc, args, value);
3609   decl = lookup_literal_operator (name, args);
3610   if (!decl || decl == error_mark_node)
3611     {
3612       error ("unable to find character literal operator %qD with %qT argument",
3613              name, TREE_TYPE (value));
3614       release_tree_vector (args);
3615       return error_mark_node;
3616     }
3617   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3618   release_tree_vector (args);
3619   if (result != error_mark_node)
3620     return result;
3621
3622   error ("unable to find character literal operator %qD with %qT argument",
3623          name, TREE_TYPE (value));
3624   return error_mark_node;
3625 }
3626
3627 /* A subroutine of cp_parser_userdef_numeric_literal to
3628    create a char... template parameter pack from a string node.  */
3629
3630 static tree
3631 make_char_string_pack (tree value)
3632 {
3633   tree charvec;
3634   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3635   const char *str = TREE_STRING_POINTER (value);
3636   int i, len = TREE_STRING_LENGTH (value) - 1;
3637   tree argvec = make_tree_vec (1);
3638
3639   /* Fill in CHARVEC with all of the parameters.  */
3640   charvec = make_tree_vec (len);
3641   for (i = 0; i < len; ++i)
3642     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3643
3644   /* Build the argument packs.  */
3645   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3646   TREE_TYPE (argpack) = char_type_node;
3647
3648   TREE_VEC_ELT (argvec, 0) = argpack;
3649
3650   return argvec;
3651 }
3652
3653 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3654    literal operator.  */
3655
3656 static tree
3657 cp_parser_userdef_numeric_literal (cp_parser *parser)
3658 {
3659   cp_token *token = cp_lexer_consume_token (parser->lexer);
3660   tree literal = token->u.value;
3661   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3662   tree value = USERDEF_LITERAL_VALUE (literal);
3663   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3664   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3665   tree decl, result;
3666   VEC(tree,gc) *args;
3667
3668   /* Look for a literal operator taking the exact type of numeric argument
3669      as the literal value.  */
3670   args = make_tree_vector ();
3671   VEC_safe_push (tree, gc, args, value);
3672   decl = lookup_literal_operator (name, args);
3673   if (decl && decl != error_mark_node)
3674     {
3675       result = finish_call_expr (decl, &args, false, true, tf_none);
3676       if (result != error_mark_node)
3677         {
3678           release_tree_vector (args);
3679           return result;
3680         }
3681     }
3682   release_tree_vector (args);
3683
3684   /* If the numeric argument didn't work, look for a raw literal
3685      operator taking a const char* argument consisting of the number
3686      in string format.  */
3687   args = make_tree_vector ();
3688   VEC_safe_push (tree, gc, args, num_string);
3689   decl = lookup_literal_operator (name, args);
3690   if (decl && decl != error_mark_node)
3691     {
3692       result = finish_call_expr (decl, &args, false, true, tf_none);
3693       if (result != error_mark_node)
3694         {
3695           release_tree_vector (args);
3696           return result;
3697         }
3698     }
3699   release_tree_vector (args);
3700
3701   /* If the raw literal didn't work, look for a non-type template
3702      function with parameter pack char....  Call the function with
3703      template parameter characters representing the number.  */
3704   args = make_tree_vector ();
3705   decl = lookup_literal_operator (name, args);
3706   if (decl && decl != error_mark_node)
3707     {
3708       tree tmpl_args = make_char_string_pack (num_string);
3709       decl = lookup_template_function (decl, tmpl_args);
3710       result = finish_call_expr (decl, &args, false, true, tf_none);
3711       if (result != error_mark_node)
3712         {
3713           release_tree_vector (args);
3714           return result;
3715         }
3716     }
3717   release_tree_vector (args);
3718
3719   error ("unable to find numeric literal operator %qD", name);
3720   return error_mark_node;
3721 }
3722
3723 /* Parse a user-defined string constant.  Returns a call to a user-defined
3724    literal operator taking a character pointer and the length of the string
3725    as arguments.  */
3726
3727 static tree
3728 cp_parser_userdef_string_literal (cp_token *token)
3729 {
3730   tree literal = token->u.value;
3731   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3732   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3733   tree value = USERDEF_LITERAL_VALUE (literal);
3734   int len = TREE_STRING_LENGTH (value)
3735         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3736   tree decl, result;
3737
3738   /* Build up a call to the user-defined operator  */
3739   /* Lookup the name we got back from the id-expression.  */
3740   VEC(tree,gc) *args = make_tree_vector ();
3741   VEC_safe_push (tree, gc, args, value);
3742   VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3743   decl = lookup_name (name);
3744   if (!decl || decl == error_mark_node)
3745     {
3746       error ("unable to find string literal operator %qD", name);
3747       release_tree_vector (args);
3748       return error_mark_node;
3749     }
3750   result = finish_call_expr (decl, &args, false, true, tf_none);
3751   release_tree_vector (args);
3752   if (result != error_mark_node)
3753     return result;
3754
3755   error ("unable to find string literal operator %qD with %qT, %qT arguments",
3756          name, TREE_TYPE (value), size_type_node);
3757   return error_mark_node;
3758 }
3759
3760
3761 /* Basic concepts [gram.basic]  */
3762
3763 /* Parse a translation-unit.
3764
3765    translation-unit:
3766      declaration-seq [opt]
3767
3768    Returns TRUE if all went well.  */
3769
3770 static bool
3771 cp_parser_translation_unit (cp_parser* parser)
3772 {
3773   /* The address of the first non-permanent object on the declarator
3774      obstack.  */
3775   static void *declarator_obstack_base;
3776
3777   bool success;
3778
3779   /* Create the declarator obstack, if necessary.  */
3780   if (!cp_error_declarator)
3781     {
3782       gcc_obstack_init (&declarator_obstack);
3783       /* Create the error declarator.  */
3784       cp_error_declarator = make_declarator (cdk_error);
3785       /* Create the empty parameter list.  */
3786       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3787       /* Remember where the base of the declarator obstack lies.  */
3788       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3789     }
3790
3791   cp_parser_declaration_seq_opt (parser);
3792
3793   /* If there are no tokens left then all went well.  */
3794   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3795     {
3796       /* Get rid of the token array; we don't need it any more.  */
3797       cp_lexer_destroy (parser->lexer);
3798       parser->lexer = NULL;
3799
3800       /* This file might have been a context that's implicitly extern
3801          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3802       if (parser->implicit_extern_c)
3803         {
3804           pop_lang_context ();
3805           parser->implicit_extern_c = false;
3806         }
3807
3808       /* Finish up.  */
3809       finish_translation_unit ();
3810
3811       success = true;
3812     }
3813   else
3814     {
3815       cp_parser_error (parser, "expected declaration");
3816       success = false;
3817     }
3818
3819   /* Make sure the declarator obstack was fully cleaned up.  */
3820   gcc_assert (obstack_next_free (&declarator_obstack)
3821               == declarator_obstack_base);
3822
3823   /* All went well.  */
3824   return success;
3825 }
3826
3827 /* Expressions [gram.expr] */
3828
3829 /* Parse a primary-expression.
3830
3831    primary-expression:
3832      literal
3833      this
3834      ( expression )
3835      id-expression
3836
3837    GNU Extensions:
3838
3839    primary-expression:
3840      ( compound-statement )
3841      __builtin_va_arg ( assignment-expression , type-id )
3842      __builtin_offsetof ( type-id , offsetof-expression )
3843
3844    C++ Extensions:
3845      __has_nothrow_assign ( type-id )   
3846      __has_nothrow_constructor ( type-id )
3847      __has_nothrow_copy ( type-id )
3848      __has_trivial_assign ( type-id )   
3849      __has_trivial_constructor ( type-id )
3850      __has_trivial_copy ( type-id )
3851      __has_trivial_destructor ( type-id )
3852      __has_virtual_destructor ( type-id )     
3853      __is_abstract ( type-id )
3854      __is_base_of ( type-id , type-id )
3855      __is_class ( type-id )
3856      __is_convertible_to ( type-id , type-id )     
3857      __is_empty ( type-id )
3858      __is_enum ( type-id )
3859      __is_final ( type-id )
3860      __is_literal_type ( type-id )
3861      __is_pod ( type-id )
3862      __is_polymorphic ( type-id )
3863      __is_std_layout ( type-id )
3864      __is_trivial ( type-id )
3865      __is_union ( type-id )
3866
3867    Objective-C++ Extension:
3868
3869    primary-expression:
3870      objc-expression
3871
3872    literal:
3873      __null
3874
3875    ADDRESS_P is true iff this expression was immediately preceded by
3876    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3877    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3878    true iff this expression is a template argument.
3879
3880    Returns a representation of the expression.  Upon return, *IDK
3881    indicates what kind of id-expression (if any) was present.  */
3882
3883 static tree
3884 cp_parser_primary_expression (cp_parser *parser,
3885                               bool address_p,
3886                               bool cast_p,
3887                               bool template_arg_p,
3888                               cp_id_kind *idk)
3889 {
3890   cp_token *token = NULL;
3891
3892   /* Assume the primary expression is not an id-expression.  */
3893   *idk = CP_ID_KIND_NONE;
3894
3895   /* Peek at the next token.  */
3896   token = cp_lexer_peek_token (parser->lexer);
3897   switch (token->type)
3898     {
3899       /* literal:
3900            integer-literal
3901            character-literal
3902            floating-literal
3903            string-literal
3904            boolean-literal
3905            pointer-literal
3906            user-defined-literal  */
3907     case CPP_CHAR:
3908     case CPP_CHAR16:
3909     case CPP_CHAR32:
3910     case CPP_WCHAR:
3911     case CPP_NUMBER:
3912       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3913         return cp_parser_userdef_numeric_literal (parser);
3914       token = cp_lexer_consume_token (parser->lexer);
3915       if (TREE_CODE (token->u.value) == FIXED_CST)
3916         {
3917           error_at (token->location,
3918                     "fixed-point types not supported in C++");
3919           return error_mark_node;
3920         }
3921       /* Floating-point literals are only allowed in an integral
3922          constant expression if they are cast to an integral or
3923          enumeration type.  */
3924       if (TREE_CODE (token->u.value) == REAL_CST
3925           && parser->integral_constant_expression_p
3926           && pedantic)
3927         {
3928           /* CAST_P will be set even in invalid code like "int(2.7 +
3929              ...)".   Therefore, we have to check that the next token
3930              is sure to end the cast.  */
3931           if (cast_p)
3932             {
3933               cp_token *next_token;
3934
3935               next_token = cp_lexer_peek_token (parser->lexer);
3936               if (/* The comma at the end of an
3937                      enumerator-definition.  */
3938                   next_token->type != CPP_COMMA
3939                   /* The curly brace at the end of an enum-specifier.  */
3940                   && next_token->type != CPP_CLOSE_BRACE
3941                   /* The end of a statement.  */
3942                   && next_token->type != CPP_SEMICOLON
3943                   /* The end of the cast-expression.  */
3944                   && next_token->type != CPP_CLOSE_PAREN
3945                   /* The end of an array bound.  */
3946                   && next_token->type != CPP_CLOSE_SQUARE
3947                   /* The closing ">" in a template-argument-list.  */
3948                   && (next_token->type != CPP_GREATER
3949                       || parser->greater_than_is_operator_p)
3950                   /* C++0x only: A ">>" treated like two ">" tokens,
3951                      in a template-argument-list.  */
3952                   && (next_token->type != CPP_RSHIFT
3953                       || (cxx_dialect == cxx98)
3954                       || parser->greater_than_is_operator_p))
3955                 cast_p = false;
3956             }
3957
3958           /* If we are within a cast, then the constraint that the
3959              cast is to an integral or enumeration type will be
3960              checked at that point.  If we are not within a cast, then
3961              this code is invalid.  */
3962           if (!cast_p)
3963             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3964         }
3965       return token->u.value;
3966
3967     case CPP_CHAR_USERDEF:
3968     case CPP_CHAR16_USERDEF:
3969     case CPP_CHAR32_USERDEF:
3970     case CPP_WCHAR_USERDEF:
3971       return cp_parser_userdef_char_literal (parser);
3972
3973     case CPP_STRING:
3974     case CPP_STRING16:
3975     case CPP_STRING32:
3976     case CPP_WSTRING:
3977     case CPP_UTF8STRING:
3978     case CPP_STRING_USERDEF:
3979     case CPP_STRING16_USERDEF:
3980     case CPP_STRING32_USERDEF:
3981     case CPP_WSTRING_USERDEF:
3982     case CPP_UTF8STRING_USERDEF:
3983       /* ??? Should wide strings be allowed when parser->translate_strings_p
3984          is false (i.e. in attributes)?  If not, we can kill the third
3985          argument to cp_parser_string_literal.  */
3986       return cp_parser_string_literal (parser,
3987                                        parser->translate_strings_p,
3988                                        true);
3989
3990     case CPP_OPEN_PAREN:
3991       {
3992         tree expr;
3993         bool saved_greater_than_is_operator_p;
3994
3995         /* Consume the `('.  */
3996         cp_lexer_consume_token (parser->lexer);
3997         /* Within a parenthesized expression, a `>' token is always
3998            the greater-than operator.  */
3999         saved_greater_than_is_operator_p
4000           = parser->greater_than_is_operator_p;
4001         parser->greater_than_is_operator_p = true;
4002         /* If we see `( { ' then we are looking at the beginning of
4003            a GNU statement-expression.  */
4004         if (cp_parser_allow_gnu_extensions_p (parser)
4005             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4006           {
4007             /* Statement-expressions are not allowed by the standard.  */
4008             pedwarn (token->location, OPT_pedantic, 
4009                      "ISO C++ forbids braced-groups within expressions");
4010
4011             /* And they're not allowed outside of a function-body; you
4012                cannot, for example, write:
4013
4014                  int i = ({ int j = 3; j + 1; });
4015
4016                at class or namespace scope.  */
4017             if (!parser->in_function_body
4018                 || parser->in_template_argument_list_p)
4019               {
4020                 error_at (token->location,
4021                           "statement-expressions are not allowed outside "
4022                           "functions nor in template-argument lists");
4023                 cp_parser_skip_to_end_of_block_or_statement (parser);
4024                 expr = error_mark_node;
4025               }
4026             else
4027               {
4028                 /* Start the statement-expression.  */
4029                 expr = begin_stmt_expr ();
4030                 /* Parse the compound-statement.  */
4031                 cp_parser_compound_statement (parser, expr, false, false);
4032                 /* Finish up.  */
4033                 expr = finish_stmt_expr (expr, false);
4034               }
4035           }
4036         else
4037           {
4038             /* Parse the parenthesized expression.  */
4039             expr = cp_parser_expression (parser, cast_p, idk);
4040             /* Let the front end know that this expression was
4041                enclosed in parentheses. This matters in case, for
4042                example, the expression is of the form `A::B', since
4043                `&A::B' might be a pointer-to-member, but `&(A::B)' is
4044                not.  */
4045             finish_parenthesized_expr (expr);
4046             /* DR 705: Wrapping an unqualified name in parentheses
4047                suppresses arg-dependent lookup.  We want to pass back
4048                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4049                (c++/37862), but none of the others.  */
4050             if (*idk != CP_ID_KIND_QUALIFIED)
4051               *idk = CP_ID_KIND_NONE;
4052           }
4053         /* The `>' token might be the end of a template-id or
4054            template-parameter-list now.  */
4055         parser->greater_than_is_operator_p
4056           = saved_greater_than_is_operator_p;
4057         /* Consume the `)'.  */
4058         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4059           cp_parser_skip_to_end_of_statement (parser);
4060
4061         return expr;
4062       }
4063
4064     case CPP_OPEN_SQUARE:
4065       if (c_dialect_objc ())
4066         /* We have an Objective-C++ message. */
4067         return cp_parser_objc_expression (parser);
4068       {
4069         tree lam = cp_parser_lambda_expression (parser);
4070         /* Don't warn about a failed tentative parse.  */
4071         if (cp_parser_error_occurred (parser))
4072           return error_mark_node;
4073         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4074         return lam;
4075       }
4076
4077     case CPP_OBJC_STRING:
4078       if (c_dialect_objc ())
4079         /* We have an Objective-C++ string literal. */
4080         return cp_parser_objc_expression (parser);
4081       cp_parser_error (parser, "expected primary-expression");
4082       return error_mark_node;
4083
4084     case CPP_KEYWORD:
4085       switch (token->keyword)
4086         {
4087           /* These two are the boolean literals.  */
4088         case RID_TRUE:
4089           cp_lexer_consume_token (parser->lexer);
4090           return boolean_true_node;
4091         case RID_FALSE:
4092           cp_lexer_consume_token (parser->lexer);
4093           return boolean_false_node;
4094
4095           /* The `__null' literal.  */
4096         case RID_NULL:
4097           cp_lexer_consume_token (parser->lexer);
4098           return null_node;
4099
4100           /* The `nullptr' literal.  */
4101         case RID_NULLPTR:
4102           cp_lexer_consume_token (parser->lexer);
4103           return nullptr_node;
4104
4105           /* Recognize the `this' keyword.  */
4106         case RID_THIS:
4107           cp_lexer_consume_token (parser->lexer);
4108           if (parser->local_variables_forbidden_p)
4109             {
4110               error_at (token->location,
4111                         "%<this%> may not be used in this context");
4112               return error_mark_node;
4113             }
4114           /* Pointers cannot appear in constant-expressions.  */
4115           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4116             return error_mark_node;
4117           return finish_this_expr ();
4118
4119           /* The `operator' keyword can be the beginning of an
4120              id-expression.  */
4121         case RID_OPERATOR:
4122           goto id_expression;
4123
4124         case RID_FUNCTION_NAME:
4125         case RID_PRETTY_FUNCTION_NAME:
4126         case RID_C99_FUNCTION_NAME:
4127           {
4128             non_integral_constant name;
4129
4130             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4131                __func__ are the names of variables -- but they are
4132                treated specially.  Therefore, they are handled here,
4133                rather than relying on the generic id-expression logic
4134                below.  Grammatically, these names are id-expressions.
4135
4136                Consume the token.  */
4137             token = cp_lexer_consume_token (parser->lexer);
4138
4139             switch (token->keyword)
4140               {
4141               case RID_FUNCTION_NAME:
4142                 name = NIC_FUNC_NAME;
4143                 break;
4144               case RID_PRETTY_FUNCTION_NAME:
4145                 name = NIC_PRETTY_FUNC;
4146                 break;
4147               case RID_C99_FUNCTION_NAME:
4148                 name = NIC_C99_FUNC;
4149                 break;
4150               default:
4151                 gcc_unreachable ();
4152               }
4153
4154             if (cp_parser_non_integral_constant_expression (parser, name))
4155               return error_mark_node;
4156
4157             /* Look up the name.  */
4158             return finish_fname (token->u.value);
4159           }
4160
4161         case RID_VA_ARG:
4162           {
4163             tree expression;
4164             tree type;
4165
4166             /* The `__builtin_va_arg' construct is used to handle
4167                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4168             cp_lexer_consume_token (parser->lexer);
4169             /* Look for the opening `('.  */
4170             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4171             /* Now, parse the assignment-expression.  */
4172             expression = cp_parser_assignment_expression (parser,
4173                                                           /*cast_p=*/false, NULL);
4174             /* Look for the `,'.  */
4175             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4176             /* Parse the type-id.  */
4177             type = cp_parser_type_id (parser);
4178             /* Look for the closing `)'.  */
4179             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4180             /* Using `va_arg' in a constant-expression is not
4181                allowed.  */
4182             if (cp_parser_non_integral_constant_expression (parser,
4183                                                             NIC_VA_ARG))
4184               return error_mark_node;
4185             return build_x_va_arg (expression, type);
4186           }
4187
4188         case RID_OFFSETOF:
4189           return cp_parser_builtin_offsetof (parser);
4190
4191         case RID_HAS_NOTHROW_ASSIGN:
4192         case RID_HAS_NOTHROW_CONSTRUCTOR:
4193         case RID_HAS_NOTHROW_COPY:        
4194         case RID_HAS_TRIVIAL_ASSIGN:
4195         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4196         case RID_HAS_TRIVIAL_COPY:        
4197         case RID_HAS_TRIVIAL_DESTRUCTOR:
4198         case RID_HAS_VIRTUAL_DESTRUCTOR:
4199         case RID_IS_ABSTRACT:
4200         case RID_IS_BASE_OF:
4201         case RID_IS_CLASS:
4202         case RID_IS_CONVERTIBLE_TO:
4203         case RID_IS_EMPTY:
4204         case RID_IS_ENUM:
4205         case RID_IS_FINAL:
4206         case RID_IS_LITERAL_TYPE:
4207         case RID_IS_POD:
4208         case RID_IS_POLYMORPHIC:
4209         case RID_IS_STD_LAYOUT:
4210         case RID_IS_TRIVIAL:
4211         case RID_IS_UNION:
4212           return cp_parser_trait_expr (parser, token->keyword);
4213
4214         /* Objective-C++ expressions.  */
4215         case RID_AT_ENCODE:
4216         case RID_AT_PROTOCOL:
4217         case RID_AT_SELECTOR:
4218           return cp_parser_objc_expression (parser);
4219
4220         case RID_TEMPLATE:
4221           if (parser->in_function_body
4222               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4223                   == CPP_LESS))
4224             {
4225               error_at (token->location,
4226                         "a template declaration cannot appear at block scope");
4227               cp_parser_skip_to_end_of_block_or_statement (parser);
4228               return error_mark_node;
4229             }
4230         default:
4231           cp_parser_error (parser, "expected primary-expression");
4232           return error_mark_node;
4233         }
4234
4235       /* An id-expression can start with either an identifier, a
4236          `::' as the beginning of a qualified-id, or the "operator"
4237          keyword.  */
4238     case CPP_NAME:
4239     case CPP_SCOPE:
4240     case CPP_TEMPLATE_ID:
4241     case CPP_NESTED_NAME_SPECIFIER:
4242       {
4243         tree id_expression;
4244         tree decl;
4245         const char *error_msg;
4246         bool template_p;
4247         bool done;
4248         cp_token *id_expr_token;
4249
4250       id_expression:
4251         /* Parse the id-expression.  */
4252         id_expression
4253           = cp_parser_id_expression (parser,
4254                                      /*template_keyword_p=*/false,
4255                                      /*check_dependency_p=*/true,
4256                                      &template_p,
4257                                      /*declarator_p=*/false,
4258                                      /*optional_p=*/false);
4259         if (id_expression == error_mark_node)
4260           return error_mark_node;
4261         id_expr_token = token;
4262         token = cp_lexer_peek_token (parser->lexer);
4263         done = (token->type != CPP_OPEN_SQUARE
4264                 && token->type != CPP_OPEN_PAREN
4265                 && token->type != CPP_DOT
4266                 && token->type != CPP_DEREF
4267                 && token->type != CPP_PLUS_PLUS
4268                 && token->type != CPP_MINUS_MINUS);
4269         /* If we have a template-id, then no further lookup is
4270            required.  If the template-id was for a template-class, we
4271            will sometimes have a TYPE_DECL at this point.  */
4272         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4273                  || TREE_CODE (id_expression) == TYPE_DECL)
4274           decl = id_expression;
4275         /* Look up the name.  */
4276         else
4277           {
4278             tree ambiguous_decls;
4279
4280             /* If we already know that this lookup is ambiguous, then
4281                we've already issued an error message; there's no reason
4282                to check again.  */
4283             if (id_expr_token->type == CPP_NAME
4284                 && id_expr_token->ambiguous_p)
4285               {
4286                 cp_parser_simulate_error (parser);
4287                 return error_mark_node;
4288               }
4289
4290             decl = cp_parser_lookup_name (parser, id_expression,
4291                                           none_type,
4292                                           template_p,
4293                                           /*is_namespace=*/false,
4294                                           /*check_dependency=*/true,
4295                                           &ambiguous_decls,
4296                                           id_expr_token->location);
4297             /* If the lookup was ambiguous, an error will already have
4298                been issued.  */
4299             if (ambiguous_decls)
4300               return error_mark_node;
4301
4302             /* In Objective-C++, we may have an Objective-C 2.0
4303                dot-syntax for classes here.  */
4304             if (c_dialect_objc ()
4305                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4306                 && TREE_CODE (decl) == TYPE_DECL
4307                 && objc_is_class_name (decl))
4308               {
4309                 tree component;
4310                 cp_lexer_consume_token (parser->lexer);
4311                 component = cp_parser_identifier (parser);
4312                 if (component == error_mark_node)
4313                   return error_mark_node;
4314
4315                 return objc_build_class_component_ref (id_expression, component);
4316               }
4317
4318             /* In Objective-C++, an instance variable (ivar) may be preferred
4319                to whatever cp_parser_lookup_name() found.  */
4320             decl = objc_lookup_ivar (decl, id_expression);
4321
4322             /* If name lookup gives us a SCOPE_REF, then the
4323                qualifying scope was dependent.  */
4324             if (TREE_CODE (decl) == SCOPE_REF)
4325               {
4326                 /* At this point, we do not know if DECL is a valid
4327                    integral constant expression.  We assume that it is
4328                    in fact such an expression, so that code like:
4329
4330                       template <int N> struct A {
4331                         int a[B<N>::i];
4332                       };
4333                      
4334                    is accepted.  At template-instantiation time, we
4335                    will check that B<N>::i is actually a constant.  */
4336                 return decl;
4337               }
4338             /* Check to see if DECL is a local variable in a context
4339                where that is forbidden.  */
4340             if (parser->local_variables_forbidden_p
4341                 && local_variable_p (decl))
4342               {
4343                 /* It might be that we only found DECL because we are
4344                    trying to be generous with pre-ISO scoping rules.
4345                    For example, consider:
4346
4347                      int i;
4348                      void g() {
4349                        for (int i = 0; i < 10; ++i) {}
4350                        extern void f(int j = i);
4351                      }
4352
4353                    Here, name look up will originally find the out
4354                    of scope `i'.  We need to issue a warning message,
4355                    but then use the global `i'.  */
4356                 decl = check_for_out_of_scope_variable (decl);
4357                 if (local_variable_p (decl))
4358                   {
4359                     error_at (id_expr_token->location,
4360                               "local variable %qD may not appear in this context",
4361                               decl);
4362                     return error_mark_node;
4363                   }
4364               }
4365           }
4366
4367         decl = (finish_id_expression
4368                 (id_expression, decl, parser->scope,
4369                  idk,
4370                  parser->integral_constant_expression_p,
4371                  parser->allow_non_integral_constant_expression_p,
4372                  &parser->non_integral_constant_expression_p,
4373                  template_p, done, address_p,
4374                  template_arg_p,
4375                  &error_msg,
4376                  id_expr_token->location));
4377         if (error_msg)
4378           cp_parser_error (parser, error_msg);
4379         return decl;
4380       }
4381
4382       /* Anything else is an error.  */
4383     default:
4384       cp_parser_error (parser, "expected primary-expression");
4385       return error_mark_node;
4386     }
4387 }
4388
4389 /* Parse an id-expression.
4390
4391    id-expression:
4392      unqualified-id
4393      qualified-id
4394
4395    qualified-id:
4396      :: [opt] nested-name-specifier template [opt] unqualified-id
4397      :: identifier
4398      :: operator-function-id
4399      :: template-id
4400
4401    Return a representation of the unqualified portion of the
4402    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4403    a `::' or nested-name-specifier.
4404
4405    Often, if the id-expression was a qualified-id, the caller will
4406    want to make a SCOPE_REF to represent the qualified-id.  This
4407    function does not do this in order to avoid wastefully creating
4408    SCOPE_REFs when they are not required.
4409
4410    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4411    `template' keyword.
4412
4413    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4414    uninstantiated templates.
4415
4416    If *TEMPLATE_P is non-NULL, it is set to true iff the
4417    `template' keyword is used to explicitly indicate that the entity
4418    named is a template.
4419
4420    If DECLARATOR_P is true, the id-expression is appearing as part of
4421    a declarator, rather than as part of an expression.  */
4422
4423 static tree
4424 cp_parser_id_expression (cp_parser *parser,
4425                          bool template_keyword_p,
4426                          bool check_dependency_p,
4427                          bool *template_p,
4428                          bool declarator_p,
4429                          bool optional_p)
4430 {
4431   bool global_scope_p;
4432   bool nested_name_specifier_p;
4433
4434   /* Assume the `template' keyword was not used.  */
4435   if (template_p)
4436     *template_p = template_keyword_p;
4437
4438   /* Look for the optional `::' operator.  */
4439   global_scope_p
4440     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4441        != NULL_TREE);
4442   /* Look for the optional nested-name-specifier.  */
4443   nested_name_specifier_p
4444     = (cp_parser_nested_name_specifier_opt (parser,
4445                                             /*typename_keyword_p=*/false,
4446                                             check_dependency_p,
4447                                             /*type_p=*/false,
4448                                             declarator_p)
4449        != NULL_TREE);
4450   /* If there is a nested-name-specifier, then we are looking at
4451      the first qualified-id production.  */
4452   if (nested_name_specifier_p)
4453     {
4454       tree saved_scope;
4455       tree saved_object_scope;
4456       tree saved_qualifying_scope;
4457       tree unqualified_id;
4458       bool is_template;
4459
4460       /* See if the next token is the `template' keyword.  */
4461       if (!template_p)
4462         template_p = &is_template;
4463       *template_p = cp_parser_optional_template_keyword (parser);
4464       /* Name lookup we do during the processing of the
4465          unqualified-id might obliterate SCOPE.  */
4466       saved_scope = parser->scope;
4467       saved_object_scope = parser->object_scope;
4468       saved_qualifying_scope = parser->qualifying_scope;
4469       /* Process the final unqualified-id.  */
4470       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4471                                                  check_dependency_p,
4472                                                  declarator_p,
4473                                                  /*optional_p=*/false);
4474       /* Restore the SAVED_SCOPE for our caller.  */
4475       parser->scope = saved_scope;
4476       parser->object_scope = saved_object_scope;
4477       parser->qualifying_scope = saved_qualifying_scope;
4478
4479       return unqualified_id;
4480     }
4481   /* Otherwise, if we are in global scope, then we are looking at one
4482      of the other qualified-id productions.  */
4483   else if (global_scope_p)
4484     {
4485       cp_token *token;
4486       tree id;
4487
4488       /* Peek at the next token.  */
4489       token = cp_lexer_peek_token (parser->lexer);
4490
4491       /* If it's an identifier, and the next token is not a "<", then
4492          we can avoid the template-id case.  This is an optimization
4493          for this common case.  */
4494       if (token->type == CPP_NAME
4495           && !cp_parser_nth_token_starts_template_argument_list_p
4496                (parser, 2))
4497         return cp_parser_identifier (parser);
4498
4499       cp_parser_parse_tentatively (parser);
4500       /* Try a template-id.  */
4501       id = cp_parser_template_id (parser,
4502                                   /*template_keyword_p=*/false,
4503                                   /*check_dependency_p=*/true,
4504                                   declarator_p);
4505       /* If that worked, we're done.  */
4506       if (cp_parser_parse_definitely (parser))
4507         return id;
4508
4509       /* Peek at the next token.  (Changes in the token buffer may
4510          have invalidated the pointer obtained above.)  */
4511       token = cp_lexer_peek_token (parser->lexer);
4512
4513       switch (token->type)
4514         {
4515         case CPP_NAME:
4516           return cp_parser_identifier (parser);
4517
4518         case CPP_KEYWORD:
4519           if (token->keyword == RID_OPERATOR)
4520             return cp_parser_operator_function_id (parser);
4521           /* Fall through.  */
4522
4523         default:
4524           cp_parser_error (parser, "expected id-expression");
4525           return error_mark_node;
4526         }
4527     }
4528   else
4529     return cp_parser_unqualified_id (parser, template_keyword_p,
4530                                      /*check_dependency_p=*/true,
4531                                      declarator_p,
4532                                      optional_p);
4533 }
4534
4535 /* Parse an unqualified-id.
4536
4537    unqualified-id:
4538      identifier
4539      operator-function-id
4540      conversion-function-id
4541      ~ class-name
4542      template-id
4543
4544    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4545    keyword, in a construct like `A::template ...'.
4546
4547    Returns a representation of unqualified-id.  For the `identifier'
4548    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4549    production a BIT_NOT_EXPR is returned; the operand of the
4550    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4551    other productions, see the documentation accompanying the
4552    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4553    names are looked up in uninstantiated templates.  If DECLARATOR_P
4554    is true, the unqualified-id is appearing as part of a declarator,
4555    rather than as part of an expression.  */
4556
4557 static tree
4558 cp_parser_unqualified_id (cp_parser* parser,
4559                           bool template_keyword_p,
4560                           bool check_dependency_p,
4561                           bool declarator_p,
4562                           bool optional_p)
4563 {
4564   cp_token *token;
4565
4566   /* Peek at the next token.  */
4567   token = cp_lexer_peek_token (parser->lexer);
4568
4569   switch (token->type)
4570     {
4571     case CPP_NAME:
4572       {
4573         tree id;
4574
4575         /* We don't know yet whether or not this will be a
4576            template-id.  */
4577         cp_parser_parse_tentatively (parser);
4578         /* Try a template-id.  */
4579         id = cp_parser_template_id (parser, template_keyword_p,
4580                                     check_dependency_p,
4581                                     declarator_p);
4582         /* If it worked, we're done.  */
4583         if (cp_parser_parse_definitely (parser))
4584           return id;
4585         /* Otherwise, it's an ordinary identifier.  */
4586         return cp_parser_identifier (parser);
4587       }
4588
4589     case CPP_TEMPLATE_ID:
4590       return cp_parser_template_id (parser, template_keyword_p,
4591                                     check_dependency_p,
4592                                     declarator_p);
4593
4594     case CPP_COMPL:
4595       {
4596         tree type_decl;
4597         tree qualifying_scope;
4598         tree object_scope;
4599         tree scope;
4600         bool done;
4601
4602         /* Consume the `~' token.  */
4603         cp_lexer_consume_token (parser->lexer);
4604         /* Parse the class-name.  The standard, as written, seems to
4605            say that:
4606
4607              template <typename T> struct S { ~S (); };
4608              template <typename T> S<T>::~S() {}
4609
4610            is invalid, since `~' must be followed by a class-name, but
4611            `S<T>' is dependent, and so not known to be a class.
4612            That's not right; we need to look in uninstantiated
4613            templates.  A further complication arises from:
4614
4615              template <typename T> void f(T t) {
4616                t.T::~T();
4617              }
4618
4619            Here, it is not possible to look up `T' in the scope of `T'
4620            itself.  We must look in both the current scope, and the
4621            scope of the containing complete expression.
4622
4623            Yet another issue is:
4624
4625              struct S {
4626                int S;
4627                ~S();
4628              };
4629
4630              S::~S() {}
4631
4632            The standard does not seem to say that the `S' in `~S'
4633            should refer to the type `S' and not the data member
4634            `S::S'.  */
4635
4636         /* DR 244 says that we look up the name after the "~" in the
4637            same scope as we looked up the qualifying name.  That idea
4638            isn't fully worked out; it's more complicated than that.  */
4639         scope = parser->scope;
4640         object_scope = parser->object_scope;
4641         qualifying_scope = parser->qualifying_scope;
4642
4643         /* Check for invalid scopes.  */
4644         if (scope == error_mark_node)
4645           {
4646             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4647               cp_lexer_consume_token (parser->lexer);
4648             return error_mark_node;
4649           }
4650         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4651           {
4652             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4653               error_at (token->location,
4654                         "scope %qT before %<~%> is not a class-name",
4655                         scope);
4656             cp_parser_simulate_error (parser);
4657             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4658               cp_lexer_consume_token (parser->lexer);
4659             return error_mark_node;
4660           }
4661         gcc_assert (!scope || TYPE_P (scope));
4662
4663         /* If the name is of the form "X::~X" it's OK even if X is a
4664            typedef.  */
4665         token = cp_lexer_peek_token (parser->lexer);
4666         if (scope
4667             && token->type == CPP_NAME
4668             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4669                 != CPP_LESS)
4670             && (token->u.value == TYPE_IDENTIFIER (scope)
4671                 || (CLASS_TYPE_P (scope)
4672                     && constructor_name_p (token->u.value, scope))))
4673           {
4674             cp_lexer_consume_token (parser->lexer);
4675             return build_nt (BIT_NOT_EXPR, scope);
4676           }
4677
4678         /* If there was an explicit qualification (S::~T), first look
4679            in the scope given by the qualification (i.e., S).
4680
4681            Note: in the calls to cp_parser_class_name below we pass
4682            typename_type so that lookup finds the injected-class-name
4683            rather than the constructor.  */
4684         done = false;
4685         type_decl = NULL_TREE;
4686         if (scope)
4687           {
4688             cp_parser_parse_tentatively (parser);
4689             type_decl = cp_parser_class_name (parser,
4690                                               /*typename_keyword_p=*/false,
4691                                               /*template_keyword_p=*/false,
4692                                               typename_type,
4693                                               /*check_dependency=*/false,
4694                                               /*class_head_p=*/false,
4695                                               declarator_p);
4696             if (cp_parser_parse_definitely (parser))
4697               done = true;
4698           }
4699         /* In "N::S::~S", look in "N" as well.  */
4700         if (!done && scope && qualifying_scope)
4701           {
4702             cp_parser_parse_tentatively (parser);
4703             parser->scope = qualifying_scope;
4704             parser->object_scope = NULL_TREE;
4705             parser->qualifying_scope = NULL_TREE;
4706             type_decl
4707               = cp_parser_class_name (parser,
4708                                       /*typename_keyword_p=*/false,
4709                                       /*template_keyword_p=*/false,
4710                                       typename_type,
4711                                       /*check_dependency=*/false,
4712                                       /*class_head_p=*/false,
4713                                       declarator_p);
4714             if (cp_parser_parse_definitely (parser))
4715               done = true;
4716           }
4717         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4718         else if (!done && object_scope)
4719           {
4720             cp_parser_parse_tentatively (parser);
4721             parser->scope = object_scope;
4722             parser->object_scope = NULL_TREE;
4723             parser->qualifying_scope = NULL_TREE;
4724             type_decl
4725               = cp_parser_class_name (parser,
4726                                       /*typename_keyword_p=*/false,
4727                                       /*template_keyword_p=*/false,
4728                                       typename_type,
4729                                       /*check_dependency=*/false,
4730                                       /*class_head_p=*/false,
4731                                       declarator_p);
4732             if (cp_parser_parse_definitely (parser))
4733               done = true;
4734           }
4735         /* Look in the surrounding context.  */
4736         if (!done)
4737           {
4738             parser->scope = NULL_TREE;
4739             parser->object_scope = NULL_TREE;
4740             parser->qualifying_scope = NULL_TREE;
4741             if (processing_template_decl)
4742               cp_parser_parse_tentatively (parser);
4743             type_decl
4744               = cp_parser_class_name (parser,
4745                                       /*typename_keyword_p=*/false,
4746                                       /*template_keyword_p=*/false,
4747                                       typename_type,
4748                                       /*check_dependency=*/false,
4749                                       /*class_head_p=*/false,
4750                                       declarator_p);
4751             if (processing_template_decl
4752                 && ! cp_parser_parse_definitely (parser))
4753               {
4754                 /* We couldn't find a type with this name, so just accept
4755                    it and check for a match at instantiation time.  */
4756                 type_decl = cp_parser_identifier (parser);
4757                 if (type_decl != error_mark_node)
4758                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4759                 return type_decl;
4760               }
4761           }
4762         /* If an error occurred, assume that the name of the
4763            destructor is the same as the name of the qualifying
4764            class.  That allows us to keep parsing after running
4765            into ill-formed destructor names.  */
4766         if (type_decl == error_mark_node && scope)
4767           return build_nt (BIT_NOT_EXPR, scope);
4768         else if (type_decl == error_mark_node)
4769           return error_mark_node;
4770
4771         /* Check that destructor name and scope match.  */
4772         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4773           {
4774             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4775               error_at (token->location,
4776                         "declaration of %<~%T%> as member of %qT",
4777                         type_decl, scope);
4778             cp_parser_simulate_error (parser);
4779             return error_mark_node;
4780           }
4781
4782         /* [class.dtor]
4783
4784            A typedef-name that names a class shall not be used as the
4785            identifier in the declarator for a destructor declaration.  */
4786         if (declarator_p
4787             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4788             && !DECL_SELF_REFERENCE_P (type_decl)
4789             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4790           error_at (token->location,
4791                     "typedef-name %qD used as destructor declarator",
4792                     type_decl);
4793
4794         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4795       }
4796
4797     case CPP_KEYWORD:
4798       if (token->keyword == RID_OPERATOR)
4799         {
4800           tree id;
4801
4802           /* This could be a template-id, so we try that first.  */
4803           cp_parser_parse_tentatively (parser);
4804           /* Try a template-id.  */
4805           id = cp_parser_template_id (parser, template_keyword_p,
4806                                       /*check_dependency_p=*/true,
4807                                       declarator_p);
4808           /* If that worked, we're done.  */
4809           if (cp_parser_parse_definitely (parser))
4810             return id;
4811           /* We still don't know whether we're looking at an
4812              operator-function-id or a conversion-function-id.  */
4813           cp_parser_parse_tentatively (parser);
4814           /* Try an operator-function-id.  */
4815           id = cp_parser_operator_function_id (parser);
4816           /* If that didn't work, try a conversion-function-id.  */
4817           if (!cp_parser_parse_definitely (parser))
4818             id = cp_parser_conversion_function_id (parser);
4819           else if (UDLIT_OPER_P (id))
4820             {
4821               /* 17.6.3.3.5  */
4822               const char *name = UDLIT_OP_SUFFIX (id);
4823               if (name[0] != '_' && !in_system_header)
4824                 warning (0, "literal operator suffixes not preceded by %<_%>"
4825                             " are reserved for future standardization");
4826             }
4827
4828           return id;
4829         }
4830       /* Fall through.  */
4831
4832     default:
4833       if (optional_p)
4834         return NULL_TREE;
4835       cp_parser_error (parser, "expected unqualified-id");
4836       return error_mark_node;
4837     }
4838 }
4839
4840 /* Parse an (optional) nested-name-specifier.
4841
4842    nested-name-specifier: [C++98]
4843      class-or-namespace-name :: nested-name-specifier [opt]
4844      class-or-namespace-name :: template nested-name-specifier [opt]
4845
4846    nested-name-specifier: [C++0x]
4847      type-name ::
4848      namespace-name ::
4849      nested-name-specifier identifier ::
4850      nested-name-specifier template [opt] simple-template-id ::
4851
4852    PARSER->SCOPE should be set appropriately before this function is
4853    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4854    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4855    in name lookups.
4856
4857    Sets PARSER->SCOPE to the class (TYPE) or namespace
4858    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4859    it unchanged if there is no nested-name-specifier.  Returns the new
4860    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4861
4862    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4863    part of a declaration and/or decl-specifier.  */
4864
4865 static tree
4866 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4867                                      bool typename_keyword_p,
4868                                      bool check_dependency_p,
4869                                      bool type_p,
4870                                      bool is_declaration)
4871 {
4872   bool success = false;
4873   cp_token_position start = 0;
4874   cp_token *token;
4875
4876   /* Remember where the nested-name-specifier starts.  */
4877   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4878     {
4879       start = cp_lexer_token_position (parser->lexer, false);
4880       push_deferring_access_checks (dk_deferred);
4881     }
4882
4883   while (true)
4884     {
4885       tree new_scope;
4886       tree old_scope;
4887       tree saved_qualifying_scope;
4888       bool template_keyword_p;
4889
4890       /* Spot cases that cannot be the beginning of a
4891          nested-name-specifier.  */
4892       token = cp_lexer_peek_token (parser->lexer);
4893
4894       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4895          the already parsed nested-name-specifier.  */
4896       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4897         {
4898           /* Grab the nested-name-specifier and continue the loop.  */
4899           cp_parser_pre_parsed_nested_name_specifier (parser);
4900           /* If we originally encountered this nested-name-specifier
4901              with IS_DECLARATION set to false, we will not have
4902              resolved TYPENAME_TYPEs, so we must do so here.  */
4903           if (is_declaration
4904               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4905             {
4906               new_scope = resolve_typename_type (parser->scope,
4907                                                  /*only_current_p=*/false);
4908               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4909                 parser->scope = new_scope;
4910             }
4911           success = true;
4912           continue;
4913         }
4914
4915       /* Spot cases that cannot be the beginning of a
4916          nested-name-specifier.  On the second and subsequent times
4917          through the loop, we look for the `template' keyword.  */
4918       if (success && token->keyword == RID_TEMPLATE)
4919         ;
4920       /* A template-id can start a nested-name-specifier.  */
4921       else if (token->type == CPP_TEMPLATE_ID)
4922         ;
4923       /* DR 743: decltype can be used in a nested-name-specifier.  */
4924       else if (token_is_decltype (token))
4925         ;
4926       else
4927         {
4928           /* If the next token is not an identifier, then it is
4929              definitely not a type-name or namespace-name.  */
4930           if (token->type != CPP_NAME)
4931             break;
4932           /* If the following token is neither a `<' (to begin a
4933              template-id), nor a `::', then we are not looking at a
4934              nested-name-specifier.  */
4935           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4936
4937           if (token->type == CPP_COLON
4938               && parser->colon_corrects_to_scope_p
4939               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4940             {
4941               error_at (token->location,
4942                         "found %<:%> in nested-name-specifier, expected %<::%>");
4943               token->type = CPP_SCOPE;
4944             }
4945
4946           if (token->type != CPP_SCOPE
4947               && !cp_parser_nth_token_starts_template_argument_list_p
4948                   (parser, 2))
4949             break;
4950         }
4951
4952       /* The nested-name-specifier is optional, so we parse
4953          tentatively.  */
4954       cp_parser_parse_tentatively (parser);
4955
4956       /* Look for the optional `template' keyword, if this isn't the
4957          first time through the loop.  */
4958       if (success)
4959         template_keyword_p = cp_parser_optional_template_keyword (parser);
4960       else
4961         template_keyword_p = false;
4962
4963       /* Save the old scope since the name lookup we are about to do
4964          might destroy it.  */
4965       old_scope = parser->scope;
4966       saved_qualifying_scope = parser->qualifying_scope;
4967       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4968          look up names in "X<T>::I" in order to determine that "Y" is
4969          a template.  So, if we have a typename at this point, we make
4970          an effort to look through it.  */
4971       if (is_declaration
4972           && !typename_keyword_p
4973           && parser->scope
4974           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4975         parser->scope = resolve_typename_type (parser->scope,
4976                                                /*only_current_p=*/false);
4977       /* Parse the qualifying entity.  */
4978       new_scope
4979         = cp_parser_qualifying_entity (parser,
4980                                        typename_keyword_p,
4981                                        template_keyword_p,
4982                                        check_dependency_p,
4983                                        type_p,
4984                                        is_declaration);
4985       /* Look for the `::' token.  */
4986       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4987
4988       /* If we found what we wanted, we keep going; otherwise, we're
4989          done.  */
4990       if (!cp_parser_parse_definitely (parser))
4991         {
4992           bool error_p = false;
4993
4994           /* Restore the OLD_SCOPE since it was valid before the
4995              failed attempt at finding the last
4996              class-or-namespace-name.  */
4997           parser->scope = old_scope;
4998           parser->qualifying_scope = saved_qualifying_scope;
4999
5000           /* If the next token is a decltype, and the one after that is a
5001              `::', then the decltype has failed to resolve to a class or
5002              enumeration type.  Give this error even when parsing
5003              tentatively since it can't possibly be valid--and we're going
5004              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5005              won't get another chance.*/
5006           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5007               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5008                   == CPP_SCOPE))
5009             {
5010               token = cp_lexer_consume_token (parser->lexer);
5011               error_at (token->location, "decltype evaluates to %qT, "
5012                         "which is not a class or enumeration type",
5013                         token->u.value);
5014               parser->scope = error_mark_node;
5015               error_p = true;
5016               /* As below.  */
5017               success = true;
5018               cp_lexer_consume_token (parser->lexer);
5019             }
5020
5021           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5022             break;
5023           /* If the next token is an identifier, and the one after
5024              that is a `::', then any valid interpretation would have
5025              found a class-or-namespace-name.  */
5026           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5027                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5028                      == CPP_SCOPE)
5029                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5030                      != CPP_COMPL))
5031             {
5032               token = cp_lexer_consume_token (parser->lexer);
5033               if (!error_p)
5034                 {
5035                   if (!token->ambiguous_p)
5036                     {
5037                       tree decl;
5038                       tree ambiguous_decls;
5039
5040                       decl = cp_parser_lookup_name (parser, token->u.value,
5041                                                     none_type,
5042                                                     /*is_template=*/false,
5043                                                     /*is_namespace=*/false,
5044                                                     /*check_dependency=*/true,
5045                                                     &ambiguous_decls,
5046                                                     token->location);
5047                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5048                         error_at (token->location,
5049                                   "%qD used without template parameters",
5050                                   decl);
5051                       else if (ambiguous_decls)
5052                         {
5053                           error_at (token->location,
5054                                     "reference to %qD is ambiguous",
5055                                     token->u.value);
5056                           print_candidates (ambiguous_decls);
5057                           decl = error_mark_node;
5058                         }
5059                       else
5060                         {
5061                           if (cxx_dialect != cxx98)
5062                             cp_parser_name_lookup_error
5063                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5064                              token->location);
5065                           else
5066                             cp_parser_name_lookup_error
5067                             (parser, token->u.value, decl, NLE_CXX98,
5068                              token->location);
5069                         }
5070                     }
5071                   parser->scope = error_mark_node;
5072                   error_p = true;
5073                   /* Treat this as a successful nested-name-specifier
5074                      due to:
5075
5076                      [basic.lookup.qual]
5077
5078                      If the name found is not a class-name (clause
5079                      _class_) or namespace-name (_namespace.def_), the
5080                      program is ill-formed.  */
5081                   success = true;
5082                 }
5083               cp_lexer_consume_token (parser->lexer);
5084             }
5085           break;
5086         }
5087       /* We've found one valid nested-name-specifier.  */
5088       success = true;
5089       /* Name lookup always gives us a DECL.  */
5090       if (TREE_CODE (new_scope) == TYPE_DECL)
5091         new_scope = TREE_TYPE (new_scope);
5092       /* Uses of "template" must be followed by actual templates.  */
5093       if (template_keyword_p
5094           && !(CLASS_TYPE_P (new_scope)
5095                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5096                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5097                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5098           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5099                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5100                    == TEMPLATE_ID_EXPR)))
5101         permerror (input_location, TYPE_P (new_scope)
5102                    ? G_("%qT is not a template")
5103                    : G_("%qD is not a template"),
5104                    new_scope);
5105       /* If it is a class scope, try to complete it; we are about to
5106          be looking up names inside the class.  */
5107       if (TYPE_P (new_scope)
5108           /* Since checking types for dependency can be expensive,
5109              avoid doing it if the type is already complete.  */
5110           && !COMPLETE_TYPE_P (new_scope)
5111           /* Do not try to complete dependent types.  */
5112           && !dependent_type_p (new_scope))
5113         {
5114           new_scope = complete_type (new_scope);
5115           /* If it is a typedef to current class, use the current
5116              class instead, as the typedef won't have any names inside
5117              it yet.  */
5118           if (!COMPLETE_TYPE_P (new_scope)
5119               && currently_open_class (new_scope))
5120             new_scope = TYPE_MAIN_VARIANT (new_scope);
5121         }
5122       /* Make sure we look in the right scope the next time through
5123          the loop.  */
5124       parser->scope = new_scope;
5125     }
5126
5127   /* If parsing tentatively, replace the sequence of tokens that makes
5128      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5129      token.  That way, should we re-parse the token stream, we will
5130      not have to repeat the effort required to do the parse, nor will
5131      we issue duplicate error messages.  */
5132   if (success && start)
5133     {
5134       cp_token *token;
5135
5136       token = cp_lexer_token_at (parser->lexer, start);
5137       /* Reset the contents of the START token.  */
5138       token->type = CPP_NESTED_NAME_SPECIFIER;
5139       /* Retrieve any deferred checks.  Do not pop this access checks yet
5140          so the memory will not be reclaimed during token replacing below.  */
5141       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5142       token->u.tree_check_value->value = parser->scope;
5143       token->u.tree_check_value->checks = get_deferred_access_checks ();
5144       token->u.tree_check_value->qualifying_scope =
5145         parser->qualifying_scope;
5146       token->keyword = RID_MAX;
5147
5148       /* Purge all subsequent tokens.  */
5149       cp_lexer_purge_tokens_after (parser->lexer, start);
5150     }
5151
5152   if (start)
5153     pop_to_parent_deferring_access_checks ();
5154
5155   return success ? parser->scope : NULL_TREE;
5156 }
5157
5158 /* Parse a nested-name-specifier.  See
5159    cp_parser_nested_name_specifier_opt for details.  This function
5160    behaves identically, except that it will an issue an error if no
5161    nested-name-specifier is present.  */
5162
5163 static tree
5164 cp_parser_nested_name_specifier (cp_parser *parser,
5165                                  bool typename_keyword_p,
5166                                  bool check_dependency_p,
5167                                  bool type_p,
5168                                  bool is_declaration)
5169 {
5170   tree scope;
5171
5172   /* Look for the nested-name-specifier.  */
5173   scope = cp_parser_nested_name_specifier_opt (parser,
5174                                                typename_keyword_p,
5175                                                check_dependency_p,
5176                                                type_p,
5177                                                is_declaration);
5178   /* If it was not present, issue an error message.  */
5179   if (!scope)
5180     {
5181       cp_parser_error (parser, "expected nested-name-specifier");
5182       parser->scope = NULL_TREE;
5183     }
5184
5185   return scope;
5186 }
5187
5188 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5189    this is either a class-name or a namespace-name (which corresponds
5190    to the class-or-namespace-name production in the grammar). For
5191    C++0x, it can also be a type-name that refers to an enumeration
5192    type or a simple-template-id.
5193
5194    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5195    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5196    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5197    TYPE_P is TRUE iff the next name should be taken as a class-name,
5198    even the same name is declared to be another entity in the same
5199    scope.
5200
5201    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5202    specified by the class-or-namespace-name.  If neither is found the
5203    ERROR_MARK_NODE is returned.  */
5204
5205 static tree
5206 cp_parser_qualifying_entity (cp_parser *parser,
5207                              bool typename_keyword_p,
5208                              bool template_keyword_p,
5209                              bool check_dependency_p,
5210                              bool type_p,
5211                              bool is_declaration)
5212 {
5213   tree saved_scope;
5214   tree saved_qualifying_scope;
5215   tree saved_object_scope;
5216   tree scope;
5217   bool only_class_p;
5218   bool successful_parse_p;
5219
5220   /* DR 743: decltype can appear in a nested-name-specifier.  */
5221   if (cp_lexer_next_token_is_decltype (parser->lexer))
5222     {
5223       scope = cp_parser_decltype (parser);
5224       if (TREE_CODE (scope) != ENUMERAL_TYPE
5225           && !MAYBE_CLASS_TYPE_P (scope))
5226         {
5227           cp_parser_simulate_error (parser);
5228           return error_mark_node;
5229         }
5230       if (TYPE_NAME (scope))
5231         scope = TYPE_NAME (scope);
5232       return scope;
5233     }
5234
5235   /* Before we try to parse the class-name, we must save away the
5236      current PARSER->SCOPE since cp_parser_class_name will destroy
5237      it.  */
5238   saved_scope = parser->scope;
5239   saved_qualifying_scope = parser->qualifying_scope;
5240   saved_object_scope = parser->object_scope;
5241   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5242      there is no need to look for a namespace-name.  */
5243   only_class_p = template_keyword_p 
5244     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5245   if (!only_class_p)
5246     cp_parser_parse_tentatively (parser);
5247   scope = cp_parser_class_name (parser,
5248                                 typename_keyword_p,
5249                                 template_keyword_p,
5250                                 type_p ? class_type : none_type,
5251                                 check_dependency_p,
5252                                 /*class_head_p=*/false,
5253                                 is_declaration);
5254   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5255   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5256   if (!only_class_p 
5257       && cxx_dialect != cxx98
5258       && !successful_parse_p)
5259     {
5260       /* Restore the saved scope.  */
5261       parser->scope = saved_scope;
5262       parser->qualifying_scope = saved_qualifying_scope;
5263       parser->object_scope = saved_object_scope;
5264
5265       /* Parse tentatively.  */
5266       cp_parser_parse_tentatively (parser);
5267      
5268       /* Parse a type-name  */
5269       scope = cp_parser_type_name (parser);
5270
5271       /* "If the name found does not designate a namespace or a class,
5272          enumeration, or dependent type, the program is ill-formed."
5273
5274          We cover classes and dependent types above and namespaces below,
5275          so this code is only looking for enums.  */
5276       if (!scope || TREE_CODE (scope) != TYPE_DECL
5277           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5278         cp_parser_simulate_error (parser);
5279
5280       successful_parse_p = cp_parser_parse_definitely (parser);
5281     }
5282   /* If that didn't work, try for a namespace-name.  */
5283   if (!only_class_p && !successful_parse_p)
5284     {
5285       /* Restore the saved scope.  */
5286       parser->scope = saved_scope;
5287       parser->qualifying_scope = saved_qualifying_scope;
5288       parser->object_scope = saved_object_scope;
5289       /* If we are not looking at an identifier followed by the scope
5290          resolution operator, then this is not part of a
5291          nested-name-specifier.  (Note that this function is only used
5292          to parse the components of a nested-name-specifier.)  */
5293       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5294           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5295         return error_mark_node;
5296       scope = cp_parser_namespace_name (parser);
5297     }
5298
5299   return scope;
5300 }
5301
5302 /* Parse a postfix-expression.
5303
5304    postfix-expression:
5305      primary-expression
5306      postfix-expression [ expression ]
5307      postfix-expression ( expression-list [opt] )
5308      simple-type-specifier ( expression-list [opt] )
5309      typename :: [opt] nested-name-specifier identifier
5310        ( expression-list [opt] )
5311      typename :: [opt] nested-name-specifier template [opt] template-id
5312        ( expression-list [opt] )
5313      postfix-expression . template [opt] id-expression
5314      postfix-expression -> template [opt] id-expression
5315      postfix-expression . pseudo-destructor-name
5316      postfix-expression -> pseudo-destructor-name
5317      postfix-expression ++
5318      postfix-expression --
5319      dynamic_cast < type-id > ( expression )
5320      static_cast < type-id > ( expression )
5321      reinterpret_cast < type-id > ( expression )
5322      const_cast < type-id > ( expression )
5323      typeid ( expression )
5324      typeid ( type-id )
5325
5326    GNU Extension:
5327
5328    postfix-expression:
5329      ( type-id ) { initializer-list , [opt] }
5330
5331    This extension is a GNU version of the C99 compound-literal
5332    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5333    but they are essentially the same concept.)
5334
5335    If ADDRESS_P is true, the postfix expression is the operand of the
5336    `&' operator.  CAST_P is true if this expression is the target of a
5337    cast.
5338
5339    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5340    class member access expressions [expr.ref].
5341
5342    Returns a representation of the expression.  */
5343
5344 static tree
5345 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5346                               bool member_access_only_p,
5347                               cp_id_kind * pidk_return)
5348 {
5349   cp_token *token;
5350   enum rid keyword;
5351   cp_id_kind idk = CP_ID_KIND_NONE;
5352   tree postfix_expression = NULL_TREE;
5353   bool is_member_access = false;
5354
5355   /* Peek at the next token.  */
5356   token = cp_lexer_peek_token (parser->lexer);
5357   /* Some of the productions are determined by keywords.  */
5358   keyword = token->keyword;
5359   switch (keyword)
5360     {
5361     case RID_DYNCAST:
5362     case RID_STATCAST:
5363     case RID_REINTCAST:
5364     case RID_CONSTCAST:
5365       {
5366         tree type;
5367         tree expression;
5368         const char *saved_message;
5369
5370         /* All of these can be handled in the same way from the point
5371            of view of parsing.  Begin by consuming the token
5372            identifying the cast.  */
5373         cp_lexer_consume_token (parser->lexer);
5374
5375         /* New types cannot be defined in the cast.  */
5376         saved_message = parser->type_definition_forbidden_message;
5377         parser->type_definition_forbidden_message
5378           = G_("types may not be defined in casts");
5379
5380         /* Look for the opening `<'.  */
5381         cp_parser_require (parser, CPP_LESS, RT_LESS);
5382         /* Parse the type to which we are casting.  */
5383         type = cp_parser_type_id (parser);
5384         /* Look for the closing `>'.  */
5385         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5386         /* Restore the old message.  */
5387         parser->type_definition_forbidden_message = saved_message;
5388
5389         /* And the expression which is being cast.  */
5390         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5391         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5392         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5393
5394         /* Only type conversions to integral or enumeration types
5395            can be used in constant-expressions.  */
5396         if (!cast_valid_in_integral_constant_expression_p (type)
5397             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5398           return error_mark_node;
5399
5400         switch (keyword)
5401           {
5402           case RID_DYNCAST:
5403             postfix_expression
5404               = build_dynamic_cast (type, expression, tf_warning_or_error);
5405             break;
5406           case RID_STATCAST:
5407             postfix_expression
5408               = build_static_cast (type, expression, tf_warning_or_error);
5409             break;
5410           case RID_REINTCAST:
5411             postfix_expression
5412               = build_reinterpret_cast (type, expression, 
5413                                         tf_warning_or_error);
5414             break;
5415           case RID_CONSTCAST:
5416             postfix_expression
5417               = build_const_cast (type, expression, tf_warning_or_error);
5418             break;
5419           default:
5420             gcc_unreachable ();
5421           }
5422       }
5423       break;
5424
5425     case RID_TYPEID:
5426       {
5427         tree type;
5428         const char *saved_message;
5429         bool saved_in_type_id_in_expr_p;
5430
5431         /* Consume the `typeid' token.  */
5432         cp_lexer_consume_token (parser->lexer);
5433         /* Look for the `(' token.  */
5434         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5435         /* Types cannot be defined in a `typeid' expression.  */
5436         saved_message = parser->type_definition_forbidden_message;
5437         parser->type_definition_forbidden_message
5438           = G_("types may not be defined in a %<typeid%> expression");
5439         /* We can't be sure yet whether we're looking at a type-id or an
5440            expression.  */
5441         cp_parser_parse_tentatively (parser);
5442         /* Try a type-id first.  */
5443         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5444         parser->in_type_id_in_expr_p = true;
5445         type = cp_parser_type_id (parser);
5446         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5447         /* Look for the `)' token.  Otherwise, we can't be sure that
5448            we're not looking at an expression: consider `typeid (int
5449            (3))', for example.  */
5450         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5451         /* If all went well, simply lookup the type-id.  */
5452         if (cp_parser_parse_definitely (parser))
5453           postfix_expression = get_typeid (type);
5454         /* Otherwise, fall back to the expression variant.  */
5455         else
5456           {
5457             tree expression;
5458
5459             /* Look for an expression.  */
5460             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5461             /* Compute its typeid.  */
5462             postfix_expression = build_typeid (expression);
5463             /* Look for the `)' token.  */
5464             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5465           }
5466         /* Restore the saved message.  */
5467         parser->type_definition_forbidden_message = saved_message;
5468         /* `typeid' may not appear in an integral constant expression.  */
5469         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5470           return error_mark_node;
5471       }
5472       break;
5473
5474     case RID_TYPENAME:
5475       {
5476         tree type;
5477         /* The syntax permitted here is the same permitted for an
5478            elaborated-type-specifier.  */
5479         type = cp_parser_elaborated_type_specifier (parser,
5480                                                     /*is_friend=*/false,
5481                                                     /*is_declaration=*/false);
5482         postfix_expression = cp_parser_functional_cast (parser, type);
5483       }
5484       break;
5485
5486     default:
5487       {
5488         tree type;
5489
5490         /* If the next thing is a simple-type-specifier, we may be
5491            looking at a functional cast.  We could also be looking at
5492            an id-expression.  So, we try the functional cast, and if
5493            that doesn't work we fall back to the primary-expression.  */
5494         cp_parser_parse_tentatively (parser);
5495         /* Look for the simple-type-specifier.  */
5496         type = cp_parser_simple_type_specifier (parser,
5497                                                 /*decl_specs=*/NULL,
5498                                                 CP_PARSER_FLAGS_NONE);
5499         /* Parse the cast itself.  */
5500         if (!cp_parser_error_occurred (parser))
5501           postfix_expression
5502             = cp_parser_functional_cast (parser, type);
5503         /* If that worked, we're done.  */
5504         if (cp_parser_parse_definitely (parser))
5505           break;
5506
5507         /* If the functional-cast didn't work out, try a
5508            compound-literal.  */
5509         if (cp_parser_allow_gnu_extensions_p (parser)
5510             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5511           {
5512             VEC(constructor_elt,gc) *initializer_list = NULL;
5513             bool saved_in_type_id_in_expr_p;
5514
5515             cp_parser_parse_tentatively (parser);
5516             /* Consume the `('.  */
5517             cp_lexer_consume_token (parser->lexer);
5518             /* Parse the type.  */
5519             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5520             parser->in_type_id_in_expr_p = true;
5521             type = cp_parser_type_id (parser);
5522             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5523             /* Look for the `)'.  */
5524             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5525             /* Look for the `{'.  */
5526             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5527             /* If things aren't going well, there's no need to
5528                keep going.  */
5529             if (!cp_parser_error_occurred (parser))
5530               {
5531                 bool non_constant_p;
5532                 /* Parse the initializer-list.  */
5533                 initializer_list
5534                   = cp_parser_initializer_list (parser, &non_constant_p);
5535                 /* Allow a trailing `,'.  */
5536                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5537                   cp_lexer_consume_token (parser->lexer);
5538                 /* Look for the final `}'.  */
5539                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5540               }
5541             /* If that worked, we're definitely looking at a
5542                compound-literal expression.  */
5543             if (cp_parser_parse_definitely (parser))
5544               {
5545                 /* Warn the user that a compound literal is not
5546                    allowed in standard C++.  */
5547                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5548                 /* For simplicity, we disallow compound literals in
5549                    constant-expressions.  We could
5550                    allow compound literals of integer type, whose
5551                    initializer was a constant, in constant
5552                    expressions.  Permitting that usage, as a further
5553                    extension, would not change the meaning of any
5554                    currently accepted programs.  (Of course, as
5555                    compound literals are not part of ISO C++, the
5556                    standard has nothing to say.)  */
5557                 if (cp_parser_non_integral_constant_expression (parser,
5558                                                                 NIC_NCC))
5559                   {
5560                     postfix_expression = error_mark_node;
5561                     break;
5562                   }
5563                 /* Form the representation of the compound-literal.  */
5564                 postfix_expression
5565                   = (finish_compound_literal
5566                      (type, build_constructor (init_list_type_node,
5567                                                initializer_list),
5568                       tf_warning_or_error));
5569                 break;
5570               }
5571           }
5572
5573         /* It must be a primary-expression.  */
5574         postfix_expression
5575           = cp_parser_primary_expression (parser, address_p, cast_p,
5576                                           /*template_arg_p=*/false,
5577                                           &idk);
5578       }
5579       break;
5580     }
5581
5582   /* Keep looping until the postfix-expression is complete.  */
5583   while (true)
5584     {
5585       if (idk == CP_ID_KIND_UNQUALIFIED
5586           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5587           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5588         /* It is not a Koenig lookup function call.  */
5589         postfix_expression
5590           = unqualified_name_lookup_error (postfix_expression);
5591
5592       /* Peek at the next token.  */
5593       token = cp_lexer_peek_token (parser->lexer);
5594
5595       switch (token->type)
5596         {
5597         case CPP_OPEN_SQUARE:
5598           postfix_expression
5599             = cp_parser_postfix_open_square_expression (parser,
5600                                                         postfix_expression,
5601                                                         false);
5602           idk = CP_ID_KIND_NONE;
5603           is_member_access = false;
5604           break;
5605
5606         case CPP_OPEN_PAREN:
5607           /* postfix-expression ( expression-list [opt] ) */
5608           {
5609             bool koenig_p;
5610             bool is_builtin_constant_p;
5611             bool saved_integral_constant_expression_p = false;
5612             bool saved_non_integral_constant_expression_p = false;
5613             VEC(tree,gc) *args;
5614
5615             is_member_access = false;
5616
5617             is_builtin_constant_p
5618               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5619             if (is_builtin_constant_p)
5620               {
5621                 /* The whole point of __builtin_constant_p is to allow
5622                    non-constant expressions to appear as arguments.  */
5623                 saved_integral_constant_expression_p
5624                   = parser->integral_constant_expression_p;
5625                 saved_non_integral_constant_expression_p
5626                   = parser->non_integral_constant_expression_p;
5627                 parser->integral_constant_expression_p = false;
5628               }
5629             args = (cp_parser_parenthesized_expression_list
5630                     (parser, non_attr,
5631                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5632                      /*non_constant_p=*/NULL));
5633             if (is_builtin_constant_p)
5634               {
5635                 parser->integral_constant_expression_p
5636                   = saved_integral_constant_expression_p;
5637                 parser->non_integral_constant_expression_p
5638                   = saved_non_integral_constant_expression_p;
5639               }
5640
5641             if (args == NULL)
5642               {
5643                 postfix_expression = error_mark_node;
5644                 break;
5645               }
5646
5647             /* Function calls are not permitted in
5648                constant-expressions.  */
5649             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5650                 && cp_parser_non_integral_constant_expression (parser,
5651                                                                NIC_FUNC_CALL))
5652               {
5653                 postfix_expression = error_mark_node;
5654                 release_tree_vector (args);
5655                 break;
5656               }
5657
5658             koenig_p = false;
5659             if (idk == CP_ID_KIND_UNQUALIFIED
5660                 || idk == CP_ID_KIND_TEMPLATE_ID)
5661               {
5662                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5663                   {
5664                     if (!VEC_empty (tree, args))
5665                       {
5666                         koenig_p = true;
5667                         if (!any_type_dependent_arguments_p (args))
5668                           postfix_expression
5669                             = perform_koenig_lookup (postfix_expression, args,
5670                                                      /*include_std=*/false,
5671                                                      tf_warning_or_error);
5672                       }
5673                     else
5674                       postfix_expression
5675                         = unqualified_fn_lookup_error (postfix_expression);
5676                   }
5677                 /* We do not perform argument-dependent lookup if
5678                    normal lookup finds a non-function, in accordance
5679                    with the expected resolution of DR 218.  */
5680                 else if (!VEC_empty (tree, args)
5681                          && is_overloaded_fn (postfix_expression))
5682                   {
5683                     tree fn = get_first_fn (postfix_expression);
5684                     fn = STRIP_TEMPLATE (fn);
5685
5686                     /* Do not do argument dependent lookup if regular
5687                        lookup finds a member function or a block-scope
5688                        function declaration.  [basic.lookup.argdep]/3  */
5689                     if (!DECL_FUNCTION_MEMBER_P (fn)
5690                         && !DECL_LOCAL_FUNCTION_P (fn))
5691                       {
5692                         koenig_p = true;
5693                         if (!any_type_dependent_arguments_p (args))
5694                           postfix_expression
5695                             = perform_koenig_lookup (postfix_expression, args,
5696                                                      /*include_std=*/false,
5697                                                      tf_warning_or_error);
5698                       }
5699                   }
5700               }
5701
5702             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5703               {
5704                 tree instance = TREE_OPERAND (postfix_expression, 0);
5705                 tree fn = TREE_OPERAND (postfix_expression, 1);
5706
5707                 if (processing_template_decl
5708                     && (type_dependent_expression_p (instance)
5709                         || (!BASELINK_P (fn)
5710                             && TREE_CODE (fn) != FIELD_DECL)
5711                         || type_dependent_expression_p (fn)
5712                         || any_type_dependent_arguments_p (args)))
5713                   {
5714                     postfix_expression
5715                       = build_nt_call_vec (postfix_expression, args);
5716                     release_tree_vector (args);
5717                     break;
5718                   }
5719
5720                 if (BASELINK_P (fn))
5721                   {
5722                   postfix_expression
5723                     = (build_new_method_call
5724                        (instance, fn, &args, NULL_TREE,
5725                         (idk == CP_ID_KIND_QUALIFIED
5726                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5727                          : LOOKUP_NORMAL),
5728                         /*fn_p=*/NULL,
5729                         tf_warning_or_error));
5730                   }
5731                 else
5732                   postfix_expression
5733                     = finish_call_expr (postfix_expression, &args,
5734                                         /*disallow_virtual=*/false,
5735                                         /*koenig_p=*/false,
5736                                         tf_warning_or_error);
5737               }
5738             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5739                      || TREE_CODE (postfix_expression) == MEMBER_REF
5740                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5741               postfix_expression = (build_offset_ref_call_from_tree
5742                                     (postfix_expression, &args));
5743             else if (idk == CP_ID_KIND_QUALIFIED)
5744               /* A call to a static class member, or a namespace-scope
5745                  function.  */
5746               postfix_expression
5747                 = finish_call_expr (postfix_expression, &args,
5748                                     /*disallow_virtual=*/true,
5749                                     koenig_p,
5750                                     tf_warning_or_error);
5751             else
5752               /* All other function calls.  */
5753               postfix_expression
5754                 = finish_call_expr (postfix_expression, &args,
5755                                     /*disallow_virtual=*/false,
5756                                     koenig_p,
5757                                     tf_warning_or_error);
5758
5759             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5760             idk = CP_ID_KIND_NONE;
5761
5762             release_tree_vector (args);
5763           }
5764           break;
5765
5766         case CPP_DOT:
5767         case CPP_DEREF:
5768           /* postfix-expression . template [opt] id-expression
5769              postfix-expression . pseudo-destructor-name
5770              postfix-expression -> template [opt] id-expression
5771              postfix-expression -> pseudo-destructor-name */
5772
5773           /* Consume the `.' or `->' operator.  */
5774           cp_lexer_consume_token (parser->lexer);
5775
5776           postfix_expression
5777             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5778                                                       postfix_expression,
5779                                                       false, &idk,
5780                                                       token->location);
5781
5782           is_member_access = true;
5783           break;
5784
5785         case CPP_PLUS_PLUS:
5786           /* postfix-expression ++  */
5787           /* Consume the `++' token.  */
5788           cp_lexer_consume_token (parser->lexer);
5789           /* Generate a representation for the complete expression.  */
5790           postfix_expression
5791             = finish_increment_expr (postfix_expression,
5792                                      POSTINCREMENT_EXPR);
5793           /* Increments may not appear in constant-expressions.  */
5794           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5795             postfix_expression = error_mark_node;
5796           idk = CP_ID_KIND_NONE;
5797           is_member_access = false;
5798           break;
5799
5800         case CPP_MINUS_MINUS:
5801           /* postfix-expression -- */
5802           /* Consume the `--' token.  */
5803           cp_lexer_consume_token (parser->lexer);
5804           /* Generate a representation for the complete expression.  */
5805           postfix_expression
5806             = finish_increment_expr (postfix_expression,
5807                                      POSTDECREMENT_EXPR);
5808           /* Decrements may not appear in constant-expressions.  */
5809           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5810             postfix_expression = error_mark_node;
5811           idk = CP_ID_KIND_NONE;
5812           is_member_access = false;
5813           break;
5814
5815         default:
5816           if (pidk_return != NULL)
5817             * pidk_return = idk;
5818           if (member_access_only_p)
5819             return is_member_access? postfix_expression : error_mark_node;
5820           else
5821             return postfix_expression;
5822         }
5823     }
5824
5825   /* We should never get here.  */
5826   gcc_unreachable ();
5827   return error_mark_node;
5828 }
5829
5830 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5831    by cp_parser_builtin_offsetof.  We're looking for
5832
5833      postfix-expression [ expression ]
5834
5835    FOR_OFFSETOF is set if we're being called in that context, which
5836    changes how we deal with integer constant expressions.  */
5837
5838 static tree
5839 cp_parser_postfix_open_square_expression (cp_parser *parser,
5840                                           tree postfix_expression,
5841                                           bool for_offsetof)
5842 {
5843   tree index;
5844
5845   /* Consume the `[' token.  */
5846   cp_lexer_consume_token (parser->lexer);
5847
5848   /* Parse the index expression.  */
5849   /* ??? For offsetof, there is a question of what to allow here.  If
5850      offsetof is not being used in an integral constant expression context,
5851      then we *could* get the right answer by computing the value at runtime.
5852      If we are in an integral constant expression context, then we might
5853      could accept any constant expression; hard to say without analysis.
5854      Rather than open the barn door too wide right away, allow only integer
5855      constant expressions here.  */
5856   if (for_offsetof)
5857     index = cp_parser_constant_expression (parser, false, NULL);
5858   else
5859     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5860
5861   /* Look for the closing `]'.  */
5862   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5863
5864   /* Build the ARRAY_REF.  */
5865   postfix_expression = grok_array_decl (postfix_expression, index);
5866
5867   /* When not doing offsetof, array references are not permitted in
5868      constant-expressions.  */
5869   if (!for_offsetof
5870       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5871     postfix_expression = error_mark_node;
5872
5873   return postfix_expression;
5874 }
5875
5876 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5877    by cp_parser_builtin_offsetof.  We're looking for
5878
5879      postfix-expression . template [opt] id-expression
5880      postfix-expression . pseudo-destructor-name
5881      postfix-expression -> template [opt] id-expression
5882      postfix-expression -> pseudo-destructor-name
5883
5884    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5885    limits what of the above we'll actually accept, but nevermind.
5886    TOKEN_TYPE is the "." or "->" token, which will already have been
5887    removed from the stream.  */
5888
5889 static tree
5890 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5891                                         enum cpp_ttype token_type,
5892                                         tree postfix_expression,
5893                                         bool for_offsetof, cp_id_kind *idk,
5894                                         location_t location)
5895 {
5896   tree name;
5897   bool dependent_p;
5898   bool pseudo_destructor_p;
5899   tree scope = NULL_TREE;
5900
5901   /* If this is a `->' operator, dereference the pointer.  */
5902   if (token_type == CPP_DEREF)
5903     postfix_expression = build_x_arrow (postfix_expression);
5904   /* Check to see whether or not the expression is type-dependent.  */
5905   dependent_p = type_dependent_expression_p (postfix_expression);
5906   /* The identifier following the `->' or `.' is not qualified.  */
5907   parser->scope = NULL_TREE;
5908   parser->qualifying_scope = NULL_TREE;
5909   parser->object_scope = NULL_TREE;
5910   *idk = CP_ID_KIND_NONE;
5911
5912   /* Enter the scope corresponding to the type of the object
5913      given by the POSTFIX_EXPRESSION.  */
5914   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5915     {
5916       scope = TREE_TYPE (postfix_expression);
5917       /* According to the standard, no expression should ever have
5918          reference type.  Unfortunately, we do not currently match
5919          the standard in this respect in that our internal representation
5920          of an expression may have reference type even when the standard
5921          says it does not.  Therefore, we have to manually obtain the
5922          underlying type here.  */
5923       scope = non_reference (scope);
5924       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5925       if (scope == unknown_type_node)
5926         {
5927           error_at (location, "%qE does not have class type",
5928                     postfix_expression);
5929           scope = NULL_TREE;
5930         }
5931       /* Unlike the object expression in other contexts, *this is not
5932          required to be of complete type for purposes of class member
5933          access (5.2.5) outside the member function body.  */
5934       else if (scope != current_class_ref
5935                && !(processing_template_decl && scope == current_class_type))
5936         scope = complete_type_or_else (scope, NULL_TREE);
5937       /* Let the name lookup machinery know that we are processing a
5938          class member access expression.  */
5939       parser->context->object_type = scope;
5940       /* If something went wrong, we want to be able to discern that case,
5941          as opposed to the case where there was no SCOPE due to the type
5942          of expression being dependent.  */
5943       if (!scope)
5944         scope = error_mark_node;
5945       /* If the SCOPE was erroneous, make the various semantic analysis
5946          functions exit quickly -- and without issuing additional error
5947          messages.  */
5948       if (scope == error_mark_node)
5949         postfix_expression = error_mark_node;
5950     }
5951
5952   /* Assume this expression is not a pseudo-destructor access.  */
5953   pseudo_destructor_p = false;
5954
5955   /* If the SCOPE is a scalar type, then, if this is a valid program,
5956      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5957      is type dependent, it can be pseudo-destructor-name or something else.
5958      Try to parse it as pseudo-destructor-name first.  */
5959   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5960     {
5961       tree s;
5962       tree type;
5963
5964       cp_parser_parse_tentatively (parser);
5965       /* Parse the pseudo-destructor-name.  */
5966       s = NULL_TREE;
5967       cp_parser_pseudo_destructor_name (parser, &s, &type);
5968       if (dependent_p
5969           && (cp_parser_error_occurred (parser)
5970               || TREE_CODE (type) != TYPE_DECL
5971               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5972         cp_parser_abort_tentative_parse (parser);
5973       else if (cp_parser_parse_definitely (parser))
5974         {
5975           pseudo_destructor_p = true;
5976           postfix_expression
5977             = finish_pseudo_destructor_expr (postfix_expression,
5978                                              s, TREE_TYPE (type));
5979         }
5980     }
5981
5982   if (!pseudo_destructor_p)
5983     {
5984       /* If the SCOPE is not a scalar type, we are looking at an
5985          ordinary class member access expression, rather than a
5986          pseudo-destructor-name.  */
5987       bool template_p;
5988       cp_token *token = cp_lexer_peek_token (parser->lexer);
5989       /* Parse the id-expression.  */
5990       name = (cp_parser_id_expression
5991               (parser,
5992                cp_parser_optional_template_keyword (parser),
5993                /*check_dependency_p=*/true,
5994                &template_p,
5995                /*declarator_p=*/false,
5996                /*optional_p=*/false));
5997       /* In general, build a SCOPE_REF if the member name is qualified.
5998          However, if the name was not dependent and has already been
5999          resolved; there is no need to build the SCOPE_REF.  For example;
6000
6001              struct X { void f(); };
6002              template <typename T> void f(T* t) { t->X::f(); }
6003
6004          Even though "t" is dependent, "X::f" is not and has been resolved
6005          to a BASELINK; there is no need to include scope information.  */
6006
6007       /* But we do need to remember that there was an explicit scope for
6008          virtual function calls.  */
6009       if (parser->scope)
6010         *idk = CP_ID_KIND_QUALIFIED;
6011
6012       /* If the name is a template-id that names a type, we will get a
6013          TYPE_DECL here.  That is invalid code.  */
6014       if (TREE_CODE (name) == TYPE_DECL)
6015         {
6016           error_at (token->location, "invalid use of %qD", name);
6017           postfix_expression = error_mark_node;
6018         }
6019       else
6020         {
6021           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6022             {
6023               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6024                 {
6025                   error_at (token->location, "%<%D::%D%> is not a class member",
6026                             parser->scope, name);
6027                   postfix_expression = error_mark_node;
6028                 }
6029               else
6030                 name = build_qualified_name (/*type=*/NULL_TREE,
6031                                              parser->scope,
6032                                              name,
6033                                              template_p);
6034               parser->scope = NULL_TREE;
6035               parser->qualifying_scope = NULL_TREE;
6036               parser->object_scope = NULL_TREE;
6037             }
6038           if (scope && name && BASELINK_P (name))
6039             adjust_result_of_qualified_name_lookup
6040               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
6041           postfix_expression
6042             = finish_class_member_access_expr (postfix_expression, name,
6043                                                template_p, 
6044                                                tf_warning_or_error);
6045         }
6046     }
6047
6048   /* We no longer need to look up names in the scope of the object on
6049      the left-hand side of the `.' or `->' operator.  */
6050   parser->context->object_type = NULL_TREE;
6051
6052   /* Outside of offsetof, these operators may not appear in
6053      constant-expressions.  */
6054   if (!for_offsetof
6055       && (cp_parser_non_integral_constant_expression
6056           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6057     postfix_expression = error_mark_node;
6058
6059   return postfix_expression;
6060 }
6061
6062 /* Parse a parenthesized expression-list.
6063
6064    expression-list:
6065      assignment-expression
6066      expression-list, assignment-expression
6067
6068    attribute-list:
6069      expression-list
6070      identifier
6071      identifier, expression-list
6072
6073    CAST_P is true if this expression is the target of a cast.
6074
6075    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6076    argument pack.
6077
6078    Returns a vector of trees.  Each element is a representation of an
6079    assignment-expression.  NULL is returned if the ( and or ) are
6080    missing.  An empty, but allocated, vector is returned on no
6081    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6082    if we are parsing an attribute list for an attribute that wants a
6083    plain identifier argument, normal_attr for an attribute that wants
6084    an expression, or non_attr if we aren't parsing an attribute list.  If
6085    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6086    not all of the expressions in the list were constant.  */
6087
6088 static VEC(tree,gc) *
6089 cp_parser_parenthesized_expression_list (cp_parser* parser,
6090                                          int is_attribute_list,
6091                                          bool cast_p,
6092                                          bool allow_expansion_p,
6093                                          bool *non_constant_p)
6094 {
6095   VEC(tree,gc) *expression_list;
6096   bool fold_expr_p = is_attribute_list != non_attr;
6097   tree identifier = NULL_TREE;
6098   bool saved_greater_than_is_operator_p;
6099
6100   /* Assume all the expressions will be constant.  */
6101   if (non_constant_p)
6102     *non_constant_p = false;
6103
6104   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6105     return NULL;
6106
6107   expression_list = make_tree_vector ();
6108
6109   /* Within a parenthesized expression, a `>' token is always
6110      the greater-than operator.  */
6111   saved_greater_than_is_operator_p
6112     = parser->greater_than_is_operator_p;
6113   parser->greater_than_is_operator_p = true;
6114
6115   /* Consume expressions until there are no more.  */
6116   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6117     while (true)
6118       {
6119         tree expr;
6120
6121         /* At the beginning of attribute lists, check to see if the
6122            next token is an identifier.  */
6123         if (is_attribute_list == id_attr
6124             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6125           {
6126             cp_token *token;
6127
6128             /* Consume the identifier.  */
6129             token = cp_lexer_consume_token (parser->lexer);
6130             /* Save the identifier.  */
6131             identifier = token->u.value;
6132           }
6133         else
6134           {
6135             bool expr_non_constant_p;
6136
6137             /* Parse the next assignment-expression.  */
6138             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6139               {
6140                 /* A braced-init-list.  */
6141                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6142                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6143                 if (non_constant_p && expr_non_constant_p)
6144                   *non_constant_p = true;
6145               }
6146             else if (non_constant_p)
6147               {
6148                 expr = (cp_parser_constant_expression
6149                         (parser, /*allow_non_constant_p=*/true,
6150                          &expr_non_constant_p));
6151                 if (expr_non_constant_p)
6152                   *non_constant_p = true;
6153               }
6154             else
6155               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6156
6157             if (fold_expr_p)
6158               expr = fold_non_dependent_expr (expr);
6159
6160             /* If we have an ellipsis, then this is an expression
6161                expansion.  */
6162             if (allow_expansion_p
6163                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6164               {
6165                 /* Consume the `...'.  */
6166                 cp_lexer_consume_token (parser->lexer);
6167
6168                 /* Build the argument pack.  */
6169                 expr = make_pack_expansion (expr);
6170               }
6171
6172              /* Add it to the list.  We add error_mark_node
6173                 expressions to the list, so that we can still tell if
6174                 the correct form for a parenthesized expression-list
6175                 is found. That gives better errors.  */
6176             VEC_safe_push (tree, gc, expression_list, expr);
6177
6178             if (expr == error_mark_node)
6179               goto skip_comma;
6180           }
6181
6182         /* After the first item, attribute lists look the same as
6183            expression lists.  */
6184         is_attribute_list = non_attr;
6185
6186       get_comma:;
6187         /* If the next token isn't a `,', then we are done.  */
6188         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6189           break;
6190
6191         /* Otherwise, consume the `,' and keep going.  */
6192         cp_lexer_consume_token (parser->lexer);
6193       }
6194
6195   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6196     {
6197       int ending;
6198
6199     skip_comma:;
6200       /* We try and resync to an unnested comma, as that will give the
6201          user better diagnostics.  */
6202       ending = cp_parser_skip_to_closing_parenthesis (parser,
6203                                                       /*recovering=*/true,
6204                                                       /*or_comma=*/true,
6205                                                       /*consume_paren=*/true);
6206       if (ending < 0)
6207         goto get_comma;
6208       if (!ending)
6209         {
6210           parser->greater_than_is_operator_p
6211             = saved_greater_than_is_operator_p;
6212           return NULL;
6213         }
6214     }
6215
6216   parser->greater_than_is_operator_p
6217     = saved_greater_than_is_operator_p;
6218
6219   if (identifier)
6220     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6221
6222   return expression_list;
6223 }
6224
6225 /* Parse a pseudo-destructor-name.
6226
6227    pseudo-destructor-name:
6228      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6229      :: [opt] nested-name-specifier template template-id :: ~ type-name
6230      :: [opt] nested-name-specifier [opt] ~ type-name
6231
6232    If either of the first two productions is used, sets *SCOPE to the
6233    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6234    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6235    or ERROR_MARK_NODE if the parse fails.  */
6236
6237 static void
6238 cp_parser_pseudo_destructor_name (cp_parser* parser,
6239                                   tree* scope,
6240                                   tree* type)
6241 {
6242   bool nested_name_specifier_p;
6243
6244   /* Assume that things will not work out.  */
6245   *type = error_mark_node;
6246
6247   /* Look for the optional `::' operator.  */
6248   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6249   /* Look for the optional nested-name-specifier.  */
6250   nested_name_specifier_p
6251     = (cp_parser_nested_name_specifier_opt (parser,
6252                                             /*typename_keyword_p=*/false,
6253                                             /*check_dependency_p=*/true,
6254                                             /*type_p=*/false,
6255                                             /*is_declaration=*/false)
6256        != NULL_TREE);
6257   /* Now, if we saw a nested-name-specifier, we might be doing the
6258      second production.  */
6259   if (nested_name_specifier_p
6260       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6261     {
6262       /* Consume the `template' keyword.  */
6263       cp_lexer_consume_token (parser->lexer);
6264       /* Parse the template-id.  */
6265       cp_parser_template_id (parser,
6266                              /*template_keyword_p=*/true,
6267                              /*check_dependency_p=*/false,
6268                              /*is_declaration=*/true);
6269       /* Look for the `::' token.  */
6270       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6271     }
6272   /* If the next token is not a `~', then there might be some
6273      additional qualification.  */
6274   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6275     {
6276       /* At this point, we're looking for "type-name :: ~".  The type-name
6277          must not be a class-name, since this is a pseudo-destructor.  So,
6278          it must be either an enum-name, or a typedef-name -- both of which
6279          are just identifiers.  So, we peek ahead to check that the "::"
6280          and "~" tokens are present; if they are not, then we can avoid
6281          calling type_name.  */
6282       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6283           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6284           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6285         {
6286           cp_parser_error (parser, "non-scalar type");
6287           return;
6288         }
6289
6290       /* Look for the type-name.  */
6291       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6292       if (*scope == error_mark_node)
6293         return;
6294
6295       /* Look for the `::' token.  */
6296       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6297     }
6298   else
6299     *scope = NULL_TREE;
6300
6301   /* Look for the `~'.  */
6302   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6303
6304   /* Once we see the ~, this has to be a pseudo-destructor.  */
6305   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6306     cp_parser_commit_to_tentative_parse (parser);
6307
6308   /* Look for the type-name again.  We are not responsible for
6309      checking that it matches the first type-name.  */
6310   *type = cp_parser_nonclass_name (parser);
6311 }
6312
6313 /* Parse a unary-expression.
6314
6315    unary-expression:
6316      postfix-expression
6317      ++ cast-expression
6318      -- cast-expression
6319      unary-operator cast-expression
6320      sizeof unary-expression
6321      sizeof ( type-id )
6322      alignof ( type-id )  [C++0x]
6323      new-expression
6324      delete-expression
6325
6326    GNU Extensions:
6327
6328    unary-expression:
6329      __extension__ cast-expression
6330      __alignof__ unary-expression
6331      __alignof__ ( type-id )
6332      alignof unary-expression  [C++0x]
6333      __real__ cast-expression
6334      __imag__ cast-expression
6335      && identifier
6336
6337    ADDRESS_P is true iff the unary-expression is appearing as the
6338    operand of the `&' operator.   CAST_P is true if this expression is
6339    the target of a cast.
6340
6341    Returns a representation of the expression.  */
6342
6343 static tree
6344 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6345                             cp_id_kind * pidk)
6346 {
6347   cp_token *token;
6348   enum tree_code unary_operator;
6349
6350   /* Peek at the next token.  */
6351   token = cp_lexer_peek_token (parser->lexer);
6352   /* Some keywords give away the kind of expression.  */
6353   if (token->type == CPP_KEYWORD)
6354     {
6355       enum rid keyword = token->keyword;
6356
6357       switch (keyword)
6358         {
6359         case RID_ALIGNOF:
6360         case RID_SIZEOF:
6361           {
6362             tree operand;
6363             enum tree_code op;
6364
6365             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6366             /* Consume the token.  */
6367             cp_lexer_consume_token (parser->lexer);
6368             /* Parse the operand.  */
6369             operand = cp_parser_sizeof_operand (parser, keyword);
6370
6371             if (TYPE_P (operand))
6372               return cxx_sizeof_or_alignof_type (operand, op, true);
6373             else
6374               {
6375                 /* ISO C++ defines alignof only with types, not with
6376                    expressions. So pedwarn if alignof is used with a non-
6377                    type expression. However, __alignof__ is ok.  */
6378                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6379                   pedwarn (token->location, OPT_pedantic,
6380                            "ISO C++ does not allow %<alignof%> "
6381                            "with a non-type");
6382
6383                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6384               }
6385           }
6386
6387         case RID_NEW:
6388           return cp_parser_new_expression (parser);
6389
6390         case RID_DELETE:
6391           return cp_parser_delete_expression (parser);
6392
6393         case RID_EXTENSION:
6394           {
6395             /* The saved value of the PEDANTIC flag.  */
6396             int saved_pedantic;
6397             tree expr;
6398
6399             /* Save away the PEDANTIC flag.  */
6400             cp_parser_extension_opt (parser, &saved_pedantic);
6401             /* Parse the cast-expression.  */
6402             expr = cp_parser_simple_cast_expression (parser);
6403             /* Restore the PEDANTIC flag.  */
6404             pedantic = saved_pedantic;
6405
6406             return expr;
6407           }
6408
6409         case RID_REALPART:
6410         case RID_IMAGPART:
6411           {
6412             tree expression;
6413
6414             /* Consume the `__real__' or `__imag__' token.  */
6415             cp_lexer_consume_token (parser->lexer);
6416             /* Parse the cast-expression.  */
6417             expression = cp_parser_simple_cast_expression (parser);
6418             /* Create the complete representation.  */
6419             return build_x_unary_op ((keyword == RID_REALPART
6420                                       ? REALPART_EXPR : IMAGPART_EXPR),
6421                                      expression,
6422                                      tf_warning_or_error);
6423           }
6424           break;
6425
6426         case RID_TRANSACTION_ATOMIC:
6427         case RID_TRANSACTION_RELAXED:
6428           return cp_parser_transaction_expression (parser, keyword);
6429
6430         case RID_NOEXCEPT:
6431           {
6432             tree expr;
6433             const char *saved_message;
6434             bool saved_integral_constant_expression_p;
6435             bool saved_non_integral_constant_expression_p;
6436             bool saved_greater_than_is_operator_p;
6437
6438             cp_lexer_consume_token (parser->lexer);
6439             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6440
6441             saved_message = parser->type_definition_forbidden_message;
6442             parser->type_definition_forbidden_message
6443               = G_("types may not be defined in %<noexcept%> expressions");
6444
6445             saved_integral_constant_expression_p
6446               = parser->integral_constant_expression_p;
6447             saved_non_integral_constant_expression_p
6448               = parser->non_integral_constant_expression_p;
6449             parser->integral_constant_expression_p = false;
6450
6451             saved_greater_than_is_operator_p
6452               = parser->greater_than_is_operator_p;
6453             parser->greater_than_is_operator_p = true;
6454
6455             ++cp_unevaluated_operand;
6456             ++c_inhibit_evaluation_warnings;
6457             expr = cp_parser_expression (parser, false, NULL);
6458             --c_inhibit_evaluation_warnings;
6459             --cp_unevaluated_operand;
6460
6461             parser->greater_than_is_operator_p
6462               = saved_greater_than_is_operator_p;
6463
6464             parser->integral_constant_expression_p
6465               = saved_integral_constant_expression_p;
6466             parser->non_integral_constant_expression_p
6467               = saved_non_integral_constant_expression_p;
6468
6469             parser->type_definition_forbidden_message = saved_message;
6470
6471             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6472             return finish_noexcept_expr (expr, tf_warning_or_error);
6473           }
6474
6475         default:
6476           break;
6477         }
6478     }
6479
6480   /* Look for the `:: new' and `:: delete', which also signal the
6481      beginning of a new-expression, or delete-expression,
6482      respectively.  If the next token is `::', then it might be one of
6483      these.  */
6484   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6485     {
6486       enum rid keyword;
6487
6488       /* See if the token after the `::' is one of the keywords in
6489          which we're interested.  */
6490       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6491       /* If it's `new', we have a new-expression.  */
6492       if (keyword == RID_NEW)
6493         return cp_parser_new_expression (parser);
6494       /* Similarly, for `delete'.  */
6495       else if (keyword == RID_DELETE)
6496         return cp_parser_delete_expression (parser);
6497     }
6498
6499   /* Look for a unary operator.  */
6500   unary_operator = cp_parser_unary_operator (token);
6501   /* The `++' and `--' operators can be handled similarly, even though
6502      they are not technically unary-operators in the grammar.  */
6503   if (unary_operator == ERROR_MARK)
6504     {
6505       if (token->type == CPP_PLUS_PLUS)
6506         unary_operator = PREINCREMENT_EXPR;
6507       else if (token->type == CPP_MINUS_MINUS)
6508         unary_operator = PREDECREMENT_EXPR;
6509       /* Handle the GNU address-of-label extension.  */
6510       else if (cp_parser_allow_gnu_extensions_p (parser)
6511                && token->type == CPP_AND_AND)
6512         {
6513           tree identifier;
6514           tree expression;
6515           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6516
6517           /* Consume the '&&' token.  */
6518           cp_lexer_consume_token (parser->lexer);
6519           /* Look for the identifier.  */
6520           identifier = cp_parser_identifier (parser);
6521           /* Create an expression representing the address.  */
6522           expression = finish_label_address_expr (identifier, loc);
6523           if (cp_parser_non_integral_constant_expression (parser,
6524                                                           NIC_ADDR_LABEL))
6525             expression = error_mark_node;
6526           return expression;
6527         }
6528     }
6529   if (unary_operator != ERROR_MARK)
6530     {
6531       tree cast_expression;
6532       tree expression = error_mark_node;
6533       non_integral_constant non_constant_p = NIC_NONE;
6534
6535       /* Consume the operator token.  */
6536       token = cp_lexer_consume_token (parser->lexer);
6537       /* Parse the cast-expression.  */
6538       cast_expression
6539         = cp_parser_cast_expression (parser,
6540                                      unary_operator == ADDR_EXPR,
6541                                      /*cast_p=*/false, pidk);
6542       /* Now, build an appropriate representation.  */
6543       switch (unary_operator)
6544         {
6545         case INDIRECT_REF:
6546           non_constant_p = NIC_STAR;
6547           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6548                                              tf_warning_or_error);
6549           break;
6550
6551         case ADDR_EXPR:
6552            non_constant_p = NIC_ADDR;
6553           /* Fall through.  */
6554         case BIT_NOT_EXPR:
6555           expression = build_x_unary_op (unary_operator, cast_expression,
6556                                          tf_warning_or_error);
6557           break;
6558
6559         case PREINCREMENT_EXPR:
6560         case PREDECREMENT_EXPR:
6561           non_constant_p = unary_operator == PREINCREMENT_EXPR
6562                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6563           /* Fall through.  */
6564         case UNARY_PLUS_EXPR:
6565         case NEGATE_EXPR:
6566         case TRUTH_NOT_EXPR:
6567           expression = finish_unary_op_expr (unary_operator, cast_expression);
6568           break;
6569
6570         default:
6571           gcc_unreachable ();
6572         }
6573
6574       if (non_constant_p != NIC_NONE
6575           && cp_parser_non_integral_constant_expression (parser,
6576                                                          non_constant_p))
6577         expression = error_mark_node;
6578
6579       return expression;
6580     }
6581
6582   return cp_parser_postfix_expression (parser, address_p, cast_p,
6583                                        /*member_access_only_p=*/false,
6584                                        pidk);
6585 }
6586
6587 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6588    unary-operator, the corresponding tree code is returned.  */
6589
6590 static enum tree_code
6591 cp_parser_unary_operator (cp_token* token)
6592 {
6593   switch (token->type)
6594     {
6595     case CPP_MULT:
6596       return INDIRECT_REF;
6597
6598     case CPP_AND:
6599       return ADDR_EXPR;
6600
6601     case CPP_PLUS:
6602       return UNARY_PLUS_EXPR;
6603
6604     case CPP_MINUS:
6605       return NEGATE_EXPR;
6606
6607     case CPP_NOT:
6608       return TRUTH_NOT_EXPR;
6609
6610     case CPP_COMPL:
6611       return BIT_NOT_EXPR;
6612
6613     default:
6614       return ERROR_MARK;
6615     }
6616 }
6617
6618 /* Parse a new-expression.
6619
6620    new-expression:
6621      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6622      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6623
6624    Returns a representation of the expression.  */
6625
6626 static tree
6627 cp_parser_new_expression (cp_parser* parser)
6628 {
6629   bool global_scope_p;
6630   VEC(tree,gc) *placement;
6631   tree type;
6632   VEC(tree,gc) *initializer;
6633   tree nelts;
6634   tree ret;
6635
6636   /* Look for the optional `::' operator.  */
6637   global_scope_p
6638     = (cp_parser_global_scope_opt (parser,
6639                                    /*current_scope_valid_p=*/false)
6640        != NULL_TREE);
6641   /* Look for the `new' operator.  */
6642   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6643   /* There's no easy way to tell a new-placement from the
6644      `( type-id )' construct.  */
6645   cp_parser_parse_tentatively (parser);
6646   /* Look for a new-placement.  */
6647   placement = cp_parser_new_placement (parser);
6648   /* If that didn't work out, there's no new-placement.  */
6649   if (!cp_parser_parse_definitely (parser))
6650     {
6651       if (placement != NULL)
6652         release_tree_vector (placement);
6653       placement = NULL;
6654     }
6655
6656   /* If the next token is a `(', then we have a parenthesized
6657      type-id.  */
6658   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6659     {
6660       cp_token *token;
6661       /* Consume the `('.  */
6662       cp_lexer_consume_token (parser->lexer);
6663       /* Parse the type-id.  */
6664       type = cp_parser_type_id (parser);
6665       /* Look for the closing `)'.  */
6666       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6667       token = cp_lexer_peek_token (parser->lexer);
6668       /* There should not be a direct-new-declarator in this production,
6669          but GCC used to allowed this, so we check and emit a sensible error
6670          message for this case.  */
6671       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6672         {
6673           error_at (token->location,
6674                     "array bound forbidden after parenthesized type-id");
6675           inform (token->location, 
6676                   "try removing the parentheses around the type-id");
6677           cp_parser_direct_new_declarator (parser);
6678         }
6679       nelts = NULL_TREE;
6680     }
6681   /* Otherwise, there must be a new-type-id.  */
6682   else
6683     type = cp_parser_new_type_id (parser, &nelts);
6684
6685   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6686   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6687       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6688     initializer = cp_parser_new_initializer (parser);
6689   else
6690     initializer = NULL;
6691
6692   /* A new-expression may not appear in an integral constant
6693      expression.  */
6694   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6695     ret = error_mark_node;
6696   else
6697     {
6698       /* Create a representation of the new-expression.  */
6699       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6700                        tf_warning_or_error);
6701     }
6702
6703   if (placement != NULL)
6704     release_tree_vector (placement);
6705   if (initializer != NULL)
6706     release_tree_vector (initializer);
6707
6708   return ret;
6709 }
6710
6711 /* Parse a new-placement.
6712
6713    new-placement:
6714      ( expression-list )
6715
6716    Returns the same representation as for an expression-list.  */
6717
6718 static VEC(tree,gc) *
6719 cp_parser_new_placement (cp_parser* parser)
6720 {
6721   VEC(tree,gc) *expression_list;
6722
6723   /* Parse the expression-list.  */
6724   expression_list = (cp_parser_parenthesized_expression_list
6725                      (parser, non_attr, /*cast_p=*/false,
6726                       /*allow_expansion_p=*/true,
6727                       /*non_constant_p=*/NULL));
6728
6729   return expression_list;
6730 }
6731
6732 /* Parse a new-type-id.
6733
6734    new-type-id:
6735      type-specifier-seq new-declarator [opt]
6736
6737    Returns the TYPE allocated.  If the new-type-id indicates an array
6738    type, *NELTS is set to the number of elements in the last array
6739    bound; the TYPE will not include the last array bound.  */
6740
6741 static tree
6742 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6743 {
6744   cp_decl_specifier_seq type_specifier_seq;
6745   cp_declarator *new_declarator;
6746   cp_declarator *declarator;
6747   cp_declarator *outer_declarator;
6748   const char *saved_message;
6749   tree type;
6750
6751   /* The type-specifier sequence must not contain type definitions.
6752      (It cannot contain declarations of new types either, but if they
6753      are not definitions we will catch that because they are not
6754      complete.)  */
6755   saved_message = parser->type_definition_forbidden_message;
6756   parser->type_definition_forbidden_message
6757     = G_("types may not be defined in a new-type-id");
6758   /* Parse the type-specifier-seq.  */
6759   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6760                                 /*is_trailing_return=*/false,
6761                                 &type_specifier_seq);
6762   /* Restore the old message.  */
6763   parser->type_definition_forbidden_message = saved_message;
6764   /* Parse the new-declarator.  */
6765   new_declarator = cp_parser_new_declarator_opt (parser);
6766
6767   /* Determine the number of elements in the last array dimension, if
6768      any.  */
6769   *nelts = NULL_TREE;
6770   /* Skip down to the last array dimension.  */
6771   declarator = new_declarator;
6772   outer_declarator = NULL;
6773   while (declarator && (declarator->kind == cdk_pointer
6774                         || declarator->kind == cdk_ptrmem))
6775     {
6776       outer_declarator = declarator;
6777       declarator = declarator->declarator;
6778     }
6779   while (declarator
6780          && declarator->kind == cdk_array
6781          && declarator->declarator
6782          && declarator->declarator->kind == cdk_array)
6783     {
6784       outer_declarator = declarator;
6785       declarator = declarator->declarator;
6786     }
6787
6788   if (declarator && declarator->kind == cdk_array)
6789     {
6790       *nelts = declarator->u.array.bounds;
6791       if (*nelts == error_mark_node)
6792         *nelts = integer_one_node;
6793
6794       if (outer_declarator)
6795         outer_declarator->declarator = declarator->declarator;
6796       else
6797         new_declarator = NULL;
6798     }
6799
6800   type = groktypename (&type_specifier_seq, new_declarator, false);
6801   return type;
6802 }
6803
6804 /* Parse an (optional) new-declarator.
6805
6806    new-declarator:
6807      ptr-operator new-declarator [opt]
6808      direct-new-declarator
6809
6810    Returns the declarator.  */
6811
6812 static cp_declarator *
6813 cp_parser_new_declarator_opt (cp_parser* parser)
6814 {
6815   enum tree_code code;
6816   tree type;
6817   cp_cv_quals cv_quals;
6818
6819   /* We don't know if there's a ptr-operator next, or not.  */
6820   cp_parser_parse_tentatively (parser);
6821   /* Look for a ptr-operator.  */
6822   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6823   /* If that worked, look for more new-declarators.  */
6824   if (cp_parser_parse_definitely (parser))
6825     {
6826       cp_declarator *declarator;
6827
6828       /* Parse another optional declarator.  */
6829       declarator = cp_parser_new_declarator_opt (parser);
6830
6831       return cp_parser_make_indirect_declarator
6832         (code, type, cv_quals, declarator);
6833     }
6834
6835   /* If the next token is a `[', there is a direct-new-declarator.  */
6836   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6837     return cp_parser_direct_new_declarator (parser);
6838
6839   return NULL;
6840 }
6841
6842 /* Parse a direct-new-declarator.
6843
6844    direct-new-declarator:
6845      [ expression ]
6846      direct-new-declarator [constant-expression]
6847
6848    */
6849
6850 static cp_declarator *
6851 cp_parser_direct_new_declarator (cp_parser* parser)
6852 {
6853   cp_declarator *declarator = NULL;
6854
6855   while (true)
6856     {
6857       tree expression;
6858
6859       /* Look for the opening `['.  */
6860       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6861       /* The first expression is not required to be constant.  */
6862       if (!declarator)
6863         {
6864           cp_token *token = cp_lexer_peek_token (parser->lexer);
6865           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6866           /* The standard requires that the expression have integral
6867              type.  DR 74 adds enumeration types.  We believe that the
6868              real intent is that these expressions be handled like the
6869              expression in a `switch' condition, which also allows
6870              classes with a single conversion to integral or
6871              enumeration type.  */
6872           if (!processing_template_decl)
6873             {
6874               expression
6875                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6876                                               expression,
6877                                               /*complain=*/true);
6878               if (!expression)
6879                 {
6880                   error_at (token->location,
6881                             "expression in new-declarator must have integral "
6882                             "or enumeration type");
6883                   expression = error_mark_node;
6884                 }
6885             }
6886         }
6887       /* But all the other expressions must be.  */
6888       else
6889         expression
6890           = cp_parser_constant_expression (parser,
6891                                            /*allow_non_constant=*/false,
6892                                            NULL);
6893       /* Look for the closing `]'.  */
6894       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6895
6896       /* Add this bound to the declarator.  */
6897       declarator = make_array_declarator (declarator, expression);
6898
6899       /* If the next token is not a `[', then there are no more
6900          bounds.  */
6901       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6902         break;
6903     }
6904
6905   return declarator;
6906 }
6907
6908 /* Parse a new-initializer.
6909
6910    new-initializer:
6911      ( expression-list [opt] )
6912      braced-init-list
6913
6914    Returns a representation of the expression-list.  */
6915
6916 static VEC(tree,gc) *
6917 cp_parser_new_initializer (cp_parser* parser)
6918 {
6919   VEC(tree,gc) *expression_list;
6920
6921   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6922     {
6923       tree t;
6924       bool expr_non_constant_p;
6925       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6926       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6927       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6928       expression_list = make_tree_vector_single (t);
6929     }
6930   else
6931     expression_list = (cp_parser_parenthesized_expression_list
6932                        (parser, non_attr, /*cast_p=*/false,
6933                         /*allow_expansion_p=*/true,
6934                         /*non_constant_p=*/NULL));
6935
6936   return expression_list;
6937 }
6938
6939 /* Parse a delete-expression.
6940
6941    delete-expression:
6942      :: [opt] delete cast-expression
6943      :: [opt] delete [ ] cast-expression
6944
6945    Returns a representation of the expression.  */
6946
6947 static tree
6948 cp_parser_delete_expression (cp_parser* parser)
6949 {
6950   bool global_scope_p;
6951   bool array_p;
6952   tree expression;
6953
6954   /* Look for the optional `::' operator.  */
6955   global_scope_p
6956     = (cp_parser_global_scope_opt (parser,
6957                                    /*current_scope_valid_p=*/false)
6958        != NULL_TREE);
6959   /* Look for the `delete' keyword.  */
6960   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6961   /* See if the array syntax is in use.  */
6962   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6963     {
6964       /* Consume the `[' token.  */
6965       cp_lexer_consume_token (parser->lexer);
6966       /* Look for the `]' token.  */
6967       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6968       /* Remember that this is the `[]' construct.  */
6969       array_p = true;
6970     }
6971   else
6972     array_p = false;
6973
6974   /* Parse the cast-expression.  */
6975   expression = cp_parser_simple_cast_expression (parser);
6976
6977   /* A delete-expression may not appear in an integral constant
6978      expression.  */
6979   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6980     return error_mark_node;
6981
6982   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6983                         tf_warning_or_error);
6984 }
6985
6986 /* Returns true if TOKEN may start a cast-expression and false
6987    otherwise.  */
6988
6989 static bool
6990 cp_parser_token_starts_cast_expression (cp_token *token)
6991 {
6992   switch (token->type)
6993     {
6994     case CPP_COMMA:
6995     case CPP_SEMICOLON:
6996     case CPP_QUERY:
6997     case CPP_COLON:
6998     case CPP_CLOSE_SQUARE:
6999     case CPP_CLOSE_PAREN:
7000     case CPP_CLOSE_BRACE:
7001     case CPP_DOT:
7002     case CPP_DOT_STAR:
7003     case CPP_DEREF:
7004     case CPP_DEREF_STAR:
7005     case CPP_DIV:
7006     case CPP_MOD:
7007     case CPP_LSHIFT:
7008     case CPP_RSHIFT:
7009     case CPP_LESS:
7010     case CPP_GREATER:
7011     case CPP_LESS_EQ:
7012     case CPP_GREATER_EQ:
7013     case CPP_EQ_EQ:
7014     case CPP_NOT_EQ:
7015     case CPP_EQ:
7016     case CPP_MULT_EQ:
7017     case CPP_DIV_EQ:
7018     case CPP_MOD_EQ:
7019     case CPP_PLUS_EQ:
7020     case CPP_MINUS_EQ:
7021     case CPP_RSHIFT_EQ:
7022     case CPP_LSHIFT_EQ:
7023     case CPP_AND_EQ:
7024     case CPP_XOR_EQ:
7025     case CPP_OR_EQ:
7026     case CPP_XOR:
7027     case CPP_OR:
7028     case CPP_OR_OR:
7029     case CPP_EOF:
7030       return false;
7031
7032       /* '[' may start a primary-expression in obj-c++.  */
7033     case CPP_OPEN_SQUARE:
7034       return c_dialect_objc ();
7035
7036     default:
7037       return true;
7038     }
7039 }
7040
7041 /* Parse a cast-expression.
7042
7043    cast-expression:
7044      unary-expression
7045      ( type-id ) cast-expression
7046
7047    ADDRESS_P is true iff the unary-expression is appearing as the
7048    operand of the `&' operator.   CAST_P is true if this expression is
7049    the target of a cast.
7050
7051    Returns a representation of the expression.  */
7052
7053 static tree
7054 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7055                            cp_id_kind * pidk)
7056 {
7057   /* If it's a `(', then we might be looking at a cast.  */
7058   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7059     {
7060       tree type = NULL_TREE;
7061       tree expr = NULL_TREE;
7062       bool compound_literal_p;
7063       const char *saved_message;
7064
7065       /* There's no way to know yet whether or not this is a cast.
7066          For example, `(int (3))' is a unary-expression, while `(int)
7067          3' is a cast.  So, we resort to parsing tentatively.  */
7068       cp_parser_parse_tentatively (parser);
7069       /* Types may not be defined in a cast.  */
7070       saved_message = parser->type_definition_forbidden_message;
7071       parser->type_definition_forbidden_message
7072         = G_("types may not be defined in casts");
7073       /* Consume the `('.  */
7074       cp_lexer_consume_token (parser->lexer);
7075       /* A very tricky bit is that `(struct S) { 3 }' is a
7076          compound-literal (which we permit in C++ as an extension).
7077          But, that construct is not a cast-expression -- it is a
7078          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7079          is legal; if the compound-literal were a cast-expression,
7080          you'd need an extra set of parentheses.)  But, if we parse
7081          the type-id, and it happens to be a class-specifier, then we
7082          will commit to the parse at that point, because we cannot
7083          undo the action that is done when creating a new class.  So,
7084          then we cannot back up and do a postfix-expression.
7085
7086          Therefore, we scan ahead to the closing `)', and check to see
7087          if the token after the `)' is a `{'.  If so, we are not
7088          looking at a cast-expression.
7089
7090          Save tokens so that we can put them back.  */
7091       cp_lexer_save_tokens (parser->lexer);
7092       /* Skip tokens until the next token is a closing parenthesis.
7093          If we find the closing `)', and the next token is a `{', then
7094          we are looking at a compound-literal.  */
7095       compound_literal_p
7096         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7097                                                   /*consume_paren=*/true)
7098            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7099       /* Roll back the tokens we skipped.  */
7100       cp_lexer_rollback_tokens (parser->lexer);
7101       /* If we were looking at a compound-literal, simulate an error
7102          so that the call to cp_parser_parse_definitely below will
7103          fail.  */
7104       if (compound_literal_p)
7105         cp_parser_simulate_error (parser);
7106       else
7107         {
7108           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7109           parser->in_type_id_in_expr_p = true;
7110           /* Look for the type-id.  */
7111           type = cp_parser_type_id (parser);
7112           /* Look for the closing `)'.  */
7113           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7114           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7115         }
7116
7117       /* Restore the saved message.  */
7118       parser->type_definition_forbidden_message = saved_message;
7119
7120       /* At this point this can only be either a cast or a
7121          parenthesized ctor such as `(T ())' that looks like a cast to
7122          function returning T.  */
7123       if (!cp_parser_error_occurred (parser)
7124           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7125                                                      (parser->lexer)))
7126         {
7127           cp_parser_parse_definitely (parser);
7128           expr = cp_parser_cast_expression (parser,
7129                                             /*address_p=*/false,
7130                                             /*cast_p=*/true, pidk);
7131
7132           /* Warn about old-style casts, if so requested.  */
7133           if (warn_old_style_cast
7134               && !in_system_header
7135               && !VOID_TYPE_P (type)
7136               && current_lang_name != lang_name_c)
7137             warning (OPT_Wold_style_cast, "use of old-style cast");
7138
7139           /* Only type conversions to integral or enumeration types
7140              can be used in constant-expressions.  */
7141           if (!cast_valid_in_integral_constant_expression_p (type)
7142               && cp_parser_non_integral_constant_expression (parser,
7143                                                              NIC_CAST))
7144             return error_mark_node;
7145
7146           /* Perform the cast.  */
7147           expr = build_c_cast (input_location, type, expr);
7148           return expr;
7149         }
7150       else 
7151         cp_parser_abort_tentative_parse (parser);
7152     }
7153
7154   /* If we get here, then it's not a cast, so it must be a
7155      unary-expression.  */
7156   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7157 }
7158
7159 /* Parse a binary expression of the general form:
7160
7161    pm-expression:
7162      cast-expression
7163      pm-expression .* cast-expression
7164      pm-expression ->* cast-expression
7165
7166    multiplicative-expression:
7167      pm-expression
7168      multiplicative-expression * pm-expression
7169      multiplicative-expression / pm-expression
7170      multiplicative-expression % pm-expression
7171
7172    additive-expression:
7173      multiplicative-expression
7174      additive-expression + multiplicative-expression
7175      additive-expression - multiplicative-expression
7176
7177    shift-expression:
7178      additive-expression
7179      shift-expression << additive-expression
7180      shift-expression >> additive-expression
7181
7182    relational-expression:
7183      shift-expression
7184      relational-expression < shift-expression
7185      relational-expression > shift-expression
7186      relational-expression <= shift-expression
7187      relational-expression >= shift-expression
7188
7189   GNU Extension:
7190
7191    relational-expression:
7192      relational-expression <? shift-expression
7193      relational-expression >? shift-expression
7194
7195    equality-expression:
7196      relational-expression
7197      equality-expression == relational-expression
7198      equality-expression != relational-expression
7199
7200    and-expression:
7201      equality-expression
7202      and-expression & equality-expression
7203
7204    exclusive-or-expression:
7205      and-expression
7206      exclusive-or-expression ^ and-expression
7207
7208    inclusive-or-expression:
7209      exclusive-or-expression
7210      inclusive-or-expression | exclusive-or-expression
7211
7212    logical-and-expression:
7213      inclusive-or-expression
7214      logical-and-expression && inclusive-or-expression
7215
7216    logical-or-expression:
7217      logical-and-expression
7218      logical-or-expression || logical-and-expression
7219
7220    All these are implemented with a single function like:
7221
7222    binary-expression:
7223      simple-cast-expression
7224      binary-expression <token> binary-expression
7225
7226    CAST_P is true if this expression is the target of a cast.
7227
7228    The binops_by_token map is used to get the tree codes for each <token> type.
7229    binary-expressions are associated according to a precedence table.  */
7230
7231 #define TOKEN_PRECEDENCE(token)                              \
7232 (((token->type == CPP_GREATER                                \
7233    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7234   && !parser->greater_than_is_operator_p)                    \
7235  ? PREC_NOT_OPERATOR                                         \
7236  : binops_by_token[token->type].prec)
7237
7238 static tree
7239 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7240                              bool no_toplevel_fold_p,
7241                              enum cp_parser_prec prec,
7242                              cp_id_kind * pidk)
7243 {
7244   cp_parser_expression_stack stack;
7245   cp_parser_expression_stack_entry *sp = &stack[0];
7246   tree lhs, rhs;
7247   cp_token *token;
7248   enum tree_code tree_type, lhs_type, rhs_type;
7249   enum cp_parser_prec new_prec, lookahead_prec;
7250   tree overload;
7251
7252   /* Parse the first expression.  */
7253   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7254   lhs_type = ERROR_MARK;
7255
7256   for (;;)
7257     {
7258       /* Get an operator token.  */
7259       token = cp_lexer_peek_token (parser->lexer);
7260
7261       if (warn_cxx0x_compat
7262           && token->type == CPP_RSHIFT
7263           && !parser->greater_than_is_operator_p)
7264         {
7265           if (warning_at (token->location, OPT_Wc__0x_compat, 
7266                           "%<>>%> operator is treated as"
7267                           " two right angle brackets in C++11"))
7268             inform (token->location,
7269                     "suggest parentheses around %<>>%> expression");
7270         }
7271
7272       new_prec = TOKEN_PRECEDENCE (token);
7273
7274       /* Popping an entry off the stack means we completed a subexpression:
7275          - either we found a token which is not an operator (`>' where it is not
7276            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7277            will happen repeatedly;
7278          - or, we found an operator which has lower priority.  This is the case
7279            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7280            parsing `3 * 4'.  */
7281       if (new_prec <= prec)
7282         {
7283           if (sp == stack)
7284             break;
7285           else
7286             goto pop;
7287         }
7288
7289      get_rhs:
7290       tree_type = binops_by_token[token->type].tree_type;
7291
7292       /* We used the operator token.  */
7293       cp_lexer_consume_token (parser->lexer);
7294
7295       /* For "false && x" or "true || x", x will never be executed;
7296          disable warnings while evaluating it.  */
7297       if (tree_type == TRUTH_ANDIF_EXPR)
7298         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7299       else if (tree_type == TRUTH_ORIF_EXPR)
7300         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7301
7302       /* Extract another operand.  It may be the RHS of this expression
7303          or the LHS of a new, higher priority expression.  */
7304       rhs = cp_parser_simple_cast_expression (parser);
7305       rhs_type = ERROR_MARK;
7306
7307       /* Get another operator token.  Look up its precedence to avoid
7308          building a useless (immediately popped) stack entry for common
7309          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7310       token = cp_lexer_peek_token (parser->lexer);
7311       lookahead_prec = TOKEN_PRECEDENCE (token);
7312       if (lookahead_prec > new_prec)
7313         {
7314           /* ... and prepare to parse the RHS of the new, higher priority
7315              expression.  Since precedence levels on the stack are
7316              monotonically increasing, we do not have to care about
7317              stack overflows.  */
7318           sp->prec = prec;
7319           sp->tree_type = tree_type;
7320           sp->lhs = lhs;
7321           sp->lhs_type = lhs_type;
7322           sp++;
7323           lhs = rhs;
7324           lhs_type = rhs_type;
7325           prec = new_prec;
7326           new_prec = lookahead_prec;
7327           goto get_rhs;
7328
7329          pop:
7330           lookahead_prec = new_prec;
7331           /* If the stack is not empty, we have parsed into LHS the right side
7332              (`4' in the example above) of an expression we had suspended.
7333              We can use the information on the stack to recover the LHS (`3')
7334              from the stack together with the tree code (`MULT_EXPR'), and
7335              the precedence of the higher level subexpression
7336              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7337              which will be used to actually build the additive expression.  */
7338           --sp;
7339           prec = sp->prec;
7340           tree_type = sp->tree_type;
7341           rhs = lhs;
7342           rhs_type = lhs_type;
7343           lhs = sp->lhs;
7344           lhs_type = sp->lhs_type;
7345         }
7346
7347       /* Undo the disabling of warnings done above.  */
7348       if (tree_type == TRUTH_ANDIF_EXPR)
7349         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7350       else if (tree_type == TRUTH_ORIF_EXPR)
7351         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7352
7353       overload = NULL;
7354       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7355          ERROR_MARK for everything that is not a binary expression.
7356          This makes warn_about_parentheses miss some warnings that
7357          involve unary operators.  For unary expressions we should
7358          pass the correct tree_code unless the unary expression was
7359          surrounded by parentheses.
7360       */
7361       if (no_toplevel_fold_p
7362           && lookahead_prec <= prec
7363           && sp == stack
7364           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7365         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7366       else
7367         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7368                                  &overload, tf_warning_or_error);
7369       lhs_type = tree_type;
7370
7371       /* If the binary operator required the use of an overloaded operator,
7372          then this expression cannot be an integral constant-expression.
7373          An overloaded operator can be used even if both operands are
7374          otherwise permissible in an integral constant-expression if at
7375          least one of the operands is of enumeration type.  */
7376
7377       if (overload
7378           && cp_parser_non_integral_constant_expression (parser,
7379                                                          NIC_OVERLOADED))
7380         return error_mark_node;
7381     }
7382
7383   return lhs;
7384 }
7385
7386
7387 /* Parse the `? expression : assignment-expression' part of a
7388    conditional-expression.  The LOGICAL_OR_EXPR is the
7389    logical-or-expression that started the conditional-expression.
7390    Returns a representation of the entire conditional-expression.
7391
7392    This routine is used by cp_parser_assignment_expression.
7393
7394      ? expression : assignment-expression
7395
7396    GNU Extensions:
7397
7398      ? : assignment-expression */
7399
7400 static tree
7401 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7402 {
7403   tree expr;
7404   tree assignment_expr;
7405   struct cp_token *token;
7406
7407   /* Consume the `?' token.  */
7408   cp_lexer_consume_token (parser->lexer);
7409   token = cp_lexer_peek_token (parser->lexer);
7410   if (cp_parser_allow_gnu_extensions_p (parser)
7411       && token->type == CPP_COLON)
7412     {
7413       pedwarn (token->location, OPT_pedantic, 
7414                "ISO C++ does not allow ?: with omitted middle operand");
7415       /* Implicit true clause.  */
7416       expr = NULL_TREE;
7417       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7418       warn_for_omitted_condop (token->location, logical_or_expr);
7419     }
7420   else
7421     {
7422       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7423       parser->colon_corrects_to_scope_p = false;
7424       /* Parse the expression.  */
7425       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7426       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7427       c_inhibit_evaluation_warnings +=
7428         ((logical_or_expr == truthvalue_true_node)
7429          - (logical_or_expr == truthvalue_false_node));
7430       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7431     }
7432
7433   /* The next token should be a `:'.  */
7434   cp_parser_require (parser, CPP_COLON, RT_COLON);
7435   /* Parse the assignment-expression.  */
7436   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7437   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7438
7439   /* Build the conditional-expression.  */
7440   return build_x_conditional_expr (logical_or_expr,
7441                                    expr,
7442                                    assignment_expr,
7443                                    tf_warning_or_error);
7444 }
7445
7446 /* Parse an assignment-expression.
7447
7448    assignment-expression:
7449      conditional-expression
7450      logical-or-expression assignment-operator assignment_expression
7451      throw-expression
7452
7453    CAST_P is true if this expression is the target of a cast.
7454
7455    Returns a representation for the expression.  */
7456
7457 static tree
7458 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7459                                  cp_id_kind * pidk)
7460 {
7461   tree expr;
7462
7463   /* If the next token is the `throw' keyword, then we're looking at
7464      a throw-expression.  */
7465   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7466     expr = cp_parser_throw_expression (parser);
7467   /* Otherwise, it must be that we are looking at a
7468      logical-or-expression.  */
7469   else
7470     {
7471       /* Parse the binary expressions (logical-or-expression).  */
7472       expr = cp_parser_binary_expression (parser, cast_p, false,
7473                                           PREC_NOT_OPERATOR, pidk);
7474       /* If the next token is a `?' then we're actually looking at a
7475          conditional-expression.  */
7476       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7477         return cp_parser_question_colon_clause (parser, expr);
7478       else
7479         {
7480           enum tree_code assignment_operator;
7481
7482           /* If it's an assignment-operator, we're using the second
7483              production.  */
7484           assignment_operator
7485             = cp_parser_assignment_operator_opt (parser);
7486           if (assignment_operator != ERROR_MARK)
7487             {
7488               bool non_constant_p;
7489
7490               /* Parse the right-hand side of the assignment.  */
7491               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7492
7493               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7494                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7495
7496               /* An assignment may not appear in a
7497                  constant-expression.  */
7498               if (cp_parser_non_integral_constant_expression (parser,
7499                                                               NIC_ASSIGNMENT))
7500                 return error_mark_node;
7501               /* Build the assignment expression.  */
7502               expr = build_x_modify_expr (expr,
7503                                           assignment_operator,
7504                                           rhs,
7505                                           tf_warning_or_error);
7506             }
7507         }
7508     }
7509
7510   return expr;
7511 }
7512
7513 /* Parse an (optional) assignment-operator.
7514
7515    assignment-operator: one of
7516      = *= /= %= += -= >>= <<= &= ^= |=
7517
7518    GNU Extension:
7519
7520    assignment-operator: one of
7521      <?= >?=
7522
7523    If the next token is an assignment operator, the corresponding tree
7524    code is returned, and the token is consumed.  For example, for
7525    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7526    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7527    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7528    operator, ERROR_MARK is returned.  */
7529
7530 static enum tree_code
7531 cp_parser_assignment_operator_opt (cp_parser* parser)
7532 {
7533   enum tree_code op;
7534   cp_token *token;
7535
7536   /* Peek at the next token.  */
7537   token = cp_lexer_peek_token (parser->lexer);
7538
7539   switch (token->type)
7540     {
7541     case CPP_EQ:
7542       op = NOP_EXPR;
7543       break;
7544
7545     case CPP_MULT_EQ:
7546       op = MULT_EXPR;
7547       break;
7548
7549     case CPP_DIV_EQ:
7550       op = TRUNC_DIV_EXPR;
7551       break;
7552
7553     case CPP_MOD_EQ:
7554       op = TRUNC_MOD_EXPR;
7555       break;
7556
7557     case CPP_PLUS_EQ:
7558       op = PLUS_EXPR;
7559       break;
7560
7561     case CPP_MINUS_EQ:
7562       op = MINUS_EXPR;
7563       break;
7564
7565     case CPP_RSHIFT_EQ:
7566       op = RSHIFT_EXPR;
7567       break;
7568
7569     case CPP_LSHIFT_EQ:
7570       op = LSHIFT_EXPR;
7571       break;
7572
7573     case CPP_AND_EQ:
7574       op = BIT_AND_EXPR;
7575       break;
7576
7577     case CPP_XOR_EQ:
7578       op = BIT_XOR_EXPR;
7579       break;
7580
7581     case CPP_OR_EQ:
7582       op = BIT_IOR_EXPR;
7583       break;
7584
7585     default:
7586       /* Nothing else is an assignment operator.  */
7587       op = ERROR_MARK;
7588     }
7589
7590   /* If it was an assignment operator, consume it.  */
7591   if (op != ERROR_MARK)
7592     cp_lexer_consume_token (parser->lexer);
7593
7594   return op;
7595 }
7596
7597 /* Parse an expression.
7598
7599    expression:
7600      assignment-expression
7601      expression , assignment-expression
7602
7603    CAST_P is true if this expression is the target of a cast.
7604
7605    Returns a representation of the expression.  */
7606
7607 static tree
7608 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7609 {
7610   tree expression = NULL_TREE;
7611
7612   while (true)
7613     {
7614       tree assignment_expression;
7615
7616       /* Parse the next assignment-expression.  */
7617       assignment_expression
7618         = cp_parser_assignment_expression (parser, cast_p, pidk);
7619       /* If this is the first assignment-expression, we can just
7620          save it away.  */
7621       if (!expression)
7622         expression = assignment_expression;
7623       else
7624         expression = build_x_compound_expr (expression,
7625                                             assignment_expression,
7626                                             tf_warning_or_error);
7627       /* If the next token is not a comma, then we are done with the
7628          expression.  */
7629       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7630         break;
7631       /* Consume the `,'.  */
7632       cp_lexer_consume_token (parser->lexer);
7633       /* A comma operator cannot appear in a constant-expression.  */
7634       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7635         expression = error_mark_node;
7636     }
7637
7638   return expression;
7639 }
7640
7641 /* Parse a constant-expression.
7642
7643    constant-expression:
7644      conditional-expression
7645
7646   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7647   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7648   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7649   is false, NON_CONSTANT_P should be NULL.  */
7650
7651 static tree
7652 cp_parser_constant_expression (cp_parser* parser,
7653                                bool allow_non_constant_p,
7654                                bool *non_constant_p)
7655 {
7656   bool saved_integral_constant_expression_p;
7657   bool saved_allow_non_integral_constant_expression_p;
7658   bool saved_non_integral_constant_expression_p;
7659   tree expression;
7660
7661   /* It might seem that we could simply parse the
7662      conditional-expression, and then check to see if it were
7663      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7664      one that the compiler can figure out is constant, possibly after
7665      doing some simplifications or optimizations.  The standard has a
7666      precise definition of constant-expression, and we must honor
7667      that, even though it is somewhat more restrictive.
7668
7669      For example:
7670
7671        int i[(2, 3)];
7672
7673      is not a legal declaration, because `(2, 3)' is not a
7674      constant-expression.  The `,' operator is forbidden in a
7675      constant-expression.  However, GCC's constant-folding machinery
7676      will fold this operation to an INTEGER_CST for `3'.  */
7677
7678   /* Save the old settings.  */
7679   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7680   saved_allow_non_integral_constant_expression_p
7681     = parser->allow_non_integral_constant_expression_p;
7682   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7683   /* We are now parsing a constant-expression.  */
7684   parser->integral_constant_expression_p = true;
7685   parser->allow_non_integral_constant_expression_p
7686     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7687   parser->non_integral_constant_expression_p = false;
7688   /* Although the grammar says "conditional-expression", we parse an
7689      "assignment-expression", which also permits "throw-expression"
7690      and the use of assignment operators.  In the case that
7691      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7692      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7693      actually essential that we look for an assignment-expression.
7694      For example, cp_parser_initializer_clauses uses this function to
7695      determine whether a particular assignment-expression is in fact
7696      constant.  */
7697   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7698   /* Restore the old settings.  */
7699   parser->integral_constant_expression_p
7700     = saved_integral_constant_expression_p;
7701   parser->allow_non_integral_constant_expression_p
7702     = saved_allow_non_integral_constant_expression_p;
7703   if (cxx_dialect >= cxx0x)
7704     {
7705       /* Require an rvalue constant expression here; that's what our
7706          callers expect.  Reference constant expressions are handled
7707          separately in e.g. cp_parser_template_argument.  */
7708       bool is_const = potential_rvalue_constant_expression (expression);
7709       parser->non_integral_constant_expression_p = !is_const;
7710       if (!is_const && !allow_non_constant_p)
7711         require_potential_rvalue_constant_expression (expression);
7712     }
7713   if (allow_non_constant_p)
7714     *non_constant_p = parser->non_integral_constant_expression_p;
7715   parser->non_integral_constant_expression_p
7716     = saved_non_integral_constant_expression_p;
7717
7718   return expression;
7719 }
7720
7721 /* Parse __builtin_offsetof.
7722
7723    offsetof-expression:
7724      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7725
7726    offsetof-member-designator:
7727      id-expression
7728      | offsetof-member-designator "." id-expression
7729      | offsetof-member-designator "[" expression "]"
7730      | offsetof-member-designator "->" id-expression  */
7731
7732 static tree
7733 cp_parser_builtin_offsetof (cp_parser *parser)
7734 {
7735   int save_ice_p, save_non_ice_p;
7736   tree type, expr;
7737   cp_id_kind dummy;
7738   cp_token *token;
7739
7740   /* We're about to accept non-integral-constant things, but will
7741      definitely yield an integral constant expression.  Save and
7742      restore these values around our local parsing.  */
7743   save_ice_p = parser->integral_constant_expression_p;
7744   save_non_ice_p = parser->non_integral_constant_expression_p;
7745
7746   /* Consume the "__builtin_offsetof" token.  */
7747   cp_lexer_consume_token (parser->lexer);
7748   /* Consume the opening `('.  */
7749   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7750   /* Parse the type-id.  */
7751   type = cp_parser_type_id (parser);
7752   /* Look for the `,'.  */
7753   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7754   token = cp_lexer_peek_token (parser->lexer);
7755
7756   /* Build the (type *)null that begins the traditional offsetof macro.  */
7757   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7758                             tf_warning_or_error);
7759
7760   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7761   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7762                                                  true, &dummy, token->location);
7763   while (true)
7764     {
7765       token = cp_lexer_peek_token (parser->lexer);
7766       switch (token->type)
7767         {
7768         case CPP_OPEN_SQUARE:
7769           /* offsetof-member-designator "[" expression "]" */
7770           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7771           break;
7772
7773         case CPP_DEREF:
7774           /* offsetof-member-designator "->" identifier */
7775           expr = grok_array_decl (expr, integer_zero_node);
7776           /* FALLTHRU */
7777
7778         case CPP_DOT:
7779           /* offsetof-member-designator "." identifier */
7780           cp_lexer_consume_token (parser->lexer);
7781           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7782                                                          expr, true, &dummy,
7783                                                          token->location);
7784           break;
7785
7786         case CPP_CLOSE_PAREN:
7787           /* Consume the ")" token.  */
7788           cp_lexer_consume_token (parser->lexer);
7789           goto success;
7790
7791         default:
7792           /* Error.  We know the following require will fail, but
7793              that gives the proper error message.  */
7794           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7795           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7796           expr = error_mark_node;
7797           goto failure;
7798         }
7799     }
7800
7801  success:
7802   /* If we're processing a template, we can't finish the semantics yet.
7803      Otherwise we can fold the entire expression now.  */
7804   if (processing_template_decl)
7805     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7806   else
7807     expr = finish_offsetof (expr);
7808
7809  failure:
7810   parser->integral_constant_expression_p = save_ice_p;
7811   parser->non_integral_constant_expression_p = save_non_ice_p;
7812
7813   return expr;
7814 }
7815
7816 /* Parse a trait expression.
7817
7818    Returns a representation of the expression, the underlying type
7819    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7820
7821 static tree
7822 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7823 {
7824   cp_trait_kind kind;
7825   tree type1, type2 = NULL_TREE;
7826   bool binary = false;
7827   cp_decl_specifier_seq decl_specs;
7828
7829   switch (keyword)
7830     {
7831     case RID_HAS_NOTHROW_ASSIGN:
7832       kind = CPTK_HAS_NOTHROW_ASSIGN;
7833       break;
7834     case RID_HAS_NOTHROW_CONSTRUCTOR:
7835       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7836       break;
7837     case RID_HAS_NOTHROW_COPY:
7838       kind = CPTK_HAS_NOTHROW_COPY;
7839       break;
7840     case RID_HAS_TRIVIAL_ASSIGN:
7841       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7842       break;
7843     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7844       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7845       break;
7846     case RID_HAS_TRIVIAL_COPY:
7847       kind = CPTK_HAS_TRIVIAL_COPY;
7848       break;
7849     case RID_HAS_TRIVIAL_DESTRUCTOR:
7850       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7851       break;
7852     case RID_HAS_VIRTUAL_DESTRUCTOR:
7853       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7854       break;
7855     case RID_IS_ABSTRACT:
7856       kind = CPTK_IS_ABSTRACT;
7857       break;
7858     case RID_IS_BASE_OF:
7859       kind = CPTK_IS_BASE_OF;
7860       binary = true;
7861       break;
7862     case RID_IS_CLASS:
7863       kind = CPTK_IS_CLASS;
7864       break;
7865     case RID_IS_CONVERTIBLE_TO:
7866       kind = CPTK_IS_CONVERTIBLE_TO;
7867       binary = true;
7868       break;
7869     case RID_IS_EMPTY:
7870       kind = CPTK_IS_EMPTY;
7871       break;
7872     case RID_IS_ENUM:
7873       kind = CPTK_IS_ENUM;
7874       break;
7875     case RID_IS_FINAL:
7876       kind = CPTK_IS_FINAL;
7877       break;
7878     case RID_IS_LITERAL_TYPE:
7879       kind = CPTK_IS_LITERAL_TYPE;
7880       break;
7881     case RID_IS_POD:
7882       kind = CPTK_IS_POD;
7883       break;
7884     case RID_IS_POLYMORPHIC:
7885       kind = CPTK_IS_POLYMORPHIC;
7886       break;
7887     case RID_IS_STD_LAYOUT:
7888       kind = CPTK_IS_STD_LAYOUT;
7889       break;
7890     case RID_IS_TRIVIAL:
7891       kind = CPTK_IS_TRIVIAL;
7892       break;
7893     case RID_IS_UNION:
7894       kind = CPTK_IS_UNION;
7895       break;
7896     case RID_UNDERLYING_TYPE:
7897       kind = CPTK_UNDERLYING_TYPE;
7898       break;
7899     case RID_BASES:
7900       kind = CPTK_BASES;
7901       break;
7902     case RID_DIRECT_BASES:
7903       kind = CPTK_DIRECT_BASES;
7904       break;
7905     default:
7906       gcc_unreachable ();
7907     }
7908
7909   /* Consume the token.  */
7910   cp_lexer_consume_token (parser->lexer);
7911
7912   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7913
7914   type1 = cp_parser_type_id (parser);
7915
7916   if (type1 == error_mark_node)
7917     return error_mark_node;
7918
7919   /* Build a trivial decl-specifier-seq.  */
7920   clear_decl_specs (&decl_specs);
7921   decl_specs.type = type1;
7922
7923   /* Call grokdeclarator to figure out what type this is.  */
7924   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7925                           /*initialized=*/0, /*attrlist=*/NULL);
7926
7927   if (binary)
7928     {
7929       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7930  
7931       type2 = cp_parser_type_id (parser);
7932
7933       if (type2 == error_mark_node)
7934         return error_mark_node;
7935
7936       /* Build a trivial decl-specifier-seq.  */
7937       clear_decl_specs (&decl_specs);
7938       decl_specs.type = type2;
7939
7940       /* Call grokdeclarator to figure out what type this is.  */
7941       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7942                               /*initialized=*/0, /*attrlist=*/NULL);
7943     }
7944
7945   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7946
7947   /* Complete the trait expression, which may mean either processing
7948      the trait expr now or saving it for template instantiation.  */
7949   switch(kind)
7950     {
7951     case CPTK_UNDERLYING_TYPE:
7952       return finish_underlying_type (type1);
7953     case CPTK_BASES:
7954       return finish_bases (type1, false);
7955     case CPTK_DIRECT_BASES:
7956       return finish_bases (type1, true);
7957     default:
7958       return finish_trait_expr (kind, type1, type2);
7959     }
7960 }
7961
7962 /* Lambdas that appear in variable initializer or default argument scope
7963    get that in their mangling, so we need to record it.  We might as well
7964    use the count for function and namespace scopes as well.  */
7965 static GTY(()) tree lambda_scope;
7966 static GTY(()) int lambda_count;
7967 typedef struct GTY(()) tree_int
7968 {
7969   tree t;
7970   int i;
7971 } tree_int;
7972 DEF_VEC_O(tree_int);
7973 DEF_VEC_ALLOC_O(tree_int,gc);
7974 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7975
7976 static void
7977 start_lambda_scope (tree decl)
7978 {
7979   tree_int ti;
7980   gcc_assert (decl);
7981   /* Once we're inside a function, we ignore other scopes and just push
7982      the function again so that popping works properly.  */
7983   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7984     decl = current_function_decl;
7985   ti.t = lambda_scope;
7986   ti.i = lambda_count;
7987   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7988   if (lambda_scope != decl)
7989     {
7990       /* Don't reset the count if we're still in the same function.  */
7991       lambda_scope = decl;
7992       lambda_count = 0;
7993     }
7994 }
7995
7996 static void
7997 record_lambda_scope (tree lambda)
7998 {
7999   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8000   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8001 }
8002
8003 static void
8004 finish_lambda_scope (void)
8005 {
8006   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8007   if (lambda_scope != p->t)
8008     {
8009       lambda_scope = p->t;
8010       lambda_count = p->i;
8011     }
8012   VEC_pop (tree_int, lambda_scope_stack);
8013 }
8014
8015 /* Parse a lambda expression.
8016
8017    lambda-expression:
8018      lambda-introducer lambda-declarator [opt] compound-statement
8019
8020    Returns a representation of the expression.  */
8021
8022 static tree
8023 cp_parser_lambda_expression (cp_parser* parser)
8024 {
8025   tree lambda_expr = build_lambda_expr ();
8026   tree type;
8027   bool ok;
8028
8029   LAMBDA_EXPR_LOCATION (lambda_expr)
8030     = cp_lexer_peek_token (parser->lexer)->location;
8031
8032   if (cp_unevaluated_operand)
8033     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8034               "lambda-expression in unevaluated context");
8035
8036   /* We may be in the middle of deferred access check.  Disable
8037      it now.  */
8038   push_deferring_access_checks (dk_no_deferred);
8039
8040   cp_parser_lambda_introducer (parser, lambda_expr);
8041
8042   type = begin_lambda_type (lambda_expr);
8043   if (type == error_mark_node)
8044     return error_mark_node;
8045
8046   record_lambda_scope (lambda_expr);
8047
8048   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8049   determine_visibility (TYPE_NAME (type));
8050
8051   /* Now that we've started the type, add the capture fields for any
8052      explicit captures.  */
8053   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8054
8055   {
8056     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8057     unsigned int saved_num_template_parameter_lists
8058         = parser->num_template_parameter_lists;
8059     unsigned char in_statement = parser->in_statement;
8060     bool in_switch_statement_p = parser->in_switch_statement_p;
8061
8062     parser->num_template_parameter_lists = 0;
8063     parser->in_statement = 0;
8064     parser->in_switch_statement_p = false;
8065
8066     /* By virtue of defining a local class, a lambda expression has access to
8067        the private variables of enclosing classes.  */
8068
8069     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8070
8071     if (ok)
8072       cp_parser_lambda_body (parser, lambda_expr);
8073     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8074       cp_parser_skip_to_end_of_block_or_statement (parser);
8075
8076     /* The capture list was built up in reverse order; fix that now.  */
8077     {
8078       tree newlist = NULL_TREE;
8079       tree elt, next;
8080
8081       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8082            elt; elt = next)
8083         {
8084           next = TREE_CHAIN (elt);
8085           TREE_CHAIN (elt) = newlist;
8086           newlist = elt;
8087         }
8088       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8089     }
8090
8091     if (ok)
8092       maybe_add_lambda_conv_op (type);
8093
8094     type = finish_struct (type, /*attributes=*/NULL_TREE);
8095
8096     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8097     parser->in_statement = in_statement;
8098     parser->in_switch_statement_p = in_switch_statement_p;
8099   }
8100
8101   pop_deferring_access_checks ();
8102
8103   /* This field is only used during parsing of the lambda.  */
8104   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8105
8106   /* This lambda shouldn't have any proxies left at this point.  */
8107   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8108   /* And now that we're done, push proxies for an enclosing lambda.  */
8109   insert_pending_capture_proxies ();
8110
8111   if (ok)
8112     return build_lambda_object (lambda_expr);
8113   else
8114     return error_mark_node;
8115 }
8116
8117 /* Parse the beginning of a lambda expression.
8118
8119    lambda-introducer:
8120      [ lambda-capture [opt] ]
8121
8122    LAMBDA_EXPR is the current representation of the lambda expression.  */
8123
8124 static void
8125 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8126 {
8127   /* Need commas after the first capture.  */
8128   bool first = true;
8129
8130   /* Eat the leading `['.  */
8131   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8132
8133   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8134   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8135       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8136     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8137   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8138     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8139
8140   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8141     {
8142       cp_lexer_consume_token (parser->lexer);
8143       first = false;
8144     }
8145
8146   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8147     {
8148       cp_token* capture_token;
8149       tree capture_id;
8150       tree capture_init_expr;
8151       cp_id_kind idk = CP_ID_KIND_NONE;
8152       bool explicit_init_p = false;
8153
8154       enum capture_kind_type
8155       {
8156         BY_COPY,
8157         BY_REFERENCE
8158       };
8159       enum capture_kind_type capture_kind = BY_COPY;
8160
8161       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8162         {
8163           error ("expected end of capture-list");
8164           return;
8165         }
8166
8167       if (first)
8168         first = false;
8169       else
8170         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8171
8172       /* Possibly capture `this'.  */
8173       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8174         {
8175           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8176           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8177             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8178                      "with by-copy capture default");
8179           cp_lexer_consume_token (parser->lexer);
8180           add_capture (lambda_expr,
8181                        /*id=*/this_identifier,
8182                        /*initializer=*/finish_this_expr(),
8183                        /*by_reference_p=*/false,
8184                        explicit_init_p);
8185           continue;
8186         }
8187
8188       /* Remember whether we want to capture as a reference or not.  */
8189       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8190         {
8191           capture_kind = BY_REFERENCE;
8192           cp_lexer_consume_token (parser->lexer);
8193         }
8194
8195       /* Get the identifier.  */
8196       capture_token = cp_lexer_peek_token (parser->lexer);
8197       capture_id = cp_parser_identifier (parser);
8198
8199       if (capture_id == error_mark_node)
8200         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8201            delimiters, but I modified this to stop on unnested ']' as well.  It
8202            was already changed to stop on unnested '}', so the
8203            "closing_parenthesis" name is no more misleading with my change.  */
8204         {
8205           cp_parser_skip_to_closing_parenthesis (parser,
8206                                                  /*recovering=*/true,
8207                                                  /*or_comma=*/true,
8208                                                  /*consume_paren=*/true);
8209           break;
8210         }
8211
8212       /* Find the initializer for this capture.  */
8213       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8214         {
8215           /* An explicit expression exists.  */
8216           cp_lexer_consume_token (parser->lexer);
8217           pedwarn (input_location, OPT_pedantic,
8218                    "ISO C++ does not allow initializers "
8219                    "in lambda expression capture lists");
8220           capture_init_expr = cp_parser_assignment_expression (parser,
8221                                                                /*cast_p=*/true,
8222                                                                &idk);
8223           explicit_init_p = true;
8224         }
8225       else
8226         {
8227           const char* error_msg;
8228
8229           /* Turn the identifier into an id-expression.  */
8230           capture_init_expr
8231             = cp_parser_lookup_name
8232                 (parser,
8233                  capture_id,
8234                  none_type,
8235                  /*is_template=*/false,
8236                  /*is_namespace=*/false,
8237                  /*check_dependency=*/true,
8238                  /*ambiguous_decls=*/NULL,
8239                  capture_token->location);
8240
8241           if (capture_init_expr == error_mark_node)
8242             {
8243               unqualified_name_lookup_error (capture_id);
8244               continue;
8245             }
8246           else if (DECL_P (capture_init_expr)
8247                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8248                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8249             {
8250               error_at (capture_token->location,
8251                         "capture of non-variable %qD ",
8252                         capture_init_expr);
8253               inform (0, "%q+#D declared here", capture_init_expr);
8254               continue;
8255             }
8256           if (TREE_CODE (capture_init_expr) == VAR_DECL
8257               && decl_storage_duration (capture_init_expr) != dk_auto)
8258             {
8259               pedwarn (capture_token->location, 0, "capture of variable "
8260                        "%qD with non-automatic storage duration",
8261                        capture_init_expr);
8262               inform (0, "%q+#D declared here", capture_init_expr);
8263               continue;
8264             }
8265
8266           capture_init_expr
8267             = finish_id_expression
8268                 (capture_id,
8269                  capture_init_expr,
8270                  parser->scope,
8271                  &idk,
8272                  /*integral_constant_expression_p=*/false,
8273                  /*allow_non_integral_constant_expression_p=*/false,
8274                  /*non_integral_constant_expression_p=*/NULL,
8275                  /*template_p=*/false,
8276                  /*done=*/true,
8277                  /*address_p=*/false,
8278                  /*template_arg_p=*/false,
8279                  &error_msg,
8280                  capture_token->location);
8281         }
8282
8283       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8284           && !explicit_init_p)
8285         {
8286           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8287               && capture_kind == BY_COPY)
8288             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8289                      "of %qD redundant with by-copy capture default",
8290                      capture_id);
8291           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8292               && capture_kind == BY_REFERENCE)
8293             pedwarn (capture_token->location, 0, "explicit by-reference "
8294                      "capture of %qD redundant with by-reference capture "
8295                      "default", capture_id);
8296         }
8297
8298       add_capture (lambda_expr,
8299                    capture_id,
8300                    capture_init_expr,
8301                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8302                    explicit_init_p);
8303     }
8304
8305   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8306 }
8307
8308 /* Parse the (optional) middle of a lambda expression.
8309
8310    lambda-declarator:
8311      ( parameter-declaration-clause [opt] )
8312        attribute-specifier [opt]
8313        mutable [opt]
8314        exception-specification [opt]
8315        lambda-return-type-clause [opt]
8316
8317    LAMBDA_EXPR is the current representation of the lambda expression.  */
8318
8319 static bool
8320 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8321 {
8322   /* 5.1.1.4 of the standard says:
8323        If a lambda-expression does not include a lambda-declarator, it is as if
8324        the lambda-declarator were ().
8325      This means an empty parameter list, no attributes, and no exception
8326      specification.  */
8327   tree param_list = void_list_node;
8328   tree attributes = NULL_TREE;
8329   tree exception_spec = NULL_TREE;
8330   tree t;
8331
8332   /* The lambda-declarator is optional, but must begin with an opening
8333      parenthesis if present.  */
8334   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8335     {
8336       cp_lexer_consume_token (parser->lexer);
8337
8338       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8339
8340       /* Parse parameters.  */
8341       param_list = cp_parser_parameter_declaration_clause (parser);
8342
8343       /* Default arguments shall not be specified in the
8344          parameter-declaration-clause of a lambda-declarator.  */
8345       for (t = param_list; t; t = TREE_CHAIN (t))
8346         if (TREE_PURPOSE (t))
8347           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8348                    "default argument specified for lambda parameter");
8349
8350       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8351
8352       attributes = cp_parser_attributes_opt (parser);
8353
8354       /* Parse optional `mutable' keyword.  */
8355       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8356         {
8357           cp_lexer_consume_token (parser->lexer);
8358           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8359         }
8360
8361       /* Parse optional exception specification.  */
8362       exception_spec = cp_parser_exception_specification_opt (parser);
8363
8364       /* Parse optional trailing return type.  */
8365       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8366         {
8367           cp_lexer_consume_token (parser->lexer);
8368           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8369         }
8370
8371       /* The function parameters must be in scope all the way until after the
8372          trailing-return-type in case of decltype.  */
8373       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8374         pop_binding (DECL_NAME (t), t);
8375
8376       leave_scope ();
8377     }
8378
8379   /* Create the function call operator.
8380
8381      Messing with declarators like this is no uglier than building up the
8382      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8383      other code.  */
8384   {
8385     cp_decl_specifier_seq return_type_specs;
8386     cp_declarator* declarator;
8387     tree fco;
8388     int quals;
8389     void *p;
8390
8391     clear_decl_specs (&return_type_specs);
8392     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8393       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8394     else
8395       /* Maybe we will deduce the return type later, but we can use void
8396          as a placeholder return type anyways.  */
8397       return_type_specs.type = void_type_node;
8398
8399     p = obstack_alloc (&declarator_obstack, 0);
8400
8401     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8402                                      sfk_none);
8403
8404     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8405              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8406     declarator = make_call_declarator (declarator, param_list, quals,
8407                                        VIRT_SPEC_UNSPECIFIED,
8408                                        exception_spec,
8409                                        /*late_return_type=*/NULL_TREE);
8410     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8411
8412     fco = grokmethod (&return_type_specs,
8413                       declarator,
8414                       attributes);
8415     if (fco != error_mark_node)
8416       {
8417         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8418         DECL_ARTIFICIAL (fco) = 1;
8419         /* Give the object parameter a different name.  */
8420         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8421       }
8422
8423     finish_member_declaration (fco);
8424
8425     obstack_free (&declarator_obstack, p);
8426
8427     return (fco != error_mark_node);
8428   }
8429 }
8430
8431 /* Parse the body of a lambda expression, which is simply
8432
8433    compound-statement
8434
8435    but which requires special handling.
8436    LAMBDA_EXPR is the current representation of the lambda expression.  */
8437
8438 static void
8439 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8440 {
8441   bool nested = (current_function_decl != NULL_TREE);
8442   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8443   if (nested)
8444     push_function_context ();
8445   else
8446     /* Still increment function_depth so that we don't GC in the
8447        middle of an expression.  */
8448     ++function_depth;
8449   /* Clear this in case we're in the middle of a default argument.  */
8450   parser->local_variables_forbidden_p = false;
8451
8452   /* Finish the function call operator
8453      - class_specifier
8454      + late_parsing_for_member
8455      + function_definition_after_declarator
8456      + ctor_initializer_opt_and_function_body  */
8457   {
8458     tree fco = lambda_function (lambda_expr);
8459     tree body;
8460     bool done = false;
8461     tree compound_stmt;
8462     tree cap;
8463
8464     /* Let the front end know that we are going to be defining this
8465        function.  */
8466     start_preparsed_function (fco,
8467                               NULL_TREE,
8468                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8469
8470     start_lambda_scope (fco);
8471     body = begin_function_body ();
8472
8473     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8474       goto out;
8475
8476     /* Push the proxies for any explicit captures.  */
8477     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8478          cap = TREE_CHAIN (cap))
8479       build_capture_proxy (TREE_PURPOSE (cap));
8480
8481     compound_stmt = begin_compound_stmt (0);
8482
8483     /* 5.1.1.4 of the standard says:
8484          If a lambda-expression does not include a trailing-return-type, it
8485          is as if the trailing-return-type denotes the following type:
8486           * if the compound-statement is of the form
8487                { return attribute-specifier [opt] expression ; }
8488              the type of the returned expression after lvalue-to-rvalue
8489              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8490              (_conv.array_ 4.2), and function-to-pointer conversion
8491              (_conv.func_ 4.3);
8492           * otherwise, void.  */
8493
8494     /* In a lambda that has neither a lambda-return-type-clause
8495        nor a deducible form, errors should be reported for return statements
8496        in the body.  Since we used void as the placeholder return type, parsing
8497        the body as usual will give such desired behavior.  */
8498     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8499         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8500         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8501       {
8502         tree expr = NULL_TREE;
8503         cp_id_kind idk = CP_ID_KIND_NONE;
8504
8505         /* Parse tentatively in case there's more after the initial return
8506            statement.  */
8507         cp_parser_parse_tentatively (parser);
8508
8509         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8510
8511         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8512
8513         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8514         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8515
8516         if (cp_parser_parse_definitely (parser))
8517           {
8518             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8519
8520             /* Will get error here if type not deduced yet.  */
8521             finish_return_stmt (expr);
8522
8523             done = true;
8524           }
8525       }
8526
8527     if (!done)
8528       {
8529         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8530           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8531         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8532           cp_parser_label_declaration (parser);
8533         cp_parser_statement_seq_opt (parser, NULL_TREE);
8534         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8535         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8536       }
8537
8538     finish_compound_stmt (compound_stmt);
8539
8540   out:
8541     finish_function_body (body);
8542     finish_lambda_scope ();
8543
8544     /* Finish the function and generate code for it if necessary.  */
8545     expand_or_defer_fn (finish_function (/*inline*/2));
8546   }
8547
8548   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8549   if (nested)
8550     pop_function_context();
8551   else
8552     --function_depth;
8553 }
8554
8555 /* Statements [gram.stmt.stmt]  */
8556
8557 /* Parse a statement.
8558
8559    statement:
8560      labeled-statement
8561      expression-statement
8562      compound-statement
8563      selection-statement
8564      iteration-statement
8565      jump-statement
8566      declaration-statement
8567      try-block
8568
8569   TM Extension:
8570
8571    statement:
8572      atomic-statement
8573
8574   IN_COMPOUND is true when the statement is nested inside a
8575   cp_parser_compound_statement; this matters for certain pragmas.
8576
8577   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8578   is a (possibly labeled) if statement which is not enclosed in braces
8579   and has an else clause.  This is used to implement -Wparentheses.  */
8580
8581 static void
8582 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8583                      bool in_compound, bool *if_p)
8584 {
8585   tree statement;
8586   cp_token *token;
8587   location_t statement_location;
8588
8589  restart:
8590   if (if_p != NULL)
8591     *if_p = false;
8592   /* There is no statement yet.  */
8593   statement = NULL_TREE;
8594   /* Peek at the next token.  */
8595   token = cp_lexer_peek_token (parser->lexer);
8596   /* Remember the location of the first token in the statement.  */
8597   statement_location = token->location;
8598   /* If this is a keyword, then that will often determine what kind of
8599      statement we have.  */
8600   if (token->type == CPP_KEYWORD)
8601     {
8602       enum rid keyword = token->keyword;
8603
8604       switch (keyword)
8605         {
8606         case RID_CASE:
8607         case RID_DEFAULT:
8608           /* Looks like a labeled-statement with a case label.
8609              Parse the label, and then use tail recursion to parse
8610              the statement.  */
8611           cp_parser_label_for_labeled_statement (parser);
8612           goto restart;
8613
8614         case RID_IF:
8615         case RID_SWITCH:
8616           statement = cp_parser_selection_statement (parser, if_p);
8617           break;
8618
8619         case RID_WHILE:
8620         case RID_DO:
8621         case RID_FOR:
8622           statement = cp_parser_iteration_statement (parser);
8623           break;
8624
8625         case RID_BREAK:
8626         case RID_CONTINUE:
8627         case RID_RETURN:
8628         case RID_GOTO:
8629           statement = cp_parser_jump_statement (parser);
8630           break;
8631
8632           /* Objective-C++ exception-handling constructs.  */
8633         case RID_AT_TRY:
8634         case RID_AT_CATCH:
8635         case RID_AT_FINALLY:
8636         case RID_AT_SYNCHRONIZED:
8637         case RID_AT_THROW:
8638           statement = cp_parser_objc_statement (parser);
8639           break;
8640
8641         case RID_TRY:
8642           statement = cp_parser_try_block (parser);
8643           break;
8644
8645         case RID_NAMESPACE:
8646           /* This must be a namespace alias definition.  */
8647           cp_parser_declaration_statement (parser);
8648           return;
8649           
8650         case RID_TRANSACTION_ATOMIC:
8651         case RID_TRANSACTION_RELAXED:
8652           statement = cp_parser_transaction (parser, keyword);
8653           break;
8654         case RID_TRANSACTION_CANCEL:
8655           statement = cp_parser_transaction_cancel (parser);
8656           break;
8657
8658         default:
8659           /* It might be a keyword like `int' that can start a
8660              declaration-statement.  */
8661           break;
8662         }
8663     }
8664   else if (token->type == CPP_NAME)
8665     {
8666       /* If the next token is a `:', then we are looking at a
8667          labeled-statement.  */
8668       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8669       if (token->type == CPP_COLON)
8670         {
8671           /* Looks like a labeled-statement with an ordinary label.
8672              Parse the label, and then use tail recursion to parse
8673              the statement.  */
8674           cp_parser_label_for_labeled_statement (parser);
8675           goto restart;
8676         }
8677     }
8678   /* Anything that starts with a `{' must be a compound-statement.  */
8679   else if (token->type == CPP_OPEN_BRACE)
8680     statement = cp_parser_compound_statement (parser, NULL, false, false);
8681   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8682      a statement all its own.  */
8683   else if (token->type == CPP_PRAGMA)
8684     {
8685       /* Only certain OpenMP pragmas are attached to statements, and thus
8686          are considered statements themselves.  All others are not.  In
8687          the context of a compound, accept the pragma as a "statement" and
8688          return so that we can check for a close brace.  Otherwise we
8689          require a real statement and must go back and read one.  */
8690       if (in_compound)
8691         cp_parser_pragma (parser, pragma_compound);
8692       else if (!cp_parser_pragma (parser, pragma_stmt))
8693         goto restart;
8694       return;
8695     }
8696   else if (token->type == CPP_EOF)
8697     {
8698       cp_parser_error (parser, "expected statement");
8699       return;
8700     }
8701
8702   /* Everything else must be a declaration-statement or an
8703      expression-statement.  Try for the declaration-statement
8704      first, unless we are looking at a `;', in which case we know that
8705      we have an expression-statement.  */
8706   if (!statement)
8707     {
8708       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8709         {
8710           cp_parser_parse_tentatively (parser);
8711           /* Try to parse the declaration-statement.  */
8712           cp_parser_declaration_statement (parser);
8713           /* If that worked, we're done.  */
8714           if (cp_parser_parse_definitely (parser))
8715             return;
8716         }
8717       /* Look for an expression-statement instead.  */
8718       statement = cp_parser_expression_statement (parser, in_statement_expr);
8719     }
8720
8721   /* Set the line number for the statement.  */
8722   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8723     SET_EXPR_LOCATION (statement, statement_location);
8724 }
8725
8726 /* Parse the label for a labeled-statement, i.e.
8727
8728    identifier :
8729    case constant-expression :
8730    default :
8731
8732    GNU Extension:
8733    case constant-expression ... constant-expression : statement
8734
8735    When a label is parsed without errors, the label is added to the
8736    parse tree by the finish_* functions, so this function doesn't
8737    have to return the label.  */
8738
8739 static void
8740 cp_parser_label_for_labeled_statement (cp_parser* parser)
8741 {
8742   cp_token *token;
8743   tree label = NULL_TREE;
8744   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8745
8746   /* The next token should be an identifier.  */
8747   token = cp_lexer_peek_token (parser->lexer);
8748   if (token->type != CPP_NAME
8749       && token->type != CPP_KEYWORD)
8750     {
8751       cp_parser_error (parser, "expected labeled-statement");
8752       return;
8753     }
8754
8755   parser->colon_corrects_to_scope_p = false;
8756   switch (token->keyword)
8757     {
8758     case RID_CASE:
8759       {
8760         tree expr, expr_hi;
8761         cp_token *ellipsis;
8762
8763         /* Consume the `case' token.  */
8764         cp_lexer_consume_token (parser->lexer);
8765         /* Parse the constant-expression.  */
8766         expr = cp_parser_constant_expression (parser,
8767                                               /*allow_non_constant_p=*/false,
8768                                               NULL);
8769
8770         ellipsis = cp_lexer_peek_token (parser->lexer);
8771         if (ellipsis->type == CPP_ELLIPSIS)
8772           {
8773             /* Consume the `...' token.  */
8774             cp_lexer_consume_token (parser->lexer);
8775             expr_hi =
8776               cp_parser_constant_expression (parser,
8777                                              /*allow_non_constant_p=*/false,
8778                                              NULL);
8779             /* We don't need to emit warnings here, as the common code
8780                will do this for us.  */
8781           }
8782         else
8783           expr_hi = NULL_TREE;
8784
8785         if (parser->in_switch_statement_p)
8786           finish_case_label (token->location, expr, expr_hi);
8787         else
8788           error_at (token->location,
8789                     "case label %qE not within a switch statement",
8790                     expr);
8791       }
8792       break;
8793
8794     case RID_DEFAULT:
8795       /* Consume the `default' token.  */
8796       cp_lexer_consume_token (parser->lexer);
8797
8798       if (parser->in_switch_statement_p)
8799         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8800       else
8801         error_at (token->location, "case label not within a switch statement");
8802       break;
8803
8804     default:
8805       /* Anything else must be an ordinary label.  */
8806       label = finish_label_stmt (cp_parser_identifier (parser));
8807       break;
8808     }
8809
8810   /* Require the `:' token.  */
8811   cp_parser_require (parser, CPP_COLON, RT_COLON);
8812
8813   /* An ordinary label may optionally be followed by attributes.
8814      However, this is only permitted if the attributes are then
8815      followed by a semicolon.  This is because, for backward
8816      compatibility, when parsing
8817        lab: __attribute__ ((unused)) int i;
8818      we want the attribute to attach to "i", not "lab".  */
8819   if (label != NULL_TREE
8820       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8821     {
8822       tree attrs;
8823
8824       cp_parser_parse_tentatively (parser);
8825       attrs = cp_parser_attributes_opt (parser);
8826       if (attrs == NULL_TREE
8827           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8828         cp_parser_abort_tentative_parse (parser);
8829       else if (!cp_parser_parse_definitely (parser))
8830         ;
8831       else
8832         cplus_decl_attributes (&label, attrs, 0);
8833     }
8834
8835   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8836 }
8837
8838 /* Parse an expression-statement.
8839
8840    expression-statement:
8841      expression [opt] ;
8842
8843    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8844    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8845    indicates whether this expression-statement is part of an
8846    expression statement.  */
8847
8848 static tree
8849 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8850 {
8851   tree statement = NULL_TREE;
8852   cp_token *token = cp_lexer_peek_token (parser->lexer);
8853
8854   /* If the next token is a ';', then there is no expression
8855      statement.  */
8856   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8857     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8858
8859   /* Give a helpful message for "A<T>::type t;" and the like.  */
8860   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8861       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8862     {
8863       if (TREE_CODE (statement) == SCOPE_REF)
8864         error_at (token->location, "need %<typename%> before %qE because "
8865                   "%qT is a dependent scope",
8866                   statement, TREE_OPERAND (statement, 0));
8867       else if (is_overloaded_fn (statement)
8868                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8869         {
8870           /* A::A a; */
8871           tree fn = get_first_fn (statement);
8872           error_at (token->location,
8873                     "%<%T::%D%> names the constructor, not the type",
8874                     DECL_CONTEXT (fn), DECL_NAME (fn));
8875         }
8876     }
8877
8878   /* Consume the final `;'.  */
8879   cp_parser_consume_semicolon_at_end_of_statement (parser);
8880
8881   if (in_statement_expr
8882       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8883     /* This is the final expression statement of a statement
8884        expression.  */
8885     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8886   else if (statement)
8887     statement = finish_expr_stmt (statement);
8888   else
8889     finish_stmt ();
8890
8891   return statement;
8892 }
8893
8894 /* Parse a compound-statement.
8895
8896    compound-statement:
8897      { statement-seq [opt] }
8898
8899    GNU extension:
8900
8901    compound-statement:
8902      { label-declaration-seq [opt] statement-seq [opt] }
8903
8904    label-declaration-seq:
8905      label-declaration
8906      label-declaration-seq label-declaration
8907
8908    Returns a tree representing the statement.  */
8909
8910 static tree
8911 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8912                               bool in_try, bool function_body)
8913 {
8914   tree compound_stmt;
8915
8916   /* Consume the `{'.  */
8917   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8918     return error_mark_node;
8919   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8920       && !function_body)
8921     pedwarn (input_location, OPT_pedantic,
8922              "compound-statement in constexpr function");
8923   /* Begin the compound-statement.  */
8924   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8925   /* If the next keyword is `__label__' we have a label declaration.  */
8926   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8927     cp_parser_label_declaration (parser);
8928   /* Parse an (optional) statement-seq.  */
8929   cp_parser_statement_seq_opt (parser, in_statement_expr);
8930   /* Finish the compound-statement.  */
8931   finish_compound_stmt (compound_stmt);
8932   /* Consume the `}'.  */
8933   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8934
8935   return compound_stmt;
8936 }
8937
8938 /* Parse an (optional) statement-seq.
8939
8940    statement-seq:
8941      statement
8942      statement-seq [opt] statement  */
8943
8944 static void
8945 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8946 {
8947   /* Scan statements until there aren't any more.  */
8948   while (true)
8949     {
8950       cp_token *token = cp_lexer_peek_token (parser->lexer);
8951
8952       /* If we are looking at a `}', then we have run out of
8953          statements; the same is true if we have reached the end
8954          of file, or have stumbled upon a stray '@end'.  */
8955       if (token->type == CPP_CLOSE_BRACE
8956           || token->type == CPP_EOF
8957           || token->type == CPP_PRAGMA_EOL
8958           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8959         break;
8960       
8961       /* If we are in a compound statement and find 'else' then
8962          something went wrong.  */
8963       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8964         {
8965           if (parser->in_statement & IN_IF_STMT) 
8966             break;
8967           else
8968             {
8969               token = cp_lexer_consume_token (parser->lexer);
8970               error_at (token->location, "%<else%> without a previous %<if%>");
8971             }
8972         }
8973
8974       /* Parse the statement.  */
8975       cp_parser_statement (parser, in_statement_expr, true, NULL);
8976     }
8977 }
8978
8979 /* Parse a selection-statement.
8980
8981    selection-statement:
8982      if ( condition ) statement
8983      if ( condition ) statement else statement
8984      switch ( condition ) statement
8985
8986    Returns the new IF_STMT or SWITCH_STMT.
8987
8988    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8989    is a (possibly labeled) if statement which is not enclosed in
8990    braces and has an else clause.  This is used to implement
8991    -Wparentheses.  */
8992
8993 static tree
8994 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8995 {
8996   cp_token *token;
8997   enum rid keyword;
8998
8999   if (if_p != NULL)
9000     *if_p = false;
9001
9002   /* Peek at the next token.  */
9003   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9004
9005   /* See what kind of keyword it is.  */
9006   keyword = token->keyword;
9007   switch (keyword)
9008     {
9009     case RID_IF:
9010     case RID_SWITCH:
9011       {
9012         tree statement;
9013         tree condition;
9014
9015         /* Look for the `('.  */
9016         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9017           {
9018             cp_parser_skip_to_end_of_statement (parser);
9019             return error_mark_node;
9020           }
9021
9022         /* Begin the selection-statement.  */
9023         if (keyword == RID_IF)
9024           statement = begin_if_stmt ();
9025         else
9026           statement = begin_switch_stmt ();
9027
9028         /* Parse the condition.  */
9029         condition = cp_parser_condition (parser);
9030         /* Look for the `)'.  */
9031         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9032           cp_parser_skip_to_closing_parenthesis (parser, true, false,
9033                                                  /*consume_paren=*/true);
9034
9035         if (keyword == RID_IF)
9036           {
9037             bool nested_if;
9038             unsigned char in_statement;
9039
9040             /* Add the condition.  */
9041             finish_if_stmt_cond (condition, statement);
9042
9043             /* Parse the then-clause.  */
9044             in_statement = parser->in_statement;
9045             parser->in_statement |= IN_IF_STMT;
9046             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9047               {
9048                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9049                 add_stmt (build_empty_stmt (loc));
9050                 cp_lexer_consume_token (parser->lexer);
9051                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9052                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9053                               "empty body in an %<if%> statement");
9054                 nested_if = false;
9055               }
9056             else
9057               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9058             parser->in_statement = in_statement;
9059
9060             finish_then_clause (statement);
9061
9062             /* If the next token is `else', parse the else-clause.  */
9063             if (cp_lexer_next_token_is_keyword (parser->lexer,
9064                                                 RID_ELSE))
9065               {
9066                 /* Consume the `else' keyword.  */
9067                 cp_lexer_consume_token (parser->lexer);
9068                 begin_else_clause (statement);
9069                 /* Parse the else-clause.  */
9070                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9071                   {
9072                     location_t loc;
9073                     loc = cp_lexer_peek_token (parser->lexer)->location;
9074                     warning_at (loc,
9075                                 OPT_Wempty_body, "suggest braces around "
9076                                 "empty body in an %<else%> statement");
9077                     add_stmt (build_empty_stmt (loc));
9078                     cp_lexer_consume_token (parser->lexer);
9079                   }
9080                 else
9081                   cp_parser_implicitly_scoped_statement (parser, NULL);
9082
9083                 finish_else_clause (statement);
9084
9085                 /* If we are currently parsing a then-clause, then
9086                    IF_P will not be NULL.  We set it to true to
9087                    indicate that this if statement has an else clause.
9088                    This may trigger the Wparentheses warning below
9089                    when we get back up to the parent if statement.  */
9090                 if (if_p != NULL)
9091                   *if_p = true;
9092               }
9093             else
9094               {
9095                 /* This if statement does not have an else clause.  If
9096                    NESTED_IF is true, then the then-clause is an if
9097                    statement which does have an else clause.  We warn
9098                    about the potential ambiguity.  */
9099                 if (nested_if)
9100                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9101                               "suggest explicit braces to avoid ambiguous"
9102                               " %<else%>");
9103               }
9104
9105             /* Now we're all done with the if-statement.  */
9106             finish_if_stmt (statement);
9107           }
9108         else
9109           {
9110             bool in_switch_statement_p;
9111             unsigned char in_statement;
9112
9113             /* Add the condition.  */
9114             finish_switch_cond (condition, statement);
9115
9116             /* Parse the body of the switch-statement.  */
9117             in_switch_statement_p = parser->in_switch_statement_p;
9118             in_statement = parser->in_statement;
9119             parser->in_switch_statement_p = true;
9120             parser->in_statement |= IN_SWITCH_STMT;
9121             cp_parser_implicitly_scoped_statement (parser, NULL);
9122             parser->in_switch_statement_p = in_switch_statement_p;
9123             parser->in_statement = in_statement;
9124
9125             /* Now we're all done with the switch-statement.  */
9126             finish_switch_stmt (statement);
9127           }
9128
9129         return statement;
9130       }
9131       break;
9132
9133     default:
9134       cp_parser_error (parser, "expected selection-statement");
9135       return error_mark_node;
9136     }
9137 }
9138
9139 /* Parse a condition.
9140
9141    condition:
9142      expression
9143      type-specifier-seq declarator = initializer-clause
9144      type-specifier-seq declarator braced-init-list
9145
9146    GNU Extension:
9147
9148    condition:
9149      type-specifier-seq declarator asm-specification [opt]
9150        attributes [opt] = assignment-expression
9151
9152    Returns the expression that should be tested.  */
9153
9154 static tree
9155 cp_parser_condition (cp_parser* parser)
9156 {
9157   cp_decl_specifier_seq type_specifiers;
9158   const char *saved_message;
9159   int declares_class_or_enum;
9160
9161   /* Try the declaration first.  */
9162   cp_parser_parse_tentatively (parser);
9163   /* New types are not allowed in the type-specifier-seq for a
9164      condition.  */
9165   saved_message = parser->type_definition_forbidden_message;
9166   parser->type_definition_forbidden_message
9167     = G_("types may not be defined in conditions");
9168   /* Parse the type-specifier-seq.  */
9169   cp_parser_decl_specifier_seq (parser,
9170                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9171                                 &type_specifiers,
9172                                 &declares_class_or_enum);
9173   /* Restore the saved message.  */
9174   parser->type_definition_forbidden_message = saved_message;
9175   /* If all is well, we might be looking at a declaration.  */
9176   if (!cp_parser_error_occurred (parser))
9177     {
9178       tree decl;
9179       tree asm_specification;
9180       tree attributes;
9181       cp_declarator *declarator;
9182       tree initializer = NULL_TREE;
9183
9184       /* Parse the declarator.  */
9185       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9186                                          /*ctor_dtor_or_conv_p=*/NULL,
9187                                          /*parenthesized_p=*/NULL,
9188                                          /*member_p=*/false);
9189       /* Parse the attributes.  */
9190       attributes = cp_parser_attributes_opt (parser);
9191       /* Parse the asm-specification.  */
9192       asm_specification = cp_parser_asm_specification_opt (parser);
9193       /* If the next token is not an `=' or '{', then we might still be
9194          looking at an expression.  For example:
9195
9196            if (A(a).x)
9197
9198          looks like a decl-specifier-seq and a declarator -- but then
9199          there is no `=', so this is an expression.  */
9200       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9201           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9202         cp_parser_simulate_error (parser);
9203         
9204       /* If we did see an `=' or '{', then we are looking at a declaration
9205          for sure.  */
9206       if (cp_parser_parse_definitely (parser))
9207         {
9208           tree pushed_scope;
9209           bool non_constant_p;
9210           bool flags = LOOKUP_ONLYCONVERTING;
9211
9212           /* Create the declaration.  */
9213           decl = start_decl (declarator, &type_specifiers,
9214                              /*initialized_p=*/true,
9215                              attributes, /*prefix_attributes=*/NULL_TREE,
9216                              &pushed_scope);
9217
9218           /* Parse the initializer.  */
9219           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9220             {
9221               initializer = cp_parser_braced_list (parser, &non_constant_p);
9222               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9223               flags = 0;
9224             }
9225           else
9226             {
9227               /* Consume the `='.  */
9228               cp_parser_require (parser, CPP_EQ, RT_EQ);
9229               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9230             }
9231           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9232             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9233
9234           /* Process the initializer.  */
9235           cp_finish_decl (decl,
9236                           initializer, !non_constant_p,
9237                           asm_specification,
9238                           flags);
9239
9240           if (pushed_scope)
9241             pop_scope (pushed_scope);
9242
9243           return convert_from_reference (decl);
9244         }
9245     }
9246   /* If we didn't even get past the declarator successfully, we are
9247      definitely not looking at a declaration.  */
9248   else
9249     cp_parser_abort_tentative_parse (parser);
9250
9251   /* Otherwise, we are looking at an expression.  */
9252   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9253 }
9254
9255 /* Parses a for-statement or range-for-statement until the closing ')',
9256    not included. */
9257
9258 static tree
9259 cp_parser_for (cp_parser *parser)
9260 {
9261   tree init, scope, decl;
9262   bool is_range_for;
9263
9264   /* Begin the for-statement.  */
9265   scope = begin_for_scope (&init);
9266
9267   /* Parse the initialization.  */
9268   is_range_for = cp_parser_for_init_statement (parser, &decl);
9269
9270   if (is_range_for)
9271     return cp_parser_range_for (parser, scope, init, decl);
9272   else
9273     return cp_parser_c_for (parser, scope, init);
9274 }
9275
9276 static tree
9277 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9278 {
9279   /* Normal for loop */
9280   tree condition = NULL_TREE;
9281   tree expression = NULL_TREE;
9282   tree stmt;
9283
9284   stmt = begin_for_stmt (scope, init);
9285   /* The for-init-statement has already been parsed in
9286      cp_parser_for_init_statement, so no work is needed here.  */
9287   finish_for_init_stmt (stmt);
9288
9289   /* If there's a condition, process it.  */
9290   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9291     condition = cp_parser_condition (parser);
9292   finish_for_cond (condition, stmt);
9293   /* Look for the `;'.  */
9294   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9295
9296   /* If there's an expression, process it.  */
9297   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9298     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9299   finish_for_expr (expression, stmt);
9300
9301   return stmt;
9302 }
9303
9304 /* Tries to parse a range-based for-statement:
9305
9306   range-based-for:
9307     decl-specifier-seq declarator : expression
9308
9309   The decl-specifier-seq declarator and the `:' are already parsed by
9310   cp_parser_for_init_statement. If processing_template_decl it returns a
9311   newly created RANGE_FOR_STMT; if not, it is converted to a
9312   regular FOR_STMT.  */
9313
9314 static tree
9315 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9316 {
9317   tree stmt, range_expr;
9318
9319   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9320     {
9321       bool expr_non_constant_p;
9322       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9323     }
9324   else
9325     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9326
9327   /* If in template, STMT is converted to a normal for-statement
9328      at instantiation. If not, it is done just ahead. */
9329   if (processing_template_decl)
9330     {
9331       if (check_for_bare_parameter_packs (range_expr))
9332         range_expr = error_mark_node;
9333       stmt = begin_range_for_stmt (scope, init);
9334       finish_range_for_decl (stmt, range_decl, range_expr);
9335       if (!type_dependent_expression_p (range_expr)
9336           /* do_auto_deduction doesn't mess with template init-lists.  */
9337           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9338         do_range_for_auto_deduction (range_decl, range_expr);
9339     }
9340   else
9341     {
9342       stmt = begin_for_stmt (scope, init);
9343       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9344     }
9345   return stmt;
9346 }
9347
9348 /* Subroutine of cp_convert_range_for: given the initializer expression,
9349    builds up the range temporary.  */
9350
9351 static tree
9352 build_range_temp (tree range_expr)
9353 {
9354   tree range_type, range_temp;
9355
9356   /* Find out the type deduced by the declaration
9357      `auto &&__range = range_expr'.  */
9358   range_type = cp_build_reference_type (make_auto (), true);
9359   range_type = do_auto_deduction (range_type, range_expr,
9360                                   type_uses_auto (range_type));
9361
9362   /* Create the __range variable.  */
9363   range_temp = build_decl (input_location, VAR_DECL,
9364                            get_identifier ("__for_range"), range_type);
9365   TREE_USED (range_temp) = 1;
9366   DECL_ARTIFICIAL (range_temp) = 1;
9367
9368   return range_temp;
9369 }
9370
9371 /* Used by cp_parser_range_for in template context: we aren't going to
9372    do a full conversion yet, but we still need to resolve auto in the
9373    type of the for-range-declaration if present.  This is basically
9374    a shortcut version of cp_convert_range_for.  */
9375
9376 static void
9377 do_range_for_auto_deduction (tree decl, tree range_expr)
9378 {
9379   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9380   if (auto_node)
9381     {
9382       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9383       range_temp = convert_from_reference (build_range_temp (range_expr));
9384       iter_type = (cp_parser_perform_range_for_lookup
9385                    (range_temp, &begin_dummy, &end_dummy));
9386       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9387       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9388                                         tf_warning_or_error);
9389       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9390                                             iter_decl, auto_node);
9391     }
9392 }
9393
9394 /* Converts a range-based for-statement into a normal
9395    for-statement, as per the definition.
9396
9397       for (RANGE_DECL : RANGE_EXPR)
9398         BLOCK
9399
9400    should be equivalent to:
9401
9402       {
9403         auto &&__range = RANGE_EXPR;
9404         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9405               __begin != __end;
9406               ++__begin)
9407           {
9408               RANGE_DECL = *__begin;
9409               BLOCK
9410           }
9411       }
9412
9413    If RANGE_EXPR is an array:
9414         BEGIN_EXPR = __range
9415         END_EXPR = __range + ARRAY_SIZE(__range)
9416    Else if RANGE_EXPR has a member 'begin' or 'end':
9417         BEGIN_EXPR = __range.begin()
9418         END_EXPR = __range.end()
9419    Else:
9420         BEGIN_EXPR = begin(__range)
9421         END_EXPR = end(__range);
9422
9423    If __range has a member 'begin' but not 'end', or vice versa, we must
9424    still use the second alternative (it will surely fail, however).
9425    When calling begin()/end() in the third alternative we must use
9426    argument dependent lookup, but always considering 'std' as an associated
9427    namespace.  */
9428
9429 tree
9430 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9431 {
9432   tree begin, end;
9433   tree iter_type, begin_expr, end_expr;
9434   tree condition, expression;
9435
9436   if (range_decl == error_mark_node || range_expr == error_mark_node)
9437     /* If an error happened previously do nothing or else a lot of
9438        unhelpful errors would be issued.  */
9439     begin_expr = end_expr = iter_type = error_mark_node;
9440   else
9441     {
9442       tree range_temp = build_range_temp (range_expr);
9443       pushdecl (range_temp);
9444       cp_finish_decl (range_temp, range_expr,
9445                       /*is_constant_init*/false, NULL_TREE,
9446                       LOOKUP_ONLYCONVERTING);
9447
9448       range_temp = convert_from_reference (range_temp);
9449       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9450                                                       &begin_expr, &end_expr);
9451     }
9452
9453   /* The new for initialization statement.  */
9454   begin = build_decl (input_location, VAR_DECL,
9455                       get_identifier ("__for_begin"), iter_type);
9456   TREE_USED (begin) = 1;
9457   DECL_ARTIFICIAL (begin) = 1;
9458   pushdecl (begin);
9459   cp_finish_decl (begin, begin_expr,
9460                   /*is_constant_init*/false, NULL_TREE,
9461                   LOOKUP_ONLYCONVERTING);
9462
9463   end = build_decl (input_location, VAR_DECL,
9464                     get_identifier ("__for_end"), iter_type);
9465   TREE_USED (end) = 1;
9466   DECL_ARTIFICIAL (end) = 1;
9467   pushdecl (end);
9468   cp_finish_decl (end, end_expr,
9469                   /*is_constant_init*/false, NULL_TREE,
9470                   LOOKUP_ONLYCONVERTING);
9471
9472   finish_for_init_stmt (statement);
9473
9474   /* The new for condition.  */
9475   condition = build_x_binary_op (NE_EXPR,
9476                                  begin, ERROR_MARK,
9477                                  end, ERROR_MARK,
9478                                  NULL, tf_warning_or_error);
9479   finish_for_cond (condition, statement);
9480
9481   /* The new increment expression.  */
9482   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9483   finish_for_expr (expression, statement);
9484
9485   /* The declaration is initialized with *__begin inside the loop body.  */
9486   cp_finish_decl (range_decl,
9487                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9488                   /*is_constant_init*/false, NULL_TREE,
9489                   LOOKUP_ONLYCONVERTING);
9490
9491   return statement;
9492 }
9493
9494 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9495    We need to solve both at the same time because the method used
9496    depends on the existence of members begin or end.
9497    Returns the type deduced for the iterator expression.  */
9498
9499 static tree
9500 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9501 {
9502   if (error_operand_p (range))
9503     {
9504       *begin = *end = error_mark_node;
9505       return error_mark_node;
9506     }
9507
9508   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9509     {
9510       error ("range-based %<for%> expression of type %qT "
9511              "has incomplete type", TREE_TYPE (range));
9512       *begin = *end = error_mark_node;
9513       return error_mark_node;
9514     }
9515   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9516     {
9517       /* If RANGE is an array, we will use pointer arithmetic.  */
9518       *begin = range;
9519       *end = build_binary_op (input_location, PLUS_EXPR,
9520                               range,
9521                               array_type_nelts_top (TREE_TYPE (range)),
9522                               0);
9523       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9524     }
9525   else
9526     {
9527       /* If it is not an array, we must do a bit of magic.  */
9528       tree id_begin, id_end;
9529       tree member_begin, member_end;
9530
9531       *begin = *end = error_mark_node;
9532
9533       id_begin = get_identifier ("begin");
9534       id_end = get_identifier ("end");
9535       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9536                                     /*protect=*/2, /*want_type=*/false,
9537                                     tf_warning_or_error);
9538       member_end = lookup_member (TREE_TYPE (range), id_end,
9539                                   /*protect=*/2, /*want_type=*/false,
9540                                   tf_warning_or_error);
9541
9542       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9543         {
9544           /* Use the member functions.  */
9545           if (member_begin != NULL_TREE)
9546             *begin = cp_parser_range_for_member_function (range, id_begin);
9547           else
9548             error ("range-based %<for%> expression of type %qT has an "
9549                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9550
9551           if (member_end != NULL_TREE)
9552             *end = cp_parser_range_for_member_function (range, id_end);
9553           else
9554             error ("range-based %<for%> expression of type %qT has a "
9555                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9556         }
9557       else
9558         {
9559           /* Use global functions with ADL.  */
9560           VEC(tree,gc) *vec;
9561           vec = make_tree_vector ();
9562
9563           VEC_safe_push (tree, gc, vec, range);
9564
9565           member_begin = perform_koenig_lookup (id_begin, vec,
9566                                                 /*include_std=*/true,
9567                                                 tf_warning_or_error);
9568           *begin = finish_call_expr (member_begin, &vec, false, true,
9569                                      tf_warning_or_error);
9570           member_end = perform_koenig_lookup (id_end, vec,
9571                                               /*include_std=*/true,
9572                                               tf_warning_or_error);
9573           *end = finish_call_expr (member_end, &vec, false, true,
9574                                    tf_warning_or_error);
9575
9576           release_tree_vector (vec);
9577         }
9578
9579       /* Last common checks.  */
9580       if (*begin == error_mark_node || *end == error_mark_node)
9581         {
9582           /* If one of the expressions is an error do no more checks.  */
9583           *begin = *end = error_mark_node;
9584           return error_mark_node;
9585         }
9586       else
9587         {
9588           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9589           /* The unqualified type of the __begin and __end temporaries should
9590              be the same, as required by the multiple auto declaration.  */
9591           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9592             error ("inconsistent begin/end types in range-based %<for%> "
9593                    "statement: %qT and %qT",
9594                    TREE_TYPE (*begin), TREE_TYPE (*end));
9595           return iter_type;
9596         }
9597     }
9598 }
9599
9600 /* Helper function for cp_parser_perform_range_for_lookup.
9601    Builds a tree for RANGE.IDENTIFIER().  */
9602
9603 static tree
9604 cp_parser_range_for_member_function (tree range, tree identifier)
9605 {
9606   tree member, res;
9607   VEC(tree,gc) *vec;
9608
9609   member = finish_class_member_access_expr (range, identifier,
9610                                             false, tf_warning_or_error);
9611   if (member == error_mark_node)
9612     return error_mark_node;
9613
9614   vec = make_tree_vector ();
9615   res = finish_call_expr (member, &vec,
9616                           /*disallow_virtual=*/false,
9617                           /*koenig_p=*/false,
9618                           tf_warning_or_error);
9619   release_tree_vector (vec);
9620   return res;
9621 }
9622
9623 /* Parse an iteration-statement.
9624
9625    iteration-statement:
9626      while ( condition ) statement
9627      do statement while ( expression ) ;
9628      for ( for-init-statement condition [opt] ; expression [opt] )
9629        statement
9630
9631    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9632
9633 static tree
9634 cp_parser_iteration_statement (cp_parser* parser)
9635 {
9636   cp_token *token;
9637   enum rid keyword;
9638   tree statement;
9639   unsigned char in_statement;
9640
9641   /* Peek at the next token.  */
9642   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9643   if (!token)
9644     return error_mark_node;
9645
9646   /* Remember whether or not we are already within an iteration
9647      statement.  */
9648   in_statement = parser->in_statement;
9649
9650   /* See what kind of keyword it is.  */
9651   keyword = token->keyword;
9652   switch (keyword)
9653     {
9654     case RID_WHILE:
9655       {
9656         tree condition;
9657
9658         /* Begin the while-statement.  */
9659         statement = begin_while_stmt ();
9660         /* Look for the `('.  */
9661         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9662         /* Parse the condition.  */
9663         condition = cp_parser_condition (parser);
9664         finish_while_stmt_cond (condition, statement);
9665         /* Look for the `)'.  */
9666         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9667         /* Parse the dependent statement.  */
9668         parser->in_statement = IN_ITERATION_STMT;
9669         cp_parser_already_scoped_statement (parser);
9670         parser->in_statement = in_statement;
9671         /* We're done with the while-statement.  */
9672         finish_while_stmt (statement);
9673       }
9674       break;
9675
9676     case RID_DO:
9677       {
9678         tree expression;
9679
9680         /* Begin the do-statement.  */
9681         statement = begin_do_stmt ();
9682         /* Parse the body of the do-statement.  */
9683         parser->in_statement = IN_ITERATION_STMT;
9684         cp_parser_implicitly_scoped_statement (parser, NULL);
9685         parser->in_statement = in_statement;
9686         finish_do_body (statement);
9687         /* Look for the `while' keyword.  */
9688         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9689         /* Look for the `('.  */
9690         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9691         /* Parse the expression.  */
9692         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9693         /* We're done with the do-statement.  */
9694         finish_do_stmt (expression, statement);
9695         /* Look for the `)'.  */
9696         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9697         /* Look for the `;'.  */
9698         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9699       }
9700       break;
9701
9702     case RID_FOR:
9703       {
9704         /* Look for the `('.  */
9705         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9706
9707         statement = cp_parser_for (parser);
9708
9709         /* Look for the `)'.  */
9710         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9711
9712         /* Parse the body of the for-statement.  */
9713         parser->in_statement = IN_ITERATION_STMT;
9714         cp_parser_already_scoped_statement (parser);
9715         parser->in_statement = in_statement;
9716
9717         /* We're done with the for-statement.  */
9718         finish_for_stmt (statement);
9719       }
9720       break;
9721
9722     default:
9723       cp_parser_error (parser, "expected iteration-statement");
9724       statement = error_mark_node;
9725       break;
9726     }
9727
9728   return statement;
9729 }
9730
9731 /* Parse a for-init-statement or the declarator of a range-based-for.
9732    Returns true if a range-based-for declaration is seen.
9733
9734    for-init-statement:
9735      expression-statement
9736      simple-declaration  */
9737
9738 static bool
9739 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9740 {
9741   /* If the next token is a `;', then we have an empty
9742      expression-statement.  Grammatically, this is also a
9743      simple-declaration, but an invalid one, because it does not
9744      declare anything.  Therefore, if we did not handle this case
9745      specially, we would issue an error message about an invalid
9746      declaration.  */
9747   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9748     {
9749       bool is_range_for = false;
9750       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9751
9752       parser->colon_corrects_to_scope_p = false;
9753
9754       /* We're going to speculatively look for a declaration, falling back
9755          to an expression, if necessary.  */
9756       cp_parser_parse_tentatively (parser);
9757       /* Parse the declaration.  */
9758       cp_parser_simple_declaration (parser,
9759                                     /*function_definition_allowed_p=*/false,
9760                                     decl);
9761       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9762       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9763         {
9764           /* It is a range-for, consume the ':' */
9765           cp_lexer_consume_token (parser->lexer);
9766           is_range_for = true;
9767           if (cxx_dialect < cxx0x)
9768             {
9769               error_at (cp_lexer_peek_token (parser->lexer)->location,
9770                         "range-based %<for%> loops are not allowed "
9771                         "in C++98 mode");
9772               *decl = error_mark_node;
9773             }
9774         }
9775       else
9776           /* The ';' is not consumed yet because we told
9777              cp_parser_simple_declaration not to.  */
9778           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9779
9780       if (cp_parser_parse_definitely (parser))
9781         return is_range_for;
9782       /* If the tentative parse failed, then we shall need to look for an
9783          expression-statement.  */
9784     }
9785   /* If we are here, it is an expression-statement.  */
9786   cp_parser_expression_statement (parser, NULL_TREE);
9787   return false;
9788 }
9789
9790 /* Parse a jump-statement.
9791
9792    jump-statement:
9793      break ;
9794      continue ;
9795      return expression [opt] ;
9796      return braced-init-list ;
9797      goto identifier ;
9798
9799    GNU extension:
9800
9801    jump-statement:
9802      goto * expression ;
9803
9804    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9805
9806 static tree
9807 cp_parser_jump_statement (cp_parser* parser)
9808 {
9809   tree statement = error_mark_node;
9810   cp_token *token;
9811   enum rid keyword;
9812   unsigned char in_statement;
9813
9814   /* Peek at the next token.  */
9815   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9816   if (!token)
9817     return error_mark_node;
9818
9819   /* See what kind of keyword it is.  */
9820   keyword = token->keyword;
9821   switch (keyword)
9822     {
9823     case RID_BREAK:
9824       in_statement = parser->in_statement & ~IN_IF_STMT;      
9825       switch (in_statement)
9826         {
9827         case 0:
9828           error_at (token->location, "break statement not within loop or switch");
9829           break;
9830         default:
9831           gcc_assert ((in_statement & IN_SWITCH_STMT)
9832                       || in_statement == IN_ITERATION_STMT);
9833           statement = finish_break_stmt ();
9834           break;
9835         case IN_OMP_BLOCK:
9836           error_at (token->location, "invalid exit from OpenMP structured block");
9837           break;
9838         case IN_OMP_FOR:
9839           error_at (token->location, "break statement used with OpenMP for loop");
9840           break;
9841         }
9842       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9843       break;
9844
9845     case RID_CONTINUE:
9846       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9847         {
9848         case 0:
9849           error_at (token->location, "continue statement not within a loop");
9850           break;
9851         case IN_ITERATION_STMT:
9852         case IN_OMP_FOR:
9853           statement = finish_continue_stmt ();
9854           break;
9855         case IN_OMP_BLOCK:
9856           error_at (token->location, "invalid exit from OpenMP structured block");
9857           break;
9858         default:
9859           gcc_unreachable ();
9860         }
9861       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9862       break;
9863
9864     case RID_RETURN:
9865       {
9866         tree expr;
9867         bool expr_non_constant_p;
9868
9869         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9870           {
9871             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9872             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9873           }
9874         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9875           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9876         else
9877           /* If the next token is a `;', then there is no
9878              expression.  */
9879           expr = NULL_TREE;
9880         /* Build the return-statement.  */
9881         statement = finish_return_stmt (expr);
9882         /* Look for the final `;'.  */
9883         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9884       }
9885       break;
9886
9887     case RID_GOTO:
9888       /* Create the goto-statement.  */
9889       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9890         {
9891           /* Issue a warning about this use of a GNU extension.  */
9892           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9893           /* Consume the '*' token.  */
9894           cp_lexer_consume_token (parser->lexer);
9895           /* Parse the dependent expression.  */
9896           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9897         }
9898       else
9899         finish_goto_stmt (cp_parser_identifier (parser));
9900       /* Look for the final `;'.  */
9901       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9902       break;
9903
9904     default:
9905       cp_parser_error (parser, "expected jump-statement");
9906       break;
9907     }
9908
9909   return statement;
9910 }
9911
9912 /* Parse a declaration-statement.
9913
9914    declaration-statement:
9915      block-declaration  */
9916
9917 static void
9918 cp_parser_declaration_statement (cp_parser* parser)
9919 {
9920   void *p;
9921
9922   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9923   p = obstack_alloc (&declarator_obstack, 0);
9924
9925  /* Parse the block-declaration.  */
9926   cp_parser_block_declaration (parser, /*statement_p=*/true);
9927
9928   /* Free any declarators allocated.  */
9929   obstack_free (&declarator_obstack, p);
9930
9931   /* Finish off the statement.  */
9932   finish_stmt ();
9933 }
9934
9935 /* Some dependent statements (like `if (cond) statement'), are
9936    implicitly in their own scope.  In other words, if the statement is
9937    a single statement (as opposed to a compound-statement), it is
9938    none-the-less treated as if it were enclosed in braces.  Any
9939    declarations appearing in the dependent statement are out of scope
9940    after control passes that point.  This function parses a statement,
9941    but ensures that is in its own scope, even if it is not a
9942    compound-statement.
9943
9944    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9945    is a (possibly labeled) if statement which is not enclosed in
9946    braces and has an else clause.  This is used to implement
9947    -Wparentheses.
9948
9949    Returns the new statement.  */
9950
9951 static tree
9952 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9953 {
9954   tree statement;
9955
9956   if (if_p != NULL)
9957     *if_p = false;
9958
9959   /* Mark if () ; with a special NOP_EXPR.  */
9960   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9961     {
9962       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9963       cp_lexer_consume_token (parser->lexer);
9964       statement = add_stmt (build_empty_stmt (loc));
9965     }
9966   /* if a compound is opened, we simply parse the statement directly.  */
9967   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9968     statement = cp_parser_compound_statement (parser, NULL, false, false);
9969   /* If the token is not a `{', then we must take special action.  */
9970   else
9971     {
9972       /* Create a compound-statement.  */
9973       statement = begin_compound_stmt (0);
9974       /* Parse the dependent-statement.  */
9975       cp_parser_statement (parser, NULL_TREE, false, if_p);
9976       /* Finish the dummy compound-statement.  */
9977       finish_compound_stmt (statement);
9978     }
9979
9980   /* Return the statement.  */
9981   return statement;
9982 }
9983
9984 /* For some dependent statements (like `while (cond) statement'), we
9985    have already created a scope.  Therefore, even if the dependent
9986    statement is a compound-statement, we do not want to create another
9987    scope.  */
9988
9989 static void
9990 cp_parser_already_scoped_statement (cp_parser* parser)
9991 {
9992   /* If the token is a `{', then we must take special action.  */
9993   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9994     cp_parser_statement (parser, NULL_TREE, false, NULL);
9995   else
9996     {
9997       /* Avoid calling cp_parser_compound_statement, so that we
9998          don't create a new scope.  Do everything else by hand.  */
9999       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10000       /* If the next keyword is `__label__' we have a label declaration.  */
10001       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10002         cp_parser_label_declaration (parser);
10003       /* Parse an (optional) statement-seq.  */
10004       cp_parser_statement_seq_opt (parser, NULL_TREE);
10005       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10006     }
10007 }
10008
10009 /* Declarations [gram.dcl.dcl] */
10010
10011 /* Parse an optional declaration-sequence.
10012
10013    declaration-seq:
10014      declaration
10015      declaration-seq declaration  */
10016
10017 static void
10018 cp_parser_declaration_seq_opt (cp_parser* parser)
10019 {
10020   while (true)
10021     {
10022       cp_token *token;
10023
10024       token = cp_lexer_peek_token (parser->lexer);
10025
10026       if (token->type == CPP_CLOSE_BRACE
10027           || token->type == CPP_EOF
10028           || token->type == CPP_PRAGMA_EOL)
10029         break;
10030
10031       if (token->type == CPP_SEMICOLON)
10032         {
10033           /* A declaration consisting of a single semicolon is
10034              invalid.  Allow it unless we're being pedantic.  */
10035           cp_lexer_consume_token (parser->lexer);
10036           if (!in_system_header)
10037             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10038           continue;
10039         }
10040
10041       /* If we're entering or exiting a region that's implicitly
10042          extern "C", modify the lang context appropriately.  */
10043       if (!parser->implicit_extern_c && token->implicit_extern_c)
10044         {
10045           push_lang_context (lang_name_c);
10046           parser->implicit_extern_c = true;
10047         }
10048       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10049         {
10050           pop_lang_context ();
10051           parser->implicit_extern_c = false;
10052         }
10053
10054       if (token->type == CPP_PRAGMA)
10055         {
10056           /* A top-level declaration can consist solely of a #pragma.
10057              A nested declaration cannot, so this is done here and not
10058              in cp_parser_declaration.  (A #pragma at block scope is
10059              handled in cp_parser_statement.)  */
10060           cp_parser_pragma (parser, pragma_external);
10061           continue;
10062         }
10063
10064       /* Parse the declaration itself.  */
10065       cp_parser_declaration (parser);
10066     }
10067 }
10068
10069 /* Parse a declaration.
10070
10071    declaration:
10072      block-declaration
10073      function-definition
10074      template-declaration
10075      explicit-instantiation
10076      explicit-specialization
10077      linkage-specification
10078      namespace-definition
10079
10080    GNU extension:
10081
10082    declaration:
10083       __extension__ declaration */
10084
10085 static void
10086 cp_parser_declaration (cp_parser* parser)
10087 {
10088   cp_token token1;
10089   cp_token token2;
10090   int saved_pedantic;
10091   void *p;
10092   tree attributes = NULL_TREE;
10093
10094   /* Check for the `__extension__' keyword.  */
10095   if (cp_parser_extension_opt (parser, &saved_pedantic))
10096     {
10097       /* Parse the qualified declaration.  */
10098       cp_parser_declaration (parser);
10099       /* Restore the PEDANTIC flag.  */
10100       pedantic = saved_pedantic;
10101
10102       return;
10103     }
10104
10105   /* Try to figure out what kind of declaration is present.  */
10106   token1 = *cp_lexer_peek_token (parser->lexer);
10107
10108   if (token1.type != CPP_EOF)
10109     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10110   else
10111     {
10112       token2.type = CPP_EOF;
10113       token2.keyword = RID_MAX;
10114     }
10115
10116   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10117   p = obstack_alloc (&declarator_obstack, 0);
10118
10119   /* If the next token is `extern' and the following token is a string
10120      literal, then we have a linkage specification.  */
10121   if (token1.keyword == RID_EXTERN
10122       && cp_parser_is_pure_string_literal (&token2))
10123     cp_parser_linkage_specification (parser);
10124   /* If the next token is `template', then we have either a template
10125      declaration, an explicit instantiation, or an explicit
10126      specialization.  */
10127   else if (token1.keyword == RID_TEMPLATE)
10128     {
10129       /* `template <>' indicates a template specialization.  */
10130       if (token2.type == CPP_LESS
10131           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10132         cp_parser_explicit_specialization (parser);
10133       /* `template <' indicates a template declaration.  */
10134       else if (token2.type == CPP_LESS)
10135         cp_parser_template_declaration (parser, /*member_p=*/false);
10136       /* Anything else must be an explicit instantiation.  */
10137       else
10138         cp_parser_explicit_instantiation (parser);
10139     }
10140   /* If the next token is `export', then we have a template
10141      declaration.  */
10142   else if (token1.keyword == RID_EXPORT)
10143     cp_parser_template_declaration (parser, /*member_p=*/false);
10144   /* If the next token is `extern', 'static' or 'inline' and the one
10145      after that is `template', we have a GNU extended explicit
10146      instantiation directive.  */
10147   else if (cp_parser_allow_gnu_extensions_p (parser)
10148            && (token1.keyword == RID_EXTERN
10149                || token1.keyword == RID_STATIC
10150                || token1.keyword == RID_INLINE)
10151            && token2.keyword == RID_TEMPLATE)
10152     cp_parser_explicit_instantiation (parser);
10153   /* If the next token is `namespace', check for a named or unnamed
10154      namespace definition.  */
10155   else if (token1.keyword == RID_NAMESPACE
10156            && (/* A named namespace definition.  */
10157                (token2.type == CPP_NAME
10158                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10159                     != CPP_EQ))
10160                /* An unnamed namespace definition.  */
10161                || token2.type == CPP_OPEN_BRACE
10162                || token2.keyword == RID_ATTRIBUTE))
10163     cp_parser_namespace_definition (parser);
10164   /* An inline (associated) namespace definition.  */
10165   else if (token1.keyword == RID_INLINE
10166            && token2.keyword == RID_NAMESPACE)
10167     cp_parser_namespace_definition (parser);
10168   /* Objective-C++ declaration/definition.  */
10169   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10170     cp_parser_objc_declaration (parser, NULL_TREE);
10171   else if (c_dialect_objc ()
10172            && token1.keyword == RID_ATTRIBUTE
10173            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10174     cp_parser_objc_declaration (parser, attributes);
10175   /* We must have either a block declaration or a function
10176      definition.  */
10177   else
10178     /* Try to parse a block-declaration, or a function-definition.  */
10179     cp_parser_block_declaration (parser, /*statement_p=*/false);
10180
10181   /* Free any declarators allocated.  */
10182   obstack_free (&declarator_obstack, p);
10183 }
10184
10185 /* Parse a block-declaration.
10186
10187    block-declaration:
10188      simple-declaration
10189      asm-definition
10190      namespace-alias-definition
10191      using-declaration
10192      using-directive
10193
10194    GNU Extension:
10195
10196    block-declaration:
10197      __extension__ block-declaration
10198
10199    C++0x Extension:
10200
10201    block-declaration:
10202      static_assert-declaration
10203
10204    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10205    part of a declaration-statement.  */
10206
10207 static void
10208 cp_parser_block_declaration (cp_parser *parser,
10209                              bool      statement_p)
10210 {
10211   cp_token *token1;
10212   int saved_pedantic;
10213
10214   /* Check for the `__extension__' keyword.  */
10215   if (cp_parser_extension_opt (parser, &saved_pedantic))
10216     {
10217       /* Parse the qualified declaration.  */
10218       cp_parser_block_declaration (parser, statement_p);
10219       /* Restore the PEDANTIC flag.  */
10220       pedantic = saved_pedantic;
10221
10222       return;
10223     }
10224
10225   /* Peek at the next token to figure out which kind of declaration is
10226      present.  */
10227   token1 = cp_lexer_peek_token (parser->lexer);
10228
10229   /* If the next keyword is `asm', we have an asm-definition.  */
10230   if (token1->keyword == RID_ASM)
10231     {
10232       if (statement_p)
10233         cp_parser_commit_to_tentative_parse (parser);
10234       cp_parser_asm_definition (parser);
10235     }
10236   /* If the next keyword is `namespace', we have a
10237      namespace-alias-definition.  */
10238   else if (token1->keyword == RID_NAMESPACE)
10239     cp_parser_namespace_alias_definition (parser);
10240   /* If the next keyword is `using', we have a
10241      using-declaration, a using-directive, or an alias-declaration.  */
10242   else if (token1->keyword == RID_USING)
10243     {
10244       cp_token *token2;
10245
10246       if (statement_p)
10247         cp_parser_commit_to_tentative_parse (parser);
10248       /* If the token after `using' is `namespace', then we have a
10249          using-directive.  */
10250       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10251       if (token2->keyword == RID_NAMESPACE)
10252         cp_parser_using_directive (parser);
10253       /* If the second token after 'using' is '=', then we have an
10254          alias-declaration.  */
10255       else if (cxx_dialect >= cxx0x
10256                && token2->type == CPP_NAME
10257                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10258                    || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10259                        == RID_ATTRIBUTE)))
10260         cp_parser_alias_declaration (parser);
10261       /* Otherwise, it's a using-declaration.  */
10262       else
10263         cp_parser_using_declaration (parser,
10264                                      /*access_declaration_p=*/false);
10265     }
10266   /* If the next keyword is `__label__' we have a misplaced label
10267      declaration.  */
10268   else if (token1->keyword == RID_LABEL)
10269     {
10270       cp_lexer_consume_token (parser->lexer);
10271       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10272       cp_parser_skip_to_end_of_statement (parser);
10273       /* If the next token is now a `;', consume it.  */
10274       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10275         cp_lexer_consume_token (parser->lexer);
10276     }
10277   /* If the next token is `static_assert' we have a static assertion.  */
10278   else if (token1->keyword == RID_STATIC_ASSERT)
10279     cp_parser_static_assert (parser, /*member_p=*/false);
10280   /* Anything else must be a simple-declaration.  */
10281   else
10282     cp_parser_simple_declaration (parser, !statement_p,
10283                                   /*maybe_range_for_decl*/NULL);
10284 }
10285
10286 /* Parse a simple-declaration.
10287
10288    simple-declaration:
10289      decl-specifier-seq [opt] init-declarator-list [opt] ;
10290
10291    init-declarator-list:
10292      init-declarator
10293      init-declarator-list , init-declarator
10294
10295    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10296    function-definition as a simple-declaration.
10297
10298    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10299    parsed declaration if it is an uninitialized single declarator not followed
10300    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10301    if present, will not be consumed.  */
10302
10303 static void
10304 cp_parser_simple_declaration (cp_parser* parser,
10305                               bool function_definition_allowed_p,
10306                               tree *maybe_range_for_decl)
10307 {
10308   cp_decl_specifier_seq decl_specifiers;
10309   int declares_class_or_enum;
10310   bool saw_declarator;
10311
10312   if (maybe_range_for_decl)
10313     *maybe_range_for_decl = NULL_TREE;
10314
10315   /* Defer access checks until we know what is being declared; the
10316      checks for names appearing in the decl-specifier-seq should be
10317      done as if we were in the scope of the thing being declared.  */
10318   push_deferring_access_checks (dk_deferred);
10319
10320   /* Parse the decl-specifier-seq.  We have to keep track of whether
10321      or not the decl-specifier-seq declares a named class or
10322      enumeration type, since that is the only case in which the
10323      init-declarator-list is allowed to be empty.
10324
10325      [dcl.dcl]
10326
10327      In a simple-declaration, the optional init-declarator-list can be
10328      omitted only when declaring a class or enumeration, that is when
10329      the decl-specifier-seq contains either a class-specifier, an
10330      elaborated-type-specifier, or an enum-specifier.  */
10331   cp_parser_decl_specifier_seq (parser,
10332                                 CP_PARSER_FLAGS_OPTIONAL,
10333                                 &decl_specifiers,
10334                                 &declares_class_or_enum);
10335   /* We no longer need to defer access checks.  */
10336   stop_deferring_access_checks ();
10337
10338   /* In a block scope, a valid declaration must always have a
10339      decl-specifier-seq.  By not trying to parse declarators, we can
10340      resolve the declaration/expression ambiguity more quickly.  */
10341   if (!function_definition_allowed_p
10342       && !decl_specifiers.any_specifiers_p)
10343     {
10344       cp_parser_error (parser, "expected declaration");
10345       goto done;
10346     }
10347
10348   /* If the next two tokens are both identifiers, the code is
10349      erroneous. The usual cause of this situation is code like:
10350
10351        T t;
10352
10353      where "T" should name a type -- but does not.  */
10354   if (!decl_specifiers.any_type_specifiers_p
10355       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10356     {
10357       /* If parsing tentatively, we should commit; we really are
10358          looking at a declaration.  */
10359       cp_parser_commit_to_tentative_parse (parser);
10360       /* Give up.  */
10361       goto done;
10362     }
10363
10364   /* If we have seen at least one decl-specifier, and the next token
10365      is not a parenthesis, then we must be looking at a declaration.
10366      (After "int (" we might be looking at a functional cast.)  */
10367   if (decl_specifiers.any_specifiers_p
10368       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10369       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10370       && !cp_parser_error_occurred (parser))
10371     cp_parser_commit_to_tentative_parse (parser);
10372
10373   /* Keep going until we hit the `;' at the end of the simple
10374      declaration.  */
10375   saw_declarator = false;
10376   while (cp_lexer_next_token_is_not (parser->lexer,
10377                                      CPP_SEMICOLON))
10378     {
10379       cp_token *token;
10380       bool function_definition_p;
10381       tree decl;
10382
10383       if (saw_declarator)
10384         {
10385           /* If we are processing next declarator, coma is expected */
10386           token = cp_lexer_peek_token (parser->lexer);
10387           gcc_assert (token->type == CPP_COMMA);
10388           cp_lexer_consume_token (parser->lexer);
10389           if (maybe_range_for_decl)
10390             *maybe_range_for_decl = error_mark_node;
10391         }
10392       else
10393         saw_declarator = true;
10394
10395       /* Parse the init-declarator.  */
10396       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10397                                         /*checks=*/NULL,
10398                                         function_definition_allowed_p,
10399                                         /*member_p=*/false,
10400                                         declares_class_or_enum,
10401                                         &function_definition_p,
10402                                         maybe_range_for_decl);
10403       /* If an error occurred while parsing tentatively, exit quickly.
10404          (That usually happens when in the body of a function; each
10405          statement is treated as a declaration-statement until proven
10406          otherwise.)  */
10407       if (cp_parser_error_occurred (parser))
10408         goto done;
10409       /* Handle function definitions specially.  */
10410       if (function_definition_p)
10411         {
10412           /* If the next token is a `,', then we are probably
10413              processing something like:
10414
10415                void f() {}, *p;
10416
10417              which is erroneous.  */
10418           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10419             {
10420               cp_token *token = cp_lexer_peek_token (parser->lexer);
10421               error_at (token->location,
10422                         "mixing"
10423                         " declarations and function-definitions is forbidden");
10424             }
10425           /* Otherwise, we're done with the list of declarators.  */
10426           else
10427             {
10428               pop_deferring_access_checks ();
10429               return;
10430             }
10431         }
10432       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10433         *maybe_range_for_decl = decl;
10434       /* The next token should be either a `,' or a `;'.  */
10435       token = cp_lexer_peek_token (parser->lexer);
10436       /* If it's a `,', there are more declarators to come.  */
10437       if (token->type == CPP_COMMA)
10438         /* will be consumed next time around */;
10439       /* If it's a `;', we are done.  */
10440       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10441         break;
10442       /* Anything else is an error.  */
10443       else
10444         {
10445           /* If we have already issued an error message we don't need
10446              to issue another one.  */
10447           if (decl != error_mark_node
10448               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10449             cp_parser_error (parser, "expected %<,%> or %<;%>");
10450           /* Skip tokens until we reach the end of the statement.  */
10451           cp_parser_skip_to_end_of_statement (parser);
10452           /* If the next token is now a `;', consume it.  */
10453           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10454             cp_lexer_consume_token (parser->lexer);
10455           goto done;
10456         }
10457       /* After the first time around, a function-definition is not
10458          allowed -- even if it was OK at first.  For example:
10459
10460            int i, f() {}
10461
10462          is not valid.  */
10463       function_definition_allowed_p = false;
10464     }
10465
10466   /* Issue an error message if no declarators are present, and the
10467      decl-specifier-seq does not itself declare a class or
10468      enumeration.  */
10469   if (!saw_declarator)
10470     {
10471       if (cp_parser_declares_only_class_p (parser))
10472         shadow_tag (&decl_specifiers);
10473       /* Perform any deferred access checks.  */
10474       perform_deferred_access_checks ();
10475     }
10476
10477   /* Consume the `;'.  */
10478   if (!maybe_range_for_decl)
10479       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10480
10481  done:
10482   pop_deferring_access_checks ();
10483 }
10484
10485 /* Parse a decl-specifier-seq.
10486
10487    decl-specifier-seq:
10488      decl-specifier-seq [opt] decl-specifier
10489
10490    decl-specifier:
10491      storage-class-specifier
10492      type-specifier
10493      function-specifier
10494      friend
10495      typedef
10496
10497    GNU Extension:
10498
10499    decl-specifier:
10500      attributes
10501
10502    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10503
10504    The parser flags FLAGS is used to control type-specifier parsing.
10505
10506    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10507    flags:
10508
10509      1: one of the decl-specifiers is an elaborated-type-specifier
10510         (i.e., a type declaration)
10511      2: one of the decl-specifiers is an enum-specifier or a
10512         class-specifier (i.e., a type definition)
10513
10514    */
10515
10516 static void
10517 cp_parser_decl_specifier_seq (cp_parser* parser,
10518                               cp_parser_flags flags,
10519                               cp_decl_specifier_seq *decl_specs,
10520                               int* declares_class_or_enum)
10521 {
10522   bool constructor_possible_p = !parser->in_declarator_p;
10523   cp_token *start_token = NULL;
10524
10525   /* Clear DECL_SPECS.  */
10526   clear_decl_specs (decl_specs);
10527
10528   /* Assume no class or enumeration type is declared.  */
10529   *declares_class_or_enum = 0;
10530
10531   /* Keep reading specifiers until there are no more to read.  */
10532   while (true)
10533     {
10534       bool constructor_p;
10535       bool found_decl_spec;
10536       cp_token *token;
10537
10538       /* Peek at the next token.  */
10539       token = cp_lexer_peek_token (parser->lexer);
10540
10541       /* Save the first token of the decl spec list for error
10542          reporting.  */
10543       if (!start_token)
10544         start_token = token;
10545       /* Handle attributes.  */
10546       if (token->keyword == RID_ATTRIBUTE)
10547         {
10548           /* Parse the attributes.  */
10549           decl_specs->attributes
10550             = chainon (decl_specs->attributes,
10551                        cp_parser_attributes_opt (parser));
10552           continue;
10553         }
10554       /* Assume we will find a decl-specifier keyword.  */
10555       found_decl_spec = true;
10556       /* If the next token is an appropriate keyword, we can simply
10557          add it to the list.  */
10558       switch (token->keyword)
10559         {
10560           /* decl-specifier:
10561                friend
10562                constexpr */
10563         case RID_FRIEND:
10564           if (!at_class_scope_p ())
10565             {
10566               error_at (token->location, "%<friend%> used outside of class");
10567               cp_lexer_purge_token (parser->lexer);
10568             }
10569           else
10570             {
10571               ++decl_specs->specs[(int) ds_friend];
10572               /* Consume the token.  */
10573               cp_lexer_consume_token (parser->lexer);
10574             }
10575           break;
10576
10577         case RID_CONSTEXPR:
10578           ++decl_specs->specs[(int) ds_constexpr];
10579           cp_lexer_consume_token (parser->lexer);
10580           break;
10581
10582           /* function-specifier:
10583                inline
10584                virtual
10585                explicit  */
10586         case RID_INLINE:
10587         case RID_VIRTUAL:
10588         case RID_EXPLICIT:
10589           cp_parser_function_specifier_opt (parser, decl_specs);
10590           break;
10591
10592           /* decl-specifier:
10593                typedef  */
10594         case RID_TYPEDEF:
10595           ++decl_specs->specs[(int) ds_typedef];
10596           /* Consume the token.  */
10597           cp_lexer_consume_token (parser->lexer);
10598           /* A constructor declarator cannot appear in a typedef.  */
10599           constructor_possible_p = false;
10600           /* The "typedef" keyword can only occur in a declaration; we
10601              may as well commit at this point.  */
10602           cp_parser_commit_to_tentative_parse (parser);
10603
10604           if (decl_specs->storage_class != sc_none)
10605             decl_specs->conflicting_specifiers_p = true;
10606           break;
10607
10608           /* storage-class-specifier:
10609                auto
10610                register
10611                static
10612                extern
10613                mutable
10614
10615              GNU Extension:
10616                thread  */
10617         case RID_AUTO:
10618           if (cxx_dialect == cxx98) 
10619             {
10620               /* Consume the token.  */
10621               cp_lexer_consume_token (parser->lexer);
10622
10623               /* Complain about `auto' as a storage specifier, if
10624                  we're complaining about C++0x compatibility.  */
10625               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10626                           " changes meaning in C++11; please remove it");
10627
10628               /* Set the storage class anyway.  */
10629               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10630                                            token->location);
10631             }
10632           else
10633             /* C++0x auto type-specifier.  */
10634             found_decl_spec = false;
10635           break;
10636
10637         case RID_REGISTER:
10638         case RID_STATIC:
10639         case RID_EXTERN:
10640         case RID_MUTABLE:
10641           /* Consume the token.  */
10642           cp_lexer_consume_token (parser->lexer);
10643           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10644                                        token->location);
10645           break;
10646         case RID_THREAD:
10647           /* Consume the token.  */
10648           cp_lexer_consume_token (parser->lexer);
10649           ++decl_specs->specs[(int) ds_thread];
10650           break;
10651
10652         default:
10653           /* We did not yet find a decl-specifier yet.  */
10654           found_decl_spec = false;
10655           break;
10656         }
10657
10658       if (found_decl_spec
10659           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10660           && token->keyword != RID_CONSTEXPR)
10661         error ("decl-specifier invalid in condition");
10662
10663       /* Constructors are a special case.  The `S' in `S()' is not a
10664          decl-specifier; it is the beginning of the declarator.  */
10665       constructor_p
10666         = (!found_decl_spec
10667            && constructor_possible_p
10668            && (cp_parser_constructor_declarator_p
10669                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10670
10671       /* If we don't have a DECL_SPEC yet, then we must be looking at
10672          a type-specifier.  */
10673       if (!found_decl_spec && !constructor_p)
10674         {
10675           int decl_spec_declares_class_or_enum;
10676           bool is_cv_qualifier;
10677           tree type_spec;
10678
10679           type_spec
10680             = cp_parser_type_specifier (parser, flags,
10681                                         decl_specs,
10682                                         /*is_declaration=*/true,
10683                                         &decl_spec_declares_class_or_enum,
10684                                         &is_cv_qualifier);
10685           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10686
10687           /* If this type-specifier referenced a user-defined type
10688              (a typedef, class-name, etc.), then we can't allow any
10689              more such type-specifiers henceforth.
10690
10691              [dcl.spec]
10692
10693              The longest sequence of decl-specifiers that could
10694              possibly be a type name is taken as the
10695              decl-specifier-seq of a declaration.  The sequence shall
10696              be self-consistent as described below.
10697
10698              [dcl.type]
10699
10700              As a general rule, at most one type-specifier is allowed
10701              in the complete decl-specifier-seq of a declaration.  The
10702              only exceptions are the following:
10703
10704              -- const or volatile can be combined with any other
10705                 type-specifier.
10706
10707              -- signed or unsigned can be combined with char, long,
10708                 short, or int.
10709
10710              -- ..
10711
10712              Example:
10713
10714                typedef char* Pc;
10715                void g (const int Pc);
10716
10717              Here, Pc is *not* part of the decl-specifier seq; it's
10718              the declarator.  Therefore, once we see a type-specifier
10719              (other than a cv-qualifier), we forbid any additional
10720              user-defined types.  We *do* still allow things like `int
10721              int' to be considered a decl-specifier-seq, and issue the
10722              error message later.  */
10723           if (type_spec && !is_cv_qualifier)
10724             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10725           /* A constructor declarator cannot follow a type-specifier.  */
10726           if (type_spec)
10727             {
10728               constructor_possible_p = false;
10729               found_decl_spec = true;
10730               if (!is_cv_qualifier)
10731                 decl_specs->any_type_specifiers_p = true;
10732             }
10733         }
10734
10735       /* If we still do not have a DECL_SPEC, then there are no more
10736          decl-specifiers.  */
10737       if (!found_decl_spec)
10738         break;
10739
10740       decl_specs->any_specifiers_p = true;
10741       /* After we see one decl-specifier, further decl-specifiers are
10742          always optional.  */
10743       flags |= CP_PARSER_FLAGS_OPTIONAL;
10744     }
10745
10746   cp_parser_check_decl_spec (decl_specs, start_token->location);
10747
10748   /* Don't allow a friend specifier with a class definition.  */
10749   if (decl_specs->specs[(int) ds_friend] != 0
10750       && (*declares_class_or_enum & 2))
10751     error_at (start_token->location,
10752               "class definition may not be declared a friend");
10753 }
10754
10755 /* Parse an (optional) storage-class-specifier.
10756
10757    storage-class-specifier:
10758      auto
10759      register
10760      static
10761      extern
10762      mutable
10763
10764    GNU Extension:
10765
10766    storage-class-specifier:
10767      thread
10768
10769    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10770
10771 static tree
10772 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10773 {
10774   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10775     {
10776     case RID_AUTO:
10777       if (cxx_dialect != cxx98)
10778         return NULL_TREE;
10779       /* Fall through for C++98.  */
10780
10781     case RID_REGISTER:
10782     case RID_STATIC:
10783     case RID_EXTERN:
10784     case RID_MUTABLE:
10785     case RID_THREAD:
10786       /* Consume the token.  */
10787       return cp_lexer_consume_token (parser->lexer)->u.value;
10788
10789     default:
10790       return NULL_TREE;
10791     }
10792 }
10793
10794 /* Parse an (optional) function-specifier.
10795
10796    function-specifier:
10797      inline
10798      virtual
10799      explicit
10800
10801    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10802    Updates DECL_SPECS, if it is non-NULL.  */
10803
10804 static tree
10805 cp_parser_function_specifier_opt (cp_parser* parser,
10806                                   cp_decl_specifier_seq *decl_specs)
10807 {
10808   cp_token *token = cp_lexer_peek_token (parser->lexer);
10809   switch (token->keyword)
10810     {
10811     case RID_INLINE:
10812       if (decl_specs)
10813         ++decl_specs->specs[(int) ds_inline];
10814       break;
10815
10816     case RID_VIRTUAL:
10817       /* 14.5.2.3 [temp.mem]
10818
10819          A member function template shall not be virtual.  */
10820       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10821         error_at (token->location, "templates may not be %<virtual%>");
10822       else if (decl_specs)
10823         ++decl_specs->specs[(int) ds_virtual];
10824       break;
10825
10826     case RID_EXPLICIT:
10827       if (decl_specs)
10828         ++decl_specs->specs[(int) ds_explicit];
10829       break;
10830
10831     default:
10832       return NULL_TREE;
10833     }
10834
10835   /* Consume the token.  */
10836   return cp_lexer_consume_token (parser->lexer)->u.value;
10837 }
10838
10839 /* Parse a linkage-specification.
10840
10841    linkage-specification:
10842      extern string-literal { declaration-seq [opt] }
10843      extern string-literal declaration  */
10844
10845 static void
10846 cp_parser_linkage_specification (cp_parser* parser)
10847 {
10848   tree linkage;
10849
10850   /* Look for the `extern' keyword.  */
10851   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10852
10853   /* Look for the string-literal.  */
10854   linkage = cp_parser_string_literal (parser, false, false);
10855
10856   /* Transform the literal into an identifier.  If the literal is a
10857      wide-character string, or contains embedded NULs, then we can't
10858      handle it as the user wants.  */
10859   if (strlen (TREE_STRING_POINTER (linkage))
10860       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10861     {
10862       cp_parser_error (parser, "invalid linkage-specification");
10863       /* Assume C++ linkage.  */
10864       linkage = lang_name_cplusplus;
10865     }
10866   else
10867     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10868
10869   /* We're now using the new linkage.  */
10870   push_lang_context (linkage);
10871
10872   /* If the next token is a `{', then we're using the first
10873      production.  */
10874   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10875     {
10876       /* Consume the `{' token.  */
10877       cp_lexer_consume_token (parser->lexer);
10878       /* Parse the declarations.  */
10879       cp_parser_declaration_seq_opt (parser);
10880       /* Look for the closing `}'.  */
10881       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10882     }
10883   /* Otherwise, there's just one declaration.  */
10884   else
10885     {
10886       bool saved_in_unbraced_linkage_specification_p;
10887
10888       saved_in_unbraced_linkage_specification_p
10889         = parser->in_unbraced_linkage_specification_p;
10890       parser->in_unbraced_linkage_specification_p = true;
10891       cp_parser_declaration (parser);
10892       parser->in_unbraced_linkage_specification_p
10893         = saved_in_unbraced_linkage_specification_p;
10894     }
10895
10896   /* We're done with the linkage-specification.  */
10897   pop_lang_context ();
10898 }
10899
10900 /* Parse a static_assert-declaration.
10901
10902    static_assert-declaration:
10903      static_assert ( constant-expression , string-literal ) ; 
10904
10905    If MEMBER_P, this static_assert is a class member.  */
10906
10907 static void 
10908 cp_parser_static_assert(cp_parser *parser, bool member_p)
10909 {
10910   tree condition;
10911   tree message;
10912   cp_token *token;
10913   location_t saved_loc;
10914   bool dummy;
10915
10916   /* Peek at the `static_assert' token so we can keep track of exactly
10917      where the static assertion started.  */
10918   token = cp_lexer_peek_token (parser->lexer);
10919   saved_loc = token->location;
10920
10921   /* Look for the `static_assert' keyword.  */
10922   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10923                                   RT_STATIC_ASSERT))
10924     return;
10925
10926   /*  We know we are in a static assertion; commit to any tentative
10927       parse.  */
10928   if (cp_parser_parsing_tentatively (parser))
10929     cp_parser_commit_to_tentative_parse (parser);
10930
10931   /* Parse the `(' starting the static assertion condition.  */
10932   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10933
10934   /* Parse the constant-expression.  Allow a non-constant expression
10935      here in order to give better diagnostics in finish_static_assert.  */
10936   condition = 
10937     cp_parser_constant_expression (parser,
10938                                    /*allow_non_constant_p=*/true,
10939                                    /*non_constant_p=*/&dummy);
10940
10941   /* Parse the separating `,'.  */
10942   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10943
10944   /* Parse the string-literal message.  */
10945   message = cp_parser_string_literal (parser, 
10946                                       /*translate=*/false,
10947                                       /*wide_ok=*/true);
10948
10949   /* A `)' completes the static assertion.  */
10950   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10951     cp_parser_skip_to_closing_parenthesis (parser, 
10952                                            /*recovering=*/true, 
10953                                            /*or_comma=*/false,
10954                                            /*consume_paren=*/true);
10955
10956   /* A semicolon terminates the declaration.  */
10957   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10958
10959   /* Complete the static assertion, which may mean either processing 
10960      the static assert now or saving it for template instantiation.  */
10961   finish_static_assert (condition, message, saved_loc, member_p);
10962 }
10963
10964 /* Parse a `decltype' type. Returns the type. 
10965
10966    simple-type-specifier:
10967      decltype ( expression )  */
10968
10969 static tree
10970 cp_parser_decltype (cp_parser *parser)
10971 {
10972   tree expr;
10973   bool id_expression_or_member_access_p = false;
10974   const char *saved_message;
10975   bool saved_integral_constant_expression_p;
10976   bool saved_non_integral_constant_expression_p;
10977   cp_token *id_expr_start_token;
10978   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10979
10980   if (start_token->type == CPP_DECLTYPE)
10981     {
10982       /* Already parsed.  */
10983       cp_lexer_consume_token (parser->lexer);
10984       return start_token->u.value;
10985     }
10986
10987   /* Look for the `decltype' token.  */
10988   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10989     return error_mark_node;
10990
10991   /* Types cannot be defined in a `decltype' expression.  Save away the
10992      old message.  */
10993   saved_message = parser->type_definition_forbidden_message;
10994
10995   /* And create the new one.  */
10996   parser->type_definition_forbidden_message
10997     = G_("types may not be defined in %<decltype%> expressions");
10998
10999   /* The restrictions on constant-expressions do not apply inside
11000      decltype expressions.  */
11001   saved_integral_constant_expression_p
11002     = parser->integral_constant_expression_p;
11003   saved_non_integral_constant_expression_p
11004     = parser->non_integral_constant_expression_p;
11005   parser->integral_constant_expression_p = false;
11006
11007   /* Do not actually evaluate the expression.  */
11008   ++cp_unevaluated_operand;
11009
11010   /* Do not warn about problems with the expression.  */
11011   ++c_inhibit_evaluation_warnings;
11012
11013   /* Parse the opening `('.  */
11014   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11015     return error_mark_node;
11016   
11017   /* First, try parsing an id-expression.  */
11018   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11019   cp_parser_parse_tentatively (parser);
11020   expr = cp_parser_id_expression (parser,
11021                                   /*template_keyword_p=*/false,
11022                                   /*check_dependency_p=*/true,
11023                                   /*template_p=*/NULL,
11024                                   /*declarator_p=*/false,
11025                                   /*optional_p=*/false);
11026
11027   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11028     {
11029       bool non_integral_constant_expression_p = false;
11030       tree id_expression = expr;
11031       cp_id_kind idk;
11032       const char *error_msg;
11033
11034       if (TREE_CODE (expr) == IDENTIFIER_NODE)
11035         /* Lookup the name we got back from the id-expression.  */
11036         expr = cp_parser_lookup_name (parser, expr,
11037                                       none_type,
11038                                       /*is_template=*/false,
11039                                       /*is_namespace=*/false,
11040                                       /*check_dependency=*/true,
11041                                       /*ambiguous_decls=*/NULL,
11042                                       id_expr_start_token->location);
11043
11044       if (expr
11045           && expr != error_mark_node
11046           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11047           && TREE_CODE (expr) != TYPE_DECL
11048           && (TREE_CODE (expr) != BIT_NOT_EXPR
11049               || !TYPE_P (TREE_OPERAND (expr, 0)))
11050           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11051         {
11052           /* Complete lookup of the id-expression.  */
11053           expr = (finish_id_expression
11054                   (id_expression, expr, parser->scope, &idk,
11055                    /*integral_constant_expression_p=*/false,
11056                    /*allow_non_integral_constant_expression_p=*/true,
11057                    &non_integral_constant_expression_p,
11058                    /*template_p=*/false,
11059                    /*done=*/true,
11060                    /*address_p=*/false,
11061                    /*template_arg_p=*/false,
11062                    &error_msg,
11063                    id_expr_start_token->location));
11064
11065           if (expr == error_mark_node)
11066             /* We found an id-expression, but it was something that we
11067                should not have found. This is an error, not something
11068                we can recover from, so note that we found an
11069                id-expression and we'll recover as gracefully as
11070                possible.  */
11071             id_expression_or_member_access_p = true;
11072         }
11073
11074       if (expr 
11075           && expr != error_mark_node
11076           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11077         /* We have an id-expression.  */
11078         id_expression_or_member_access_p = true;
11079     }
11080
11081   if (!id_expression_or_member_access_p)
11082     {
11083       /* Abort the id-expression parse.  */
11084       cp_parser_abort_tentative_parse (parser);
11085
11086       /* Parsing tentatively, again.  */
11087       cp_parser_parse_tentatively (parser);
11088
11089       /* Parse a class member access.  */
11090       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11091                                            /*cast_p=*/false,
11092                                            /*member_access_only_p=*/true, NULL);
11093
11094       if (expr 
11095           && expr != error_mark_node
11096           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11097         /* We have an id-expression.  */
11098         id_expression_or_member_access_p = true;
11099     }
11100
11101   if (id_expression_or_member_access_p)
11102     /* We have parsed the complete id-expression or member access.  */
11103     cp_parser_parse_definitely (parser);
11104   else
11105     {
11106       bool saved_greater_than_is_operator_p;
11107
11108       /* Abort our attempt to parse an id-expression or member access
11109          expression.  */
11110       cp_parser_abort_tentative_parse (parser);
11111
11112       /* Within a parenthesized expression, a `>' token is always
11113          the greater-than operator.  */
11114       saved_greater_than_is_operator_p
11115         = parser->greater_than_is_operator_p;
11116       parser->greater_than_is_operator_p = true;
11117
11118       /* Parse a full expression.  */
11119       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11120
11121       /* The `>' token might be the end of a template-id or
11122          template-parameter-list now.  */
11123       parser->greater_than_is_operator_p
11124         = saved_greater_than_is_operator_p;
11125     }
11126
11127   /* Go back to evaluating expressions.  */
11128   --cp_unevaluated_operand;
11129   --c_inhibit_evaluation_warnings;
11130
11131   /* Restore the old message and the integral constant expression
11132      flags.  */
11133   parser->type_definition_forbidden_message = saved_message;
11134   parser->integral_constant_expression_p
11135     = saved_integral_constant_expression_p;
11136   parser->non_integral_constant_expression_p
11137     = saved_non_integral_constant_expression_p;
11138
11139   /* Parse to the closing `)'.  */
11140   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11141     {
11142       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11143                                              /*consume_paren=*/true);
11144       return error_mark_node;
11145     }
11146
11147   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11148                                tf_warning_or_error);
11149
11150   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11151      it again.  */
11152   start_token->type = CPP_DECLTYPE;
11153   start_token->u.value = expr;
11154   start_token->keyword = RID_MAX;
11155   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11156
11157   return expr;
11158 }
11159
11160 /* Special member functions [gram.special] */
11161
11162 /* Parse a conversion-function-id.
11163
11164    conversion-function-id:
11165      operator conversion-type-id
11166
11167    Returns an IDENTIFIER_NODE representing the operator.  */
11168
11169 static tree
11170 cp_parser_conversion_function_id (cp_parser* parser)
11171 {
11172   tree type;
11173   tree saved_scope;
11174   tree saved_qualifying_scope;
11175   tree saved_object_scope;
11176   tree pushed_scope = NULL_TREE;
11177
11178   /* Look for the `operator' token.  */
11179   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11180     return error_mark_node;
11181   /* When we parse the conversion-type-id, the current scope will be
11182      reset.  However, we need that information in able to look up the
11183      conversion function later, so we save it here.  */
11184   saved_scope = parser->scope;
11185   saved_qualifying_scope = parser->qualifying_scope;
11186   saved_object_scope = parser->object_scope;
11187   /* We must enter the scope of the class so that the names of
11188      entities declared within the class are available in the
11189      conversion-type-id.  For example, consider:
11190
11191        struct S {
11192          typedef int I;
11193          operator I();
11194        };
11195
11196        S::operator I() { ... }
11197
11198      In order to see that `I' is a type-name in the definition, we
11199      must be in the scope of `S'.  */
11200   if (saved_scope)
11201     pushed_scope = push_scope (saved_scope);
11202   /* Parse the conversion-type-id.  */
11203   type = cp_parser_conversion_type_id (parser);
11204   /* Leave the scope of the class, if any.  */
11205   if (pushed_scope)
11206     pop_scope (pushed_scope);
11207   /* Restore the saved scope.  */
11208   parser->scope = saved_scope;
11209   parser->qualifying_scope = saved_qualifying_scope;
11210   parser->object_scope = saved_object_scope;
11211   /* If the TYPE is invalid, indicate failure.  */
11212   if (type == error_mark_node)
11213     return error_mark_node;
11214   return mangle_conv_op_name_for_type (type);
11215 }
11216
11217 /* Parse a conversion-type-id:
11218
11219    conversion-type-id:
11220      type-specifier-seq conversion-declarator [opt]
11221
11222    Returns the TYPE specified.  */
11223
11224 static tree
11225 cp_parser_conversion_type_id (cp_parser* parser)
11226 {
11227   tree attributes;
11228   cp_decl_specifier_seq type_specifiers;
11229   cp_declarator *declarator;
11230   tree type_specified;
11231
11232   /* Parse the attributes.  */
11233   attributes = cp_parser_attributes_opt (parser);
11234   /* Parse the type-specifiers.  */
11235   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11236                                 /*is_trailing_return=*/false,
11237                                 &type_specifiers);
11238   /* If that didn't work, stop.  */
11239   if (type_specifiers.type == error_mark_node)
11240     return error_mark_node;
11241   /* Parse the conversion-declarator.  */
11242   declarator = cp_parser_conversion_declarator_opt (parser);
11243
11244   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11245                                     /*initialized=*/0, &attributes);
11246   if (attributes)
11247     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11248
11249   /* Don't give this error when parsing tentatively.  This happens to
11250      work because we always parse this definitively once.  */
11251   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11252       && type_uses_auto (type_specified))
11253     {
11254       error ("invalid use of %<auto%> in conversion operator");
11255       return error_mark_node;
11256     }
11257
11258   return type_specified;
11259 }
11260
11261 /* Parse an (optional) conversion-declarator.
11262
11263    conversion-declarator:
11264      ptr-operator conversion-declarator [opt]
11265
11266    */
11267
11268 static cp_declarator *
11269 cp_parser_conversion_declarator_opt (cp_parser* parser)
11270 {
11271   enum tree_code code;
11272   tree class_type;
11273   cp_cv_quals cv_quals;
11274
11275   /* We don't know if there's a ptr-operator next, or not.  */
11276   cp_parser_parse_tentatively (parser);
11277   /* Try the ptr-operator.  */
11278   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11279   /* If it worked, look for more conversion-declarators.  */
11280   if (cp_parser_parse_definitely (parser))
11281     {
11282       cp_declarator *declarator;
11283
11284       /* Parse another optional declarator.  */
11285       declarator = cp_parser_conversion_declarator_opt (parser);
11286
11287       return cp_parser_make_indirect_declarator
11288         (code, class_type, cv_quals, declarator);
11289    }
11290
11291   return NULL;
11292 }
11293
11294 /* Parse an (optional) ctor-initializer.
11295
11296    ctor-initializer:
11297      : mem-initializer-list
11298
11299    Returns TRUE iff the ctor-initializer was actually present.  */
11300
11301 static bool
11302 cp_parser_ctor_initializer_opt (cp_parser* parser)
11303 {
11304   /* If the next token is not a `:', then there is no
11305      ctor-initializer.  */
11306   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11307     {
11308       /* Do default initialization of any bases and members.  */
11309       if (DECL_CONSTRUCTOR_P (current_function_decl))
11310         finish_mem_initializers (NULL_TREE);
11311
11312       return false;
11313     }
11314
11315   /* Consume the `:' token.  */
11316   cp_lexer_consume_token (parser->lexer);
11317   /* And the mem-initializer-list.  */
11318   cp_parser_mem_initializer_list (parser);
11319
11320   return true;
11321 }
11322
11323 /* Parse a mem-initializer-list.
11324
11325    mem-initializer-list:
11326      mem-initializer ... [opt]
11327      mem-initializer ... [opt] , mem-initializer-list  */
11328
11329 static void
11330 cp_parser_mem_initializer_list (cp_parser* parser)
11331 {
11332   tree mem_initializer_list = NULL_TREE;
11333   tree target_ctor = error_mark_node;
11334   cp_token *token = cp_lexer_peek_token (parser->lexer);
11335
11336   /* Let the semantic analysis code know that we are starting the
11337      mem-initializer-list.  */
11338   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11339     error_at (token->location,
11340               "only constructors take member initializers");
11341
11342   /* Loop through the list.  */
11343   while (true)
11344     {
11345       tree mem_initializer;
11346
11347       token = cp_lexer_peek_token (parser->lexer);
11348       /* Parse the mem-initializer.  */
11349       mem_initializer = cp_parser_mem_initializer (parser);
11350       /* If the next token is a `...', we're expanding member initializers. */
11351       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11352         {
11353           /* Consume the `...'. */
11354           cp_lexer_consume_token (parser->lexer);
11355
11356           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11357              can be expanded but members cannot. */
11358           if (mem_initializer != error_mark_node
11359               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11360             {
11361               error_at (token->location,
11362                         "cannot expand initializer for member %<%D%>",
11363                         TREE_PURPOSE (mem_initializer));
11364               mem_initializer = error_mark_node;
11365             }
11366
11367           /* Construct the pack expansion type. */
11368           if (mem_initializer != error_mark_node)
11369             mem_initializer = make_pack_expansion (mem_initializer);
11370         }
11371       if (target_ctor != error_mark_node
11372           && mem_initializer != error_mark_node)
11373         {
11374           error ("mem-initializer for %qD follows constructor delegation",
11375                  TREE_PURPOSE (mem_initializer));
11376           mem_initializer = error_mark_node;
11377         }
11378       /* Look for a target constructor. */
11379       if (mem_initializer != error_mark_node
11380           && TYPE_P (TREE_PURPOSE (mem_initializer))
11381           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11382         {
11383           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11384           if (mem_initializer_list)
11385             {
11386               error ("constructor delegation follows mem-initializer for %qD",
11387                      TREE_PURPOSE (mem_initializer_list));
11388               mem_initializer = error_mark_node;
11389             }
11390           target_ctor = mem_initializer;
11391         }
11392       /* Add it to the list, unless it was erroneous.  */
11393       if (mem_initializer != error_mark_node)
11394         {
11395           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11396           mem_initializer_list = mem_initializer;
11397         }
11398       /* If the next token is not a `,', we're done.  */
11399       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11400         break;
11401       /* Consume the `,' token.  */
11402       cp_lexer_consume_token (parser->lexer);
11403     }
11404
11405   /* Perform semantic analysis.  */
11406   if (DECL_CONSTRUCTOR_P (current_function_decl))
11407     finish_mem_initializers (mem_initializer_list);
11408 }
11409
11410 /* Parse a mem-initializer.
11411
11412    mem-initializer:
11413      mem-initializer-id ( expression-list [opt] )
11414      mem-initializer-id braced-init-list
11415
11416    GNU extension:
11417
11418    mem-initializer:
11419      ( expression-list [opt] )
11420
11421    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11422    class) or FIELD_DECL (for a non-static data member) to initialize;
11423    the TREE_VALUE is the expression-list.  An empty initialization
11424    list is represented by void_list_node.  */
11425
11426 static tree
11427 cp_parser_mem_initializer (cp_parser* parser)
11428 {
11429   tree mem_initializer_id;
11430   tree expression_list;
11431   tree member;
11432   cp_token *token = cp_lexer_peek_token (parser->lexer);
11433
11434   /* Find out what is being initialized.  */
11435   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11436     {
11437       permerror (token->location,
11438                  "anachronistic old-style base class initializer");
11439       mem_initializer_id = NULL_TREE;
11440     }
11441   else
11442     {
11443       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11444       if (mem_initializer_id == error_mark_node)
11445         return mem_initializer_id;
11446     }
11447   member = expand_member_init (mem_initializer_id);
11448   if (member && !DECL_P (member))
11449     in_base_initializer = 1;
11450
11451   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11452     {
11453       bool expr_non_constant_p;
11454       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11455       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11456       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11457       expression_list = build_tree_list (NULL_TREE, expression_list);
11458     }
11459   else
11460     {
11461       VEC(tree,gc)* vec;
11462       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11463                                                      /*cast_p=*/false,
11464                                                      /*allow_expansion_p=*/true,
11465                                                      /*non_constant_p=*/NULL);
11466       if (vec == NULL)
11467         return error_mark_node;
11468       expression_list = build_tree_list_vec (vec);
11469       release_tree_vector (vec);
11470     }
11471
11472   if (expression_list == error_mark_node)
11473     return error_mark_node;
11474   if (!expression_list)
11475     expression_list = void_type_node;
11476
11477   in_base_initializer = 0;
11478
11479   return member ? build_tree_list (member, expression_list) : error_mark_node;
11480 }
11481
11482 /* Parse a mem-initializer-id.
11483
11484    mem-initializer-id:
11485      :: [opt] nested-name-specifier [opt] class-name
11486      identifier
11487
11488    Returns a TYPE indicating the class to be initializer for the first
11489    production.  Returns an IDENTIFIER_NODE indicating the data member
11490    to be initialized for the second production.  */
11491
11492 static tree
11493 cp_parser_mem_initializer_id (cp_parser* parser)
11494 {
11495   bool global_scope_p;
11496   bool nested_name_specifier_p;
11497   bool template_p = false;
11498   tree id;
11499
11500   cp_token *token = cp_lexer_peek_token (parser->lexer);
11501
11502   /* `typename' is not allowed in this context ([temp.res]).  */
11503   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11504     {
11505       error_at (token->location, 
11506                 "keyword %<typename%> not allowed in this context (a qualified "
11507                 "member initializer is implicitly a type)");
11508       cp_lexer_consume_token (parser->lexer);
11509     }
11510   /* Look for the optional `::' operator.  */
11511   global_scope_p
11512     = (cp_parser_global_scope_opt (parser,
11513                                    /*current_scope_valid_p=*/false)
11514        != NULL_TREE);
11515   /* Look for the optional nested-name-specifier.  The simplest way to
11516      implement:
11517
11518        [temp.res]
11519
11520        The keyword `typename' is not permitted in a base-specifier or
11521        mem-initializer; in these contexts a qualified name that
11522        depends on a template-parameter is implicitly assumed to be a
11523        type name.
11524
11525      is to assume that we have seen the `typename' keyword at this
11526      point.  */
11527   nested_name_specifier_p
11528     = (cp_parser_nested_name_specifier_opt (parser,
11529                                             /*typename_keyword_p=*/true,
11530                                             /*check_dependency_p=*/true,
11531                                             /*type_p=*/true,
11532                                             /*is_declaration=*/true)
11533        != NULL_TREE);
11534   if (nested_name_specifier_p)
11535     template_p = cp_parser_optional_template_keyword (parser);
11536   /* If there is a `::' operator or a nested-name-specifier, then we
11537      are definitely looking for a class-name.  */
11538   if (global_scope_p || nested_name_specifier_p)
11539     return cp_parser_class_name (parser,
11540                                  /*typename_keyword_p=*/true,
11541                                  /*template_keyword_p=*/template_p,
11542                                  typename_type,
11543                                  /*check_dependency_p=*/true,
11544                                  /*class_head_p=*/false,
11545                                  /*is_declaration=*/true);
11546   /* Otherwise, we could also be looking for an ordinary identifier.  */
11547   cp_parser_parse_tentatively (parser);
11548   /* Try a class-name.  */
11549   id = cp_parser_class_name (parser,
11550                              /*typename_keyword_p=*/true,
11551                              /*template_keyword_p=*/false,
11552                              none_type,
11553                              /*check_dependency_p=*/true,
11554                              /*class_head_p=*/false,
11555                              /*is_declaration=*/true);
11556   /* If we found one, we're done.  */
11557   if (cp_parser_parse_definitely (parser))
11558     return id;
11559   /* Otherwise, look for an ordinary identifier.  */
11560   return cp_parser_identifier (parser);
11561 }
11562
11563 /* Overloading [gram.over] */
11564
11565 /* Parse an operator-function-id.
11566
11567    operator-function-id:
11568      operator operator
11569
11570    Returns an IDENTIFIER_NODE for the operator which is a
11571    human-readable spelling of the identifier, e.g., `operator +'.  */
11572
11573 static tree
11574 cp_parser_operator_function_id (cp_parser* parser)
11575 {
11576   /* Look for the `operator' keyword.  */
11577   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11578     return error_mark_node;
11579   /* And then the name of the operator itself.  */
11580   return cp_parser_operator (parser);
11581 }
11582
11583 /* Return an identifier node for a user-defined literal operator.
11584    The suffix identifier is chained to the operator name identifier.  */
11585
11586 static tree
11587 cp_literal_operator_id (const char* name)
11588 {
11589   tree identifier;
11590   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11591                               + strlen (name) + 10);
11592   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11593   identifier = get_identifier (buffer);
11594   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11595
11596   return identifier;
11597 }
11598
11599 /* Parse an operator.
11600
11601    operator:
11602      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11603      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11604      || ++ -- , ->* -> () []
11605
11606    GNU Extensions:
11607
11608    operator:
11609      <? >? <?= >?=
11610
11611    Returns an IDENTIFIER_NODE for the operator which is a
11612    human-readable spelling of the identifier, e.g., `operator +'.  */
11613
11614 static tree
11615 cp_parser_operator (cp_parser* parser)
11616 {
11617   tree id = NULL_TREE;
11618   cp_token *token;
11619
11620   /* Peek at the next token.  */
11621   token = cp_lexer_peek_token (parser->lexer);
11622   /* Figure out which operator we have.  */
11623   switch (token->type)
11624     {
11625     case CPP_KEYWORD:
11626       {
11627         enum tree_code op;
11628
11629         /* The keyword should be either `new' or `delete'.  */
11630         if (token->keyword == RID_NEW)
11631           op = NEW_EXPR;
11632         else if (token->keyword == RID_DELETE)
11633           op = DELETE_EXPR;
11634         else
11635           break;
11636
11637         /* Consume the `new' or `delete' token.  */
11638         cp_lexer_consume_token (parser->lexer);
11639
11640         /* Peek at the next token.  */
11641         token = cp_lexer_peek_token (parser->lexer);
11642         /* If it's a `[' token then this is the array variant of the
11643            operator.  */
11644         if (token->type == CPP_OPEN_SQUARE)
11645           {
11646             /* Consume the `[' token.  */
11647             cp_lexer_consume_token (parser->lexer);
11648             /* Look for the `]' token.  */
11649             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11650             id = ansi_opname (op == NEW_EXPR
11651                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11652           }
11653         /* Otherwise, we have the non-array variant.  */
11654         else
11655           id = ansi_opname (op);
11656
11657         return id;
11658       }
11659
11660     case CPP_PLUS:
11661       id = ansi_opname (PLUS_EXPR);
11662       break;
11663
11664     case CPP_MINUS:
11665       id = ansi_opname (MINUS_EXPR);
11666       break;
11667
11668     case CPP_MULT:
11669       id = ansi_opname (MULT_EXPR);
11670       break;
11671
11672     case CPP_DIV:
11673       id = ansi_opname (TRUNC_DIV_EXPR);
11674       break;
11675
11676     case CPP_MOD:
11677       id = ansi_opname (TRUNC_MOD_EXPR);
11678       break;
11679
11680     case CPP_XOR:
11681       id = ansi_opname (BIT_XOR_EXPR);
11682       break;
11683
11684     case CPP_AND:
11685       id = ansi_opname (BIT_AND_EXPR);
11686       break;
11687
11688     case CPP_OR:
11689       id = ansi_opname (BIT_IOR_EXPR);
11690       break;
11691
11692     case CPP_COMPL:
11693       id = ansi_opname (BIT_NOT_EXPR);
11694       break;
11695
11696     case CPP_NOT:
11697       id = ansi_opname (TRUTH_NOT_EXPR);
11698       break;
11699
11700     case CPP_EQ:
11701       id = ansi_assopname (NOP_EXPR);
11702       break;
11703
11704     case CPP_LESS:
11705       id = ansi_opname (LT_EXPR);
11706       break;
11707
11708     case CPP_GREATER:
11709       id = ansi_opname (GT_EXPR);
11710       break;
11711
11712     case CPP_PLUS_EQ:
11713       id = ansi_assopname (PLUS_EXPR);
11714       break;
11715
11716     case CPP_MINUS_EQ:
11717       id = ansi_assopname (MINUS_EXPR);
11718       break;
11719
11720     case CPP_MULT_EQ:
11721       id = ansi_assopname (MULT_EXPR);
11722       break;
11723
11724     case CPP_DIV_EQ:
11725       id = ansi_assopname (TRUNC_DIV_EXPR);
11726       break;
11727
11728     case CPP_MOD_EQ:
11729       id = ansi_assopname (TRUNC_MOD_EXPR);
11730       break;
11731
11732     case CPP_XOR_EQ:
11733       id = ansi_assopname (BIT_XOR_EXPR);
11734       break;
11735
11736     case CPP_AND_EQ:
11737       id = ansi_assopname (BIT_AND_EXPR);
11738       break;
11739
11740     case CPP_OR_EQ:
11741       id = ansi_assopname (BIT_IOR_EXPR);
11742       break;
11743
11744     case CPP_LSHIFT:
11745       id = ansi_opname (LSHIFT_EXPR);
11746       break;
11747
11748     case CPP_RSHIFT:
11749       id = ansi_opname (RSHIFT_EXPR);
11750       break;
11751
11752     case CPP_LSHIFT_EQ:
11753       id = ansi_assopname (LSHIFT_EXPR);
11754       break;
11755
11756     case CPP_RSHIFT_EQ:
11757       id = ansi_assopname (RSHIFT_EXPR);
11758       break;
11759
11760     case CPP_EQ_EQ:
11761       id = ansi_opname (EQ_EXPR);
11762       break;
11763
11764     case CPP_NOT_EQ:
11765       id = ansi_opname (NE_EXPR);
11766       break;
11767
11768     case CPP_LESS_EQ:
11769       id = ansi_opname (LE_EXPR);
11770       break;
11771
11772     case CPP_GREATER_EQ:
11773       id = ansi_opname (GE_EXPR);
11774       break;
11775
11776     case CPP_AND_AND:
11777       id = ansi_opname (TRUTH_ANDIF_EXPR);
11778       break;
11779
11780     case CPP_OR_OR:
11781       id = ansi_opname (TRUTH_ORIF_EXPR);
11782       break;
11783
11784     case CPP_PLUS_PLUS:
11785       id = ansi_opname (POSTINCREMENT_EXPR);
11786       break;
11787
11788     case CPP_MINUS_MINUS:
11789       id = ansi_opname (PREDECREMENT_EXPR);
11790       break;
11791
11792     case CPP_COMMA:
11793       id = ansi_opname (COMPOUND_EXPR);
11794       break;
11795
11796     case CPP_DEREF_STAR:
11797       id = ansi_opname (MEMBER_REF);
11798       break;
11799
11800     case CPP_DEREF:
11801       id = ansi_opname (COMPONENT_REF);
11802       break;
11803
11804     case CPP_OPEN_PAREN:
11805       /* Consume the `('.  */
11806       cp_lexer_consume_token (parser->lexer);
11807       /* Look for the matching `)'.  */
11808       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11809       return ansi_opname (CALL_EXPR);
11810
11811     case CPP_OPEN_SQUARE:
11812       /* Consume the `['.  */
11813       cp_lexer_consume_token (parser->lexer);
11814       /* Look for the matching `]'.  */
11815       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11816       return ansi_opname (ARRAY_REF);
11817
11818     case CPP_STRING:
11819       if (cxx_dialect == cxx98)
11820         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11821       if (TREE_STRING_LENGTH (token->u.value) > 2)
11822         {
11823           error ("expected empty string after %<operator%> keyword");
11824           return error_mark_node;
11825         }
11826       /* Consume the string.  */
11827       cp_lexer_consume_token (parser->lexer);
11828       /* Look for the suffix identifier.  */
11829       token = cp_lexer_peek_token (parser->lexer);
11830       if (token->type == CPP_NAME)
11831         {
11832           id = cp_parser_identifier (parser);
11833           if (id != error_mark_node)
11834             {
11835               const char *name = IDENTIFIER_POINTER (id);
11836               return cp_literal_operator_id (name);
11837             }
11838         }
11839       else
11840         {
11841           error ("expected suffix identifier");
11842           return error_mark_node;
11843         }
11844
11845     case CPP_STRING_USERDEF:
11846       error ("missing space between %<\"\"%> and suffix identifier");
11847       return error_mark_node;
11848
11849     default:
11850       /* Anything else is an error.  */
11851       break;
11852     }
11853
11854   /* If we have selected an identifier, we need to consume the
11855      operator token.  */
11856   if (id)
11857     cp_lexer_consume_token (parser->lexer);
11858   /* Otherwise, no valid operator name was present.  */
11859   else
11860     {
11861       cp_parser_error (parser, "expected operator");
11862       id = error_mark_node;
11863     }
11864
11865   return id;
11866 }
11867
11868 /* Parse a template-declaration.
11869
11870    template-declaration:
11871      export [opt] template < template-parameter-list > declaration
11872
11873    If MEMBER_P is TRUE, this template-declaration occurs within a
11874    class-specifier.
11875
11876    The grammar rule given by the standard isn't correct.  What
11877    is really meant is:
11878
11879    template-declaration:
11880      export [opt] template-parameter-list-seq
11881        decl-specifier-seq [opt] init-declarator [opt] ;
11882      export [opt] template-parameter-list-seq
11883        function-definition
11884
11885    template-parameter-list-seq:
11886      template-parameter-list-seq [opt]
11887      template < template-parameter-list >  */
11888
11889 static void
11890 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11891 {
11892   /* Check for `export'.  */
11893   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11894     {
11895       /* Consume the `export' token.  */
11896       cp_lexer_consume_token (parser->lexer);
11897       /* Warn that we do not support `export'.  */
11898       warning (0, "keyword %<export%> not implemented, and will be ignored");
11899     }
11900
11901   cp_parser_template_declaration_after_export (parser, member_p);
11902 }
11903
11904 /* Parse a template-parameter-list.
11905
11906    template-parameter-list:
11907      template-parameter
11908      template-parameter-list , template-parameter
11909
11910    Returns a TREE_LIST.  Each node represents a template parameter.
11911    The nodes are connected via their TREE_CHAINs.  */
11912
11913 static tree
11914 cp_parser_template_parameter_list (cp_parser* parser)
11915 {
11916   tree parameter_list = NULL_TREE;
11917
11918   begin_template_parm_list ();
11919
11920   /* The loop below parses the template parms.  We first need to know
11921      the total number of template parms to be able to compute proper
11922      canonical types of each dependent type. So after the loop, when
11923      we know the total number of template parms,
11924      end_template_parm_list computes the proper canonical types and
11925      fixes up the dependent types accordingly.  */
11926   while (true)
11927     {
11928       tree parameter;
11929       bool is_non_type;
11930       bool is_parameter_pack;
11931       location_t parm_loc;
11932
11933       /* Parse the template-parameter.  */
11934       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11935       parameter = cp_parser_template_parameter (parser, 
11936                                                 &is_non_type,
11937                                                 &is_parameter_pack);
11938       /* Add it to the list.  */
11939       if (parameter != error_mark_node)
11940         parameter_list = process_template_parm (parameter_list,
11941                                                 parm_loc,
11942                                                 parameter,
11943                                                 is_non_type,
11944                                                 is_parameter_pack,
11945                                                 0);
11946       else
11947        {
11948          tree err_parm = build_tree_list (parameter, parameter);
11949          parameter_list = chainon (parameter_list, err_parm);
11950        }
11951
11952       /* If the next token is not a `,', we're done.  */
11953       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11954         break;
11955       /* Otherwise, consume the `,' token.  */
11956       cp_lexer_consume_token (parser->lexer);
11957     }
11958
11959   return end_template_parm_list (parameter_list);
11960 }
11961
11962 /* Parse a template-parameter.
11963
11964    template-parameter:
11965      type-parameter
11966      parameter-declaration
11967
11968    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11969    the parameter.  The TREE_PURPOSE is the default value, if any.
11970    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11971    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11972    set to true iff this parameter is a parameter pack. */
11973
11974 static tree
11975 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11976                               bool *is_parameter_pack)
11977 {
11978   cp_token *token;
11979   cp_parameter_declarator *parameter_declarator;
11980   cp_declarator *id_declarator;
11981   tree parm;
11982
11983   /* Assume it is a type parameter or a template parameter.  */
11984   *is_non_type = false;
11985   /* Assume it not a parameter pack. */
11986   *is_parameter_pack = false;
11987   /* Peek at the next token.  */
11988   token = cp_lexer_peek_token (parser->lexer);
11989   /* If it is `class' or `template', we have a type-parameter.  */
11990   if (token->keyword == RID_TEMPLATE)
11991     return cp_parser_type_parameter (parser, is_parameter_pack);
11992   /* If it is `class' or `typename' we do not know yet whether it is a
11993      type parameter or a non-type parameter.  Consider:
11994
11995        template <typename T, typename T::X X> ...
11996
11997      or:
11998
11999        template <class C, class D*> ...
12000
12001      Here, the first parameter is a type parameter, and the second is
12002      a non-type parameter.  We can tell by looking at the token after
12003      the identifier -- if it is a `,', `=', or `>' then we have a type
12004      parameter.  */
12005   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12006     {
12007       /* Peek at the token after `class' or `typename'.  */
12008       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12009       /* If it's an ellipsis, we have a template type parameter
12010          pack. */
12011       if (token->type == CPP_ELLIPSIS)
12012         return cp_parser_type_parameter (parser, is_parameter_pack);
12013       /* If it's an identifier, skip it.  */
12014       if (token->type == CPP_NAME)
12015         token = cp_lexer_peek_nth_token (parser->lexer, 3);
12016       /* Now, see if the token looks like the end of a template
12017          parameter.  */
12018       if (token->type == CPP_COMMA
12019           || token->type == CPP_EQ
12020           || token->type == CPP_GREATER)
12021         return cp_parser_type_parameter (parser, is_parameter_pack);
12022     }
12023
12024   /* Otherwise, it is a non-type parameter.
12025
12026      [temp.param]
12027
12028      When parsing a default template-argument for a non-type
12029      template-parameter, the first non-nested `>' is taken as the end
12030      of the template parameter-list rather than a greater-than
12031      operator.  */
12032   *is_non_type = true;
12033   parameter_declarator
12034      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12035                                         /*parenthesized_p=*/NULL);
12036
12037   /* If the parameter declaration is marked as a parameter pack, set
12038      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12039      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12040      grokdeclarator. */
12041   if (parameter_declarator
12042       && parameter_declarator->declarator
12043       && parameter_declarator->declarator->parameter_pack_p)
12044     {
12045       *is_parameter_pack = true;
12046       parameter_declarator->declarator->parameter_pack_p = false;
12047     }
12048
12049   /* If the next token is an ellipsis, and we don't already have it
12050      marked as a parameter pack, then we have a parameter pack (that
12051      has no declarator).  */
12052   if (!*is_parameter_pack
12053       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12054       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12055     {
12056       /* Consume the `...'.  */
12057       cp_lexer_consume_token (parser->lexer);
12058       maybe_warn_variadic_templates ();
12059       
12060       *is_parameter_pack = true;
12061     }
12062   /* We might end up with a pack expansion as the type of the non-type
12063      template parameter, in which case this is a non-type template
12064      parameter pack.  */
12065   else if (parameter_declarator
12066            && parameter_declarator->decl_specifiers.type
12067            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12068     {
12069       *is_parameter_pack = true;
12070       parameter_declarator->decl_specifiers.type = 
12071         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12072     }
12073
12074   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12075     {
12076       /* Parameter packs cannot have default arguments.  However, a
12077          user may try to do so, so we'll parse them and give an
12078          appropriate diagnostic here.  */
12079
12080       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12081       
12082       /* Find the name of the parameter pack.  */     
12083       id_declarator = parameter_declarator->declarator;
12084       while (id_declarator && id_declarator->kind != cdk_id)
12085         id_declarator = id_declarator->declarator;
12086       
12087       if (id_declarator && id_declarator->kind == cdk_id)
12088         error_at (start_token->location,
12089                   "template parameter pack %qD cannot have a default argument",
12090                   id_declarator->u.id.unqualified_name);
12091       else
12092         error_at (start_token->location,
12093                   "template parameter pack cannot have a default argument");
12094       
12095       /* Parse the default argument, but throw away the result.  */
12096       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12097     }
12098
12099   parm = grokdeclarator (parameter_declarator->declarator,
12100                          &parameter_declarator->decl_specifiers,
12101                          TPARM, /*initialized=*/0,
12102                          /*attrlist=*/NULL);
12103   if (parm == error_mark_node)
12104     return error_mark_node;
12105
12106   return build_tree_list (parameter_declarator->default_argument, parm);
12107 }
12108
12109 /* Parse a type-parameter.
12110
12111    type-parameter:
12112      class identifier [opt]
12113      class identifier [opt] = type-id
12114      typename identifier [opt]
12115      typename identifier [opt] = type-id
12116      template < template-parameter-list > class identifier [opt]
12117      template < template-parameter-list > class identifier [opt]
12118        = id-expression
12119
12120    GNU Extension (variadic templates):
12121
12122    type-parameter:
12123      class ... identifier [opt]
12124      typename ... identifier [opt]
12125
12126    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12127    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12128    the declaration of the parameter.
12129
12130    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12131
12132 static tree
12133 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12134 {
12135   cp_token *token;
12136   tree parameter;
12137
12138   /* Look for a keyword to tell us what kind of parameter this is.  */
12139   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12140   if (!token)
12141     return error_mark_node;
12142
12143   switch (token->keyword)
12144     {
12145     case RID_CLASS:
12146     case RID_TYPENAME:
12147       {
12148         tree identifier;
12149         tree default_argument;
12150
12151         /* If the next token is an ellipsis, we have a template
12152            argument pack. */
12153         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12154           {
12155             /* Consume the `...' token. */
12156             cp_lexer_consume_token (parser->lexer);
12157             maybe_warn_variadic_templates ();
12158
12159             *is_parameter_pack = true;
12160           }
12161
12162         /* If the next token is an identifier, then it names the
12163            parameter.  */
12164         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12165           identifier = cp_parser_identifier (parser);
12166         else
12167           identifier = NULL_TREE;
12168
12169         /* Create the parameter.  */
12170         parameter = finish_template_type_parm (class_type_node, identifier);
12171
12172         /* If the next token is an `=', we have a default argument.  */
12173         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12174           {
12175             /* Consume the `=' token.  */
12176             cp_lexer_consume_token (parser->lexer);
12177             /* Parse the default-argument.  */
12178             push_deferring_access_checks (dk_no_deferred);
12179             default_argument = cp_parser_type_id (parser);
12180
12181             /* Template parameter packs cannot have default
12182                arguments. */
12183             if (*is_parameter_pack)
12184               {
12185                 if (identifier)
12186                   error_at (token->location,
12187                             "template parameter pack %qD cannot have a "
12188                             "default argument", identifier);
12189                 else
12190                   error_at (token->location,
12191                             "template parameter packs cannot have "
12192                             "default arguments");
12193                 default_argument = NULL_TREE;
12194               }
12195             pop_deferring_access_checks ();
12196           }
12197         else
12198           default_argument = NULL_TREE;
12199
12200         /* Create the combined representation of the parameter and the
12201            default argument.  */
12202         parameter = build_tree_list (default_argument, parameter);
12203       }
12204       break;
12205
12206     case RID_TEMPLATE:
12207       {
12208         tree identifier;
12209         tree default_argument;
12210
12211         /* Look for the `<'.  */
12212         cp_parser_require (parser, CPP_LESS, RT_LESS);
12213         /* Parse the template-parameter-list.  */
12214         cp_parser_template_parameter_list (parser);
12215         /* Look for the `>'.  */
12216         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12217         /* Look for the `class' keyword.  */
12218         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12219         /* If the next token is an ellipsis, we have a template
12220            argument pack. */
12221         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12222           {
12223             /* Consume the `...' token. */
12224             cp_lexer_consume_token (parser->lexer);
12225             maybe_warn_variadic_templates ();
12226
12227             *is_parameter_pack = true;
12228           }
12229         /* If the next token is an `=', then there is a
12230            default-argument.  If the next token is a `>', we are at
12231            the end of the parameter-list.  If the next token is a `,',
12232            then we are at the end of this parameter.  */
12233         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12234             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12235             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12236           {
12237             identifier = cp_parser_identifier (parser);
12238             /* Treat invalid names as if the parameter were nameless.  */
12239             if (identifier == error_mark_node)
12240               identifier = NULL_TREE;
12241           }
12242         else
12243           identifier = NULL_TREE;
12244
12245         /* Create the template parameter.  */
12246         parameter = finish_template_template_parm (class_type_node,
12247                                                    identifier);
12248
12249         /* If the next token is an `=', then there is a
12250            default-argument.  */
12251         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12252           {
12253             bool is_template;
12254
12255             /* Consume the `='.  */
12256             cp_lexer_consume_token (parser->lexer);
12257             /* Parse the id-expression.  */
12258             push_deferring_access_checks (dk_no_deferred);
12259             /* save token before parsing the id-expression, for error
12260                reporting */
12261             token = cp_lexer_peek_token (parser->lexer);
12262             default_argument
12263               = cp_parser_id_expression (parser,
12264                                          /*template_keyword_p=*/false,
12265                                          /*check_dependency_p=*/true,
12266                                          /*template_p=*/&is_template,
12267                                          /*declarator_p=*/false,
12268                                          /*optional_p=*/false);
12269             if (TREE_CODE (default_argument) == TYPE_DECL)
12270               /* If the id-expression was a template-id that refers to
12271                  a template-class, we already have the declaration here,
12272                  so no further lookup is needed.  */
12273                  ;
12274             else
12275               /* Look up the name.  */
12276               default_argument
12277                 = cp_parser_lookup_name (parser, default_argument,
12278                                          none_type,
12279                                          /*is_template=*/is_template,
12280                                          /*is_namespace=*/false,
12281                                          /*check_dependency=*/true,
12282                                          /*ambiguous_decls=*/NULL,
12283                                          token->location);
12284             /* See if the default argument is valid.  */
12285             default_argument
12286               = check_template_template_default_arg (default_argument);
12287
12288             /* Template parameter packs cannot have default
12289                arguments. */
12290             if (*is_parameter_pack)
12291               {
12292                 if (identifier)
12293                   error_at (token->location,
12294                             "template parameter pack %qD cannot "
12295                             "have a default argument",
12296                             identifier);
12297                 else
12298                   error_at (token->location, "template parameter packs cannot "
12299                             "have default arguments");
12300                 default_argument = NULL_TREE;
12301               }
12302             pop_deferring_access_checks ();
12303           }
12304         else
12305           default_argument = NULL_TREE;
12306
12307         /* Create the combined representation of the parameter and the
12308            default argument.  */
12309         parameter = build_tree_list (default_argument, parameter);
12310       }
12311       break;
12312
12313     default:
12314       gcc_unreachable ();
12315       break;
12316     }
12317
12318   return parameter;
12319 }
12320
12321 /* Parse a template-id.
12322
12323    template-id:
12324      template-name < template-argument-list [opt] >
12325
12326    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12327    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12328    returned.  Otherwise, if the template-name names a function, or set
12329    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12330    names a class, returns a TYPE_DECL for the specialization.
12331
12332    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12333    uninstantiated templates.  */
12334
12335 static tree
12336 cp_parser_template_id (cp_parser *parser,
12337                        bool template_keyword_p,
12338                        bool check_dependency_p,
12339                        bool is_declaration)
12340 {
12341   int i;
12342   tree templ;
12343   tree arguments;
12344   tree template_id;
12345   cp_token_position start_of_id = 0;
12346   deferred_access_check *chk;
12347   VEC (deferred_access_check,gc) *access_check;
12348   cp_token *next_token = NULL, *next_token_2 = NULL;
12349   bool is_identifier;
12350
12351   /* If the next token corresponds to a template-id, there is no need
12352      to reparse it.  */
12353   next_token = cp_lexer_peek_token (parser->lexer);
12354   if (next_token->type == CPP_TEMPLATE_ID)
12355     {
12356       struct tree_check *check_value;
12357
12358       /* Get the stored value.  */
12359       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12360       /* Perform any access checks that were deferred.  */
12361       access_check = check_value->checks;
12362       if (access_check)
12363         {
12364           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12365             perform_or_defer_access_check (chk->binfo,
12366                                            chk->decl,
12367                                            chk->diag_decl);
12368         }
12369       /* Return the stored value.  */
12370       return check_value->value;
12371     }
12372
12373   /* Avoid performing name lookup if there is no possibility of
12374      finding a template-id.  */
12375   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12376       || (next_token->type == CPP_NAME
12377           && !cp_parser_nth_token_starts_template_argument_list_p
12378                (parser, 2)))
12379     {
12380       cp_parser_error (parser, "expected template-id");
12381       return error_mark_node;
12382     }
12383
12384   /* Remember where the template-id starts.  */
12385   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12386     start_of_id = cp_lexer_token_position (parser->lexer, false);
12387
12388   push_deferring_access_checks (dk_deferred);
12389
12390   /* Parse the template-name.  */
12391   is_identifier = false;
12392   templ = cp_parser_template_name (parser, template_keyword_p,
12393                                    check_dependency_p,
12394                                    is_declaration,
12395                                    &is_identifier);
12396   if (templ == error_mark_node || is_identifier)
12397     {
12398       pop_deferring_access_checks ();
12399       return templ;
12400     }
12401
12402   /* If we find the sequence `[:' after a template-name, it's probably
12403      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12404      parse correctly the argument list.  */
12405   next_token = cp_lexer_peek_token (parser->lexer);
12406   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12407   if (next_token->type == CPP_OPEN_SQUARE
12408       && next_token->flags & DIGRAPH
12409       && next_token_2->type == CPP_COLON
12410       && !(next_token_2->flags & PREV_WHITE))
12411     {
12412       cp_parser_parse_tentatively (parser);
12413       /* Change `:' into `::'.  */
12414       next_token_2->type = CPP_SCOPE;
12415       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12416          CPP_LESS.  */
12417       cp_lexer_consume_token (parser->lexer);
12418
12419       /* Parse the arguments.  */
12420       arguments = cp_parser_enclosed_template_argument_list (parser);
12421       if (!cp_parser_parse_definitely (parser))
12422         {
12423           /* If we couldn't parse an argument list, then we revert our changes
12424              and return simply an error. Maybe this is not a template-id
12425              after all.  */
12426           next_token_2->type = CPP_COLON;
12427           cp_parser_error (parser, "expected %<<%>");
12428           pop_deferring_access_checks ();
12429           return error_mark_node;
12430         }
12431       /* Otherwise, emit an error about the invalid digraph, but continue
12432          parsing because we got our argument list.  */
12433       if (permerror (next_token->location,
12434                      "%<<::%> cannot begin a template-argument list"))
12435         {
12436           static bool hint = false;
12437           inform (next_token->location,
12438                   "%<<:%> is an alternate spelling for %<[%>."
12439                   " Insert whitespace between %<<%> and %<::%>");
12440           if (!hint && !flag_permissive)
12441             {
12442               inform (next_token->location, "(if you use %<-fpermissive%>"
12443                       " G++ will accept your code)");
12444               hint = true;
12445             }
12446         }
12447     }
12448   else
12449     {
12450       /* Look for the `<' that starts the template-argument-list.  */
12451       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12452         {
12453           pop_deferring_access_checks ();
12454           return error_mark_node;
12455         }
12456       /* Parse the arguments.  */
12457       arguments = cp_parser_enclosed_template_argument_list (parser);
12458     }
12459
12460   /* Build a representation of the specialization.  */
12461   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12462     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12463   else if (DECL_TYPE_TEMPLATE_P (templ)
12464            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12465     {
12466       bool entering_scope;
12467       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12468          template (rather than some instantiation thereof) only if
12469          is not nested within some other construct.  For example, in
12470          "template <typename T> void f(T) { A<T>::", A<T> is just an
12471          instantiation of A.  */
12472       entering_scope = (template_parm_scope_p ()
12473                         && cp_lexer_next_token_is (parser->lexer,
12474                                                    CPP_SCOPE));
12475       template_id
12476         = finish_template_type (templ, arguments, entering_scope);
12477     }
12478   else
12479     {
12480       /* If it's not a class-template or a template-template, it should be
12481          a function-template.  */
12482       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12483                    || TREE_CODE (templ) == OVERLOAD
12484                    || BASELINK_P (templ)));
12485
12486       template_id = lookup_template_function (templ, arguments);
12487     }
12488
12489   /* If parsing tentatively, replace the sequence of tokens that makes
12490      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12491      should we re-parse the token stream, we will not have to repeat
12492      the effort required to do the parse, nor will we issue duplicate
12493      error messages about problems during instantiation of the
12494      template.  */
12495   if (start_of_id)
12496     {
12497       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12498
12499       /* Reset the contents of the START_OF_ID token.  */
12500       token->type = CPP_TEMPLATE_ID;
12501       /* Retrieve any deferred checks.  Do not pop this access checks yet
12502          so the memory will not be reclaimed during token replacing below.  */
12503       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12504       token->u.tree_check_value->value = template_id;
12505       token->u.tree_check_value->checks = get_deferred_access_checks ();
12506       token->keyword = RID_MAX;
12507
12508       /* Purge all subsequent tokens.  */
12509       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12510
12511       /* ??? Can we actually assume that, if template_id ==
12512          error_mark_node, we will have issued a diagnostic to the
12513          user, as opposed to simply marking the tentative parse as
12514          failed?  */
12515       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12516         error_at (token->location, "parse error in template argument list");
12517     }
12518
12519   pop_deferring_access_checks ();
12520   return template_id;
12521 }
12522
12523 /* Parse a template-name.
12524
12525    template-name:
12526      identifier
12527
12528    The standard should actually say:
12529
12530    template-name:
12531      identifier
12532      operator-function-id
12533
12534    A defect report has been filed about this issue.
12535
12536    A conversion-function-id cannot be a template name because they cannot
12537    be part of a template-id. In fact, looking at this code:
12538
12539    a.operator K<int>()
12540
12541    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12542    It is impossible to call a templated conversion-function-id with an
12543    explicit argument list, since the only allowed template parameter is
12544    the type to which it is converting.
12545
12546    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12547    `template' keyword, in a construction like:
12548
12549      T::template f<3>()
12550
12551    In that case `f' is taken to be a template-name, even though there
12552    is no way of knowing for sure.
12553
12554    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12555    name refers to a set of overloaded functions, at least one of which
12556    is a template, or an IDENTIFIER_NODE with the name of the template,
12557    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12558    names are looked up inside uninstantiated templates.  */
12559
12560 static tree
12561 cp_parser_template_name (cp_parser* parser,
12562                          bool template_keyword_p,
12563                          bool check_dependency_p,
12564                          bool is_declaration,
12565                          bool *is_identifier)
12566 {
12567   tree identifier;
12568   tree decl;
12569   tree fns;
12570   cp_token *token = cp_lexer_peek_token (parser->lexer);
12571
12572   /* If the next token is `operator', then we have either an
12573      operator-function-id or a conversion-function-id.  */
12574   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12575     {
12576       /* We don't know whether we're looking at an
12577          operator-function-id or a conversion-function-id.  */
12578       cp_parser_parse_tentatively (parser);
12579       /* Try an operator-function-id.  */
12580       identifier = cp_parser_operator_function_id (parser);
12581       /* If that didn't work, try a conversion-function-id.  */
12582       if (!cp_parser_parse_definitely (parser))
12583         {
12584           cp_parser_error (parser, "expected template-name");
12585           return error_mark_node;
12586         }
12587     }
12588   /* Look for the identifier.  */
12589   else
12590     identifier = cp_parser_identifier (parser);
12591
12592   /* If we didn't find an identifier, we don't have a template-id.  */
12593   if (identifier == error_mark_node)
12594     return error_mark_node;
12595
12596   /* If the name immediately followed the `template' keyword, then it
12597      is a template-name.  However, if the next token is not `<', then
12598      we do not treat it as a template-name, since it is not being used
12599      as part of a template-id.  This enables us to handle constructs
12600      like:
12601
12602        template <typename T> struct S { S(); };
12603        template <typename T> S<T>::S();
12604
12605      correctly.  We would treat `S' as a template -- if it were `S<T>'
12606      -- but we do not if there is no `<'.  */
12607
12608   if (processing_template_decl
12609       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12610     {
12611       /* In a declaration, in a dependent context, we pretend that the
12612          "template" keyword was present in order to improve error
12613          recovery.  For example, given:
12614
12615            template <typename T> void f(T::X<int>);
12616
12617          we want to treat "X<int>" as a template-id.  */
12618       if (is_declaration
12619           && !template_keyword_p
12620           && parser->scope && TYPE_P (parser->scope)
12621           && check_dependency_p
12622           && dependent_scope_p (parser->scope)
12623           /* Do not do this for dtors (or ctors), since they never
12624              need the template keyword before their name.  */
12625           && !constructor_name_p (identifier, parser->scope))
12626         {
12627           cp_token_position start = 0;
12628
12629           /* Explain what went wrong.  */
12630           error_at (token->location, "non-template %qD used as template",
12631                     identifier);
12632           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12633                   parser->scope, identifier);
12634           /* If parsing tentatively, find the location of the "<" token.  */
12635           if (cp_parser_simulate_error (parser))
12636             start = cp_lexer_token_position (parser->lexer, true);
12637           /* Parse the template arguments so that we can issue error
12638              messages about them.  */
12639           cp_lexer_consume_token (parser->lexer);
12640           cp_parser_enclosed_template_argument_list (parser);
12641           /* Skip tokens until we find a good place from which to
12642              continue parsing.  */
12643           cp_parser_skip_to_closing_parenthesis (parser,
12644                                                  /*recovering=*/true,
12645                                                  /*or_comma=*/true,
12646                                                  /*consume_paren=*/false);
12647           /* If parsing tentatively, permanently remove the
12648              template argument list.  That will prevent duplicate
12649              error messages from being issued about the missing
12650              "template" keyword.  */
12651           if (start)
12652             cp_lexer_purge_tokens_after (parser->lexer, start);
12653           if (is_identifier)
12654             *is_identifier = true;
12655           return identifier;
12656         }
12657
12658       /* If the "template" keyword is present, then there is generally
12659          no point in doing name-lookup, so we just return IDENTIFIER.
12660          But, if the qualifying scope is non-dependent then we can
12661          (and must) do name-lookup normally.  */
12662       if (template_keyword_p
12663           && (!parser->scope
12664               || (TYPE_P (parser->scope)
12665                   && dependent_type_p (parser->scope))))
12666         return identifier;
12667     }
12668
12669   /* Look up the name.  */
12670   decl = cp_parser_lookup_name (parser, identifier,
12671                                 none_type,
12672                                 /*is_template=*/true,
12673                                 /*is_namespace=*/false,
12674                                 check_dependency_p,
12675                                 /*ambiguous_decls=*/NULL,
12676                                 token->location);
12677
12678   /* If DECL is a template, then the name was a template-name.  */
12679   if (TREE_CODE (decl) == TEMPLATE_DECL)
12680     ;
12681   else
12682     {
12683       tree fn = NULL_TREE;
12684
12685       /* The standard does not explicitly indicate whether a name that
12686          names a set of overloaded declarations, some of which are
12687          templates, is a template-name.  However, such a name should
12688          be a template-name; otherwise, there is no way to form a
12689          template-id for the overloaded templates.  */
12690       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12691       if (TREE_CODE (fns) == OVERLOAD)
12692         for (fn = fns; fn; fn = OVL_NEXT (fn))
12693           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12694             break;
12695
12696       if (!fn)
12697         {
12698           /* The name does not name a template.  */
12699           cp_parser_error (parser, "expected template-name");
12700           return error_mark_node;
12701         }
12702     }
12703
12704   /* If DECL is dependent, and refers to a function, then just return
12705      its name; we will look it up again during template instantiation.  */
12706   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12707     {
12708       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12709       if (TYPE_P (scope) && dependent_type_p (scope))
12710         return identifier;
12711     }
12712
12713   return decl;
12714 }
12715
12716 /* Parse a template-argument-list.
12717
12718    template-argument-list:
12719      template-argument ... [opt]
12720      template-argument-list , template-argument ... [opt]
12721
12722    Returns a TREE_VEC containing the arguments.  */
12723
12724 static tree
12725 cp_parser_template_argument_list (cp_parser* parser)
12726 {
12727   tree fixed_args[10];
12728   unsigned n_args = 0;
12729   unsigned alloced = 10;
12730   tree *arg_ary = fixed_args;
12731   tree vec;
12732   bool saved_in_template_argument_list_p;
12733   bool saved_ice_p;
12734   bool saved_non_ice_p;
12735
12736   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12737   parser->in_template_argument_list_p = true;
12738   /* Even if the template-id appears in an integral
12739      constant-expression, the contents of the argument list do
12740      not.  */
12741   saved_ice_p = parser->integral_constant_expression_p;
12742   parser->integral_constant_expression_p = false;
12743   saved_non_ice_p = parser->non_integral_constant_expression_p;
12744   parser->non_integral_constant_expression_p = false;
12745
12746   /* Parse the arguments.  */
12747   do
12748     {
12749       tree argument;
12750
12751       if (n_args)
12752         /* Consume the comma.  */
12753         cp_lexer_consume_token (parser->lexer);
12754
12755       /* Parse the template-argument.  */
12756       argument = cp_parser_template_argument (parser);
12757
12758       /* If the next token is an ellipsis, we're expanding a template
12759          argument pack. */
12760       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12761         {
12762           if (argument == error_mark_node)
12763             {
12764               cp_token *token = cp_lexer_peek_token (parser->lexer);
12765               error_at (token->location,
12766                         "expected parameter pack before %<...%>");
12767             }
12768           /* Consume the `...' token. */
12769           cp_lexer_consume_token (parser->lexer);
12770
12771           /* Make the argument into a TYPE_PACK_EXPANSION or
12772              EXPR_PACK_EXPANSION. */
12773           argument = make_pack_expansion (argument);
12774         }
12775
12776       if (n_args == alloced)
12777         {
12778           alloced *= 2;
12779
12780           if (arg_ary == fixed_args)
12781             {
12782               arg_ary = XNEWVEC (tree, alloced);
12783               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12784             }
12785           else
12786             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12787         }
12788       arg_ary[n_args++] = argument;
12789     }
12790   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12791
12792   vec = make_tree_vec (n_args);
12793
12794   while (n_args--)
12795     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12796
12797   if (arg_ary != fixed_args)
12798     free (arg_ary);
12799   parser->non_integral_constant_expression_p = saved_non_ice_p;
12800   parser->integral_constant_expression_p = saved_ice_p;
12801   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12802 #ifdef ENABLE_CHECKING
12803   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12804 #endif
12805   return vec;
12806 }
12807
12808 /* Parse a template-argument.
12809
12810    template-argument:
12811      assignment-expression
12812      type-id
12813      id-expression
12814
12815    The representation is that of an assignment-expression, type-id, or
12816    id-expression -- except that the qualified id-expression is
12817    evaluated, so that the value returned is either a DECL or an
12818    OVERLOAD.
12819
12820    Although the standard says "assignment-expression", it forbids
12821    throw-expressions or assignments in the template argument.
12822    Therefore, we use "conditional-expression" instead.  */
12823
12824 static tree
12825 cp_parser_template_argument (cp_parser* parser)
12826 {
12827   tree argument;
12828   bool template_p;
12829   bool address_p;
12830   bool maybe_type_id = false;
12831   cp_token *token = NULL, *argument_start_token = NULL;
12832   cp_id_kind idk;
12833
12834   /* There's really no way to know what we're looking at, so we just
12835      try each alternative in order.
12836
12837        [temp.arg]
12838
12839        In a template-argument, an ambiguity between a type-id and an
12840        expression is resolved to a type-id, regardless of the form of
12841        the corresponding template-parameter.
12842
12843      Therefore, we try a type-id first.  */
12844   cp_parser_parse_tentatively (parser);
12845   argument = cp_parser_template_type_arg (parser);
12846   /* If there was no error parsing the type-id but the next token is a
12847      '>>', our behavior depends on which dialect of C++ we're
12848      parsing. In C++98, we probably found a typo for '> >'. But there
12849      are type-id which are also valid expressions. For instance:
12850
12851      struct X { int operator >> (int); };
12852      template <int V> struct Foo {};
12853      Foo<X () >> 5> r;
12854
12855      Here 'X()' is a valid type-id of a function type, but the user just
12856      wanted to write the expression "X() >> 5". Thus, we remember that we
12857      found a valid type-id, but we still try to parse the argument as an
12858      expression to see what happens. 
12859
12860      In C++0x, the '>>' will be considered two separate '>'
12861      tokens.  */
12862   if (!cp_parser_error_occurred (parser)
12863       && cxx_dialect == cxx98
12864       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12865     {
12866       maybe_type_id = true;
12867       cp_parser_abort_tentative_parse (parser);
12868     }
12869   else
12870     {
12871       /* If the next token isn't a `,' or a `>', then this argument wasn't
12872       really finished. This means that the argument is not a valid
12873       type-id.  */
12874       if (!cp_parser_next_token_ends_template_argument_p (parser))
12875         cp_parser_error (parser, "expected template-argument");
12876       /* If that worked, we're done.  */
12877       if (cp_parser_parse_definitely (parser))
12878         return argument;
12879     }
12880   /* We're still not sure what the argument will be.  */
12881   cp_parser_parse_tentatively (parser);
12882   /* Try a template.  */
12883   argument_start_token = cp_lexer_peek_token (parser->lexer);
12884   argument = cp_parser_id_expression (parser,
12885                                       /*template_keyword_p=*/false,
12886                                       /*check_dependency_p=*/true,
12887                                       &template_p,
12888                                       /*declarator_p=*/false,
12889                                       /*optional_p=*/false);
12890   /* If the next token isn't a `,' or a `>', then this argument wasn't
12891      really finished.  */
12892   if (!cp_parser_next_token_ends_template_argument_p (parser))
12893     cp_parser_error (parser, "expected template-argument");
12894   if (!cp_parser_error_occurred (parser))
12895     {
12896       /* Figure out what is being referred to.  If the id-expression
12897          was for a class template specialization, then we will have a
12898          TYPE_DECL at this point.  There is no need to do name lookup
12899          at this point in that case.  */
12900       if (TREE_CODE (argument) != TYPE_DECL)
12901         argument = cp_parser_lookup_name (parser, argument,
12902                                           none_type,
12903                                           /*is_template=*/template_p,
12904                                           /*is_namespace=*/false,
12905                                           /*check_dependency=*/true,
12906                                           /*ambiguous_decls=*/NULL,
12907                                           argument_start_token->location);
12908       if (TREE_CODE (argument) != TEMPLATE_DECL
12909           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12910         cp_parser_error (parser, "expected template-name");
12911     }
12912   if (cp_parser_parse_definitely (parser))
12913     return argument;
12914   /* It must be a non-type argument.  There permitted cases are given
12915      in [temp.arg.nontype]:
12916
12917      -- an integral constant-expression of integral or enumeration
12918         type; or
12919
12920      -- the name of a non-type template-parameter; or
12921
12922      -- the name of an object or function with external linkage...
12923
12924      -- the address of an object or function with external linkage...
12925
12926      -- a pointer to member...  */
12927   /* Look for a non-type template parameter.  */
12928   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12929     {
12930       cp_parser_parse_tentatively (parser);
12931       argument = cp_parser_primary_expression (parser,
12932                                                /*address_p=*/false,
12933                                                /*cast_p=*/false,
12934                                                /*template_arg_p=*/true,
12935                                                &idk);
12936       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12937           || !cp_parser_next_token_ends_template_argument_p (parser))
12938         cp_parser_simulate_error (parser);
12939       if (cp_parser_parse_definitely (parser))
12940         return argument;
12941     }
12942
12943   /* If the next token is "&", the argument must be the address of an
12944      object or function with external linkage.  */
12945   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12946   if (address_p)
12947     cp_lexer_consume_token (parser->lexer);
12948   /* See if we might have an id-expression.  */
12949   token = cp_lexer_peek_token (parser->lexer);
12950   if (token->type == CPP_NAME
12951       || token->keyword == RID_OPERATOR
12952       || token->type == CPP_SCOPE
12953       || token->type == CPP_TEMPLATE_ID
12954       || token->type == CPP_NESTED_NAME_SPECIFIER)
12955     {
12956       cp_parser_parse_tentatively (parser);
12957       argument = cp_parser_primary_expression (parser,
12958                                                address_p,
12959                                                /*cast_p=*/false,
12960                                                /*template_arg_p=*/true,
12961                                                &idk);
12962       if (cp_parser_error_occurred (parser)
12963           || !cp_parser_next_token_ends_template_argument_p (parser))
12964         cp_parser_abort_tentative_parse (parser);
12965       else
12966         {
12967           tree probe;
12968
12969           if (TREE_CODE (argument) == INDIRECT_REF)
12970             {
12971               gcc_assert (REFERENCE_REF_P (argument));
12972               argument = TREE_OPERAND (argument, 0);
12973             }
12974
12975           /* If we're in a template, we represent a qualified-id referring
12976              to a static data member as a SCOPE_REF even if the scope isn't
12977              dependent so that we can check access control later.  */
12978           probe = argument;
12979           if (TREE_CODE (probe) == SCOPE_REF)
12980             probe = TREE_OPERAND (probe, 1);
12981           if (TREE_CODE (probe) == VAR_DECL)
12982             {
12983               /* A variable without external linkage might still be a
12984                  valid constant-expression, so no error is issued here
12985                  if the external-linkage check fails.  */
12986               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12987                 cp_parser_simulate_error (parser);
12988             }
12989           else if (is_overloaded_fn (argument))
12990             /* All overloaded functions are allowed; if the external
12991                linkage test does not pass, an error will be issued
12992                later.  */
12993             ;
12994           else if (address_p
12995                    && (TREE_CODE (argument) == OFFSET_REF
12996                        || TREE_CODE (argument) == SCOPE_REF))
12997             /* A pointer-to-member.  */
12998             ;
12999           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13000             ;
13001           else
13002             cp_parser_simulate_error (parser);
13003
13004           if (cp_parser_parse_definitely (parser))
13005             {
13006               if (address_p)
13007                 argument = build_x_unary_op (ADDR_EXPR, argument,
13008                                              tf_warning_or_error);
13009               return argument;
13010             }
13011         }
13012     }
13013   /* If the argument started with "&", there are no other valid
13014      alternatives at this point.  */
13015   if (address_p)
13016     {
13017       cp_parser_error (parser, "invalid non-type template argument");
13018       return error_mark_node;
13019     }
13020
13021   /* If the argument wasn't successfully parsed as a type-id followed
13022      by '>>', the argument can only be a constant expression now.
13023      Otherwise, we try parsing the constant-expression tentatively,
13024      because the argument could really be a type-id.  */
13025   if (maybe_type_id)
13026     cp_parser_parse_tentatively (parser);
13027   argument = cp_parser_constant_expression (parser,
13028                                             /*allow_non_constant_p=*/false,
13029                                             /*non_constant_p=*/NULL);
13030   argument = fold_non_dependent_expr (argument);
13031   if (!maybe_type_id)
13032     return argument;
13033   if (!cp_parser_next_token_ends_template_argument_p (parser))
13034     cp_parser_error (parser, "expected template-argument");
13035   if (cp_parser_parse_definitely (parser))
13036     return argument;
13037   /* We did our best to parse the argument as a non type-id, but that
13038      was the only alternative that matched (albeit with a '>' after
13039      it). We can assume it's just a typo from the user, and a
13040      diagnostic will then be issued.  */
13041   return cp_parser_template_type_arg (parser);
13042 }
13043
13044 /* Parse an explicit-instantiation.
13045
13046    explicit-instantiation:
13047      template declaration
13048
13049    Although the standard says `declaration', what it really means is:
13050
13051    explicit-instantiation:
13052      template decl-specifier-seq [opt] declarator [opt] ;
13053
13054    Things like `template int S<int>::i = 5, int S<double>::j;' are not
13055    supposed to be allowed.  A defect report has been filed about this
13056    issue.
13057
13058    GNU Extension:
13059
13060    explicit-instantiation:
13061      storage-class-specifier template
13062        decl-specifier-seq [opt] declarator [opt] ;
13063      function-specifier template
13064        decl-specifier-seq [opt] declarator [opt] ;  */
13065
13066 static void
13067 cp_parser_explicit_instantiation (cp_parser* parser)
13068 {
13069   int declares_class_or_enum;
13070   cp_decl_specifier_seq decl_specifiers;
13071   tree extension_specifier = NULL_TREE;
13072
13073   timevar_push (TV_TEMPLATE_INST);
13074
13075   /* Look for an (optional) storage-class-specifier or
13076      function-specifier.  */
13077   if (cp_parser_allow_gnu_extensions_p (parser))
13078     {
13079       extension_specifier
13080         = cp_parser_storage_class_specifier_opt (parser);
13081       if (!extension_specifier)
13082         extension_specifier
13083           = cp_parser_function_specifier_opt (parser,
13084                                               /*decl_specs=*/NULL);
13085     }
13086
13087   /* Look for the `template' keyword.  */
13088   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13089   /* Let the front end know that we are processing an explicit
13090      instantiation.  */
13091   begin_explicit_instantiation ();
13092   /* [temp.explicit] says that we are supposed to ignore access
13093      control while processing explicit instantiation directives.  */
13094   push_deferring_access_checks (dk_no_check);
13095   /* Parse a decl-specifier-seq.  */
13096   cp_parser_decl_specifier_seq (parser,
13097                                 CP_PARSER_FLAGS_OPTIONAL,
13098                                 &decl_specifiers,
13099                                 &declares_class_or_enum);
13100   /* If there was exactly one decl-specifier, and it declared a class,
13101      and there's no declarator, then we have an explicit type
13102      instantiation.  */
13103   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13104     {
13105       tree type;
13106
13107       type = check_tag_decl (&decl_specifiers);
13108       /* Turn access control back on for names used during
13109          template instantiation.  */
13110       pop_deferring_access_checks ();
13111       if (type)
13112         do_type_instantiation (type, extension_specifier,
13113                                /*complain=*/tf_error);
13114     }
13115   else
13116     {
13117       cp_declarator *declarator;
13118       tree decl;
13119
13120       /* Parse the declarator.  */
13121       declarator
13122         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13123                                 /*ctor_dtor_or_conv_p=*/NULL,
13124                                 /*parenthesized_p=*/NULL,
13125                                 /*member_p=*/false);
13126       if (declares_class_or_enum & 2)
13127         cp_parser_check_for_definition_in_return_type (declarator,
13128                                                        decl_specifiers.type,
13129                                                        decl_specifiers.type_location);
13130       if (declarator != cp_error_declarator)
13131         {
13132           if (decl_specifiers.specs[(int)ds_inline])
13133             permerror (input_location, "explicit instantiation shall not use"
13134                        " %<inline%> specifier");
13135           if (decl_specifiers.specs[(int)ds_constexpr])
13136             permerror (input_location, "explicit instantiation shall not use"
13137                        " %<constexpr%> specifier");
13138
13139           decl = grokdeclarator (declarator, &decl_specifiers,
13140                                  NORMAL, 0, &decl_specifiers.attributes);
13141           /* Turn access control back on for names used during
13142              template instantiation.  */
13143           pop_deferring_access_checks ();
13144           /* Do the explicit instantiation.  */
13145           do_decl_instantiation (decl, extension_specifier);
13146         }
13147       else
13148         {
13149           pop_deferring_access_checks ();
13150           /* Skip the body of the explicit instantiation.  */
13151           cp_parser_skip_to_end_of_statement (parser);
13152         }
13153     }
13154   /* We're done with the instantiation.  */
13155   end_explicit_instantiation ();
13156
13157   cp_parser_consume_semicolon_at_end_of_statement (parser);
13158
13159   timevar_pop (TV_TEMPLATE_INST);
13160 }
13161
13162 /* Parse an explicit-specialization.
13163
13164    explicit-specialization:
13165      template < > declaration
13166
13167    Although the standard says `declaration', what it really means is:
13168
13169    explicit-specialization:
13170      template <> decl-specifier [opt] init-declarator [opt] ;
13171      template <> function-definition
13172      template <> explicit-specialization
13173      template <> template-declaration  */
13174
13175 static void
13176 cp_parser_explicit_specialization (cp_parser* parser)
13177 {
13178   bool need_lang_pop;
13179   cp_token *token = cp_lexer_peek_token (parser->lexer);
13180
13181   /* Look for the `template' keyword.  */
13182   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13183   /* Look for the `<'.  */
13184   cp_parser_require (parser, CPP_LESS, RT_LESS);
13185   /* Look for the `>'.  */
13186   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13187   /* We have processed another parameter list.  */
13188   ++parser->num_template_parameter_lists;
13189   /* [temp]
13190
13191      A template ... explicit specialization ... shall not have C
13192      linkage.  */
13193   if (current_lang_name == lang_name_c)
13194     {
13195       error_at (token->location, "template specialization with C linkage");
13196       /* Give it C++ linkage to avoid confusing other parts of the
13197          front end.  */
13198       push_lang_context (lang_name_cplusplus);
13199       need_lang_pop = true;
13200     }
13201   else
13202     need_lang_pop = false;
13203   /* Let the front end know that we are beginning a specialization.  */
13204   if (!begin_specialization ())
13205     {
13206       end_specialization ();
13207       return;
13208     }
13209
13210   /* If the next keyword is `template', we need to figure out whether
13211      or not we're looking a template-declaration.  */
13212   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13213     {
13214       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13215           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13216         cp_parser_template_declaration_after_export (parser,
13217                                                      /*member_p=*/false);
13218       else
13219         cp_parser_explicit_specialization (parser);
13220     }
13221   else
13222     /* Parse the dependent declaration.  */
13223     cp_parser_single_declaration (parser,
13224                                   /*checks=*/NULL,
13225                                   /*member_p=*/false,
13226                                   /*explicit_specialization_p=*/true,
13227                                   /*friend_p=*/NULL);
13228   /* We're done with the specialization.  */
13229   end_specialization ();
13230   /* For the erroneous case of a template with C linkage, we pushed an
13231      implicit C++ linkage scope; exit that scope now.  */
13232   if (need_lang_pop)
13233     pop_lang_context ();
13234   /* We're done with this parameter list.  */
13235   --parser->num_template_parameter_lists;
13236 }
13237
13238 /* Parse a type-specifier.
13239
13240    type-specifier:
13241      simple-type-specifier
13242      class-specifier
13243      enum-specifier
13244      elaborated-type-specifier
13245      cv-qualifier
13246
13247    GNU Extension:
13248
13249    type-specifier:
13250      __complex__
13251
13252    Returns a representation of the type-specifier.  For a
13253    class-specifier, enum-specifier, or elaborated-type-specifier, a
13254    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13255
13256    The parser flags FLAGS is used to control type-specifier parsing.
13257
13258    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13259    in a decl-specifier-seq.
13260
13261    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13262    class-specifier, enum-specifier, or elaborated-type-specifier, then
13263    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13264    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13265    zero.
13266
13267    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13268    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13269    is set to FALSE.  */
13270
13271 static tree
13272 cp_parser_type_specifier (cp_parser* parser,
13273                           cp_parser_flags flags,
13274                           cp_decl_specifier_seq *decl_specs,
13275                           bool is_declaration,
13276                           int* declares_class_or_enum,
13277                           bool* is_cv_qualifier)
13278 {
13279   tree type_spec = NULL_TREE;
13280   cp_token *token;
13281   enum rid keyword;
13282   cp_decl_spec ds = ds_last;
13283
13284   /* Assume this type-specifier does not declare a new type.  */
13285   if (declares_class_or_enum)
13286     *declares_class_or_enum = 0;
13287   /* And that it does not specify a cv-qualifier.  */
13288   if (is_cv_qualifier)
13289     *is_cv_qualifier = false;
13290   /* Peek at the next token.  */
13291   token = cp_lexer_peek_token (parser->lexer);
13292
13293   /* If we're looking at a keyword, we can use that to guide the
13294      production we choose.  */
13295   keyword = token->keyword;
13296   switch (keyword)
13297     {
13298     case RID_ENUM:
13299       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13300         goto elaborated_type_specifier;
13301
13302       /* Look for the enum-specifier.  */
13303       type_spec = cp_parser_enum_specifier (parser);
13304       /* If that worked, we're done.  */
13305       if (type_spec)
13306         {
13307           if (declares_class_or_enum)
13308             *declares_class_or_enum = 2;
13309           if (decl_specs)
13310             cp_parser_set_decl_spec_type (decl_specs,
13311                                           type_spec,
13312                                           token->location,
13313                                           /*type_definition_p=*/true);
13314           return type_spec;
13315         }
13316       else
13317         goto elaborated_type_specifier;
13318
13319       /* Any of these indicate either a class-specifier, or an
13320          elaborated-type-specifier.  */
13321     case RID_CLASS:
13322     case RID_STRUCT:
13323     case RID_UNION:
13324       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13325         goto elaborated_type_specifier;
13326
13327       /* Parse tentatively so that we can back up if we don't find a
13328          class-specifier.  */
13329       cp_parser_parse_tentatively (parser);
13330       /* Look for the class-specifier.  */
13331       type_spec = cp_parser_class_specifier (parser);
13332       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13333       /* If that worked, we're done.  */
13334       if (cp_parser_parse_definitely (parser))
13335         {
13336           if (declares_class_or_enum)
13337             *declares_class_or_enum = 2;
13338           if (decl_specs)
13339             cp_parser_set_decl_spec_type (decl_specs,
13340                                           type_spec,
13341                                           token->location,
13342                                           /*type_definition_p=*/true);
13343           return type_spec;
13344         }
13345
13346       /* Fall through.  */
13347     elaborated_type_specifier:
13348       /* We're declaring (not defining) a class or enum.  */
13349       if (declares_class_or_enum)
13350         *declares_class_or_enum = 1;
13351
13352       /* Fall through.  */
13353     case RID_TYPENAME:
13354       /* Look for an elaborated-type-specifier.  */
13355       type_spec
13356         = (cp_parser_elaborated_type_specifier
13357            (parser,
13358             decl_specs && decl_specs->specs[(int) ds_friend],
13359             is_declaration));
13360       if (decl_specs)
13361         cp_parser_set_decl_spec_type (decl_specs,
13362                                       type_spec,
13363                                       token->location,
13364                                       /*type_definition_p=*/false);
13365       return type_spec;
13366
13367     case RID_CONST:
13368       ds = ds_const;
13369       if (is_cv_qualifier)
13370         *is_cv_qualifier = true;
13371       break;
13372
13373     case RID_VOLATILE:
13374       ds = ds_volatile;
13375       if (is_cv_qualifier)
13376         *is_cv_qualifier = true;
13377       break;
13378
13379     case RID_RESTRICT:
13380       ds = ds_restrict;
13381       if (is_cv_qualifier)
13382         *is_cv_qualifier = true;
13383       break;
13384
13385     case RID_COMPLEX:
13386       /* The `__complex__' keyword is a GNU extension.  */
13387       ds = ds_complex;
13388       break;
13389
13390     default:
13391       break;
13392     }
13393
13394   /* Handle simple keywords.  */
13395   if (ds != ds_last)
13396     {
13397       if (decl_specs)
13398         {
13399           ++decl_specs->specs[(int)ds];
13400           decl_specs->any_specifiers_p = true;
13401         }
13402       return cp_lexer_consume_token (parser->lexer)->u.value;
13403     }
13404
13405   /* If we do not already have a type-specifier, assume we are looking
13406      at a simple-type-specifier.  */
13407   type_spec = cp_parser_simple_type_specifier (parser,
13408                                                decl_specs,
13409                                                flags);
13410
13411   /* If we didn't find a type-specifier, and a type-specifier was not
13412      optional in this context, issue an error message.  */
13413   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13414     {
13415       cp_parser_error (parser, "expected type specifier");
13416       return error_mark_node;
13417     }
13418
13419   return type_spec;
13420 }
13421
13422 /* Parse a simple-type-specifier.
13423
13424    simple-type-specifier:
13425      :: [opt] nested-name-specifier [opt] type-name
13426      :: [opt] nested-name-specifier template template-id
13427      char
13428      wchar_t
13429      bool
13430      short
13431      int
13432      long
13433      signed
13434      unsigned
13435      float
13436      double
13437      void
13438
13439    C++0x Extension:
13440
13441    simple-type-specifier:
13442      auto
13443      decltype ( expression )   
13444      char16_t
13445      char32_t
13446      __underlying_type ( type-id )
13447
13448    GNU Extension:
13449
13450    simple-type-specifier:
13451      __int128
13452      __typeof__ unary-expression
13453      __typeof__ ( type-id )
13454
13455    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13456    appropriately updated.  */
13457
13458 static tree
13459 cp_parser_simple_type_specifier (cp_parser* parser,
13460                                  cp_decl_specifier_seq *decl_specs,
13461                                  cp_parser_flags flags)
13462 {
13463   tree type = NULL_TREE;
13464   cp_token *token;
13465
13466   /* Peek at the next token.  */
13467   token = cp_lexer_peek_token (parser->lexer);
13468
13469   /* If we're looking at a keyword, things are easy.  */
13470   switch (token->keyword)
13471     {
13472     case RID_CHAR:
13473       if (decl_specs)
13474         decl_specs->explicit_char_p = true;
13475       type = char_type_node;
13476       break;
13477     case RID_CHAR16:
13478       type = char16_type_node;
13479       break;
13480     case RID_CHAR32:
13481       type = char32_type_node;
13482       break;
13483     case RID_WCHAR:
13484       type = wchar_type_node;
13485       break;
13486     case RID_BOOL:
13487       type = boolean_type_node;
13488       break;
13489     case RID_SHORT:
13490       if (decl_specs)
13491         ++decl_specs->specs[(int) ds_short];
13492       type = short_integer_type_node;
13493       break;
13494     case RID_INT:
13495       if (decl_specs)
13496         decl_specs->explicit_int_p = true;
13497       type = integer_type_node;
13498       break;
13499     case RID_INT128:
13500       if (!int128_integer_type_node)
13501         break;
13502       if (decl_specs)
13503         decl_specs->explicit_int128_p = true;
13504       type = int128_integer_type_node;
13505       break;
13506     case RID_LONG:
13507       if (decl_specs)
13508         ++decl_specs->specs[(int) ds_long];
13509       type = long_integer_type_node;
13510       break;
13511     case RID_SIGNED:
13512       if (decl_specs)
13513         ++decl_specs->specs[(int) ds_signed];
13514       type = integer_type_node;
13515       break;
13516     case RID_UNSIGNED:
13517       if (decl_specs)
13518         ++decl_specs->specs[(int) ds_unsigned];
13519       type = unsigned_type_node;
13520       break;
13521     case RID_FLOAT:
13522       type = float_type_node;
13523       break;
13524     case RID_DOUBLE:
13525       type = double_type_node;
13526       break;
13527     case RID_VOID:
13528       type = void_type_node;
13529       break;
13530       
13531     case RID_AUTO:
13532       maybe_warn_cpp0x (CPP0X_AUTO);
13533       type = make_auto ();
13534       break;
13535
13536     case RID_DECLTYPE:
13537       /* Since DR 743, decltype can either be a simple-type-specifier by
13538          itself or begin a nested-name-specifier.  Parsing it will replace
13539          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13540          handling below decide what to do.  */
13541       cp_parser_decltype (parser);
13542       cp_lexer_set_token_position (parser->lexer, token);
13543       break;
13544
13545     case RID_TYPEOF:
13546       /* Consume the `typeof' token.  */
13547       cp_lexer_consume_token (parser->lexer);
13548       /* Parse the operand to `typeof'.  */
13549       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13550       /* If it is not already a TYPE, take its type.  */
13551       if (!TYPE_P (type))
13552         type = finish_typeof (type);
13553
13554       if (decl_specs)
13555         cp_parser_set_decl_spec_type (decl_specs, type,
13556                                       token->location,
13557                                       /*type_definition_p=*/false);
13558
13559       return type;
13560
13561     case RID_UNDERLYING_TYPE:
13562       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13563       if (decl_specs)
13564         cp_parser_set_decl_spec_type (decl_specs, type,
13565                                       token->location,
13566                                       /*type_definition_p=*/false);
13567
13568       return type;
13569
13570     case RID_BASES:
13571     case RID_DIRECT_BASES:
13572       type = cp_parser_trait_expr (parser, token->keyword);
13573       if (decl_specs)
13574        cp_parser_set_decl_spec_type (decl_specs, type,
13575                                      token->location,
13576                                      /*type_definition_p=*/false);
13577       return type;
13578     default:
13579       break;
13580     }
13581
13582   /* If token is an already-parsed decltype not followed by ::,
13583      it's a simple-type-specifier.  */
13584   if (token->type == CPP_DECLTYPE
13585       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13586     {
13587       type = token->u.value;
13588       if (decl_specs)
13589         cp_parser_set_decl_spec_type (decl_specs, type,
13590                                       token->location,
13591                                       /*type_definition_p=*/false);
13592       cp_lexer_consume_token (parser->lexer);
13593       return type;
13594     }
13595
13596   /* If the type-specifier was for a built-in type, we're done.  */
13597   if (type)
13598     {
13599       /* Record the type.  */
13600       if (decl_specs
13601           && (token->keyword != RID_SIGNED
13602               && token->keyword != RID_UNSIGNED
13603               && token->keyword != RID_SHORT
13604               && token->keyword != RID_LONG))
13605         cp_parser_set_decl_spec_type (decl_specs,
13606                                       type,
13607                                       token->location,
13608                                       /*type_definition_p=*/false);
13609       if (decl_specs)
13610         decl_specs->any_specifiers_p = true;
13611
13612       /* Consume the token.  */
13613       cp_lexer_consume_token (parser->lexer);
13614
13615       /* There is no valid C++ program where a non-template type is
13616          followed by a "<".  That usually indicates that the user thought
13617          that the type was a template.  */
13618       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13619
13620       return TYPE_NAME (type);
13621     }
13622
13623   /* The type-specifier must be a user-defined type.  */
13624   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13625     {
13626       bool qualified_p;
13627       bool global_p;
13628
13629       /* Don't gobble tokens or issue error messages if this is an
13630          optional type-specifier.  */
13631       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13632         cp_parser_parse_tentatively (parser);
13633
13634       /* Look for the optional `::' operator.  */
13635       global_p
13636         = (cp_parser_global_scope_opt (parser,
13637                                        /*current_scope_valid_p=*/false)
13638            != NULL_TREE);
13639       /* Look for the nested-name specifier.  */
13640       qualified_p
13641         = (cp_parser_nested_name_specifier_opt (parser,
13642                                                 /*typename_keyword_p=*/false,
13643                                                 /*check_dependency_p=*/true,
13644                                                 /*type_p=*/false,
13645                                                 /*is_declaration=*/false)
13646            != NULL_TREE);
13647       token = cp_lexer_peek_token (parser->lexer);
13648       /* If we have seen a nested-name-specifier, and the next token
13649          is `template', then we are using the template-id production.  */
13650       if (parser->scope
13651           && cp_parser_optional_template_keyword (parser))
13652         {
13653           /* Look for the template-id.  */
13654           type = cp_parser_template_id (parser,
13655                                         /*template_keyword_p=*/true,
13656                                         /*check_dependency_p=*/true,
13657                                         /*is_declaration=*/false);
13658           /* If the template-id did not name a type, we are out of
13659              luck.  */
13660           if (TREE_CODE (type) != TYPE_DECL)
13661             {
13662               cp_parser_error (parser, "expected template-id for type");
13663               type = NULL_TREE;
13664             }
13665         }
13666       /* Otherwise, look for a type-name.  */
13667       else
13668         type = cp_parser_type_name (parser);
13669       /* Keep track of all name-lookups performed in class scopes.  */
13670       if (type
13671           && !global_p
13672           && !qualified_p
13673           && TREE_CODE (type) == TYPE_DECL
13674           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13675         maybe_note_name_used_in_class (DECL_NAME (type), type);
13676       /* If it didn't work out, we don't have a TYPE.  */
13677       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13678           && !cp_parser_parse_definitely (parser))
13679         type = NULL_TREE;
13680       if (type && decl_specs)
13681         cp_parser_set_decl_spec_type (decl_specs, type,
13682                                       token->location,
13683                                       /*type_definition_p=*/false);
13684     }
13685
13686   /* If we didn't get a type-name, issue an error message.  */
13687   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13688     {
13689       cp_parser_error (parser, "expected type-name");
13690       return error_mark_node;
13691     }
13692
13693   if (type && type != error_mark_node)
13694     {
13695       /* See if TYPE is an Objective-C type, and if so, parse and
13696          accept any protocol references following it.  Do this before
13697          the cp_parser_check_for_invalid_template_id() call, because
13698          Objective-C types can be followed by '<...>' which would
13699          enclose protocol names rather than template arguments, and so
13700          everything is fine.  */
13701       if (c_dialect_objc () && !parser->scope
13702           && (objc_is_id (type) || objc_is_class_name (type)))
13703         {
13704           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13705           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13706
13707           /* Clobber the "unqualified" type previously entered into
13708              DECL_SPECS with the new, improved protocol-qualified version.  */
13709           if (decl_specs)
13710             decl_specs->type = qual_type;
13711
13712           return qual_type;
13713         }
13714
13715       /* There is no valid C++ program where a non-template type is
13716          followed by a "<".  That usually indicates that the user
13717          thought that the type was a template.  */
13718       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13719                                                token->location);
13720     }
13721
13722   return type;
13723 }
13724
13725 /* Parse a type-name.
13726
13727    type-name:
13728      class-name
13729      enum-name
13730      typedef-name
13731      simple-template-id [in c++0x]
13732
13733    enum-name:
13734      identifier
13735
13736    typedef-name:
13737      identifier
13738
13739    Returns a TYPE_DECL for the type.  */
13740
13741 static tree
13742 cp_parser_type_name (cp_parser* parser)
13743 {
13744   tree type_decl;
13745
13746   /* We can't know yet whether it is a class-name or not.  */
13747   cp_parser_parse_tentatively (parser);
13748   /* Try a class-name.  */
13749   type_decl = cp_parser_class_name (parser,
13750                                     /*typename_keyword_p=*/false,
13751                                     /*template_keyword_p=*/false,
13752                                     none_type,
13753                                     /*check_dependency_p=*/true,
13754                                     /*class_head_p=*/false,
13755                                     /*is_declaration=*/false);
13756   /* If it's not a class-name, keep looking.  */
13757   if (!cp_parser_parse_definitely (parser))
13758     {
13759       if (cxx_dialect < cxx0x)
13760         /* It must be a typedef-name or an enum-name.  */
13761         return cp_parser_nonclass_name (parser);
13762
13763       cp_parser_parse_tentatively (parser);
13764       /* It is either a simple-template-id representing an
13765          instantiation of an alias template...  */
13766       type_decl = cp_parser_template_id (parser,
13767                                          /*template_keyword_p=*/false,
13768                                          /*check_dependency_p=*/false,
13769                                          /*is_declaration=*/false);
13770       /* Note that this must be an instantiation of an alias template
13771          because [temp.names]/6 says:
13772          
13773              A template-id that names an alias template specialization
13774              is a type-name.
13775
13776          Whereas [temp.names]/7 says:
13777          
13778              A simple-template-id that names a class template
13779              specialization is a class-name.  */
13780       if (type_decl != NULL_TREE
13781           && TREE_CODE (type_decl) == TYPE_DECL
13782           && TYPE_DECL_ALIAS_P (type_decl))
13783         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13784       else
13785         cp_parser_simulate_error (parser);
13786
13787       if (!cp_parser_parse_definitely (parser))
13788         /* ... Or a typedef-name or an enum-name.  */
13789         return cp_parser_nonclass_name (parser);
13790     }
13791
13792   return type_decl;
13793 }
13794
13795 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13796
13797    enum-name:
13798      identifier
13799
13800    typedef-name:
13801      identifier
13802
13803    Returns a TYPE_DECL for the type.  */
13804
13805 static tree
13806 cp_parser_nonclass_name (cp_parser* parser)
13807 {
13808   tree type_decl;
13809   tree identifier;
13810
13811   cp_token *token = cp_lexer_peek_token (parser->lexer);
13812   identifier = cp_parser_identifier (parser);
13813   if (identifier == error_mark_node)
13814     return error_mark_node;
13815
13816   /* Look up the type-name.  */
13817   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13818
13819   if (TREE_CODE (type_decl) == USING_DECL)
13820     {
13821       if (!DECL_DEPENDENT_P (type_decl))
13822         type_decl = strip_using_decl (type_decl);
13823       else if (USING_DECL_TYPENAME_P (type_decl))
13824         {
13825           /* We have found a type introduced by a using
13826              declaration at class scope that refers to a dependent
13827              type.
13828              
13829              using typename :: [opt] nested-name-specifier unqualified-id ;
13830           */
13831           type_decl = make_typename_type (TREE_TYPE (type_decl),
13832                                           DECL_NAME (type_decl),
13833                                           typename_type, tf_error);
13834           if (type_decl != error_mark_node)
13835             type_decl = TYPE_NAME (type_decl);
13836         }
13837     }
13838   
13839   if (TREE_CODE (type_decl) != TYPE_DECL
13840       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13841     {
13842       /* See if this is an Objective-C type.  */
13843       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13844       tree type = objc_get_protocol_qualified_type (identifier, protos);
13845       if (type)
13846         type_decl = TYPE_NAME (type);
13847     }
13848
13849   /* Issue an error if we did not find a type-name.  */
13850   if (TREE_CODE (type_decl) != TYPE_DECL
13851       /* In Objective-C, we have the complication that class names are
13852          normally type names and start declarations (eg, the
13853          "NSObject" in "NSObject *object;"), but can be used in an
13854          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13855          is an expression.  So, a classname followed by a dot is not a
13856          valid type-name.  */
13857       || (objc_is_class_name (TREE_TYPE (type_decl))
13858           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13859     {
13860       if (!cp_parser_simulate_error (parser))
13861         cp_parser_name_lookup_error (parser, identifier, type_decl,
13862                                      NLE_TYPE, token->location);
13863       return error_mark_node;
13864     }
13865   /* Remember that the name was used in the definition of the
13866      current class so that we can check later to see if the
13867      meaning would have been different after the class was
13868      entirely defined.  */
13869   else if (type_decl != error_mark_node
13870            && !parser->scope)
13871     maybe_note_name_used_in_class (identifier, type_decl);
13872   
13873   return type_decl;
13874 }
13875
13876 /* Parse an elaborated-type-specifier.  Note that the grammar given
13877    here incorporates the resolution to DR68.
13878
13879    elaborated-type-specifier:
13880      class-key :: [opt] nested-name-specifier [opt] identifier
13881      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13882      enum-key :: [opt] nested-name-specifier [opt] identifier
13883      typename :: [opt] nested-name-specifier identifier
13884      typename :: [opt] nested-name-specifier template [opt]
13885        template-id
13886
13887    GNU extension:
13888
13889    elaborated-type-specifier:
13890      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13891      class-key attributes :: [opt] nested-name-specifier [opt]
13892                template [opt] template-id
13893      enum attributes :: [opt] nested-name-specifier [opt] identifier
13894
13895    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13896    declared `friend'.  If IS_DECLARATION is TRUE, then this
13897    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13898    something is being declared.
13899
13900    Returns the TYPE specified.  */
13901
13902 static tree
13903 cp_parser_elaborated_type_specifier (cp_parser* parser,
13904                                      bool is_friend,
13905                                      bool is_declaration)
13906 {
13907   enum tag_types tag_type;
13908   tree identifier;
13909   tree type = NULL_TREE;
13910   tree attributes = NULL_TREE;
13911   tree globalscope;
13912   cp_token *token = NULL;
13913
13914   /* See if we're looking at the `enum' keyword.  */
13915   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13916     {
13917       /* Consume the `enum' token.  */
13918       cp_lexer_consume_token (parser->lexer);
13919       /* Remember that it's an enumeration type.  */
13920       tag_type = enum_type;
13921       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13922          enums) is used here.  */
13923       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13924           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13925         {
13926             pedwarn (input_location, 0, "elaborated-type-specifier "
13927                       "for a scoped enum must not use the %<%D%> keyword",
13928                       cp_lexer_peek_token (parser->lexer)->u.value);
13929           /* Consume the `struct' or `class' and parse it anyway.  */
13930           cp_lexer_consume_token (parser->lexer);
13931         }
13932       /* Parse the attributes.  */
13933       attributes = cp_parser_attributes_opt (parser);
13934     }
13935   /* Or, it might be `typename'.  */
13936   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13937                                            RID_TYPENAME))
13938     {
13939       /* Consume the `typename' token.  */
13940       cp_lexer_consume_token (parser->lexer);
13941       /* Remember that it's a `typename' type.  */
13942       tag_type = typename_type;
13943     }
13944   /* Otherwise it must be a class-key.  */
13945   else
13946     {
13947       tag_type = cp_parser_class_key (parser);
13948       if (tag_type == none_type)
13949         return error_mark_node;
13950       /* Parse the attributes.  */
13951       attributes = cp_parser_attributes_opt (parser);
13952     }
13953
13954   /* Look for the `::' operator.  */
13955   globalscope =  cp_parser_global_scope_opt (parser,
13956                                              /*current_scope_valid_p=*/false);
13957   /* Look for the nested-name-specifier.  */
13958   if (tag_type == typename_type && !globalscope)
13959     {
13960       if (!cp_parser_nested_name_specifier (parser,
13961                                            /*typename_keyword_p=*/true,
13962                                            /*check_dependency_p=*/true,
13963                                            /*type_p=*/true,
13964                                             is_declaration))
13965         return error_mark_node;
13966     }
13967   else
13968     /* Even though `typename' is not present, the proposed resolution
13969        to Core Issue 180 says that in `class A<T>::B', `B' should be
13970        considered a type-name, even if `A<T>' is dependent.  */
13971     cp_parser_nested_name_specifier_opt (parser,
13972                                          /*typename_keyword_p=*/true,
13973                                          /*check_dependency_p=*/true,
13974                                          /*type_p=*/true,
13975                                          is_declaration);
13976  /* For everything but enumeration types, consider a template-id.
13977     For an enumeration type, consider only a plain identifier.  */
13978   if (tag_type != enum_type)
13979     {
13980       bool template_p = false;
13981       tree decl;
13982
13983       /* Allow the `template' keyword.  */
13984       template_p = cp_parser_optional_template_keyword (parser);
13985       /* If we didn't see `template', we don't know if there's a
13986          template-id or not.  */
13987       if (!template_p)
13988         cp_parser_parse_tentatively (parser);
13989       /* Parse the template-id.  */
13990       token = cp_lexer_peek_token (parser->lexer);
13991       decl = cp_parser_template_id (parser, template_p,
13992                                     /*check_dependency_p=*/true,
13993                                     is_declaration);
13994       /* If we didn't find a template-id, look for an ordinary
13995          identifier.  */
13996       if (!template_p && !cp_parser_parse_definitely (parser))
13997         ;
13998       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13999          in effect, then we must assume that, upon instantiation, the
14000          template will correspond to a class.  */
14001       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14002                && tag_type == typename_type)
14003         type = make_typename_type (parser->scope, decl,
14004                                    typename_type,
14005                                    /*complain=*/tf_error);
14006       /* If the `typename' keyword is in effect and DECL is not a type
14007          decl. Then type is non existant.   */
14008       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14009         type = NULL_TREE; 
14010       else 
14011         type = check_elaborated_type_specifier (tag_type, decl,
14012                                                 /*allow_template_p=*/true);
14013     }
14014
14015   if (!type)
14016     {
14017       token = cp_lexer_peek_token (parser->lexer);
14018       identifier = cp_parser_identifier (parser);
14019
14020       if (identifier == error_mark_node)
14021         {
14022           parser->scope = NULL_TREE;
14023           return error_mark_node;
14024         }
14025
14026       /* For a `typename', we needn't call xref_tag.  */
14027       if (tag_type == typename_type
14028           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14029         return cp_parser_make_typename_type (parser, parser->scope,
14030                                              identifier,
14031                                              token->location);
14032       /* Look up a qualified name in the usual way.  */
14033       if (parser->scope)
14034         {
14035           tree decl;
14036           tree ambiguous_decls;
14037
14038           decl = cp_parser_lookup_name (parser, identifier,
14039                                         tag_type,
14040                                         /*is_template=*/false,
14041                                         /*is_namespace=*/false,
14042                                         /*check_dependency=*/true,
14043                                         &ambiguous_decls,
14044                                         token->location);
14045
14046           /* If the lookup was ambiguous, an error will already have been
14047              issued.  */
14048           if (ambiguous_decls)
14049             return error_mark_node;
14050
14051           /* If we are parsing friend declaration, DECL may be a
14052              TEMPLATE_DECL tree node here.  However, we need to check
14053              whether this TEMPLATE_DECL results in valid code.  Consider
14054              the following example:
14055
14056                namespace N {
14057                  template <class T> class C {};
14058                }
14059                class X {
14060                  template <class T> friend class N::C; // #1, valid code
14061                };
14062                template <class T> class Y {
14063                  friend class N::C;                    // #2, invalid code
14064                };
14065
14066              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14067              name lookup of `N::C'.  We see that friend declaration must
14068              be template for the code to be valid.  Note that
14069              processing_template_decl does not work here since it is
14070              always 1 for the above two cases.  */
14071
14072           decl = (cp_parser_maybe_treat_template_as_class
14073                   (decl, /*tag_name_p=*/is_friend
14074                          && parser->num_template_parameter_lists));
14075
14076           if (TREE_CODE (decl) != TYPE_DECL)
14077             {
14078               cp_parser_diagnose_invalid_type_name (parser,
14079                                                     parser->scope,
14080                                                     identifier,
14081                                                     token->location);
14082               return error_mark_node;
14083             }
14084
14085           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14086             {
14087               bool allow_template = (parser->num_template_parameter_lists
14088                                       || DECL_SELF_REFERENCE_P (decl));
14089               type = check_elaborated_type_specifier (tag_type, decl, 
14090                                                       allow_template);
14091
14092               if (type == error_mark_node)
14093                 return error_mark_node;
14094             }
14095
14096           /* Forward declarations of nested types, such as
14097
14098                class C1::C2;
14099                class C1::C2::C3;
14100
14101              are invalid unless all components preceding the final '::'
14102              are complete.  If all enclosing types are complete, these
14103              declarations become merely pointless.
14104
14105              Invalid forward declarations of nested types are errors
14106              caught elsewhere in parsing.  Those that are pointless arrive
14107              here.  */
14108
14109           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14110               && !is_friend && !processing_explicit_instantiation)
14111             warning (0, "declaration %qD does not declare anything", decl);
14112
14113           type = TREE_TYPE (decl);
14114         }
14115       else
14116         {
14117           /* An elaborated-type-specifier sometimes introduces a new type and
14118              sometimes names an existing type.  Normally, the rule is that it
14119              introduces a new type only if there is not an existing type of
14120              the same name already in scope.  For example, given:
14121
14122                struct S {};
14123                void f() { struct S s; }
14124
14125              the `struct S' in the body of `f' is the same `struct S' as in
14126              the global scope; the existing definition is used.  However, if
14127              there were no global declaration, this would introduce a new
14128              local class named `S'.
14129
14130              An exception to this rule applies to the following code:
14131
14132                namespace N { struct S; }
14133
14134              Here, the elaborated-type-specifier names a new type
14135              unconditionally; even if there is already an `S' in the
14136              containing scope this declaration names a new type.
14137              This exception only applies if the elaborated-type-specifier
14138              forms the complete declaration:
14139
14140                [class.name]
14141
14142                A declaration consisting solely of `class-key identifier ;' is
14143                either a redeclaration of the name in the current scope or a
14144                forward declaration of the identifier as a class name.  It
14145                introduces the name into the current scope.
14146
14147              We are in this situation precisely when the next token is a `;'.
14148
14149              An exception to the exception is that a `friend' declaration does
14150              *not* name a new type; i.e., given:
14151
14152                struct S { friend struct T; };
14153
14154              `T' is not a new type in the scope of `S'.
14155
14156              Also, `new struct S' or `sizeof (struct S)' never results in the
14157              definition of a new type; a new type can only be declared in a
14158              declaration context.  */
14159
14160           tag_scope ts;
14161           bool template_p;
14162
14163           if (is_friend)
14164             /* Friends have special name lookup rules.  */
14165             ts = ts_within_enclosing_non_class;
14166           else if (is_declaration
14167                    && cp_lexer_next_token_is (parser->lexer,
14168                                               CPP_SEMICOLON))
14169             /* This is a `class-key identifier ;' */
14170             ts = ts_current;
14171           else
14172             ts = ts_global;
14173
14174           template_p =
14175             (parser->num_template_parameter_lists
14176              && (cp_parser_next_token_starts_class_definition_p (parser)
14177                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14178           /* An unqualified name was used to reference this type, so
14179              there were no qualifying templates.  */
14180           if (!cp_parser_check_template_parameters (parser,
14181                                                     /*num_templates=*/0,
14182                                                     token->location,
14183                                                     /*declarator=*/NULL))
14184             return error_mark_node;
14185           type = xref_tag (tag_type, identifier, ts, template_p);
14186         }
14187     }
14188
14189   if (type == error_mark_node)
14190     return error_mark_node;
14191
14192   /* Allow attributes on forward declarations of classes.  */
14193   if (attributes)
14194     {
14195       if (TREE_CODE (type) == TYPENAME_TYPE)
14196         warning (OPT_Wattributes,
14197                  "attributes ignored on uninstantiated type");
14198       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14199                && ! processing_explicit_instantiation)
14200         warning (OPT_Wattributes,
14201                  "attributes ignored on template instantiation");
14202       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14203         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14204       else
14205         warning (OPT_Wattributes,
14206                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14207     }
14208
14209   if (tag_type != enum_type)
14210     {
14211       /* Indicate whether this class was declared as a `class' or as a
14212          `struct'.  */
14213       if (TREE_CODE (type) == RECORD_TYPE)
14214         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14215       cp_parser_check_class_key (tag_type, type);
14216     }
14217
14218   /* A "<" cannot follow an elaborated type specifier.  If that
14219      happens, the user was probably trying to form a template-id.  */
14220   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14221
14222   return type;
14223 }
14224
14225 /* Parse an enum-specifier.
14226
14227    enum-specifier:
14228      enum-head { enumerator-list [opt] }
14229      enum-head { enumerator-list , } [C++0x]
14230
14231    enum-head:
14232      enum-key identifier [opt] enum-base [opt]
14233      enum-key nested-name-specifier identifier enum-base [opt]
14234
14235    enum-key:
14236      enum
14237      enum class   [C++0x]
14238      enum struct  [C++0x]
14239
14240    enum-base:   [C++0x]
14241      : type-specifier-seq
14242
14243    opaque-enum-specifier:
14244      enum-key identifier enum-base [opt] ;
14245
14246    GNU Extensions:
14247      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14248        { enumerator-list [opt] }attributes[opt]
14249      enum-key attributes[opt] identifier [opt] enum-base [opt]
14250        { enumerator-list, }attributes[opt] [C++0x]
14251
14252    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14253    if the token stream isn't an enum-specifier after all.  */
14254
14255 static tree
14256 cp_parser_enum_specifier (cp_parser* parser)
14257 {
14258   tree identifier;
14259   tree type = NULL_TREE;
14260   tree prev_scope;
14261   tree nested_name_specifier = NULL_TREE;
14262   tree attributes;
14263   bool scoped_enum_p = false;
14264   bool has_underlying_type = false;
14265   bool nested_being_defined = false;
14266   bool new_value_list = false;
14267   bool is_new_type = false;
14268   bool is_anonymous = false;
14269   tree underlying_type = NULL_TREE;
14270   cp_token *type_start_token = NULL;
14271   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14272
14273   parser->colon_corrects_to_scope_p = false;
14274
14275   /* Parse tentatively so that we can back up if we don't find a
14276      enum-specifier.  */
14277   cp_parser_parse_tentatively (parser);
14278
14279   /* Caller guarantees that the current token is 'enum', an identifier
14280      possibly follows, and the token after that is an opening brace.
14281      If we don't have an identifier, fabricate an anonymous name for
14282      the enumeration being defined.  */
14283   cp_lexer_consume_token (parser->lexer);
14284
14285   /* Parse the "class" or "struct", which indicates a scoped
14286      enumeration type in C++0x.  */
14287   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14288       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14289     {
14290       if (cxx_dialect < cxx0x)
14291         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14292
14293       /* Consume the `struct' or `class' token.  */
14294       cp_lexer_consume_token (parser->lexer);
14295
14296       scoped_enum_p = true;
14297     }
14298
14299   attributes = cp_parser_attributes_opt (parser);
14300
14301   /* Clear the qualification.  */
14302   parser->scope = NULL_TREE;
14303   parser->qualifying_scope = NULL_TREE;
14304   parser->object_scope = NULL_TREE;
14305
14306   /* Figure out in what scope the declaration is being placed.  */
14307   prev_scope = current_scope ();
14308
14309   type_start_token = cp_lexer_peek_token (parser->lexer);
14310
14311   push_deferring_access_checks (dk_no_check);
14312   nested_name_specifier
14313       = cp_parser_nested_name_specifier_opt (parser,
14314                                              /*typename_keyword_p=*/true,
14315                                              /*check_dependency_p=*/false,
14316                                              /*type_p=*/false,
14317                                              /*is_declaration=*/false);
14318
14319   if (nested_name_specifier)
14320     {
14321       tree name;
14322
14323       identifier = cp_parser_identifier (parser);
14324       name =  cp_parser_lookup_name (parser, identifier,
14325                                      enum_type,
14326                                      /*is_template=*/false,
14327                                      /*is_namespace=*/false,
14328                                      /*check_dependency=*/true,
14329                                      /*ambiguous_decls=*/NULL,
14330                                      input_location);
14331       if (name)
14332         {
14333           type = TREE_TYPE (name);
14334           if (TREE_CODE (type) == TYPENAME_TYPE)
14335             {
14336               /* Are template enums allowed in ISO? */
14337               if (template_parm_scope_p ())
14338                 pedwarn (type_start_token->location, OPT_pedantic,
14339                          "%qD is an enumeration template", name);
14340               /* ignore a typename reference, for it will be solved by name
14341                  in start_enum.  */
14342               type = NULL_TREE;
14343             }
14344         }
14345       else
14346         error_at (type_start_token->location,
14347                   "%qD is not an enumerator-name", identifier);
14348     }
14349   else
14350     {
14351       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14352         identifier = cp_parser_identifier (parser);
14353       else
14354         {
14355           identifier = make_anon_name ();
14356           is_anonymous = true;
14357         }
14358     }
14359   pop_deferring_access_checks ();
14360
14361   /* Check for the `:' that denotes a specified underlying type in C++0x.
14362      Note that a ':' could also indicate a bitfield width, however.  */
14363   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14364     {
14365       cp_decl_specifier_seq type_specifiers;
14366
14367       /* Consume the `:'.  */
14368       cp_lexer_consume_token (parser->lexer);
14369
14370       /* Parse the type-specifier-seq.  */
14371       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14372                                     /*is_trailing_return=*/false,
14373                                     &type_specifiers);
14374
14375       /* At this point this is surely not elaborated type specifier.  */
14376       if (!cp_parser_parse_definitely (parser))
14377         return NULL_TREE;
14378
14379       if (cxx_dialect < cxx0x)
14380         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14381
14382       has_underlying_type = true;
14383
14384       /* If that didn't work, stop.  */
14385       if (type_specifiers.type != error_mark_node)
14386         {
14387           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14388                                             /*initialized=*/0, NULL);
14389           if (underlying_type == error_mark_node)
14390             underlying_type = NULL_TREE;
14391         }
14392     }
14393
14394   /* Look for the `{' but don't consume it yet.  */
14395   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14396     {
14397       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14398         {
14399           cp_parser_error (parser, "expected %<{%>");
14400           if (has_underlying_type)
14401             {
14402               type = NULL_TREE;
14403               goto out;
14404             }
14405         }
14406       /* An opaque-enum-specifier must have a ';' here.  */
14407       if ((scoped_enum_p || underlying_type)
14408           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14409         {
14410           cp_parser_error (parser, "expected %<;%> or %<{%>");
14411           if (has_underlying_type)
14412             {
14413               type = NULL_TREE;
14414               goto out;
14415             }
14416         }
14417     }
14418
14419   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14420     return NULL_TREE;
14421
14422   if (nested_name_specifier)
14423     {
14424       if (CLASS_TYPE_P (nested_name_specifier))
14425         {
14426           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14427           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14428           push_scope (nested_name_specifier);
14429         }
14430       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14431         {
14432           push_nested_namespace (nested_name_specifier);
14433         }
14434     }
14435
14436   /* Issue an error message if type-definitions are forbidden here.  */
14437   if (!cp_parser_check_type_definition (parser))
14438     type = error_mark_node;
14439   else
14440     /* Create the new type.  We do this before consuming the opening
14441        brace so the enum will be recorded as being on the line of its
14442        tag (or the 'enum' keyword, if there is no tag).  */
14443     type = start_enum (identifier, type, underlying_type,
14444                        scoped_enum_p, &is_new_type);
14445
14446   /* If the next token is not '{' it is an opaque-enum-specifier or an
14447      elaborated-type-specifier.  */
14448   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14449     {
14450       timevar_push (TV_PARSE_ENUM);
14451       if (nested_name_specifier)
14452         {
14453           /* The following catches invalid code such as:
14454              enum class S<int>::E { A, B, C }; */
14455           if (!processing_specialization
14456               && CLASS_TYPE_P (nested_name_specifier)
14457               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14458             error_at (type_start_token->location, "cannot add an enumerator "
14459                       "list to a template instantiation");
14460
14461           /* If that scope does not contain the scope in which the
14462              class was originally declared, the program is invalid.  */
14463           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14464             {
14465               if (at_namespace_scope_p ())
14466                 error_at (type_start_token->location,
14467                           "declaration of %qD in namespace %qD which does not "
14468                           "enclose %qD",
14469                           type, prev_scope, nested_name_specifier);
14470               else
14471                 error_at (type_start_token->location,
14472                           "declaration of %qD in %qD which does not enclose %qD",
14473                           type, prev_scope, nested_name_specifier);
14474               type = error_mark_node;
14475             }
14476         }
14477
14478       if (scoped_enum_p)
14479         begin_scope (sk_scoped_enum, type);
14480
14481       /* Consume the opening brace.  */
14482       cp_lexer_consume_token (parser->lexer);
14483
14484       if (type == error_mark_node)
14485         ; /* Nothing to add */
14486       else if (OPAQUE_ENUM_P (type)
14487                || (cxx_dialect > cxx98 && processing_specialization))
14488         {
14489           new_value_list = true;
14490           SET_OPAQUE_ENUM_P (type, false);
14491           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14492         }
14493       else
14494         {
14495           error_at (type_start_token->location, "multiple definition of %q#T", type);
14496           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14497                     "previous definition here");
14498           type = error_mark_node;
14499         }
14500
14501       if (type == error_mark_node)
14502         cp_parser_skip_to_end_of_block_or_statement (parser);
14503       /* If the next token is not '}', then there are some enumerators.  */
14504       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14505         cp_parser_enumerator_list (parser, type);
14506
14507       /* Consume the final '}'.  */
14508       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14509
14510       if (scoped_enum_p)
14511         finish_scope ();
14512       timevar_pop (TV_PARSE_ENUM);
14513     }
14514   else
14515     {
14516       /* If a ';' follows, then it is an opaque-enum-specifier
14517         and additional restrictions apply.  */
14518       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14519         {
14520           if (is_anonymous)
14521             error_at (type_start_token->location,
14522                       "opaque-enum-specifier without name");
14523           else if (nested_name_specifier)
14524             error_at (type_start_token->location,
14525                       "opaque-enum-specifier must use a simple identifier");
14526         }
14527     }
14528
14529   /* Look for trailing attributes to apply to this enumeration, and
14530      apply them if appropriate.  */
14531   if (cp_parser_allow_gnu_extensions_p (parser))
14532     {
14533       tree trailing_attr = cp_parser_attributes_opt (parser);
14534       trailing_attr = chainon (trailing_attr, attributes);
14535       cplus_decl_attributes (&type,
14536                              trailing_attr,
14537                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14538     }
14539
14540   /* Finish up the enumeration.  */
14541   if (type != error_mark_node)
14542     {
14543       if (new_value_list)
14544         finish_enum_value_list (type);
14545       if (is_new_type)
14546         finish_enum (type);
14547     }
14548
14549   if (nested_name_specifier)
14550     {
14551       if (CLASS_TYPE_P (nested_name_specifier))
14552         {
14553           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14554           pop_scope (nested_name_specifier);
14555         }
14556       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14557         {
14558           pop_nested_namespace (nested_name_specifier);
14559         }
14560     }
14561  out:
14562   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14563   return type;
14564 }
14565
14566 /* Parse an enumerator-list.  The enumerators all have the indicated
14567    TYPE.
14568
14569    enumerator-list:
14570      enumerator-definition
14571      enumerator-list , enumerator-definition  */
14572
14573 static void
14574 cp_parser_enumerator_list (cp_parser* parser, tree type)
14575 {
14576   while (true)
14577     {
14578       /* Parse an enumerator-definition.  */
14579       cp_parser_enumerator_definition (parser, type);
14580
14581       /* If the next token is not a ',', we've reached the end of
14582          the list.  */
14583       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14584         break;
14585       /* Otherwise, consume the `,' and keep going.  */
14586       cp_lexer_consume_token (parser->lexer);
14587       /* If the next token is a `}', there is a trailing comma.  */
14588       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14589         {
14590           if (cxx_dialect < cxx0x && !in_system_header)
14591             pedwarn (input_location, OPT_pedantic,
14592                      "comma at end of enumerator list");
14593           break;
14594         }
14595     }
14596 }
14597
14598 /* Parse an enumerator-definition.  The enumerator has the indicated
14599    TYPE.
14600
14601    enumerator-definition:
14602      enumerator
14603      enumerator = constant-expression
14604
14605    enumerator:
14606      identifier  */
14607
14608 static void
14609 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14610 {
14611   tree identifier;
14612   tree value;
14613   location_t loc;
14614
14615   /* Save the input location because we are interested in the location
14616      of the identifier and not the location of the explicit value.  */
14617   loc = cp_lexer_peek_token (parser->lexer)->location;
14618
14619   /* Look for the identifier.  */
14620   identifier = cp_parser_identifier (parser);
14621   if (identifier == error_mark_node)
14622     return;
14623
14624   /* If the next token is an '=', then there is an explicit value.  */
14625   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14626     {
14627       /* Consume the `=' token.  */
14628       cp_lexer_consume_token (parser->lexer);
14629       /* Parse the value.  */
14630       value = cp_parser_constant_expression (parser,
14631                                              /*allow_non_constant_p=*/false,
14632                                              NULL);
14633     }
14634   else
14635     value = NULL_TREE;
14636
14637   /* If we are processing a template, make sure the initializer of the
14638      enumerator doesn't contain any bare template parameter pack.  */
14639   if (check_for_bare_parameter_packs (value))
14640     value = error_mark_node;
14641
14642   /* integral_constant_value will pull out this expression, so make sure
14643      it's folded as appropriate.  */
14644   value = fold_non_dependent_expr (value);
14645
14646   /* Create the enumerator.  */
14647   build_enumerator (identifier, value, type, loc);
14648 }
14649
14650 /* Parse a namespace-name.
14651
14652    namespace-name:
14653      original-namespace-name
14654      namespace-alias
14655
14656    Returns the NAMESPACE_DECL for the namespace.  */
14657
14658 static tree
14659 cp_parser_namespace_name (cp_parser* parser)
14660 {
14661   tree identifier;
14662   tree namespace_decl;
14663
14664   cp_token *token = cp_lexer_peek_token (parser->lexer);
14665
14666   /* Get the name of the namespace.  */
14667   identifier = cp_parser_identifier (parser);
14668   if (identifier == error_mark_node)
14669     return error_mark_node;
14670
14671   /* Look up the identifier in the currently active scope.  Look only
14672      for namespaces, due to:
14673
14674        [basic.lookup.udir]
14675
14676        When looking up a namespace-name in a using-directive or alias
14677        definition, only namespace names are considered.
14678
14679      And:
14680
14681        [basic.lookup.qual]
14682
14683        During the lookup of a name preceding the :: scope resolution
14684        operator, object, function, and enumerator names are ignored.
14685
14686      (Note that cp_parser_qualifying_entity only calls this
14687      function if the token after the name is the scope resolution
14688      operator.)  */
14689   namespace_decl = cp_parser_lookup_name (parser, identifier,
14690                                           none_type,
14691                                           /*is_template=*/false,
14692                                           /*is_namespace=*/true,
14693                                           /*check_dependency=*/true,
14694                                           /*ambiguous_decls=*/NULL,
14695                                           token->location);
14696   /* If it's not a namespace, issue an error.  */
14697   if (namespace_decl == error_mark_node
14698       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14699     {
14700       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14701         error_at (token->location, "%qD is not a namespace-name", identifier);
14702       cp_parser_error (parser, "expected namespace-name");
14703       namespace_decl = error_mark_node;
14704     }
14705
14706   return namespace_decl;
14707 }
14708
14709 /* Parse a namespace-definition.
14710
14711    namespace-definition:
14712      named-namespace-definition
14713      unnamed-namespace-definition
14714
14715    named-namespace-definition:
14716      original-namespace-definition
14717      extension-namespace-definition
14718
14719    original-namespace-definition:
14720      namespace identifier { namespace-body }
14721
14722    extension-namespace-definition:
14723      namespace original-namespace-name { namespace-body }
14724
14725    unnamed-namespace-definition:
14726      namespace { namespace-body } */
14727
14728 static void
14729 cp_parser_namespace_definition (cp_parser* parser)
14730 {
14731   tree identifier, attribs;
14732   bool has_visibility;
14733   bool is_inline;
14734
14735   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14736     {
14737       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14738       is_inline = true;
14739       cp_lexer_consume_token (parser->lexer);
14740     }
14741   else
14742     is_inline = false;
14743
14744   /* Look for the `namespace' keyword.  */
14745   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14746
14747   /* Get the name of the namespace.  We do not attempt to distinguish
14748      between an original-namespace-definition and an
14749      extension-namespace-definition at this point.  The semantic
14750      analysis routines are responsible for that.  */
14751   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14752     identifier = cp_parser_identifier (parser);
14753   else
14754     identifier = NULL_TREE;
14755
14756   /* Parse any specified attributes.  */
14757   attribs = cp_parser_attributes_opt (parser);
14758
14759   /* Look for the `{' to start the namespace.  */
14760   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14761   /* Start the namespace.  */
14762   push_namespace (identifier);
14763
14764   /* "inline namespace" is equivalent to a stub namespace definition
14765      followed by a strong using directive.  */
14766   if (is_inline)
14767     {
14768       tree name_space = current_namespace;
14769       /* Set up namespace association.  */
14770       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14771         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14772                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14773       /* Import the contents of the inline namespace.  */
14774       pop_namespace ();
14775       do_using_directive (name_space);
14776       push_namespace (identifier);
14777     }
14778
14779   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14780
14781   /* Parse the body of the namespace.  */
14782   cp_parser_namespace_body (parser);
14783
14784   if (has_visibility)
14785     pop_visibility (1);
14786
14787   /* Finish the namespace.  */
14788   pop_namespace ();
14789   /* Look for the final `}'.  */
14790   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14791 }
14792
14793 /* Parse a namespace-body.
14794
14795    namespace-body:
14796      declaration-seq [opt]  */
14797
14798 static void
14799 cp_parser_namespace_body (cp_parser* parser)
14800 {
14801   cp_parser_declaration_seq_opt (parser);
14802 }
14803
14804 /* Parse a namespace-alias-definition.
14805
14806    namespace-alias-definition:
14807      namespace identifier = qualified-namespace-specifier ;  */
14808
14809 static void
14810 cp_parser_namespace_alias_definition (cp_parser* parser)
14811 {
14812   tree identifier;
14813   tree namespace_specifier;
14814
14815   cp_token *token = cp_lexer_peek_token (parser->lexer);
14816
14817   /* Look for the `namespace' keyword.  */
14818   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14819   /* Look for the identifier.  */
14820   identifier = cp_parser_identifier (parser);
14821   if (identifier == error_mark_node)
14822     return;
14823   /* Look for the `=' token.  */
14824   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14825       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14826     {
14827       error_at (token->location, "%<namespace%> definition is not allowed here");
14828       /* Skip the definition.  */
14829       cp_lexer_consume_token (parser->lexer);
14830       if (cp_parser_skip_to_closing_brace (parser))
14831         cp_lexer_consume_token (parser->lexer);
14832       return;
14833     }
14834   cp_parser_require (parser, CPP_EQ, RT_EQ);
14835   /* Look for the qualified-namespace-specifier.  */
14836   namespace_specifier
14837     = cp_parser_qualified_namespace_specifier (parser);
14838   /* Look for the `;' token.  */
14839   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14840
14841   /* Register the alias in the symbol table.  */
14842   do_namespace_alias (identifier, namespace_specifier);
14843 }
14844
14845 /* Parse a qualified-namespace-specifier.
14846
14847    qualified-namespace-specifier:
14848      :: [opt] nested-name-specifier [opt] namespace-name
14849
14850    Returns a NAMESPACE_DECL corresponding to the specified
14851    namespace.  */
14852
14853 static tree
14854 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14855 {
14856   /* Look for the optional `::'.  */
14857   cp_parser_global_scope_opt (parser,
14858                               /*current_scope_valid_p=*/false);
14859
14860   /* Look for the optional nested-name-specifier.  */
14861   cp_parser_nested_name_specifier_opt (parser,
14862                                        /*typename_keyword_p=*/false,
14863                                        /*check_dependency_p=*/true,
14864                                        /*type_p=*/false,
14865                                        /*is_declaration=*/true);
14866
14867   return cp_parser_namespace_name (parser);
14868 }
14869
14870 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14871    access declaration.
14872
14873    using-declaration:
14874      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14875      using :: unqualified-id ;  
14876
14877    access-declaration:
14878      qualified-id ;  
14879
14880    */
14881
14882 static bool
14883 cp_parser_using_declaration (cp_parser* parser, 
14884                              bool access_declaration_p)
14885 {
14886   cp_token *token;
14887   bool typename_p = false;
14888   bool global_scope_p;
14889   tree decl;
14890   tree identifier;
14891   tree qscope;
14892   int oldcount = errorcount;
14893   cp_token *diag_token = NULL;
14894
14895   if (access_declaration_p)
14896     {
14897       diag_token = cp_lexer_peek_token (parser->lexer);
14898       cp_parser_parse_tentatively (parser);
14899     }
14900   else
14901     {
14902       /* Look for the `using' keyword.  */
14903       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14904       
14905       /* Peek at the next token.  */
14906       token = cp_lexer_peek_token (parser->lexer);
14907       /* See if it's `typename'.  */
14908       if (token->keyword == RID_TYPENAME)
14909         {
14910           /* Remember that we've seen it.  */
14911           typename_p = true;
14912           /* Consume the `typename' token.  */
14913           cp_lexer_consume_token (parser->lexer);
14914         }
14915     }
14916
14917   /* Look for the optional global scope qualification.  */
14918   global_scope_p
14919     = (cp_parser_global_scope_opt (parser,
14920                                    /*current_scope_valid_p=*/false)
14921        != NULL_TREE);
14922
14923   /* If we saw `typename', or didn't see `::', then there must be a
14924      nested-name-specifier present.  */
14925   if (typename_p || !global_scope_p)
14926     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14927                                               /*check_dependency_p=*/true,
14928                                               /*type_p=*/false,
14929                                               /*is_declaration=*/true);
14930   /* Otherwise, we could be in either of the two productions.  In that
14931      case, treat the nested-name-specifier as optional.  */
14932   else
14933     qscope = cp_parser_nested_name_specifier_opt (parser,
14934                                                   /*typename_keyword_p=*/false,
14935                                                   /*check_dependency_p=*/true,
14936                                                   /*type_p=*/false,
14937                                                   /*is_declaration=*/true);
14938   if (!qscope)
14939     qscope = global_namespace;
14940
14941   if (access_declaration_p && cp_parser_error_occurred (parser))
14942     /* Something has already gone wrong; there's no need to parse
14943        further.  Since an error has occurred, the return value of
14944        cp_parser_parse_definitely will be false, as required.  */
14945     return cp_parser_parse_definitely (parser);
14946
14947   token = cp_lexer_peek_token (parser->lexer);
14948   /* Parse the unqualified-id.  */
14949   identifier = cp_parser_unqualified_id (parser,
14950                                          /*template_keyword_p=*/false,
14951                                          /*check_dependency_p=*/true,
14952                                          /*declarator_p=*/true,
14953                                          /*optional_p=*/false);
14954
14955   if (access_declaration_p)
14956     {
14957       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14958         cp_parser_simulate_error (parser);
14959       if (!cp_parser_parse_definitely (parser))
14960         return false;
14961     }
14962
14963   /* The function we call to handle a using-declaration is different
14964      depending on what scope we are in.  */
14965   if (qscope == error_mark_node || identifier == error_mark_node)
14966     ;
14967   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14968            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14969     /* [namespace.udecl]
14970
14971        A using declaration shall not name a template-id.  */
14972     error_at (token->location,
14973               "a template-id may not appear in a using-declaration");
14974   else
14975     {
14976       if (at_class_scope_p ())
14977         {
14978           /* Create the USING_DECL.  */
14979           decl = do_class_using_decl (parser->scope, identifier);
14980
14981           if (decl && typename_p)
14982             USING_DECL_TYPENAME_P (decl) = 1;
14983
14984           if (check_for_bare_parameter_packs (decl))
14985             return false;
14986           else
14987             /* Add it to the list of members in this class.  */
14988             finish_member_declaration (decl);
14989         }
14990       else
14991         {
14992           decl = cp_parser_lookup_name_simple (parser,
14993                                                identifier,
14994                                                token->location);
14995           if (decl == error_mark_node)
14996             cp_parser_name_lookup_error (parser, identifier,
14997                                          decl, NLE_NULL,
14998                                          token->location);
14999           else if (check_for_bare_parameter_packs (decl))
15000             return false;
15001           else if (!at_namespace_scope_p ())
15002             do_local_using_decl (decl, qscope, identifier);
15003           else
15004             do_toplevel_using_decl (decl, qscope, identifier);
15005         }
15006     }
15007
15008   /* Look for the final `;'.  */
15009   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15010
15011   if (access_declaration_p && errorcount == oldcount)
15012     warning_at (diag_token->location, OPT_Wdeprecated,
15013                 "access declarations are deprecated "
15014                 "in favour of using-declarations; "
15015                 "suggestion: add the %<using%> keyword");
15016
15017   return true;
15018 }
15019
15020 /* Parse an alias-declaration.
15021
15022    alias-declaration:
15023      using identifier attribute-specifier-seq [opt] = type-id  */
15024
15025 static tree
15026 cp_parser_alias_declaration (cp_parser* parser)
15027 {
15028   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15029   location_t id_location;
15030   cp_declarator *declarator;
15031   cp_decl_specifier_seq decl_specs;
15032   bool member_p;
15033   const char *saved_message = NULL;
15034
15035   /* Look for the `using' keyword.  */
15036   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15037   id_location = cp_lexer_peek_token (parser->lexer)->location;
15038   id = cp_parser_identifier (parser);
15039   attributes = cp_parser_attributes_opt (parser);
15040   cp_parser_require (parser, CPP_EQ, RT_EQ);
15041
15042   /* Now we are going to parse the type-id of the declaration.  */
15043
15044   /*
15045     [dcl.type]/3 says:
15046
15047         "A type-specifier-seq shall not define a class or enumeration
15048          unless it appears in the type-id of an alias-declaration (7.1.3) that
15049          is not the declaration of a template-declaration."
15050
15051     In other words, if we currently are in an alias template, the
15052     type-id should not define a type.
15053
15054     So let's set parser->type_definition_forbidden_message in that
15055     case; cp_parser_check_type_definition (called by
15056     cp_parser_class_specifier) will then emit an error if a type is
15057     defined in the type-id.  */
15058   if (parser->num_template_parameter_lists)
15059     {
15060       saved_message = parser->type_definition_forbidden_message;
15061       parser->type_definition_forbidden_message =
15062         G_("types may not be defined in alias template declarations");
15063     }
15064
15065   type = cp_parser_type_id (parser);
15066
15067   /* Restore the error message if need be.  */
15068   if (parser->num_template_parameter_lists)
15069     parser->type_definition_forbidden_message = saved_message;
15070
15071   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15072
15073   if (cp_parser_error_occurred (parser))
15074     return error_mark_node;
15075
15076   /* A typedef-name can also be introduced by an alias-declaration. The
15077      identifier following the using keyword becomes a typedef-name. It has
15078      the same semantics as if it were introduced by the typedef
15079      specifier. In particular, it does not define a new type and it shall
15080      not appear in the type-id.  */
15081
15082   clear_decl_specs (&decl_specs);
15083   decl_specs.type = type;
15084   decl_specs.attributes = attributes;
15085   ++decl_specs.specs[(int) ds_typedef];
15086   ++decl_specs.specs[(int) ds_alias];
15087
15088   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15089   declarator->id_loc = id_location;
15090
15091   member_p = at_class_scope_p ();
15092   if (member_p)
15093     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15094                       NULL_TREE, attributes);
15095   else
15096     decl = start_decl (declarator, &decl_specs, 0,
15097                        attributes, NULL_TREE, &pushed_scope);
15098   if (decl == error_mark_node)
15099     return decl;
15100
15101   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15102
15103   if (pushed_scope)
15104     pop_scope (pushed_scope);
15105
15106   /* If decl is a template, return its TEMPLATE_DECL so that it gets
15107      added into the symbol table; otherwise, return the TYPE_DECL.  */
15108   if (DECL_LANG_SPECIFIC (decl)
15109       && DECL_TEMPLATE_INFO (decl)
15110       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15111     {
15112       decl = DECL_TI_TEMPLATE (decl);
15113       if (member_p)
15114         check_member_template (decl);
15115     }
15116
15117   return decl;
15118 }
15119
15120 /* Parse a using-directive.
15121
15122    using-directive:
15123      using namespace :: [opt] nested-name-specifier [opt]
15124        namespace-name ;  */
15125
15126 static void
15127 cp_parser_using_directive (cp_parser* parser)
15128 {
15129   tree namespace_decl;
15130   tree attribs;
15131
15132   /* Look for the `using' keyword.  */
15133   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15134   /* And the `namespace' keyword.  */
15135   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15136   /* Look for the optional `::' operator.  */
15137   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15138   /* And the optional nested-name-specifier.  */
15139   cp_parser_nested_name_specifier_opt (parser,
15140                                        /*typename_keyword_p=*/false,
15141                                        /*check_dependency_p=*/true,
15142                                        /*type_p=*/false,
15143                                        /*is_declaration=*/true);
15144   /* Get the namespace being used.  */
15145   namespace_decl = cp_parser_namespace_name (parser);
15146   /* And any specified attributes.  */
15147   attribs = cp_parser_attributes_opt (parser);
15148   /* Update the symbol table.  */
15149   parse_using_directive (namespace_decl, attribs);
15150   /* Look for the final `;'.  */
15151   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15152 }
15153
15154 /* Parse an asm-definition.
15155
15156    asm-definition:
15157      asm ( string-literal ) ;
15158
15159    GNU Extension:
15160
15161    asm-definition:
15162      asm volatile [opt] ( string-literal ) ;
15163      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15164      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15165                           : asm-operand-list [opt] ) ;
15166      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15167                           : asm-operand-list [opt]
15168                           : asm-clobber-list [opt] ) ;
15169      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15170                                : asm-clobber-list [opt]
15171                                : asm-goto-list ) ;  */
15172
15173 static void
15174 cp_parser_asm_definition (cp_parser* parser)
15175 {
15176   tree string;
15177   tree outputs = NULL_TREE;
15178   tree inputs = NULL_TREE;
15179   tree clobbers = NULL_TREE;
15180   tree labels = NULL_TREE;
15181   tree asm_stmt;
15182   bool volatile_p = false;
15183   bool extended_p = false;
15184   bool invalid_inputs_p = false;
15185   bool invalid_outputs_p = false;
15186   bool goto_p = false;
15187   required_token missing = RT_NONE;
15188
15189   /* Look for the `asm' keyword.  */
15190   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15191   /* See if the next token is `volatile'.  */
15192   if (cp_parser_allow_gnu_extensions_p (parser)
15193       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15194     {
15195       /* Remember that we saw the `volatile' keyword.  */
15196       volatile_p = true;
15197       /* Consume the token.  */
15198       cp_lexer_consume_token (parser->lexer);
15199     }
15200   if (cp_parser_allow_gnu_extensions_p (parser)
15201       && parser->in_function_body
15202       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15203     {
15204       /* Remember that we saw the `goto' keyword.  */
15205       goto_p = true;
15206       /* Consume the token.  */
15207       cp_lexer_consume_token (parser->lexer);
15208     }
15209   /* Look for the opening `('.  */
15210   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15211     return;
15212   /* Look for the string.  */
15213   string = cp_parser_string_literal (parser, false, false);
15214   if (string == error_mark_node)
15215     {
15216       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15217                                              /*consume_paren=*/true);
15218       return;
15219     }
15220
15221   /* If we're allowing GNU extensions, check for the extended assembly
15222      syntax.  Unfortunately, the `:' tokens need not be separated by
15223      a space in C, and so, for compatibility, we tolerate that here
15224      too.  Doing that means that we have to treat the `::' operator as
15225      two `:' tokens.  */
15226   if (cp_parser_allow_gnu_extensions_p (parser)
15227       && parser->in_function_body
15228       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15229           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15230     {
15231       bool inputs_p = false;
15232       bool clobbers_p = false;
15233       bool labels_p = false;
15234
15235       /* The extended syntax was used.  */
15236       extended_p = true;
15237
15238       /* Look for outputs.  */
15239       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15240         {
15241           /* Consume the `:'.  */
15242           cp_lexer_consume_token (parser->lexer);
15243           /* Parse the output-operands.  */
15244           if (cp_lexer_next_token_is_not (parser->lexer,
15245                                           CPP_COLON)
15246               && cp_lexer_next_token_is_not (parser->lexer,
15247                                              CPP_SCOPE)
15248               && cp_lexer_next_token_is_not (parser->lexer,
15249                                              CPP_CLOSE_PAREN)
15250               && !goto_p)
15251             outputs = cp_parser_asm_operand_list (parser);
15252
15253             if (outputs == error_mark_node)
15254               invalid_outputs_p = true;
15255         }
15256       /* If the next token is `::', there are no outputs, and the
15257          next token is the beginning of the inputs.  */
15258       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15259         /* The inputs are coming next.  */
15260         inputs_p = true;
15261
15262       /* Look for inputs.  */
15263       if (inputs_p
15264           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15265         {
15266           /* Consume the `:' or `::'.  */
15267           cp_lexer_consume_token (parser->lexer);
15268           /* Parse the output-operands.  */
15269           if (cp_lexer_next_token_is_not (parser->lexer,
15270                                           CPP_COLON)
15271               && cp_lexer_next_token_is_not (parser->lexer,
15272                                              CPP_SCOPE)
15273               && cp_lexer_next_token_is_not (parser->lexer,
15274                                              CPP_CLOSE_PAREN))
15275             inputs = cp_parser_asm_operand_list (parser);
15276
15277             if (inputs == error_mark_node)
15278               invalid_inputs_p = true;
15279         }
15280       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15281         /* The clobbers are coming next.  */
15282         clobbers_p = true;
15283
15284       /* Look for clobbers.  */
15285       if (clobbers_p
15286           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15287         {
15288           clobbers_p = true;
15289           /* Consume the `:' or `::'.  */
15290           cp_lexer_consume_token (parser->lexer);
15291           /* Parse the clobbers.  */
15292           if (cp_lexer_next_token_is_not (parser->lexer,
15293                                           CPP_COLON)
15294               && cp_lexer_next_token_is_not (parser->lexer,
15295                                              CPP_CLOSE_PAREN))
15296             clobbers = cp_parser_asm_clobber_list (parser);
15297         }
15298       else if (goto_p
15299                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15300         /* The labels are coming next.  */
15301         labels_p = true;
15302
15303       /* Look for labels.  */
15304       if (labels_p
15305           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15306         {
15307           labels_p = true;
15308           /* Consume the `:' or `::'.  */
15309           cp_lexer_consume_token (parser->lexer);
15310           /* Parse the labels.  */
15311           labels = cp_parser_asm_label_list (parser);
15312         }
15313
15314       if (goto_p && !labels_p)
15315         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15316     }
15317   else if (goto_p)
15318     missing = RT_COLON_SCOPE;
15319
15320   /* Look for the closing `)'.  */
15321   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15322                           missing ? missing : RT_CLOSE_PAREN))
15323     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15324                                            /*consume_paren=*/true);
15325   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15326
15327   if (!invalid_inputs_p && !invalid_outputs_p)
15328     {
15329       /* Create the ASM_EXPR.  */
15330       if (parser->in_function_body)
15331         {
15332           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15333                                       inputs, clobbers, labels);
15334           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15335           if (!extended_p)
15336             {
15337               tree temp = asm_stmt;
15338               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15339                 temp = TREE_OPERAND (temp, 0);
15340
15341               ASM_INPUT_P (temp) = 1;
15342             }
15343         }
15344       else
15345         cgraph_add_asm_node (string);
15346     }
15347 }
15348
15349 /* Declarators [gram.dcl.decl] */
15350
15351 /* Parse an init-declarator.
15352
15353    init-declarator:
15354      declarator initializer [opt]
15355
15356    GNU Extension:
15357
15358    init-declarator:
15359      declarator asm-specification [opt] attributes [opt] initializer [opt]
15360
15361    function-definition:
15362      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15363        function-body
15364      decl-specifier-seq [opt] declarator function-try-block
15365
15366    GNU Extension:
15367
15368    function-definition:
15369      __extension__ function-definition
15370
15371    TM Extension:
15372
15373    function-definition:
15374      decl-specifier-seq [opt] declarator function-transaction-block
15375
15376    The DECL_SPECIFIERS apply to this declarator.  Returns a
15377    representation of the entity declared.  If MEMBER_P is TRUE, then
15378    this declarator appears in a class scope.  The new DECL created by
15379    this declarator is returned.
15380
15381    The CHECKS are access checks that should be performed once we know
15382    what entity is being declared (and, therefore, what classes have
15383    befriended it).
15384
15385    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15386    for a function-definition here as well.  If the declarator is a
15387    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15388    be TRUE upon return.  By that point, the function-definition will
15389    have been completely parsed.
15390
15391    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15392    is FALSE.
15393
15394    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15395    parsed declaration if it is an uninitialized single declarator not followed
15396    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15397    if present, will not be consumed.  If returned, this declarator will be
15398    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15399
15400 static tree
15401 cp_parser_init_declarator (cp_parser* parser,
15402                            cp_decl_specifier_seq *decl_specifiers,
15403                            VEC (deferred_access_check,gc)* checks,
15404                            bool function_definition_allowed_p,
15405                            bool member_p,
15406                            int declares_class_or_enum,
15407                            bool* function_definition_p,
15408                            tree* maybe_range_for_decl)
15409 {
15410   cp_token *token = NULL, *asm_spec_start_token = NULL,
15411            *attributes_start_token = NULL;
15412   cp_declarator *declarator;
15413   tree prefix_attributes;
15414   tree attributes;
15415   tree asm_specification;
15416   tree initializer;
15417   tree decl = NULL_TREE;
15418   tree scope;
15419   int is_initialized;
15420   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15421      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15422      "(...)".  */
15423   enum cpp_ttype initialization_kind;
15424   bool is_direct_init = false;
15425   bool is_non_constant_init;
15426   int ctor_dtor_or_conv_p;
15427   bool friend_p;
15428   tree pushed_scope = NULL_TREE;
15429   bool range_for_decl_p = false;
15430
15431   /* Gather the attributes that were provided with the
15432      decl-specifiers.  */
15433   prefix_attributes = decl_specifiers->attributes;
15434
15435   /* Assume that this is not the declarator for a function
15436      definition.  */
15437   if (function_definition_p)
15438     *function_definition_p = false;
15439
15440   /* Defer access checks while parsing the declarator; we cannot know
15441      what names are accessible until we know what is being
15442      declared.  */
15443   resume_deferring_access_checks ();
15444
15445   /* Parse the declarator.  */
15446   token = cp_lexer_peek_token (parser->lexer);
15447   declarator
15448     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15449                             &ctor_dtor_or_conv_p,
15450                             /*parenthesized_p=*/NULL,
15451                             member_p);
15452   /* Gather up the deferred checks.  */
15453   stop_deferring_access_checks ();
15454
15455   /* If the DECLARATOR was erroneous, there's no need to go
15456      further.  */
15457   if (declarator == cp_error_declarator)
15458     return error_mark_node;
15459
15460   /* Check that the number of template-parameter-lists is OK.  */
15461   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15462                                                        token->location))
15463     return error_mark_node;
15464
15465   if (declares_class_or_enum & 2)
15466     cp_parser_check_for_definition_in_return_type (declarator,
15467                                                    decl_specifiers->type,
15468                                                    decl_specifiers->type_location);
15469
15470   /* Figure out what scope the entity declared by the DECLARATOR is
15471      located in.  `grokdeclarator' sometimes changes the scope, so
15472      we compute it now.  */
15473   scope = get_scope_of_declarator (declarator);
15474
15475   /* Perform any lookups in the declared type which were thought to be
15476      dependent, but are not in the scope of the declarator.  */
15477   decl_specifiers->type
15478     = maybe_update_decl_type (decl_specifiers->type, scope);
15479
15480   /* If we're allowing GNU extensions, look for an asm-specification
15481      and attributes.  */
15482   if (cp_parser_allow_gnu_extensions_p (parser))
15483     {
15484       /* Look for an asm-specification.  */
15485       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15486       asm_specification = cp_parser_asm_specification_opt (parser);
15487       /* And attributes.  */
15488       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15489       attributes = cp_parser_attributes_opt (parser);
15490     }
15491   else
15492     {
15493       asm_specification = NULL_TREE;
15494       attributes = NULL_TREE;
15495     }
15496
15497   /* Peek at the next token.  */
15498   token = cp_lexer_peek_token (parser->lexer);
15499   /* Check to see if the token indicates the start of a
15500      function-definition.  */
15501   if (function_declarator_p (declarator)
15502       && cp_parser_token_starts_function_definition_p (token))
15503     {
15504       if (!function_definition_allowed_p)
15505         {
15506           /* If a function-definition should not appear here, issue an
15507              error message.  */
15508           cp_parser_error (parser,
15509                            "a function-definition is not allowed here");
15510           return error_mark_node;
15511         }
15512       else
15513         {
15514           location_t func_brace_location
15515             = cp_lexer_peek_token (parser->lexer)->location;
15516
15517           /* Neither attributes nor an asm-specification are allowed
15518              on a function-definition.  */
15519           if (asm_specification)
15520             error_at (asm_spec_start_token->location,
15521                       "an asm-specification is not allowed "
15522                       "on a function-definition");
15523           if (attributes)
15524             error_at (attributes_start_token->location,
15525                       "attributes are not allowed on a function-definition");
15526           /* This is a function-definition.  */
15527           *function_definition_p = true;
15528
15529           /* Parse the function definition.  */
15530           if (member_p)
15531             decl = cp_parser_save_member_function_body (parser,
15532                                                         decl_specifiers,
15533                                                         declarator,
15534                                                         prefix_attributes);
15535           else
15536             decl
15537               = (cp_parser_function_definition_from_specifiers_and_declarator
15538                  (parser, decl_specifiers, prefix_attributes, declarator));
15539
15540           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15541             {
15542               /* This is where the prologue starts...  */
15543               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15544                 = func_brace_location;
15545             }
15546
15547           return decl;
15548         }
15549     }
15550
15551   /* [dcl.dcl]
15552
15553      Only in function declarations for constructors, destructors, and
15554      type conversions can the decl-specifier-seq be omitted.
15555
15556      We explicitly postpone this check past the point where we handle
15557      function-definitions because we tolerate function-definitions
15558      that are missing their return types in some modes.  */
15559   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15560     {
15561       cp_parser_error (parser,
15562                        "expected constructor, destructor, or type conversion");
15563       return error_mark_node;
15564     }
15565
15566   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15567   if (token->type == CPP_EQ
15568       || token->type == CPP_OPEN_PAREN
15569       || token->type == CPP_OPEN_BRACE)
15570     {
15571       is_initialized = SD_INITIALIZED;
15572       initialization_kind = token->type;
15573       if (maybe_range_for_decl)
15574         *maybe_range_for_decl = error_mark_node;
15575
15576       if (token->type == CPP_EQ
15577           && function_declarator_p (declarator))
15578         {
15579           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15580           if (t2->keyword == RID_DEFAULT)
15581             is_initialized = SD_DEFAULTED;
15582           else if (t2->keyword == RID_DELETE)
15583             is_initialized = SD_DELETED;
15584         }
15585     }
15586   else
15587     {
15588       /* If the init-declarator isn't initialized and isn't followed by a
15589          `,' or `;', it's not a valid init-declarator.  */
15590       if (token->type != CPP_COMMA
15591           && token->type != CPP_SEMICOLON)
15592         {
15593           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15594             range_for_decl_p = true;
15595           else
15596             {
15597               cp_parser_error (parser, "expected initializer");
15598               return error_mark_node;
15599             }
15600         }
15601       is_initialized = SD_UNINITIALIZED;
15602       initialization_kind = CPP_EOF;
15603     }
15604
15605   /* Because start_decl has side-effects, we should only call it if we
15606      know we're going ahead.  By this point, we know that we cannot
15607      possibly be looking at any other construct.  */
15608   cp_parser_commit_to_tentative_parse (parser);
15609
15610   /* If the decl specifiers were bad, issue an error now that we're
15611      sure this was intended to be a declarator.  Then continue
15612      declaring the variable(s), as int, to try to cut down on further
15613      errors.  */
15614   if (decl_specifiers->any_specifiers_p
15615       && decl_specifiers->type == error_mark_node)
15616     {
15617       cp_parser_error (parser, "invalid type in declaration");
15618       decl_specifiers->type = integer_type_node;
15619     }
15620
15621   /* Check to see whether or not this declaration is a friend.  */
15622   friend_p = cp_parser_friend_p (decl_specifiers);
15623
15624   /* Enter the newly declared entry in the symbol table.  If we're
15625      processing a declaration in a class-specifier, we wait until
15626      after processing the initializer.  */
15627   if (!member_p)
15628     {
15629       if (parser->in_unbraced_linkage_specification_p)
15630         decl_specifiers->storage_class = sc_extern;
15631       decl = start_decl (declarator, decl_specifiers,
15632                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15633                          attributes, prefix_attributes,
15634                          &pushed_scope);
15635       /* Adjust location of decl if declarator->id_loc is more appropriate:
15636          set, and decl wasn't merged with another decl, in which case its
15637          location would be different from input_location, and more accurate.  */
15638       if (DECL_P (decl)
15639           && declarator->id_loc != UNKNOWN_LOCATION
15640           && DECL_SOURCE_LOCATION (decl) == input_location)
15641         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15642     }
15643   else if (scope)
15644     /* Enter the SCOPE.  That way unqualified names appearing in the
15645        initializer will be looked up in SCOPE.  */
15646     pushed_scope = push_scope (scope);
15647
15648   /* Perform deferred access control checks, now that we know in which
15649      SCOPE the declared entity resides.  */
15650   if (!member_p && decl)
15651     {
15652       tree saved_current_function_decl = NULL_TREE;
15653
15654       /* If the entity being declared is a function, pretend that we
15655          are in its scope.  If it is a `friend', it may have access to
15656          things that would not otherwise be accessible.  */
15657       if (TREE_CODE (decl) == FUNCTION_DECL)
15658         {
15659           saved_current_function_decl = current_function_decl;
15660           current_function_decl = decl;
15661         }
15662
15663       /* Perform access checks for template parameters.  */
15664       cp_parser_perform_template_parameter_access_checks (checks);
15665
15666       /* Perform the access control checks for the declarator and the
15667          decl-specifiers.  */
15668       perform_deferred_access_checks ();
15669
15670       /* Restore the saved value.  */
15671       if (TREE_CODE (decl) == FUNCTION_DECL)
15672         current_function_decl = saved_current_function_decl;
15673     }
15674
15675   /* Parse the initializer.  */
15676   initializer = NULL_TREE;
15677   is_direct_init = false;
15678   is_non_constant_init = true;
15679   if (is_initialized)
15680     {
15681       if (function_declarator_p (declarator))
15682         {
15683           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15684            if (initialization_kind == CPP_EQ)
15685              initializer = cp_parser_pure_specifier (parser);
15686            else
15687              {
15688                /* If the declaration was erroneous, we don't really
15689                   know what the user intended, so just silently
15690                   consume the initializer.  */
15691                if (decl != error_mark_node)
15692                  error_at (initializer_start_token->location,
15693                            "initializer provided for function");
15694                cp_parser_skip_to_closing_parenthesis (parser,
15695                                                       /*recovering=*/true,
15696                                                       /*or_comma=*/false,
15697                                                       /*consume_paren=*/true);
15698              }
15699         }
15700       else
15701         {
15702           /* We want to record the extra mangling scope for in-class
15703              initializers of class members and initializers of static data
15704              member templates.  The former is a C++0x feature which isn't
15705              implemented yet, and I expect it will involve deferring
15706              parsing of the initializer until end of class as with default
15707              arguments.  So right here we only handle the latter.  */
15708           if (!member_p && processing_template_decl)
15709             start_lambda_scope (decl);
15710           initializer = cp_parser_initializer (parser,
15711                                                &is_direct_init,
15712                                                &is_non_constant_init);
15713           if (!member_p && processing_template_decl)
15714             finish_lambda_scope ();
15715         }
15716     }
15717
15718   /* The old parser allows attributes to appear after a parenthesized
15719      initializer.  Mark Mitchell proposed removing this functionality
15720      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15721      attributes -- but ignores them.  */
15722   if (cp_parser_allow_gnu_extensions_p (parser)
15723       && initialization_kind == CPP_OPEN_PAREN)
15724     if (cp_parser_attributes_opt (parser))
15725       warning (OPT_Wattributes,
15726                "attributes after parenthesized initializer ignored");
15727
15728   /* For an in-class declaration, use `grokfield' to create the
15729      declaration.  */
15730   if (member_p)
15731     {
15732       if (pushed_scope)
15733         {
15734           pop_scope (pushed_scope);
15735           pushed_scope = NULL_TREE;
15736         }
15737       decl = grokfield (declarator, decl_specifiers,
15738                         initializer, !is_non_constant_init,
15739                         /*asmspec=*/NULL_TREE,
15740                         prefix_attributes);
15741       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15742         cp_parser_save_default_args (parser, decl);
15743     }
15744
15745   /* Finish processing the declaration.  But, skip member
15746      declarations.  */
15747   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15748     {
15749       cp_finish_decl (decl,
15750                       initializer, !is_non_constant_init,
15751                       asm_specification,
15752                       /* If the initializer is in parentheses, then this is
15753                          a direct-initialization, which means that an
15754                          `explicit' constructor is OK.  Otherwise, an
15755                          `explicit' constructor cannot be used.  */
15756                       ((is_direct_init || !is_initialized)
15757                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15758     }
15759   else if ((cxx_dialect != cxx98) && friend_p
15760            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15761     /* Core issue #226 (C++0x only): A default template-argument
15762        shall not be specified in a friend class template
15763        declaration. */
15764     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15765                              /*is_partial=*/0, /*is_friend_decl=*/1);
15766
15767   if (!friend_p && pushed_scope)
15768     pop_scope (pushed_scope);
15769
15770   return decl;
15771 }
15772
15773 /* Parse a declarator.
15774
15775    declarator:
15776      direct-declarator
15777      ptr-operator declarator
15778
15779    abstract-declarator:
15780      ptr-operator abstract-declarator [opt]
15781      direct-abstract-declarator
15782
15783    GNU Extensions:
15784
15785    declarator:
15786      attributes [opt] direct-declarator
15787      attributes [opt] ptr-operator declarator
15788
15789    abstract-declarator:
15790      attributes [opt] ptr-operator abstract-declarator [opt]
15791      attributes [opt] direct-abstract-declarator
15792
15793    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15794    detect constructor, destructor or conversion operators. It is set
15795    to -1 if the declarator is a name, and +1 if it is a
15796    function. Otherwise it is set to zero. Usually you just want to
15797    test for >0, but internally the negative value is used.
15798
15799    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15800    a decl-specifier-seq unless it declares a constructor, destructor,
15801    or conversion.  It might seem that we could check this condition in
15802    semantic analysis, rather than parsing, but that makes it difficult
15803    to handle something like `f()'.  We want to notice that there are
15804    no decl-specifiers, and therefore realize that this is an
15805    expression, not a declaration.)
15806
15807    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15808    the declarator is a direct-declarator of the form "(...)".
15809
15810    MEMBER_P is true iff this declarator is a member-declarator.  */
15811
15812 static cp_declarator *
15813 cp_parser_declarator (cp_parser* parser,
15814                       cp_parser_declarator_kind dcl_kind,
15815                       int* ctor_dtor_or_conv_p,
15816                       bool* parenthesized_p,
15817                       bool member_p)
15818 {
15819   cp_declarator *declarator;
15820   enum tree_code code;
15821   cp_cv_quals cv_quals;
15822   tree class_type;
15823   tree attributes = NULL_TREE;
15824
15825   /* Assume this is not a constructor, destructor, or type-conversion
15826      operator.  */
15827   if (ctor_dtor_or_conv_p)
15828     *ctor_dtor_or_conv_p = 0;
15829
15830   if (cp_parser_allow_gnu_extensions_p (parser))
15831     attributes = cp_parser_attributes_opt (parser);
15832
15833   /* Check for the ptr-operator production.  */
15834   cp_parser_parse_tentatively (parser);
15835   /* Parse the ptr-operator.  */
15836   code = cp_parser_ptr_operator (parser,
15837                                  &class_type,
15838                                  &cv_quals);
15839   /* If that worked, then we have a ptr-operator.  */
15840   if (cp_parser_parse_definitely (parser))
15841     {
15842       /* If a ptr-operator was found, then this declarator was not
15843          parenthesized.  */
15844       if (parenthesized_p)
15845         *parenthesized_p = true;
15846       /* The dependent declarator is optional if we are parsing an
15847          abstract-declarator.  */
15848       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15849         cp_parser_parse_tentatively (parser);
15850
15851       /* Parse the dependent declarator.  */
15852       declarator = cp_parser_declarator (parser, dcl_kind,
15853                                          /*ctor_dtor_or_conv_p=*/NULL,
15854                                          /*parenthesized_p=*/NULL,
15855                                          /*member_p=*/false);
15856
15857       /* If we are parsing an abstract-declarator, we must handle the
15858          case where the dependent declarator is absent.  */
15859       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15860           && !cp_parser_parse_definitely (parser))
15861         declarator = NULL;
15862
15863       declarator = cp_parser_make_indirect_declarator
15864         (code, class_type, cv_quals, declarator);
15865     }
15866   /* Everything else is a direct-declarator.  */
15867   else
15868     {
15869       if (parenthesized_p)
15870         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15871                                                    CPP_OPEN_PAREN);
15872       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15873                                                 ctor_dtor_or_conv_p,
15874                                                 member_p);
15875     }
15876
15877   if (attributes && declarator && declarator != cp_error_declarator)
15878     declarator->attributes = attributes;
15879
15880   return declarator;
15881 }
15882
15883 /* Parse a direct-declarator or direct-abstract-declarator.
15884
15885    direct-declarator:
15886      declarator-id
15887      direct-declarator ( parameter-declaration-clause )
15888        cv-qualifier-seq [opt]
15889        exception-specification [opt]
15890      direct-declarator [ constant-expression [opt] ]
15891      ( declarator )
15892
15893    direct-abstract-declarator:
15894      direct-abstract-declarator [opt]
15895        ( parameter-declaration-clause )
15896        cv-qualifier-seq [opt]
15897        exception-specification [opt]
15898      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15899      ( abstract-declarator )
15900
15901    Returns a representation of the declarator.  DCL_KIND is
15902    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15903    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15904    we are parsing a direct-declarator.  It is
15905    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15906    of ambiguity we prefer an abstract declarator, as per
15907    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15908    cp_parser_declarator.  */
15909
15910 static cp_declarator *
15911 cp_parser_direct_declarator (cp_parser* parser,
15912                              cp_parser_declarator_kind dcl_kind,
15913                              int* ctor_dtor_or_conv_p,
15914                              bool member_p)
15915 {
15916   cp_token *token;
15917   cp_declarator *declarator = NULL;
15918   tree scope = NULL_TREE;
15919   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15920   bool saved_in_declarator_p = parser->in_declarator_p;
15921   bool first = true;
15922   tree pushed_scope = NULL_TREE;
15923
15924   while (true)
15925     {
15926       /* Peek at the next token.  */
15927       token = cp_lexer_peek_token (parser->lexer);
15928       if (token->type == CPP_OPEN_PAREN)
15929         {
15930           /* This is either a parameter-declaration-clause, or a
15931              parenthesized declarator. When we know we are parsing a
15932              named declarator, it must be a parenthesized declarator
15933              if FIRST is true. For instance, `(int)' is a
15934              parameter-declaration-clause, with an omitted
15935              direct-abstract-declarator. But `((*))', is a
15936              parenthesized abstract declarator. Finally, when T is a
15937              template parameter `(T)' is a
15938              parameter-declaration-clause, and not a parenthesized
15939              named declarator.
15940
15941              We first try and parse a parameter-declaration-clause,
15942              and then try a nested declarator (if FIRST is true).
15943
15944              It is not an error for it not to be a
15945              parameter-declaration-clause, even when FIRST is
15946              false. Consider,
15947
15948                int i (int);
15949                int i (3);
15950
15951              The first is the declaration of a function while the
15952              second is the definition of a variable, including its
15953              initializer.
15954
15955              Having seen only the parenthesis, we cannot know which of
15956              these two alternatives should be selected.  Even more
15957              complex are examples like:
15958
15959                int i (int (a));
15960                int i (int (3));
15961
15962              The former is a function-declaration; the latter is a
15963              variable initialization.
15964
15965              Thus again, we try a parameter-declaration-clause, and if
15966              that fails, we back out and return.  */
15967
15968           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15969             {
15970               tree params;
15971               unsigned saved_num_template_parameter_lists;
15972               bool is_declarator = false;
15973               tree t;
15974
15975               /* In a member-declarator, the only valid interpretation
15976                  of a parenthesis is the start of a
15977                  parameter-declaration-clause.  (It is invalid to
15978                  initialize a static data member with a parenthesized
15979                  initializer; only the "=" form of initialization is
15980                  permitted.)  */
15981               if (!member_p)
15982                 cp_parser_parse_tentatively (parser);
15983
15984               /* Consume the `('.  */
15985               cp_lexer_consume_token (parser->lexer);
15986               if (first)
15987                 {
15988                   /* If this is going to be an abstract declarator, we're
15989                      in a declarator and we can't have default args.  */
15990                   parser->default_arg_ok_p = false;
15991                   parser->in_declarator_p = true;
15992                 }
15993
15994               /* Inside the function parameter list, surrounding
15995                  template-parameter-lists do not apply.  */
15996               saved_num_template_parameter_lists
15997                 = parser->num_template_parameter_lists;
15998               parser->num_template_parameter_lists = 0;
15999
16000               begin_scope (sk_function_parms, NULL_TREE);
16001
16002               /* Parse the parameter-declaration-clause.  */
16003               params = cp_parser_parameter_declaration_clause (parser);
16004
16005               parser->num_template_parameter_lists
16006                 = saved_num_template_parameter_lists;
16007
16008               /* Consume the `)'.  */
16009               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16010
16011               /* If all went well, parse the cv-qualifier-seq and the
16012                  exception-specification.  */
16013               if (member_p || cp_parser_parse_definitely (parser))
16014                 {
16015                   cp_cv_quals cv_quals;
16016                   cp_virt_specifiers virt_specifiers;
16017                   tree exception_specification;
16018                   tree late_return;
16019
16020                   is_declarator = true;
16021
16022                   if (ctor_dtor_or_conv_p)
16023                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16024                   first = false;
16025
16026                   /* Parse the cv-qualifier-seq.  */
16027                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16028                   /* And the exception-specification.  */
16029                   exception_specification
16030                     = cp_parser_exception_specification_opt (parser);
16031                   /* Parse the virt-specifier-seq.  */
16032                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16033
16034                   late_return = (cp_parser_late_return_type_opt
16035                                  (parser, member_p ? cv_quals : -1));
16036
16037                   /* Create the function-declarator.  */
16038                   declarator = make_call_declarator (declarator,
16039                                                      params,
16040                                                      cv_quals,
16041                                                      virt_specifiers,
16042                                                      exception_specification,
16043                                                      late_return);
16044                   /* Any subsequent parameter lists are to do with
16045                      return type, so are not those of the declared
16046                      function.  */
16047                   parser->default_arg_ok_p = false;
16048                 }
16049
16050               /* Remove the function parms from scope.  */
16051               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16052                 pop_binding (DECL_NAME (t), t);
16053               leave_scope();
16054
16055               if (is_declarator)
16056                 /* Repeat the main loop.  */
16057                 continue;
16058             }
16059
16060           /* If this is the first, we can try a parenthesized
16061              declarator.  */
16062           if (first)
16063             {
16064               bool saved_in_type_id_in_expr_p;
16065
16066               parser->default_arg_ok_p = saved_default_arg_ok_p;
16067               parser->in_declarator_p = saved_in_declarator_p;
16068
16069               /* Consume the `('.  */
16070               cp_lexer_consume_token (parser->lexer);
16071               /* Parse the nested declarator.  */
16072               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16073               parser->in_type_id_in_expr_p = true;
16074               declarator
16075                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16076                                         /*parenthesized_p=*/NULL,
16077                                         member_p);
16078               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16079               first = false;
16080               /* Expect a `)'.  */
16081               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16082                 declarator = cp_error_declarator;
16083               if (declarator == cp_error_declarator)
16084                 break;
16085
16086               goto handle_declarator;
16087             }
16088           /* Otherwise, we must be done.  */
16089           else
16090             break;
16091         }
16092       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16093                && token->type == CPP_OPEN_SQUARE)
16094         {
16095           /* Parse an array-declarator.  */
16096           tree bounds;
16097
16098           if (ctor_dtor_or_conv_p)
16099             *ctor_dtor_or_conv_p = 0;
16100
16101           first = false;
16102           parser->default_arg_ok_p = false;
16103           parser->in_declarator_p = true;
16104           /* Consume the `['.  */
16105           cp_lexer_consume_token (parser->lexer);
16106           /* Peek at the next token.  */
16107           token = cp_lexer_peek_token (parser->lexer);
16108           /* If the next token is `]', then there is no
16109              constant-expression.  */
16110           if (token->type != CPP_CLOSE_SQUARE)
16111             {
16112               bool non_constant_p;
16113
16114               bounds
16115                 = cp_parser_constant_expression (parser,
16116                                                  /*allow_non_constant=*/true,
16117                                                  &non_constant_p);
16118               if (!non_constant_p)
16119                 /* OK */;
16120               else if (error_operand_p (bounds))
16121                 /* Already gave an error.  */;
16122               else if (!parser->in_function_body
16123                        || current_binding_level->kind == sk_function_parms)
16124                 {
16125                   /* Normally, the array bound must be an integral constant
16126                      expression.  However, as an extension, we allow VLAs
16127                      in function scopes as long as they aren't part of a
16128                      parameter declaration.  */
16129                   cp_parser_error (parser,
16130                                    "array bound is not an integer constant");
16131                   bounds = error_mark_node;
16132                 }
16133               else if (processing_template_decl)
16134                 {
16135                   /* Remember this wasn't a constant-expression.  */
16136                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16137                   TREE_SIDE_EFFECTS (bounds) = 1;
16138                 }
16139             }
16140           else
16141             bounds = NULL_TREE;
16142           /* Look for the closing `]'.  */
16143           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16144             {
16145               declarator = cp_error_declarator;
16146               break;
16147             }
16148
16149           declarator = make_array_declarator (declarator, bounds);
16150         }
16151       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16152         {
16153           {
16154             tree qualifying_scope;
16155             tree unqualified_name;
16156             special_function_kind sfk;
16157             bool abstract_ok;
16158             bool pack_expansion_p = false;
16159             cp_token *declarator_id_start_token;
16160
16161             /* Parse a declarator-id */
16162             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16163             if (abstract_ok)
16164               {
16165                 cp_parser_parse_tentatively (parser);
16166
16167                 /* If we see an ellipsis, we should be looking at a
16168                    parameter pack. */
16169                 if (token->type == CPP_ELLIPSIS)
16170                   {
16171                     /* Consume the `...' */
16172                     cp_lexer_consume_token (parser->lexer);
16173
16174                     pack_expansion_p = true;
16175                   }
16176               }
16177
16178             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16179             unqualified_name
16180               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16181             qualifying_scope = parser->scope;
16182             if (abstract_ok)
16183               {
16184                 bool okay = false;
16185
16186                 if (!unqualified_name && pack_expansion_p)
16187                   {
16188                     /* Check whether an error occurred. */
16189                     okay = !cp_parser_error_occurred (parser);
16190
16191                     /* We already consumed the ellipsis to mark a
16192                        parameter pack, but we have no way to report it,
16193                        so abort the tentative parse. We will be exiting
16194                        immediately anyway. */
16195                     cp_parser_abort_tentative_parse (parser);
16196                   }
16197                 else
16198                   okay = cp_parser_parse_definitely (parser);
16199
16200                 if (!okay)
16201                   unqualified_name = error_mark_node;
16202                 else if (unqualified_name
16203                          && (qualifying_scope
16204                              || (TREE_CODE (unqualified_name)
16205                                  != IDENTIFIER_NODE)))
16206                   {
16207                     cp_parser_error (parser, "expected unqualified-id");
16208                     unqualified_name = error_mark_node;
16209                   }
16210               }
16211
16212             if (!unqualified_name)
16213               return NULL;
16214             if (unqualified_name == error_mark_node)
16215               {
16216                 declarator = cp_error_declarator;
16217                 pack_expansion_p = false;
16218                 declarator->parameter_pack_p = false;
16219                 break;
16220               }
16221
16222             if (qualifying_scope && at_namespace_scope_p ()
16223                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16224               {
16225                 /* In the declaration of a member of a template class
16226                    outside of the class itself, the SCOPE will sometimes
16227                    be a TYPENAME_TYPE.  For example, given:
16228
16229                    template <typename T>
16230                    int S<T>::R::i = 3;
16231
16232                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16233                    this context, we must resolve S<T>::R to an ordinary
16234                    type, rather than a typename type.
16235
16236                    The reason we normally avoid resolving TYPENAME_TYPEs
16237                    is that a specialization of `S' might render
16238                    `S<T>::R' not a type.  However, if `S' is
16239                    specialized, then this `i' will not be used, so there
16240                    is no harm in resolving the types here.  */
16241                 tree type;
16242
16243                 /* Resolve the TYPENAME_TYPE.  */
16244                 type = resolve_typename_type (qualifying_scope,
16245                                               /*only_current_p=*/false);
16246                 /* If that failed, the declarator is invalid.  */
16247                 if (TREE_CODE (type) == TYPENAME_TYPE)
16248                   {
16249                     if (typedef_variant_p (type))
16250                       error_at (declarator_id_start_token->location,
16251                                 "cannot define member of dependent typedef "
16252                                 "%qT", type);
16253                     else
16254                       error_at (declarator_id_start_token->location,
16255                                 "%<%T::%E%> is not a type",
16256                                 TYPE_CONTEXT (qualifying_scope),
16257                                 TYPE_IDENTIFIER (qualifying_scope));
16258                   }
16259                 qualifying_scope = type;
16260               }
16261
16262             sfk = sfk_none;
16263
16264             if (unqualified_name)
16265               {
16266                 tree class_type;
16267
16268                 if (qualifying_scope
16269                     && CLASS_TYPE_P (qualifying_scope))
16270                   class_type = qualifying_scope;
16271                 else
16272                   class_type = current_class_type;
16273
16274                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16275                   {
16276                     tree name_type = TREE_TYPE (unqualified_name);
16277                     if (class_type && same_type_p (name_type, class_type))
16278                       {
16279                         if (qualifying_scope
16280                             && CLASSTYPE_USE_TEMPLATE (name_type))
16281                           {
16282                             error_at (declarator_id_start_token->location,
16283                                       "invalid use of constructor as a template");
16284                             inform (declarator_id_start_token->location,
16285                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16286                                     "name the constructor in a qualified name",
16287                                     class_type,
16288                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16289                                     class_type, name_type);
16290                             declarator = cp_error_declarator;
16291                             break;
16292                           }
16293                         else
16294                           unqualified_name = constructor_name (class_type);
16295                       }
16296                     else
16297                       {
16298                         /* We do not attempt to print the declarator
16299                            here because we do not have enough
16300                            information about its original syntactic
16301                            form.  */
16302                         cp_parser_error (parser, "invalid declarator");
16303                         declarator = cp_error_declarator;
16304                         break;
16305                       }
16306                   }
16307
16308                 if (class_type)
16309                   {
16310                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16311                       sfk = sfk_destructor;
16312                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16313                       sfk = sfk_conversion;
16314                     else if (/* There's no way to declare a constructor
16315                                 for an anonymous type, even if the type
16316                                 got a name for linkage purposes.  */
16317                              !TYPE_WAS_ANONYMOUS (class_type)
16318                              && constructor_name_p (unqualified_name,
16319                                                     class_type))
16320                       {
16321                         unqualified_name = constructor_name (class_type);
16322                         sfk = sfk_constructor;
16323                       }
16324                     else if (is_overloaded_fn (unqualified_name)
16325                              && DECL_CONSTRUCTOR_P (get_first_fn
16326                                                     (unqualified_name)))
16327                       sfk = sfk_constructor;
16328
16329                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16330                       *ctor_dtor_or_conv_p = -1;
16331                   }
16332               }
16333             declarator = make_id_declarator (qualifying_scope,
16334                                              unqualified_name,
16335                                              sfk);
16336             declarator->id_loc = token->location;
16337             declarator->parameter_pack_p = pack_expansion_p;
16338
16339             if (pack_expansion_p)
16340               maybe_warn_variadic_templates ();
16341           }
16342
16343         handle_declarator:;
16344           scope = get_scope_of_declarator (declarator);
16345           if (scope)
16346             /* Any names that appear after the declarator-id for a
16347                member are looked up in the containing scope.  */
16348             pushed_scope = push_scope (scope);
16349           parser->in_declarator_p = true;
16350           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16351               || (declarator && declarator->kind == cdk_id))
16352             /* Default args are only allowed on function
16353                declarations.  */
16354             parser->default_arg_ok_p = saved_default_arg_ok_p;
16355           else
16356             parser->default_arg_ok_p = false;
16357
16358           first = false;
16359         }
16360       /* We're done.  */
16361       else
16362         break;
16363     }
16364
16365   /* For an abstract declarator, we might wind up with nothing at this
16366      point.  That's an error; the declarator is not optional.  */
16367   if (!declarator)
16368     cp_parser_error (parser, "expected declarator");
16369
16370   /* If we entered a scope, we must exit it now.  */
16371   if (pushed_scope)
16372     pop_scope (pushed_scope);
16373
16374   parser->default_arg_ok_p = saved_default_arg_ok_p;
16375   parser->in_declarator_p = saved_in_declarator_p;
16376
16377   return declarator;
16378 }
16379
16380 /* Parse a ptr-operator.
16381
16382    ptr-operator:
16383      * cv-qualifier-seq [opt]
16384      &
16385      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16386
16387    GNU Extension:
16388
16389    ptr-operator:
16390      & cv-qualifier-seq [opt]
16391
16392    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16393    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16394    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16395    filled in with the TYPE containing the member.  *CV_QUALS is
16396    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16397    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16398    Note that the tree codes returned by this function have nothing
16399    to do with the types of trees that will be eventually be created
16400    to represent the pointer or reference type being parsed. They are
16401    just constants with suggestive names. */
16402 static enum tree_code
16403 cp_parser_ptr_operator (cp_parser* parser,
16404                         tree* type,
16405                         cp_cv_quals *cv_quals)
16406 {
16407   enum tree_code code = ERROR_MARK;
16408   cp_token *token;
16409
16410   /* Assume that it's not a pointer-to-member.  */
16411   *type = NULL_TREE;
16412   /* And that there are no cv-qualifiers.  */
16413   *cv_quals = TYPE_UNQUALIFIED;
16414
16415   /* Peek at the next token.  */
16416   token = cp_lexer_peek_token (parser->lexer);
16417
16418   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16419   if (token->type == CPP_MULT)
16420     code = INDIRECT_REF;
16421   else if (token->type == CPP_AND)
16422     code = ADDR_EXPR;
16423   else if ((cxx_dialect != cxx98) &&
16424            token->type == CPP_AND_AND) /* C++0x only */
16425     code = NON_LVALUE_EXPR;
16426
16427   if (code != ERROR_MARK)
16428     {
16429       /* Consume the `*', `&' or `&&'.  */
16430       cp_lexer_consume_token (parser->lexer);
16431
16432       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16433          `&', if we are allowing GNU extensions.  (The only qualifier
16434          that can legally appear after `&' is `restrict', but that is
16435          enforced during semantic analysis.  */
16436       if (code == INDIRECT_REF
16437           || cp_parser_allow_gnu_extensions_p (parser))
16438         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16439     }
16440   else
16441     {
16442       /* Try the pointer-to-member case.  */
16443       cp_parser_parse_tentatively (parser);
16444       /* Look for the optional `::' operator.  */
16445       cp_parser_global_scope_opt (parser,
16446                                   /*current_scope_valid_p=*/false);
16447       /* Look for the nested-name specifier.  */
16448       token = cp_lexer_peek_token (parser->lexer);
16449       cp_parser_nested_name_specifier (parser,
16450                                        /*typename_keyword_p=*/false,
16451                                        /*check_dependency_p=*/true,
16452                                        /*type_p=*/false,
16453                                        /*is_declaration=*/false);
16454       /* If we found it, and the next token is a `*', then we are
16455          indeed looking at a pointer-to-member operator.  */
16456       if (!cp_parser_error_occurred (parser)
16457           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16458         {
16459           /* Indicate that the `*' operator was used.  */
16460           code = INDIRECT_REF;
16461
16462           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16463             error_at (token->location, "%qD is a namespace", parser->scope);
16464           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16465             error_at (token->location, "cannot form pointer to member of "
16466                       "non-class %q#T", parser->scope);
16467           else
16468             {
16469               /* The type of which the member is a member is given by the
16470                  current SCOPE.  */
16471               *type = parser->scope;
16472               /* The next name will not be qualified.  */
16473               parser->scope = NULL_TREE;
16474               parser->qualifying_scope = NULL_TREE;
16475               parser->object_scope = NULL_TREE;
16476               /* Look for the optional cv-qualifier-seq.  */
16477               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16478             }
16479         }
16480       /* If that didn't work we don't have a ptr-operator.  */
16481       if (!cp_parser_parse_definitely (parser))
16482         cp_parser_error (parser, "expected ptr-operator");
16483     }
16484
16485   return code;
16486 }
16487
16488 /* Parse an (optional) cv-qualifier-seq.
16489
16490    cv-qualifier-seq:
16491      cv-qualifier cv-qualifier-seq [opt]
16492
16493    cv-qualifier:
16494      const
16495      volatile
16496
16497    GNU Extension:
16498
16499    cv-qualifier:
16500      __restrict__
16501
16502    Returns a bitmask representing the cv-qualifiers.  */
16503
16504 static cp_cv_quals
16505 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16506 {
16507   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16508
16509   while (true)
16510     {
16511       cp_token *token;
16512       cp_cv_quals cv_qualifier;
16513
16514       /* Peek at the next token.  */
16515       token = cp_lexer_peek_token (parser->lexer);
16516       /* See if it's a cv-qualifier.  */
16517       switch (token->keyword)
16518         {
16519         case RID_CONST:
16520           cv_qualifier = TYPE_QUAL_CONST;
16521           break;
16522
16523         case RID_VOLATILE:
16524           cv_qualifier = TYPE_QUAL_VOLATILE;
16525           break;
16526
16527         case RID_RESTRICT:
16528           cv_qualifier = TYPE_QUAL_RESTRICT;
16529           break;
16530
16531         default:
16532           cv_qualifier = TYPE_UNQUALIFIED;
16533           break;
16534         }
16535
16536       if (!cv_qualifier)
16537         break;
16538
16539       if (cv_quals & cv_qualifier)
16540         {
16541           error_at (token->location, "duplicate cv-qualifier");
16542           cp_lexer_purge_token (parser->lexer);
16543         }
16544       else
16545         {
16546           cp_lexer_consume_token (parser->lexer);
16547           cv_quals |= cv_qualifier;
16548         }
16549     }
16550
16551   return cv_quals;
16552 }
16553
16554 /* Parse an (optional) virt-specifier-seq.
16555
16556    virt-specifier-seq:
16557      virt-specifier virt-specifier-seq [opt]
16558
16559    virt-specifier:
16560      override
16561      final
16562
16563    Returns a bitmask representing the virt-specifiers.  */
16564
16565 static cp_virt_specifiers
16566 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16567 {
16568   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16569
16570   while (true)
16571     {
16572       cp_token *token;
16573       cp_virt_specifiers virt_specifier;
16574
16575       /* Peek at the next token.  */
16576       token = cp_lexer_peek_token (parser->lexer);
16577       /* See if it's a virt-specifier-qualifier.  */
16578       if (token->type != CPP_NAME)
16579         break;
16580       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16581         {
16582           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16583           virt_specifier = VIRT_SPEC_OVERRIDE;
16584         }
16585       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16586         {
16587           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16588           virt_specifier = VIRT_SPEC_FINAL;
16589         }
16590       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16591         {
16592           virt_specifier = VIRT_SPEC_FINAL;
16593         }
16594       else
16595         break;
16596
16597       if (virt_specifiers & virt_specifier)
16598         {
16599           error_at (token->location, "duplicate virt-specifier");
16600           cp_lexer_purge_token (parser->lexer);
16601         }
16602       else
16603         {
16604           cp_lexer_consume_token (parser->lexer);
16605           virt_specifiers |= virt_specifier;
16606         }
16607     }
16608   return virt_specifiers;
16609 }
16610
16611 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16612    is in scope even though it isn't real.  */
16613
16614 static void
16615 inject_this_parameter (tree ctype, cp_cv_quals quals)
16616 {
16617   tree this_parm;
16618
16619   if (current_class_ptr)
16620     {
16621       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16622       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16623       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16624           && cp_type_quals (type) == quals)
16625         return;
16626     }
16627
16628   this_parm = build_this_parm (ctype, quals);
16629   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16630   current_class_ptr = NULL_TREE;
16631   current_class_ref
16632     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16633   current_class_ptr = this_parm;
16634 }
16635
16636 /* Parse a late-specified return type, if any.  This is not a separate
16637    non-terminal, but part of a function declarator, which looks like
16638
16639    -> trailing-type-specifier-seq abstract-declarator(opt)
16640
16641    Returns the type indicated by the type-id.
16642
16643    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16644    function.  */
16645
16646 static tree
16647 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16648 {
16649   cp_token *token;
16650   tree type;
16651
16652   /* Peek at the next token.  */
16653   token = cp_lexer_peek_token (parser->lexer);
16654   /* A late-specified return type is indicated by an initial '->'. */
16655   if (token->type != CPP_DEREF)
16656     return NULL_TREE;
16657
16658   /* Consume the ->.  */
16659   cp_lexer_consume_token (parser->lexer);
16660
16661   if (quals >= 0)
16662     {
16663       /* DR 1207: 'this' is in scope in the trailing return type.  */
16664       gcc_assert (current_class_ptr == NULL_TREE);
16665       inject_this_parameter (current_class_type, quals);
16666     }
16667
16668   type = cp_parser_trailing_type_id (parser);
16669
16670   if (quals >= 0)
16671     current_class_ptr = current_class_ref = NULL_TREE;
16672
16673   return type;
16674 }
16675
16676 /* Parse a declarator-id.
16677
16678    declarator-id:
16679      id-expression
16680      :: [opt] nested-name-specifier [opt] type-name
16681
16682    In the `id-expression' case, the value returned is as for
16683    cp_parser_id_expression if the id-expression was an unqualified-id.
16684    If the id-expression was a qualified-id, then a SCOPE_REF is
16685    returned.  The first operand is the scope (either a NAMESPACE_DECL
16686    or TREE_TYPE), but the second is still just a representation of an
16687    unqualified-id.  */
16688
16689 static tree
16690 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16691 {
16692   tree id;
16693   /* The expression must be an id-expression.  Assume that qualified
16694      names are the names of types so that:
16695
16696        template <class T>
16697        int S<T>::R::i = 3;
16698
16699      will work; we must treat `S<T>::R' as the name of a type.
16700      Similarly, assume that qualified names are templates, where
16701      required, so that:
16702
16703        template <class T>
16704        int S<T>::R<T>::i = 3;
16705
16706      will work, too.  */
16707   id = cp_parser_id_expression (parser,
16708                                 /*template_keyword_p=*/false,
16709                                 /*check_dependency_p=*/false,
16710                                 /*template_p=*/NULL,
16711                                 /*declarator_p=*/true,
16712                                 optional_p);
16713   if (id && BASELINK_P (id))
16714     id = BASELINK_FUNCTIONS (id);
16715   return id;
16716 }
16717
16718 /* Parse a type-id.
16719
16720    type-id:
16721      type-specifier-seq abstract-declarator [opt]
16722
16723    Returns the TYPE specified.  */
16724
16725 static tree
16726 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16727                      bool is_trailing_return)
16728 {
16729   cp_decl_specifier_seq type_specifier_seq;
16730   cp_declarator *abstract_declarator;
16731
16732   /* Parse the type-specifier-seq.  */
16733   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16734                                 is_trailing_return,
16735                                 &type_specifier_seq);
16736   if (type_specifier_seq.type == error_mark_node)
16737     return error_mark_node;
16738
16739   /* There might or might not be an abstract declarator.  */
16740   cp_parser_parse_tentatively (parser);
16741   /* Look for the declarator.  */
16742   abstract_declarator
16743     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16744                             /*parenthesized_p=*/NULL,
16745                             /*member_p=*/false);
16746   /* Check to see if there really was a declarator.  */
16747   if (!cp_parser_parse_definitely (parser))
16748     abstract_declarator = NULL;
16749
16750   if (type_specifier_seq.type
16751       && type_uses_auto (type_specifier_seq.type))
16752     {
16753       /* A type-id with type 'auto' is only ok if the abstract declarator
16754          is a function declarator with a late-specified return type.  */
16755       if (abstract_declarator
16756           && abstract_declarator->kind == cdk_function
16757           && abstract_declarator->u.function.late_return_type)
16758         /* OK */;
16759       else
16760         {
16761           error ("invalid use of %<auto%>");
16762           return error_mark_node;
16763         }
16764     }
16765   
16766   return groktypename (&type_specifier_seq, abstract_declarator,
16767                        is_template_arg);
16768 }
16769
16770 static tree cp_parser_type_id (cp_parser *parser)
16771 {
16772   return cp_parser_type_id_1 (parser, false, false);
16773 }
16774
16775 static tree cp_parser_template_type_arg (cp_parser *parser)
16776 {
16777   tree r;
16778   const char *saved_message = parser->type_definition_forbidden_message;
16779   parser->type_definition_forbidden_message
16780     = G_("types may not be defined in template arguments");
16781   r = cp_parser_type_id_1 (parser, true, false);
16782   parser->type_definition_forbidden_message = saved_message;
16783   return r;
16784 }
16785
16786 static tree cp_parser_trailing_type_id (cp_parser *parser)
16787 {
16788   return cp_parser_type_id_1 (parser, false, true);
16789 }
16790
16791 /* Parse a type-specifier-seq.
16792
16793    type-specifier-seq:
16794      type-specifier type-specifier-seq [opt]
16795
16796    GNU extension:
16797
16798    type-specifier-seq:
16799      attributes type-specifier-seq [opt]
16800
16801    If IS_DECLARATION is true, we are at the start of a "condition" or
16802    exception-declaration, so we might be followed by a declarator-id.
16803
16804    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16805    i.e. we've just seen "->".
16806
16807    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16808
16809 static void
16810 cp_parser_type_specifier_seq (cp_parser* parser,
16811                               bool is_declaration,
16812                               bool is_trailing_return,
16813                               cp_decl_specifier_seq *type_specifier_seq)
16814 {
16815   bool seen_type_specifier = false;
16816   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16817   cp_token *start_token = NULL;
16818
16819   /* Clear the TYPE_SPECIFIER_SEQ.  */
16820   clear_decl_specs (type_specifier_seq);
16821
16822   /* In the context of a trailing return type, enum E { } is an
16823      elaborated-type-specifier followed by a function-body, not an
16824      enum-specifier.  */
16825   if (is_trailing_return)
16826     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16827
16828   /* Parse the type-specifiers and attributes.  */
16829   while (true)
16830     {
16831       tree type_specifier;
16832       bool is_cv_qualifier;
16833
16834       /* Check for attributes first.  */
16835       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16836         {
16837           type_specifier_seq->attributes =
16838             chainon (type_specifier_seq->attributes,
16839                      cp_parser_attributes_opt (parser));
16840           continue;
16841         }
16842
16843       /* record the token of the beginning of the type specifier seq,
16844          for error reporting purposes*/
16845      if (!start_token)
16846        start_token = cp_lexer_peek_token (parser->lexer);
16847
16848       /* Look for the type-specifier.  */
16849       type_specifier = cp_parser_type_specifier (parser,
16850                                                  flags,
16851                                                  type_specifier_seq,
16852                                                  /*is_declaration=*/false,
16853                                                  NULL,
16854                                                  &is_cv_qualifier);
16855       if (!type_specifier)
16856         {
16857           /* If the first type-specifier could not be found, this is not a
16858              type-specifier-seq at all.  */
16859           if (!seen_type_specifier)
16860             {
16861               cp_parser_error (parser, "expected type-specifier");
16862               type_specifier_seq->type = error_mark_node;
16863               return;
16864             }
16865           /* If subsequent type-specifiers could not be found, the
16866              type-specifier-seq is complete.  */
16867           break;
16868         }
16869
16870       seen_type_specifier = true;
16871       /* The standard says that a condition can be:
16872
16873             type-specifier-seq declarator = assignment-expression
16874
16875          However, given:
16876
16877            struct S {};
16878            if (int S = ...)
16879
16880          we should treat the "S" as a declarator, not as a
16881          type-specifier.  The standard doesn't say that explicitly for
16882          type-specifier-seq, but it does say that for
16883          decl-specifier-seq in an ordinary declaration.  Perhaps it
16884          would be clearer just to allow a decl-specifier-seq here, and
16885          then add a semantic restriction that if any decl-specifiers
16886          that are not type-specifiers appear, the program is invalid.  */
16887       if (is_declaration && !is_cv_qualifier)
16888         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16889     }
16890
16891   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16892 }
16893
16894 /* Parse a parameter-declaration-clause.
16895
16896    parameter-declaration-clause:
16897      parameter-declaration-list [opt] ... [opt]
16898      parameter-declaration-list , ...
16899
16900    Returns a representation for the parameter declarations.  A return
16901    value of NULL indicates a parameter-declaration-clause consisting
16902    only of an ellipsis.  */
16903
16904 static tree
16905 cp_parser_parameter_declaration_clause (cp_parser* parser)
16906 {
16907   tree parameters;
16908   cp_token *token;
16909   bool ellipsis_p;
16910   bool is_error;
16911
16912   /* Peek at the next token.  */
16913   token = cp_lexer_peek_token (parser->lexer);
16914   /* Check for trivial parameter-declaration-clauses.  */
16915   if (token->type == CPP_ELLIPSIS)
16916     {
16917       /* Consume the `...' token.  */
16918       cp_lexer_consume_token (parser->lexer);
16919       return NULL_TREE;
16920     }
16921   else if (token->type == CPP_CLOSE_PAREN)
16922     /* There are no parameters.  */
16923     {
16924 #ifndef NO_IMPLICIT_EXTERN_C
16925       if (in_system_header && current_class_type == NULL
16926           && current_lang_name == lang_name_c)
16927         return NULL_TREE;
16928       else
16929 #endif
16930         return void_list_node;
16931     }
16932   /* Check for `(void)', too, which is a special case.  */
16933   else if (token->keyword == RID_VOID
16934            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16935                == CPP_CLOSE_PAREN))
16936     {
16937       /* Consume the `void' token.  */
16938       cp_lexer_consume_token (parser->lexer);
16939       /* There are no parameters.  */
16940       return void_list_node;
16941     }
16942
16943   /* Parse the parameter-declaration-list.  */
16944   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16945   /* If a parse error occurred while parsing the
16946      parameter-declaration-list, then the entire
16947      parameter-declaration-clause is erroneous.  */
16948   if (is_error)
16949     return NULL;
16950
16951   /* Peek at the next token.  */
16952   token = cp_lexer_peek_token (parser->lexer);
16953   /* If it's a `,', the clause should terminate with an ellipsis.  */
16954   if (token->type == CPP_COMMA)
16955     {
16956       /* Consume the `,'.  */
16957       cp_lexer_consume_token (parser->lexer);
16958       /* Expect an ellipsis.  */
16959       ellipsis_p
16960         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16961     }
16962   /* It might also be `...' if the optional trailing `,' was
16963      omitted.  */
16964   else if (token->type == CPP_ELLIPSIS)
16965     {
16966       /* Consume the `...' token.  */
16967       cp_lexer_consume_token (parser->lexer);
16968       /* And remember that we saw it.  */
16969       ellipsis_p = true;
16970     }
16971   else
16972     ellipsis_p = false;
16973
16974   /* Finish the parameter list.  */
16975   if (!ellipsis_p)
16976     parameters = chainon (parameters, void_list_node);
16977
16978   return parameters;
16979 }
16980
16981 /* Parse a parameter-declaration-list.
16982
16983    parameter-declaration-list:
16984      parameter-declaration
16985      parameter-declaration-list , parameter-declaration
16986
16987    Returns a representation of the parameter-declaration-list, as for
16988    cp_parser_parameter_declaration_clause.  However, the
16989    `void_list_node' is never appended to the list.  Upon return,
16990    *IS_ERROR will be true iff an error occurred.  */
16991
16992 static tree
16993 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16994 {
16995   tree parameters = NULL_TREE;
16996   tree *tail = &parameters; 
16997   bool saved_in_unbraced_linkage_specification_p;
16998   int index = 0;
16999
17000   /* Assume all will go well.  */
17001   *is_error = false;
17002   /* The special considerations that apply to a function within an
17003      unbraced linkage specifications do not apply to the parameters
17004      to the function.  */
17005   saved_in_unbraced_linkage_specification_p 
17006     = parser->in_unbraced_linkage_specification_p;
17007   parser->in_unbraced_linkage_specification_p = false;
17008
17009   /* Look for more parameters.  */
17010   while (true)
17011     {
17012       cp_parameter_declarator *parameter;
17013       tree decl = error_mark_node;
17014       bool parenthesized_p = false;
17015       /* Parse the parameter.  */
17016       parameter
17017         = cp_parser_parameter_declaration (parser,
17018                                            /*template_parm_p=*/false,
17019                                            &parenthesized_p);
17020
17021       /* We don't know yet if the enclosing context is deprecated, so wait
17022          and warn in grokparms if appropriate.  */
17023       deprecated_state = DEPRECATED_SUPPRESS;
17024
17025       if (parameter)
17026         decl = grokdeclarator (parameter->declarator,
17027                                &parameter->decl_specifiers,
17028                                PARM,
17029                                parameter->default_argument != NULL_TREE,
17030                                &parameter->decl_specifiers.attributes);
17031
17032       deprecated_state = DEPRECATED_NORMAL;
17033
17034       /* If a parse error occurred parsing the parameter declaration,
17035          then the entire parameter-declaration-list is erroneous.  */
17036       if (decl == error_mark_node)
17037         {
17038           *is_error = true;
17039           parameters = error_mark_node;
17040           break;
17041         }
17042
17043       if (parameter->decl_specifiers.attributes)
17044         cplus_decl_attributes (&decl,
17045                                parameter->decl_specifiers.attributes,
17046                                0);
17047       if (DECL_NAME (decl))
17048         decl = pushdecl (decl);
17049
17050       if (decl != error_mark_node)
17051         {
17052           retrofit_lang_decl (decl);
17053           DECL_PARM_INDEX (decl) = ++index;
17054           DECL_PARM_LEVEL (decl) = function_parm_depth ();
17055         }
17056
17057       /* Add the new parameter to the list.  */
17058       *tail = build_tree_list (parameter->default_argument, decl);
17059       tail = &TREE_CHAIN (*tail);
17060
17061       /* Peek at the next token.  */
17062       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17063           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17064           /* These are for Objective-C++ */
17065           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17066           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17067         /* The parameter-declaration-list is complete.  */
17068         break;
17069       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17070         {
17071           cp_token *token;
17072
17073           /* Peek at the next token.  */
17074           token = cp_lexer_peek_nth_token (parser->lexer, 2);
17075           /* If it's an ellipsis, then the list is complete.  */
17076           if (token->type == CPP_ELLIPSIS)
17077             break;
17078           /* Otherwise, there must be more parameters.  Consume the
17079              `,'.  */
17080           cp_lexer_consume_token (parser->lexer);
17081           /* When parsing something like:
17082
17083                 int i(float f, double d)
17084
17085              we can tell after seeing the declaration for "f" that we
17086              are not looking at an initialization of a variable "i",
17087              but rather at the declaration of a function "i".
17088
17089              Due to the fact that the parsing of template arguments
17090              (as specified to a template-id) requires backtracking we
17091              cannot use this technique when inside a template argument
17092              list.  */
17093           if (!parser->in_template_argument_list_p
17094               && !parser->in_type_id_in_expr_p
17095               && cp_parser_uncommitted_to_tentative_parse_p (parser)
17096               /* However, a parameter-declaration of the form
17097                  "foat(f)" (which is a valid declaration of a
17098                  parameter "f") can also be interpreted as an
17099                  expression (the conversion of "f" to "float").  */
17100               && !parenthesized_p)
17101             cp_parser_commit_to_tentative_parse (parser);
17102         }
17103       else
17104         {
17105           cp_parser_error (parser, "expected %<,%> or %<...%>");
17106           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17107             cp_parser_skip_to_closing_parenthesis (parser,
17108                                                    /*recovering=*/true,
17109                                                    /*or_comma=*/false,
17110                                                    /*consume_paren=*/false);
17111           break;
17112         }
17113     }
17114
17115   parser->in_unbraced_linkage_specification_p
17116     = saved_in_unbraced_linkage_specification_p;
17117
17118   return parameters;
17119 }
17120
17121 /* Parse a parameter declaration.
17122
17123    parameter-declaration:
17124      decl-specifier-seq ... [opt] declarator
17125      decl-specifier-seq declarator = assignment-expression
17126      decl-specifier-seq ... [opt] abstract-declarator [opt]
17127      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17128
17129    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17130    declares a template parameter.  (In that case, a non-nested `>'
17131    token encountered during the parsing of the assignment-expression
17132    is not interpreted as a greater-than operator.)
17133
17134    Returns a representation of the parameter, or NULL if an error
17135    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17136    true iff the declarator is of the form "(p)".  */
17137
17138 static cp_parameter_declarator *
17139 cp_parser_parameter_declaration (cp_parser *parser,
17140                                  bool template_parm_p,
17141                                  bool *parenthesized_p)
17142 {
17143   int declares_class_or_enum;
17144   cp_decl_specifier_seq decl_specifiers;
17145   cp_declarator *declarator;
17146   tree default_argument;
17147   cp_token *token = NULL, *declarator_token_start = NULL;
17148   const char *saved_message;
17149
17150   /* In a template parameter, `>' is not an operator.
17151
17152      [temp.param]
17153
17154      When parsing a default template-argument for a non-type
17155      template-parameter, the first non-nested `>' is taken as the end
17156      of the template parameter-list rather than a greater-than
17157      operator.  */
17158
17159   /* Type definitions may not appear in parameter types.  */
17160   saved_message = parser->type_definition_forbidden_message;
17161   parser->type_definition_forbidden_message
17162     = G_("types may not be defined in parameter types");
17163
17164   /* Parse the declaration-specifiers.  */
17165   cp_parser_decl_specifier_seq (parser,
17166                                 CP_PARSER_FLAGS_NONE,
17167                                 &decl_specifiers,
17168                                 &declares_class_or_enum);
17169
17170   /* Complain about missing 'typename' or other invalid type names.  */
17171   if (!decl_specifiers.any_type_specifiers_p)
17172     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17173
17174   /* If an error occurred, there's no reason to attempt to parse the
17175      rest of the declaration.  */
17176   if (cp_parser_error_occurred (parser))
17177     {
17178       parser->type_definition_forbidden_message = saved_message;
17179       return NULL;
17180     }
17181
17182   /* Peek at the next token.  */
17183   token = cp_lexer_peek_token (parser->lexer);
17184
17185   /* If the next token is a `)', `,', `=', `>', or `...', then there
17186      is no declarator. However, when variadic templates are enabled,
17187      there may be a declarator following `...'.  */
17188   if (token->type == CPP_CLOSE_PAREN
17189       || token->type == CPP_COMMA
17190       || token->type == CPP_EQ
17191       || token->type == CPP_GREATER)
17192     {
17193       declarator = NULL;
17194       if (parenthesized_p)
17195         *parenthesized_p = false;
17196     }
17197   /* Otherwise, there should be a declarator.  */
17198   else
17199     {
17200       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17201       parser->default_arg_ok_p = false;
17202
17203       /* After seeing a decl-specifier-seq, if the next token is not a
17204          "(", there is no possibility that the code is a valid
17205          expression.  Therefore, if parsing tentatively, we commit at
17206          this point.  */
17207       if (!parser->in_template_argument_list_p
17208           /* In an expression context, having seen:
17209
17210                (int((char ...
17211
17212              we cannot be sure whether we are looking at a
17213              function-type (taking a "char" as a parameter) or a cast
17214              of some object of type "char" to "int".  */
17215           && !parser->in_type_id_in_expr_p
17216           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17217           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17218           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17219         cp_parser_commit_to_tentative_parse (parser);
17220       /* Parse the declarator.  */
17221       declarator_token_start = token;
17222       declarator = cp_parser_declarator (parser,
17223                                          CP_PARSER_DECLARATOR_EITHER,
17224                                          /*ctor_dtor_or_conv_p=*/NULL,
17225                                          parenthesized_p,
17226                                          /*member_p=*/false);
17227       parser->default_arg_ok_p = saved_default_arg_ok_p;
17228       /* After the declarator, allow more attributes.  */
17229       decl_specifiers.attributes
17230         = chainon (decl_specifiers.attributes,
17231                    cp_parser_attributes_opt (parser));
17232     }
17233
17234   /* If the next token is an ellipsis, and we have not seen a
17235      declarator name, and the type of the declarator contains parameter
17236      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17237      a parameter pack expansion expression. Otherwise, leave the
17238      ellipsis for a C-style variadic function. */
17239   token = cp_lexer_peek_token (parser->lexer);
17240   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17241     {
17242       tree type = decl_specifiers.type;
17243
17244       if (type && DECL_P (type))
17245         type = TREE_TYPE (type);
17246
17247       if (type
17248           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17249           && declarator_can_be_parameter_pack (declarator)
17250           && (!declarator || !declarator->parameter_pack_p)
17251           && uses_parameter_packs (type))
17252         {
17253           /* Consume the `...'. */
17254           cp_lexer_consume_token (parser->lexer);
17255           maybe_warn_variadic_templates ();
17256           
17257           /* Build a pack expansion type */
17258           if (declarator)
17259             declarator->parameter_pack_p = true;
17260           else
17261             decl_specifiers.type = make_pack_expansion (type);
17262         }
17263     }
17264
17265   /* The restriction on defining new types applies only to the type
17266      of the parameter, not to the default argument.  */
17267   parser->type_definition_forbidden_message = saved_message;
17268
17269   /* If the next token is `=', then process a default argument.  */
17270   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17271     {
17272       token = cp_lexer_peek_token (parser->lexer);
17273       /* If we are defining a class, then the tokens that make up the
17274          default argument must be saved and processed later.  */
17275       if (!template_parm_p && at_class_scope_p ()
17276           && TYPE_BEING_DEFINED (current_class_type)
17277           && !LAMBDA_TYPE_P (current_class_type))
17278         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17279       /* Outside of a class definition, we can just parse the
17280          assignment-expression.  */
17281       else
17282         default_argument
17283           = cp_parser_default_argument (parser, template_parm_p);
17284
17285       if (!parser->default_arg_ok_p)
17286         {
17287           if (flag_permissive)
17288             warning (0, "deprecated use of default argument for parameter of non-function");
17289           else
17290             {
17291               error_at (token->location,
17292                         "default arguments are only "
17293                         "permitted for function parameters");
17294               default_argument = NULL_TREE;
17295             }
17296         }
17297       else if ((declarator && declarator->parameter_pack_p)
17298                || (decl_specifiers.type
17299                    && PACK_EXPANSION_P (decl_specifiers.type)))
17300         {
17301           /* Find the name of the parameter pack.  */     
17302           cp_declarator *id_declarator = declarator;
17303           while (id_declarator && id_declarator->kind != cdk_id)
17304             id_declarator = id_declarator->declarator;
17305           
17306           if (id_declarator && id_declarator->kind == cdk_id)
17307             error_at (declarator_token_start->location,
17308                       template_parm_p
17309                       ? G_("template parameter pack %qD "
17310                            "cannot have a default argument")
17311                       : G_("parameter pack %qD cannot have "
17312                            "a default argument"),
17313                       id_declarator->u.id.unqualified_name);
17314           else
17315             error_at (declarator_token_start->location,
17316                       template_parm_p
17317                       ? G_("template parameter pack cannot have "
17318                            "a default argument")
17319                       : G_("parameter pack cannot have a "
17320                            "default argument"));
17321
17322           default_argument = NULL_TREE;
17323         }
17324     }
17325   else
17326     default_argument = NULL_TREE;
17327
17328   return make_parameter_declarator (&decl_specifiers,
17329                                     declarator,
17330                                     default_argument);
17331 }
17332
17333 /* Parse a default argument and return it.
17334
17335    TEMPLATE_PARM_P is true if this is a default argument for a
17336    non-type template parameter.  */
17337 static tree
17338 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17339 {
17340   tree default_argument = NULL_TREE;
17341   bool saved_greater_than_is_operator_p;
17342   bool saved_local_variables_forbidden_p;
17343   bool non_constant_p, is_direct_init;
17344
17345   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17346      set correctly.  */
17347   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17348   parser->greater_than_is_operator_p = !template_parm_p;
17349   /* Local variable names (and the `this' keyword) may not
17350      appear in a default argument.  */
17351   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17352   parser->local_variables_forbidden_p = true;
17353   /* Parse the assignment-expression.  */
17354   if (template_parm_p)
17355     push_deferring_access_checks (dk_no_deferred);
17356   default_argument
17357     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17358   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17359     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17360   if (template_parm_p)
17361     pop_deferring_access_checks ();
17362   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17363   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17364
17365   return default_argument;
17366 }
17367
17368 /* Parse a function-body.
17369
17370    function-body:
17371      compound_statement  */
17372
17373 static void
17374 cp_parser_function_body (cp_parser *parser)
17375 {
17376   cp_parser_compound_statement (parser, NULL, false, true);
17377 }
17378
17379 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17380    true if a ctor-initializer was present.  */
17381
17382 static bool
17383 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17384 {
17385   tree body, list;
17386   bool ctor_initializer_p;
17387   const bool check_body_p =
17388      DECL_CONSTRUCTOR_P (current_function_decl)
17389      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17390   tree last = NULL;
17391
17392   /* Begin the function body.  */
17393   body = begin_function_body ();
17394   /* Parse the optional ctor-initializer.  */
17395   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17396
17397   /* If we're parsing a constexpr constructor definition, we need
17398      to check that the constructor body is indeed empty.  However,
17399      before we get to cp_parser_function_body lot of junk has been
17400      generated, so we can't just check that we have an empty block.
17401      Rather we take a snapshot of the outermost block, and check whether
17402      cp_parser_function_body changed its state.  */
17403   if (check_body_p)
17404     {
17405       list = body;
17406       if (TREE_CODE (list) == BIND_EXPR)
17407         list = BIND_EXPR_BODY (list);
17408       if (TREE_CODE (list) == STATEMENT_LIST
17409           && STATEMENT_LIST_TAIL (list) != NULL)
17410         last = STATEMENT_LIST_TAIL (list)->stmt;
17411     }
17412   /* Parse the function-body.  */
17413   cp_parser_function_body (parser);
17414   if (check_body_p)
17415     check_constexpr_ctor_body (last, list);
17416   /* Finish the function body.  */
17417   finish_function_body (body);
17418
17419   return ctor_initializer_p;
17420 }
17421
17422 /* Parse an initializer.
17423
17424    initializer:
17425      = initializer-clause
17426      ( expression-list )
17427
17428    Returns an expression representing the initializer.  If no
17429    initializer is present, NULL_TREE is returned.
17430
17431    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17432    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17433    set to TRUE if there is no initializer present.  If there is an
17434    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17435    is set to true; otherwise it is set to false.  */
17436
17437 static tree
17438 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17439                        bool* non_constant_p)
17440 {
17441   cp_token *token;
17442   tree init;
17443
17444   /* Peek at the next token.  */
17445   token = cp_lexer_peek_token (parser->lexer);
17446
17447   /* Let our caller know whether or not this initializer was
17448      parenthesized.  */
17449   *is_direct_init = (token->type != CPP_EQ);
17450   /* Assume that the initializer is constant.  */
17451   *non_constant_p = false;
17452
17453   if (token->type == CPP_EQ)
17454     {
17455       /* Consume the `='.  */
17456       cp_lexer_consume_token (parser->lexer);
17457       /* Parse the initializer-clause.  */
17458       init = cp_parser_initializer_clause (parser, non_constant_p);
17459     }
17460   else if (token->type == CPP_OPEN_PAREN)
17461     {
17462       VEC(tree,gc) *vec;
17463       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17464                                                      /*cast_p=*/false,
17465                                                      /*allow_expansion_p=*/true,
17466                                                      non_constant_p);
17467       if (vec == NULL)
17468         return error_mark_node;
17469       init = build_tree_list_vec (vec);
17470       release_tree_vector (vec);
17471     }
17472   else if (token->type == CPP_OPEN_BRACE)
17473     {
17474       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17475       init = cp_parser_braced_list (parser, non_constant_p);
17476       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17477     }
17478   else
17479     {
17480       /* Anything else is an error.  */
17481       cp_parser_error (parser, "expected initializer");
17482       init = error_mark_node;
17483     }
17484
17485   return init;
17486 }
17487
17488 /* Parse an initializer-clause.
17489
17490    initializer-clause:
17491      assignment-expression
17492      braced-init-list
17493
17494    Returns an expression representing the initializer.
17495
17496    If the `assignment-expression' production is used the value
17497    returned is simply a representation for the expression.
17498
17499    Otherwise, calls cp_parser_braced_list.  */
17500
17501 static tree
17502 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17503 {
17504   tree initializer;
17505
17506   /* Assume the expression is constant.  */
17507   *non_constant_p = false;
17508
17509   /* If it is not a `{', then we are looking at an
17510      assignment-expression.  */
17511   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17512     {
17513       initializer
17514         = cp_parser_constant_expression (parser,
17515                                         /*allow_non_constant_p=*/true,
17516                                         non_constant_p);
17517     }
17518   else
17519     initializer = cp_parser_braced_list (parser, non_constant_p);
17520
17521   return initializer;
17522 }
17523
17524 /* Parse a brace-enclosed initializer list.
17525
17526    braced-init-list:
17527      { initializer-list , [opt] }
17528      { }
17529
17530    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17531    the elements of the initializer-list (or NULL, if the last
17532    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17533    NULL_TREE.  There is no way to detect whether or not the optional
17534    trailing `,' was provided.  NON_CONSTANT_P is as for
17535    cp_parser_initializer.  */     
17536
17537 static tree
17538 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17539 {
17540   tree initializer;
17541
17542   /* Consume the `{' token.  */
17543   cp_lexer_consume_token (parser->lexer);
17544   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17545   initializer = make_node (CONSTRUCTOR);
17546   /* If it's not a `}', then there is a non-trivial initializer.  */
17547   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17548     {
17549       /* Parse the initializer list.  */
17550       CONSTRUCTOR_ELTS (initializer)
17551         = cp_parser_initializer_list (parser, non_constant_p);
17552       /* A trailing `,' token is allowed.  */
17553       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17554         cp_lexer_consume_token (parser->lexer);
17555     }
17556   /* Now, there should be a trailing `}'.  */
17557   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17558   TREE_TYPE (initializer) = init_list_type_node;
17559   return initializer;
17560 }
17561
17562 /* Parse an initializer-list.
17563
17564    initializer-list:
17565      initializer-clause ... [opt]
17566      initializer-list , initializer-clause ... [opt]
17567
17568    GNU Extension:
17569
17570    initializer-list:
17571      designation initializer-clause ...[opt]
17572      initializer-list , designation initializer-clause ...[opt]
17573
17574    designation:
17575      . identifier =
17576      identifier :
17577      [ constant-expression ] =
17578
17579    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17580    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17581    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17582    as for cp_parser_initializer.  */
17583
17584 static VEC(constructor_elt,gc) *
17585 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17586 {
17587   VEC(constructor_elt,gc) *v = NULL;
17588
17589   /* Assume all of the expressions are constant.  */
17590   *non_constant_p = false;
17591
17592   /* Parse the rest of the list.  */
17593   while (true)
17594     {
17595       cp_token *token;
17596       tree designator;
17597       tree initializer;
17598       bool clause_non_constant_p;
17599
17600       /* If the next token is an identifier and the following one is a
17601          colon, we are looking at the GNU designated-initializer
17602          syntax.  */
17603       if (cp_parser_allow_gnu_extensions_p (parser)
17604           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17605           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17606         {
17607           /* Warn the user that they are using an extension.  */
17608           pedwarn (input_location, OPT_pedantic, 
17609                    "ISO C++ does not allow designated initializers");
17610           /* Consume the identifier.  */
17611           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17612           /* Consume the `:'.  */
17613           cp_lexer_consume_token (parser->lexer);
17614         }
17615       /* Also handle the C99 syntax, '. id ='.  */
17616       else if (cp_parser_allow_gnu_extensions_p (parser)
17617                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17618                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17619                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17620         {
17621           /* Warn the user that they are using an extension.  */
17622           pedwarn (input_location, OPT_pedantic,
17623                    "ISO C++ does not allow C99 designated initializers");
17624           /* Consume the `.'.  */
17625           cp_lexer_consume_token (parser->lexer);
17626           /* Consume the identifier.  */
17627           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17628           /* Consume the `='.  */
17629           cp_lexer_consume_token (parser->lexer);
17630         }
17631       /* Also handle C99 array designators, '[ const ] ='.  */
17632       else if (cp_parser_allow_gnu_extensions_p (parser)
17633                && !c_dialect_objc ()
17634                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17635         {
17636           /* In C++11, [ could start a lambda-introducer.  */
17637           cp_parser_parse_tentatively (parser);
17638           cp_lexer_consume_token (parser->lexer);
17639           designator = cp_parser_constant_expression (parser, false, NULL);
17640           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17641           cp_parser_require (parser, CPP_EQ, RT_EQ);
17642           if (!cp_parser_parse_definitely (parser))
17643             designator = NULL_TREE;
17644         }
17645       else
17646         designator = NULL_TREE;
17647
17648       /* Parse the initializer.  */
17649       initializer = cp_parser_initializer_clause (parser,
17650                                                   &clause_non_constant_p);
17651       /* If any clause is non-constant, so is the entire initializer.  */
17652       if (clause_non_constant_p)
17653         *non_constant_p = true;
17654
17655       /* If we have an ellipsis, this is an initializer pack
17656          expansion.  */
17657       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17658         {
17659           /* Consume the `...'.  */
17660           cp_lexer_consume_token (parser->lexer);
17661
17662           /* Turn the initializer into an initializer expansion.  */
17663           initializer = make_pack_expansion (initializer);
17664         }
17665
17666       /* Add it to the vector.  */
17667       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17668
17669       /* If the next token is not a comma, we have reached the end of
17670          the list.  */
17671       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17672         break;
17673
17674       /* Peek at the next token.  */
17675       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17676       /* If the next token is a `}', then we're still done.  An
17677          initializer-clause can have a trailing `,' after the
17678          initializer-list and before the closing `}'.  */
17679       if (token->type == CPP_CLOSE_BRACE)
17680         break;
17681
17682       /* Consume the `,' token.  */
17683       cp_lexer_consume_token (parser->lexer);
17684     }
17685
17686   return v;
17687 }
17688
17689 /* Classes [gram.class] */
17690
17691 /* Parse a class-name.
17692
17693    class-name:
17694      identifier
17695      template-id
17696
17697    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17698    to indicate that names looked up in dependent types should be
17699    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17700    keyword has been used to indicate that the name that appears next
17701    is a template.  TAG_TYPE indicates the explicit tag given before
17702    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17703    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17704    is the class being defined in a class-head.
17705
17706    Returns the TYPE_DECL representing the class.  */
17707
17708 static tree
17709 cp_parser_class_name (cp_parser *parser,
17710                       bool typename_keyword_p,
17711                       bool template_keyword_p,
17712                       enum tag_types tag_type,
17713                       bool check_dependency_p,
17714                       bool class_head_p,
17715                       bool is_declaration)
17716 {
17717   tree decl;
17718   tree scope;
17719   bool typename_p;
17720   cp_token *token;
17721   tree identifier = NULL_TREE;
17722
17723   /* All class-names start with an identifier.  */
17724   token = cp_lexer_peek_token (parser->lexer);
17725   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17726     {
17727       cp_parser_error (parser, "expected class-name");
17728       return error_mark_node;
17729     }
17730
17731   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17732      to a template-id, so we save it here.  */
17733   scope = parser->scope;
17734   if (scope == error_mark_node)
17735     return error_mark_node;
17736
17737   /* Any name names a type if we're following the `typename' keyword
17738      in a qualified name where the enclosing scope is type-dependent.  */
17739   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17740                 && dependent_type_p (scope));
17741   /* Handle the common case (an identifier, but not a template-id)
17742      efficiently.  */
17743   if (token->type == CPP_NAME
17744       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17745     {
17746       cp_token *identifier_token;
17747       bool ambiguous_p;
17748
17749       /* Look for the identifier.  */
17750       identifier_token = cp_lexer_peek_token (parser->lexer);
17751       ambiguous_p = identifier_token->ambiguous_p;
17752       identifier = cp_parser_identifier (parser);
17753       /* If the next token isn't an identifier, we are certainly not
17754          looking at a class-name.  */
17755       if (identifier == error_mark_node)
17756         decl = error_mark_node;
17757       /* If we know this is a type-name, there's no need to look it
17758          up.  */
17759       else if (typename_p)
17760         decl = identifier;
17761       else
17762         {
17763           tree ambiguous_decls;
17764           /* If we already know that this lookup is ambiguous, then
17765              we've already issued an error message; there's no reason
17766              to check again.  */
17767           if (ambiguous_p)
17768             {
17769               cp_parser_simulate_error (parser);
17770               return error_mark_node;
17771             }
17772           /* If the next token is a `::', then the name must be a type
17773              name.
17774
17775              [basic.lookup.qual]
17776
17777              During the lookup for a name preceding the :: scope
17778              resolution operator, object, function, and enumerator
17779              names are ignored.  */
17780           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17781             tag_type = typename_type;
17782           /* Look up the name.  */
17783           decl = cp_parser_lookup_name (parser, identifier,
17784                                         tag_type,
17785                                         /*is_template=*/false,
17786                                         /*is_namespace=*/false,
17787                                         check_dependency_p,
17788                                         &ambiguous_decls,
17789                                         identifier_token->location);
17790           if (ambiguous_decls)
17791             {
17792               if (cp_parser_parsing_tentatively (parser))
17793                 cp_parser_simulate_error (parser);
17794               return error_mark_node;
17795             }
17796         }
17797     }
17798   else
17799     {
17800       /* Try a template-id.  */
17801       decl = cp_parser_template_id (parser, template_keyword_p,
17802                                     check_dependency_p,
17803                                     is_declaration);
17804       if (decl == error_mark_node)
17805         return error_mark_node;
17806     }
17807
17808   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17809
17810   /* If this is a typename, create a TYPENAME_TYPE.  */
17811   if (typename_p && decl != error_mark_node)
17812     {
17813       decl = make_typename_type (scope, decl, typename_type,
17814                                  /*complain=*/tf_error);
17815       if (decl != error_mark_node)
17816         decl = TYPE_NAME (decl);
17817     }
17818
17819   /* Check to see that it is really the name of a class.  */
17820   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17821       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17822       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17823     /* Situations like this:
17824
17825          template <typename T> struct A {
17826            typename T::template X<int>::I i;
17827          };
17828
17829        are problematic.  Is `T::template X<int>' a class-name?  The
17830        standard does not seem to be definitive, but there is no other
17831        valid interpretation of the following `::'.  Therefore, those
17832        names are considered class-names.  */
17833     {
17834       decl = make_typename_type (scope, decl, tag_type, tf_error);
17835       if (decl != error_mark_node)
17836         decl = TYPE_NAME (decl);
17837     }
17838   else if (TREE_CODE (decl) != TYPE_DECL
17839            || TREE_TYPE (decl) == error_mark_node
17840            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17841            /* In Objective-C 2.0, a classname followed by '.' starts a
17842               dot-syntax expression, and it's not a type-name.  */
17843            || (c_dialect_objc ()
17844                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17845                && objc_is_class_name (decl)))
17846     decl = error_mark_node;
17847
17848   if (decl == error_mark_node)
17849     cp_parser_error (parser, "expected class-name");
17850   else if (identifier && !parser->scope)
17851     maybe_note_name_used_in_class (identifier, decl);
17852
17853   return decl;
17854 }
17855
17856 /* Parse a class-specifier.
17857
17858    class-specifier:
17859      class-head { member-specification [opt] }
17860
17861    Returns the TREE_TYPE representing the class.  */
17862
17863 static tree
17864 cp_parser_class_specifier_1 (cp_parser* parser)
17865 {
17866   tree type;
17867   tree attributes = NULL_TREE;
17868   bool nested_name_specifier_p;
17869   unsigned saved_num_template_parameter_lists;
17870   bool saved_in_function_body;
17871   unsigned char in_statement;
17872   bool in_switch_statement_p;
17873   bool saved_in_unbraced_linkage_specification_p;
17874   tree old_scope = NULL_TREE;
17875   tree scope = NULL_TREE;
17876   tree bases;
17877   cp_token *closing_brace;
17878
17879   push_deferring_access_checks (dk_no_deferred);
17880
17881   /* Parse the class-head.  */
17882   type = cp_parser_class_head (parser,
17883                                &nested_name_specifier_p,
17884                                &attributes,
17885                                &bases);
17886   /* If the class-head was a semantic disaster, skip the entire body
17887      of the class.  */
17888   if (!type)
17889     {
17890       cp_parser_skip_to_end_of_block_or_statement (parser);
17891       pop_deferring_access_checks ();
17892       return error_mark_node;
17893     }
17894
17895   /* Look for the `{'.  */
17896   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17897     {
17898       pop_deferring_access_checks ();
17899       return error_mark_node;
17900     }
17901
17902   /* Process the base classes. If they're invalid, skip the 
17903      entire class body.  */
17904   if (!xref_basetypes (type, bases))
17905     {
17906       /* Consuming the closing brace yields better error messages
17907          later on.  */
17908       if (cp_parser_skip_to_closing_brace (parser))
17909         cp_lexer_consume_token (parser->lexer);
17910       pop_deferring_access_checks ();
17911       return error_mark_node;
17912     }
17913
17914   /* Issue an error message if type-definitions are forbidden here.  */
17915   cp_parser_check_type_definition (parser);
17916   /* Remember that we are defining one more class.  */
17917   ++parser->num_classes_being_defined;
17918   /* Inside the class, surrounding template-parameter-lists do not
17919      apply.  */
17920   saved_num_template_parameter_lists
17921     = parser->num_template_parameter_lists;
17922   parser->num_template_parameter_lists = 0;
17923   /* We are not in a function body.  */
17924   saved_in_function_body = parser->in_function_body;
17925   parser->in_function_body = false;
17926   /* Or in a loop.  */
17927   in_statement = parser->in_statement;
17928   parser->in_statement = 0;
17929   /* Or in a switch.  */
17930   in_switch_statement_p = parser->in_switch_statement_p;
17931   parser->in_switch_statement_p = false;
17932   /* We are not immediately inside an extern "lang" block.  */
17933   saved_in_unbraced_linkage_specification_p
17934     = parser->in_unbraced_linkage_specification_p;
17935   parser->in_unbraced_linkage_specification_p = false;
17936
17937   /* Start the class.  */
17938   if (nested_name_specifier_p)
17939     {
17940       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17941       old_scope = push_inner_scope (scope);
17942     }
17943   type = begin_class_definition (type, attributes);
17944
17945   if (type == error_mark_node)
17946     /* If the type is erroneous, skip the entire body of the class.  */
17947     cp_parser_skip_to_closing_brace (parser);
17948   else
17949     /* Parse the member-specification.  */
17950     cp_parser_member_specification_opt (parser);
17951
17952   /* Look for the trailing `}'.  */
17953   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17954   /* Look for trailing attributes to apply to this class.  */
17955   if (cp_parser_allow_gnu_extensions_p (parser))
17956     attributes = cp_parser_attributes_opt (parser);
17957   if (type != error_mark_node)
17958     type = finish_struct (type, attributes);
17959   if (nested_name_specifier_p)
17960     pop_inner_scope (old_scope, scope);
17961
17962   /* We've finished a type definition.  Check for the common syntax
17963      error of forgetting a semicolon after the definition.  We need to
17964      be careful, as we can't just check for not-a-semicolon and be done
17965      with it; the user might have typed:
17966
17967      class X { } c = ...;
17968      class X { } *p = ...;
17969
17970      and so forth.  Instead, enumerate all the possible tokens that
17971      might follow this production; if we don't see one of them, then
17972      complain and silently insert the semicolon.  */
17973   {
17974     cp_token *token = cp_lexer_peek_token (parser->lexer);
17975     bool want_semicolon = true;
17976
17977     switch (token->type)
17978       {
17979       case CPP_NAME:
17980       case CPP_SEMICOLON:
17981       case CPP_MULT:
17982       case CPP_AND:
17983       case CPP_OPEN_PAREN:
17984       case CPP_CLOSE_PAREN:
17985       case CPP_COMMA:
17986         want_semicolon = false;
17987         break;
17988
17989         /* While it's legal for type qualifiers and storage class
17990            specifiers to follow type definitions in the grammar, only
17991            compiler testsuites contain code like that.  Assume that if
17992            we see such code, then what we're really seeing is a case
17993            like:
17994
17995            class X { }
17996            const <type> var = ...;
17997
17998            or
17999
18000            class Y { }
18001            static <type> func (...) ...
18002
18003            i.e. the qualifier or specifier applies to the next
18004            declaration.  To do so, however, we need to look ahead one
18005            more token to see if *that* token is a type specifier.
18006
18007            This code could be improved to handle:
18008
18009            class Z { }
18010            static const <type> var = ...;  */
18011       case CPP_KEYWORD:
18012         if (keyword_is_decl_specifier (token->keyword))
18013           {
18014             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18015
18016             /* Handling user-defined types here would be nice, but very
18017                tricky.  */
18018             want_semicolon
18019               = (lookahead->type == CPP_KEYWORD
18020                  && keyword_begins_type_specifier (lookahead->keyword));
18021           }
18022         break;
18023       default:
18024         break;
18025       }
18026
18027     /* If we don't have a type, then something is very wrong and we
18028        shouldn't try to do anything clever.  Likewise for not seeing the
18029        closing brace.  */
18030     if (closing_brace && TYPE_P (type) && want_semicolon)
18031       {
18032         cp_token_position prev
18033           = cp_lexer_previous_token_position (parser->lexer);
18034         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18035         location_t loc = prev_token->location;
18036
18037         if (CLASSTYPE_DECLARED_CLASS (type))
18038           error_at (loc, "expected %<;%> after class definition");
18039         else if (TREE_CODE (type) == RECORD_TYPE)
18040           error_at (loc, "expected %<;%> after struct definition");
18041         else if (TREE_CODE (type) == UNION_TYPE)
18042           error_at (loc, "expected %<;%> after union definition");
18043         else
18044           gcc_unreachable ();
18045
18046         /* Unget one token and smash it to look as though we encountered
18047            a semicolon in the input stream.  */
18048         cp_lexer_set_token_position (parser->lexer, prev);
18049         token = cp_lexer_peek_token (parser->lexer);
18050         token->type = CPP_SEMICOLON;
18051         token->keyword = RID_MAX;
18052       }
18053   }
18054
18055   /* If this class is not itself within the scope of another class,
18056      then we need to parse the bodies of all of the queued function
18057      definitions.  Note that the queued functions defined in a class
18058      are not always processed immediately following the
18059      class-specifier for that class.  Consider:
18060
18061        struct A {
18062          struct B { void f() { sizeof (A); } };
18063        };
18064
18065      If `f' were processed before the processing of `A' were
18066      completed, there would be no way to compute the size of `A'.
18067      Note that the nesting we are interested in here is lexical --
18068      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18069      for:
18070
18071        struct A { struct B; };
18072        struct A::B { void f() { } };
18073
18074      there is no need to delay the parsing of `A::B::f'.  */
18075   if (--parser->num_classes_being_defined == 0)
18076     {
18077       tree decl;
18078       tree class_type = NULL_TREE;
18079       tree pushed_scope = NULL_TREE;
18080       unsigned ix;
18081       cp_default_arg_entry *e;
18082       tree save_ccp, save_ccr;
18083
18084       /* In a first pass, parse default arguments to the functions.
18085          Then, in a second pass, parse the bodies of the functions.
18086          This two-phased approach handles cases like:
18087
18088             struct S {
18089               void f() { g(); }
18090               void g(int i = 3);
18091             };
18092
18093          */
18094       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18095                         ix, e)
18096         {
18097           decl = e->decl;
18098           /* If there are default arguments that have not yet been processed,
18099              take care of them now.  */
18100           if (class_type != e->class_type)
18101             {
18102               if (pushed_scope)
18103                 pop_scope (pushed_scope);
18104               class_type = e->class_type;
18105               pushed_scope = push_scope (class_type);
18106             }
18107           /* Make sure that any template parameters are in scope.  */
18108           maybe_begin_member_template_processing (decl);
18109           /* Parse the default argument expressions.  */
18110           cp_parser_late_parsing_default_args (parser, decl);
18111           /* Remove any template parameters from the symbol table.  */
18112           maybe_end_member_template_processing ();
18113         }
18114       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18115       /* Now parse any NSDMIs.  */
18116       save_ccp = current_class_ptr;
18117       save_ccr = current_class_ref;
18118       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18119         {
18120           if (class_type != DECL_CONTEXT (decl))
18121             {
18122               if (pushed_scope)
18123                 pop_scope (pushed_scope);
18124               class_type = DECL_CONTEXT (decl);
18125               pushed_scope = push_scope (class_type);
18126             }
18127           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18128           cp_parser_late_parsing_nsdmi (parser, decl);
18129         }
18130       VEC_truncate (tree, unparsed_nsdmis, 0);
18131       current_class_ptr = save_ccp;
18132       current_class_ref = save_ccr;
18133       if (pushed_scope)
18134         pop_scope (pushed_scope);
18135       /* Now parse the body of the functions.  */
18136       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18137         cp_parser_late_parsing_for_member (parser, decl);
18138       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18139     }
18140
18141   /* Put back any saved access checks.  */
18142   pop_deferring_access_checks ();
18143
18144   /* Restore saved state.  */
18145   parser->in_switch_statement_p = in_switch_statement_p;
18146   parser->in_statement = in_statement;
18147   parser->in_function_body = saved_in_function_body;
18148   parser->num_template_parameter_lists
18149     = saved_num_template_parameter_lists;
18150   parser->in_unbraced_linkage_specification_p
18151     = saved_in_unbraced_linkage_specification_p;
18152
18153   return type;
18154 }
18155
18156 static tree
18157 cp_parser_class_specifier (cp_parser* parser)
18158 {
18159   tree ret;
18160   timevar_push (TV_PARSE_STRUCT);
18161   ret = cp_parser_class_specifier_1 (parser);
18162   timevar_pop (TV_PARSE_STRUCT);
18163   return ret;
18164 }
18165
18166 /* Parse a class-head.
18167
18168    class-head:
18169      class-key identifier [opt] base-clause [opt]
18170      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18171      class-key nested-name-specifier [opt] template-id
18172        base-clause [opt]
18173
18174    class-virt-specifier:
18175      final
18176
18177    GNU Extensions:
18178      class-key attributes identifier [opt] base-clause [opt]
18179      class-key attributes nested-name-specifier identifier base-clause [opt]
18180      class-key attributes nested-name-specifier [opt] template-id
18181        base-clause [opt]
18182
18183    Upon return BASES is initialized to the list of base classes (or
18184    NULL, if there are none) in the same form returned by
18185    cp_parser_base_clause.
18186
18187    Returns the TYPE of the indicated class.  Sets
18188    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18189    involving a nested-name-specifier was used, and FALSE otherwise.
18190
18191    Returns error_mark_node if this is not a class-head.
18192
18193    Returns NULL_TREE if the class-head is syntactically valid, but
18194    semantically invalid in a way that means we should skip the entire
18195    body of the class.  */
18196
18197 static tree
18198 cp_parser_class_head (cp_parser* parser,
18199                       bool* nested_name_specifier_p,
18200                       tree *attributes_p,
18201                       tree *bases)
18202 {
18203   tree nested_name_specifier;
18204   enum tag_types class_key;
18205   tree id = NULL_TREE;
18206   tree type = NULL_TREE;
18207   tree attributes;
18208   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18209   bool template_id_p = false;
18210   bool qualified_p = false;
18211   bool invalid_nested_name_p = false;
18212   bool invalid_explicit_specialization_p = false;
18213   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18214   tree pushed_scope = NULL_TREE;
18215   unsigned num_templates;
18216   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18217   /* Assume no nested-name-specifier will be present.  */
18218   *nested_name_specifier_p = false;
18219   /* Assume no template parameter lists will be used in defining the
18220      type.  */
18221   num_templates = 0;
18222   parser->colon_corrects_to_scope_p = false;
18223
18224   *bases = NULL_TREE;
18225
18226   /* Look for the class-key.  */
18227   class_key = cp_parser_class_key (parser);
18228   if (class_key == none_type)
18229     return error_mark_node;
18230
18231   /* Parse the attributes.  */
18232   attributes = cp_parser_attributes_opt (parser);
18233
18234   /* If the next token is `::', that is invalid -- but sometimes
18235      people do try to write:
18236
18237        struct ::S {};
18238
18239      Handle this gracefully by accepting the extra qualifier, and then
18240      issuing an error about it later if this really is a
18241      class-head.  If it turns out just to be an elaborated type
18242      specifier, remain silent.  */
18243   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18244     qualified_p = true;
18245
18246   push_deferring_access_checks (dk_no_check);
18247
18248   /* Determine the name of the class.  Begin by looking for an
18249      optional nested-name-specifier.  */
18250   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18251   nested_name_specifier
18252     = cp_parser_nested_name_specifier_opt (parser,
18253                                            /*typename_keyword_p=*/false,
18254                                            /*check_dependency_p=*/false,
18255                                            /*type_p=*/false,
18256                                            /*is_declaration=*/false);
18257   /* If there was a nested-name-specifier, then there *must* be an
18258      identifier.  */
18259   if (nested_name_specifier)
18260     {
18261       type_start_token = cp_lexer_peek_token (parser->lexer);
18262       /* Although the grammar says `identifier', it really means
18263          `class-name' or `template-name'.  You are only allowed to
18264          define a class that has already been declared with this
18265          syntax.
18266
18267          The proposed resolution for Core Issue 180 says that wherever
18268          you see `class T::X' you should treat `X' as a type-name.
18269
18270          It is OK to define an inaccessible class; for example:
18271
18272            class A { class B; };
18273            class A::B {};
18274
18275          We do not know if we will see a class-name, or a
18276          template-name.  We look for a class-name first, in case the
18277          class-name is a template-id; if we looked for the
18278          template-name first we would stop after the template-name.  */
18279       cp_parser_parse_tentatively (parser);
18280       type = cp_parser_class_name (parser,
18281                                    /*typename_keyword_p=*/false,
18282                                    /*template_keyword_p=*/false,
18283                                    class_type,
18284                                    /*check_dependency_p=*/false,
18285                                    /*class_head_p=*/true,
18286                                    /*is_declaration=*/false);
18287       /* If that didn't work, ignore the nested-name-specifier.  */
18288       if (!cp_parser_parse_definitely (parser))
18289         {
18290           invalid_nested_name_p = true;
18291           type_start_token = cp_lexer_peek_token (parser->lexer);
18292           id = cp_parser_identifier (parser);
18293           if (id == error_mark_node)
18294             id = NULL_TREE;
18295         }
18296       /* If we could not find a corresponding TYPE, treat this
18297          declaration like an unqualified declaration.  */
18298       if (type == error_mark_node)
18299         nested_name_specifier = NULL_TREE;
18300       /* Otherwise, count the number of templates used in TYPE and its
18301          containing scopes.  */
18302       else
18303         {
18304           tree scope;
18305
18306           for (scope = TREE_TYPE (type);
18307                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18308                scope = (TYPE_P (scope)
18309                         ? TYPE_CONTEXT (scope)
18310                         : DECL_CONTEXT (scope)))
18311             if (TYPE_P (scope)
18312                 && CLASS_TYPE_P (scope)
18313                 && CLASSTYPE_TEMPLATE_INFO (scope)
18314                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18315                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18316               ++num_templates;
18317         }
18318     }
18319   /* Otherwise, the identifier is optional.  */
18320   else
18321     {
18322       /* We don't know whether what comes next is a template-id,
18323          an identifier, or nothing at all.  */
18324       cp_parser_parse_tentatively (parser);
18325       /* Check for a template-id.  */
18326       type_start_token = cp_lexer_peek_token (parser->lexer);
18327       id = cp_parser_template_id (parser,
18328                                   /*template_keyword_p=*/false,
18329                                   /*check_dependency_p=*/true,
18330                                   /*is_declaration=*/true);
18331       /* If that didn't work, it could still be an identifier.  */
18332       if (!cp_parser_parse_definitely (parser))
18333         {
18334           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18335             {
18336               type_start_token = cp_lexer_peek_token (parser->lexer);
18337               id = cp_parser_identifier (parser);
18338             }
18339           else
18340             id = NULL_TREE;
18341         }
18342       else
18343         {
18344           template_id_p = true;
18345           ++num_templates;
18346         }
18347     }
18348
18349   pop_deferring_access_checks ();
18350
18351   if (id)
18352     {
18353       cp_parser_check_for_invalid_template_id (parser, id,
18354                                                type_start_token->location);
18355     }
18356   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18357
18358   /* If it's not a `:' or a `{' then we can't really be looking at a
18359      class-head, since a class-head only appears as part of a
18360      class-specifier.  We have to detect this situation before calling
18361      xref_tag, since that has irreversible side-effects.  */
18362   if (!cp_parser_next_token_starts_class_definition_p (parser))
18363     {
18364       cp_parser_error (parser, "expected %<{%> or %<:%>");
18365       type = error_mark_node;
18366       goto out;
18367     }
18368
18369   /* At this point, we're going ahead with the class-specifier, even
18370      if some other problem occurs.  */
18371   cp_parser_commit_to_tentative_parse (parser);
18372   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18373     {
18374       cp_parser_error (parser,
18375                        "cannot specify %<override%> for a class");
18376       type = error_mark_node;
18377       goto out;
18378     }
18379   /* Issue the error about the overly-qualified name now.  */
18380   if (qualified_p)
18381     {
18382       cp_parser_error (parser,
18383                        "global qualification of class name is invalid");
18384       type = error_mark_node;
18385       goto out;
18386     }
18387   else if (invalid_nested_name_p)
18388     {
18389       cp_parser_error (parser,
18390                        "qualified name does not name a class");
18391       type = error_mark_node;
18392       goto out;
18393     }
18394   else if (nested_name_specifier)
18395     {
18396       tree scope;
18397
18398       /* Reject typedef-names in class heads.  */
18399       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18400         {
18401           error_at (type_start_token->location,
18402                     "invalid class name in declaration of %qD",
18403                     type);
18404           type = NULL_TREE;
18405           goto done;
18406         }
18407
18408       /* Figure out in what scope the declaration is being placed.  */
18409       scope = current_scope ();
18410       /* If that scope does not contain the scope in which the
18411          class was originally declared, the program is invalid.  */
18412       if (scope && !is_ancestor (scope, nested_name_specifier))
18413         {
18414           if (at_namespace_scope_p ())
18415             error_at (type_start_token->location,
18416                       "declaration of %qD in namespace %qD which does not "
18417                       "enclose %qD",
18418                       type, scope, nested_name_specifier);
18419           else
18420             error_at (type_start_token->location,
18421                       "declaration of %qD in %qD which does not enclose %qD",
18422                       type, scope, nested_name_specifier);
18423           type = NULL_TREE;
18424           goto done;
18425         }
18426       /* [dcl.meaning]
18427
18428          A declarator-id shall not be qualified except for the
18429          definition of a ... nested class outside of its class
18430          ... [or] the definition or explicit instantiation of a
18431          class member of a namespace outside of its namespace.  */
18432       if (scope == nested_name_specifier)
18433         {
18434           permerror (nested_name_specifier_token_start->location,
18435                      "extra qualification not allowed");
18436           nested_name_specifier = NULL_TREE;
18437           num_templates = 0;
18438         }
18439     }
18440   /* An explicit-specialization must be preceded by "template <>".  If
18441      it is not, try to recover gracefully.  */
18442   if (at_namespace_scope_p ()
18443       && parser->num_template_parameter_lists == 0
18444       && template_id_p)
18445     {
18446       error_at (type_start_token->location,
18447                 "an explicit specialization must be preceded by %<template <>%>");
18448       invalid_explicit_specialization_p = true;
18449       /* Take the same action that would have been taken by
18450          cp_parser_explicit_specialization.  */
18451       ++parser->num_template_parameter_lists;
18452       begin_specialization ();
18453     }
18454   /* There must be no "return" statements between this point and the
18455      end of this function; set "type "to the correct return value and
18456      use "goto done;" to return.  */
18457   /* Make sure that the right number of template parameters were
18458      present.  */
18459   if (!cp_parser_check_template_parameters (parser, num_templates,
18460                                             type_start_token->location,
18461                                             /*declarator=*/NULL))
18462     {
18463       /* If something went wrong, there is no point in even trying to
18464          process the class-definition.  */
18465       type = NULL_TREE;
18466       goto done;
18467     }
18468
18469   /* Look up the type.  */
18470   if (template_id_p)
18471     {
18472       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18473           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18474               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18475         {
18476           error_at (type_start_token->location,
18477                     "function template %qD redeclared as a class template", id);
18478           type = error_mark_node;
18479         }
18480       else
18481         {
18482           type = TREE_TYPE (id);
18483           type = maybe_process_partial_specialization (type);
18484         }
18485       if (nested_name_specifier)
18486         pushed_scope = push_scope (nested_name_specifier);
18487     }
18488   else if (nested_name_specifier)
18489     {
18490       tree class_type;
18491
18492       /* Given:
18493
18494             template <typename T> struct S { struct T };
18495             template <typename T> struct S<T>::T { };
18496
18497          we will get a TYPENAME_TYPE when processing the definition of
18498          `S::T'.  We need to resolve it to the actual type before we
18499          try to define it.  */
18500       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18501         {
18502           class_type = resolve_typename_type (TREE_TYPE (type),
18503                                               /*only_current_p=*/false);
18504           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18505             type = TYPE_NAME (class_type);
18506           else
18507             {
18508               cp_parser_error (parser, "could not resolve typename type");
18509               type = error_mark_node;
18510             }
18511         }
18512
18513       if (maybe_process_partial_specialization (TREE_TYPE (type))
18514           == error_mark_node)
18515         {
18516           type = NULL_TREE;
18517           goto done;
18518         }
18519
18520       class_type = current_class_type;
18521       /* Enter the scope indicated by the nested-name-specifier.  */
18522       pushed_scope = push_scope (nested_name_specifier);
18523       /* Get the canonical version of this type.  */
18524       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18525       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18526           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18527         {
18528           type = push_template_decl (type);
18529           if (type == error_mark_node)
18530             {
18531               type = NULL_TREE;
18532               goto done;
18533             }
18534         }
18535
18536       type = TREE_TYPE (type);
18537       *nested_name_specifier_p = true;
18538     }
18539   else      /* The name is not a nested name.  */
18540     {
18541       /* If the class was unnamed, create a dummy name.  */
18542       if (!id)
18543         id = make_anon_name ();
18544       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18545                        parser->num_template_parameter_lists);
18546     }
18547
18548   /* Indicate whether this class was declared as a `class' or as a
18549      `struct'.  */
18550   if (TREE_CODE (type) == RECORD_TYPE)
18551     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18552   cp_parser_check_class_key (class_key, type);
18553
18554   /* If this type was already complete, and we see another definition,
18555      that's an error.  */
18556   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18557     {
18558       error_at (type_start_token->location, "redefinition of %q#T",
18559                 type);
18560       error_at (type_start_token->location, "previous definition of %q+#T",
18561                 type);
18562       type = NULL_TREE;
18563       goto done;
18564     }
18565   else if (type == error_mark_node)
18566     type = NULL_TREE;
18567
18568   /* We will have entered the scope containing the class; the names of
18569      base classes should be looked up in that context.  For example:
18570
18571        struct A { struct B {}; struct C; };
18572        struct A::C : B {};
18573
18574      is valid.  */
18575
18576   /* Get the list of base-classes, if there is one.  */
18577   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18578     *bases = cp_parser_base_clause (parser);
18579
18580  done:
18581   /* Leave the scope given by the nested-name-specifier.  We will
18582      enter the class scope itself while processing the members.  */
18583   if (pushed_scope)
18584     pop_scope (pushed_scope);
18585
18586   if (invalid_explicit_specialization_p)
18587     {
18588       end_specialization ();
18589       --parser->num_template_parameter_lists;
18590     }
18591
18592   if (type)
18593     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18594   *attributes_p = attributes;
18595   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18596     CLASSTYPE_FINAL (type) = 1;
18597  out:
18598   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18599   return type;
18600 }
18601
18602 /* Parse a class-key.
18603
18604    class-key:
18605      class
18606      struct
18607      union
18608
18609    Returns the kind of class-key specified, or none_type to indicate
18610    error.  */
18611
18612 static enum tag_types
18613 cp_parser_class_key (cp_parser* parser)
18614 {
18615   cp_token *token;
18616   enum tag_types tag_type;
18617
18618   /* Look for the class-key.  */
18619   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18620   if (!token)
18621     return none_type;
18622
18623   /* Check to see if the TOKEN is a class-key.  */
18624   tag_type = cp_parser_token_is_class_key (token);
18625   if (!tag_type)
18626     cp_parser_error (parser, "expected class-key");
18627   return tag_type;
18628 }
18629
18630 /* Parse an (optional) member-specification.
18631
18632    member-specification:
18633      member-declaration member-specification [opt]
18634      access-specifier : member-specification [opt]  */
18635
18636 static void
18637 cp_parser_member_specification_opt (cp_parser* parser)
18638 {
18639   while (true)
18640     {
18641       cp_token *token;
18642       enum rid keyword;
18643
18644       /* Peek at the next token.  */
18645       token = cp_lexer_peek_token (parser->lexer);
18646       /* If it's a `}', or EOF then we've seen all the members.  */
18647       if (token->type == CPP_CLOSE_BRACE
18648           || token->type == CPP_EOF
18649           || token->type == CPP_PRAGMA_EOL)
18650         break;
18651
18652       /* See if this token is a keyword.  */
18653       keyword = token->keyword;
18654       switch (keyword)
18655         {
18656         case RID_PUBLIC:
18657         case RID_PROTECTED:
18658         case RID_PRIVATE:
18659           /* Consume the access-specifier.  */
18660           cp_lexer_consume_token (parser->lexer);
18661           /* Remember which access-specifier is active.  */
18662           current_access_specifier = token->u.value;
18663           /* Look for the `:'.  */
18664           cp_parser_require (parser, CPP_COLON, RT_COLON);
18665           break;
18666
18667         default:
18668           /* Accept #pragmas at class scope.  */
18669           if (token->type == CPP_PRAGMA)
18670             {
18671               cp_parser_pragma (parser, pragma_external);
18672               break;
18673             }
18674
18675           /* Otherwise, the next construction must be a
18676              member-declaration.  */
18677           cp_parser_member_declaration (parser);
18678         }
18679     }
18680 }
18681
18682 /* Parse a member-declaration.
18683
18684    member-declaration:
18685      decl-specifier-seq [opt] member-declarator-list [opt] ;
18686      function-definition ; [opt]
18687      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18688      using-declaration
18689      template-declaration
18690      alias-declaration
18691
18692    member-declarator-list:
18693      member-declarator
18694      member-declarator-list , member-declarator
18695
18696    member-declarator:
18697      declarator pure-specifier [opt]
18698      declarator constant-initializer [opt]
18699      identifier [opt] : constant-expression
18700
18701    GNU Extensions:
18702
18703    member-declaration:
18704      __extension__ member-declaration
18705
18706    member-declarator:
18707      declarator attributes [opt] pure-specifier [opt]
18708      declarator attributes [opt] constant-initializer [opt]
18709      identifier [opt] attributes [opt] : constant-expression  
18710
18711    C++0x Extensions:
18712
18713    member-declaration:
18714      static_assert-declaration  */
18715
18716 static void
18717 cp_parser_member_declaration (cp_parser* parser)
18718 {
18719   cp_decl_specifier_seq decl_specifiers;
18720   tree prefix_attributes;
18721   tree decl;
18722   int declares_class_or_enum;
18723   bool friend_p;
18724   cp_token *token = NULL;
18725   cp_token *decl_spec_token_start = NULL;
18726   cp_token *initializer_token_start = NULL;
18727   int saved_pedantic;
18728   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18729
18730   /* Check for the `__extension__' keyword.  */
18731   if (cp_parser_extension_opt (parser, &saved_pedantic))
18732     {
18733       /* Recurse.  */
18734       cp_parser_member_declaration (parser);
18735       /* Restore the old value of the PEDANTIC flag.  */
18736       pedantic = saved_pedantic;
18737
18738       return;
18739     }
18740
18741   /* Check for a template-declaration.  */
18742   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18743     {
18744       /* An explicit specialization here is an error condition, and we
18745          expect the specialization handler to detect and report this.  */
18746       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18747           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18748         cp_parser_explicit_specialization (parser);
18749       else
18750         cp_parser_template_declaration (parser, /*member_p=*/true);
18751
18752       return;
18753     }
18754
18755   /* Check for a using-declaration.  */
18756   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18757     {
18758       if (cxx_dialect < cxx0x)
18759         {
18760           /* Parse the using-declaration.  */
18761           cp_parser_using_declaration (parser,
18762                                        /*access_declaration_p=*/false);
18763           return;
18764         }
18765       else
18766         {
18767           tree decl;
18768           cp_parser_parse_tentatively (parser);
18769           decl = cp_parser_alias_declaration (parser);
18770           if (cp_parser_parse_definitely (parser))
18771             finish_member_declaration (decl);
18772           else
18773             cp_parser_using_declaration (parser,
18774                                          /*access_declaration_p=*/false);
18775           return;
18776         }
18777     }
18778
18779   /* Check for @defs.  */
18780   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18781     {
18782       tree ivar, member;
18783       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18784       ivar = ivar_chains;
18785       while (ivar)
18786         {
18787           member = ivar;
18788           ivar = TREE_CHAIN (member);
18789           TREE_CHAIN (member) = NULL_TREE;
18790           finish_member_declaration (member);
18791         }
18792       return;
18793     }
18794
18795   /* If the next token is `static_assert' we have a static assertion.  */
18796   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18797     {
18798       cp_parser_static_assert (parser, /*member_p=*/true);
18799       return;
18800     }
18801
18802   parser->colon_corrects_to_scope_p = false;
18803
18804   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18805       goto out;
18806
18807   /* Parse the decl-specifier-seq.  */
18808   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18809   cp_parser_decl_specifier_seq (parser,
18810                                 CP_PARSER_FLAGS_OPTIONAL,
18811                                 &decl_specifiers,
18812                                 &declares_class_or_enum);
18813   prefix_attributes = decl_specifiers.attributes;
18814   decl_specifiers.attributes = NULL_TREE;
18815   /* Check for an invalid type-name.  */
18816   if (!decl_specifiers.any_type_specifiers_p
18817       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18818     goto out;
18819   /* If there is no declarator, then the decl-specifier-seq should
18820      specify a type.  */
18821   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18822     {
18823       /* If there was no decl-specifier-seq, and the next token is a
18824          `;', then we have something like:
18825
18826            struct S { ; };
18827
18828          [class.mem]
18829
18830          Each member-declaration shall declare at least one member
18831          name of the class.  */
18832       if (!decl_specifiers.any_specifiers_p)
18833         {
18834           cp_token *token = cp_lexer_peek_token (parser->lexer);
18835           if (!in_system_header_at (token->location))
18836             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18837         }
18838       else
18839         {
18840           tree type;
18841
18842           /* See if this declaration is a friend.  */
18843           friend_p = cp_parser_friend_p (&decl_specifiers);
18844           /* If there were decl-specifiers, check to see if there was
18845              a class-declaration.  */
18846           type = check_tag_decl (&decl_specifiers);
18847           /* Nested classes have already been added to the class, but
18848              a `friend' needs to be explicitly registered.  */
18849           if (friend_p)
18850             {
18851               /* If the `friend' keyword was present, the friend must
18852                  be introduced with a class-key.  */
18853                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18854                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18855                           "in C++03 a class-key must be used "
18856                           "when declaring a friend");
18857                /* In this case:
18858
18859                     template <typename T> struct A {
18860                       friend struct A<T>::B;
18861                     };
18862
18863                   A<T>::B will be represented by a TYPENAME_TYPE, and
18864                   therefore not recognized by check_tag_decl.  */
18865                if (!type)
18866                  {
18867                    type = decl_specifiers.type;
18868                    if (type && TREE_CODE (type) == TYPE_DECL)
18869                      type = TREE_TYPE (type);
18870                  }
18871                if (!type || !TYPE_P (type))
18872                  error_at (decl_spec_token_start->location,
18873                            "friend declaration does not name a class or "
18874                            "function");
18875                else
18876                  make_friend_class (current_class_type, type,
18877                                     /*complain=*/true);
18878             }
18879           /* If there is no TYPE, an error message will already have
18880              been issued.  */
18881           else if (!type || type == error_mark_node)
18882             ;
18883           /* An anonymous aggregate has to be handled specially; such
18884              a declaration really declares a data member (with a
18885              particular type), as opposed to a nested class.  */
18886           else if (ANON_AGGR_TYPE_P (type))
18887             {
18888               /* Remove constructors and such from TYPE, now that we
18889                  know it is an anonymous aggregate.  */
18890               fixup_anonymous_aggr (type);
18891               /* And make the corresponding data member.  */
18892               decl = build_decl (decl_spec_token_start->location,
18893                                  FIELD_DECL, NULL_TREE, type);
18894               /* Add it to the class.  */
18895               finish_member_declaration (decl);
18896             }
18897           else
18898             cp_parser_check_access_in_redeclaration
18899                                               (TYPE_NAME (type),
18900                                                decl_spec_token_start->location);
18901         }
18902     }
18903   else
18904     {
18905       bool assume_semicolon = false;
18906
18907       /* See if these declarations will be friends.  */
18908       friend_p = cp_parser_friend_p (&decl_specifiers);
18909
18910       /* Keep going until we hit the `;' at the end of the
18911          declaration.  */
18912       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18913         {
18914           tree attributes = NULL_TREE;
18915           tree first_attribute;
18916
18917           /* Peek at the next token.  */
18918           token = cp_lexer_peek_token (parser->lexer);
18919
18920           /* Check for a bitfield declaration.  */
18921           if (token->type == CPP_COLON
18922               || (token->type == CPP_NAME
18923                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18924                   == CPP_COLON))
18925             {
18926               tree identifier;
18927               tree width;
18928
18929               /* Get the name of the bitfield.  Note that we cannot just
18930                  check TOKEN here because it may have been invalidated by
18931                  the call to cp_lexer_peek_nth_token above.  */
18932               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18933                 identifier = cp_parser_identifier (parser);
18934               else
18935                 identifier = NULL_TREE;
18936
18937               /* Consume the `:' token.  */
18938               cp_lexer_consume_token (parser->lexer);
18939               /* Get the width of the bitfield.  */
18940               width
18941                 = cp_parser_constant_expression (parser,
18942                                                  /*allow_non_constant=*/false,
18943                                                  NULL);
18944
18945               /* Look for attributes that apply to the bitfield.  */
18946               attributes = cp_parser_attributes_opt (parser);
18947               /* Remember which attributes are prefix attributes and
18948                  which are not.  */
18949               first_attribute = attributes;
18950               /* Combine the attributes.  */
18951               attributes = chainon (prefix_attributes, attributes);
18952
18953               /* Create the bitfield declaration.  */
18954               decl = grokbitfield (identifier
18955                                    ? make_id_declarator (NULL_TREE,
18956                                                          identifier,
18957                                                          sfk_none)
18958                                    : NULL,
18959                                    &decl_specifiers,
18960                                    width,
18961                                    attributes);
18962             }
18963           else
18964             {
18965               cp_declarator *declarator;
18966               tree initializer;
18967               tree asm_specification;
18968               int ctor_dtor_or_conv_p;
18969
18970               /* Parse the declarator.  */
18971               declarator
18972                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18973                                         &ctor_dtor_or_conv_p,
18974                                         /*parenthesized_p=*/NULL,
18975                                         /*member_p=*/true);
18976
18977               /* If something went wrong parsing the declarator, make sure
18978                  that we at least consume some tokens.  */
18979               if (declarator == cp_error_declarator)
18980                 {
18981                   /* Skip to the end of the statement.  */
18982                   cp_parser_skip_to_end_of_statement (parser);
18983                   /* If the next token is not a semicolon, that is
18984                      probably because we just skipped over the body of
18985                      a function.  So, we consume a semicolon if
18986                      present, but do not issue an error message if it
18987                      is not present.  */
18988                   if (cp_lexer_next_token_is (parser->lexer,
18989                                               CPP_SEMICOLON))
18990                     cp_lexer_consume_token (parser->lexer);
18991                   goto out;
18992                 }
18993
18994               if (declares_class_or_enum & 2)
18995                 cp_parser_check_for_definition_in_return_type
18996                                             (declarator, decl_specifiers.type,
18997                                              decl_specifiers.type_location);
18998
18999               /* Look for an asm-specification.  */
19000               asm_specification = cp_parser_asm_specification_opt (parser);
19001               /* Look for attributes that apply to the declaration.  */
19002               attributes = cp_parser_attributes_opt (parser);
19003               /* Remember which attributes are prefix attributes and
19004                  which are not.  */
19005               first_attribute = attributes;
19006               /* Combine the attributes.  */
19007               attributes = chainon (prefix_attributes, attributes);
19008
19009               /* If it's an `=', then we have a constant-initializer or a
19010                  pure-specifier.  It is not correct to parse the
19011                  initializer before registering the member declaration
19012                  since the member declaration should be in scope while
19013                  its initializer is processed.  However, the rest of the
19014                  front end does not yet provide an interface that allows
19015                  us to handle this correctly.  */
19016               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19017                 {
19018                   /* In [class.mem]:
19019
19020                      A pure-specifier shall be used only in the declaration of
19021                      a virtual function.
19022
19023                      A member-declarator can contain a constant-initializer
19024                      only if it declares a static member of integral or
19025                      enumeration type.
19026
19027                      Therefore, if the DECLARATOR is for a function, we look
19028                      for a pure-specifier; otherwise, we look for a
19029                      constant-initializer.  When we call `grokfield', it will
19030                      perform more stringent semantics checks.  */
19031                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19032                   if (function_declarator_p (declarator)
19033                       || (decl_specifiers.type
19034                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19035                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19036                               == FUNCTION_TYPE)))
19037                     initializer = cp_parser_pure_specifier (parser);
19038                   else if (decl_specifiers.storage_class != sc_static)
19039                     initializer = cp_parser_save_nsdmi (parser);
19040                   else if (cxx_dialect >= cxx0x)
19041                     {
19042                       bool nonconst;
19043                       /* Don't require a constant rvalue in C++11, since we
19044                          might want a reference constant.  We'll enforce
19045                          constancy later.  */
19046                       cp_lexer_consume_token (parser->lexer);
19047                       /* Parse the initializer.  */
19048                       initializer = cp_parser_initializer_clause (parser,
19049                                                                   &nonconst);
19050                     }
19051                   else
19052                     /* Parse the initializer.  */
19053                     initializer = cp_parser_constant_initializer (parser);
19054                 }
19055               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19056                        && !function_declarator_p (declarator))
19057                 {
19058                   bool x;
19059                   if (decl_specifiers.storage_class != sc_static)
19060                     initializer = cp_parser_save_nsdmi (parser);
19061                   else
19062                     initializer = cp_parser_initializer (parser, &x, &x);
19063                 }
19064               /* Otherwise, there is no initializer.  */
19065               else
19066                 initializer = NULL_TREE;
19067
19068               /* See if we are probably looking at a function
19069                  definition.  We are certainly not looking at a
19070                  member-declarator.  Calling `grokfield' has
19071                  side-effects, so we must not do it unless we are sure
19072                  that we are looking at a member-declarator.  */
19073               if (cp_parser_token_starts_function_definition_p
19074                   (cp_lexer_peek_token (parser->lexer)))
19075                 {
19076                   /* The grammar does not allow a pure-specifier to be
19077                      used when a member function is defined.  (It is
19078                      possible that this fact is an oversight in the
19079                      standard, since a pure function may be defined
19080                      outside of the class-specifier.  */
19081                   if (initializer)
19082                     error_at (initializer_token_start->location,
19083                               "pure-specifier on function-definition");
19084                   decl = cp_parser_save_member_function_body (parser,
19085                                                               &decl_specifiers,
19086                                                               declarator,
19087                                                               attributes);
19088                   /* If the member was not a friend, declare it here.  */
19089                   if (!friend_p)
19090                     finish_member_declaration (decl);
19091                   /* Peek at the next token.  */
19092                   token = cp_lexer_peek_token (parser->lexer);
19093                   /* If the next token is a semicolon, consume it.  */
19094                   if (token->type == CPP_SEMICOLON)
19095                     cp_lexer_consume_token (parser->lexer);
19096                   goto out;
19097                 }
19098               else
19099                 if (declarator->kind == cdk_function)
19100                   declarator->id_loc = token->location;
19101                 /* Create the declaration.  */
19102                 decl = grokfield (declarator, &decl_specifiers,
19103                                   initializer, /*init_const_expr_p=*/true,
19104                                   asm_specification,
19105                                   attributes);
19106             }
19107
19108           /* Reset PREFIX_ATTRIBUTES.  */
19109           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19110             attributes = TREE_CHAIN (attributes);
19111           if (attributes)
19112             TREE_CHAIN (attributes) = NULL_TREE;
19113
19114           /* If there is any qualification still in effect, clear it
19115              now; we will be starting fresh with the next declarator.  */
19116           parser->scope = NULL_TREE;
19117           parser->qualifying_scope = NULL_TREE;
19118           parser->object_scope = NULL_TREE;
19119           /* If it's a `,', then there are more declarators.  */
19120           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19121             cp_lexer_consume_token (parser->lexer);
19122           /* If the next token isn't a `;', then we have a parse error.  */
19123           else if (cp_lexer_next_token_is_not (parser->lexer,
19124                                                CPP_SEMICOLON))
19125             {
19126               /* The next token might be a ways away from where the
19127                  actual semicolon is missing.  Find the previous token
19128                  and use that for our error position.  */
19129               cp_token *token = cp_lexer_previous_token (parser->lexer);
19130               error_at (token->location,
19131                         "expected %<;%> at end of member declaration");
19132
19133               /* Assume that the user meant to provide a semicolon.  If
19134                  we were to cp_parser_skip_to_end_of_statement, we might
19135                  skip to a semicolon inside a member function definition
19136                  and issue nonsensical error messages.  */
19137               assume_semicolon = true;
19138             }
19139
19140           if (decl)
19141             {
19142               /* Add DECL to the list of members.  */
19143               if (!friend_p)
19144                 finish_member_declaration (decl);
19145
19146               if (TREE_CODE (decl) == FUNCTION_DECL)
19147                 cp_parser_save_default_args (parser, decl);
19148               else if (TREE_CODE (decl) == FIELD_DECL
19149                        && !DECL_C_BIT_FIELD (decl)
19150                        && DECL_INITIAL (decl))
19151                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19152                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19153             }
19154
19155           if (assume_semicolon)
19156             goto out;
19157         }
19158     }
19159
19160   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19161  out:
19162   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19163 }
19164
19165 /* Parse a pure-specifier.
19166
19167    pure-specifier:
19168      = 0
19169
19170    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19171    Otherwise, ERROR_MARK_NODE is returned.  */
19172
19173 static tree
19174 cp_parser_pure_specifier (cp_parser* parser)
19175 {
19176   cp_token *token;
19177
19178   /* Look for the `=' token.  */
19179   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19180     return error_mark_node;
19181   /* Look for the `0' token.  */
19182   token = cp_lexer_peek_token (parser->lexer);
19183
19184   if (token->type == CPP_EOF
19185       || token->type == CPP_PRAGMA_EOL)
19186     return error_mark_node;
19187
19188   cp_lexer_consume_token (parser->lexer);
19189
19190   /* Accept = default or = delete in c++0x mode.  */
19191   if (token->keyword == RID_DEFAULT
19192       || token->keyword == RID_DELETE)
19193     {
19194       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19195       return token->u.value;
19196     }
19197
19198   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19199   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19200     {
19201       cp_parser_error (parser,
19202                        "invalid pure specifier (only %<= 0%> is allowed)");
19203       cp_parser_skip_to_end_of_statement (parser);
19204       return error_mark_node;
19205     }
19206   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19207     {
19208       error_at (token->location, "templates may not be %<virtual%>");
19209       return error_mark_node;
19210     }
19211
19212   return integer_zero_node;
19213 }
19214
19215 /* Parse a constant-initializer.
19216
19217    constant-initializer:
19218      = constant-expression
19219
19220    Returns a representation of the constant-expression.  */
19221
19222 static tree
19223 cp_parser_constant_initializer (cp_parser* parser)
19224 {
19225   /* Look for the `=' token.  */
19226   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19227     return error_mark_node;
19228
19229   /* It is invalid to write:
19230
19231        struct S { static const int i = { 7 }; };
19232
19233      */
19234   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19235     {
19236       cp_parser_error (parser,
19237                        "a brace-enclosed initializer is not allowed here");
19238       /* Consume the opening brace.  */
19239       cp_lexer_consume_token (parser->lexer);
19240       /* Skip the initializer.  */
19241       cp_parser_skip_to_closing_brace (parser);
19242       /* Look for the trailing `}'.  */
19243       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19244
19245       return error_mark_node;
19246     }
19247
19248   return cp_parser_constant_expression (parser,
19249                                         /*allow_non_constant=*/false,
19250                                         NULL);
19251 }
19252
19253 /* Derived classes [gram.class.derived] */
19254
19255 /* Parse a base-clause.
19256
19257    base-clause:
19258      : base-specifier-list
19259
19260    base-specifier-list:
19261      base-specifier ... [opt]
19262      base-specifier-list , base-specifier ... [opt]
19263
19264    Returns a TREE_LIST representing the base-classes, in the order in
19265    which they were declared.  The representation of each node is as
19266    described by cp_parser_base_specifier.
19267
19268    In the case that no bases are specified, this function will return
19269    NULL_TREE, not ERROR_MARK_NODE.  */
19270
19271 static tree
19272 cp_parser_base_clause (cp_parser* parser)
19273 {
19274   tree bases = NULL_TREE;
19275
19276   /* Look for the `:' that begins the list.  */
19277   cp_parser_require (parser, CPP_COLON, RT_COLON);
19278
19279   /* Scan the base-specifier-list.  */
19280   while (true)
19281     {
19282       cp_token *token;
19283       tree base;
19284       bool pack_expansion_p = false;
19285
19286       /* Look for the base-specifier.  */
19287       base = cp_parser_base_specifier (parser);
19288       /* Look for the (optional) ellipsis. */
19289       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19290         {
19291           /* Consume the `...'. */
19292           cp_lexer_consume_token (parser->lexer);
19293
19294           pack_expansion_p = true;
19295         }
19296
19297       /* Add BASE to the front of the list.  */
19298       if (base && base != error_mark_node)
19299         {
19300           if (pack_expansion_p)
19301             /* Make this a pack expansion type. */
19302             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19303
19304           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19305             {
19306               TREE_CHAIN (base) = bases;
19307               bases = base;
19308             }
19309         }
19310       /* Peek at the next token.  */
19311       token = cp_lexer_peek_token (parser->lexer);
19312       /* If it's not a comma, then the list is complete.  */
19313       if (token->type != CPP_COMMA)
19314         break;
19315       /* Consume the `,'.  */
19316       cp_lexer_consume_token (parser->lexer);
19317     }
19318
19319   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19320      base class had a qualified name.  However, the next name that
19321      appears is certainly not qualified.  */
19322   parser->scope = NULL_TREE;
19323   parser->qualifying_scope = NULL_TREE;
19324   parser->object_scope = NULL_TREE;
19325
19326   return nreverse (bases);
19327 }
19328
19329 /* Parse a base-specifier.
19330
19331    base-specifier:
19332      :: [opt] nested-name-specifier [opt] class-name
19333      virtual access-specifier [opt] :: [opt] nested-name-specifier
19334        [opt] class-name
19335      access-specifier virtual [opt] :: [opt] nested-name-specifier
19336        [opt] class-name
19337
19338    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19339    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19340    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19341    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19342
19343 static tree
19344 cp_parser_base_specifier (cp_parser* parser)
19345 {
19346   cp_token *token;
19347   bool done = false;
19348   bool virtual_p = false;
19349   bool duplicate_virtual_error_issued_p = false;
19350   bool duplicate_access_error_issued_p = false;
19351   bool class_scope_p, template_p;
19352   tree access = access_default_node;
19353   tree type;
19354
19355   /* Process the optional `virtual' and `access-specifier'.  */
19356   while (!done)
19357     {
19358       /* Peek at the next token.  */
19359       token = cp_lexer_peek_token (parser->lexer);
19360       /* Process `virtual'.  */
19361       switch (token->keyword)
19362         {
19363         case RID_VIRTUAL:
19364           /* If `virtual' appears more than once, issue an error.  */
19365           if (virtual_p && !duplicate_virtual_error_issued_p)
19366             {
19367               cp_parser_error (parser,
19368                                "%<virtual%> specified more than once in base-specified");
19369               duplicate_virtual_error_issued_p = true;
19370             }
19371
19372           virtual_p = true;
19373
19374           /* Consume the `virtual' token.  */
19375           cp_lexer_consume_token (parser->lexer);
19376
19377           break;
19378
19379         case RID_PUBLIC:
19380         case RID_PROTECTED:
19381         case RID_PRIVATE:
19382           /* If more than one access specifier appears, issue an
19383              error.  */
19384           if (access != access_default_node
19385               && !duplicate_access_error_issued_p)
19386             {
19387               cp_parser_error (parser,
19388                                "more than one access specifier in base-specified");
19389               duplicate_access_error_issued_p = true;
19390             }
19391
19392           access = ridpointers[(int) token->keyword];
19393
19394           /* Consume the access-specifier.  */
19395           cp_lexer_consume_token (parser->lexer);
19396
19397           break;
19398
19399         default:
19400           done = true;
19401           break;
19402         }
19403     }
19404   /* It is not uncommon to see programs mechanically, erroneously, use
19405      the 'typename' keyword to denote (dependent) qualified types
19406      as base classes.  */
19407   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19408     {
19409       token = cp_lexer_peek_token (parser->lexer);
19410       if (!processing_template_decl)
19411         error_at (token->location,
19412                   "keyword %<typename%> not allowed outside of templates");
19413       else
19414         error_at (token->location,
19415                   "keyword %<typename%> not allowed in this context "
19416                   "(the base class is implicitly a type)");
19417       cp_lexer_consume_token (parser->lexer);
19418     }
19419
19420   /* Look for the optional `::' operator.  */
19421   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19422   /* Look for the nested-name-specifier.  The simplest way to
19423      implement:
19424
19425        [temp.res]
19426
19427        The keyword `typename' is not permitted in a base-specifier or
19428        mem-initializer; in these contexts a qualified name that
19429        depends on a template-parameter is implicitly assumed to be a
19430        type name.
19431
19432      is to pretend that we have seen the `typename' keyword at this
19433      point.  */
19434   cp_parser_nested_name_specifier_opt (parser,
19435                                        /*typename_keyword_p=*/true,
19436                                        /*check_dependency_p=*/true,
19437                                        typename_type,
19438                                        /*is_declaration=*/true);
19439   /* If the base class is given by a qualified name, assume that names
19440      we see are type names or templates, as appropriate.  */
19441   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19442   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19443
19444   if (!parser->scope
19445       && cp_lexer_next_token_is_decltype (parser->lexer))
19446     /* DR 950 allows decltype as a base-specifier.  */
19447     type = cp_parser_decltype (parser);
19448   else
19449     {
19450       /* Otherwise, look for the class-name.  */
19451       type = cp_parser_class_name (parser,
19452                                    class_scope_p,
19453                                    template_p,
19454                                    typename_type,
19455                                    /*check_dependency_p=*/true,
19456                                    /*class_head_p=*/false,
19457                                    /*is_declaration=*/true);
19458       type = TREE_TYPE (type);
19459     }
19460
19461   if (type == error_mark_node)
19462     return error_mark_node;
19463
19464   return finish_base_specifier (type, access, virtual_p);
19465 }
19466
19467 /* Exception handling [gram.exception] */
19468
19469 /* Parse an (optional) noexcept-specification.
19470
19471    noexcept-specification:
19472      noexcept ( constant-expression ) [opt]
19473
19474    If no noexcept-specification is present, returns NULL_TREE.
19475    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19476    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19477    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
19478    Otherwise, returns a noexcept specification unless RETURN_COND is true,
19479    in which case a boolean condition is returned instead.  */
19480
19481 static tree
19482 cp_parser_noexcept_specification_opt (cp_parser* parser,
19483                                       bool require_constexpr,
19484                                       bool* consumed_expr,
19485                                       bool return_cond)
19486 {
19487   cp_token *token;
19488   const char *saved_message;
19489
19490   /* Peek at the next token.  */
19491   token = cp_lexer_peek_token (parser->lexer);
19492
19493   /* Is it a noexcept-specification?  */
19494   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19495     {
19496       tree expr;
19497       cp_lexer_consume_token (parser->lexer);
19498
19499       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19500         {
19501           cp_lexer_consume_token (parser->lexer);
19502
19503           if (require_constexpr)
19504             {
19505               /* Types may not be defined in an exception-specification.  */
19506               saved_message = parser->type_definition_forbidden_message;
19507               parser->type_definition_forbidden_message
19508               = G_("types may not be defined in an exception-specification");
19509
19510               expr = cp_parser_constant_expression (parser, false, NULL);
19511
19512               /* Restore the saved message.  */
19513               parser->type_definition_forbidden_message = saved_message;
19514             }
19515           else
19516             {
19517               expr = cp_parser_expression (parser, false, NULL);
19518               *consumed_expr = true;
19519             }
19520
19521           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19522         }
19523       else
19524         {
19525           expr = boolean_true_node;
19526           if (!require_constexpr)
19527             *consumed_expr = false;
19528         }
19529
19530       /* We cannot build a noexcept-spec right away because this will check
19531          that expr is a constexpr.  */
19532       if (!return_cond)
19533         return build_noexcept_spec (expr, tf_warning_or_error);
19534       else
19535         return expr;
19536     }
19537   else
19538     return NULL_TREE;
19539 }
19540
19541 /* Parse an (optional) exception-specification.
19542
19543    exception-specification:
19544      throw ( type-id-list [opt] )
19545
19546    Returns a TREE_LIST representing the exception-specification.  The
19547    TREE_VALUE of each node is a type.  */
19548
19549 static tree
19550 cp_parser_exception_specification_opt (cp_parser* parser)
19551 {
19552   cp_token *token;
19553   tree type_id_list;
19554   const char *saved_message;
19555
19556   /* Peek at the next token.  */
19557   token = cp_lexer_peek_token (parser->lexer);
19558
19559   /* Is it a noexcept-specification?  */
19560   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19561                                                       false);
19562   if (type_id_list != NULL_TREE)
19563     return type_id_list;
19564
19565   /* If it's not `throw', then there's no exception-specification.  */
19566   if (!cp_parser_is_keyword (token, RID_THROW))
19567     return NULL_TREE;
19568
19569 #if 0
19570   /* Enable this once a lot of code has transitioned to noexcept?  */
19571   if (cxx_dialect == cxx0x && !in_system_header)
19572     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19573              "deprecated in C++0x; use %<noexcept%> instead");
19574 #endif
19575
19576   /* Consume the `throw'.  */
19577   cp_lexer_consume_token (parser->lexer);
19578
19579   /* Look for the `('.  */
19580   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19581
19582   /* Peek at the next token.  */
19583   token = cp_lexer_peek_token (parser->lexer);
19584   /* If it's not a `)', then there is a type-id-list.  */
19585   if (token->type != CPP_CLOSE_PAREN)
19586     {
19587       /* Types may not be defined in an exception-specification.  */
19588       saved_message = parser->type_definition_forbidden_message;
19589       parser->type_definition_forbidden_message
19590         = G_("types may not be defined in an exception-specification");
19591       /* Parse the type-id-list.  */
19592       type_id_list = cp_parser_type_id_list (parser);
19593       /* Restore the saved message.  */
19594       parser->type_definition_forbidden_message = saved_message;
19595     }
19596   else
19597     type_id_list = empty_except_spec;
19598
19599   /* Look for the `)'.  */
19600   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19601
19602   return type_id_list;
19603 }
19604
19605 /* Parse an (optional) type-id-list.
19606
19607    type-id-list:
19608      type-id ... [opt]
19609      type-id-list , type-id ... [opt]
19610
19611    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19612    in the order that the types were presented.  */
19613
19614 static tree
19615 cp_parser_type_id_list (cp_parser* parser)
19616 {
19617   tree types = NULL_TREE;
19618
19619   while (true)
19620     {
19621       cp_token *token;
19622       tree type;
19623
19624       /* Get the next type-id.  */
19625       type = cp_parser_type_id (parser);
19626       /* Parse the optional ellipsis. */
19627       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19628         {
19629           /* Consume the `...'. */
19630           cp_lexer_consume_token (parser->lexer);
19631
19632           /* Turn the type into a pack expansion expression. */
19633           type = make_pack_expansion (type);
19634         }
19635       /* Add it to the list.  */
19636       types = add_exception_specifier (types, type, /*complain=*/1);
19637       /* Peek at the next token.  */
19638       token = cp_lexer_peek_token (parser->lexer);
19639       /* If it is not a `,', we are done.  */
19640       if (token->type != CPP_COMMA)
19641         break;
19642       /* Consume the `,'.  */
19643       cp_lexer_consume_token (parser->lexer);
19644     }
19645
19646   return nreverse (types);
19647 }
19648
19649 /* Parse a try-block.
19650
19651    try-block:
19652      try compound-statement handler-seq  */
19653
19654 static tree
19655 cp_parser_try_block (cp_parser* parser)
19656 {
19657   tree try_block;
19658
19659   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19660   try_block = begin_try_block ();
19661   cp_parser_compound_statement (parser, NULL, true, false);
19662   finish_try_block (try_block);
19663   cp_parser_handler_seq (parser);
19664   finish_handler_sequence (try_block);
19665
19666   return try_block;
19667 }
19668
19669 /* Parse a function-try-block.
19670
19671    function-try-block:
19672      try ctor-initializer [opt] function-body handler-seq  */
19673
19674 static bool
19675 cp_parser_function_try_block (cp_parser* parser)
19676 {
19677   tree compound_stmt;
19678   tree try_block;
19679   bool ctor_initializer_p;
19680
19681   /* Look for the `try' keyword.  */
19682   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19683     return false;
19684   /* Let the rest of the front end know where we are.  */
19685   try_block = begin_function_try_block (&compound_stmt);
19686   /* Parse the function-body.  */
19687   ctor_initializer_p
19688     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19689   /* We're done with the `try' part.  */
19690   finish_function_try_block (try_block);
19691   /* Parse the handlers.  */
19692   cp_parser_handler_seq (parser);
19693   /* We're done with the handlers.  */
19694   finish_function_handler_sequence (try_block, compound_stmt);
19695
19696   return ctor_initializer_p;
19697 }
19698
19699 /* Parse a handler-seq.
19700
19701    handler-seq:
19702      handler handler-seq [opt]  */
19703
19704 static void
19705 cp_parser_handler_seq (cp_parser* parser)
19706 {
19707   while (true)
19708     {
19709       cp_token *token;
19710
19711       /* Parse the handler.  */
19712       cp_parser_handler (parser);
19713       /* Peek at the next token.  */
19714       token = cp_lexer_peek_token (parser->lexer);
19715       /* If it's not `catch' then there are no more handlers.  */
19716       if (!cp_parser_is_keyword (token, RID_CATCH))
19717         break;
19718     }
19719 }
19720
19721 /* Parse a handler.
19722
19723    handler:
19724      catch ( exception-declaration ) compound-statement  */
19725
19726 static void
19727 cp_parser_handler (cp_parser* parser)
19728 {
19729   tree handler;
19730   tree declaration;
19731
19732   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19733   handler = begin_handler ();
19734   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19735   declaration = cp_parser_exception_declaration (parser);
19736   finish_handler_parms (declaration, handler);
19737   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19738   cp_parser_compound_statement (parser, NULL, false, false);
19739   finish_handler (handler);
19740 }
19741
19742 /* Parse an exception-declaration.
19743
19744    exception-declaration:
19745      type-specifier-seq declarator
19746      type-specifier-seq abstract-declarator
19747      type-specifier-seq
19748      ...
19749
19750    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19751    ellipsis variant is used.  */
19752
19753 static tree
19754 cp_parser_exception_declaration (cp_parser* parser)
19755 {
19756   cp_decl_specifier_seq type_specifiers;
19757   cp_declarator *declarator;
19758   const char *saved_message;
19759
19760   /* If it's an ellipsis, it's easy to handle.  */
19761   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19762     {
19763       /* Consume the `...' token.  */
19764       cp_lexer_consume_token (parser->lexer);
19765       return NULL_TREE;
19766     }
19767
19768   /* Types may not be defined in exception-declarations.  */
19769   saved_message = parser->type_definition_forbidden_message;
19770   parser->type_definition_forbidden_message
19771     = G_("types may not be defined in exception-declarations");
19772
19773   /* Parse the type-specifier-seq.  */
19774   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19775                                 /*is_trailing_return=*/false,
19776                                 &type_specifiers);
19777   /* If it's a `)', then there is no declarator.  */
19778   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19779     declarator = NULL;
19780   else
19781     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19782                                        /*ctor_dtor_or_conv_p=*/NULL,
19783                                        /*parenthesized_p=*/NULL,
19784                                        /*member_p=*/false);
19785
19786   /* Restore the saved message.  */
19787   parser->type_definition_forbidden_message = saved_message;
19788
19789   if (!type_specifiers.any_specifiers_p)
19790     return error_mark_node;
19791
19792   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19793 }
19794
19795 /* Parse a throw-expression.
19796
19797    throw-expression:
19798      throw assignment-expression [opt]
19799
19800    Returns a THROW_EXPR representing the throw-expression.  */
19801
19802 static tree
19803 cp_parser_throw_expression (cp_parser* parser)
19804 {
19805   tree expression;
19806   cp_token* token;
19807
19808   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19809   token = cp_lexer_peek_token (parser->lexer);
19810   /* Figure out whether or not there is an assignment-expression
19811      following the "throw" keyword.  */
19812   if (token->type == CPP_COMMA
19813       || token->type == CPP_SEMICOLON
19814       || token->type == CPP_CLOSE_PAREN
19815       || token->type == CPP_CLOSE_SQUARE
19816       || token->type == CPP_CLOSE_BRACE
19817       || token->type == CPP_COLON)
19818     expression = NULL_TREE;
19819   else
19820     expression = cp_parser_assignment_expression (parser,
19821                                                   /*cast_p=*/false, NULL);
19822
19823   return build_throw (expression);
19824 }
19825
19826 /* GNU Extensions */
19827
19828 /* Parse an (optional) asm-specification.
19829
19830    asm-specification:
19831      asm ( string-literal )
19832
19833    If the asm-specification is present, returns a STRING_CST
19834    corresponding to the string-literal.  Otherwise, returns
19835    NULL_TREE.  */
19836
19837 static tree
19838 cp_parser_asm_specification_opt (cp_parser* parser)
19839 {
19840   cp_token *token;
19841   tree asm_specification;
19842
19843   /* Peek at the next token.  */
19844   token = cp_lexer_peek_token (parser->lexer);
19845   /* If the next token isn't the `asm' keyword, then there's no
19846      asm-specification.  */
19847   if (!cp_parser_is_keyword (token, RID_ASM))
19848     return NULL_TREE;
19849
19850   /* Consume the `asm' token.  */
19851   cp_lexer_consume_token (parser->lexer);
19852   /* Look for the `('.  */
19853   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19854
19855   /* Look for the string-literal.  */
19856   asm_specification = cp_parser_string_literal (parser, false, false);
19857
19858   /* Look for the `)'.  */
19859   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19860
19861   return asm_specification;
19862 }
19863
19864 /* Parse an asm-operand-list.
19865
19866    asm-operand-list:
19867      asm-operand
19868      asm-operand-list , asm-operand
19869
19870    asm-operand:
19871      string-literal ( expression )
19872      [ string-literal ] string-literal ( expression )
19873
19874    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19875    each node is the expression.  The TREE_PURPOSE is itself a
19876    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19877    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19878    is a STRING_CST for the string literal before the parenthesis. Returns
19879    ERROR_MARK_NODE if any of the operands are invalid.  */
19880
19881 static tree
19882 cp_parser_asm_operand_list (cp_parser* parser)
19883 {
19884   tree asm_operands = NULL_TREE;
19885   bool invalid_operands = false;
19886
19887   while (true)
19888     {
19889       tree string_literal;
19890       tree expression;
19891       tree name;
19892
19893       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19894         {
19895           /* Consume the `[' token.  */
19896           cp_lexer_consume_token (parser->lexer);
19897           /* Read the operand name.  */
19898           name = cp_parser_identifier (parser);
19899           if (name != error_mark_node)
19900             name = build_string (IDENTIFIER_LENGTH (name),
19901                                  IDENTIFIER_POINTER (name));
19902           /* Look for the closing `]'.  */
19903           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19904         }
19905       else
19906         name = NULL_TREE;
19907       /* Look for the string-literal.  */
19908       string_literal = cp_parser_string_literal (parser, false, false);
19909
19910       /* Look for the `('.  */
19911       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19912       /* Parse the expression.  */
19913       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19914       /* Look for the `)'.  */
19915       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19916
19917       if (name == error_mark_node 
19918           || string_literal == error_mark_node 
19919           || expression == error_mark_node)
19920         invalid_operands = true;
19921
19922       /* Add this operand to the list.  */
19923       asm_operands = tree_cons (build_tree_list (name, string_literal),
19924                                 expression,
19925                                 asm_operands);
19926       /* If the next token is not a `,', there are no more
19927          operands.  */
19928       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19929         break;
19930       /* Consume the `,'.  */
19931       cp_lexer_consume_token (parser->lexer);
19932     }
19933
19934   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19935 }
19936
19937 /* Parse an asm-clobber-list.
19938
19939    asm-clobber-list:
19940      string-literal
19941      asm-clobber-list , string-literal
19942
19943    Returns a TREE_LIST, indicating the clobbers in the order that they
19944    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19945
19946 static tree
19947 cp_parser_asm_clobber_list (cp_parser* parser)
19948 {
19949   tree clobbers = NULL_TREE;
19950
19951   while (true)
19952     {
19953       tree string_literal;
19954
19955       /* Look for the string literal.  */
19956       string_literal = cp_parser_string_literal (parser, false, false);
19957       /* Add it to the list.  */
19958       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19959       /* If the next token is not a `,', then the list is
19960          complete.  */
19961       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19962         break;
19963       /* Consume the `,' token.  */
19964       cp_lexer_consume_token (parser->lexer);
19965     }
19966
19967   return clobbers;
19968 }
19969
19970 /* Parse an asm-label-list.
19971
19972    asm-label-list:
19973      identifier
19974      asm-label-list , identifier
19975
19976    Returns a TREE_LIST, indicating the labels in the order that they
19977    appeared.  The TREE_VALUE of each node is a label.  */
19978
19979 static tree
19980 cp_parser_asm_label_list (cp_parser* parser)
19981 {
19982   tree labels = NULL_TREE;
19983
19984   while (true)
19985     {
19986       tree identifier, label, name;
19987
19988       /* Look for the identifier.  */
19989       identifier = cp_parser_identifier (parser);
19990       if (!error_operand_p (identifier))
19991         {
19992           label = lookup_label (identifier);
19993           if (TREE_CODE (label) == LABEL_DECL)
19994             {
19995               TREE_USED (label) = 1;
19996               check_goto (label);
19997               name = build_string (IDENTIFIER_LENGTH (identifier),
19998                                    IDENTIFIER_POINTER (identifier));
19999               labels = tree_cons (name, label, labels);
20000             }
20001         }
20002       /* If the next token is not a `,', then the list is
20003          complete.  */
20004       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20005         break;
20006       /* Consume the `,' token.  */
20007       cp_lexer_consume_token (parser->lexer);
20008     }
20009
20010   return nreverse (labels);
20011 }
20012
20013 /* Parse an (optional) series of attributes.
20014
20015    attributes:
20016      attributes attribute
20017
20018    attribute:
20019      __attribute__ (( attribute-list [opt] ))
20020
20021    The return value is as for cp_parser_attribute_list.  */
20022
20023 static tree
20024 cp_parser_attributes_opt (cp_parser* parser)
20025 {
20026   tree attributes = NULL_TREE;
20027
20028   while (true)
20029     {
20030       cp_token *token;
20031       tree attribute_list;
20032
20033       /* Peek at the next token.  */
20034       token = cp_lexer_peek_token (parser->lexer);
20035       /* If it's not `__attribute__', then we're done.  */
20036       if (token->keyword != RID_ATTRIBUTE)
20037         break;
20038
20039       /* Consume the `__attribute__' keyword.  */
20040       cp_lexer_consume_token (parser->lexer);
20041       /* Look for the two `(' tokens.  */
20042       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20043       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20044
20045       /* Peek at the next token.  */
20046       token = cp_lexer_peek_token (parser->lexer);
20047       if (token->type != CPP_CLOSE_PAREN)
20048         /* Parse the attribute-list.  */
20049         attribute_list = cp_parser_attribute_list (parser);
20050       else
20051         /* If the next token is a `)', then there is no attribute
20052            list.  */
20053         attribute_list = NULL;
20054
20055       /* Look for the two `)' tokens.  */
20056       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20057       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20058
20059       /* Add these new attributes to the list.  */
20060       attributes = chainon (attributes, attribute_list);
20061     }
20062
20063   return attributes;
20064 }
20065
20066 /* Parse an attribute-list.
20067
20068    attribute-list:
20069      attribute
20070      attribute-list , attribute
20071
20072    attribute:
20073      identifier
20074      identifier ( identifier )
20075      identifier ( identifier , expression-list )
20076      identifier ( expression-list )
20077
20078    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20079    to an attribute.  The TREE_PURPOSE of each node is the identifier
20080    indicating which attribute is in use.  The TREE_VALUE represents
20081    the arguments, if any.  */
20082
20083 static tree
20084 cp_parser_attribute_list (cp_parser* parser)
20085 {
20086   tree attribute_list = NULL_TREE;
20087   bool save_translate_strings_p = parser->translate_strings_p;
20088
20089   parser->translate_strings_p = false;
20090   while (true)
20091     {
20092       cp_token *token;
20093       tree identifier;
20094       tree attribute;
20095
20096       /* Look for the identifier.  We also allow keywords here; for
20097          example `__attribute__ ((const))' is legal.  */
20098       token = cp_lexer_peek_token (parser->lexer);
20099       if (token->type == CPP_NAME
20100           || token->type == CPP_KEYWORD)
20101         {
20102           tree arguments = NULL_TREE;
20103
20104           /* Consume the token.  */
20105           token = cp_lexer_consume_token (parser->lexer);
20106
20107           /* Save away the identifier that indicates which attribute
20108              this is.  */
20109           identifier = (token->type == CPP_KEYWORD) 
20110             /* For keywords, use the canonical spelling, not the
20111                parsed identifier.  */
20112             ? ridpointers[(int) token->keyword]
20113             : token->u.value;
20114           
20115           attribute = build_tree_list (identifier, NULL_TREE);
20116
20117           /* Peek at the next token.  */
20118           token = cp_lexer_peek_token (parser->lexer);
20119           /* If it's an `(', then parse the attribute arguments.  */
20120           if (token->type == CPP_OPEN_PAREN)
20121             {
20122               VEC(tree,gc) *vec;
20123               int attr_flag = (attribute_takes_identifier_p (identifier)
20124                                ? id_attr : normal_attr);
20125               vec = cp_parser_parenthesized_expression_list
20126                     (parser, attr_flag, /*cast_p=*/false,
20127                      /*allow_expansion_p=*/false,
20128                      /*non_constant_p=*/NULL);
20129               if (vec == NULL)
20130                 arguments = error_mark_node;
20131               else
20132                 {
20133                   arguments = build_tree_list_vec (vec);
20134                   release_tree_vector (vec);
20135                 }
20136               /* Save the arguments away.  */
20137               TREE_VALUE (attribute) = arguments;
20138             }
20139
20140           if (arguments != error_mark_node)
20141             {
20142               /* Add this attribute to the list.  */
20143               TREE_CHAIN (attribute) = attribute_list;
20144               attribute_list = attribute;
20145             }
20146
20147           token = cp_lexer_peek_token (parser->lexer);
20148         }
20149       /* Now, look for more attributes.  If the next token isn't a
20150          `,', we're done.  */
20151       if (token->type != CPP_COMMA)
20152         break;
20153
20154       /* Consume the comma and keep going.  */
20155       cp_lexer_consume_token (parser->lexer);
20156     }
20157   parser->translate_strings_p = save_translate_strings_p;
20158
20159   /* We built up the list in reverse order.  */
20160   return nreverse (attribute_list);
20161 }
20162
20163 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
20164    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
20165    current value of the PEDANTIC flag, regardless of whether or not
20166    the `__extension__' keyword is present.  The caller is responsible
20167    for restoring the value of the PEDANTIC flag.  */
20168
20169 static bool
20170 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20171 {
20172   /* Save the old value of the PEDANTIC flag.  */
20173   *saved_pedantic = pedantic;
20174
20175   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20176     {
20177       /* Consume the `__extension__' token.  */
20178       cp_lexer_consume_token (parser->lexer);
20179       /* We're not being pedantic while the `__extension__' keyword is
20180          in effect.  */
20181       pedantic = 0;
20182
20183       return true;
20184     }
20185
20186   return false;
20187 }
20188
20189 /* Parse a label declaration.
20190
20191    label-declaration:
20192      __label__ label-declarator-seq ;
20193
20194    label-declarator-seq:
20195      identifier , label-declarator-seq
20196      identifier  */
20197
20198 static void
20199 cp_parser_label_declaration (cp_parser* parser)
20200 {
20201   /* Look for the `__label__' keyword.  */
20202   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20203
20204   while (true)
20205     {
20206       tree identifier;
20207
20208       /* Look for an identifier.  */
20209       identifier = cp_parser_identifier (parser);
20210       /* If we failed, stop.  */
20211       if (identifier == error_mark_node)
20212         break;
20213       /* Declare it as a label.  */
20214       finish_label_decl (identifier);
20215       /* If the next token is a `;', stop.  */
20216       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20217         break;
20218       /* Look for the `,' separating the label declarations.  */
20219       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20220     }
20221
20222   /* Look for the final `;'.  */
20223   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20224 }
20225
20226 /* Support Functions */
20227
20228 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20229    NAME should have one of the representations used for an
20230    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20231    is returned.  If PARSER->SCOPE is a dependent type, then a
20232    SCOPE_REF is returned.
20233
20234    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20235    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20236    was formed.  Abstractly, such entities should not be passed to this
20237    function, because they do not need to be looked up, but it is
20238    simpler to check for this special case here, rather than at the
20239    call-sites.
20240
20241    In cases not explicitly covered above, this function returns a
20242    DECL, OVERLOAD, or baselink representing the result of the lookup.
20243    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20244    is returned.
20245
20246    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20247    (e.g., "struct") that was used.  In that case bindings that do not
20248    refer to types are ignored.
20249
20250    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20251    ignored.
20252
20253    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20254    are ignored.
20255
20256    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20257    types.
20258
20259    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20260    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20261    NULL_TREE otherwise.  */
20262
20263 static tree
20264 cp_parser_lookup_name (cp_parser *parser, tree name,
20265                        enum tag_types tag_type,
20266                        bool is_template,
20267                        bool is_namespace,
20268                        bool check_dependency,
20269                        tree *ambiguous_decls,
20270                        location_t name_location)
20271 {
20272   int flags = 0;
20273   tree decl;
20274   tree object_type = parser->context->object_type;
20275
20276   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20277     flags |= LOOKUP_COMPLAIN;
20278
20279   /* Assume that the lookup will be unambiguous.  */
20280   if (ambiguous_decls)
20281     *ambiguous_decls = NULL_TREE;
20282
20283   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20284      no longer valid.  Note that if we are parsing tentatively, and
20285      the parse fails, OBJECT_TYPE will be automatically restored.  */
20286   parser->context->object_type = NULL_TREE;
20287
20288   if (name == error_mark_node)
20289     return error_mark_node;
20290
20291   /* A template-id has already been resolved; there is no lookup to
20292      do.  */
20293   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20294     return name;
20295   if (BASELINK_P (name))
20296     {
20297       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20298                   == TEMPLATE_ID_EXPR);
20299       return name;
20300     }
20301
20302   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20303      it should already have been checked to make sure that the name
20304      used matches the type being destroyed.  */
20305   if (TREE_CODE (name) == BIT_NOT_EXPR)
20306     {
20307       tree type;
20308
20309       /* Figure out to which type this destructor applies.  */
20310       if (parser->scope)
20311         type = parser->scope;
20312       else if (object_type)
20313         type = object_type;
20314       else
20315         type = current_class_type;
20316       /* If that's not a class type, there is no destructor.  */
20317       if (!type || !CLASS_TYPE_P (type))
20318         return error_mark_node;
20319       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20320         lazily_declare_fn (sfk_destructor, type);
20321       if (!CLASSTYPE_DESTRUCTORS (type))
20322           return error_mark_node;
20323       /* If it was a class type, return the destructor.  */
20324       return CLASSTYPE_DESTRUCTORS (type);
20325     }
20326
20327   /* By this point, the NAME should be an ordinary identifier.  If
20328      the id-expression was a qualified name, the qualifying scope is
20329      stored in PARSER->SCOPE at this point.  */
20330   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20331
20332   /* Perform the lookup.  */
20333   if (parser->scope)
20334     {
20335       bool dependent_p;
20336
20337       if (parser->scope == error_mark_node)
20338         return error_mark_node;
20339
20340       /* If the SCOPE is dependent, the lookup must be deferred until
20341          the template is instantiated -- unless we are explicitly
20342          looking up names in uninstantiated templates.  Even then, we
20343          cannot look up the name if the scope is not a class type; it
20344          might, for example, be a template type parameter.  */
20345       dependent_p = (TYPE_P (parser->scope)
20346                      && dependent_scope_p (parser->scope));
20347       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20348           && dependent_p)
20349         /* Defer lookup.  */
20350         decl = error_mark_node;
20351       else
20352         {
20353           tree pushed_scope = NULL_TREE;
20354
20355           /* If PARSER->SCOPE is a dependent type, then it must be a
20356              class type, and we must not be checking dependencies;
20357              otherwise, we would have processed this lookup above.  So
20358              that PARSER->SCOPE is not considered a dependent base by
20359              lookup_member, we must enter the scope here.  */
20360           if (dependent_p)
20361             pushed_scope = push_scope (parser->scope);
20362
20363           /* If the PARSER->SCOPE is a template specialization, it
20364              may be instantiated during name lookup.  In that case,
20365              errors may be issued.  Even if we rollback the current
20366              tentative parse, those errors are valid.  */
20367           decl = lookup_qualified_name (parser->scope, name,
20368                                         tag_type != none_type,
20369                                         /*complain=*/true);
20370
20371           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20372              lookup result and the nested-name-specifier nominates a class C:
20373                * if the name specified after the nested-name-specifier, when
20374                looked up in C, is the injected-class-name of C (Clause 9), or
20375                * if the name specified after the nested-name-specifier is the
20376                same as the identifier or the simple-template-id's template-
20377                name in the last component of the nested-name-specifier,
20378              the name is instead considered to name the constructor of
20379              class C. [ Note: for example, the constructor is not an
20380              acceptable lookup result in an elaborated-type-specifier so
20381              the constructor would not be used in place of the
20382              injected-class-name. --end note ] Such a constructor name
20383              shall be used only in the declarator-id of a declaration that
20384              names a constructor or in a using-declaration.  */
20385           if (tag_type == none_type
20386               && DECL_SELF_REFERENCE_P (decl)
20387               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20388             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20389                                           tag_type != none_type,
20390                                           /*complain=*/true);
20391
20392           /* If we have a single function from a using decl, pull it out.  */
20393           if (TREE_CODE (decl) == OVERLOAD
20394               && !really_overloaded_fn (decl))
20395             decl = OVL_FUNCTION (decl);
20396
20397           if (pushed_scope)
20398             pop_scope (pushed_scope);
20399         }
20400
20401       /* If the scope is a dependent type and either we deferred lookup or
20402          we did lookup but didn't find the name, rememeber the name.  */
20403       if (decl == error_mark_node && TYPE_P (parser->scope)
20404           && dependent_type_p (parser->scope))
20405         {
20406           if (tag_type)
20407             {
20408               tree type;
20409
20410               /* The resolution to Core Issue 180 says that `struct
20411                  A::B' should be considered a type-name, even if `A'
20412                  is dependent.  */
20413               type = make_typename_type (parser->scope, name, tag_type,
20414                                          /*complain=*/tf_error);
20415               decl = TYPE_NAME (type);
20416             }
20417           else if (is_template
20418                    && (cp_parser_next_token_ends_template_argument_p (parser)
20419                        || cp_lexer_next_token_is (parser->lexer,
20420                                                   CPP_CLOSE_PAREN)))
20421             decl = make_unbound_class_template (parser->scope,
20422                                                 name, NULL_TREE,
20423                                                 /*complain=*/tf_error);
20424           else
20425             decl = build_qualified_name (/*type=*/NULL_TREE,
20426                                          parser->scope, name,
20427                                          is_template);
20428         }
20429       parser->qualifying_scope = parser->scope;
20430       parser->object_scope = NULL_TREE;
20431     }
20432   else if (object_type)
20433     {
20434       tree object_decl = NULL_TREE;
20435       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20436          OBJECT_TYPE is not a class.  */
20437       if (CLASS_TYPE_P (object_type))
20438         /* If the OBJECT_TYPE is a template specialization, it may
20439            be instantiated during name lookup.  In that case, errors
20440            may be issued.  Even if we rollback the current tentative
20441            parse, those errors are valid.  */
20442         object_decl = lookup_member (object_type,
20443                                      name,
20444                                      /*protect=*/0,
20445                                      tag_type != none_type,
20446                                      tf_warning_or_error);
20447       /* Look it up in the enclosing context, too.  */
20448       decl = lookup_name_real (name, tag_type != none_type,
20449                                /*nonclass=*/0,
20450                                /*block_p=*/true, is_namespace, flags);
20451       parser->object_scope = object_type;
20452       parser->qualifying_scope = NULL_TREE;
20453       if (object_decl)
20454         decl = object_decl;
20455     }
20456   else
20457     {
20458       decl = lookup_name_real (name, tag_type != none_type,
20459                                /*nonclass=*/0,
20460                                /*block_p=*/true, is_namespace, flags);
20461       parser->qualifying_scope = NULL_TREE;
20462       parser->object_scope = NULL_TREE;
20463     }
20464
20465   /* If the lookup failed, let our caller know.  */
20466   if (!decl || decl == error_mark_node)
20467     return error_mark_node;
20468
20469   /* Pull out the template from an injected-class-name (or multiple).  */
20470   if (is_template)
20471     decl = maybe_get_template_decl_from_type_decl (decl);
20472
20473   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20474   if (TREE_CODE (decl) == TREE_LIST)
20475     {
20476       if (ambiguous_decls)
20477         *ambiguous_decls = decl;
20478       /* The error message we have to print is too complicated for
20479          cp_parser_error, so we incorporate its actions directly.  */
20480       if (!cp_parser_simulate_error (parser))
20481         {
20482           error_at (name_location, "reference to %qD is ambiguous",
20483                     name);
20484           print_candidates (decl);
20485         }
20486       return error_mark_node;
20487     }
20488
20489   gcc_assert (DECL_P (decl)
20490               || TREE_CODE (decl) == OVERLOAD
20491               || TREE_CODE (decl) == SCOPE_REF
20492               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20493               || BASELINK_P (decl));
20494
20495   /* If we have resolved the name of a member declaration, check to
20496      see if the declaration is accessible.  When the name resolves to
20497      set of overloaded functions, accessibility is checked when
20498      overload resolution is done.
20499
20500      During an explicit instantiation, access is not checked at all,
20501      as per [temp.explicit].  */
20502   if (DECL_P (decl))
20503     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20504
20505   maybe_record_typedef_use (decl);
20506
20507   return decl;
20508 }
20509
20510 /* Like cp_parser_lookup_name, but for use in the typical case where
20511    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20512    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20513
20514 static tree
20515 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20516 {
20517   return cp_parser_lookup_name (parser, name,
20518                                 none_type,
20519                                 /*is_template=*/false,
20520                                 /*is_namespace=*/false,
20521                                 /*check_dependency=*/true,
20522                                 /*ambiguous_decls=*/NULL,
20523                                 location);
20524 }
20525
20526 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20527    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20528    true, the DECL indicates the class being defined in a class-head,
20529    or declared in an elaborated-type-specifier.
20530
20531    Otherwise, return DECL.  */
20532
20533 static tree
20534 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20535 {
20536   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20537      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20538
20539        struct A {
20540          template <typename T> struct B;
20541        };
20542
20543        template <typename T> struct A::B {};
20544
20545      Similarly, in an elaborated-type-specifier:
20546
20547        namespace N { struct X{}; }
20548
20549        struct A {
20550          template <typename T> friend struct N::X;
20551        };
20552
20553      However, if the DECL refers to a class type, and we are in
20554      the scope of the class, then the name lookup automatically
20555      finds the TYPE_DECL created by build_self_reference rather
20556      than a TEMPLATE_DECL.  For example, in:
20557
20558        template <class T> struct S {
20559          S s;
20560        };
20561
20562      there is no need to handle such case.  */
20563
20564   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20565     return DECL_TEMPLATE_RESULT (decl);
20566
20567   return decl;
20568 }
20569
20570 /* If too many, or too few, template-parameter lists apply to the
20571    declarator, issue an error message.  Returns TRUE if all went well,
20572    and FALSE otherwise.  */
20573
20574 static bool
20575 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20576                                                 cp_declarator *declarator,
20577                                                 location_t declarator_location)
20578 {
20579   unsigned num_templates;
20580
20581   /* We haven't seen any classes that involve template parameters yet.  */
20582   num_templates = 0;
20583
20584   switch (declarator->kind)
20585     {
20586     case cdk_id:
20587       if (declarator->u.id.qualifying_scope)
20588         {
20589           tree scope;
20590
20591           scope = declarator->u.id.qualifying_scope;
20592
20593           while (scope && CLASS_TYPE_P (scope))
20594             {
20595               /* You're supposed to have one `template <...>'
20596                  for every template class, but you don't need one
20597                  for a full specialization.  For example:
20598
20599                  template <class T> struct S{};
20600                  template <> struct S<int> { void f(); };
20601                  void S<int>::f () {}
20602
20603                  is correct; there shouldn't be a `template <>' for
20604                  the definition of `S<int>::f'.  */
20605               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20606                 /* If SCOPE does not have template information of any
20607                    kind, then it is not a template, nor is it nested
20608                    within a template.  */
20609                 break;
20610               if (explicit_class_specialization_p (scope))
20611                 break;
20612               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20613                 ++num_templates;
20614
20615               scope = TYPE_CONTEXT (scope);
20616             }
20617         }
20618       else if (TREE_CODE (declarator->u.id.unqualified_name)
20619                == TEMPLATE_ID_EXPR)
20620         /* If the DECLARATOR has the form `X<y>' then it uses one
20621            additional level of template parameters.  */
20622         ++num_templates;
20623
20624       return cp_parser_check_template_parameters 
20625         (parser, num_templates, declarator_location, declarator);
20626
20627
20628     case cdk_function:
20629     case cdk_array:
20630     case cdk_pointer:
20631     case cdk_reference:
20632     case cdk_ptrmem:
20633       return (cp_parser_check_declarator_template_parameters
20634               (parser, declarator->declarator, declarator_location));
20635
20636     case cdk_error:
20637       return true;
20638
20639     default:
20640       gcc_unreachable ();
20641     }
20642   return false;
20643 }
20644
20645 /* NUM_TEMPLATES were used in the current declaration.  If that is
20646    invalid, return FALSE and issue an error messages.  Otherwise,
20647    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20648    declarator and we can print more accurate diagnostics.  */
20649
20650 static bool
20651 cp_parser_check_template_parameters (cp_parser* parser,
20652                                      unsigned num_templates,
20653                                      location_t location,
20654                                      cp_declarator *declarator)
20655 {
20656   /* If there are the same number of template classes and parameter
20657      lists, that's OK.  */
20658   if (parser->num_template_parameter_lists == num_templates)
20659     return true;
20660   /* If there are more, but only one more, then we are referring to a
20661      member template.  That's OK too.  */
20662   if (parser->num_template_parameter_lists == num_templates + 1)
20663     return true;
20664   /* If there are more template classes than parameter lists, we have
20665      something like:
20666
20667        template <class T> void S<T>::R<T>::f ();  */
20668   if (parser->num_template_parameter_lists < num_templates)
20669     {
20670       if (declarator && !current_function_decl)
20671         error_at (location, "specializing member %<%T::%E%> "
20672                   "requires %<template<>%> syntax", 
20673                   declarator->u.id.qualifying_scope,
20674                   declarator->u.id.unqualified_name);
20675       else if (declarator)
20676         error_at (location, "invalid declaration of %<%T::%E%>",
20677                   declarator->u.id.qualifying_scope,
20678                   declarator->u.id.unqualified_name);
20679       else 
20680         error_at (location, "too few template-parameter-lists");
20681       return false;
20682     }
20683   /* Otherwise, there are too many template parameter lists.  We have
20684      something like:
20685
20686      template <class T> template <class U> void S::f();  */
20687   error_at (location, "too many template-parameter-lists");
20688   return false;
20689 }
20690
20691 /* Parse an optional `::' token indicating that the following name is
20692    from the global namespace.  If so, PARSER->SCOPE is set to the
20693    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20694    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20695    Returns the new value of PARSER->SCOPE, if the `::' token is
20696    present, and NULL_TREE otherwise.  */
20697
20698 static tree
20699 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20700 {
20701   cp_token *token;
20702
20703   /* Peek at the next token.  */
20704   token = cp_lexer_peek_token (parser->lexer);
20705   /* If we're looking at a `::' token then we're starting from the
20706      global namespace, not our current location.  */
20707   if (token->type == CPP_SCOPE)
20708     {
20709       /* Consume the `::' token.  */
20710       cp_lexer_consume_token (parser->lexer);
20711       /* Set the SCOPE so that we know where to start the lookup.  */
20712       parser->scope = global_namespace;
20713       parser->qualifying_scope = global_namespace;
20714       parser->object_scope = NULL_TREE;
20715
20716       return parser->scope;
20717     }
20718   else if (!current_scope_valid_p)
20719     {
20720       parser->scope = NULL_TREE;
20721       parser->qualifying_scope = NULL_TREE;
20722       parser->object_scope = NULL_TREE;
20723     }
20724
20725   return NULL_TREE;
20726 }
20727
20728 /* Returns TRUE if the upcoming token sequence is the start of a
20729    constructor declarator.  If FRIEND_P is true, the declarator is
20730    preceded by the `friend' specifier.  */
20731
20732 static bool
20733 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20734 {
20735   bool constructor_p;
20736   tree nested_name_specifier;
20737   cp_token *next_token;
20738
20739   /* The common case is that this is not a constructor declarator, so
20740      try to avoid doing lots of work if at all possible.  It's not
20741      valid declare a constructor at function scope.  */
20742   if (parser->in_function_body)
20743     return false;
20744   /* And only certain tokens can begin a constructor declarator.  */
20745   next_token = cp_lexer_peek_token (parser->lexer);
20746   if (next_token->type != CPP_NAME
20747       && next_token->type != CPP_SCOPE
20748       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20749       && next_token->type != CPP_TEMPLATE_ID)
20750     return false;
20751
20752   /* Parse tentatively; we are going to roll back all of the tokens
20753      consumed here.  */
20754   cp_parser_parse_tentatively (parser);
20755   /* Assume that we are looking at a constructor declarator.  */
20756   constructor_p = true;
20757
20758   /* Look for the optional `::' operator.  */
20759   cp_parser_global_scope_opt (parser,
20760                               /*current_scope_valid_p=*/false);
20761   /* Look for the nested-name-specifier.  */
20762   nested_name_specifier
20763     = (cp_parser_nested_name_specifier_opt (parser,
20764                                             /*typename_keyword_p=*/false,
20765                                             /*check_dependency_p=*/false,
20766                                             /*type_p=*/false,
20767                                             /*is_declaration=*/false));
20768   /* Outside of a class-specifier, there must be a
20769      nested-name-specifier.  */
20770   if (!nested_name_specifier &&
20771       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20772        || friend_p))
20773     constructor_p = false;
20774   else if (nested_name_specifier == error_mark_node)
20775     constructor_p = false;
20776
20777   /* If we have a class scope, this is easy; DR 147 says that S::S always
20778      names the constructor, and no other qualified name could.  */
20779   if (constructor_p && nested_name_specifier
20780       && CLASS_TYPE_P (nested_name_specifier))
20781     {
20782       tree id = cp_parser_unqualified_id (parser,
20783                                           /*template_keyword_p=*/false,
20784                                           /*check_dependency_p=*/false,
20785                                           /*declarator_p=*/true,
20786                                           /*optional_p=*/false);
20787       if (is_overloaded_fn (id))
20788         id = DECL_NAME (get_first_fn (id));
20789       if (!constructor_name_p (id, nested_name_specifier))
20790         constructor_p = false;
20791     }
20792   /* If we still think that this might be a constructor-declarator,
20793      look for a class-name.  */
20794   else if (constructor_p)
20795     {
20796       /* If we have:
20797
20798            template <typename T> struct S {
20799              S();
20800            };
20801
20802          we must recognize that the nested `S' names a class.  */
20803       tree type_decl;
20804       type_decl = cp_parser_class_name (parser,
20805                                         /*typename_keyword_p=*/false,
20806                                         /*template_keyword_p=*/false,
20807                                         none_type,
20808                                         /*check_dependency_p=*/false,
20809                                         /*class_head_p=*/false,
20810                                         /*is_declaration=*/false);
20811       /* If there was no class-name, then this is not a constructor.  */
20812       constructor_p = !cp_parser_error_occurred (parser);
20813
20814       /* If we're still considering a constructor, we have to see a `(',
20815          to begin the parameter-declaration-clause, followed by either a
20816          `)', an `...', or a decl-specifier.  We need to check for a
20817          type-specifier to avoid being fooled into thinking that:
20818
20819            S (f) (int);
20820
20821          is a constructor.  (It is actually a function named `f' that
20822          takes one parameter (of type `int') and returns a value of type
20823          `S'.  */
20824       if (constructor_p
20825           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20826         constructor_p = false;
20827
20828       if (constructor_p
20829           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20830           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20831           /* A parameter declaration begins with a decl-specifier,
20832              which is either the "attribute" keyword, a storage class
20833              specifier, or (usually) a type-specifier.  */
20834           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20835         {
20836           tree type;
20837           tree pushed_scope = NULL_TREE;
20838           unsigned saved_num_template_parameter_lists;
20839
20840           /* Names appearing in the type-specifier should be looked up
20841              in the scope of the class.  */
20842           if (current_class_type)
20843             type = NULL_TREE;
20844           else
20845             {
20846               type = TREE_TYPE (type_decl);
20847               if (TREE_CODE (type) == TYPENAME_TYPE)
20848                 {
20849                   type = resolve_typename_type (type,
20850                                                 /*only_current_p=*/false);
20851                   if (TREE_CODE (type) == TYPENAME_TYPE)
20852                     {
20853                       cp_parser_abort_tentative_parse (parser);
20854                       return false;
20855                     }
20856                 }
20857               pushed_scope = push_scope (type);
20858             }
20859
20860           /* Inside the constructor parameter list, surrounding
20861              template-parameter-lists do not apply.  */
20862           saved_num_template_parameter_lists
20863             = parser->num_template_parameter_lists;
20864           parser->num_template_parameter_lists = 0;
20865
20866           /* Look for the type-specifier.  */
20867           cp_parser_type_specifier (parser,
20868                                     CP_PARSER_FLAGS_NONE,
20869                                     /*decl_specs=*/NULL,
20870                                     /*is_declarator=*/true,
20871                                     /*declares_class_or_enum=*/NULL,
20872                                     /*is_cv_qualifier=*/NULL);
20873
20874           parser->num_template_parameter_lists
20875             = saved_num_template_parameter_lists;
20876
20877           /* Leave the scope of the class.  */
20878           if (pushed_scope)
20879             pop_scope (pushed_scope);
20880
20881           constructor_p = !cp_parser_error_occurred (parser);
20882         }
20883     }
20884
20885   /* We did not really want to consume any tokens.  */
20886   cp_parser_abort_tentative_parse (parser);
20887
20888   return constructor_p;
20889 }
20890
20891 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20892    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20893    they must be performed once we are in the scope of the function.
20894
20895    Returns the function defined.  */
20896
20897 static tree
20898 cp_parser_function_definition_from_specifiers_and_declarator
20899   (cp_parser* parser,
20900    cp_decl_specifier_seq *decl_specifiers,
20901    tree attributes,
20902    const cp_declarator *declarator)
20903 {
20904   tree fn;
20905   bool success_p;
20906
20907   /* Begin the function-definition.  */
20908   success_p = start_function (decl_specifiers, declarator, attributes);
20909
20910   /* The things we're about to see are not directly qualified by any
20911      template headers we've seen thus far.  */
20912   reset_specialization ();
20913
20914   /* If there were names looked up in the decl-specifier-seq that we
20915      did not check, check them now.  We must wait until we are in the
20916      scope of the function to perform the checks, since the function
20917      might be a friend.  */
20918   perform_deferred_access_checks ();
20919
20920   if (!success_p)
20921     {
20922       /* Skip the entire function.  */
20923       cp_parser_skip_to_end_of_block_or_statement (parser);
20924       fn = error_mark_node;
20925     }
20926   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20927     {
20928       /* Seen already, skip it.  An error message has already been output.  */
20929       cp_parser_skip_to_end_of_block_or_statement (parser);
20930       fn = current_function_decl;
20931       current_function_decl = NULL_TREE;
20932       /* If this is a function from a class, pop the nested class.  */
20933       if (current_class_name)
20934         pop_nested_class ();
20935     }
20936   else
20937     {
20938       timevar_id_t tv;
20939       if (DECL_DECLARED_INLINE_P (current_function_decl))
20940         tv = TV_PARSE_INLINE;
20941       else
20942         tv = TV_PARSE_FUNC;
20943       timevar_push (tv);
20944       fn = cp_parser_function_definition_after_declarator (parser,
20945                                                          /*inline_p=*/false);
20946       timevar_pop (tv);
20947     }
20948
20949   return fn;
20950 }
20951
20952 /* Parse the part of a function-definition that follows the
20953    declarator.  INLINE_P is TRUE iff this function is an inline
20954    function defined within a class-specifier.
20955
20956    Returns the function defined.  */
20957
20958 static tree
20959 cp_parser_function_definition_after_declarator (cp_parser* parser,
20960                                                 bool inline_p)
20961 {
20962   tree fn;
20963   bool ctor_initializer_p = false;
20964   bool saved_in_unbraced_linkage_specification_p;
20965   bool saved_in_function_body;
20966   unsigned saved_num_template_parameter_lists;
20967   cp_token *token;
20968
20969   saved_in_function_body = parser->in_function_body;
20970   parser->in_function_body = true;
20971   /* If the next token is `return', then the code may be trying to
20972      make use of the "named return value" extension that G++ used to
20973      support.  */
20974   token = cp_lexer_peek_token (parser->lexer);
20975   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20976     {
20977       /* Consume the `return' keyword.  */
20978       cp_lexer_consume_token (parser->lexer);
20979       /* Look for the identifier that indicates what value is to be
20980          returned.  */
20981       cp_parser_identifier (parser);
20982       /* Issue an error message.  */
20983       error_at (token->location,
20984                 "named return values are no longer supported");
20985       /* Skip tokens until we reach the start of the function body.  */
20986       while (true)
20987         {
20988           cp_token *token = cp_lexer_peek_token (parser->lexer);
20989           if (token->type == CPP_OPEN_BRACE
20990               || token->type == CPP_EOF
20991               || token->type == CPP_PRAGMA_EOL)
20992             break;
20993           cp_lexer_consume_token (parser->lexer);
20994         }
20995     }
20996   /* The `extern' in `extern "C" void f () { ... }' does not apply to
20997      anything declared inside `f'.  */
20998   saved_in_unbraced_linkage_specification_p
20999     = parser->in_unbraced_linkage_specification_p;
21000   parser->in_unbraced_linkage_specification_p = false;
21001   /* Inside the function, surrounding template-parameter-lists do not
21002      apply.  */
21003   saved_num_template_parameter_lists
21004     = parser->num_template_parameter_lists;
21005   parser->num_template_parameter_lists = 0;
21006
21007   start_lambda_scope (current_function_decl);
21008
21009   /* If the next token is `try', `__transaction_atomic', or
21010      `__transaction_relaxed`, then we are looking at either function-try-block
21011      or function-transaction-block.  Note that all of these include the
21012      function-body.  */
21013   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21014     ctor_initializer_p = cp_parser_function_transaction (parser,
21015         RID_TRANSACTION_ATOMIC);
21016   else if (cp_lexer_next_token_is_keyword (parser->lexer,
21017       RID_TRANSACTION_RELAXED))
21018     ctor_initializer_p = cp_parser_function_transaction (parser,
21019         RID_TRANSACTION_RELAXED);
21020   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21021     ctor_initializer_p = cp_parser_function_try_block (parser);
21022   else
21023     ctor_initializer_p
21024       = cp_parser_ctor_initializer_opt_and_function_body (parser);
21025
21026   finish_lambda_scope ();
21027
21028   /* Finish the function.  */
21029   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21030                         (inline_p ? 2 : 0));
21031   /* Generate code for it, if necessary.  */
21032   expand_or_defer_fn (fn);
21033   /* Restore the saved values.  */
21034   parser->in_unbraced_linkage_specification_p
21035     = saved_in_unbraced_linkage_specification_p;
21036   parser->num_template_parameter_lists
21037     = saved_num_template_parameter_lists;
21038   parser->in_function_body = saved_in_function_body;
21039
21040   return fn;
21041 }
21042
21043 /* Parse a template-declaration, assuming that the `export' (and
21044    `extern') keywords, if present, has already been scanned.  MEMBER_P
21045    is as for cp_parser_template_declaration.  */
21046
21047 static void
21048 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21049 {
21050   tree decl = NULL_TREE;
21051   VEC (deferred_access_check,gc) *checks;
21052   tree parameter_list;
21053   bool friend_p = false;
21054   bool need_lang_pop;
21055   cp_token *token;
21056
21057   /* Look for the `template' keyword.  */
21058   token = cp_lexer_peek_token (parser->lexer);
21059   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21060     return;
21061
21062   /* And the `<'.  */
21063   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21064     return;
21065   if (at_class_scope_p () && current_function_decl)
21066     {
21067       /* 14.5.2.2 [temp.mem]
21068
21069          A local class shall not have member templates.  */
21070       error_at (token->location,
21071                 "invalid declaration of member template in local class");
21072       cp_parser_skip_to_end_of_block_or_statement (parser);
21073       return;
21074     }
21075   /* [temp]
21076
21077      A template ... shall not have C linkage.  */
21078   if (current_lang_name == lang_name_c)
21079     {
21080       error_at (token->location, "template with C linkage");
21081       /* Give it C++ linkage to avoid confusing other parts of the
21082          front end.  */
21083       push_lang_context (lang_name_cplusplus);
21084       need_lang_pop = true;
21085     }
21086   else
21087     need_lang_pop = false;
21088
21089   /* We cannot perform access checks on the template parameter
21090      declarations until we know what is being declared, just as we
21091      cannot check the decl-specifier list.  */
21092   push_deferring_access_checks (dk_deferred);
21093
21094   /* If the next token is `>', then we have an invalid
21095      specialization.  Rather than complain about an invalid template
21096      parameter, issue an error message here.  */
21097   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21098     {
21099       cp_parser_error (parser, "invalid explicit specialization");
21100       begin_specialization ();
21101       parameter_list = NULL_TREE;
21102     }
21103   else
21104     {
21105       /* Parse the template parameters.  */
21106       parameter_list = cp_parser_template_parameter_list (parser);
21107       fixup_template_parms ();
21108     }
21109
21110   /* Get the deferred access checks from the parameter list.  These
21111      will be checked once we know what is being declared, as for a
21112      member template the checks must be performed in the scope of the
21113      class containing the member.  */
21114   checks = get_deferred_access_checks ();
21115
21116   /* Look for the `>'.  */
21117   cp_parser_skip_to_end_of_template_parameter_list (parser);
21118   /* We just processed one more parameter list.  */
21119   ++parser->num_template_parameter_lists;
21120   /* If the next token is `template', there are more template
21121      parameters.  */
21122   if (cp_lexer_next_token_is_keyword (parser->lexer,
21123                                       RID_TEMPLATE))
21124     cp_parser_template_declaration_after_export (parser, member_p);
21125   else if (cxx_dialect >= cxx0x
21126            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21127     decl = cp_parser_alias_declaration (parser);
21128   else
21129     {
21130       /* There are no access checks when parsing a template, as we do not
21131          know if a specialization will be a friend.  */
21132       push_deferring_access_checks (dk_no_check);
21133       token = cp_lexer_peek_token (parser->lexer);
21134       decl = cp_parser_single_declaration (parser,
21135                                            checks,
21136                                            member_p,
21137                                            /*explicit_specialization_p=*/false,
21138                                            &friend_p);
21139       pop_deferring_access_checks ();
21140
21141       /* If this is a member template declaration, let the front
21142          end know.  */
21143       if (member_p && !friend_p && decl)
21144         {
21145           if (TREE_CODE (decl) == TYPE_DECL)
21146             cp_parser_check_access_in_redeclaration (decl, token->location);
21147
21148           decl = finish_member_template_decl (decl);
21149         }
21150       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21151         make_friend_class (current_class_type, TREE_TYPE (decl),
21152                            /*complain=*/true);
21153     }
21154   /* We are done with the current parameter list.  */
21155   --parser->num_template_parameter_lists;
21156
21157   pop_deferring_access_checks ();
21158
21159   /* Finish up.  */
21160   finish_template_decl (parameter_list);
21161
21162   /* Check the template arguments for a literal operator template.  */
21163   if (decl
21164       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21165       && UDLIT_OPER_P (DECL_NAME (decl)))
21166     {
21167       bool ok = true;
21168       if (parameter_list == NULL_TREE)
21169         ok = false;
21170       else
21171         {
21172           int num_parms = TREE_VEC_LENGTH (parameter_list);
21173           if (num_parms != 1)
21174             ok = false;
21175           else
21176             {
21177               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21178               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21179               if (TREE_TYPE (parm) != char_type_node
21180                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21181                 ok = false;
21182             }
21183         }
21184       if (!ok)
21185         error ("literal operator template %qD has invalid parameter list."
21186                "  Expected non-type template argument pack <char...>",
21187                decl);
21188     }
21189   /* Register member declarations.  */
21190   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21191     finish_member_declaration (decl);
21192   /* For the erroneous case of a template with C linkage, we pushed an
21193      implicit C++ linkage scope; exit that scope now.  */
21194   if (need_lang_pop)
21195     pop_lang_context ();
21196   /* If DECL is a function template, we must return to parse it later.
21197      (Even though there is no definition, there might be default
21198      arguments that need handling.)  */
21199   if (member_p && decl
21200       && (TREE_CODE (decl) == FUNCTION_DECL
21201           || DECL_FUNCTION_TEMPLATE_P (decl)))
21202     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21203 }
21204
21205 /* Perform the deferred access checks from a template-parameter-list.
21206    CHECKS is a TREE_LIST of access checks, as returned by
21207    get_deferred_access_checks.  */
21208
21209 static void
21210 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21211 {
21212   ++processing_template_parmlist;
21213   perform_access_checks (checks);
21214   --processing_template_parmlist;
21215 }
21216
21217 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21218    `function-definition' sequence.  MEMBER_P is true, this declaration
21219    appears in a class scope.
21220
21221    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
21222    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
21223
21224 static tree
21225 cp_parser_single_declaration (cp_parser* parser,
21226                               VEC (deferred_access_check,gc)* checks,
21227                               bool member_p,
21228                               bool explicit_specialization_p,
21229                               bool* friend_p)
21230 {
21231   int declares_class_or_enum;
21232   tree decl = NULL_TREE;
21233   cp_decl_specifier_seq decl_specifiers;
21234   bool function_definition_p = false;
21235   cp_token *decl_spec_token_start;
21236
21237   /* This function is only used when processing a template
21238      declaration.  */
21239   gcc_assert (innermost_scope_kind () == sk_template_parms
21240               || innermost_scope_kind () == sk_template_spec);
21241
21242   /* Defer access checks until we know what is being declared.  */
21243   push_deferring_access_checks (dk_deferred);
21244
21245   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21246      alternative.  */
21247   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21248   cp_parser_decl_specifier_seq (parser,
21249                                 CP_PARSER_FLAGS_OPTIONAL,
21250                                 &decl_specifiers,
21251                                 &declares_class_or_enum);
21252   if (friend_p)
21253     *friend_p = cp_parser_friend_p (&decl_specifiers);
21254
21255   /* There are no template typedefs.  */
21256   if (decl_specifiers.specs[(int) ds_typedef])
21257     {
21258       error_at (decl_spec_token_start->location,
21259                 "template declaration of %<typedef%>");
21260       decl = error_mark_node;
21261     }
21262
21263   /* Gather up the access checks that occurred the
21264      decl-specifier-seq.  */
21265   stop_deferring_access_checks ();
21266
21267   /* Check for the declaration of a template class.  */
21268   if (declares_class_or_enum)
21269     {
21270       if (cp_parser_declares_only_class_p (parser))
21271         {
21272           decl = shadow_tag (&decl_specifiers);
21273
21274           /* In this case:
21275
21276                struct C {
21277                  friend template <typename T> struct A<T>::B;
21278                };
21279
21280              A<T>::B will be represented by a TYPENAME_TYPE, and
21281              therefore not recognized by shadow_tag.  */
21282           if (friend_p && *friend_p
21283               && !decl
21284               && decl_specifiers.type
21285               && TYPE_P (decl_specifiers.type))
21286             decl = decl_specifiers.type;
21287
21288           if (decl && decl != error_mark_node)
21289             decl = TYPE_NAME (decl);
21290           else
21291             decl = error_mark_node;
21292
21293           /* Perform access checks for template parameters.  */
21294           cp_parser_perform_template_parameter_access_checks (checks);
21295         }
21296     }
21297
21298   /* Complain about missing 'typename' or other invalid type names.  */
21299   if (!decl_specifiers.any_type_specifiers_p
21300       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21301     {
21302       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21303          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21304          the rest of this declaration.  */
21305       decl = error_mark_node;
21306       goto out;
21307     }
21308
21309   /* If it's not a template class, try for a template function.  If
21310      the next token is a `;', then this declaration does not declare
21311      anything.  But, if there were errors in the decl-specifiers, then
21312      the error might well have come from an attempted class-specifier.
21313      In that case, there's no need to warn about a missing declarator.  */
21314   if (!decl
21315       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21316           || decl_specifiers.type != error_mark_node))
21317     {
21318       decl = cp_parser_init_declarator (parser,
21319                                         &decl_specifiers,
21320                                         checks,
21321                                         /*function_definition_allowed_p=*/true,
21322                                         member_p,
21323                                         declares_class_or_enum,
21324                                         &function_definition_p,
21325                                         NULL);
21326
21327     /* 7.1.1-1 [dcl.stc]
21328
21329        A storage-class-specifier shall not be specified in an explicit
21330        specialization...  */
21331     if (decl
21332         && explicit_specialization_p
21333         && decl_specifiers.storage_class != sc_none)
21334       {
21335         error_at (decl_spec_token_start->location,
21336                   "explicit template specialization cannot have a storage class");
21337         decl = error_mark_node;
21338       }
21339     }
21340
21341   /* Look for a trailing `;' after the declaration.  */
21342   if (!function_definition_p
21343       && (decl == error_mark_node
21344           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21345     cp_parser_skip_to_end_of_block_or_statement (parser);
21346
21347  out:
21348   pop_deferring_access_checks ();
21349
21350   /* Clear any current qualification; whatever comes next is the start
21351      of something new.  */
21352   parser->scope = NULL_TREE;
21353   parser->qualifying_scope = NULL_TREE;
21354   parser->object_scope = NULL_TREE;
21355
21356   return decl;
21357 }
21358
21359 /* Parse a cast-expression that is not the operand of a unary "&".  */
21360
21361 static tree
21362 cp_parser_simple_cast_expression (cp_parser *parser)
21363 {
21364   return cp_parser_cast_expression (parser, /*address_p=*/false,
21365                                     /*cast_p=*/false, NULL);
21366 }
21367
21368 /* Parse a functional cast to TYPE.  Returns an expression
21369    representing the cast.  */
21370
21371 static tree
21372 cp_parser_functional_cast (cp_parser* parser, tree type)
21373 {
21374   VEC(tree,gc) *vec;
21375   tree expression_list;
21376   tree cast;
21377   bool nonconst_p;
21378
21379   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21380     {
21381       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21382       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21383       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21384       if (TREE_CODE (type) == TYPE_DECL)
21385         type = TREE_TYPE (type);
21386       return finish_compound_literal (type, expression_list,
21387                                       tf_warning_or_error);
21388     }
21389
21390
21391   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21392                                                  /*cast_p=*/true,
21393                                                  /*allow_expansion_p=*/true,
21394                                                  /*non_constant_p=*/NULL);
21395   if (vec == NULL)
21396     expression_list = error_mark_node;
21397   else
21398     {
21399       expression_list = build_tree_list_vec (vec);
21400       release_tree_vector (vec);
21401     }
21402
21403   cast = build_functional_cast (type, expression_list,
21404                                 tf_warning_or_error);
21405   /* [expr.const]/1: In an integral constant expression "only type
21406      conversions to integral or enumeration type can be used".  */
21407   if (TREE_CODE (type) == TYPE_DECL)
21408     type = TREE_TYPE (type);
21409   if (cast != error_mark_node
21410       && !cast_valid_in_integral_constant_expression_p (type)
21411       && cp_parser_non_integral_constant_expression (parser,
21412                                                      NIC_CONSTRUCTOR))
21413     return error_mark_node;
21414   return cast;
21415 }
21416
21417 /* Save the tokens that make up the body of a member function defined
21418    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21419    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21420    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21421    for the member function.  */
21422
21423 static tree
21424 cp_parser_save_member_function_body (cp_parser* parser,
21425                                      cp_decl_specifier_seq *decl_specifiers,
21426                                      cp_declarator *declarator,
21427                                      tree attributes)
21428 {
21429   cp_token *first;
21430   cp_token *last;
21431   tree fn;
21432
21433   /* Create the FUNCTION_DECL.  */
21434   fn = grokmethod (decl_specifiers, declarator, attributes);
21435   /* If something went badly wrong, bail out now.  */
21436   if (fn == error_mark_node)
21437     {
21438       /* If there's a function-body, skip it.  */
21439       if (cp_parser_token_starts_function_definition_p
21440           (cp_lexer_peek_token (parser->lexer)))
21441         cp_parser_skip_to_end_of_block_or_statement (parser);
21442       return error_mark_node;
21443     }
21444
21445   /* Remember it, if there default args to post process.  */
21446   cp_parser_save_default_args (parser, fn);
21447
21448   /* Save away the tokens that make up the body of the
21449      function.  */
21450   first = parser->lexer->next_token;
21451   /* We can have braced-init-list mem-initializers before the fn body.  */
21452   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21453     {
21454       cp_lexer_consume_token (parser->lexer);
21455       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21456              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21457         {
21458           /* cache_group will stop after an un-nested { } pair, too.  */
21459           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21460             break;
21461
21462           /* variadic mem-inits have ... after the ')'.  */
21463           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21464             cp_lexer_consume_token (parser->lexer);
21465         }
21466     }
21467   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21468   /* Handle function try blocks.  */
21469   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21470     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21471   last = parser->lexer->next_token;
21472
21473   /* Save away the inline definition; we will process it when the
21474      class is complete.  */
21475   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21476   DECL_PENDING_INLINE_P (fn) = 1;
21477
21478   /* We need to know that this was defined in the class, so that
21479      friend templates are handled correctly.  */
21480   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21481
21482   /* Add FN to the queue of functions to be parsed later.  */
21483   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21484
21485   return fn;
21486 }
21487
21488 /* Save the tokens that make up the in-class initializer for a non-static
21489    data member.  Returns a DEFAULT_ARG.  */
21490
21491 static tree
21492 cp_parser_save_nsdmi (cp_parser* parser)
21493 {
21494   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21495 }
21496
21497 /* Parse a template-argument-list, as well as the trailing ">" (but
21498    not the opening "<").  See cp_parser_template_argument_list for the
21499    return value.  */
21500
21501 static tree
21502 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21503 {
21504   tree arguments;
21505   tree saved_scope;
21506   tree saved_qualifying_scope;
21507   tree saved_object_scope;
21508   bool saved_greater_than_is_operator_p;
21509   int saved_unevaluated_operand;
21510   int saved_inhibit_evaluation_warnings;
21511
21512   /* [temp.names]
21513
21514      When parsing a template-id, the first non-nested `>' is taken as
21515      the end of the template-argument-list rather than a greater-than
21516      operator.  */
21517   saved_greater_than_is_operator_p
21518     = parser->greater_than_is_operator_p;
21519   parser->greater_than_is_operator_p = false;
21520   /* Parsing the argument list may modify SCOPE, so we save it
21521      here.  */
21522   saved_scope = parser->scope;
21523   saved_qualifying_scope = parser->qualifying_scope;
21524   saved_object_scope = parser->object_scope;
21525   /* We need to evaluate the template arguments, even though this
21526      template-id may be nested within a "sizeof".  */
21527   saved_unevaluated_operand = cp_unevaluated_operand;
21528   cp_unevaluated_operand = 0;
21529   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21530   c_inhibit_evaluation_warnings = 0;
21531   /* Parse the template-argument-list itself.  */
21532   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21533       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21534     arguments = NULL_TREE;
21535   else
21536     arguments = cp_parser_template_argument_list (parser);
21537   /* Look for the `>' that ends the template-argument-list. If we find
21538      a '>>' instead, it's probably just a typo.  */
21539   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21540     {
21541       if (cxx_dialect != cxx98)
21542         {
21543           /* In C++0x, a `>>' in a template argument list or cast
21544              expression is considered to be two separate `>'
21545              tokens. So, change the current token to a `>', but don't
21546              consume it: it will be consumed later when the outer
21547              template argument list (or cast expression) is parsed.
21548              Note that this replacement of `>' for `>>' is necessary
21549              even if we are parsing tentatively: in the tentative
21550              case, after calling
21551              cp_parser_enclosed_template_argument_list we will always
21552              throw away all of the template arguments and the first
21553              closing `>', either because the template argument list
21554              was erroneous or because we are replacing those tokens
21555              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21556              not have been thrown away) is needed either to close an
21557              outer template argument list or to complete a new-style
21558              cast.  */
21559           cp_token *token = cp_lexer_peek_token (parser->lexer);
21560           token->type = CPP_GREATER;
21561         }
21562       else if (!saved_greater_than_is_operator_p)
21563         {
21564           /* If we're in a nested template argument list, the '>>' has
21565             to be a typo for '> >'. We emit the error message, but we
21566             continue parsing and we push a '>' as next token, so that
21567             the argument list will be parsed correctly.  Note that the
21568             global source location is still on the token before the
21569             '>>', so we need to say explicitly where we want it.  */
21570           cp_token *token = cp_lexer_peek_token (parser->lexer);
21571           error_at (token->location, "%<>>%> should be %<> >%> "
21572                     "within a nested template argument list");
21573
21574           token->type = CPP_GREATER;
21575         }
21576       else
21577         {
21578           /* If this is not a nested template argument list, the '>>'
21579             is a typo for '>'. Emit an error message and continue.
21580             Same deal about the token location, but here we can get it
21581             right by consuming the '>>' before issuing the diagnostic.  */
21582           cp_token *token = cp_lexer_consume_token (parser->lexer);
21583           error_at (token->location,
21584                     "spurious %<>>%>, use %<>%> to terminate "
21585                     "a template argument list");
21586         }
21587     }
21588   else
21589     cp_parser_skip_to_end_of_template_parameter_list (parser);
21590   /* The `>' token might be a greater-than operator again now.  */
21591   parser->greater_than_is_operator_p
21592     = saved_greater_than_is_operator_p;
21593   /* Restore the SAVED_SCOPE.  */
21594   parser->scope = saved_scope;
21595   parser->qualifying_scope = saved_qualifying_scope;
21596   parser->object_scope = saved_object_scope;
21597   cp_unevaluated_operand = saved_unevaluated_operand;
21598   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21599
21600   return arguments;
21601 }
21602
21603 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21604    arguments, or the body of the function have not yet been parsed,
21605    parse them now.  */
21606
21607 static void
21608 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21609 {
21610   timevar_push (TV_PARSE_INMETH);
21611   /* If this member is a template, get the underlying
21612      FUNCTION_DECL.  */
21613   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21614     member_function = DECL_TEMPLATE_RESULT (member_function);
21615
21616   /* There should not be any class definitions in progress at this
21617      point; the bodies of members are only parsed outside of all class
21618      definitions.  */
21619   gcc_assert (parser->num_classes_being_defined == 0);
21620   /* While we're parsing the member functions we might encounter more
21621      classes.  We want to handle them right away, but we don't want
21622      them getting mixed up with functions that are currently in the
21623      queue.  */
21624   push_unparsed_function_queues (parser);
21625
21626   /* Make sure that any template parameters are in scope.  */
21627   maybe_begin_member_template_processing (member_function);
21628
21629   /* If the body of the function has not yet been parsed, parse it
21630      now.  */
21631   if (DECL_PENDING_INLINE_P (member_function))
21632     {
21633       tree function_scope;
21634       cp_token_cache *tokens;
21635
21636       /* The function is no longer pending; we are processing it.  */
21637       tokens = DECL_PENDING_INLINE_INFO (member_function);
21638       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21639       DECL_PENDING_INLINE_P (member_function) = 0;
21640
21641       /* If this is a local class, enter the scope of the containing
21642          function.  */
21643       function_scope = current_function_decl;
21644       if (function_scope)
21645         push_function_context ();
21646
21647       /* Push the body of the function onto the lexer stack.  */
21648       cp_parser_push_lexer_for_tokens (parser, tokens);
21649
21650       /* Let the front end know that we going to be defining this
21651          function.  */
21652       start_preparsed_function (member_function, NULL_TREE,
21653                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21654
21655       /* Don't do access checking if it is a templated function.  */
21656       if (processing_template_decl)
21657         push_deferring_access_checks (dk_no_check);
21658
21659       /* Now, parse the body of the function.  */
21660       cp_parser_function_definition_after_declarator (parser,
21661                                                       /*inline_p=*/true);
21662
21663       if (processing_template_decl)
21664         pop_deferring_access_checks ();
21665
21666       /* Leave the scope of the containing function.  */
21667       if (function_scope)
21668         pop_function_context ();
21669       cp_parser_pop_lexer (parser);
21670     }
21671
21672   /* Remove any template parameters from the symbol table.  */
21673   maybe_end_member_template_processing ();
21674
21675   /* Restore the queue.  */
21676   pop_unparsed_function_queues (parser);
21677   timevar_pop (TV_PARSE_INMETH);
21678 }
21679
21680 /* If DECL contains any default args, remember it on the unparsed
21681    functions queue.  */
21682
21683 static void
21684 cp_parser_save_default_args (cp_parser* parser, tree decl)
21685 {
21686   tree probe;
21687
21688   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21689        probe;
21690        probe = TREE_CHAIN (probe))
21691     if (TREE_PURPOSE (probe))
21692       {
21693         cp_default_arg_entry *entry
21694           = VEC_safe_push (cp_default_arg_entry, gc,
21695                            unparsed_funs_with_default_args, NULL);
21696         entry->class_type = current_class_type;
21697         entry->decl = decl;
21698         break;
21699       }
21700 }
21701
21702 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21703    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21704    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21705    from the parameter-type-list.  */
21706
21707 static tree
21708 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21709                                       tree default_arg, tree parmtype)
21710 {
21711   cp_token_cache *tokens;
21712   tree parsed_arg;
21713   bool dummy;
21714
21715   if (default_arg == error_mark_node)
21716     return error_mark_node;
21717
21718   /* Push the saved tokens for the default argument onto the parser's
21719      lexer stack.  */
21720   tokens = DEFARG_TOKENS (default_arg);
21721   cp_parser_push_lexer_for_tokens (parser, tokens);
21722
21723   start_lambda_scope (decl);
21724
21725   /* Parse the default argument.  */
21726   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21727   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21728     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21729
21730   finish_lambda_scope ();
21731
21732   if (!processing_template_decl)
21733     {
21734       /* In a non-template class, check conversions now.  In a template,
21735          we'll wait and instantiate these as needed.  */
21736       if (TREE_CODE (decl) == PARM_DECL)
21737         parsed_arg = check_default_argument (parmtype, parsed_arg);
21738       else
21739         {
21740           int flags = LOOKUP_IMPLICIT;
21741           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21742               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21743             flags = LOOKUP_NORMAL;
21744           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21745         }
21746     }
21747
21748   /* If the token stream has not been completely used up, then
21749      there was extra junk after the end of the default
21750      argument.  */
21751   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21752     {
21753       if (TREE_CODE (decl) == PARM_DECL)
21754         cp_parser_error (parser, "expected %<,%>");
21755       else
21756         cp_parser_error (parser, "expected %<;%>");
21757     }
21758
21759   /* Revert to the main lexer.  */
21760   cp_parser_pop_lexer (parser);
21761
21762   return parsed_arg;
21763 }
21764
21765 /* FIELD is a non-static data member with an initializer which we saved for
21766    later; parse it now.  */
21767
21768 static void
21769 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21770 {
21771   tree def;
21772
21773   push_unparsed_function_queues (parser);
21774   def = cp_parser_late_parse_one_default_arg (parser, field,
21775                                               DECL_INITIAL (field),
21776                                               NULL_TREE);
21777   pop_unparsed_function_queues (parser);
21778
21779   DECL_INITIAL (field) = def;
21780 }
21781
21782 /* FN is a FUNCTION_DECL which may contains a parameter with an
21783    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21784    assumes that the current scope is the scope in which the default
21785    argument should be processed.  */
21786
21787 static void
21788 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21789 {
21790   bool saved_local_variables_forbidden_p;
21791   tree parm, parmdecl;
21792
21793   /* While we're parsing the default args, we might (due to the
21794      statement expression extension) encounter more classes.  We want
21795      to handle them right away, but we don't want them getting mixed
21796      up with default args that are currently in the queue.  */
21797   push_unparsed_function_queues (parser);
21798
21799   /* Local variable names (and the `this' keyword) may not appear
21800      in a default argument.  */
21801   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21802   parser->local_variables_forbidden_p = true;
21803
21804   push_defarg_context (fn);
21805
21806   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21807          parmdecl = DECL_ARGUMENTS (fn);
21808        parm && parm != void_list_node;
21809        parm = TREE_CHAIN (parm),
21810          parmdecl = DECL_CHAIN (parmdecl))
21811     {
21812       tree default_arg = TREE_PURPOSE (parm);
21813       tree parsed_arg;
21814       VEC(tree,gc) *insts;
21815       tree copy;
21816       unsigned ix;
21817
21818       if (!default_arg)
21819         continue;
21820
21821       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21822         /* This can happen for a friend declaration for a function
21823            already declared with default arguments.  */
21824         continue;
21825
21826       parsed_arg
21827         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21828                                                 default_arg,
21829                                                 TREE_VALUE (parm));
21830       if (parsed_arg == error_mark_node)
21831         {
21832           continue;
21833         }
21834
21835       TREE_PURPOSE (parm) = parsed_arg;
21836
21837       /* Update any instantiations we've already created.  */
21838       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21839            VEC_iterate (tree, insts, ix, copy); ix++)
21840         TREE_PURPOSE (copy) = parsed_arg;
21841     }
21842
21843   pop_defarg_context ();
21844
21845   /* Make sure no default arg is missing.  */
21846   check_default_args (fn);
21847
21848   /* Restore the state of local_variables_forbidden_p.  */
21849   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21850
21851   /* Restore the queue.  */
21852   pop_unparsed_function_queues (parser);
21853 }
21854
21855 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21856    either a TYPE or an expression, depending on the form of the
21857    input.  The KEYWORD indicates which kind of expression we have
21858    encountered.  */
21859
21860 static tree
21861 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21862 {
21863   tree expr = NULL_TREE;
21864   const char *saved_message;
21865   char *tmp;
21866   bool saved_integral_constant_expression_p;
21867   bool saved_non_integral_constant_expression_p;
21868   bool pack_expansion_p = false;
21869
21870   /* Types cannot be defined in a `sizeof' expression.  Save away the
21871      old message.  */
21872   saved_message = parser->type_definition_forbidden_message;
21873   /* And create the new one.  */
21874   tmp = concat ("types may not be defined in %<",
21875                 IDENTIFIER_POINTER (ridpointers[keyword]),
21876                 "%> expressions", NULL);
21877   parser->type_definition_forbidden_message = tmp;
21878
21879   /* The restrictions on constant-expressions do not apply inside
21880      sizeof expressions.  */
21881   saved_integral_constant_expression_p
21882     = parser->integral_constant_expression_p;
21883   saved_non_integral_constant_expression_p
21884     = parser->non_integral_constant_expression_p;
21885   parser->integral_constant_expression_p = false;
21886
21887   /* If it's a `...', then we are computing the length of a parameter
21888      pack.  */
21889   if (keyword == RID_SIZEOF
21890       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21891     {
21892       /* Consume the `...'.  */
21893       cp_lexer_consume_token (parser->lexer);
21894       maybe_warn_variadic_templates ();
21895
21896       /* Note that this is an expansion.  */
21897       pack_expansion_p = true;
21898     }
21899
21900   /* Do not actually evaluate the expression.  */
21901   ++cp_unevaluated_operand;
21902   ++c_inhibit_evaluation_warnings;
21903   /* If it's a `(', then we might be looking at the type-id
21904      construction.  */
21905   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21906     {
21907       tree type;
21908       bool saved_in_type_id_in_expr_p;
21909
21910       /* We can't be sure yet whether we're looking at a type-id or an
21911          expression.  */
21912       cp_parser_parse_tentatively (parser);
21913       /* Consume the `('.  */
21914       cp_lexer_consume_token (parser->lexer);
21915       /* Parse the type-id.  */
21916       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21917       parser->in_type_id_in_expr_p = true;
21918       type = cp_parser_type_id (parser);
21919       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21920       /* Now, look for the trailing `)'.  */
21921       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21922       /* If all went well, then we're done.  */
21923       if (cp_parser_parse_definitely (parser))
21924         {
21925           cp_decl_specifier_seq decl_specs;
21926
21927           /* Build a trivial decl-specifier-seq.  */
21928           clear_decl_specs (&decl_specs);
21929           decl_specs.type = type;
21930
21931           /* Call grokdeclarator to figure out what type this is.  */
21932           expr = grokdeclarator (NULL,
21933                                  &decl_specs,
21934                                  TYPENAME,
21935                                  /*initialized=*/0,
21936                                  /*attrlist=*/NULL);
21937         }
21938     }
21939
21940   /* If the type-id production did not work out, then we must be
21941      looking at the unary-expression production.  */
21942   if (!expr)
21943     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21944                                        /*cast_p=*/false, NULL);
21945
21946   if (pack_expansion_p)
21947     /* Build a pack expansion. */
21948     expr = make_pack_expansion (expr);
21949
21950   /* Go back to evaluating expressions.  */
21951   --cp_unevaluated_operand;
21952   --c_inhibit_evaluation_warnings;
21953
21954   /* Free the message we created.  */
21955   free (tmp);
21956   /* And restore the old one.  */
21957   parser->type_definition_forbidden_message = saved_message;
21958   parser->integral_constant_expression_p
21959     = saved_integral_constant_expression_p;
21960   parser->non_integral_constant_expression_p
21961     = saved_non_integral_constant_expression_p;
21962
21963   return expr;
21964 }
21965
21966 /* If the current declaration has no declarator, return true.  */
21967
21968 static bool
21969 cp_parser_declares_only_class_p (cp_parser *parser)
21970 {
21971   /* If the next token is a `;' or a `,' then there is no
21972      declarator.  */
21973   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21974           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21975 }
21976
21977 /* Update the DECL_SPECS to reflect the storage class indicated by
21978    KEYWORD.  */
21979
21980 static void
21981 cp_parser_set_storage_class (cp_parser *parser,
21982                              cp_decl_specifier_seq *decl_specs,
21983                              enum rid keyword,
21984                              location_t location)
21985 {
21986   cp_storage_class storage_class;
21987
21988   if (parser->in_unbraced_linkage_specification_p)
21989     {
21990       error_at (location, "invalid use of %qD in linkage specification",
21991                 ridpointers[keyword]);
21992       return;
21993     }
21994   else if (decl_specs->storage_class != sc_none)
21995     {
21996       decl_specs->conflicting_specifiers_p = true;
21997       return;
21998     }
21999
22000   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22001       && decl_specs->specs[(int) ds_thread])
22002     {
22003       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
22004       decl_specs->specs[(int) ds_thread] = 0;
22005     }
22006
22007   switch (keyword)
22008     {
22009     case RID_AUTO:
22010       storage_class = sc_auto;
22011       break;
22012     case RID_REGISTER:
22013       storage_class = sc_register;
22014       break;
22015     case RID_STATIC:
22016       storage_class = sc_static;
22017       break;
22018     case RID_EXTERN:
22019       storage_class = sc_extern;
22020       break;
22021     case RID_MUTABLE:
22022       storage_class = sc_mutable;
22023       break;
22024     default:
22025       gcc_unreachable ();
22026     }
22027   decl_specs->storage_class = storage_class;
22028
22029   /* A storage class specifier cannot be applied alongside a typedef 
22030      specifier. If there is a typedef specifier present then set 
22031      conflicting_specifiers_p which will trigger an error later
22032      on in grokdeclarator. */
22033   if (decl_specs->specs[(int)ds_typedef])
22034     decl_specs->conflicting_specifiers_p = true;
22035 }
22036
22037 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22038    is true, the type is a class or enum definition.  */
22039
22040 static void
22041 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22042                               tree type_spec,
22043                               location_t location,
22044                               bool type_definition_p)
22045 {
22046   decl_specs->any_specifiers_p = true;
22047
22048   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22049      (with, for example, in "typedef int wchar_t;") we remember that
22050      this is what happened.  In system headers, we ignore these
22051      declarations so that G++ can work with system headers that are not
22052      C++-safe.  */
22053   if (decl_specs->specs[(int) ds_typedef]
22054       && !type_definition_p
22055       && (type_spec == boolean_type_node
22056           || type_spec == char16_type_node
22057           || type_spec == char32_type_node
22058           || type_spec == wchar_type_node)
22059       && (decl_specs->type
22060           || decl_specs->specs[(int) ds_long]
22061           || decl_specs->specs[(int) ds_short]
22062           || decl_specs->specs[(int) ds_unsigned]
22063           || decl_specs->specs[(int) ds_signed]))
22064     {
22065       decl_specs->redefined_builtin_type = type_spec;
22066       if (!decl_specs->type)
22067         {
22068           decl_specs->type = type_spec;
22069           decl_specs->type_definition_p = false;
22070           decl_specs->type_location = location;
22071         }
22072     }
22073   else if (decl_specs->type)
22074     decl_specs->multiple_types_p = true;
22075   else
22076     {
22077       decl_specs->type = type_spec;
22078       decl_specs->type_definition_p = type_definition_p;
22079       decl_specs->redefined_builtin_type = NULL_TREE;
22080       decl_specs->type_location = location;
22081     }
22082 }
22083
22084 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22085    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
22086
22087 static bool
22088 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22089 {
22090   return decl_specifiers->specs[(int) ds_friend] != 0;
22091 }
22092
22093 /* Issue an error message indicating that TOKEN_DESC was expected.
22094    If KEYWORD is true, it indicated this function is called by
22095    cp_parser_require_keword and the required token can only be
22096    a indicated keyword. */
22097
22098 static void
22099 cp_parser_required_error (cp_parser *parser,
22100                           required_token token_desc,
22101                           bool keyword)
22102 {
22103   switch (token_desc)
22104     {
22105       case RT_NEW:
22106         cp_parser_error (parser, "expected %<new%>");
22107         return;
22108       case RT_DELETE:
22109         cp_parser_error (parser, "expected %<delete%>");
22110         return;
22111       case RT_RETURN:
22112         cp_parser_error (parser, "expected %<return%>");
22113         return;
22114       case RT_WHILE:
22115         cp_parser_error (parser, "expected %<while%>");
22116         return;
22117       case RT_EXTERN:
22118         cp_parser_error (parser, "expected %<extern%>");
22119         return;
22120       case RT_STATIC_ASSERT:
22121         cp_parser_error (parser, "expected %<static_assert%>");
22122         return;
22123       case RT_DECLTYPE:
22124         cp_parser_error (parser, "expected %<decltype%>");
22125         return;
22126       case RT_OPERATOR:
22127         cp_parser_error (parser, "expected %<operator%>");
22128         return;
22129       case RT_CLASS:
22130         cp_parser_error (parser, "expected %<class%>");
22131         return;
22132       case RT_TEMPLATE:
22133         cp_parser_error (parser, "expected %<template%>");
22134         return;
22135       case RT_NAMESPACE:
22136         cp_parser_error (parser, "expected %<namespace%>");
22137         return;
22138       case RT_USING:
22139         cp_parser_error (parser, "expected %<using%>");
22140         return;
22141       case RT_ASM:
22142         cp_parser_error (parser, "expected %<asm%>");
22143         return;
22144       case RT_TRY:
22145         cp_parser_error (parser, "expected %<try%>");
22146         return;
22147       case RT_CATCH:
22148         cp_parser_error (parser, "expected %<catch%>");
22149         return;
22150       case RT_THROW:
22151         cp_parser_error (parser, "expected %<throw%>");
22152         return;
22153       case RT_LABEL:
22154         cp_parser_error (parser, "expected %<__label__%>");
22155         return;
22156       case RT_AT_TRY:
22157         cp_parser_error (parser, "expected %<@try%>");
22158         return;
22159       case RT_AT_SYNCHRONIZED:
22160         cp_parser_error (parser, "expected %<@synchronized%>");
22161         return;
22162       case RT_AT_THROW:
22163         cp_parser_error (parser, "expected %<@throw%>");
22164         return;
22165       case RT_TRANSACTION_ATOMIC:
22166         cp_parser_error (parser, "expected %<__transaction_atomic%>");
22167         return;
22168       case RT_TRANSACTION_RELAXED:
22169         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22170         return;
22171       default:
22172         break;
22173     }
22174   if (!keyword)
22175     {
22176       switch (token_desc)
22177         {
22178           case RT_SEMICOLON:
22179             cp_parser_error (parser, "expected %<;%>");
22180             return;
22181           case RT_OPEN_PAREN:
22182             cp_parser_error (parser, "expected %<(%>");
22183             return;
22184           case RT_CLOSE_BRACE:
22185             cp_parser_error (parser, "expected %<}%>");
22186             return;
22187           case RT_OPEN_BRACE:
22188             cp_parser_error (parser, "expected %<{%>");
22189             return;
22190           case RT_CLOSE_SQUARE:
22191             cp_parser_error (parser, "expected %<]%>");
22192             return;
22193           case RT_OPEN_SQUARE:
22194             cp_parser_error (parser, "expected %<[%>");
22195             return;
22196           case RT_COMMA:
22197             cp_parser_error (parser, "expected %<,%>");
22198             return;
22199           case RT_SCOPE:
22200             cp_parser_error (parser, "expected %<::%>");
22201             return;
22202           case RT_LESS:
22203             cp_parser_error (parser, "expected %<<%>");
22204             return;
22205           case RT_GREATER:
22206             cp_parser_error (parser, "expected %<>%>");
22207             return;
22208           case RT_EQ:
22209             cp_parser_error (parser, "expected %<=%>");
22210             return;
22211           case RT_ELLIPSIS:
22212             cp_parser_error (parser, "expected %<...%>");
22213             return;
22214           case RT_MULT:
22215             cp_parser_error (parser, "expected %<*%>");
22216             return;
22217           case RT_COMPL:
22218             cp_parser_error (parser, "expected %<~%>");
22219             return;
22220           case RT_COLON:
22221             cp_parser_error (parser, "expected %<:%>");
22222             return;
22223           case RT_COLON_SCOPE:
22224             cp_parser_error (parser, "expected %<:%> or %<::%>");
22225             return;
22226           case RT_CLOSE_PAREN:
22227             cp_parser_error (parser, "expected %<)%>");
22228             return;
22229           case RT_COMMA_CLOSE_PAREN:
22230             cp_parser_error (parser, "expected %<,%> or %<)%>");
22231             return;
22232           case RT_PRAGMA_EOL:
22233             cp_parser_error (parser, "expected end of line");
22234             return;
22235           case RT_NAME:
22236             cp_parser_error (parser, "expected identifier");
22237             return;
22238           case RT_SELECT:
22239             cp_parser_error (parser, "expected selection-statement");
22240             return;
22241           case RT_INTERATION:
22242             cp_parser_error (parser, "expected iteration-statement");
22243             return;
22244           case RT_JUMP:
22245             cp_parser_error (parser, "expected jump-statement");
22246             return;
22247           case RT_CLASS_KEY:
22248             cp_parser_error (parser, "expected class-key");
22249             return;
22250           case RT_CLASS_TYPENAME_TEMPLATE:
22251             cp_parser_error (parser,
22252                  "expected %<class%>, %<typename%>, or %<template%>");
22253             return;
22254           default:
22255             gcc_unreachable ();
22256         }
22257     }
22258   else
22259     gcc_unreachable ();
22260 }
22261
22262
22263
22264 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22265    issue an error message indicating that TOKEN_DESC was expected.
22266
22267    Returns the token consumed, if the token had the appropriate type.
22268    Otherwise, returns NULL.  */
22269
22270 static cp_token *
22271 cp_parser_require (cp_parser* parser,
22272                    enum cpp_ttype type,
22273                    required_token token_desc)
22274 {
22275   if (cp_lexer_next_token_is (parser->lexer, type))
22276     return cp_lexer_consume_token (parser->lexer);
22277   else
22278     {
22279       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22280       if (!cp_parser_simulate_error (parser))
22281         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22282       return NULL;
22283     }
22284 }
22285
22286 /* An error message is produced if the next token is not '>'.
22287    All further tokens are skipped until the desired token is
22288    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22289
22290 static void
22291 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22292 {
22293   /* Current level of '< ... >'.  */
22294   unsigned level = 0;
22295   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22296   unsigned nesting_depth = 0;
22297
22298   /* Are we ready, yet?  If not, issue error message.  */
22299   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22300     return;
22301
22302   /* Skip tokens until the desired token is found.  */
22303   while (true)
22304     {
22305       /* Peek at the next token.  */
22306       switch (cp_lexer_peek_token (parser->lexer)->type)
22307         {
22308         case CPP_LESS:
22309           if (!nesting_depth)
22310             ++level;
22311           break;
22312
22313         case CPP_RSHIFT:
22314           if (cxx_dialect == cxx98)
22315             /* C++0x views the `>>' operator as two `>' tokens, but
22316                C++98 does not. */
22317             break;
22318           else if (!nesting_depth && level-- == 0)
22319             {
22320               /* We've hit a `>>' where the first `>' closes the
22321                  template argument list, and the second `>' is
22322                  spurious.  Just consume the `>>' and stop; we've
22323                  already produced at least one error.  */
22324               cp_lexer_consume_token (parser->lexer);
22325               return;
22326             }
22327           /* Fall through for C++0x, so we handle the second `>' in
22328              the `>>'.  */
22329
22330         case CPP_GREATER:
22331           if (!nesting_depth && level-- == 0)
22332             {
22333               /* We've reached the token we want, consume it and stop.  */
22334               cp_lexer_consume_token (parser->lexer);
22335               return;
22336             }
22337           break;
22338
22339         case CPP_OPEN_PAREN:
22340         case CPP_OPEN_SQUARE:
22341           ++nesting_depth;
22342           break;
22343
22344         case CPP_CLOSE_PAREN:
22345         case CPP_CLOSE_SQUARE:
22346           if (nesting_depth-- == 0)
22347             return;
22348           break;
22349
22350         case CPP_EOF:
22351         case CPP_PRAGMA_EOL:
22352         case CPP_SEMICOLON:
22353         case CPP_OPEN_BRACE:
22354         case CPP_CLOSE_BRACE:
22355           /* The '>' was probably forgotten, don't look further.  */
22356           return;
22357
22358         default:
22359           break;
22360         }
22361
22362       /* Consume this token.  */
22363       cp_lexer_consume_token (parser->lexer);
22364     }
22365 }
22366
22367 /* If the next token is the indicated keyword, consume it.  Otherwise,
22368    issue an error message indicating that TOKEN_DESC was expected.
22369
22370    Returns the token consumed, if the token had the appropriate type.
22371    Otherwise, returns NULL.  */
22372
22373 static cp_token *
22374 cp_parser_require_keyword (cp_parser* parser,
22375                            enum rid keyword,
22376                            required_token token_desc)
22377 {
22378   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22379
22380   if (token && token->keyword != keyword)
22381     {
22382       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22383       return NULL;
22384     }
22385
22386   return token;
22387 }
22388
22389 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22390    function-definition.  */
22391
22392 static bool
22393 cp_parser_token_starts_function_definition_p (cp_token* token)
22394 {
22395   return (/* An ordinary function-body begins with an `{'.  */
22396           token->type == CPP_OPEN_BRACE
22397           /* A ctor-initializer begins with a `:'.  */
22398           || token->type == CPP_COLON
22399           /* A function-try-block begins with `try'.  */
22400           || token->keyword == RID_TRY
22401           /* A function-transaction-block begins with `__transaction_atomic'
22402              or `__transaction_relaxed'.  */
22403           || token->keyword == RID_TRANSACTION_ATOMIC
22404           || token->keyword == RID_TRANSACTION_RELAXED
22405           /* The named return value extension begins with `return'.  */
22406           || token->keyword == RID_RETURN);
22407 }
22408
22409 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22410    definition.  */
22411
22412 static bool
22413 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22414 {
22415   cp_token *token;
22416
22417   token = cp_lexer_peek_token (parser->lexer);
22418   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22419 }
22420
22421 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22422    C++0x) ending a template-argument.  */
22423
22424 static bool
22425 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22426 {
22427   cp_token *token;
22428
22429   token = cp_lexer_peek_token (parser->lexer);
22430   return (token->type == CPP_COMMA 
22431           || token->type == CPP_GREATER
22432           || token->type == CPP_ELLIPSIS
22433           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22434 }
22435
22436 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22437    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22438
22439 static bool
22440 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22441                                                      size_t n)
22442 {
22443   cp_token *token;
22444
22445   token = cp_lexer_peek_nth_token (parser->lexer, n);
22446   if (token->type == CPP_LESS)
22447     return true;
22448   /* Check for the sequence `<::' in the original code. It would be lexed as
22449      `[:', where `[' is a digraph, and there is no whitespace before
22450      `:'.  */
22451   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22452     {
22453       cp_token *token2;
22454       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22455       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22456         return true;
22457     }
22458   return false;
22459 }
22460
22461 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22462    or none_type otherwise.  */
22463
22464 static enum tag_types
22465 cp_parser_token_is_class_key (cp_token* token)
22466 {
22467   switch (token->keyword)
22468     {
22469     case RID_CLASS:
22470       return class_type;
22471     case RID_STRUCT:
22472       return record_type;
22473     case RID_UNION:
22474       return union_type;
22475
22476     default:
22477       return none_type;
22478     }
22479 }
22480
22481 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22482
22483 static void
22484 cp_parser_check_class_key (enum tag_types class_key, tree type)
22485 {
22486   if (type == error_mark_node)
22487     return;
22488   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22489     {
22490       permerror (input_location, "%qs tag used in naming %q#T",
22491                  class_key == union_type ? "union"
22492                  : class_key == record_type ? "struct" : "class",
22493                  type);
22494       inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22495               "%q#T was previously declared here", type);
22496     }
22497 }
22498
22499 /* Issue an error message if DECL is redeclared with different
22500    access than its original declaration [class.access.spec/3].
22501    This applies to nested classes and nested class templates.
22502    [class.mem/1].  */
22503
22504 static void
22505 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22506 {
22507   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22508     return;
22509
22510   if ((TREE_PRIVATE (decl)
22511        != (current_access_specifier == access_private_node))
22512       || (TREE_PROTECTED (decl)
22513           != (current_access_specifier == access_protected_node)))
22514     error_at (location, "%qD redeclared with different access", decl);
22515 }
22516
22517 /* Look for the `template' keyword, as a syntactic disambiguator.
22518    Return TRUE iff it is present, in which case it will be
22519    consumed.  */
22520
22521 static bool
22522 cp_parser_optional_template_keyword (cp_parser *parser)
22523 {
22524   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22525     {
22526       /* The `template' keyword can only be used within templates;
22527          outside templates the parser can always figure out what is a
22528          template and what is not.  */
22529       if (!processing_template_decl)
22530         {
22531           cp_token *token = cp_lexer_peek_token (parser->lexer);
22532           error_at (token->location,
22533                     "%<template%> (as a disambiguator) is only allowed "
22534                     "within templates");
22535           /* If this part of the token stream is rescanned, the same
22536              error message would be generated.  So, we purge the token
22537              from the stream.  */
22538           cp_lexer_purge_token (parser->lexer);
22539           return false;
22540         }
22541       else
22542         {
22543           /* Consume the `template' keyword.  */
22544           cp_lexer_consume_token (parser->lexer);
22545           return true;
22546         }
22547     }
22548
22549   return false;
22550 }
22551
22552 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22553    set PARSER->SCOPE, and perform other related actions.  */
22554
22555 static void
22556 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22557 {
22558   int i;
22559   struct tree_check *check_value;
22560   deferred_access_check *chk;
22561   VEC (deferred_access_check,gc) *checks;
22562
22563   /* Get the stored value.  */
22564   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22565   /* Perform any access checks that were deferred.  */
22566   checks = check_value->checks;
22567   if (checks)
22568     {
22569       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22570         perform_or_defer_access_check (chk->binfo,
22571                                        chk->decl,
22572                                        chk->diag_decl);
22573     }
22574   /* Set the scope from the stored value.  */
22575   parser->scope = check_value->value;
22576   parser->qualifying_scope = check_value->qualifying_scope;
22577   parser->object_scope = NULL_TREE;
22578 }
22579
22580 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22581    encounter the end of a block before what we were looking for.  */
22582
22583 static bool
22584 cp_parser_cache_group (cp_parser *parser,
22585                        enum cpp_ttype end,
22586                        unsigned depth)
22587 {
22588   while (true)
22589     {
22590       cp_token *token = cp_lexer_peek_token (parser->lexer);
22591
22592       /* Abort a parenthesized expression if we encounter a semicolon.  */
22593       if ((end == CPP_CLOSE_PAREN || depth == 0)
22594           && token->type == CPP_SEMICOLON)
22595         return true;
22596       /* If we've reached the end of the file, stop.  */
22597       if (token->type == CPP_EOF
22598           || (end != CPP_PRAGMA_EOL
22599               && token->type == CPP_PRAGMA_EOL))
22600         return true;
22601       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22602         /* We've hit the end of an enclosing block, so there's been some
22603            kind of syntax error.  */
22604         return true;
22605
22606       /* Consume the token.  */
22607       cp_lexer_consume_token (parser->lexer);
22608       /* See if it starts a new group.  */
22609       if (token->type == CPP_OPEN_BRACE)
22610         {
22611           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22612           /* In theory this should probably check end == '}', but
22613              cp_parser_save_member_function_body needs it to exit
22614              after either '}' or ')' when called with ')'.  */
22615           if (depth == 0)
22616             return false;
22617         }
22618       else if (token->type == CPP_OPEN_PAREN)
22619         {
22620           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22621           if (depth == 0 && end == CPP_CLOSE_PAREN)
22622             return false;
22623         }
22624       else if (token->type == CPP_PRAGMA)
22625         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22626       else if (token->type == end)
22627         return false;
22628     }
22629 }
22630
22631 /* Like above, for caching a default argument or NSDMI.  Both of these are
22632    terminated by a non-nested comma, but it can be unclear whether or not a
22633    comma is nested in a template argument list unless we do more parsing.
22634    In order to handle this ambiguity, when we encounter a ',' after a '<'
22635    we try to parse what follows as a parameter-declaration-list (in the
22636    case of a default argument) or a member-declarator (in the case of an
22637    NSDMI).  If that succeeds, then we stop caching.  */
22638
22639 static tree
22640 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22641 {
22642   unsigned depth = 0;
22643   int maybe_template_id = 0;
22644   cp_token *first_token;
22645   cp_token *token;
22646   tree default_argument;
22647
22648   /* Add tokens until we have processed the entire default
22649      argument.  We add the range [first_token, token).  */
22650   first_token = cp_lexer_peek_token (parser->lexer);
22651   if (first_token->type == CPP_OPEN_BRACE)
22652     {
22653       /* For list-initialization, this is straightforward.  */
22654       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22655       token = cp_lexer_peek_token (parser->lexer);
22656     }
22657   else while (true)
22658     {
22659       bool done = false;
22660
22661       /* Peek at the next token.  */
22662       token = cp_lexer_peek_token (parser->lexer);
22663       /* What we do depends on what token we have.  */
22664       switch (token->type)
22665         {
22666           /* In valid code, a default argument must be
22667              immediately followed by a `,' `)', or `...'.  */
22668         case CPP_COMMA:
22669           if (depth == 0 && maybe_template_id)
22670             {
22671               /* If we've seen a '<', we might be in a
22672                  template-argument-list.  Until Core issue 325 is
22673                  resolved, we don't know how this situation ought
22674                  to be handled, so try to DTRT.  We check whether
22675                  what comes after the comma is a valid parameter
22676                  declaration list.  If it is, then the comma ends
22677                  the default argument; otherwise the default
22678                  argument continues.  */
22679               bool error = false;
22680               tree t;
22681
22682               /* Set ITALP so cp_parser_parameter_declaration_list
22683                  doesn't decide to commit to this parse.  */
22684               bool saved_italp = parser->in_template_argument_list_p;
22685               parser->in_template_argument_list_p = true;
22686
22687               cp_parser_parse_tentatively (parser);
22688               cp_lexer_consume_token (parser->lexer);
22689
22690               if (nsdmi)
22691                 {
22692                   int ctor_dtor_or_conv_p;
22693                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22694                                         &ctor_dtor_or_conv_p,
22695                                         /*parenthesized_p=*/NULL,
22696                                         /*member_p=*/true);
22697                 }
22698               else
22699                 {
22700                   begin_scope (sk_function_parms, NULL_TREE);
22701                   cp_parser_parameter_declaration_list (parser, &error);
22702                   for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22703                     pop_binding (DECL_NAME (t), t);
22704                   leave_scope ();
22705                 }
22706               if (!cp_parser_error_occurred (parser) && !error)
22707                 done = true;
22708               cp_parser_abort_tentative_parse (parser);
22709
22710               parser->in_template_argument_list_p = saved_italp;
22711               break;
22712             }
22713         case CPP_CLOSE_PAREN:
22714         case CPP_ELLIPSIS:
22715           /* If we run into a non-nested `;', `}', or `]',
22716              then the code is invalid -- but the default
22717              argument is certainly over.  */
22718         case CPP_SEMICOLON:
22719         case CPP_CLOSE_BRACE:
22720         case CPP_CLOSE_SQUARE:
22721           if (depth == 0)
22722             done = true;
22723           /* Update DEPTH, if necessary.  */
22724           else if (token->type == CPP_CLOSE_PAREN
22725                    || token->type == CPP_CLOSE_BRACE
22726                    || token->type == CPP_CLOSE_SQUARE)
22727             --depth;
22728           break;
22729
22730         case CPP_OPEN_PAREN:
22731         case CPP_OPEN_SQUARE:
22732         case CPP_OPEN_BRACE:
22733           ++depth;
22734           break;
22735
22736         case CPP_LESS:
22737           if (depth == 0)
22738             /* This might be the comparison operator, or it might
22739                start a template argument list.  */
22740             ++maybe_template_id;
22741           break;
22742
22743         case CPP_RSHIFT:
22744           if (cxx_dialect == cxx98)
22745             break;
22746           /* Fall through for C++0x, which treats the `>>'
22747              operator like two `>' tokens in certain
22748              cases.  */
22749
22750         case CPP_GREATER:
22751           if (depth == 0)
22752             {
22753               /* This might be an operator, or it might close a
22754                  template argument list.  But if a previous '<'
22755                  started a template argument list, this will have
22756                  closed it, so we can't be in one anymore.  */
22757               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22758               if (maybe_template_id < 0)
22759                 maybe_template_id = 0;
22760             }
22761           break;
22762
22763           /* If we run out of tokens, issue an error message.  */
22764         case CPP_EOF:
22765         case CPP_PRAGMA_EOL:
22766           error_at (token->location, "file ends in default argument");
22767           done = true;
22768           break;
22769
22770         case CPP_NAME:
22771         case CPP_SCOPE:
22772           /* In these cases, we should look for template-ids.
22773              For example, if the default argument is
22774              `X<int, double>()', we need to do name lookup to
22775              figure out whether or not `X' is a template; if
22776              so, the `,' does not end the default argument.
22777
22778              That is not yet done.  */
22779           break;
22780
22781         default:
22782           break;
22783         }
22784
22785       /* If we've reached the end, stop.  */
22786       if (done)
22787         break;
22788
22789       /* Add the token to the token block.  */
22790       token = cp_lexer_consume_token (parser->lexer);
22791     }
22792
22793   /* Create a DEFAULT_ARG to represent the unparsed default
22794      argument.  */
22795   default_argument = make_node (DEFAULT_ARG);
22796   DEFARG_TOKENS (default_argument)
22797     = cp_token_cache_new (first_token, token);
22798   DEFARG_INSTANTIATIONS (default_argument) = NULL;
22799
22800   return default_argument;
22801 }
22802
22803 /* Begin parsing tentatively.  We always save tokens while parsing
22804    tentatively so that if the tentative parsing fails we can restore the
22805    tokens.  */
22806
22807 static void
22808 cp_parser_parse_tentatively (cp_parser* parser)
22809 {
22810   /* Enter a new parsing context.  */
22811   parser->context = cp_parser_context_new (parser->context);
22812   /* Begin saving tokens.  */
22813   cp_lexer_save_tokens (parser->lexer);
22814   /* In order to avoid repetitive access control error messages,
22815      access checks are queued up until we are no longer parsing
22816      tentatively.  */
22817   push_deferring_access_checks (dk_deferred);
22818 }
22819
22820 /* Commit to the currently active tentative parse.  */
22821
22822 static void
22823 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22824 {
22825   cp_parser_context *context;
22826   cp_lexer *lexer;
22827
22828   /* Mark all of the levels as committed.  */
22829   lexer = parser->lexer;
22830   for (context = parser->context; context->next; context = context->next)
22831     {
22832       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22833         break;
22834       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22835       while (!cp_lexer_saving_tokens (lexer))
22836         lexer = lexer->next;
22837       cp_lexer_commit_tokens (lexer);
22838     }
22839 }
22840
22841 /* Abort the currently active tentative parse.  All consumed tokens
22842    will be rolled back, and no diagnostics will be issued.  */
22843
22844 static void
22845 cp_parser_abort_tentative_parse (cp_parser* parser)
22846 {
22847   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22848               || errorcount > 0);
22849   cp_parser_simulate_error (parser);
22850   /* Now, pretend that we want to see if the construct was
22851      successfully parsed.  */
22852   cp_parser_parse_definitely (parser);
22853 }
22854
22855 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22856    token stream.  Otherwise, commit to the tokens we have consumed.
22857    Returns true if no error occurred; false otherwise.  */
22858
22859 static bool
22860 cp_parser_parse_definitely (cp_parser* parser)
22861 {
22862   bool error_occurred;
22863   cp_parser_context *context;
22864
22865   /* Remember whether or not an error occurred, since we are about to
22866      destroy that information.  */
22867   error_occurred = cp_parser_error_occurred (parser);
22868   /* Remove the topmost context from the stack.  */
22869   context = parser->context;
22870   parser->context = context->next;
22871   /* If no parse errors occurred, commit to the tentative parse.  */
22872   if (!error_occurred)
22873     {
22874       /* Commit to the tokens read tentatively, unless that was
22875          already done.  */
22876       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22877         cp_lexer_commit_tokens (parser->lexer);
22878
22879       pop_to_parent_deferring_access_checks ();
22880     }
22881   /* Otherwise, if errors occurred, roll back our state so that things
22882      are just as they were before we began the tentative parse.  */
22883   else
22884     {
22885       cp_lexer_rollback_tokens (parser->lexer);
22886       pop_deferring_access_checks ();
22887     }
22888   /* Add the context to the front of the free list.  */
22889   context->next = cp_parser_context_free_list;
22890   cp_parser_context_free_list = context;
22891
22892   return !error_occurred;
22893 }
22894
22895 /* Returns true if we are parsing tentatively and are not committed to
22896    this tentative parse.  */
22897
22898 static bool
22899 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22900 {
22901   return (cp_parser_parsing_tentatively (parser)
22902           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22903 }
22904
22905 /* Returns nonzero iff an error has occurred during the most recent
22906    tentative parse.  */
22907
22908 static bool
22909 cp_parser_error_occurred (cp_parser* parser)
22910 {
22911   return (cp_parser_parsing_tentatively (parser)
22912           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22913 }
22914
22915 /* Returns nonzero if GNU extensions are allowed.  */
22916
22917 static bool
22918 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22919 {
22920   return parser->allow_gnu_extensions_p;
22921 }
22922 \f
22923 /* Objective-C++ Productions */
22924
22925
22926 /* Parse an Objective-C expression, which feeds into a primary-expression
22927    above.
22928
22929    objc-expression:
22930      objc-message-expression
22931      objc-string-literal
22932      objc-encode-expression
22933      objc-protocol-expression
22934      objc-selector-expression
22935
22936   Returns a tree representation of the expression.  */
22937
22938 static tree
22939 cp_parser_objc_expression (cp_parser* parser)
22940 {
22941   /* Try to figure out what kind of declaration is present.  */
22942   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22943
22944   switch (kwd->type)
22945     {
22946     case CPP_OPEN_SQUARE:
22947       return cp_parser_objc_message_expression (parser);
22948
22949     case CPP_OBJC_STRING:
22950       kwd = cp_lexer_consume_token (parser->lexer);
22951       return objc_build_string_object (kwd->u.value);
22952
22953     case CPP_KEYWORD:
22954       switch (kwd->keyword)
22955         {
22956         case RID_AT_ENCODE:
22957           return cp_parser_objc_encode_expression (parser);
22958
22959         case RID_AT_PROTOCOL:
22960           return cp_parser_objc_protocol_expression (parser);
22961
22962         case RID_AT_SELECTOR:
22963           return cp_parser_objc_selector_expression (parser);
22964
22965         default:
22966           break;
22967         }
22968     default:
22969       error_at (kwd->location,
22970                 "misplaced %<@%D%> Objective-C++ construct",
22971                 kwd->u.value);
22972       cp_parser_skip_to_end_of_block_or_statement (parser);
22973     }
22974
22975   return error_mark_node;
22976 }
22977
22978 /* Parse an Objective-C message expression.
22979
22980    objc-message-expression:
22981      [ objc-message-receiver objc-message-args ]
22982
22983    Returns a representation of an Objective-C message.  */
22984
22985 static tree
22986 cp_parser_objc_message_expression (cp_parser* parser)
22987 {
22988   tree receiver, messageargs;
22989
22990   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
22991   receiver = cp_parser_objc_message_receiver (parser);
22992   messageargs = cp_parser_objc_message_args (parser);
22993   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22994
22995   return objc_build_message_expr (receiver, messageargs);
22996 }
22997
22998 /* Parse an objc-message-receiver.
22999
23000    objc-message-receiver:
23001      expression
23002      simple-type-specifier
23003
23004   Returns a representation of the type or expression.  */
23005
23006 static tree
23007 cp_parser_objc_message_receiver (cp_parser* parser)
23008 {
23009   tree rcv;
23010
23011   /* An Objective-C message receiver may be either (1) a type
23012      or (2) an expression.  */
23013   cp_parser_parse_tentatively (parser);
23014   rcv = cp_parser_expression (parser, false, NULL);
23015
23016   if (cp_parser_parse_definitely (parser))
23017     return rcv;
23018
23019   rcv = cp_parser_simple_type_specifier (parser,
23020                                          /*decl_specs=*/NULL,
23021                                          CP_PARSER_FLAGS_NONE);
23022
23023   return objc_get_class_reference (rcv);
23024 }
23025
23026 /* Parse the arguments and selectors comprising an Objective-C message.
23027
23028    objc-message-args:
23029      objc-selector
23030      objc-selector-args
23031      objc-selector-args , objc-comma-args
23032
23033    objc-selector-args:
23034      objc-selector [opt] : assignment-expression
23035      objc-selector-args objc-selector [opt] : assignment-expression
23036
23037    objc-comma-args:
23038      assignment-expression
23039      objc-comma-args , assignment-expression
23040
23041    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23042    selector arguments and TREE_VALUE containing a list of comma
23043    arguments.  */
23044
23045 static tree
23046 cp_parser_objc_message_args (cp_parser* parser)
23047 {
23048   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23049   bool maybe_unary_selector_p = true;
23050   cp_token *token = cp_lexer_peek_token (parser->lexer);
23051
23052   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23053     {
23054       tree selector = NULL_TREE, arg;
23055
23056       if (token->type != CPP_COLON)
23057         selector = cp_parser_objc_selector (parser);
23058
23059       /* Detect if we have a unary selector.  */
23060       if (maybe_unary_selector_p
23061           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23062         return build_tree_list (selector, NULL_TREE);
23063
23064       maybe_unary_selector_p = false;
23065       cp_parser_require (parser, CPP_COLON, RT_COLON);
23066       arg = cp_parser_assignment_expression (parser, false, NULL);
23067
23068       sel_args
23069         = chainon (sel_args,
23070                    build_tree_list (selector, arg));
23071
23072       token = cp_lexer_peek_token (parser->lexer);
23073     }
23074
23075   /* Handle non-selector arguments, if any. */
23076   while (token->type == CPP_COMMA)
23077     {
23078       tree arg;
23079
23080       cp_lexer_consume_token (parser->lexer);
23081       arg = cp_parser_assignment_expression (parser, false, NULL);
23082
23083       addl_args
23084         = chainon (addl_args,
23085                    build_tree_list (NULL_TREE, arg));
23086
23087       token = cp_lexer_peek_token (parser->lexer);
23088     }
23089
23090   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23091     {
23092       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23093       return build_tree_list (error_mark_node, error_mark_node);
23094     }
23095
23096   return build_tree_list (sel_args, addl_args);
23097 }
23098
23099 /* Parse an Objective-C encode expression.
23100
23101    objc-encode-expression:
23102      @encode objc-typename
23103
23104    Returns an encoded representation of the type argument.  */
23105
23106 static tree
23107 cp_parser_objc_encode_expression (cp_parser* parser)
23108 {
23109   tree type;
23110   cp_token *token;
23111
23112   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
23113   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23114   token = cp_lexer_peek_token (parser->lexer);
23115   type = complete_type (cp_parser_type_id (parser));
23116   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23117
23118   if (!type)
23119     {
23120       error_at (token->location, 
23121                 "%<@encode%> must specify a type as an argument");
23122       return error_mark_node;
23123     }
23124
23125   /* This happens if we find @encode(T) (where T is a template
23126      typename or something dependent on a template typename) when
23127      parsing a template.  In that case, we can't compile it
23128      immediately, but we rather create an AT_ENCODE_EXPR which will
23129      need to be instantiated when the template is used.
23130   */
23131   if (dependent_type_p (type))
23132     {
23133       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23134       TREE_READONLY (value) = 1;
23135       return value;
23136     }
23137
23138   return objc_build_encode_expr (type);
23139 }
23140
23141 /* Parse an Objective-C @defs expression.  */
23142
23143 static tree
23144 cp_parser_objc_defs_expression (cp_parser *parser)
23145 {
23146   tree name;
23147
23148   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
23149   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23150   name = cp_parser_identifier (parser);
23151   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23152
23153   return objc_get_class_ivars (name);
23154 }
23155
23156 /* Parse an Objective-C protocol expression.
23157
23158   objc-protocol-expression:
23159     @protocol ( identifier )
23160
23161   Returns a representation of the protocol expression.  */
23162
23163 static tree
23164 cp_parser_objc_protocol_expression (cp_parser* parser)
23165 {
23166   tree proto;
23167
23168   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23169   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23170   proto = cp_parser_identifier (parser);
23171   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23172
23173   return objc_build_protocol_expr (proto);
23174 }
23175
23176 /* Parse an Objective-C selector expression.
23177
23178    objc-selector-expression:
23179      @selector ( objc-method-signature )
23180
23181    objc-method-signature:
23182      objc-selector
23183      objc-selector-seq
23184
23185    objc-selector-seq:
23186      objc-selector :
23187      objc-selector-seq objc-selector :
23188
23189   Returns a representation of the method selector.  */
23190
23191 static tree
23192 cp_parser_objc_selector_expression (cp_parser* parser)
23193 {
23194   tree sel_seq = NULL_TREE;
23195   bool maybe_unary_selector_p = true;
23196   cp_token *token;
23197   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23198
23199   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
23200   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23201   token = cp_lexer_peek_token (parser->lexer);
23202
23203   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23204          || token->type == CPP_SCOPE)
23205     {
23206       tree selector = NULL_TREE;
23207
23208       if (token->type != CPP_COLON
23209           || token->type == CPP_SCOPE)
23210         selector = cp_parser_objc_selector (parser);
23211
23212       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23213           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23214         {
23215           /* Detect if we have a unary selector.  */
23216           if (maybe_unary_selector_p)
23217             {
23218               sel_seq = selector;
23219               goto finish_selector;
23220             }
23221           else
23222             {
23223               cp_parser_error (parser, "expected %<:%>");
23224             }
23225         }
23226       maybe_unary_selector_p = false;
23227       token = cp_lexer_consume_token (parser->lexer);
23228
23229       if (token->type == CPP_SCOPE)
23230         {
23231           sel_seq
23232             = chainon (sel_seq,
23233                        build_tree_list (selector, NULL_TREE));
23234           sel_seq
23235             = chainon (sel_seq,
23236                        build_tree_list (NULL_TREE, NULL_TREE));
23237         }
23238       else
23239         sel_seq
23240           = chainon (sel_seq,
23241                      build_tree_list (selector, NULL_TREE));
23242
23243       token = cp_lexer_peek_token (parser->lexer);
23244     }
23245
23246  finish_selector:
23247   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23248
23249   return objc_build_selector_expr (loc, sel_seq);
23250 }
23251
23252 /* Parse a list of identifiers.
23253
23254    objc-identifier-list:
23255      identifier
23256      objc-identifier-list , identifier
23257
23258    Returns a TREE_LIST of identifier nodes.  */
23259
23260 static tree
23261 cp_parser_objc_identifier_list (cp_parser* parser)
23262 {
23263   tree identifier;
23264   tree list;
23265   cp_token *sep;
23266
23267   identifier = cp_parser_identifier (parser);
23268   if (identifier == error_mark_node)
23269     return error_mark_node;      
23270
23271   list = build_tree_list (NULL_TREE, identifier);
23272   sep = cp_lexer_peek_token (parser->lexer);
23273
23274   while (sep->type == CPP_COMMA)
23275     {
23276       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23277       identifier = cp_parser_identifier (parser);
23278       if (identifier == error_mark_node)
23279         return list;
23280
23281       list = chainon (list, build_tree_list (NULL_TREE,
23282                                              identifier));
23283       sep = cp_lexer_peek_token (parser->lexer);
23284     }
23285   
23286   return list;
23287 }
23288
23289 /* Parse an Objective-C alias declaration.
23290
23291    objc-alias-declaration:
23292      @compatibility_alias identifier identifier ;
23293
23294    This function registers the alias mapping with the Objective-C front end.
23295    It returns nothing.  */
23296
23297 static void
23298 cp_parser_objc_alias_declaration (cp_parser* parser)
23299 {
23300   tree alias, orig;
23301
23302   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
23303   alias = cp_parser_identifier (parser);
23304   orig = cp_parser_identifier (parser);
23305   objc_declare_alias (alias, orig);
23306   cp_parser_consume_semicolon_at_end_of_statement (parser);
23307 }
23308
23309 /* Parse an Objective-C class forward-declaration.
23310
23311    objc-class-declaration:
23312      @class objc-identifier-list ;
23313
23314    The function registers the forward declarations with the Objective-C
23315    front end.  It returns nothing.  */
23316
23317 static void
23318 cp_parser_objc_class_declaration (cp_parser* parser)
23319 {
23320   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
23321   while (true)
23322     {
23323       tree id;
23324       
23325       id = cp_parser_identifier (parser);
23326       if (id == error_mark_node)
23327         break;
23328       
23329       objc_declare_class (id);
23330
23331       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23332         cp_lexer_consume_token (parser->lexer);
23333       else
23334         break;
23335     }
23336   cp_parser_consume_semicolon_at_end_of_statement (parser);
23337 }
23338
23339 /* Parse a list of Objective-C protocol references.
23340
23341    objc-protocol-refs-opt:
23342      objc-protocol-refs [opt]
23343
23344    objc-protocol-refs:
23345      < objc-identifier-list >
23346
23347    Returns a TREE_LIST of identifiers, if any.  */
23348
23349 static tree
23350 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23351 {
23352   tree protorefs = NULL_TREE;
23353
23354   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23355     {
23356       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
23357       protorefs = cp_parser_objc_identifier_list (parser);
23358       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23359     }
23360
23361   return protorefs;
23362 }
23363
23364 /* Parse a Objective-C visibility specification.  */
23365
23366 static void
23367 cp_parser_objc_visibility_spec (cp_parser* parser)
23368 {
23369   cp_token *vis = cp_lexer_peek_token (parser->lexer);
23370
23371   switch (vis->keyword)
23372     {
23373     case RID_AT_PRIVATE:
23374       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23375       break;
23376     case RID_AT_PROTECTED:
23377       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23378       break;
23379     case RID_AT_PUBLIC:
23380       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23381       break;
23382     case RID_AT_PACKAGE:
23383       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23384       break;
23385     default:
23386       return;
23387     }
23388
23389   /* Eat '@private'/'@protected'/'@public'.  */
23390   cp_lexer_consume_token (parser->lexer);
23391 }
23392
23393 /* Parse an Objective-C method type.  Return 'true' if it is a class
23394    (+) method, and 'false' if it is an instance (-) method.  */
23395
23396 static inline bool
23397 cp_parser_objc_method_type (cp_parser* parser)
23398 {
23399   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23400     return true;
23401   else
23402     return false;
23403 }
23404
23405 /* Parse an Objective-C protocol qualifier.  */
23406
23407 static tree
23408 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23409 {
23410   tree quals = NULL_TREE, node;
23411   cp_token *token = cp_lexer_peek_token (parser->lexer);
23412
23413   node = token->u.value;
23414
23415   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23416          && (node == ridpointers [(int) RID_IN]
23417              || node == ridpointers [(int) RID_OUT]
23418              || node == ridpointers [(int) RID_INOUT]
23419              || node == ridpointers [(int) RID_BYCOPY]
23420              || node == ridpointers [(int) RID_BYREF]
23421              || node == ridpointers [(int) RID_ONEWAY]))
23422     {
23423       quals = tree_cons (NULL_TREE, node, quals);
23424       cp_lexer_consume_token (parser->lexer);
23425       token = cp_lexer_peek_token (parser->lexer);
23426       node = token->u.value;
23427     }
23428
23429   return quals;
23430 }
23431
23432 /* Parse an Objective-C typename.  */
23433
23434 static tree
23435 cp_parser_objc_typename (cp_parser* parser)
23436 {
23437   tree type_name = NULL_TREE;
23438
23439   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23440     {
23441       tree proto_quals, cp_type = NULL_TREE;
23442
23443       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23444       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23445
23446       /* An ObjC type name may consist of just protocol qualifiers, in which
23447          case the type shall default to 'id'.  */
23448       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23449         {
23450           cp_type = cp_parser_type_id (parser);
23451           
23452           /* If the type could not be parsed, an error has already
23453              been produced.  For error recovery, behave as if it had
23454              not been specified, which will use the default type
23455              'id'.  */
23456           if (cp_type == error_mark_node)
23457             {
23458               cp_type = NULL_TREE;
23459               /* We need to skip to the closing parenthesis as
23460                  cp_parser_type_id() does not seem to do it for
23461                  us.  */
23462               cp_parser_skip_to_closing_parenthesis (parser,
23463                                                      /*recovering=*/true,
23464                                                      /*or_comma=*/false,
23465                                                      /*consume_paren=*/false);
23466             }
23467         }
23468
23469       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23470       type_name = build_tree_list (proto_quals, cp_type);
23471     }
23472
23473   return type_name;
23474 }
23475
23476 /* Check to see if TYPE refers to an Objective-C selector name.  */
23477
23478 static bool
23479 cp_parser_objc_selector_p (enum cpp_ttype type)
23480 {
23481   return (type == CPP_NAME || type == CPP_KEYWORD
23482           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23483           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23484           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23485           || type == CPP_XOR || type == CPP_XOR_EQ);
23486 }
23487
23488 /* Parse an Objective-C selector.  */
23489
23490 static tree
23491 cp_parser_objc_selector (cp_parser* parser)
23492 {
23493   cp_token *token = cp_lexer_consume_token (parser->lexer);
23494
23495   if (!cp_parser_objc_selector_p (token->type))
23496     {
23497       error_at (token->location, "invalid Objective-C++ selector name");
23498       return error_mark_node;
23499     }
23500
23501   /* C++ operator names are allowed to appear in ObjC selectors.  */
23502   switch (token->type)
23503     {
23504     case CPP_AND_AND: return get_identifier ("and");
23505     case CPP_AND_EQ: return get_identifier ("and_eq");
23506     case CPP_AND: return get_identifier ("bitand");
23507     case CPP_OR: return get_identifier ("bitor");
23508     case CPP_COMPL: return get_identifier ("compl");
23509     case CPP_NOT: return get_identifier ("not");
23510     case CPP_NOT_EQ: return get_identifier ("not_eq");
23511     case CPP_OR_OR: return get_identifier ("or");
23512     case CPP_OR_EQ: return get_identifier ("or_eq");
23513     case CPP_XOR: return get_identifier ("xor");
23514     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23515     default: return token->u.value;
23516     }
23517 }
23518
23519 /* Parse an Objective-C params list.  */
23520
23521 static tree
23522 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23523 {
23524   tree params = NULL_TREE;
23525   bool maybe_unary_selector_p = true;
23526   cp_token *token = cp_lexer_peek_token (parser->lexer);
23527
23528   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23529     {
23530       tree selector = NULL_TREE, type_name, identifier;
23531       tree parm_attr = NULL_TREE;
23532
23533       if (token->keyword == RID_ATTRIBUTE)
23534         break;
23535
23536       if (token->type != CPP_COLON)
23537         selector = cp_parser_objc_selector (parser);
23538
23539       /* Detect if we have a unary selector.  */
23540       if (maybe_unary_selector_p
23541           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23542         {
23543           params = selector; /* Might be followed by attributes.  */
23544           break;
23545         }
23546
23547       maybe_unary_selector_p = false;
23548       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23549         {
23550           /* Something went quite wrong.  There should be a colon
23551              here, but there is not.  Stop parsing parameters.  */
23552           break;
23553         }
23554       type_name = cp_parser_objc_typename (parser);
23555       /* New ObjC allows attributes on parameters too.  */
23556       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23557         parm_attr = cp_parser_attributes_opt (parser);
23558       identifier = cp_parser_identifier (parser);
23559
23560       params
23561         = chainon (params,
23562                    objc_build_keyword_decl (selector,
23563                                             type_name,
23564                                             identifier,
23565                                             parm_attr));
23566
23567       token = cp_lexer_peek_token (parser->lexer);
23568     }
23569
23570   if (params == NULL_TREE)
23571     {
23572       cp_parser_error (parser, "objective-c++ method declaration is expected");
23573       return error_mark_node;
23574     }
23575
23576   /* We allow tail attributes for the method.  */
23577   if (token->keyword == RID_ATTRIBUTE)
23578     {
23579       *attributes = cp_parser_attributes_opt (parser);
23580       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23581           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23582         return params;
23583       cp_parser_error (parser, 
23584                        "method attributes must be specified at the end");
23585       return error_mark_node;
23586     }
23587
23588   if (params == NULL_TREE)
23589     {
23590       cp_parser_error (parser, "objective-c++ method declaration is expected");
23591       return error_mark_node;
23592     }
23593   return params;
23594 }
23595
23596 /* Parse the non-keyword Objective-C params.  */
23597
23598 static tree
23599 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23600                                        tree* attributes)
23601 {
23602   tree params = make_node (TREE_LIST);
23603   cp_token *token = cp_lexer_peek_token (parser->lexer);
23604   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23605
23606   while (token->type == CPP_COMMA)
23607     {
23608       cp_parameter_declarator *parmdecl;
23609       tree parm;
23610
23611       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23612       token = cp_lexer_peek_token (parser->lexer);
23613
23614       if (token->type == CPP_ELLIPSIS)
23615         {
23616           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23617           *ellipsisp = true;
23618           token = cp_lexer_peek_token (parser->lexer);
23619           break;
23620         }
23621
23622       /* TODO: parse attributes for tail parameters.  */
23623       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23624       parm = grokdeclarator (parmdecl->declarator,
23625                              &parmdecl->decl_specifiers,
23626                              PARM, /*initialized=*/0,
23627                              /*attrlist=*/NULL);
23628
23629       chainon (params, build_tree_list (NULL_TREE, parm));
23630       token = cp_lexer_peek_token (parser->lexer);
23631     }
23632
23633   /* We allow tail attributes for the method.  */
23634   if (token->keyword == RID_ATTRIBUTE)
23635     {
23636       if (*attributes == NULL_TREE)
23637         {
23638           *attributes = cp_parser_attributes_opt (parser);
23639           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23640               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23641             return params;
23642         }
23643       else        
23644         /* We have an error, but parse the attributes, so that we can 
23645            carry on.  */
23646         *attributes = cp_parser_attributes_opt (parser);
23647
23648       cp_parser_error (parser, 
23649                        "method attributes must be specified at the end");
23650       return error_mark_node;
23651     }
23652
23653   return params;
23654 }
23655
23656 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23657
23658 static void
23659 cp_parser_objc_interstitial_code (cp_parser* parser)
23660 {
23661   cp_token *token = cp_lexer_peek_token (parser->lexer);
23662
23663   /* If the next token is `extern' and the following token is a string
23664      literal, then we have a linkage specification.  */
23665   if (token->keyword == RID_EXTERN
23666       && cp_parser_is_pure_string_literal
23667          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23668     cp_parser_linkage_specification (parser);
23669   /* Handle #pragma, if any.  */
23670   else if (token->type == CPP_PRAGMA)
23671     cp_parser_pragma (parser, pragma_external);
23672   /* Allow stray semicolons.  */
23673   else if (token->type == CPP_SEMICOLON)
23674     cp_lexer_consume_token (parser->lexer);
23675   /* Mark methods as optional or required, when building protocols.  */
23676   else if (token->keyword == RID_AT_OPTIONAL)
23677     {
23678       cp_lexer_consume_token (parser->lexer);
23679       objc_set_method_opt (true);
23680     }
23681   else if (token->keyword == RID_AT_REQUIRED)
23682     {
23683       cp_lexer_consume_token (parser->lexer);
23684       objc_set_method_opt (false);
23685     }
23686   else if (token->keyword == RID_NAMESPACE)
23687     cp_parser_namespace_definition (parser);
23688   /* Other stray characters must generate errors.  */
23689   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23690     {
23691       cp_lexer_consume_token (parser->lexer);
23692       error ("stray %qs between Objective-C++ methods",
23693              token->type == CPP_OPEN_BRACE ? "{" : "}");
23694     }
23695   /* Finally, try to parse a block-declaration, or a function-definition.  */
23696   else
23697     cp_parser_block_declaration (parser, /*statement_p=*/false);
23698 }
23699
23700 /* Parse a method signature.  */
23701
23702 static tree
23703 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23704 {
23705   tree rettype, kwdparms, optparms;
23706   bool ellipsis = false;
23707   bool is_class_method;
23708
23709   is_class_method = cp_parser_objc_method_type (parser);
23710   rettype = cp_parser_objc_typename (parser);
23711   *attributes = NULL_TREE;
23712   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23713   if (kwdparms == error_mark_node)
23714     return error_mark_node;
23715   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23716   if (optparms == error_mark_node)
23717     return error_mark_node;
23718
23719   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23720 }
23721
23722 static bool
23723 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23724 {
23725   tree tattr;  
23726   cp_lexer_save_tokens (parser->lexer);
23727   tattr = cp_parser_attributes_opt (parser);
23728   gcc_assert (tattr) ;
23729   
23730   /* If the attributes are followed by a method introducer, this is not allowed.
23731      Dump the attributes and flag the situation.  */
23732   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23733       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23734     return true;
23735
23736   /* Otherwise, the attributes introduce some interstitial code, possibly so
23737      rewind to allow that check.  */
23738   cp_lexer_rollback_tokens (parser->lexer);
23739   return false;  
23740 }
23741
23742 /* Parse an Objective-C method prototype list.  */
23743
23744 static void
23745 cp_parser_objc_method_prototype_list (cp_parser* parser)
23746 {
23747   cp_token *token = cp_lexer_peek_token (parser->lexer);
23748
23749   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23750     {
23751       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23752         {
23753           tree attributes, sig;
23754           bool is_class_method;
23755           if (token->type == CPP_PLUS)
23756             is_class_method = true;
23757           else
23758             is_class_method = false;
23759           sig = cp_parser_objc_method_signature (parser, &attributes);
23760           if (sig == error_mark_node)
23761             {
23762               cp_parser_skip_to_end_of_block_or_statement (parser);
23763               token = cp_lexer_peek_token (parser->lexer);
23764               continue;
23765             }
23766           objc_add_method_declaration (is_class_method, sig, attributes);
23767           cp_parser_consume_semicolon_at_end_of_statement (parser);
23768         }
23769       else if (token->keyword == RID_AT_PROPERTY)
23770         cp_parser_objc_at_property_declaration (parser);
23771       else if (token->keyword == RID_ATTRIBUTE 
23772                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23773         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23774                     OPT_Wattributes, 
23775                     "prefix attributes are ignored for methods");
23776       else
23777         /* Allow for interspersed non-ObjC++ code.  */
23778         cp_parser_objc_interstitial_code (parser);
23779
23780       token = cp_lexer_peek_token (parser->lexer);
23781     }
23782
23783   if (token->type != CPP_EOF)
23784     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23785   else
23786     cp_parser_error (parser, "expected %<@end%>");
23787
23788   objc_finish_interface ();
23789 }
23790
23791 /* Parse an Objective-C method definition list.  */
23792
23793 static void
23794 cp_parser_objc_method_definition_list (cp_parser* parser)
23795 {
23796   cp_token *token = cp_lexer_peek_token (parser->lexer);
23797
23798   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23799     {
23800       tree meth;
23801
23802       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23803         {
23804           cp_token *ptk;
23805           tree sig, attribute;
23806           bool is_class_method;
23807           if (token->type == CPP_PLUS)
23808             is_class_method = true;
23809           else
23810             is_class_method = false;
23811           push_deferring_access_checks (dk_deferred);
23812           sig = cp_parser_objc_method_signature (parser, &attribute);
23813           if (sig == error_mark_node)
23814             {
23815               cp_parser_skip_to_end_of_block_or_statement (parser);
23816               token = cp_lexer_peek_token (parser->lexer);
23817               continue;
23818             }
23819           objc_start_method_definition (is_class_method, sig, attribute,
23820                                         NULL_TREE);
23821
23822           /* For historical reasons, we accept an optional semicolon.  */
23823           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23824             cp_lexer_consume_token (parser->lexer);
23825
23826           ptk = cp_lexer_peek_token (parser->lexer);
23827           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23828                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23829             {
23830               perform_deferred_access_checks ();
23831               stop_deferring_access_checks ();
23832               meth = cp_parser_function_definition_after_declarator (parser,
23833                                                                      false);
23834               pop_deferring_access_checks ();
23835               objc_finish_method_definition (meth);
23836             }
23837         }
23838       /* The following case will be removed once @synthesize is
23839          completely implemented.  */
23840       else if (token->keyword == RID_AT_PROPERTY)
23841         cp_parser_objc_at_property_declaration (parser);
23842       else if (token->keyword == RID_AT_SYNTHESIZE)
23843         cp_parser_objc_at_synthesize_declaration (parser);
23844       else if (token->keyword == RID_AT_DYNAMIC)
23845         cp_parser_objc_at_dynamic_declaration (parser);
23846       else if (token->keyword == RID_ATTRIBUTE 
23847                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23848         warning_at (token->location, OPT_Wattributes,
23849                     "prefix attributes are ignored for methods");
23850       else
23851         /* Allow for interspersed non-ObjC++ code.  */
23852         cp_parser_objc_interstitial_code (parser);
23853
23854       token = cp_lexer_peek_token (parser->lexer);
23855     }
23856
23857   if (token->type != CPP_EOF)
23858     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23859   else
23860     cp_parser_error (parser, "expected %<@end%>");
23861
23862   objc_finish_implementation ();
23863 }
23864
23865 /* Parse Objective-C ivars.  */
23866
23867 static void
23868 cp_parser_objc_class_ivars (cp_parser* parser)
23869 {
23870   cp_token *token = cp_lexer_peek_token (parser->lexer);
23871
23872   if (token->type != CPP_OPEN_BRACE)
23873     return;     /* No ivars specified.  */
23874
23875   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23876   token = cp_lexer_peek_token (parser->lexer);
23877
23878   while (token->type != CPP_CLOSE_BRACE 
23879         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23880     {
23881       cp_decl_specifier_seq declspecs;
23882       int decl_class_or_enum_p;
23883       tree prefix_attributes;
23884
23885       cp_parser_objc_visibility_spec (parser);
23886
23887       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23888         break;
23889
23890       cp_parser_decl_specifier_seq (parser,
23891                                     CP_PARSER_FLAGS_OPTIONAL,
23892                                     &declspecs,
23893                                     &decl_class_or_enum_p);
23894
23895       /* auto, register, static, extern, mutable.  */
23896       if (declspecs.storage_class != sc_none)
23897         {
23898           cp_parser_error (parser, "invalid type for instance variable");         
23899           declspecs.storage_class = sc_none;
23900         }
23901
23902       /* __thread.  */
23903       if (declspecs.specs[(int) ds_thread])
23904         {
23905           cp_parser_error (parser, "invalid type for instance variable");
23906           declspecs.specs[(int) ds_thread] = 0;
23907         }
23908       
23909       /* typedef.  */
23910       if (declspecs.specs[(int) ds_typedef])
23911         {
23912           cp_parser_error (parser, "invalid type for instance variable");
23913           declspecs.specs[(int) ds_typedef] = 0;
23914         }
23915
23916       prefix_attributes = declspecs.attributes;
23917       declspecs.attributes = NULL_TREE;
23918
23919       /* Keep going until we hit the `;' at the end of the
23920          declaration.  */
23921       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23922         {
23923           tree width = NULL_TREE, attributes, first_attribute, decl;
23924           cp_declarator *declarator = NULL;
23925           int ctor_dtor_or_conv_p;
23926
23927           /* Check for a (possibly unnamed) bitfield declaration.  */
23928           token = cp_lexer_peek_token (parser->lexer);
23929           if (token->type == CPP_COLON)
23930             goto eat_colon;
23931
23932           if (token->type == CPP_NAME
23933               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23934                   == CPP_COLON))
23935             {
23936               /* Get the name of the bitfield.  */
23937               declarator = make_id_declarator (NULL_TREE,
23938                                                cp_parser_identifier (parser),
23939                                                sfk_none);
23940
23941              eat_colon:
23942               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23943               /* Get the width of the bitfield.  */
23944               width
23945                 = cp_parser_constant_expression (parser,
23946                                                  /*allow_non_constant=*/false,
23947                                                  NULL);
23948             }
23949           else
23950             {
23951               /* Parse the declarator.  */
23952               declarator
23953                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23954                                         &ctor_dtor_or_conv_p,
23955                                         /*parenthesized_p=*/NULL,
23956                                         /*member_p=*/false);
23957             }
23958
23959           /* Look for attributes that apply to the ivar.  */
23960           attributes = cp_parser_attributes_opt (parser);
23961           /* Remember which attributes are prefix attributes and
23962              which are not.  */
23963           first_attribute = attributes;
23964           /* Combine the attributes.  */
23965           attributes = chainon (prefix_attributes, attributes);
23966
23967           if (width)
23968               /* Create the bitfield declaration.  */
23969               decl = grokbitfield (declarator, &declspecs,
23970                                    width,
23971                                    attributes);
23972           else
23973             decl = grokfield (declarator, &declspecs,
23974                               NULL_TREE, /*init_const_expr_p=*/false,
23975                               NULL_TREE, attributes);
23976
23977           /* Add the instance variable.  */
23978           if (decl != error_mark_node && decl != NULL_TREE)
23979             objc_add_instance_variable (decl);
23980
23981           /* Reset PREFIX_ATTRIBUTES.  */
23982           while (attributes && TREE_CHAIN (attributes) != first_attribute)
23983             attributes = TREE_CHAIN (attributes);
23984           if (attributes)
23985             TREE_CHAIN (attributes) = NULL_TREE;
23986
23987           token = cp_lexer_peek_token (parser->lexer);
23988
23989           if (token->type == CPP_COMMA)
23990             {
23991               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23992               continue;
23993             }
23994           break;
23995         }
23996
23997       cp_parser_consume_semicolon_at_end_of_statement (parser);
23998       token = cp_lexer_peek_token (parser->lexer);
23999     }
24000
24001   if (token->keyword == RID_AT_END)
24002     cp_parser_error (parser, "expected %<}%>");
24003
24004   /* Do not consume the RID_AT_END, so it will be read again as terminating
24005      the @interface of @implementation.  */ 
24006   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24007     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
24008     
24009   /* For historical reasons, we accept an optional semicolon.  */
24010   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24011     cp_lexer_consume_token (parser->lexer);
24012 }
24013
24014 /* Parse an Objective-C protocol declaration.  */
24015
24016 static void
24017 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24018 {
24019   tree proto, protorefs;
24020   cp_token *tok;
24021
24022   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
24023   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24024     {
24025       tok = cp_lexer_peek_token (parser->lexer);
24026       error_at (tok->location, "identifier expected after %<@protocol%>");
24027       cp_parser_consume_semicolon_at_end_of_statement (parser);
24028       return;
24029     }
24030
24031   /* See if we have a forward declaration or a definition.  */
24032   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24033
24034   /* Try a forward declaration first.  */
24035   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24036     {
24037       while (true)
24038         {
24039           tree id;
24040           
24041           id = cp_parser_identifier (parser);
24042           if (id == error_mark_node)
24043             break;
24044           
24045           objc_declare_protocol (id, attributes);
24046           
24047           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24048             cp_lexer_consume_token (parser->lexer);
24049           else
24050             break;
24051         }
24052       cp_parser_consume_semicolon_at_end_of_statement (parser);
24053     }
24054
24055   /* Ok, we got a full-fledged definition (or at least should).  */
24056   else
24057     {
24058       proto = cp_parser_identifier (parser);
24059       protorefs = cp_parser_objc_protocol_refs_opt (parser);
24060       objc_start_protocol (proto, protorefs, attributes);
24061       cp_parser_objc_method_prototype_list (parser);
24062     }
24063 }
24064
24065 /* Parse an Objective-C superclass or category.  */
24066
24067 static void
24068 cp_parser_objc_superclass_or_category (cp_parser *parser, 
24069                                        bool iface_p,
24070                                        tree *super,
24071                                        tree *categ, bool *is_class_extension)
24072 {
24073   cp_token *next = cp_lexer_peek_token (parser->lexer);
24074
24075   *super = *categ = NULL_TREE;
24076   *is_class_extension = false;
24077   if (next->type == CPP_COLON)
24078     {
24079       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24080       *super = cp_parser_identifier (parser);
24081     }
24082   else if (next->type == CPP_OPEN_PAREN)
24083     {
24084       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
24085
24086       /* If there is no category name, and this is an @interface, we
24087          have a class extension.  */
24088       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24089         {
24090           *categ = NULL_TREE;
24091           *is_class_extension = true;
24092         }
24093       else
24094         *categ = cp_parser_identifier (parser);
24095
24096       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24097     }
24098 }
24099
24100 /* Parse an Objective-C class interface.  */
24101
24102 static void
24103 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24104 {
24105   tree name, super, categ, protos;
24106   bool is_class_extension;
24107
24108   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
24109   name = cp_parser_identifier (parser);
24110   if (name == error_mark_node)
24111     {
24112       /* It's hard to recover because even if valid @interface stuff
24113          is to follow, we can't compile it (or validate it) if we
24114          don't even know which class it refers to.  Let's assume this
24115          was a stray '@interface' token in the stream and skip it.
24116       */
24117       return;
24118     }
24119   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24120                                          &is_class_extension);
24121   protos = cp_parser_objc_protocol_refs_opt (parser);
24122
24123   /* We have either a class or a category on our hands.  */
24124   if (categ || is_class_extension)
24125     objc_start_category_interface (name, categ, protos, attributes);
24126   else
24127     {
24128       objc_start_class_interface (name, super, protos, attributes);
24129       /* Handle instance variable declarations, if any.  */
24130       cp_parser_objc_class_ivars (parser);
24131       objc_continue_interface ();
24132     }
24133
24134   cp_parser_objc_method_prototype_list (parser);
24135 }
24136
24137 /* Parse an Objective-C class implementation.  */
24138
24139 static void
24140 cp_parser_objc_class_implementation (cp_parser* parser)
24141 {
24142   tree name, super, categ;
24143   bool is_class_extension;
24144
24145   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
24146   name = cp_parser_identifier (parser);
24147   if (name == error_mark_node)
24148     {
24149       /* It's hard to recover because even if valid @implementation
24150          stuff is to follow, we can't compile it (or validate it) if
24151          we don't even know which class it refers to.  Let's assume
24152          this was a stray '@implementation' token in the stream and
24153          skip it.
24154       */
24155       return;
24156     }
24157   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24158                                          &is_class_extension);
24159
24160   /* We have either a class or a category on our hands.  */
24161   if (categ)
24162     objc_start_category_implementation (name, categ);
24163   else
24164     {
24165       objc_start_class_implementation (name, super);
24166       /* Handle instance variable declarations, if any.  */
24167       cp_parser_objc_class_ivars (parser);
24168       objc_continue_implementation ();
24169     }
24170
24171   cp_parser_objc_method_definition_list (parser);
24172 }
24173
24174 /* Consume the @end token and finish off the implementation.  */
24175
24176 static void
24177 cp_parser_objc_end_implementation (cp_parser* parser)
24178 {
24179   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24180   objc_finish_implementation ();
24181 }
24182
24183 /* Parse an Objective-C declaration.  */
24184
24185 static void
24186 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24187 {
24188   /* Try to figure out what kind of declaration is present.  */
24189   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24190
24191   if (attributes)
24192     switch (kwd->keyword)
24193       {
24194         case RID_AT_ALIAS:
24195         case RID_AT_CLASS:
24196         case RID_AT_END:
24197           error_at (kwd->location, "attributes may not be specified before"
24198                     " the %<@%D%> Objective-C++ keyword",
24199                     kwd->u.value);
24200           attributes = NULL;
24201           break;
24202         case RID_AT_IMPLEMENTATION:
24203           warning_at (kwd->location, OPT_Wattributes,
24204                       "prefix attributes are ignored before %<@%D%>",
24205                       kwd->u.value);
24206           attributes = NULL;
24207         default:
24208           break;
24209       }
24210
24211   switch (kwd->keyword)
24212     {
24213     case RID_AT_ALIAS:
24214       cp_parser_objc_alias_declaration (parser);
24215       break;
24216     case RID_AT_CLASS:
24217       cp_parser_objc_class_declaration (parser);
24218       break;
24219     case RID_AT_PROTOCOL:
24220       cp_parser_objc_protocol_declaration (parser, attributes);
24221       break;
24222     case RID_AT_INTERFACE:
24223       cp_parser_objc_class_interface (parser, attributes);
24224       break;
24225     case RID_AT_IMPLEMENTATION:
24226       cp_parser_objc_class_implementation (parser);
24227       break;
24228     case RID_AT_END:
24229       cp_parser_objc_end_implementation (parser);
24230       break;
24231     default:
24232       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24233                 kwd->u.value);
24234       cp_parser_skip_to_end_of_block_or_statement (parser);
24235     }
24236 }
24237
24238 /* Parse an Objective-C try-catch-finally statement.
24239
24240    objc-try-catch-finally-stmt:
24241      @try compound-statement objc-catch-clause-seq [opt]
24242        objc-finally-clause [opt]
24243
24244    objc-catch-clause-seq:
24245      objc-catch-clause objc-catch-clause-seq [opt]
24246
24247    objc-catch-clause:
24248      @catch ( objc-exception-declaration ) compound-statement
24249
24250    objc-finally-clause:
24251      @finally compound-statement
24252
24253    objc-exception-declaration:
24254      parameter-declaration
24255      '...'
24256
24257    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24258
24259    Returns NULL_TREE.
24260
24261    PS: This function is identical to c_parser_objc_try_catch_finally_statement
24262    for C.  Keep them in sync.  */   
24263
24264 static tree
24265 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24266 {
24267   location_t location;
24268   tree stmt;
24269
24270   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24271   location = cp_lexer_peek_token (parser->lexer)->location;
24272   objc_maybe_warn_exceptions (location);
24273   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24274      node, lest it get absorbed into the surrounding block.  */
24275   stmt = push_stmt_list ();
24276   cp_parser_compound_statement (parser, NULL, false, false);
24277   objc_begin_try_stmt (location, pop_stmt_list (stmt));
24278
24279   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24280     {
24281       cp_parameter_declarator *parm;
24282       tree parameter_declaration = error_mark_node;
24283       bool seen_open_paren = false;
24284
24285       cp_lexer_consume_token (parser->lexer);
24286       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24287         seen_open_paren = true;
24288       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24289         {
24290           /* We have "@catch (...)" (where the '...' are literally
24291              what is in the code).  Skip the '...'.
24292              parameter_declaration is set to NULL_TREE, and
24293              objc_being_catch_clauses() knows that that means
24294              '...'.  */
24295           cp_lexer_consume_token (parser->lexer);
24296           parameter_declaration = NULL_TREE;
24297         }
24298       else
24299         {
24300           /* We have "@catch (NSException *exception)" or something
24301              like that.  Parse the parameter declaration.  */
24302           parm = cp_parser_parameter_declaration (parser, false, NULL);
24303           if (parm == NULL)
24304             parameter_declaration = error_mark_node;
24305           else
24306             parameter_declaration = grokdeclarator (parm->declarator,
24307                                                     &parm->decl_specifiers,
24308                                                     PARM, /*initialized=*/0,
24309                                                     /*attrlist=*/NULL);
24310         }
24311       if (seen_open_paren)
24312         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24313       else
24314         {
24315           /* If there was no open parenthesis, we are recovering from
24316              an error, and we are trying to figure out what mistake
24317              the user has made.  */
24318
24319           /* If there is an immediate closing parenthesis, the user
24320              probably forgot the opening one (ie, they typed "@catch
24321              NSException *e)".  Parse the closing parenthesis and keep
24322              going.  */
24323           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24324             cp_lexer_consume_token (parser->lexer);
24325           
24326           /* If these is no immediate closing parenthesis, the user
24327              probably doesn't know that parenthesis are required at
24328              all (ie, they typed "@catch NSException *e").  So, just
24329              forget about the closing parenthesis and keep going.  */
24330         }
24331       objc_begin_catch_clause (parameter_declaration);
24332       cp_parser_compound_statement (parser, NULL, false, false);
24333       objc_finish_catch_clause ();
24334     }
24335   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24336     {
24337       cp_lexer_consume_token (parser->lexer);
24338       location = cp_lexer_peek_token (parser->lexer)->location;
24339       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24340          node, lest it get absorbed into the surrounding block.  */
24341       stmt = push_stmt_list ();
24342       cp_parser_compound_statement (parser, NULL, false, false);
24343       objc_build_finally_clause (location, pop_stmt_list (stmt));
24344     }
24345
24346   return objc_finish_try_stmt ();
24347 }
24348
24349 /* Parse an Objective-C synchronized statement.
24350
24351    objc-synchronized-stmt:
24352      @synchronized ( expression ) compound-statement
24353
24354    Returns NULL_TREE.  */
24355
24356 static tree
24357 cp_parser_objc_synchronized_statement (cp_parser *parser)
24358 {
24359   location_t location;
24360   tree lock, stmt;
24361
24362   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24363
24364   location = cp_lexer_peek_token (parser->lexer)->location;
24365   objc_maybe_warn_exceptions (location);
24366   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24367   lock = cp_parser_expression (parser, false, NULL);
24368   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24369
24370   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24371      node, lest it get absorbed into the surrounding block.  */
24372   stmt = push_stmt_list ();
24373   cp_parser_compound_statement (parser, NULL, false, false);
24374
24375   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24376 }
24377
24378 /* Parse an Objective-C throw statement.
24379
24380    objc-throw-stmt:
24381      @throw assignment-expression [opt] ;
24382
24383    Returns a constructed '@throw' statement.  */
24384
24385 static tree
24386 cp_parser_objc_throw_statement (cp_parser *parser)
24387 {
24388   tree expr = NULL_TREE;
24389   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24390
24391   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24392
24393   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24394     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24395
24396   cp_parser_consume_semicolon_at_end_of_statement (parser);
24397
24398   return objc_build_throw_stmt (loc, expr);
24399 }
24400
24401 /* Parse an Objective-C statement.  */
24402
24403 static tree
24404 cp_parser_objc_statement (cp_parser * parser)
24405 {
24406   /* Try to figure out what kind of declaration is present.  */
24407   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24408
24409   switch (kwd->keyword)
24410     {
24411     case RID_AT_TRY:
24412       return cp_parser_objc_try_catch_finally_statement (parser);
24413     case RID_AT_SYNCHRONIZED:
24414       return cp_parser_objc_synchronized_statement (parser);
24415     case RID_AT_THROW:
24416       return cp_parser_objc_throw_statement (parser);
24417     default:
24418       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24419                kwd->u.value);
24420       cp_parser_skip_to_end_of_block_or_statement (parser);
24421     }
24422
24423   return error_mark_node;
24424 }
24425
24426 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24427    look ahead to see if an objc keyword follows the attributes.  This
24428    is to detect the use of prefix attributes on ObjC @interface and 
24429    @protocol.  */
24430
24431 static bool
24432 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24433 {
24434   cp_lexer_save_tokens (parser->lexer);
24435   *attrib = cp_parser_attributes_opt (parser);
24436   gcc_assert (*attrib);
24437   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24438     {
24439       cp_lexer_commit_tokens (parser->lexer);
24440       return true;
24441     }
24442   cp_lexer_rollback_tokens (parser->lexer);
24443   return false;  
24444 }
24445
24446 /* This routine is a minimal replacement for
24447    c_parser_struct_declaration () used when parsing the list of
24448    types/names or ObjC++ properties.  For example, when parsing the
24449    code
24450
24451    @property (readonly) int a, b, c;
24452
24453    this function is responsible for parsing "int a, int b, int c" and
24454    returning the declarations as CHAIN of DECLs.
24455
24456    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24457    similar parsing.  */
24458 static tree
24459 cp_parser_objc_struct_declaration (cp_parser *parser)
24460 {
24461   tree decls = NULL_TREE;
24462   cp_decl_specifier_seq declspecs;
24463   int decl_class_or_enum_p;
24464   tree prefix_attributes;
24465
24466   cp_parser_decl_specifier_seq (parser,
24467                                 CP_PARSER_FLAGS_NONE,
24468                                 &declspecs,
24469                                 &decl_class_or_enum_p);
24470
24471   if (declspecs.type == error_mark_node)
24472     return error_mark_node;
24473
24474   /* auto, register, static, extern, mutable.  */
24475   if (declspecs.storage_class != sc_none)
24476     {
24477       cp_parser_error (parser, "invalid type for property");
24478       declspecs.storage_class = sc_none;
24479     }
24480   
24481   /* __thread.  */
24482   if (declspecs.specs[(int) ds_thread])
24483     {
24484       cp_parser_error (parser, "invalid type for property");
24485       declspecs.specs[(int) ds_thread] = 0;
24486     }
24487   
24488   /* typedef.  */
24489   if (declspecs.specs[(int) ds_typedef])
24490     {
24491       cp_parser_error (parser, "invalid type for property");
24492       declspecs.specs[(int) ds_typedef] = 0;
24493     }
24494
24495   prefix_attributes = declspecs.attributes;
24496   declspecs.attributes = NULL_TREE;
24497
24498   /* Keep going until we hit the `;' at the end of the declaration. */
24499   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24500     {
24501       tree attributes, first_attribute, decl;
24502       cp_declarator *declarator;
24503       cp_token *token;
24504
24505       /* Parse the declarator.  */
24506       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24507                                          NULL, NULL, false);
24508
24509       /* Look for attributes that apply to the ivar.  */
24510       attributes = cp_parser_attributes_opt (parser);
24511       /* Remember which attributes are prefix attributes and
24512          which are not.  */
24513       first_attribute = attributes;
24514       /* Combine the attributes.  */
24515       attributes = chainon (prefix_attributes, attributes);
24516       
24517       decl = grokfield (declarator, &declspecs,
24518                         NULL_TREE, /*init_const_expr_p=*/false,
24519                         NULL_TREE, attributes);
24520
24521       if (decl == error_mark_node || decl == NULL_TREE)
24522         return error_mark_node;
24523       
24524       /* Reset PREFIX_ATTRIBUTES.  */
24525       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24526         attributes = TREE_CHAIN (attributes);
24527       if (attributes)
24528         TREE_CHAIN (attributes) = NULL_TREE;
24529
24530       DECL_CHAIN (decl) = decls;
24531       decls = decl;
24532
24533       token = cp_lexer_peek_token (parser->lexer);
24534       if (token->type == CPP_COMMA)
24535         {
24536           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24537           continue;
24538         }
24539       else
24540         break;
24541     }
24542   return decls;
24543 }
24544
24545 /* Parse an Objective-C @property declaration.  The syntax is:
24546
24547    objc-property-declaration:
24548      '@property' objc-property-attributes[opt] struct-declaration ;
24549
24550    objc-property-attributes:
24551     '(' objc-property-attribute-list ')'
24552
24553    objc-property-attribute-list:
24554      objc-property-attribute
24555      objc-property-attribute-list, objc-property-attribute
24556
24557    objc-property-attribute
24558      'getter' = identifier
24559      'setter' = identifier
24560      'readonly'
24561      'readwrite'
24562      'assign'
24563      'retain'
24564      'copy'
24565      'nonatomic'
24566
24567   For example:
24568     @property NSString *name;
24569     @property (readonly) id object;
24570     @property (retain, nonatomic, getter=getTheName) id name;
24571     @property int a, b, c;
24572
24573    PS: This function is identical to
24574    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24575 static void 
24576 cp_parser_objc_at_property_declaration (cp_parser *parser)
24577 {
24578   /* The following variables hold the attributes of the properties as
24579      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24580      seen.  When we see an attribute, we set them to 'true' (if they
24581      are boolean properties) or to the identifier (if they have an
24582      argument, ie, for getter and setter).  Note that here we only
24583      parse the list of attributes, check the syntax and accumulate the
24584      attributes that we find.  objc_add_property_declaration() will
24585      then process the information.  */
24586   bool property_assign = false;
24587   bool property_copy = false;
24588   tree property_getter_ident = NULL_TREE;
24589   bool property_nonatomic = false;
24590   bool property_readonly = false;
24591   bool property_readwrite = false;
24592   bool property_retain = false;
24593   tree property_setter_ident = NULL_TREE;
24594
24595   /* 'properties' is the list of properties that we read.  Usually a
24596      single one, but maybe more (eg, in "@property int a, b, c;" there
24597      are three).  */
24598   tree properties;
24599   location_t loc;
24600
24601   loc = cp_lexer_peek_token (parser->lexer)->location;
24602
24603   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24604
24605   /* Parse the optional attribute list...  */
24606   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24607     {
24608       /* Eat the '('.  */
24609       cp_lexer_consume_token (parser->lexer);
24610
24611       while (true)
24612         {
24613           bool syntax_error = false;
24614           cp_token *token = cp_lexer_peek_token (parser->lexer);
24615           enum rid keyword;
24616
24617           if (token->type != CPP_NAME)
24618             {
24619               cp_parser_error (parser, "expected identifier");
24620               break;
24621             }
24622           keyword = C_RID_CODE (token->u.value);
24623           cp_lexer_consume_token (parser->lexer);
24624           switch (keyword)
24625             {
24626             case RID_ASSIGN:    property_assign = true;    break;
24627             case RID_COPY:      property_copy = true;      break;
24628             case RID_NONATOMIC: property_nonatomic = true; break;
24629             case RID_READONLY:  property_readonly = true;  break;
24630             case RID_READWRITE: property_readwrite = true; break;
24631             case RID_RETAIN:    property_retain = true;    break;
24632
24633             case RID_GETTER:
24634             case RID_SETTER:
24635               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24636                 {
24637                   if (keyword == RID_GETTER)
24638                     cp_parser_error (parser,
24639                                      "missing %<=%> (after %<getter%> attribute)");
24640                   else
24641                     cp_parser_error (parser,
24642                                      "missing %<=%> (after %<setter%> attribute)");
24643                   syntax_error = true;
24644                   break;
24645                 }
24646               cp_lexer_consume_token (parser->lexer); /* eat the = */
24647               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24648                 {
24649                   cp_parser_error (parser, "expected identifier");
24650                   syntax_error = true;
24651                   break;
24652                 }
24653               if (keyword == RID_SETTER)
24654                 {
24655                   if (property_setter_ident != NULL_TREE)
24656                     {
24657                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24658                       cp_lexer_consume_token (parser->lexer);
24659                     }
24660                   else
24661                     property_setter_ident = cp_parser_objc_selector (parser);
24662                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24663                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24664                   else
24665                     cp_lexer_consume_token (parser->lexer);
24666                 }
24667               else
24668                 {
24669                   if (property_getter_ident != NULL_TREE)
24670                     {
24671                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24672                       cp_lexer_consume_token (parser->lexer);
24673                     }
24674                   else
24675                     property_getter_ident = cp_parser_objc_selector (parser);
24676                 }
24677               break;
24678             default:
24679               cp_parser_error (parser, "unknown property attribute");
24680               syntax_error = true;
24681               break;
24682             }
24683
24684           if (syntax_error)
24685             break;
24686
24687           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24688             cp_lexer_consume_token (parser->lexer);
24689           else
24690             break;
24691         }
24692
24693       /* FIXME: "@property (setter, assign);" will generate a spurious
24694          "error: expected â€˜)’ before â€˜,’ token".  This is because
24695          cp_parser_require, unlike the C counterpart, will produce an
24696          error even if we are in error recovery.  */
24697       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24698         {
24699           cp_parser_skip_to_closing_parenthesis (parser,
24700                                                  /*recovering=*/true,
24701                                                  /*or_comma=*/false,
24702                                                  /*consume_paren=*/true);
24703         }
24704     }
24705
24706   /* ... and the property declaration(s).  */
24707   properties = cp_parser_objc_struct_declaration (parser);
24708
24709   if (properties == error_mark_node)
24710     {
24711       cp_parser_skip_to_end_of_statement (parser);
24712       /* If the next token is now a `;', consume it.  */
24713       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24714         cp_lexer_consume_token (parser->lexer);
24715       return;
24716     }
24717
24718   if (properties == NULL_TREE)
24719     cp_parser_error (parser, "expected identifier");
24720   else
24721     {
24722       /* Comma-separated properties are chained together in
24723          reverse order; add them one by one.  */
24724       properties = nreverse (properties);
24725       
24726       for (; properties; properties = TREE_CHAIN (properties))
24727         objc_add_property_declaration (loc, copy_node (properties),
24728                                        property_readonly, property_readwrite,
24729                                        property_assign, property_retain,
24730                                        property_copy, property_nonatomic,
24731                                        property_getter_ident, property_setter_ident);
24732     }
24733   
24734   cp_parser_consume_semicolon_at_end_of_statement (parser);
24735 }
24736
24737 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24738
24739    objc-synthesize-declaration:
24740      @synthesize objc-synthesize-identifier-list ;
24741
24742    objc-synthesize-identifier-list:
24743      objc-synthesize-identifier
24744      objc-synthesize-identifier-list, objc-synthesize-identifier
24745
24746    objc-synthesize-identifier
24747      identifier
24748      identifier = identifier
24749
24750   For example:
24751     @synthesize MyProperty;
24752     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24753
24754   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24755   for C.  Keep them in sync.
24756 */
24757 static void 
24758 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24759 {
24760   tree list = NULL_TREE;
24761   location_t loc;
24762   loc = cp_lexer_peek_token (parser->lexer)->location;
24763
24764   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24765   while (true)
24766     {
24767       tree property, ivar;
24768       property = cp_parser_identifier (parser);
24769       if (property == error_mark_node)
24770         {
24771           cp_parser_consume_semicolon_at_end_of_statement (parser);
24772           return;
24773         }
24774       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24775         {
24776           cp_lexer_consume_token (parser->lexer);
24777           ivar = cp_parser_identifier (parser);
24778           if (ivar == error_mark_node)
24779             {
24780               cp_parser_consume_semicolon_at_end_of_statement (parser);
24781               return;
24782             }
24783         }
24784       else
24785         ivar = NULL_TREE;
24786       list = chainon (list, build_tree_list (ivar, property));
24787       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24788         cp_lexer_consume_token (parser->lexer);
24789       else
24790         break;
24791     }
24792   cp_parser_consume_semicolon_at_end_of_statement (parser);
24793   objc_add_synthesize_declaration (loc, list);
24794 }
24795
24796 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24797
24798    objc-dynamic-declaration:
24799      @dynamic identifier-list ;
24800
24801    For example:
24802      @dynamic MyProperty;
24803      @dynamic MyProperty, AnotherProperty;
24804
24805   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24806   for C.  Keep them in sync.
24807 */
24808 static void 
24809 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24810 {
24811   tree list = NULL_TREE;
24812   location_t loc;
24813   loc = cp_lexer_peek_token (parser->lexer)->location;
24814
24815   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24816   while (true)
24817     {
24818       tree property;
24819       property = cp_parser_identifier (parser);
24820       if (property == error_mark_node)
24821         {
24822           cp_parser_consume_semicolon_at_end_of_statement (parser);
24823           return;
24824         }
24825       list = chainon (list, build_tree_list (NULL, property));
24826       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24827         cp_lexer_consume_token (parser->lexer);
24828       else
24829         break;
24830     }
24831   cp_parser_consume_semicolon_at_end_of_statement (parser);
24832   objc_add_dynamic_declaration (loc, list);
24833 }
24834
24835 \f
24836 /* OpenMP 2.5 parsing routines.  */
24837
24838 /* Returns name of the next clause.
24839    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24840    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24841    returned and the token is consumed.  */
24842
24843 static pragma_omp_clause
24844 cp_parser_omp_clause_name (cp_parser *parser)
24845 {
24846   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24847
24848   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24849     result = PRAGMA_OMP_CLAUSE_IF;
24850   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24851     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24852   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24853     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24854   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24855     {
24856       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24857       const char *p = IDENTIFIER_POINTER (id);
24858
24859       switch (p[0])
24860         {
24861         case 'c':
24862           if (!strcmp ("collapse", p))
24863             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24864           else if (!strcmp ("copyin", p))
24865             result = PRAGMA_OMP_CLAUSE_COPYIN;
24866           else if (!strcmp ("copyprivate", p))
24867             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24868           break;
24869         case 'f':
24870           if (!strcmp ("final", p))
24871             result = PRAGMA_OMP_CLAUSE_FINAL;
24872           else if (!strcmp ("firstprivate", p))
24873             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24874           break;
24875         case 'l':
24876           if (!strcmp ("lastprivate", p))
24877             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24878           break;
24879         case 'm':
24880           if (!strcmp ("mergeable", p))
24881             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24882           break;
24883         case 'n':
24884           if (!strcmp ("nowait", p))
24885             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24886           else if (!strcmp ("num_threads", p))
24887             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24888           break;
24889         case 'o':
24890           if (!strcmp ("ordered", p))
24891             result = PRAGMA_OMP_CLAUSE_ORDERED;
24892           break;
24893         case 'r':
24894           if (!strcmp ("reduction", p))
24895             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24896           break;
24897         case 's':
24898           if (!strcmp ("schedule", p))
24899             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24900           else if (!strcmp ("shared", p))
24901             result = PRAGMA_OMP_CLAUSE_SHARED;
24902           break;
24903         case 'u':
24904           if (!strcmp ("untied", p))
24905             result = PRAGMA_OMP_CLAUSE_UNTIED;
24906           break;
24907         }
24908     }
24909
24910   if (result != PRAGMA_OMP_CLAUSE_NONE)
24911     cp_lexer_consume_token (parser->lexer);
24912
24913   return result;
24914 }
24915
24916 /* Validate that a clause of the given type does not already exist.  */
24917
24918 static void
24919 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24920                            const char *name, location_t location)
24921 {
24922   tree c;
24923
24924   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24925     if (OMP_CLAUSE_CODE (c) == code)
24926       {
24927         error_at (location, "too many %qs clauses", name);
24928         break;
24929       }
24930 }
24931
24932 /* OpenMP 2.5:
24933    variable-list:
24934      identifier
24935      variable-list , identifier
24936
24937    In addition, we match a closing parenthesis.  An opening parenthesis
24938    will have been consumed by the caller.
24939
24940    If KIND is nonzero, create the appropriate node and install the decl
24941    in OMP_CLAUSE_DECL and add the node to the head of the list.
24942
24943    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24944    return the list created.  */
24945
24946 static tree
24947 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24948                                 tree list)
24949 {
24950   cp_token *token;
24951   while (1)
24952     {
24953       tree name, decl;
24954
24955       token = cp_lexer_peek_token (parser->lexer);
24956       name = cp_parser_id_expression (parser, /*template_p=*/false,
24957                                       /*check_dependency_p=*/true,
24958                                       /*template_p=*/NULL,
24959                                       /*declarator_p=*/false,
24960                                       /*optional_p=*/false);
24961       if (name == error_mark_node)
24962         goto skip_comma;
24963
24964       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24965       if (decl == error_mark_node)
24966         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24967                                      token->location);
24968       else if (kind != 0)
24969         {
24970           tree u = build_omp_clause (token->location, kind);
24971           OMP_CLAUSE_DECL (u) = decl;
24972           OMP_CLAUSE_CHAIN (u) = list;
24973           list = u;
24974         }
24975       else
24976         list = tree_cons (decl, NULL_TREE, list);
24977
24978     get_comma:
24979       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24980         break;
24981       cp_lexer_consume_token (parser->lexer);
24982     }
24983
24984   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24985     {
24986       int ending;
24987
24988       /* Try to resync to an unnested comma.  Copied from
24989          cp_parser_parenthesized_expression_list.  */
24990     skip_comma:
24991       ending = cp_parser_skip_to_closing_parenthesis (parser,
24992                                                       /*recovering=*/true,
24993                                                       /*or_comma=*/true,
24994                                                       /*consume_paren=*/true);
24995       if (ending < 0)
24996         goto get_comma;
24997     }
24998
24999   return list;
25000 }
25001
25002 /* Similarly, but expect leading and trailing parenthesis.  This is a very
25003    common case for omp clauses.  */
25004
25005 static tree
25006 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25007 {
25008   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25009     return cp_parser_omp_var_list_no_open (parser, kind, list);
25010   return list;
25011 }
25012
25013 /* OpenMP 3.0:
25014    collapse ( constant-expression ) */
25015
25016 static tree
25017 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25018 {
25019   tree c, num;
25020   location_t loc;
25021   HOST_WIDE_INT n;
25022
25023   loc = cp_lexer_peek_token (parser->lexer)->location;
25024   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25025     return list;
25026
25027   num = cp_parser_constant_expression (parser, false, NULL);
25028
25029   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25030     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25031                                            /*or_comma=*/false,
25032                                            /*consume_paren=*/true);
25033
25034   if (num == error_mark_node)
25035     return list;
25036   num = fold_non_dependent_expr (num);
25037   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25038       || !host_integerp (num, 0)
25039       || (n = tree_low_cst (num, 0)) <= 0
25040       || (int) n != n)
25041     {
25042       error_at (loc, "collapse argument needs positive constant integer expression");
25043       return list;
25044     }
25045
25046   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25047   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25048   OMP_CLAUSE_CHAIN (c) = list;
25049   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25050
25051   return c;
25052 }
25053
25054 /* OpenMP 2.5:
25055    default ( shared | none ) */
25056
25057 static tree
25058 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25059 {
25060   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25061   tree c;
25062
25063   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25064     return list;
25065   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25066     {
25067       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25068       const char *p = IDENTIFIER_POINTER (id);
25069
25070       switch (p[0])
25071         {
25072         case 'n':
25073           if (strcmp ("none", p) != 0)
25074             goto invalid_kind;
25075           kind = OMP_CLAUSE_DEFAULT_NONE;
25076           break;
25077
25078         case 's':
25079           if (strcmp ("shared", p) != 0)
25080             goto invalid_kind;
25081           kind = OMP_CLAUSE_DEFAULT_SHARED;
25082           break;
25083
25084         default:
25085           goto invalid_kind;
25086         }
25087
25088       cp_lexer_consume_token (parser->lexer);
25089     }
25090   else
25091     {
25092     invalid_kind:
25093       cp_parser_error (parser, "expected %<none%> or %<shared%>");
25094     }
25095
25096   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25097     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25098                                            /*or_comma=*/false,
25099                                            /*consume_paren=*/true);
25100
25101   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25102     return list;
25103
25104   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25105   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25106   OMP_CLAUSE_CHAIN (c) = list;
25107   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25108
25109   return c;
25110 }
25111
25112 /* OpenMP 3.1:
25113    final ( expression ) */
25114
25115 static tree
25116 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25117 {
25118   tree t, c;
25119
25120   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25121     return list;
25122
25123   t = cp_parser_condition (parser);
25124
25125   if (t == error_mark_node
25126       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25127     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25128                                            /*or_comma=*/false,
25129                                            /*consume_paren=*/true);
25130
25131   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25132
25133   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25134   OMP_CLAUSE_FINAL_EXPR (c) = t;
25135   OMP_CLAUSE_CHAIN (c) = list;
25136
25137   return c;
25138 }
25139
25140 /* OpenMP 2.5:
25141    if ( expression ) */
25142
25143 static tree
25144 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25145 {
25146   tree t, c;
25147
25148   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25149     return list;
25150
25151   t = cp_parser_condition (parser);
25152
25153   if (t == error_mark_node
25154       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25155     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25156                                            /*or_comma=*/false,
25157                                            /*consume_paren=*/true);
25158
25159   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25160
25161   c = build_omp_clause (location, OMP_CLAUSE_IF);
25162   OMP_CLAUSE_IF_EXPR (c) = t;
25163   OMP_CLAUSE_CHAIN (c) = list;
25164
25165   return c;
25166 }
25167
25168 /* OpenMP 3.1:
25169    mergeable */
25170
25171 static tree
25172 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25173                                 tree list, location_t location)
25174 {
25175   tree c;
25176
25177   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25178                              location);
25179
25180   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25181   OMP_CLAUSE_CHAIN (c) = list;
25182   return c;
25183 }
25184
25185 /* OpenMP 2.5:
25186    nowait */
25187
25188 static tree
25189 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25190                              tree list, location_t location)
25191 {
25192   tree c;
25193
25194   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25195
25196   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25197   OMP_CLAUSE_CHAIN (c) = list;
25198   return c;
25199 }
25200
25201 /* OpenMP 2.5:
25202    num_threads ( expression ) */
25203
25204 static tree
25205 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25206                                   location_t location)
25207 {
25208   tree t, c;
25209
25210   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25211     return list;
25212
25213   t = cp_parser_expression (parser, false, NULL);
25214
25215   if (t == error_mark_node
25216       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25217     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25218                                            /*or_comma=*/false,
25219                                            /*consume_paren=*/true);
25220
25221   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25222                              "num_threads", location);
25223
25224   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25225   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25226   OMP_CLAUSE_CHAIN (c) = list;
25227
25228   return c;
25229 }
25230
25231 /* OpenMP 2.5:
25232    ordered */
25233
25234 static tree
25235 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25236                               tree list, location_t location)
25237 {
25238   tree c;
25239
25240   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25241                              "ordered", location);
25242
25243   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25244   OMP_CLAUSE_CHAIN (c) = list;
25245   return c;
25246 }
25247
25248 /* OpenMP 2.5:
25249    reduction ( reduction-operator : variable-list )
25250
25251    reduction-operator:
25252      One of: + * - & ^ | && ||
25253
25254    OpenMP 3.1:
25255
25256    reduction-operator:
25257      One of: + * - & ^ | && || min max  */
25258
25259 static tree
25260 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25261 {
25262   enum tree_code code;
25263   tree nlist, c;
25264
25265   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25266     return list;
25267
25268   switch (cp_lexer_peek_token (parser->lexer)->type)
25269     {
25270     case CPP_PLUS:
25271       code = PLUS_EXPR;
25272       break;
25273     case CPP_MULT:
25274       code = MULT_EXPR;
25275       break;
25276     case CPP_MINUS:
25277       code = MINUS_EXPR;
25278       break;
25279     case CPP_AND:
25280       code = BIT_AND_EXPR;
25281       break;
25282     case CPP_XOR:
25283       code = BIT_XOR_EXPR;
25284       break;
25285     case CPP_OR:
25286       code = BIT_IOR_EXPR;
25287       break;
25288     case CPP_AND_AND:
25289       code = TRUTH_ANDIF_EXPR;
25290       break;
25291     case CPP_OR_OR:
25292       code = TRUTH_ORIF_EXPR;
25293       break;
25294     case CPP_NAME:
25295       {
25296         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25297         const char *p = IDENTIFIER_POINTER (id);
25298
25299         if (strcmp (p, "min") == 0)
25300           {
25301             code = MIN_EXPR;
25302             break;
25303           }
25304         if (strcmp (p, "max") == 0)
25305           {
25306             code = MAX_EXPR;
25307             break;
25308           }
25309       }
25310       /* FALLTHROUGH */
25311     default:
25312       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25313                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25314     resync_fail:
25315       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25316                                              /*or_comma=*/false,
25317                                              /*consume_paren=*/true);
25318       return list;
25319     }
25320   cp_lexer_consume_token (parser->lexer);
25321
25322   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25323     goto resync_fail;
25324
25325   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25326   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25327     OMP_CLAUSE_REDUCTION_CODE (c) = code;
25328
25329   return nlist;
25330 }
25331
25332 /* OpenMP 2.5:
25333    schedule ( schedule-kind )
25334    schedule ( schedule-kind , expression )
25335
25336    schedule-kind:
25337      static | dynamic | guided | runtime | auto  */
25338
25339 static tree
25340 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25341 {
25342   tree c, t;
25343
25344   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25345     return list;
25346
25347   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25348
25349   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25350     {
25351       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25352       const char *p = IDENTIFIER_POINTER (id);
25353
25354       switch (p[0])
25355         {
25356         case 'd':
25357           if (strcmp ("dynamic", p) != 0)
25358             goto invalid_kind;
25359           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25360           break;
25361
25362         case 'g':
25363           if (strcmp ("guided", p) != 0)
25364             goto invalid_kind;
25365           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25366           break;
25367
25368         case 'r':
25369           if (strcmp ("runtime", p) != 0)
25370             goto invalid_kind;
25371           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25372           break;
25373
25374         default:
25375           goto invalid_kind;
25376         }
25377     }
25378   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25379     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25380   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25381     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25382   else
25383     goto invalid_kind;
25384   cp_lexer_consume_token (parser->lexer);
25385
25386   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25387     {
25388       cp_token *token;
25389       cp_lexer_consume_token (parser->lexer);
25390
25391       token = cp_lexer_peek_token (parser->lexer);
25392       t = cp_parser_assignment_expression (parser, false, NULL);
25393
25394       if (t == error_mark_node)
25395         goto resync_fail;
25396       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25397         error_at (token->location, "schedule %<runtime%> does not take "
25398                   "a %<chunk_size%> parameter");
25399       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25400         error_at (token->location, "schedule %<auto%> does not take "
25401                   "a %<chunk_size%> parameter");
25402       else
25403         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25404
25405       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25406         goto resync_fail;
25407     }
25408   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25409     goto resync_fail;
25410
25411   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25412   OMP_CLAUSE_CHAIN (c) = list;
25413   return c;
25414
25415  invalid_kind:
25416   cp_parser_error (parser, "invalid schedule kind");
25417  resync_fail:
25418   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25419                                          /*or_comma=*/false,
25420                                          /*consume_paren=*/true);
25421   return list;
25422 }
25423
25424 /* OpenMP 3.0:
25425    untied */
25426
25427 static tree
25428 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25429                              tree list, location_t location)
25430 {
25431   tree c;
25432
25433   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25434
25435   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25436   OMP_CLAUSE_CHAIN (c) = list;
25437   return c;
25438 }
25439
25440 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25441    is a bitmask in MASK.  Return the list of clauses found; the result
25442    of clause default goes in *pdefault.  */
25443
25444 static tree
25445 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25446                            const char *where, cp_token *pragma_tok)
25447 {
25448   tree clauses = NULL;
25449   bool first = true;
25450   cp_token *token = NULL;
25451
25452   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25453     {
25454       pragma_omp_clause c_kind;
25455       const char *c_name;
25456       tree prev = clauses;
25457
25458       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25459         cp_lexer_consume_token (parser->lexer);
25460
25461       token = cp_lexer_peek_token (parser->lexer);
25462       c_kind = cp_parser_omp_clause_name (parser);
25463       first = false;
25464
25465       switch (c_kind)
25466         {
25467         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25468           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25469                                                    token->location);
25470           c_name = "collapse";
25471           break;
25472         case PRAGMA_OMP_CLAUSE_COPYIN:
25473           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25474           c_name = "copyin";
25475           break;
25476         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25477           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25478                                             clauses);
25479           c_name = "copyprivate";
25480           break;
25481         case PRAGMA_OMP_CLAUSE_DEFAULT:
25482           clauses = cp_parser_omp_clause_default (parser, clauses,
25483                                                   token->location);
25484           c_name = "default";
25485           break;
25486         case PRAGMA_OMP_CLAUSE_FINAL:
25487           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25488           c_name = "final";
25489           break;
25490         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25491           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25492                                             clauses);
25493           c_name = "firstprivate";
25494           break;
25495         case PRAGMA_OMP_CLAUSE_IF:
25496           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25497           c_name = "if";
25498           break;
25499         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25500           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25501                                             clauses);
25502           c_name = "lastprivate";
25503           break;
25504         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25505           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25506                                                     token->location);
25507           c_name = "mergeable";
25508           break;
25509         case PRAGMA_OMP_CLAUSE_NOWAIT:
25510           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25511           c_name = "nowait";
25512           break;
25513         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25514           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25515                                                       token->location);
25516           c_name = "num_threads";
25517           break;
25518         case PRAGMA_OMP_CLAUSE_ORDERED:
25519           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25520                                                   token->location);
25521           c_name = "ordered";
25522           break;
25523         case PRAGMA_OMP_CLAUSE_PRIVATE:
25524           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25525                                             clauses);
25526           c_name = "private";
25527           break;
25528         case PRAGMA_OMP_CLAUSE_REDUCTION:
25529           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25530           c_name = "reduction";
25531           break;
25532         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25533           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25534                                                    token->location);
25535           c_name = "schedule";
25536           break;
25537         case PRAGMA_OMP_CLAUSE_SHARED:
25538           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25539                                             clauses);
25540           c_name = "shared";
25541           break;
25542         case PRAGMA_OMP_CLAUSE_UNTIED:
25543           clauses = cp_parser_omp_clause_untied (parser, clauses,
25544                                                  token->location);
25545           c_name = "nowait";
25546           break;
25547         default:
25548           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25549           goto saw_error;
25550         }
25551
25552       if (((mask >> c_kind) & 1) == 0)
25553         {
25554           /* Remove the invalid clause(s) from the list to avoid
25555              confusing the rest of the compiler.  */
25556           clauses = prev;
25557           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25558         }
25559     }
25560  saw_error:
25561   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25562   return finish_omp_clauses (clauses);
25563 }
25564
25565 /* OpenMP 2.5:
25566    structured-block:
25567      statement
25568
25569    In practice, we're also interested in adding the statement to an
25570    outer node.  So it is convenient if we work around the fact that
25571    cp_parser_statement calls add_stmt.  */
25572
25573 static unsigned
25574 cp_parser_begin_omp_structured_block (cp_parser *parser)
25575 {
25576   unsigned save = parser->in_statement;
25577
25578   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25579      This preserves the "not within loop or switch" style error messages
25580      for nonsense cases like
25581         void foo() {
25582         #pragma omp single
25583           break;
25584         }
25585   */
25586   if (parser->in_statement)
25587     parser->in_statement = IN_OMP_BLOCK;
25588
25589   return save;
25590 }
25591
25592 static void
25593 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25594 {
25595   parser->in_statement = save;
25596 }
25597
25598 static tree
25599 cp_parser_omp_structured_block (cp_parser *parser)
25600 {
25601   tree stmt = begin_omp_structured_block ();
25602   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25603
25604   cp_parser_statement (parser, NULL_TREE, false, NULL);
25605
25606   cp_parser_end_omp_structured_block (parser, save);
25607   return finish_omp_structured_block (stmt);
25608 }
25609
25610 /* OpenMP 2.5:
25611    # pragma omp atomic new-line
25612      expression-stmt
25613
25614    expression-stmt:
25615      x binop= expr | x++ | ++x | x-- | --x
25616    binop:
25617      +, *, -, /, &, ^, |, <<, >>
25618
25619   where x is an lvalue expression with scalar type.
25620
25621    OpenMP 3.1:
25622    # pragma omp atomic new-line
25623      update-stmt
25624
25625    # pragma omp atomic read new-line
25626      read-stmt
25627
25628    # pragma omp atomic write new-line
25629      write-stmt
25630
25631    # pragma omp atomic update new-line
25632      update-stmt
25633
25634    # pragma omp atomic capture new-line
25635      capture-stmt
25636
25637    # pragma omp atomic capture new-line
25638      capture-block
25639
25640    read-stmt:
25641      v = x
25642    write-stmt:
25643      x = expr
25644    update-stmt:
25645      expression-stmt | x = x binop expr
25646    capture-stmt:
25647      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25648    capture-block:
25649      { v = x; update-stmt; } | { update-stmt; v = x; }
25650
25651   where x and v are lvalue expressions with scalar type.  */
25652
25653 static void
25654 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25655 {
25656   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25657   tree rhs1 = NULL_TREE, orig_lhs;
25658   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25659   bool structured_block = false;
25660
25661   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25662     {
25663       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25664       const char *p = IDENTIFIER_POINTER (id);
25665
25666       if (!strcmp (p, "read"))
25667         code = OMP_ATOMIC_READ;
25668       else if (!strcmp (p, "write"))
25669         code = NOP_EXPR;
25670       else if (!strcmp (p, "update"))
25671         code = OMP_ATOMIC;
25672       else if (!strcmp (p, "capture"))
25673         code = OMP_ATOMIC_CAPTURE_NEW;
25674       else
25675         p = NULL;
25676       if (p)
25677         cp_lexer_consume_token (parser->lexer);
25678     }
25679   cp_parser_require_pragma_eol (parser, pragma_tok);
25680
25681   switch (code)
25682     {
25683     case OMP_ATOMIC_READ:
25684     case NOP_EXPR: /* atomic write */
25685       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25686                                       /*cast_p=*/false, NULL);
25687       if (v == error_mark_node)
25688         goto saw_error;
25689       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25690         goto saw_error;
25691       if (code == NOP_EXPR)
25692         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25693       else
25694         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25695                                           /*cast_p=*/false, NULL);
25696       if (lhs == error_mark_node)
25697         goto saw_error;
25698       if (code == NOP_EXPR)
25699         {
25700           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25701              opcode.  */
25702           code = OMP_ATOMIC;
25703           rhs = lhs;
25704           lhs = v;
25705           v = NULL_TREE;
25706         }
25707       goto done;
25708     case OMP_ATOMIC_CAPTURE_NEW:
25709       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25710         {
25711           cp_lexer_consume_token (parser->lexer);
25712           structured_block = true;
25713         }
25714       else
25715         {
25716           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25717                                           /*cast_p=*/false, NULL);
25718           if (v == error_mark_node)
25719             goto saw_error;
25720           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25721             goto saw_error;
25722         }
25723     default:
25724       break;
25725     }
25726
25727 restart:
25728   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25729                                     /*cast_p=*/false, NULL);
25730   orig_lhs = lhs;
25731   switch (TREE_CODE (lhs))
25732     {
25733     case ERROR_MARK:
25734       goto saw_error;
25735
25736     case POSTINCREMENT_EXPR:
25737       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25738         code = OMP_ATOMIC_CAPTURE_OLD;
25739       /* FALLTHROUGH */
25740     case PREINCREMENT_EXPR:
25741       lhs = TREE_OPERAND (lhs, 0);
25742       opcode = PLUS_EXPR;
25743       rhs = integer_one_node;
25744       break;
25745
25746     case POSTDECREMENT_EXPR:
25747       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25748         code = OMP_ATOMIC_CAPTURE_OLD;
25749       /* FALLTHROUGH */
25750     case PREDECREMENT_EXPR:
25751       lhs = TREE_OPERAND (lhs, 0);
25752       opcode = MINUS_EXPR;
25753       rhs = integer_one_node;
25754       break;
25755
25756     case COMPOUND_EXPR:
25757       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25758          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25759          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25760          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25761          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25762                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25763             == BOOLEAN_TYPE)
25764        /* Undo effects of boolean_increment for post {in,de}crement.  */
25765        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25766       /* FALLTHRU */
25767     case MODIFY_EXPR:
25768       if (TREE_CODE (lhs) == MODIFY_EXPR
25769          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25770         {
25771           /* Undo effects of boolean_increment.  */
25772           if (integer_onep (TREE_OPERAND (lhs, 1)))
25773             {
25774               /* This is pre or post increment.  */
25775               rhs = TREE_OPERAND (lhs, 1);
25776               lhs = TREE_OPERAND (lhs, 0);
25777               opcode = NOP_EXPR;
25778               if (code == OMP_ATOMIC_CAPTURE_NEW
25779                   && !structured_block
25780                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25781                 code = OMP_ATOMIC_CAPTURE_OLD;
25782               break;
25783             }
25784         }
25785       /* FALLTHRU */
25786     default:
25787       switch (cp_lexer_peek_token (parser->lexer)->type)
25788         {
25789         case CPP_MULT_EQ:
25790           opcode = MULT_EXPR;
25791           break;
25792         case CPP_DIV_EQ:
25793           opcode = TRUNC_DIV_EXPR;
25794           break;
25795         case CPP_PLUS_EQ:
25796           opcode = PLUS_EXPR;
25797           break;
25798         case CPP_MINUS_EQ:
25799           opcode = MINUS_EXPR;
25800           break;
25801         case CPP_LSHIFT_EQ:
25802           opcode = LSHIFT_EXPR;
25803           break;
25804         case CPP_RSHIFT_EQ:
25805           opcode = RSHIFT_EXPR;
25806           break;
25807         case CPP_AND_EQ:
25808           opcode = BIT_AND_EXPR;
25809           break;
25810         case CPP_OR_EQ:
25811           opcode = BIT_IOR_EXPR;
25812           break;
25813         case CPP_XOR_EQ:
25814           opcode = BIT_XOR_EXPR;
25815           break;
25816         case CPP_EQ:
25817           if (structured_block || code == OMP_ATOMIC)
25818             {
25819               enum cp_parser_prec oprec;
25820               cp_token *token;
25821               cp_lexer_consume_token (parser->lexer);
25822               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25823                                                  /*cast_p=*/false, NULL);
25824               if (rhs1 == error_mark_node)
25825                 goto saw_error;
25826               token = cp_lexer_peek_token (parser->lexer);
25827               switch (token->type)
25828                 {
25829                 case CPP_SEMICOLON:
25830                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25831                     {
25832                       code = OMP_ATOMIC_CAPTURE_OLD;
25833                       v = lhs;
25834                       lhs = NULL_TREE;
25835                       lhs1 = rhs1;
25836                       rhs1 = NULL_TREE;
25837                       cp_lexer_consume_token (parser->lexer);
25838                       goto restart;
25839                     }
25840                   cp_parser_error (parser,
25841                                    "invalid form of %<#pragma omp atomic%>");
25842                   goto saw_error;
25843                 case CPP_MULT:
25844                   opcode = MULT_EXPR;
25845                   break;
25846                 case CPP_DIV:
25847                   opcode = TRUNC_DIV_EXPR;
25848                   break;
25849                 case CPP_PLUS:
25850                   opcode = PLUS_EXPR;
25851                   break;
25852                 case CPP_MINUS:
25853                   opcode = MINUS_EXPR;
25854                   break;
25855                 case CPP_LSHIFT:
25856                   opcode = LSHIFT_EXPR;
25857                   break;
25858                 case CPP_RSHIFT:
25859                   opcode = RSHIFT_EXPR;
25860                   break;
25861                 case CPP_AND:
25862                   opcode = BIT_AND_EXPR;
25863                   break;
25864                 case CPP_OR:
25865                   opcode = BIT_IOR_EXPR;
25866                   break;
25867                 case CPP_XOR:
25868                   opcode = BIT_XOR_EXPR;
25869                   break;
25870                 default:
25871                   cp_parser_error (parser,
25872                                    "invalid operator for %<#pragma omp atomic%>");
25873                   goto saw_error;
25874                 }
25875               oprec = TOKEN_PRECEDENCE (token);
25876               gcc_assert (oprec != PREC_NOT_OPERATOR);
25877               if (commutative_tree_code (opcode))
25878                 oprec = (enum cp_parser_prec) (oprec - 1);
25879               cp_lexer_consume_token (parser->lexer);
25880               rhs = cp_parser_binary_expression (parser, false, false,
25881                                                  oprec, NULL);
25882               if (rhs == error_mark_node)
25883                 goto saw_error;
25884               goto stmt_done;
25885             }
25886           /* FALLTHROUGH */
25887         default:
25888           cp_parser_error (parser,
25889                            "invalid operator for %<#pragma omp atomic%>");
25890           goto saw_error;
25891         }
25892       cp_lexer_consume_token (parser->lexer);
25893
25894       rhs = cp_parser_expression (parser, false, NULL);
25895       if (rhs == error_mark_node)
25896         goto saw_error;
25897       break;
25898     }
25899 stmt_done:
25900   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25901     {
25902       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25903         goto saw_error;
25904       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25905                                       /*cast_p=*/false, NULL);
25906       if (v == error_mark_node)
25907         goto saw_error;
25908       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25909         goto saw_error;
25910       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25911                                          /*cast_p=*/false, NULL);
25912       if (lhs1 == error_mark_node)
25913         goto saw_error;
25914     }
25915   if (structured_block)
25916     {
25917       cp_parser_consume_semicolon_at_end_of_statement (parser);
25918       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25919     }
25920 done:
25921   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25922   if (!structured_block)
25923     cp_parser_consume_semicolon_at_end_of_statement (parser);
25924   return;
25925
25926  saw_error:
25927   cp_parser_skip_to_end_of_block_or_statement (parser);
25928   if (structured_block)
25929     {
25930       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25931         cp_lexer_consume_token (parser->lexer);
25932       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25933         {
25934           cp_parser_skip_to_end_of_block_or_statement (parser);
25935           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25936             cp_lexer_consume_token (parser->lexer);
25937         }
25938     }
25939 }
25940
25941
25942 /* OpenMP 2.5:
25943    # pragma omp barrier new-line  */
25944
25945 static void
25946 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25947 {
25948   cp_parser_require_pragma_eol (parser, pragma_tok);
25949   finish_omp_barrier ();
25950 }
25951
25952 /* OpenMP 2.5:
25953    # pragma omp critical [(name)] new-line
25954      structured-block  */
25955
25956 static tree
25957 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25958 {
25959   tree stmt, name = NULL;
25960
25961   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25962     {
25963       cp_lexer_consume_token (parser->lexer);
25964
25965       name = cp_parser_identifier (parser);
25966
25967       if (name == error_mark_node
25968           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25969         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25970                                                /*or_comma=*/false,
25971                                                /*consume_paren=*/true);
25972       if (name == error_mark_node)
25973         name = NULL;
25974     }
25975   cp_parser_require_pragma_eol (parser, pragma_tok);
25976
25977   stmt = cp_parser_omp_structured_block (parser);
25978   return c_finish_omp_critical (input_location, stmt, name);
25979 }
25980
25981 /* OpenMP 2.5:
25982    # pragma omp flush flush-vars[opt] new-line
25983
25984    flush-vars:
25985      ( variable-list ) */
25986
25987 static void
25988 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25989 {
25990   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25991     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25992   cp_parser_require_pragma_eol (parser, pragma_tok);
25993
25994   finish_omp_flush ();
25995 }
25996
25997 /* Helper function, to parse omp for increment expression.  */
25998
25999 static tree
26000 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26001 {
26002   tree cond = cp_parser_binary_expression (parser, false, true,
26003                                            PREC_NOT_OPERATOR, NULL);
26004   if (cond == error_mark_node
26005       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26006     {
26007       cp_parser_skip_to_end_of_statement (parser);
26008       return error_mark_node;
26009     }
26010
26011   switch (TREE_CODE (cond))
26012     {
26013     case GT_EXPR:
26014     case GE_EXPR:
26015     case LT_EXPR:
26016     case LE_EXPR:
26017       break;
26018     default:
26019       return error_mark_node;
26020     }
26021
26022   /* If decl is an iterator, preserve LHS and RHS of the relational
26023      expr until finish_omp_for.  */
26024   if (decl
26025       && (type_dependent_expression_p (decl)
26026           || CLASS_TYPE_P (TREE_TYPE (decl))))
26027     return cond;
26028
26029   return build_x_binary_op (TREE_CODE (cond),
26030                             TREE_OPERAND (cond, 0), ERROR_MARK,
26031                             TREE_OPERAND (cond, 1), ERROR_MARK,
26032                             /*overload=*/NULL, tf_warning_or_error);
26033 }
26034
26035 /* Helper function, to parse omp for increment expression.  */
26036
26037 static tree
26038 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26039 {
26040   cp_token *token = cp_lexer_peek_token (parser->lexer);
26041   enum tree_code op;
26042   tree lhs, rhs;
26043   cp_id_kind idk;
26044   bool decl_first;
26045
26046   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26047     {
26048       op = (token->type == CPP_PLUS_PLUS
26049             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26050       cp_lexer_consume_token (parser->lexer);
26051       lhs = cp_parser_cast_expression (parser, false, false, NULL);
26052       if (lhs != decl)
26053         return error_mark_node;
26054       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26055     }
26056
26057   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26058   if (lhs != decl)
26059     return error_mark_node;
26060
26061   token = cp_lexer_peek_token (parser->lexer);
26062   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26063     {
26064       op = (token->type == CPP_PLUS_PLUS
26065             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26066       cp_lexer_consume_token (parser->lexer);
26067       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26068     }
26069
26070   op = cp_parser_assignment_operator_opt (parser);
26071   if (op == ERROR_MARK)
26072     return error_mark_node;
26073
26074   if (op != NOP_EXPR)
26075     {
26076       rhs = cp_parser_assignment_expression (parser, false, NULL);
26077       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26078       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26079     }
26080
26081   lhs = cp_parser_binary_expression (parser, false, false,
26082                                      PREC_ADDITIVE_EXPRESSION, NULL);
26083   token = cp_lexer_peek_token (parser->lexer);
26084   decl_first = lhs == decl;
26085   if (decl_first)
26086     lhs = NULL_TREE;
26087   if (token->type != CPP_PLUS
26088       && token->type != CPP_MINUS)
26089     return error_mark_node;
26090
26091   do
26092     {
26093       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26094       cp_lexer_consume_token (parser->lexer);
26095       rhs = cp_parser_binary_expression (parser, false, false,
26096                                          PREC_ADDITIVE_EXPRESSION, NULL);
26097       token = cp_lexer_peek_token (parser->lexer);
26098       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26099         {
26100           if (lhs == NULL_TREE)
26101             {
26102               if (op == PLUS_EXPR)
26103                 lhs = rhs;
26104               else
26105                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
26106             }
26107           else
26108             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
26109                                      NULL, tf_warning_or_error);
26110         }
26111     }
26112   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26113
26114   if (!decl_first)
26115     {
26116       if (rhs != decl || op == MINUS_EXPR)
26117         return error_mark_node;
26118       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26119     }
26120   else
26121     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26122
26123   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26124 }
26125
26126 /* Parse the restricted form of the for statement allowed by OpenMP.  */
26127
26128 static tree
26129 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26130 {
26131   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26132   tree real_decl, initv, condv, incrv, declv;
26133   tree this_pre_body, cl;
26134   location_t loc_first;
26135   bool collapse_err = false;
26136   int i, collapse = 1, nbraces = 0;
26137   VEC(tree,gc) *for_block = make_tree_vector ();
26138
26139   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26140     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26141       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26142
26143   gcc_assert (collapse >= 1);
26144
26145   declv = make_tree_vec (collapse);
26146   initv = make_tree_vec (collapse);
26147   condv = make_tree_vec (collapse);
26148   incrv = make_tree_vec (collapse);
26149
26150   loc_first = cp_lexer_peek_token (parser->lexer)->location;
26151
26152   for (i = 0; i < collapse; i++)
26153     {
26154       int bracecount = 0;
26155       bool add_private_clause = false;
26156       location_t loc;
26157
26158       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26159         {
26160           cp_parser_error (parser, "for statement expected");
26161           return NULL;
26162         }
26163       loc = cp_lexer_consume_token (parser->lexer)->location;
26164
26165       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26166         return NULL;
26167
26168       init = decl = real_decl = NULL;
26169       this_pre_body = push_stmt_list ();
26170       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26171         {
26172           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26173
26174              init-expr:
26175                        var = lb
26176                        integer-type var = lb
26177                        random-access-iterator-type var = lb
26178                        pointer-type var = lb
26179           */
26180           cp_decl_specifier_seq type_specifiers;
26181
26182           /* First, try to parse as an initialized declaration.  See
26183              cp_parser_condition, from whence the bulk of this is copied.  */
26184
26185           cp_parser_parse_tentatively (parser);
26186           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26187                                         /*is_trailing_return=*/false,
26188                                         &type_specifiers);
26189           if (cp_parser_parse_definitely (parser))
26190             {
26191               /* If parsing a type specifier seq succeeded, then this
26192                  MUST be a initialized declaration.  */
26193               tree asm_specification, attributes;
26194               cp_declarator *declarator;
26195
26196               declarator = cp_parser_declarator (parser,
26197                                                  CP_PARSER_DECLARATOR_NAMED,
26198                                                  /*ctor_dtor_or_conv_p=*/NULL,
26199                                                  /*parenthesized_p=*/NULL,
26200                                                  /*member_p=*/false);
26201               attributes = cp_parser_attributes_opt (parser);
26202               asm_specification = cp_parser_asm_specification_opt (parser);
26203
26204               if (declarator == cp_error_declarator) 
26205                 cp_parser_skip_to_end_of_statement (parser);
26206
26207               else 
26208                 {
26209                   tree pushed_scope, auto_node;
26210
26211                   decl = start_decl (declarator, &type_specifiers,
26212                                      SD_INITIALIZED, attributes,
26213                                      /*prefix_attributes=*/NULL_TREE,
26214                                      &pushed_scope);
26215
26216                   auto_node = type_uses_auto (TREE_TYPE (decl));
26217                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26218                     {
26219                       if (cp_lexer_next_token_is (parser->lexer, 
26220                                                   CPP_OPEN_PAREN))
26221                         error ("parenthesized initialization is not allowed in "
26222                                "OpenMP %<for%> loop");
26223                       else
26224                         /* Trigger an error.  */
26225                         cp_parser_require (parser, CPP_EQ, RT_EQ);
26226
26227                       init = error_mark_node;
26228                       cp_parser_skip_to_end_of_statement (parser);
26229                     }
26230                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
26231                            || type_dependent_expression_p (decl)
26232                            || auto_node)
26233                     {
26234                       bool is_direct_init, is_non_constant_init;
26235
26236                       init = cp_parser_initializer (parser,
26237                                                     &is_direct_init,
26238                                                     &is_non_constant_init);
26239
26240                       if (auto_node)
26241                         {
26242                           TREE_TYPE (decl)
26243                             = do_auto_deduction (TREE_TYPE (decl), init,
26244                                                  auto_node);
26245
26246                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
26247                               && !type_dependent_expression_p (decl))
26248                             goto non_class;
26249                         }
26250                       
26251                       cp_finish_decl (decl, init, !is_non_constant_init,
26252                                       asm_specification,
26253                                       LOOKUP_ONLYCONVERTING);
26254                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
26255                         {
26256                           VEC_safe_push (tree, gc, for_block, this_pre_body);
26257                           init = NULL_TREE;
26258                         }
26259                       else
26260                         init = pop_stmt_list (this_pre_body);
26261                       this_pre_body = NULL_TREE;
26262                     }
26263                   else
26264                     {
26265                       /* Consume '='.  */
26266                       cp_lexer_consume_token (parser->lexer);
26267                       init = cp_parser_assignment_expression (parser, false, NULL);
26268
26269                     non_class:
26270                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26271                         init = error_mark_node;
26272                       else
26273                         cp_finish_decl (decl, NULL_TREE,
26274                                         /*init_const_expr_p=*/false,
26275                                         asm_specification,
26276                                         LOOKUP_ONLYCONVERTING);
26277                     }
26278
26279                   if (pushed_scope)
26280                     pop_scope (pushed_scope);
26281                 }
26282             }
26283           else 
26284             {
26285               cp_id_kind idk;
26286               /* If parsing a type specifier sequence failed, then
26287                  this MUST be a simple expression.  */
26288               cp_parser_parse_tentatively (parser);
26289               decl = cp_parser_primary_expression (parser, false, false,
26290                                                    false, &idk);
26291               if (!cp_parser_error_occurred (parser)
26292                   && decl
26293                   && DECL_P (decl)
26294                   && CLASS_TYPE_P (TREE_TYPE (decl)))
26295                 {
26296                   tree rhs;
26297
26298                   cp_parser_parse_definitely (parser);
26299                   cp_parser_require (parser, CPP_EQ, RT_EQ);
26300                   rhs = cp_parser_assignment_expression (parser, false, NULL);
26301                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26302                                                          rhs,
26303                                                          tf_warning_or_error));
26304                   add_private_clause = true;
26305                 }
26306               else
26307                 {
26308                   decl = NULL;
26309                   cp_parser_abort_tentative_parse (parser);
26310                   init = cp_parser_expression (parser, false, NULL);
26311                   if (init)
26312                     {
26313                       if (TREE_CODE (init) == MODIFY_EXPR
26314                           || TREE_CODE (init) == MODOP_EXPR)
26315                         real_decl = TREE_OPERAND (init, 0);
26316                     }
26317                 }
26318             }
26319         }
26320       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26321       if (this_pre_body)
26322         {
26323           this_pre_body = pop_stmt_list (this_pre_body);
26324           if (pre_body)
26325             {
26326               tree t = pre_body;
26327               pre_body = push_stmt_list ();
26328               add_stmt (t);
26329               add_stmt (this_pre_body);
26330               pre_body = pop_stmt_list (pre_body);
26331             }
26332           else
26333             pre_body = this_pre_body;
26334         }
26335
26336       if (decl)
26337         real_decl = decl;
26338       if (par_clauses != NULL && real_decl != NULL_TREE)
26339         {
26340           tree *c;
26341           for (c = par_clauses; *c ; )
26342             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26343                 && OMP_CLAUSE_DECL (*c) == real_decl)
26344               {
26345                 error_at (loc, "iteration variable %qD"
26346                           " should not be firstprivate", real_decl);
26347                 *c = OMP_CLAUSE_CHAIN (*c);
26348               }
26349             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26350                      && OMP_CLAUSE_DECL (*c) == real_decl)
26351               {
26352                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26353                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
26354                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26355                 OMP_CLAUSE_DECL (l) = real_decl;
26356                 OMP_CLAUSE_CHAIN (l) = clauses;
26357                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26358                 clauses = l;
26359                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26360                 CP_OMP_CLAUSE_INFO (*c) = NULL;
26361                 add_private_clause = false;
26362               }
26363             else
26364               {
26365                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26366                     && OMP_CLAUSE_DECL (*c) == real_decl)
26367                   add_private_clause = false;
26368                 c = &OMP_CLAUSE_CHAIN (*c);
26369               }
26370         }
26371
26372       if (add_private_clause)
26373         {
26374           tree c;
26375           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26376             {
26377               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26378                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26379                   && OMP_CLAUSE_DECL (c) == decl)
26380                 break;
26381               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26382                        && OMP_CLAUSE_DECL (c) == decl)
26383                 error_at (loc, "iteration variable %qD "
26384                           "should not be firstprivate",
26385                           decl);
26386               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26387                        && OMP_CLAUSE_DECL (c) == decl)
26388                 error_at (loc, "iteration variable %qD should not be reduction",
26389                           decl);
26390             }
26391           if (c == NULL)
26392             {
26393               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26394               OMP_CLAUSE_DECL (c) = decl;
26395               c = finish_omp_clauses (c);
26396               if (c)
26397                 {
26398                   OMP_CLAUSE_CHAIN (c) = clauses;
26399                   clauses = c;
26400                 }
26401             }
26402         }
26403
26404       cond = NULL;
26405       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26406         cond = cp_parser_omp_for_cond (parser, decl);
26407       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26408
26409       incr = NULL;
26410       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26411         {
26412           /* If decl is an iterator, preserve the operator on decl
26413              until finish_omp_for.  */
26414           if (real_decl
26415               && ((processing_template_decl
26416                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26417                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26418             incr = cp_parser_omp_for_incr (parser, real_decl);
26419           else
26420             incr = cp_parser_expression (parser, false, NULL);
26421         }
26422
26423       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26424         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26425                                                /*or_comma=*/false,
26426                                                /*consume_paren=*/true);
26427
26428       TREE_VEC_ELT (declv, i) = decl;
26429       TREE_VEC_ELT (initv, i) = init;
26430       TREE_VEC_ELT (condv, i) = cond;
26431       TREE_VEC_ELT (incrv, i) = incr;
26432
26433       if (i == collapse - 1)
26434         break;
26435
26436       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26437          in between the collapsed for loops to be still considered perfectly
26438          nested.  Hopefully the final version clarifies this.
26439          For now handle (multiple) {'s and empty statements.  */
26440       cp_parser_parse_tentatively (parser);
26441       do
26442         {
26443           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26444             break;
26445           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26446             {
26447               cp_lexer_consume_token (parser->lexer);
26448               bracecount++;
26449             }
26450           else if (bracecount
26451                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26452             cp_lexer_consume_token (parser->lexer);
26453           else
26454             {
26455               loc = cp_lexer_peek_token (parser->lexer)->location;
26456               error_at (loc, "not enough collapsed for loops");
26457               collapse_err = true;
26458               cp_parser_abort_tentative_parse (parser);
26459               declv = NULL_TREE;
26460               break;
26461             }
26462         }
26463       while (1);
26464
26465       if (declv)
26466         {
26467           cp_parser_parse_definitely (parser);
26468           nbraces += bracecount;
26469         }
26470     }
26471
26472   /* Note that we saved the original contents of this flag when we entered
26473      the structured block, and so we don't need to re-save it here.  */
26474   parser->in_statement = IN_OMP_FOR;
26475
26476   /* Note that the grammar doesn't call for a structured block here,
26477      though the loop as a whole is a structured block.  */
26478   body = push_stmt_list ();
26479   cp_parser_statement (parser, NULL_TREE, false, NULL);
26480   body = pop_stmt_list (body);
26481
26482   if (declv == NULL_TREE)
26483     ret = NULL_TREE;
26484   else
26485     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26486                           pre_body, clauses);
26487
26488   while (nbraces)
26489     {
26490       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26491         {
26492           cp_lexer_consume_token (parser->lexer);
26493           nbraces--;
26494         }
26495       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26496         cp_lexer_consume_token (parser->lexer);
26497       else
26498         {
26499           if (!collapse_err)
26500             {
26501               error_at (cp_lexer_peek_token (parser->lexer)->location,
26502                         "collapsed loops not perfectly nested");
26503             }
26504           collapse_err = true;
26505           cp_parser_statement_seq_opt (parser, NULL);
26506           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26507             break;
26508         }
26509     }
26510
26511   while (!VEC_empty (tree, for_block))
26512     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26513   release_tree_vector (for_block);
26514
26515   return ret;
26516 }
26517
26518 /* OpenMP 2.5:
26519    #pragma omp for for-clause[optseq] new-line
26520      for-loop  */
26521
26522 #define OMP_FOR_CLAUSE_MASK                             \
26523         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26524         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26525         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26526         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26527         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26528         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26529         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26530         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26531
26532 static tree
26533 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26534 {
26535   tree clauses, sb, ret;
26536   unsigned int save;
26537
26538   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26539                                        "#pragma omp for", pragma_tok);
26540
26541   sb = begin_omp_structured_block ();
26542   save = cp_parser_begin_omp_structured_block (parser);
26543
26544   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26545
26546   cp_parser_end_omp_structured_block (parser, save);
26547   add_stmt (finish_omp_structured_block (sb));
26548
26549   return ret;
26550 }
26551
26552 /* OpenMP 2.5:
26553    # pragma omp master new-line
26554      structured-block  */
26555
26556 static tree
26557 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26558 {
26559   cp_parser_require_pragma_eol (parser, pragma_tok);
26560   return c_finish_omp_master (input_location,
26561                               cp_parser_omp_structured_block (parser));
26562 }
26563
26564 /* OpenMP 2.5:
26565    # pragma omp ordered new-line
26566      structured-block  */
26567
26568 static tree
26569 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26570 {
26571   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26572   cp_parser_require_pragma_eol (parser, pragma_tok);
26573   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26574 }
26575
26576 /* OpenMP 2.5:
26577
26578    section-scope:
26579      { section-sequence }
26580
26581    section-sequence:
26582      section-directive[opt] structured-block
26583      section-sequence section-directive structured-block  */
26584
26585 static tree
26586 cp_parser_omp_sections_scope (cp_parser *parser)
26587 {
26588   tree stmt, substmt;
26589   bool error_suppress = false;
26590   cp_token *tok;
26591
26592   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26593     return NULL_TREE;
26594
26595   stmt = push_stmt_list ();
26596
26597   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26598     {
26599       unsigned save;
26600
26601       substmt = begin_omp_structured_block ();
26602       save = cp_parser_begin_omp_structured_block (parser);
26603
26604       while (1)
26605         {
26606           cp_parser_statement (parser, NULL_TREE, false, NULL);
26607
26608           tok = cp_lexer_peek_token (parser->lexer);
26609           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26610             break;
26611           if (tok->type == CPP_CLOSE_BRACE)
26612             break;
26613           if (tok->type == CPP_EOF)
26614             break;
26615         }
26616
26617       cp_parser_end_omp_structured_block (parser, save);
26618       substmt = finish_omp_structured_block (substmt);
26619       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26620       add_stmt (substmt);
26621     }
26622
26623   while (1)
26624     {
26625       tok = cp_lexer_peek_token (parser->lexer);
26626       if (tok->type == CPP_CLOSE_BRACE)
26627         break;
26628       if (tok->type == CPP_EOF)
26629         break;
26630
26631       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26632         {
26633           cp_lexer_consume_token (parser->lexer);
26634           cp_parser_require_pragma_eol (parser, tok);
26635           error_suppress = false;
26636         }
26637       else if (!error_suppress)
26638         {
26639           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26640           error_suppress = true;
26641         }
26642
26643       substmt = cp_parser_omp_structured_block (parser);
26644       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26645       add_stmt (substmt);
26646     }
26647   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26648
26649   substmt = pop_stmt_list (stmt);
26650
26651   stmt = make_node (OMP_SECTIONS);
26652   TREE_TYPE (stmt) = void_type_node;
26653   OMP_SECTIONS_BODY (stmt) = substmt;
26654
26655   add_stmt (stmt);
26656   return stmt;
26657 }
26658
26659 /* OpenMP 2.5:
26660    # pragma omp sections sections-clause[optseq] newline
26661      sections-scope  */
26662
26663 #define OMP_SECTIONS_CLAUSE_MASK                        \
26664         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26665         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26666         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26667         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26668         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26669
26670 static tree
26671 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26672 {
26673   tree clauses, ret;
26674
26675   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26676                                        "#pragma omp sections", pragma_tok);
26677
26678   ret = cp_parser_omp_sections_scope (parser);
26679   if (ret)
26680     OMP_SECTIONS_CLAUSES (ret) = clauses;
26681
26682   return ret;
26683 }
26684
26685 /* OpenMP 2.5:
26686    # pragma parallel parallel-clause new-line
26687    # pragma parallel for parallel-for-clause new-line
26688    # pragma parallel sections parallel-sections-clause new-line  */
26689
26690 #define OMP_PARALLEL_CLAUSE_MASK                        \
26691         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26692         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26693         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26694         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26695         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26696         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26697         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26698         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26699
26700 static tree
26701 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26702 {
26703   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26704   const char *p_name = "#pragma omp parallel";
26705   tree stmt, clauses, par_clause, ws_clause, block;
26706   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26707   unsigned int save;
26708   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26709
26710   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26711     {
26712       cp_lexer_consume_token (parser->lexer);
26713       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26714       p_name = "#pragma omp parallel for";
26715       mask |= OMP_FOR_CLAUSE_MASK;
26716       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26717     }
26718   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26719     {
26720       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26721       const char *p = IDENTIFIER_POINTER (id);
26722       if (strcmp (p, "sections") == 0)
26723         {
26724           cp_lexer_consume_token (parser->lexer);
26725           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26726           p_name = "#pragma omp parallel sections";
26727           mask |= OMP_SECTIONS_CLAUSE_MASK;
26728           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26729         }
26730     }
26731
26732   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26733   block = begin_omp_parallel ();
26734   save = cp_parser_begin_omp_structured_block (parser);
26735
26736   switch (p_kind)
26737     {
26738     case PRAGMA_OMP_PARALLEL:
26739       cp_parser_statement (parser, NULL_TREE, false, NULL);
26740       par_clause = clauses;
26741       break;
26742
26743     case PRAGMA_OMP_PARALLEL_FOR:
26744       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26745       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26746       break;
26747
26748     case PRAGMA_OMP_PARALLEL_SECTIONS:
26749       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26750       stmt = cp_parser_omp_sections_scope (parser);
26751       if (stmt)
26752         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26753       break;
26754
26755     default:
26756       gcc_unreachable ();
26757     }
26758
26759   cp_parser_end_omp_structured_block (parser, save);
26760   stmt = finish_omp_parallel (par_clause, block);
26761   if (p_kind != PRAGMA_OMP_PARALLEL)
26762     OMP_PARALLEL_COMBINED (stmt) = 1;
26763   return stmt;
26764 }
26765
26766 /* OpenMP 2.5:
26767    # pragma omp single single-clause[optseq] new-line
26768      structured-block  */
26769
26770 #define OMP_SINGLE_CLAUSE_MASK                          \
26771         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26772         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26773         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26774         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26775
26776 static tree
26777 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26778 {
26779   tree stmt = make_node (OMP_SINGLE);
26780   TREE_TYPE (stmt) = void_type_node;
26781
26782   OMP_SINGLE_CLAUSES (stmt)
26783     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26784                                  "#pragma omp single", pragma_tok);
26785   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26786
26787   return add_stmt (stmt);
26788 }
26789
26790 /* OpenMP 3.0:
26791    # pragma omp task task-clause[optseq] new-line
26792      structured-block  */
26793
26794 #define OMP_TASK_CLAUSE_MASK                            \
26795         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26796         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26797         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26798         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26799         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26800         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26801         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26802         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26803
26804 static tree
26805 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26806 {
26807   tree clauses, block;
26808   unsigned int save;
26809
26810   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26811                                        "#pragma omp task", pragma_tok);
26812   block = begin_omp_task ();
26813   save = cp_parser_begin_omp_structured_block (parser);
26814   cp_parser_statement (parser, NULL_TREE, false, NULL);
26815   cp_parser_end_omp_structured_block (parser, save);
26816   return finish_omp_task (clauses, block);
26817 }
26818
26819 /* OpenMP 3.0:
26820    # pragma omp taskwait new-line  */
26821
26822 static void
26823 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26824 {
26825   cp_parser_require_pragma_eol (parser, pragma_tok);
26826   finish_omp_taskwait ();
26827 }
26828
26829 /* OpenMP 3.1:
26830    # pragma omp taskyield new-line  */
26831
26832 static void
26833 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26834 {
26835   cp_parser_require_pragma_eol (parser, pragma_tok);
26836   finish_omp_taskyield ();
26837 }
26838
26839 /* OpenMP 2.5:
26840    # pragma omp threadprivate (variable-list) */
26841
26842 static void
26843 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26844 {
26845   tree vars;
26846
26847   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26848   cp_parser_require_pragma_eol (parser, pragma_tok);
26849
26850   finish_omp_threadprivate (vars);
26851 }
26852
26853 /* Main entry point to OpenMP statement pragmas.  */
26854
26855 static void
26856 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26857 {
26858   tree stmt;
26859
26860   switch (pragma_tok->pragma_kind)
26861     {
26862     case PRAGMA_OMP_ATOMIC:
26863       cp_parser_omp_atomic (parser, pragma_tok);
26864       return;
26865     case PRAGMA_OMP_CRITICAL:
26866       stmt = cp_parser_omp_critical (parser, pragma_tok);
26867       break;
26868     case PRAGMA_OMP_FOR:
26869       stmt = cp_parser_omp_for (parser, pragma_tok);
26870       break;
26871     case PRAGMA_OMP_MASTER:
26872       stmt = cp_parser_omp_master (parser, pragma_tok);
26873       break;
26874     case PRAGMA_OMP_ORDERED:
26875       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26876       break;
26877     case PRAGMA_OMP_PARALLEL:
26878       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26879       break;
26880     case PRAGMA_OMP_SECTIONS:
26881       stmt = cp_parser_omp_sections (parser, pragma_tok);
26882       break;
26883     case PRAGMA_OMP_SINGLE:
26884       stmt = cp_parser_omp_single (parser, pragma_tok);
26885       break;
26886     case PRAGMA_OMP_TASK:
26887       stmt = cp_parser_omp_task (parser, pragma_tok);
26888       break;
26889     default:
26890       gcc_unreachable ();
26891     }
26892
26893   if (stmt)
26894     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26895 }
26896 \f
26897 /* Transactional Memory parsing routines.  */
26898
26899 /* Parse a transaction attribute.
26900
26901    txn-attribute:
26902         attribute
26903         [ [ identifier ] ]
26904
26905    ??? Simplify this when C++0x bracket attributes are
26906    implemented properly.  */
26907
26908 static tree
26909 cp_parser_txn_attribute_opt (cp_parser *parser)
26910 {
26911   cp_token *token;
26912   tree attr_name, attr = NULL;
26913
26914   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26915     return cp_parser_attributes_opt (parser);
26916
26917   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26918     return NULL_TREE;
26919   cp_lexer_consume_token (parser->lexer);
26920   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26921     goto error1;
26922
26923   token = cp_lexer_peek_token (parser->lexer);
26924   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26925     {
26926       token = cp_lexer_consume_token (parser->lexer);
26927
26928       attr_name = (token->type == CPP_KEYWORD
26929                    /* For keywords, use the canonical spelling,
26930                       not the parsed identifier.  */
26931                    ? ridpointers[(int) token->keyword]
26932                    : token->u.value);
26933       attr = build_tree_list (attr_name, NULL_TREE);
26934     }
26935   else
26936     cp_parser_error (parser, "expected identifier");
26937
26938   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26939  error1:
26940   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26941   return attr;
26942 }
26943
26944 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26945
26946    transaction-statement:
26947      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
26948        compound-statement
26949      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
26950 */
26951
26952 static tree
26953 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26954 {
26955   unsigned char old_in = parser->in_transaction;
26956   unsigned char this_in = 1, new_in;
26957   cp_token *token;
26958   tree stmt, attrs, noex;
26959
26960   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26961       || keyword == RID_TRANSACTION_RELAXED);
26962   token = cp_parser_require_keyword (parser, keyword,
26963       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26964           : RT_TRANSACTION_RELAXED));
26965   gcc_assert (token != NULL);
26966
26967   if (keyword == RID_TRANSACTION_RELAXED)
26968     this_in |= TM_STMT_ATTR_RELAXED;
26969   else
26970     {
26971       attrs = cp_parser_txn_attribute_opt (parser);
26972       if (attrs)
26973         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26974     }
26975
26976   /* Parse a noexcept specification.  */
26977   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
26978
26979   /* Keep track if we're in the lexical scope of an outer transaction.  */
26980   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
26981
26982   stmt = begin_transaction_stmt (token->location, NULL, this_in);
26983
26984   parser->in_transaction = new_in;
26985   cp_parser_compound_statement (parser, NULL, false, false);
26986   parser->in_transaction = old_in;
26987
26988   finish_transaction_stmt (stmt, NULL, this_in, noex);
26989
26990   return stmt;
26991 }
26992
26993 /* Parse a __transaction_atomic or __transaction_relaxed expression.
26994
26995    transaction-expression:
26996      __transaction_atomic txn-noexcept-spec[opt] ( expression )
26997      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
26998 */
26999
27000 static tree
27001 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27002 {
27003   unsigned char old_in = parser->in_transaction;
27004   unsigned char this_in = 1;
27005   cp_token *token;
27006   tree expr, noex;
27007   bool noex_expr;
27008
27009   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27010       || keyword == RID_TRANSACTION_RELAXED);
27011
27012   if (!flag_tm)
27013     error (keyword == RID_TRANSACTION_RELAXED
27014            ? G_("%<__transaction_relaxed%> without transactional memory "
27015                 "support enabled")
27016            : G_("%<__transaction_atomic%> without transactional memory "
27017                 "support enabled"));
27018
27019   token = cp_parser_require_keyword (parser, keyword,
27020       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27021           : RT_TRANSACTION_RELAXED));
27022   gcc_assert (token != NULL);
27023
27024   if (keyword == RID_TRANSACTION_RELAXED)
27025     this_in |= TM_STMT_ATTR_RELAXED;
27026
27027   /* Set this early.  This might mean that we allow transaction_cancel in
27028      an expression that we find out later actually has to be a constexpr.
27029      However, we expect that cxx_constant_value will be able to deal with
27030      this; also, if the noexcept has no constexpr, then what we parse next
27031      really is a transaction's body.  */
27032   parser->in_transaction = this_in;
27033
27034   /* Parse a noexcept specification.  */
27035   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27036                                                true);
27037
27038   if (!noex || !noex_expr
27039       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27040     {
27041       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27042
27043       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27044       finish_parenthesized_expr (expr);
27045
27046       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27047     }
27048   else
27049     {
27050       /* The only expression that is available got parsed for the noexcept
27051          already.  noexcept is true then.  */
27052       expr = noex;
27053       noex = boolean_true_node;
27054     }
27055
27056   expr = build_transaction_expr (token->location, expr, this_in, noex);
27057   parser->in_transaction = old_in;
27058
27059   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27060     return error_mark_node;
27061
27062   return (flag_tm ? expr : error_mark_node);
27063 }
27064
27065 /* Parse a function-transaction-block.
27066
27067    function-transaction-block:
27068      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27069          function-body
27070      __transaction_atomic txn-attribute[opt] function-try-block
27071      __transaction_relaxed ctor-initializer[opt] function-body
27072      __transaction_relaxed function-try-block
27073 */
27074
27075 static bool
27076 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27077 {
27078   unsigned char old_in = parser->in_transaction;
27079   unsigned char new_in = 1;
27080   tree compound_stmt, stmt, attrs;
27081   bool ctor_initializer_p;
27082   cp_token *token;
27083
27084   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27085       || keyword == RID_TRANSACTION_RELAXED);
27086   token = cp_parser_require_keyword (parser, keyword,
27087       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27088           : RT_TRANSACTION_RELAXED));
27089   gcc_assert (token != NULL);
27090
27091   if (keyword == RID_TRANSACTION_RELAXED)
27092     new_in |= TM_STMT_ATTR_RELAXED;
27093   else
27094     {
27095       attrs = cp_parser_txn_attribute_opt (parser);
27096       if (attrs)
27097         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27098     }
27099
27100   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27101
27102   parser->in_transaction = new_in;
27103
27104   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27105     ctor_initializer_p = cp_parser_function_try_block (parser);
27106   else
27107     ctor_initializer_p
27108       = cp_parser_ctor_initializer_opt_and_function_body (parser);
27109
27110   parser->in_transaction = old_in;
27111
27112   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27113
27114   return ctor_initializer_p;
27115 }
27116
27117 /* Parse a __transaction_cancel statement.
27118
27119    cancel-statement:
27120      __transaction_cancel txn-attribute[opt] ;
27121      __transaction_cancel txn-attribute[opt] throw-expression ;
27122
27123    ??? Cancel and throw is not yet implemented.  */
27124
27125 static tree
27126 cp_parser_transaction_cancel (cp_parser *parser)
27127 {
27128   cp_token *token;
27129   bool is_outer = false;
27130   tree stmt, attrs;
27131
27132   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27133                                      RT_TRANSACTION_CANCEL);
27134   gcc_assert (token != NULL);
27135
27136   attrs = cp_parser_txn_attribute_opt (parser);
27137   if (attrs)
27138     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27139
27140   /* ??? Parse cancel-and-throw here.  */
27141
27142   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27143
27144   if (!flag_tm)
27145     {
27146       error_at (token->location, "%<__transaction_cancel%> without "
27147                 "transactional memory support enabled");
27148       return error_mark_node;
27149     }
27150   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27151     {
27152       error_at (token->location, "%<__transaction_cancel%> within a "
27153                 "%<__transaction_relaxed%>");
27154       return error_mark_node;
27155     }
27156   else if (is_outer)
27157     {
27158       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27159           && !is_tm_may_cancel_outer (current_function_decl))
27160         {
27161           error_at (token->location, "outer %<__transaction_cancel%> not "
27162                     "within outer %<__transaction_atomic%>");
27163           error_at (token->location,
27164                     "  or a %<transaction_may_cancel_outer%> function");
27165           return error_mark_node;
27166         }
27167     }
27168   else if (parser->in_transaction == 0)
27169     {
27170       error_at (token->location, "%<__transaction_cancel%> not within "
27171                 "%<__transaction_atomic%>");
27172       return error_mark_node;
27173     }
27174
27175   stmt = build_tm_abort_call (token->location, is_outer);
27176   add_stmt (stmt);
27177   finish_stmt ();
27178
27179   return stmt;
27180 }
27181 \f
27182 /* The parser.  */
27183
27184 static GTY (()) cp_parser *the_parser;
27185
27186 \f
27187 /* Special handling for the first token or line in the file.  The first
27188    thing in the file might be #pragma GCC pch_preprocess, which loads a
27189    PCH file, which is a GC collection point.  So we need to handle this
27190    first pragma without benefit of an existing lexer structure.
27191
27192    Always returns one token to the caller in *FIRST_TOKEN.  This is
27193    either the true first token of the file, or the first token after
27194    the initial pragma.  */
27195
27196 static void
27197 cp_parser_initial_pragma (cp_token *first_token)
27198 {
27199   tree name = NULL;
27200
27201   cp_lexer_get_preprocessor_token (NULL, first_token);
27202   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27203     return;
27204
27205   cp_lexer_get_preprocessor_token (NULL, first_token);
27206   if (first_token->type == CPP_STRING)
27207     {
27208       name = first_token->u.value;
27209
27210       cp_lexer_get_preprocessor_token (NULL, first_token);
27211       if (first_token->type != CPP_PRAGMA_EOL)
27212         error_at (first_token->location,
27213                   "junk at end of %<#pragma GCC pch_preprocess%>");
27214     }
27215   else
27216     error_at (first_token->location, "expected string literal");
27217
27218   /* Skip to the end of the pragma.  */
27219   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27220     cp_lexer_get_preprocessor_token (NULL, first_token);
27221
27222   /* Now actually load the PCH file.  */
27223   if (name)
27224     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27225
27226   /* Read one more token to return to our caller.  We have to do this
27227      after reading the PCH file in, since its pointers have to be
27228      live.  */
27229   cp_lexer_get_preprocessor_token (NULL, first_token);
27230 }
27231
27232 /* Normal parsing of a pragma token.  Here we can (and must) use the
27233    regular lexer.  */
27234
27235 static bool
27236 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27237 {
27238   cp_token *pragma_tok;
27239   unsigned int id;
27240
27241   pragma_tok = cp_lexer_consume_token (parser->lexer);
27242   gcc_assert (pragma_tok->type == CPP_PRAGMA);
27243   parser->lexer->in_pragma = true;
27244
27245   id = pragma_tok->pragma_kind;
27246   switch (id)
27247     {
27248     case PRAGMA_GCC_PCH_PREPROCESS:
27249       error_at (pragma_tok->location,
27250                 "%<#pragma GCC pch_preprocess%> must be first");
27251       break;
27252
27253     case PRAGMA_OMP_BARRIER:
27254       switch (context)
27255         {
27256         case pragma_compound:
27257           cp_parser_omp_barrier (parser, pragma_tok);
27258           return false;
27259         case pragma_stmt:
27260           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27261                     "used in compound statements");
27262           break;
27263         default:
27264           goto bad_stmt;
27265         }
27266       break;
27267
27268     case PRAGMA_OMP_FLUSH:
27269       switch (context)
27270         {
27271         case pragma_compound:
27272           cp_parser_omp_flush (parser, pragma_tok);
27273           return false;
27274         case pragma_stmt:
27275           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27276                     "used in compound statements");
27277           break;
27278         default:
27279           goto bad_stmt;
27280         }
27281       break;
27282
27283     case PRAGMA_OMP_TASKWAIT:
27284       switch (context)
27285         {
27286         case pragma_compound:
27287           cp_parser_omp_taskwait (parser, pragma_tok);
27288           return false;
27289         case pragma_stmt:
27290           error_at (pragma_tok->location,
27291                     "%<#pragma omp taskwait%> may only be "
27292                     "used in compound statements");
27293           break;
27294         default:
27295           goto bad_stmt;
27296         }
27297       break;
27298
27299     case PRAGMA_OMP_TASKYIELD:
27300       switch (context)
27301         {
27302         case pragma_compound:
27303           cp_parser_omp_taskyield (parser, pragma_tok);
27304           return false;
27305         case pragma_stmt:
27306           error_at (pragma_tok->location,
27307                     "%<#pragma omp taskyield%> may only be "
27308                     "used in compound statements");
27309           break;
27310         default:
27311           goto bad_stmt;
27312         }
27313       break;
27314
27315     case PRAGMA_OMP_THREADPRIVATE:
27316       cp_parser_omp_threadprivate (parser, pragma_tok);
27317       return false;
27318
27319     case PRAGMA_OMP_ATOMIC:
27320     case PRAGMA_OMP_CRITICAL:
27321     case PRAGMA_OMP_FOR:
27322     case PRAGMA_OMP_MASTER:
27323     case PRAGMA_OMP_ORDERED:
27324     case PRAGMA_OMP_PARALLEL:
27325     case PRAGMA_OMP_SECTIONS:
27326     case PRAGMA_OMP_SINGLE:
27327     case PRAGMA_OMP_TASK:
27328       if (context == pragma_external)
27329         goto bad_stmt;
27330       cp_parser_omp_construct (parser, pragma_tok);
27331       return true;
27332
27333     case PRAGMA_OMP_SECTION:
27334       error_at (pragma_tok->location, 
27335                 "%<#pragma omp section%> may only be used in "
27336                 "%<#pragma omp sections%> construct");
27337       break;
27338
27339     default:
27340       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27341       c_invoke_pragma_handler (id);
27342       break;
27343
27344     bad_stmt:
27345       cp_parser_error (parser, "expected declaration specifiers");
27346       break;
27347     }
27348
27349   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27350   return false;
27351 }
27352
27353 /* The interface the pragma parsers have to the lexer.  */
27354
27355 enum cpp_ttype
27356 pragma_lex (tree *value)
27357 {
27358   cp_token *tok;
27359   enum cpp_ttype ret;
27360
27361   tok = cp_lexer_peek_token (the_parser->lexer);
27362
27363   ret = tok->type;
27364   *value = tok->u.value;
27365
27366   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27367     ret = CPP_EOF;
27368   else if (ret == CPP_STRING)
27369     *value = cp_parser_string_literal (the_parser, false, false);
27370   else
27371     {
27372       cp_lexer_consume_token (the_parser->lexer);
27373       if (ret == CPP_KEYWORD)
27374         ret = CPP_NAME;
27375     }
27376
27377   return ret;
27378 }
27379
27380 \f
27381 /* External interface.  */
27382
27383 /* Parse one entire translation unit.  */
27384
27385 void
27386 c_parse_file (void)
27387 {
27388   static bool already_called = false;
27389
27390   if (already_called)
27391     {
27392       sorry ("inter-module optimizations not implemented for C++");
27393       return;
27394     }
27395   already_called = true;
27396
27397   the_parser = cp_parser_new ();
27398   push_deferring_access_checks (flag_access_control
27399                                 ? dk_no_deferred : dk_no_check);
27400   cp_parser_translation_unit (the_parser);
27401   the_parser = NULL;
27402 }
27403
27404 #include "gt-cp-parser.h"