db9301fcb7328775ea8cddd6350417e1f0f28dcd
[dragonfly.git] / contrib / gcc-5.0 / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2    Copyright (C) 2000-2015 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "print-tree.h"
38 #include "stringpool.h"
39 #include "attribs.h"
40 #include "trans-mem.h"
41 #include "cp-tree.h"
42 #include "intl.h"
43 #include "c-family/c-pragma.h"
44 #include "decl.h"
45 #include "flags.h"
46 #include "diagnostic-core.h"
47 #include "target.h"
48 #include "hash-map.h"
49 #include "is-a.h"
50 #include "plugin-api.h"
51 #include "hard-reg-set.h"
52 #include "input.h"
53 #include "function.h"
54 #include "ipa-ref.h"
55 #include "cgraph.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-objc.h"
58 #include "plugin.h"
59 #include "tree-pretty-print.h"
60 #include "parser.h"
61 #include "type-utils.h"
62 #include "omp-low.h"
63 #include "gomp-constants.h"
64
65 \f
66 /* The lexer.  */
67
68 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
69    and c-lex.c) and the C++ parser.  */
70
71 static cp_token eof_token =
72 {
73   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
74 };
75
76 /* The various kinds of non integral constant we encounter. */
77 typedef enum non_integral_constant {
78   NIC_NONE,
79   /* floating-point literal */
80   NIC_FLOAT,
81   /* %<this%> */
82   NIC_THIS,
83   /* %<__FUNCTION__%> */
84   NIC_FUNC_NAME,
85   /* %<__PRETTY_FUNCTION__%> */
86   NIC_PRETTY_FUNC,
87   /* %<__func__%> */
88   NIC_C99_FUNC,
89   /* "%<va_arg%> */
90   NIC_VA_ARG,
91   /* a cast */
92   NIC_CAST,
93   /* %<typeid%> operator */
94   NIC_TYPEID,
95   /* non-constant compound literals */
96   NIC_NCC,
97   /* a function call */
98   NIC_FUNC_CALL,
99   /* an increment */
100   NIC_INC,
101   /* an decrement */
102   NIC_DEC,
103   /* an array reference */
104   NIC_ARRAY_REF,
105   /* %<->%> */
106   NIC_ARROW,
107   /* %<.%> */
108   NIC_POINT,
109   /* the address of a label */
110   NIC_ADDR_LABEL,
111   /* %<*%> */
112   NIC_STAR,
113   /* %<&%> */
114   NIC_ADDR,
115   /* %<++%> */
116   NIC_PREINCREMENT,
117   /* %<--%> */
118   NIC_PREDECREMENT,
119   /* %<new%> */
120   NIC_NEW,
121   /* %<delete%> */
122   NIC_DEL,
123   /* calls to overloaded operators */
124   NIC_OVERLOADED,
125   /* an assignment */
126   NIC_ASSIGNMENT,
127   /* a comma operator */
128   NIC_COMMA,
129   /* a call to a constructor */
130   NIC_CONSTRUCTOR,
131   /* a transaction expression */
132   NIC_TRANSACTION
133 } non_integral_constant;
134
135 /* The various kinds of errors about name-lookup failing. */
136 typedef enum name_lookup_error {
137   /* NULL */
138   NLE_NULL,
139   /* is not a type */
140   NLE_TYPE,
141   /* is not a class or namespace */
142   NLE_CXX98,
143   /* is not a class, namespace, or enumeration */
144   NLE_NOT_CXX98
145 } name_lookup_error;
146
147 /* The various kinds of required token */
148 typedef enum required_token {
149   RT_NONE,
150   RT_SEMICOLON,  /* ';' */
151   RT_OPEN_PAREN, /* '(' */
152   RT_CLOSE_BRACE, /* '}' */
153   RT_OPEN_BRACE,  /* '{' */
154   RT_CLOSE_SQUARE, /* ']' */
155   RT_OPEN_SQUARE,  /* '[' */
156   RT_COMMA, /* ',' */
157   RT_SCOPE, /* '::' */
158   RT_LESS, /* '<' */
159   RT_GREATER, /* '>' */
160   RT_EQ, /* '=' */
161   RT_ELLIPSIS, /* '...' */
162   RT_MULT, /* '*' */
163   RT_COMPL, /* '~' */
164   RT_COLON, /* ':' */
165   RT_COLON_SCOPE, /* ':' or '::' */
166   RT_CLOSE_PAREN, /* ')' */
167   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
168   RT_PRAGMA_EOL, /* end of line */
169   RT_NAME, /* identifier */
170
171   /* The type is CPP_KEYWORD */
172   RT_NEW, /* new */
173   RT_DELETE, /* delete */
174   RT_RETURN, /* return */
175   RT_WHILE, /* while */
176   RT_EXTERN, /* extern */
177   RT_STATIC_ASSERT, /* static_assert */
178   RT_DECLTYPE, /* decltype */
179   RT_OPERATOR, /* operator */
180   RT_CLASS, /* class */
181   RT_TEMPLATE, /* template */
182   RT_NAMESPACE, /* namespace */
183   RT_USING, /* using */
184   RT_ASM, /* asm */
185   RT_TRY, /* try */
186   RT_CATCH, /* catch */
187   RT_THROW, /* throw */
188   RT_LABEL, /* __label__ */
189   RT_AT_TRY, /* @try */
190   RT_AT_SYNCHRONIZED, /* @synchronized */
191   RT_AT_THROW, /* @throw */
192
193   RT_SELECT,  /* selection-statement */
194   RT_INTERATION, /* iteration-statement */
195   RT_JUMP, /* jump-statement */
196   RT_CLASS_KEY, /* class-key */
197   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
198   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
199   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
200   RT_TRANSACTION_CANCEL /* __transaction_cancel */
201 } required_token;
202
203 /* Prototypes.  */
204
205 static cp_lexer *cp_lexer_new_main
206   (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208   (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210   (cp_lexer *);
211 static int cp_lexer_saving_tokens
212   (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214   (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216   (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218   (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220   (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222   (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224   (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226   (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228   (cp_lexer *);
229 static void cp_lexer_purge_token
230   (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232   (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234   (cp_lexer *);
235 static void cp_lexer_commit_tokens
236   (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238   (cp_lexer *);
239 static void cp_lexer_print_token
240   (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242   (cp_lexer *);
243 static void cp_lexer_start_debugging
244   (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246   (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249   (cp_token *, cp_token *);
250
251 static void cp_parser_initial_pragma
252   (cp_token *);
253
254 static tree cp_literal_operator_id
255   (const char *);
256
257 static void cp_parser_cilk_simd
258   (cp_parser *, cp_token *);
259 static tree cp_parser_cilk_for
260   (cp_parser *, tree);
261 static bool cp_parser_omp_declare_reduction_exprs
262   (tree, cp_parser *);
263 static tree cp_parser_cilk_simd_vectorlength 
264   (cp_parser *, tree, bool);
265
266 /* Manifest constants.  */
267 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
268 #define CP_SAVED_TOKEN_STACK 5
269
270 /* Variables.  */
271
272 /* The stream to which debugging output should be written.  */
273 static FILE *cp_lexer_debug_stream;
274
275 /* Nonzero if we are parsing an unevaluated operand: an operand to
276    sizeof, typeof, or alignof.  */
277 int cp_unevaluated_operand;
278
279 /* Dump up to NUM tokens in BUFFER to FILE starting with token
280    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
281    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
282    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
283    highlighted by surrounding it in [[ ]].  */
284
285 static void
286 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
287                       cp_token *start_token, unsigned num,
288                       cp_token *curr_token)
289 {
290   unsigned i, nprinted;
291   cp_token *token;
292   bool do_print;
293
294   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
295
296   if (buffer == NULL)
297     return;
298
299   if (num == 0)
300     num = buffer->length ();
301
302   if (start_token == NULL)
303     start_token = buffer->address ();
304
305   if (start_token > buffer->address ())
306     {
307       cp_lexer_print_token (file, &(*buffer)[0]);
308       fprintf (file, " ... ");
309     }
310
311   do_print = false;
312   nprinted = 0;
313   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
314     {
315       if (token == start_token)
316         do_print = true;
317
318       if (!do_print)
319         continue;
320
321       nprinted++;
322       if (token == curr_token)
323         fprintf (file, "[[");
324
325       cp_lexer_print_token (file, token);
326
327       if (token == curr_token)
328         fprintf (file, "]]");
329
330       switch (token->type)
331         {
332           case CPP_SEMICOLON:
333           case CPP_OPEN_BRACE:
334           case CPP_CLOSE_BRACE:
335           case CPP_EOF:
336             fputc ('\n', file);
337             break;
338
339           default:
340             fputc (' ', file);
341         }
342     }
343
344   if (i == num && i < buffer->length ())
345     {
346       fprintf (file, " ... ");
347       cp_lexer_print_token (file, &buffer->last ());
348     }
349
350   fprintf (file, "\n");
351 }
352
353
354 /* Dump all tokens in BUFFER to stderr.  */
355
356 void
357 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
358 {
359   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
360 }
361
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> &ref)
364 {
365   cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
366 }
367
368 DEBUG_FUNCTION void
369 debug (vec<cp_token, va_gc> *ptr)
370 {
371   if (ptr)
372     debug (*ptr);
373   else
374     fprintf (stderr, "<nil>\n");
375 }
376
377
378 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
379    description for T.  */
380
381 static void
382 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
383 {
384   if (t)
385     {
386       fprintf (file, "%s: ", desc);
387       print_node_brief (file, "", t, 0);
388     }
389 }
390
391
392 /* Dump parser context C to FILE.  */
393
394 static void
395 cp_debug_print_context (FILE *file, cp_parser_context *c)
396 {
397   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
398   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
399   print_node_brief (file, "", c->object_type, 0);
400   fprintf (file, "}\n");
401 }
402
403
404 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
405
406 static void
407 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
408 {
409   unsigned i;
410   cp_parser_context *c;
411
412   fprintf (file, "Parsing context stack:\n");
413   for (i = 0, c = first; c; c = c->next, i++)
414     {
415       fprintf (file, "\t#%u: ", i);
416       cp_debug_print_context (file, c);
417     }
418 }
419
420
421 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
422
423 static void
424 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
425 {
426   if (flag)
427     fprintf (file, "%s: true\n", desc);
428 }
429
430
431 /* Print an unparsed function entry UF to FILE.  */
432
433 static void
434 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
435 {
436   unsigned i;
437   cp_default_arg_entry *default_arg_fn;
438   tree fn;
439
440   fprintf (file, "\tFunctions with default args:\n");
441   for (i = 0;
442        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
443        i++)
444     {
445       fprintf (file, "\t\tClass type: ");
446       print_node_brief (file, "", default_arg_fn->class_type, 0);
447       fprintf (file, "\t\tDeclaration: ");
448       print_node_brief (file, "", default_arg_fn->decl, 0);
449       fprintf (file, "\n");
450     }
451
452   fprintf (file, "\n\tFunctions with definitions that require "
453            "post-processing\n\t\t");
454   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
455     {
456       print_node_brief (file, "", fn, 0);
457       fprintf (file, " ");
458     }
459   fprintf (file, "\n");
460
461   fprintf (file, "\n\tNon-static data members with initializers that require "
462            "post-processing\n\t\t");
463   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
464     {
465       print_node_brief (file, "", fn, 0);
466       fprintf (file, " ");
467     }
468   fprintf (file, "\n");
469 }
470
471
472 /* Print the stack of unparsed member functions S to FILE.  */
473
474 static void
475 cp_debug_print_unparsed_queues (FILE *file,
476                                 vec<cp_unparsed_functions_entry, va_gc> *s)
477 {
478   unsigned i;
479   cp_unparsed_functions_entry *uf;
480
481   fprintf (file, "Unparsed functions\n");
482   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
483     {
484       fprintf (file, "#%u:\n", i);
485       cp_debug_print_unparsed_function (file, uf);
486     }
487 }
488
489
490 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
491    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
492
493 static void
494 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
495 {
496   cp_token *next_token, *first_token, *start_token;
497
498   if (file == NULL)
499     file = stderr;
500
501   next_token = parser->lexer->next_token;
502   first_token = parser->lexer->buffer->address ();
503   start_token = (next_token > first_token + window_size / 2)
504                 ? next_token - window_size / 2
505                 : first_token;
506   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
507                         next_token);
508 }
509
510
511 /* Dump debugging information for the given PARSER.  If FILE is NULL,
512    the output is printed on stderr.  */
513
514 void
515 cp_debug_parser (FILE *file, cp_parser *parser)
516 {
517   const size_t window_size = 20;
518   cp_token *token;
519   expanded_location eloc;
520
521   if (file == NULL)
522     file = stderr;
523
524   fprintf (file, "Parser state\n\n");
525   fprintf (file, "Number of tokens: %u\n",
526            vec_safe_length (parser->lexer->buffer));
527   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
528   cp_debug_print_tree_if_set (file, "Object scope",
529                                      parser->object_scope);
530   cp_debug_print_tree_if_set (file, "Qualifying scope",
531                                      parser->qualifying_scope);
532   cp_debug_print_context_stack (file, parser->context);
533   cp_debug_print_flag (file, "Allow GNU extensions",
534                               parser->allow_gnu_extensions_p);
535   cp_debug_print_flag (file, "'>' token is greater-than",
536                               parser->greater_than_is_operator_p);
537   cp_debug_print_flag (file, "Default args allowed in current "
538                               "parameter list", parser->default_arg_ok_p);
539   cp_debug_print_flag (file, "Parsing integral constant-expression",
540                               parser->integral_constant_expression_p);
541   cp_debug_print_flag (file, "Allow non-constant expression in current "
542                               "constant-expression",
543                               parser->allow_non_integral_constant_expression_p);
544   cp_debug_print_flag (file, "Seen non-constant expression",
545                               parser->non_integral_constant_expression_p);
546   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
547                               "current context",
548                               parser->local_variables_forbidden_p);
549   cp_debug_print_flag (file, "In unbraced linkage specification",
550                               parser->in_unbraced_linkage_specification_p);
551   cp_debug_print_flag (file, "Parsing a declarator",
552                               parser->in_declarator_p);
553   cp_debug_print_flag (file, "In template argument list",
554                               parser->in_template_argument_list_p);
555   cp_debug_print_flag (file, "Parsing an iteration statement",
556                               parser->in_statement & IN_ITERATION_STMT);
557   cp_debug_print_flag (file, "Parsing a switch statement",
558                               parser->in_statement & IN_SWITCH_STMT);
559   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
560                               parser->in_statement & IN_OMP_BLOCK);
561   cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
562                               parser->in_statement & IN_CILK_SIMD_FOR);
563   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
564                               parser->in_statement & IN_OMP_FOR);
565   cp_debug_print_flag (file, "Parsing an if statement",
566                               parser->in_statement & IN_IF_STMT);
567   cp_debug_print_flag (file, "Parsing a type-id in an expression "
568                               "context", parser->in_type_id_in_expr_p);
569   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
570                               parser->implicit_extern_c);
571   cp_debug_print_flag (file, "String expressions should be translated "
572                               "to execution character set",
573                               parser->translate_strings_p);
574   cp_debug_print_flag (file, "Parsing function body outside of a "
575                               "local class", parser->in_function_body);
576   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
577                               parser->colon_corrects_to_scope_p);
578   cp_debug_print_flag (file, "Colon doesn't start a class definition",
579                               parser->colon_doesnt_start_class_def_p);
580   if (parser->type_definition_forbidden_message)
581     fprintf (file, "Error message for forbidden type definitions: %s\n",
582              parser->type_definition_forbidden_message);
583   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
584   fprintf (file, "Number of class definitions in progress: %u\n",
585            parser->num_classes_being_defined);
586   fprintf (file, "Number of template parameter lists for the current "
587            "declaration: %u\n", parser->num_template_parameter_lists);
588   cp_debug_parser_tokens (file, parser, window_size);
589   token = parser->lexer->next_token;
590   fprintf (file, "Next token to parse:\n");
591   fprintf (file, "\tToken:  ");
592   cp_lexer_print_token (file, token);
593   eloc = expand_location (token->location);
594   fprintf (file, "\n\tFile:   %s\n", eloc.file);
595   fprintf (file, "\tLine:   %d\n", eloc.line);
596   fprintf (file, "\tColumn: %d\n", eloc.column);
597 }
598
599 DEBUG_FUNCTION void
600 debug (cp_parser &ref)
601 {
602   cp_debug_parser (stderr, &ref);
603 }
604
605 DEBUG_FUNCTION void
606 debug (cp_parser *ptr)
607 {
608   if (ptr)
609     debug (*ptr);
610   else
611     fprintf (stderr, "<nil>\n");
612 }
613
614 /* Allocate memory for a new lexer object and return it.  */
615
616 static cp_lexer *
617 cp_lexer_alloc (void)
618 {
619   cp_lexer *lexer;
620
621   c_common_no_more_pch ();
622
623   /* Allocate the memory.  */
624   lexer = ggc_cleared_alloc<cp_lexer> ();
625
626   /* Initially we are not debugging.  */
627   lexer->debugging_p = false;
628
629   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630
631   /* Create the buffer.  */
632   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
633
634   return lexer;
635 }
636
637
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
639    preprocessor.  */
640
641 static cp_lexer *
642 cp_lexer_new_main (void)
643 {
644   cp_lexer *lexer;
645   cp_token token;
646
647   /* It's possible that parsing the first pragma will load a PCH file,
648      which is a GC collection point.  So we have to do that before
649      allocating any memory.  */
650   cp_parser_initial_pragma (&token);
651
652   lexer = cp_lexer_alloc ();
653
654   /* Put the first token in the buffer.  */
655   lexer->buffer->quick_push (token);
656
657   /* Get the remaining tokens from the preprocessor.  */
658   while (token.type != CPP_EOF)
659     {
660       cp_lexer_get_preprocessor_token (lexer, &token);
661       vec_safe_push (lexer->buffer, token);
662     }
663
664   lexer->last_token = lexer->buffer->address ()
665                       + lexer->buffer->length ()
666                       - 1;
667   lexer->next_token = lexer->buffer->length ()
668                       ? lexer->buffer->address ()
669                       : &eof_token;
670
671   /* Subsequent preprocessor diagnostics should use compiler
672      diagnostic functions to get the compiler source location.  */
673   done_lexing = true;
674
675   gcc_assert (!lexer->next_token->purged_p);
676   return lexer;
677 }
678
679 /* Create a new lexer whose token stream is primed with the tokens in
680    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
681
682 static cp_lexer *
683 cp_lexer_new_from_tokens (cp_token_cache *cache)
684 {
685   cp_token *first = cache->first;
686   cp_token *last = cache->last;
687   cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
688
689   /* We do not own the buffer.  */
690   lexer->buffer = NULL;
691   lexer->next_token = first == last ? &eof_token : first;
692   lexer->last_token = last;
693
694   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
695
696   /* Initially we are not debugging.  */
697   lexer->debugging_p = false;
698
699   gcc_assert (!lexer->next_token->purged_p);
700   return lexer;
701 }
702
703 /* Frees all resources associated with LEXER.  */
704
705 static void
706 cp_lexer_destroy (cp_lexer *lexer)
707 {
708   vec_free (lexer->buffer);
709   lexer->saved_tokens.release ();
710   ggc_free (lexer);
711 }
712
713 /* Returns nonzero if debugging information should be output.  */
714
715 static inline bool
716 cp_lexer_debugging_p (cp_lexer *lexer)
717 {
718   return lexer->debugging_p;
719 }
720
721
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 {
725   gcc_assert (!previous_p || lexer->next_token != &eof_token);
726
727   return lexer->next_token - previous_p;
728 }
729
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 {
733   return pos;
734 }
735
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 {
739   lexer->next_token = cp_lexer_token_at (lexer, pos);
740 }
741
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
744 {
745   if (lexer->next_token == &eof_token)
746     return lexer->last_token - 1;
747   else
748     return cp_lexer_token_position (lexer, true);
749 }
750
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
753 {
754   cp_token_position tp = cp_lexer_previous_token_position (lexer);
755
756   return cp_lexer_token_at (lexer, tp);
757 }
758
759 /* nonzero if we are presently saving tokens.  */
760
761 static inline int
762 cp_lexer_saving_tokens (const cp_lexer* lexer)
763 {
764   return lexer->saved_tokens.length () != 0;
765 }
766
767 /* Store the next token from the preprocessor in *TOKEN.  Return true
768    if we reach EOF.  If LEXER is NULL, assume we are handling an
769    initial #pragma pch_preprocess, and thus want the lexer to return
770    processed strings.  */
771
772 static void
773 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
774 {
775   static int is_extern_c = 0;
776
777    /* Get a new token from the preprocessor.  */
778   token->type
779     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
780                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
781   token->keyword = RID_MAX;
782   token->pragma_kind = PRAGMA_NONE;
783   token->purged_p = false;
784   token->error_reported = false;
785
786   /* On some systems, some header files are surrounded by an
787      implicit extern "C" block.  Set a flag in the token if it
788      comes from such a header.  */
789   is_extern_c += pending_lang_change;
790   pending_lang_change = 0;
791   token->implicit_extern_c = is_extern_c > 0;
792
793   /* Check to see if this token is a keyword.  */
794   if (token->type == CPP_NAME)
795     {
796       if (C_IS_RESERVED_WORD (token->u.value))
797         {
798           /* Mark this token as a keyword.  */
799           token->type = CPP_KEYWORD;
800           /* Record which keyword.  */
801           token->keyword = C_RID_CODE (token->u.value);
802         }
803       else
804         {
805           if (warn_cxx0x_compat
806               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
807               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
808             {
809               /* Warn about the C++0x keyword (but still treat it as
810                  an identifier).  */
811               warning (OPT_Wc__0x_compat, 
812                        "identifier %qE is a keyword in C++11",
813                        token->u.value);
814
815               /* Clear out the C_RID_CODE so we don't warn about this
816                  particular identifier-turned-keyword again.  */
817               C_SET_RID_CODE (token->u.value, RID_MAX);
818             }
819
820           token->keyword = RID_MAX;
821         }
822     }
823   else if (token->type == CPP_AT_NAME)
824     {
825       /* This only happens in Objective-C++; it must be a keyword.  */
826       token->type = CPP_KEYWORD;
827       switch (C_RID_CODE (token->u.value))
828         {
829           /* Replace 'class' with '@class', 'private' with '@private',
830              etc.  This prevents confusion with the C++ keyword
831              'class', and makes the tokens consistent with other
832              Objective-C 'AT' keywords.  For example '@class' is
833              reported as RID_AT_CLASS which is consistent with
834              '@synchronized', which is reported as
835              RID_AT_SYNCHRONIZED.
836           */
837         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
838         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
839         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
840         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
841         case RID_THROW:     token->keyword = RID_AT_THROW; break;
842         case RID_TRY:       token->keyword = RID_AT_TRY; break;
843         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
844         default:            token->keyword = C_RID_CODE (token->u.value);
845         }
846     }
847   else if (token->type == CPP_PRAGMA)
848     {
849       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
850       token->pragma_kind = ((enum pragma_kind)
851                             TREE_INT_CST_LOW (token->u.value));
852       token->u.value = NULL_TREE;
853     }
854 }
855
856 /* Update the globals input_location and the input file stack from TOKEN.  */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
859 {
860   if (token->type != CPP_EOF)
861     {
862       input_location = token->location;
863     }
864 }
865
866 /* Update the globals input_location and the input file stack from LEXER.  */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
869 {
870   cp_token *token = cp_lexer_peek_token (lexer);
871   cp_lexer_set_source_position_from_token (token);
872 }
873
874 /* Return a pointer to the next token in the token stream, but do not
875    consume it.  */
876
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
879 {
880   if (cp_lexer_debugging_p (lexer))
881     {
882       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884       putc ('\n', cp_lexer_debug_stream);
885     }
886   return lexer->next_token;
887 }
888
889 /* Return true if the next token has the indicated TYPE.  */
890
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 {
894   return cp_lexer_peek_token (lexer)->type == type;
895 }
896
897 /* Return true if the next token does not have the indicated TYPE.  */
898
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 {
902   return !cp_lexer_next_token_is (lexer, type);
903 }
904
905 /* Return true if the next token is the indicated KEYWORD.  */
906
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 {
910   return cp_lexer_peek_token (lexer)->keyword == keyword;
911 }
912
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 {
916   return cp_lexer_peek_nth_token (lexer, n)->type == type;
917 }
918
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 {
922   return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
923 }
924
925 /* Return true if the next token is not the indicated KEYWORD.  */
926
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
929 {
930   return cp_lexer_peek_token (lexer)->keyword != keyword;
931 }
932
933 /* Return true if the next token is a keyword for a decl-specifier.  */
934
935 static bool
936 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
937 {
938   cp_token *token;
939
940   token = cp_lexer_peek_token (lexer);
941   switch (token->keyword) 
942     {
943       /* auto specifier: storage-class-specifier in C++,
944          simple-type-specifier in C++0x.  */
945     case RID_AUTO:
946       /* Storage classes.  */
947     case RID_REGISTER:
948     case RID_STATIC:
949     case RID_EXTERN:
950     case RID_MUTABLE:
951     case RID_THREAD:
952       /* Elaborated type specifiers.  */
953     case RID_ENUM:
954     case RID_CLASS:
955     case RID_STRUCT:
956     case RID_UNION:
957     case RID_TYPENAME:
958       /* Simple type specifiers.  */
959     case RID_CHAR:
960     case RID_CHAR16:
961     case RID_CHAR32:
962     case RID_WCHAR:
963     case RID_BOOL:
964     case RID_SHORT:
965     case RID_INT:
966     case RID_LONG:
967     case RID_SIGNED:
968     case RID_UNSIGNED:
969     case RID_FLOAT:
970     case RID_DOUBLE:
971     case RID_VOID:
972       /* GNU extensions.  */ 
973     case RID_ATTRIBUTE:
974     case RID_TYPEOF:
975       /* C++0x extensions.  */
976     case RID_DECLTYPE:
977     case RID_UNDERLYING_TYPE:
978       return true;
979
980     default:
981       if (token->keyword >= RID_FIRST_INT_N
982           && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
983           && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
984         return true;
985       return false;
986     }
987 }
988
989 /* Returns TRUE iff the token T begins a decltype type.  */
990
991 static bool
992 token_is_decltype (cp_token *t)
993 {
994   return (t->keyword == RID_DECLTYPE
995           || t->type == CPP_DECLTYPE);
996 }
997
998 /* Returns TRUE iff the next token begins a decltype type.  */
999
1000 static bool
1001 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1002 {
1003   cp_token *t = cp_lexer_peek_token (lexer);
1004   return token_is_decltype (t);
1005 }
1006
1007 /* Return a pointer to the Nth token in the token stream.  If N is 1,
1008    then this is precisely equivalent to cp_lexer_peek_token (except
1009    that it is not inline).  One would like to disallow that case, but
1010    there is one case (cp_parser_nth_token_starts_template_id) where
1011    the caller passes a variable for N and it might be 1.  */
1012
1013 static cp_token *
1014 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1015 {
1016   cp_token *token;
1017
1018   /* N is 1-based, not zero-based.  */
1019   gcc_assert (n > 0);
1020
1021   if (cp_lexer_debugging_p (lexer))
1022     fprintf (cp_lexer_debug_stream,
1023              "cp_lexer: peeking ahead %ld at token: ", (long)n);
1024
1025   --n;
1026   token = lexer->next_token;
1027   gcc_assert (!n || token != &eof_token);
1028   while (n != 0)
1029     {
1030       ++token;
1031       if (token == lexer->last_token)
1032         {
1033           token = &eof_token;
1034           break;
1035         }
1036
1037       if (!token->purged_p)
1038         --n;
1039     }
1040
1041   if (cp_lexer_debugging_p (lexer))
1042     {
1043       cp_lexer_print_token (cp_lexer_debug_stream, token);
1044       putc ('\n', cp_lexer_debug_stream);
1045     }
1046
1047   return token;
1048 }
1049
1050 /* Return the next token, and advance the lexer's next_token pointer
1051    to point to the next non-purged token.  */
1052
1053 static cp_token *
1054 cp_lexer_consume_token (cp_lexer* lexer)
1055 {
1056   cp_token *token = lexer->next_token;
1057
1058   gcc_assert (token != &eof_token);
1059   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1060
1061   do
1062     {
1063       lexer->next_token++;
1064       if (lexer->next_token == lexer->last_token)
1065         {
1066           lexer->next_token = &eof_token;
1067           break;
1068         }
1069
1070     }
1071   while (lexer->next_token->purged_p);
1072
1073   cp_lexer_set_source_position_from_token (token);
1074
1075   /* Provide debugging output.  */
1076   if (cp_lexer_debugging_p (lexer))
1077     {
1078       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1079       cp_lexer_print_token (cp_lexer_debug_stream, token);
1080       putc ('\n', cp_lexer_debug_stream);
1081     }
1082
1083   return token;
1084 }
1085
1086 /* Permanently remove the next token from the token stream, and
1087    advance the next_token pointer to refer to the next non-purged
1088    token.  */
1089
1090 static void
1091 cp_lexer_purge_token (cp_lexer *lexer)
1092 {
1093   cp_token *tok = lexer->next_token;
1094
1095   gcc_assert (tok != &eof_token);
1096   tok->purged_p = true;
1097   tok->location = UNKNOWN_LOCATION;
1098   tok->u.value = NULL_TREE;
1099   tok->keyword = RID_MAX;
1100
1101   do
1102     {
1103       tok++;
1104       if (tok == lexer->last_token)
1105         {
1106           tok = &eof_token;
1107           break;
1108         }
1109     }
1110   while (tok->purged_p);
1111   lexer->next_token = tok;
1112 }
1113
1114 /* Permanently remove all tokens after TOK, up to, but not
1115    including, the token that will be returned next by
1116    cp_lexer_peek_token.  */
1117
1118 static void
1119 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1120 {
1121   cp_token *peek = lexer->next_token;
1122
1123   if (peek == &eof_token)
1124     peek = lexer->last_token;
1125
1126   gcc_assert (tok < peek);
1127
1128   for ( tok += 1; tok != peek; tok += 1)
1129     {
1130       tok->purged_p = true;
1131       tok->location = UNKNOWN_LOCATION;
1132       tok->u.value = NULL_TREE;
1133       tok->keyword = RID_MAX;
1134     }
1135 }
1136
1137 /* Begin saving tokens.  All tokens consumed after this point will be
1138    preserved.  */
1139
1140 static void
1141 cp_lexer_save_tokens (cp_lexer* lexer)
1142 {
1143   /* Provide debugging output.  */
1144   if (cp_lexer_debugging_p (lexer))
1145     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1146
1147   lexer->saved_tokens.safe_push (lexer->next_token);
1148 }
1149
1150 /* Commit to the portion of the token stream most recently saved.  */
1151
1152 static void
1153 cp_lexer_commit_tokens (cp_lexer* lexer)
1154 {
1155   /* Provide debugging output.  */
1156   if (cp_lexer_debugging_p (lexer))
1157     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1158
1159   lexer->saved_tokens.pop ();
1160 }
1161
1162 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1163    to the token stream.  Stop saving tokens.  */
1164
1165 static void
1166 cp_lexer_rollback_tokens (cp_lexer* lexer)
1167 {
1168   /* Provide debugging output.  */
1169   if (cp_lexer_debugging_p (lexer))
1170     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1171
1172   lexer->next_token = lexer->saved_tokens.pop ();
1173 }
1174
1175 /* RAII wrapper around the above functions, with sanity checking.  Creating
1176    a variable saves tokens, which are committed when the variable is
1177    destroyed unless they are explicitly rolled back by calling the rollback
1178    member function.  */
1179
1180 struct saved_token_sentinel
1181 {
1182   cp_lexer *lexer;
1183   unsigned len;
1184   bool commit;
1185   saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1186   {
1187     len = lexer->saved_tokens.length ();
1188     cp_lexer_save_tokens (lexer);
1189   }
1190   void rollback ()
1191   {
1192     cp_lexer_rollback_tokens (lexer);
1193     commit = false;
1194   }
1195   ~saved_token_sentinel()
1196   {
1197     if (commit)
1198       cp_lexer_commit_tokens (lexer);
1199     gcc_assert (lexer->saved_tokens.length () == len);
1200   }
1201 };
1202
1203 /* Print a representation of the TOKEN on the STREAM.  */
1204
1205 static void
1206 cp_lexer_print_token (FILE * stream, cp_token *token)
1207 {
1208   /* We don't use cpp_type2name here because the parser defines
1209      a few tokens of its own.  */
1210   static const char *const token_names[] = {
1211     /* cpplib-defined token types */
1212 #define OP(e, s) #e,
1213 #define TK(e, s) #e,
1214     TTYPE_TABLE
1215 #undef OP
1216 #undef TK
1217     /* C++ parser token types - see "Manifest constants", above.  */
1218     "KEYWORD",
1219     "TEMPLATE_ID",
1220     "NESTED_NAME_SPECIFIER",
1221   };
1222
1223   /* For some tokens, print the associated data.  */
1224   switch (token->type)
1225     {
1226     case CPP_KEYWORD:
1227       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1228          For example, `struct' is mapped to an INTEGER_CST.  */
1229       if (!identifier_p (token->u.value))
1230         break;
1231       /* else fall through */
1232     case CPP_NAME:
1233       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1234       break;
1235
1236     case CPP_STRING:
1237     case CPP_STRING16:
1238     case CPP_STRING32:
1239     case CPP_WSTRING:
1240     case CPP_UTF8STRING:
1241       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1242       break;
1243
1244     case CPP_NUMBER:
1245       print_generic_expr (stream, token->u.value, 0);
1246       break;
1247
1248     default:
1249       /* If we have a name for the token, print it out.  Otherwise, we
1250          simply give the numeric code.  */
1251       if (token->type < ARRAY_SIZE(token_names))
1252         fputs (token_names[token->type], stream);
1253       else
1254         fprintf (stream, "[%d]", token->type);
1255       break;
1256     }
1257 }
1258
1259 DEBUG_FUNCTION void
1260 debug (cp_token &ref)
1261 {
1262   cp_lexer_print_token (stderr, &ref);
1263   fprintf (stderr, "\n");
1264 }
1265
1266 DEBUG_FUNCTION void
1267 debug (cp_token *ptr)
1268 {
1269   if (ptr)
1270     debug (*ptr);
1271   else
1272     fprintf (stderr, "<nil>\n");
1273 }
1274
1275
1276 /* Start emitting debugging information.  */
1277
1278 static void
1279 cp_lexer_start_debugging (cp_lexer* lexer)
1280 {
1281   lexer->debugging_p = true;
1282   cp_lexer_debug_stream = stderr;
1283 }
1284
1285 /* Stop emitting debugging information.  */
1286
1287 static void
1288 cp_lexer_stop_debugging (cp_lexer* lexer)
1289 {
1290   lexer->debugging_p = false;
1291   cp_lexer_debug_stream = NULL;
1292 }
1293
1294 /* Create a new cp_token_cache, representing a range of tokens.  */
1295
1296 static cp_token_cache *
1297 cp_token_cache_new (cp_token *first, cp_token *last)
1298 {
1299   cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1300   cache->first = first;
1301   cache->last = last;
1302   return cache;
1303 }
1304
1305 /* Diagnose if #pragma omp declare simd isn't followed immediately
1306    by function declaration or definition.  */
1307
1308 static inline void
1309 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1310 {
1311   if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1312     {
1313       error ("%<#pragma omp declare simd%> not immediately followed by "
1314              "function declaration or definition");
1315       parser->omp_declare_simd = NULL;
1316     }
1317 }
1318
1319 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1320    and put that into "omp declare simd" attribute.  */
1321
1322 static inline void
1323 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1324 {
1325   if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1326     {
1327       if (fndecl == error_mark_node)
1328         {
1329           parser->omp_declare_simd = NULL;
1330           return;
1331         }
1332       if (TREE_CODE (fndecl) != FUNCTION_DECL)
1333         {
1334           cp_ensure_no_omp_declare_simd (parser);
1335           return;
1336         }
1337     }
1338 }
1339 \f
1340 /* Decl-specifiers.  */
1341
1342 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1343
1344 static void
1345 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1346 {
1347   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1348 }
1349
1350 /* Declarators.  */
1351
1352 /* Nothing other than the parser should be creating declarators;
1353    declarators are a semi-syntactic representation of C++ entities.
1354    Other parts of the front end that need to create entities (like
1355    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1356
1357 static cp_declarator *make_call_declarator
1358   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1359 static cp_declarator *make_array_declarator
1360   (cp_declarator *, tree);
1361 static cp_declarator *make_pointer_declarator
1362   (cp_cv_quals, cp_declarator *, tree);
1363 static cp_declarator *make_reference_declarator
1364   (cp_cv_quals, cp_declarator *, bool, tree);
1365 static cp_parameter_declarator *make_parameter_declarator
1366   (cp_decl_specifier_seq *, cp_declarator *, tree);
1367 static cp_declarator *make_ptrmem_declarator
1368   (cp_cv_quals, tree, cp_declarator *, tree);
1369
1370 /* An erroneous declarator.  */
1371 static cp_declarator *cp_error_declarator;
1372
1373 /* The obstack on which declarators and related data structures are
1374    allocated.  */
1375 static struct obstack declarator_obstack;
1376
1377 /* Alloc BYTES from the declarator memory pool.  */
1378
1379 static inline void *
1380 alloc_declarator (size_t bytes)
1381 {
1382   return obstack_alloc (&declarator_obstack, bytes);
1383 }
1384
1385 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1386    common to all declarators.  */
1387
1388 static cp_declarator *
1389 make_declarator (cp_declarator_kind kind)
1390 {
1391   cp_declarator *declarator;
1392
1393   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1394   declarator->kind = kind;
1395   declarator->attributes = NULL_TREE;
1396   declarator->std_attributes = NULL_TREE;
1397   declarator->declarator = NULL;
1398   declarator->parameter_pack_p = false;
1399   declarator->id_loc = UNKNOWN_LOCATION;
1400
1401   return declarator;
1402 }
1403
1404 /* Make a declarator for a generalized identifier.  If
1405    QUALIFYING_SCOPE is non-NULL, the identifier is
1406    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1407    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1408    is, if any.   */
1409
1410 static cp_declarator *
1411 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1412                     special_function_kind sfk)
1413 {
1414   cp_declarator *declarator;
1415
1416   /* It is valid to write:
1417
1418        class C { void f(); };
1419        typedef C D;
1420        void D::f();
1421
1422      The standard is not clear about whether `typedef const C D' is
1423      legal; as of 2002-09-15 the committee is considering that
1424      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1425      well.  */
1426   if (qualifying_scope && TYPE_P (qualifying_scope))
1427     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1428
1429   gcc_assert (identifier_p (unqualified_name)
1430               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1431               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1432
1433   declarator = make_declarator (cdk_id);
1434   declarator->u.id.qualifying_scope = qualifying_scope;
1435   declarator->u.id.unqualified_name = unqualified_name;
1436   declarator->u.id.sfk = sfk;
1437   
1438   return declarator;
1439 }
1440
1441 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1442    of modifiers such as const or volatile to apply to the pointer
1443    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1444    appertain to the pointer or reference.  */
1445
1446 cp_declarator *
1447 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1448                          tree attributes)
1449 {
1450   cp_declarator *declarator;
1451
1452   declarator = make_declarator (cdk_pointer);
1453   declarator->declarator = target;
1454   declarator->u.pointer.qualifiers = cv_qualifiers;
1455   declarator->u.pointer.class_type = NULL_TREE;
1456   if (target)
1457     {
1458       declarator->id_loc = target->id_loc;
1459       declarator->parameter_pack_p = target->parameter_pack_p;
1460       target->parameter_pack_p = false;
1461     }
1462   else
1463     declarator->parameter_pack_p = false;
1464
1465   declarator->std_attributes = attributes;
1466
1467   return declarator;
1468 }
1469
1470 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1471    represent the attributes that appertain to the pointer or
1472    reference.  */
1473
1474 cp_declarator *
1475 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1476                            bool rvalue_ref, tree attributes)
1477 {
1478   cp_declarator *declarator;
1479
1480   declarator = make_declarator (cdk_reference);
1481   declarator->declarator = target;
1482   declarator->u.reference.qualifiers = cv_qualifiers;
1483   declarator->u.reference.rvalue_ref = rvalue_ref;
1484   if (target)
1485     {
1486       declarator->id_loc = target->id_loc;
1487       declarator->parameter_pack_p = target->parameter_pack_p;
1488       target->parameter_pack_p = false;
1489     }
1490   else
1491     declarator->parameter_pack_p = false;
1492
1493   declarator->std_attributes = attributes;
1494
1495   return declarator;
1496 }
1497
1498 /* Like make_pointer_declarator -- but for a pointer to a non-static
1499    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1500    appertain to the pointer or reference.  */
1501
1502 cp_declarator *
1503 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1504                         cp_declarator *pointee,
1505                         tree attributes)
1506 {
1507   cp_declarator *declarator;
1508
1509   declarator = make_declarator (cdk_ptrmem);
1510   declarator->declarator = pointee;
1511   declarator->u.pointer.qualifiers = cv_qualifiers;
1512   declarator->u.pointer.class_type = class_type;
1513
1514   if (pointee)
1515     {
1516       declarator->parameter_pack_p = pointee->parameter_pack_p;
1517       pointee->parameter_pack_p = false;
1518     }
1519   else
1520     declarator->parameter_pack_p = false;
1521
1522   declarator->std_attributes = attributes;
1523
1524   return declarator;
1525 }
1526
1527 /* Make a declarator for the function given by TARGET, with the
1528    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1529    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1530    indicates what exceptions can be thrown.  */
1531
1532 cp_declarator *
1533 make_call_declarator (cp_declarator *target,
1534                       tree parms,
1535                       cp_cv_quals cv_qualifiers,
1536                       cp_virt_specifiers virt_specifiers,
1537                       cp_ref_qualifier ref_qualifier,
1538                       tree exception_specification,
1539                       tree late_return_type)
1540 {
1541   cp_declarator *declarator;
1542
1543   declarator = make_declarator (cdk_function);
1544   declarator->declarator = target;
1545   declarator->u.function.parameters = parms;
1546   declarator->u.function.qualifiers = cv_qualifiers;
1547   declarator->u.function.virt_specifiers = virt_specifiers;
1548   declarator->u.function.ref_qualifier = ref_qualifier;
1549   declarator->u.function.exception_specification = exception_specification;
1550   declarator->u.function.late_return_type = late_return_type;
1551   if (target)
1552     {
1553       declarator->id_loc = target->id_loc;
1554       declarator->parameter_pack_p = target->parameter_pack_p;
1555       target->parameter_pack_p = false;
1556     }
1557   else
1558     declarator->parameter_pack_p = false;
1559
1560   return declarator;
1561 }
1562
1563 /* Make a declarator for an array of BOUNDS elements, each of which is
1564    defined by ELEMENT.  */
1565
1566 cp_declarator *
1567 make_array_declarator (cp_declarator *element, tree bounds)
1568 {
1569   cp_declarator *declarator;
1570
1571   declarator = make_declarator (cdk_array);
1572   declarator->declarator = element;
1573   declarator->u.array.bounds = bounds;
1574   if (element)
1575     {
1576       declarator->id_loc = element->id_loc;
1577       declarator->parameter_pack_p = element->parameter_pack_p;
1578       element->parameter_pack_p = false;
1579     }
1580   else
1581     declarator->parameter_pack_p = false;
1582
1583   return declarator;
1584 }
1585
1586 /* Determine whether the declarator we've seen so far can be a
1587    parameter pack, when followed by an ellipsis.  */
1588 static bool 
1589 declarator_can_be_parameter_pack (cp_declarator *declarator)
1590 {
1591   /* Search for a declarator name, or any other declarator that goes
1592      after the point where the ellipsis could appear in a parameter
1593      pack. If we find any of these, then this declarator can not be
1594      made into a parameter pack.  */
1595   bool found = false;
1596   while (declarator && !found)
1597     {
1598       switch ((int)declarator->kind)
1599         {
1600         case cdk_id:
1601         case cdk_array:
1602           found = true;
1603           break;
1604
1605         case cdk_error:
1606           return true;
1607
1608         default:
1609           declarator = declarator->declarator;
1610           break;
1611         }
1612     }
1613
1614   return !found;
1615 }
1616
1617 cp_parameter_declarator *no_parameters;
1618
1619 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1620    DECLARATOR and DEFAULT_ARGUMENT.  */
1621
1622 cp_parameter_declarator *
1623 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1624                            cp_declarator *declarator,
1625                            tree default_argument)
1626 {
1627   cp_parameter_declarator *parameter;
1628
1629   parameter = ((cp_parameter_declarator *)
1630                alloc_declarator (sizeof (cp_parameter_declarator)));
1631   parameter->next = NULL;
1632   if (decl_specifiers)
1633     parameter->decl_specifiers = *decl_specifiers;
1634   else
1635     clear_decl_specs (&parameter->decl_specifiers);
1636   parameter->declarator = declarator;
1637   parameter->default_argument = default_argument;
1638   parameter->ellipsis_p = false;
1639
1640   return parameter;
1641 }
1642
1643 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1644
1645 static bool
1646 function_declarator_p (const cp_declarator *declarator)
1647 {
1648   while (declarator)
1649     {
1650       if (declarator->kind == cdk_function
1651           && declarator->declarator->kind == cdk_id)
1652         return true;
1653       if (declarator->kind == cdk_id
1654           || declarator->kind == cdk_error)
1655         return false;
1656       declarator = declarator->declarator;
1657     }
1658   return false;
1659 }
1660  
1661 /* The parser.  */
1662
1663 /* Overview
1664    --------
1665
1666    A cp_parser parses the token stream as specified by the C++
1667    grammar.  Its job is purely parsing, not semantic analysis.  For
1668    example, the parser breaks the token stream into declarators,
1669    expressions, statements, and other similar syntactic constructs.
1670    It does not check that the types of the expressions on either side
1671    of an assignment-statement are compatible, or that a function is
1672    not declared with a parameter of type `void'.
1673
1674    The parser invokes routines elsewhere in the compiler to perform
1675    semantic analysis and to build up the abstract syntax tree for the
1676    code processed.
1677
1678    The parser (and the template instantiation code, which is, in a
1679    way, a close relative of parsing) are the only parts of the
1680    compiler that should be calling push_scope and pop_scope, or
1681    related functions.  The parser (and template instantiation code)
1682    keeps track of what scope is presently active; everything else
1683    should simply honor that.  (The code that generates static
1684    initializers may also need to set the scope, in order to check
1685    access control correctly when emitting the initializers.)
1686
1687    Methodology
1688    -----------
1689
1690    The parser is of the standard recursive-descent variety.  Upcoming
1691    tokens in the token stream are examined in order to determine which
1692    production to use when parsing a non-terminal.  Some C++ constructs
1693    require arbitrary look ahead to disambiguate.  For example, it is
1694    impossible, in the general case, to tell whether a statement is an
1695    expression or declaration without scanning the entire statement.
1696    Therefore, the parser is capable of "parsing tentatively."  When the
1697    parser is not sure what construct comes next, it enters this mode.
1698    Then, while we attempt to parse the construct, the parser queues up
1699    error messages, rather than issuing them immediately, and saves the
1700    tokens it consumes.  If the construct is parsed successfully, the
1701    parser "commits", i.e., it issues any queued error messages and
1702    the tokens that were being preserved are permanently discarded.
1703    If, however, the construct is not parsed successfully, the parser
1704    rolls back its state completely so that it can resume parsing using
1705    a different alternative.
1706
1707    Future Improvements
1708    -------------------
1709
1710    The performance of the parser could probably be improved substantially.
1711    We could often eliminate the need to parse tentatively by looking ahead
1712    a little bit.  In some places, this approach might not entirely eliminate
1713    the need to parse tentatively, but it might still speed up the average
1714    case.  */
1715
1716 /* Flags that are passed to some parsing functions.  These values can
1717    be bitwise-ored together.  */
1718
1719 enum
1720 {
1721   /* No flags.  */
1722   CP_PARSER_FLAGS_NONE = 0x0,
1723   /* The construct is optional.  If it is not present, then no error
1724      should be issued.  */
1725   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1726   /* When parsing a type-specifier, treat user-defined type-names
1727      as non-type identifiers.  */
1728   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1729   /* When parsing a type-specifier, do not try to parse a class-specifier
1730      or enum-specifier.  */
1731   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1732   /* When parsing a decl-specifier-seq, only allow type-specifier or
1733      constexpr.  */
1734   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1735 };
1736
1737 /* This type is used for parameters and variables which hold
1738    combinations of the above flags.  */
1739 typedef int cp_parser_flags;
1740
1741 /* The different kinds of declarators we want to parse.  */
1742
1743 typedef enum cp_parser_declarator_kind
1744 {
1745   /* We want an abstract declarator.  */
1746   CP_PARSER_DECLARATOR_ABSTRACT,
1747   /* We want a named declarator.  */
1748   CP_PARSER_DECLARATOR_NAMED,
1749   /* We don't mind, but the name must be an unqualified-id.  */
1750   CP_PARSER_DECLARATOR_EITHER
1751 } cp_parser_declarator_kind;
1752
1753 /* The precedence values used to parse binary expressions.  The minimum value
1754    of PREC must be 1, because zero is reserved to quickly discriminate
1755    binary operators from other tokens.  */
1756
1757 enum cp_parser_prec
1758 {
1759   PREC_NOT_OPERATOR,
1760   PREC_LOGICAL_OR_EXPRESSION,
1761   PREC_LOGICAL_AND_EXPRESSION,
1762   PREC_INCLUSIVE_OR_EXPRESSION,
1763   PREC_EXCLUSIVE_OR_EXPRESSION,
1764   PREC_AND_EXPRESSION,
1765   PREC_EQUALITY_EXPRESSION,
1766   PREC_RELATIONAL_EXPRESSION,
1767   PREC_SHIFT_EXPRESSION,
1768   PREC_ADDITIVE_EXPRESSION,
1769   PREC_MULTIPLICATIVE_EXPRESSION,
1770   PREC_PM_EXPRESSION,
1771   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1772 };
1773
1774 /* A mapping from a token type to a corresponding tree node type, with a
1775    precedence value.  */
1776
1777 typedef struct cp_parser_binary_operations_map_node
1778 {
1779   /* The token type.  */
1780   enum cpp_ttype token_type;
1781   /* The corresponding tree code.  */
1782   enum tree_code tree_type;
1783   /* The precedence of this operator.  */
1784   enum cp_parser_prec prec;
1785 } cp_parser_binary_operations_map_node;
1786
1787 typedef struct cp_parser_expression_stack_entry
1788 {
1789   /* Left hand side of the binary operation we are currently
1790      parsing.  */
1791   tree lhs;
1792   /* Original tree code for left hand side, if it was a binary
1793      expression itself (used for -Wparentheses).  */
1794   enum tree_code lhs_type;
1795   /* Tree code for the binary operation we are parsing.  */
1796   enum tree_code tree_type;
1797   /* Precedence of the binary operation we are parsing.  */
1798   enum cp_parser_prec prec;
1799   /* Location of the binary operation we are parsing.  */
1800   location_t loc;
1801 } cp_parser_expression_stack_entry;
1802
1803 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1804    entries because precedence levels on the stack are monotonically
1805    increasing.  */
1806 typedef struct cp_parser_expression_stack_entry
1807   cp_parser_expression_stack[NUM_PREC_VALUES];
1808
1809 /* Prototypes.  */
1810
1811 /* Constructors and destructors.  */
1812
1813 static cp_parser_context *cp_parser_context_new
1814   (cp_parser_context *);
1815
1816 /* Class variables.  */
1817
1818 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1819
1820 /* The operator-precedence table used by cp_parser_binary_expression.
1821    Transformed into an associative array (binops_by_token) by
1822    cp_parser_new.  */
1823
1824 static const cp_parser_binary_operations_map_node binops[] = {
1825   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1826   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1827
1828   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1829   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1830   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1831
1832   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1833   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1834
1835   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1836   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1837
1838   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1839   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1840   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1841   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1842
1843   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1844   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1845
1846   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1847
1848   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1849
1850   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1851
1852   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1853
1854   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1855 };
1856
1857 /* The same as binops, but initialized by cp_parser_new so that
1858    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1859    for speed.  */
1860 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1861
1862 /* Constructors and destructors.  */
1863
1864 /* Construct a new context.  The context below this one on the stack
1865    is given by NEXT.  */
1866
1867 static cp_parser_context *
1868 cp_parser_context_new (cp_parser_context* next)
1869 {
1870   cp_parser_context *context;
1871
1872   /* Allocate the storage.  */
1873   if (cp_parser_context_free_list != NULL)
1874     {
1875       /* Pull the first entry from the free list.  */
1876       context = cp_parser_context_free_list;
1877       cp_parser_context_free_list = context->next;
1878       memset (context, 0, sizeof (*context));
1879     }
1880   else
1881     context = ggc_cleared_alloc<cp_parser_context> ();
1882
1883   /* No errors have occurred yet in this context.  */
1884   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1885   /* If this is not the bottommost context, copy information that we
1886      need from the previous context.  */
1887   if (next)
1888     {
1889       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1890          expression, then we are parsing one in this context, too.  */
1891       context->object_type = next->object_type;
1892       /* Thread the stack.  */
1893       context->next = next;
1894     }
1895
1896   return context;
1897 }
1898
1899 /* Managing the unparsed function queues.  */
1900
1901 #define unparsed_funs_with_default_args \
1902   parser->unparsed_queues->last ().funs_with_default_args
1903 #define unparsed_funs_with_definitions \
1904   parser->unparsed_queues->last ().funs_with_definitions
1905 #define unparsed_nsdmis \
1906   parser->unparsed_queues->last ().nsdmis
1907 #define unparsed_classes \
1908   parser->unparsed_queues->last ().classes
1909
1910 static void
1911 push_unparsed_function_queues (cp_parser *parser)
1912 {
1913   cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1914   vec_safe_push (parser->unparsed_queues, e);
1915 }
1916
1917 static void
1918 pop_unparsed_function_queues (cp_parser *parser)
1919 {
1920   release_tree_vector (unparsed_funs_with_definitions);
1921   parser->unparsed_queues->pop ();
1922 }
1923
1924 /* Prototypes.  */
1925
1926 /* Constructors and destructors.  */
1927
1928 static cp_parser *cp_parser_new
1929   (void);
1930
1931 /* Routines to parse various constructs.
1932
1933    Those that return `tree' will return the error_mark_node (rather
1934    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1935    Sometimes, they will return an ordinary node if error-recovery was
1936    attempted, even though a parse error occurred.  So, to check
1937    whether or not a parse error occurred, you should always use
1938    cp_parser_error_occurred.  If the construct is optional (indicated
1939    either by an `_opt' in the name of the function that does the
1940    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1941    the construct is not present.  */
1942
1943 /* Lexical conventions [gram.lex]  */
1944
1945 static tree cp_parser_identifier
1946   (cp_parser *);
1947 static tree cp_parser_string_literal
1948   (cp_parser *, bool, bool, bool);
1949 static tree cp_parser_userdef_char_literal
1950   (cp_parser *);
1951 static tree cp_parser_userdef_string_literal
1952   (tree);
1953 static tree cp_parser_userdef_numeric_literal
1954   (cp_parser *);
1955
1956 /* Basic concepts [gram.basic]  */
1957
1958 static bool cp_parser_translation_unit
1959   (cp_parser *);
1960
1961 /* Expressions [gram.expr]  */
1962
1963 static tree cp_parser_primary_expression
1964   (cp_parser *, bool, bool, bool, cp_id_kind *);
1965 static tree cp_parser_id_expression
1966   (cp_parser *, bool, bool, bool *, bool, bool);
1967 static tree cp_parser_unqualified_id
1968   (cp_parser *, bool, bool, bool, bool);
1969 static tree cp_parser_nested_name_specifier_opt
1970   (cp_parser *, bool, bool, bool, bool);
1971 static tree cp_parser_nested_name_specifier
1972   (cp_parser *, bool, bool, bool, bool);
1973 static tree cp_parser_qualifying_entity
1974   (cp_parser *, bool, bool, bool, bool, bool);
1975 static tree cp_parser_postfix_expression
1976   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1977 static tree cp_parser_postfix_open_square_expression
1978   (cp_parser *, tree, bool, bool);
1979 static tree cp_parser_postfix_dot_deref_expression
1980   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1981 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1982   (cp_parser *, int, bool, bool, bool *, bool = false);
1983 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1984 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1985 static void cp_parser_pseudo_destructor_name
1986   (cp_parser *, tree, tree *, tree *);
1987 static tree cp_parser_unary_expression
1988   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1989 static enum tree_code cp_parser_unary_operator
1990   (cp_token *);
1991 static tree cp_parser_new_expression
1992   (cp_parser *);
1993 static vec<tree, va_gc> *cp_parser_new_placement
1994   (cp_parser *);
1995 static tree cp_parser_new_type_id
1996   (cp_parser *, tree *);
1997 static cp_declarator *cp_parser_new_declarator_opt
1998   (cp_parser *);
1999 static cp_declarator *cp_parser_direct_new_declarator
2000   (cp_parser *);
2001 static vec<tree, va_gc> *cp_parser_new_initializer
2002   (cp_parser *);
2003 static tree cp_parser_delete_expression
2004   (cp_parser *);
2005 static tree cp_parser_cast_expression
2006   (cp_parser *, bool, bool, bool, cp_id_kind *);
2007 static tree cp_parser_binary_expression
2008   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2009 static tree cp_parser_question_colon_clause
2010   (cp_parser *, tree);
2011 static tree cp_parser_assignment_expression
2012   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2013 static enum tree_code cp_parser_assignment_operator_opt
2014   (cp_parser *);
2015 static tree cp_parser_expression
2016   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2017 static tree cp_parser_constant_expression
2018   (cp_parser *, bool = false, bool * = NULL);
2019 static tree cp_parser_builtin_offsetof
2020   (cp_parser *);
2021 static tree cp_parser_lambda_expression
2022   (cp_parser *);
2023 static void cp_parser_lambda_introducer
2024   (cp_parser *, tree);
2025 static bool cp_parser_lambda_declarator_opt
2026   (cp_parser *, tree);
2027 static void cp_parser_lambda_body
2028   (cp_parser *, tree);
2029
2030 /* Statements [gram.stmt.stmt]  */
2031
2032 static void cp_parser_statement
2033   (cp_parser *, tree, bool, bool *);
2034 static void cp_parser_label_for_labeled_statement
2035 (cp_parser *, tree);
2036 static tree cp_parser_expression_statement
2037   (cp_parser *, tree);
2038 static tree cp_parser_compound_statement
2039   (cp_parser *, tree, bool, bool);
2040 static void cp_parser_statement_seq_opt
2041   (cp_parser *, tree);
2042 static tree cp_parser_selection_statement
2043   (cp_parser *, bool *);
2044 static tree cp_parser_condition
2045   (cp_parser *);
2046 static tree cp_parser_iteration_statement
2047   (cp_parser *, bool);
2048 static bool cp_parser_for_init_statement
2049   (cp_parser *, tree *decl);
2050 static tree cp_parser_for
2051   (cp_parser *, bool);
2052 static tree cp_parser_c_for
2053   (cp_parser *, tree, tree, bool);
2054 static tree cp_parser_range_for
2055   (cp_parser *, tree, tree, tree, bool);
2056 static void do_range_for_auto_deduction
2057   (tree, tree);
2058 static tree cp_parser_perform_range_for_lookup
2059   (tree, tree *, tree *);
2060 static tree cp_parser_range_for_member_function
2061   (tree, tree);
2062 static tree cp_parser_jump_statement
2063   (cp_parser *);
2064 static void cp_parser_declaration_statement
2065   (cp_parser *);
2066
2067 static tree cp_parser_implicitly_scoped_statement
2068   (cp_parser *, bool *);
2069 static void cp_parser_already_scoped_statement
2070   (cp_parser *);
2071
2072 /* Declarations [gram.dcl.dcl] */
2073
2074 static void cp_parser_declaration_seq_opt
2075   (cp_parser *);
2076 static void cp_parser_declaration
2077   (cp_parser *);
2078 static void cp_parser_block_declaration
2079   (cp_parser *, bool);
2080 static void cp_parser_simple_declaration
2081   (cp_parser *, bool, tree *);
2082 static void cp_parser_decl_specifier_seq
2083   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2084 static tree cp_parser_storage_class_specifier_opt
2085   (cp_parser *);
2086 static tree cp_parser_function_specifier_opt
2087   (cp_parser *, cp_decl_specifier_seq *);
2088 static tree cp_parser_type_specifier
2089   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2090    int *, bool *);
2091 static tree cp_parser_simple_type_specifier
2092   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2093 static tree cp_parser_type_name
2094   (cp_parser *);
2095 static tree cp_parser_nonclass_name 
2096   (cp_parser* parser);
2097 static tree cp_parser_elaborated_type_specifier
2098   (cp_parser *, bool, bool);
2099 static tree cp_parser_enum_specifier
2100   (cp_parser *);
2101 static void cp_parser_enumerator_list
2102   (cp_parser *, tree);
2103 static void cp_parser_enumerator_definition
2104   (cp_parser *, tree);
2105 static tree cp_parser_namespace_name
2106   (cp_parser *);
2107 static void cp_parser_namespace_definition
2108   (cp_parser *);
2109 static void cp_parser_namespace_body
2110   (cp_parser *);
2111 static tree cp_parser_qualified_namespace_specifier
2112   (cp_parser *);
2113 static void cp_parser_namespace_alias_definition
2114   (cp_parser *);
2115 static bool cp_parser_using_declaration
2116   (cp_parser *, bool);
2117 static void cp_parser_using_directive
2118   (cp_parser *);
2119 static tree cp_parser_alias_declaration
2120   (cp_parser *);
2121 static void cp_parser_asm_definition
2122   (cp_parser *);
2123 static void cp_parser_linkage_specification
2124   (cp_parser *);
2125 static void cp_parser_static_assert
2126   (cp_parser *, bool);
2127 static tree cp_parser_decltype
2128   (cp_parser *);
2129
2130 /* Declarators [gram.dcl.decl] */
2131
2132 static tree cp_parser_init_declarator
2133   (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2134    bool, bool, int, bool *, tree *, location_t *);
2135 static cp_declarator *cp_parser_declarator
2136   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2137 static cp_declarator *cp_parser_direct_declarator
2138   (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2139 static enum tree_code cp_parser_ptr_operator
2140   (cp_parser *, tree *, cp_cv_quals *, tree *);
2141 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2142   (cp_parser *);
2143 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2144   (cp_parser *);
2145 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2146   (cp_parser *);
2147 static tree cp_parser_late_return_type_opt
2148   (cp_parser *, cp_declarator *, cp_cv_quals);
2149 static tree cp_parser_declarator_id
2150   (cp_parser *, bool);
2151 static tree cp_parser_type_id
2152   (cp_parser *);
2153 static tree cp_parser_template_type_arg
2154   (cp_parser *);
2155 static tree cp_parser_trailing_type_id (cp_parser *);
2156 static tree cp_parser_type_id_1
2157   (cp_parser *, bool, bool);
2158 static void cp_parser_type_specifier_seq
2159   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2160 static tree cp_parser_parameter_declaration_clause
2161   (cp_parser *);
2162 static tree cp_parser_parameter_declaration_list
2163   (cp_parser *, bool *);
2164 static cp_parameter_declarator *cp_parser_parameter_declaration
2165   (cp_parser *, bool, bool *);
2166 static tree cp_parser_default_argument 
2167   (cp_parser *, bool);
2168 static void cp_parser_function_body
2169   (cp_parser *, bool);
2170 static tree cp_parser_initializer
2171   (cp_parser *, bool *, bool *);
2172 static tree cp_parser_initializer_clause
2173   (cp_parser *, bool *);
2174 static tree cp_parser_braced_list
2175   (cp_parser*, bool*);
2176 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2177   (cp_parser *, bool *);
2178
2179 static bool cp_parser_ctor_initializer_opt_and_function_body
2180   (cp_parser *, bool);
2181
2182 static tree cp_parser_late_parsing_omp_declare_simd
2183   (cp_parser *, tree);
2184
2185 static tree cp_parser_late_parsing_cilk_simd_fn_info
2186   (cp_parser *, tree);
2187
2188 static tree synthesize_implicit_template_parm
2189   (cp_parser *);
2190 static tree finish_fully_implicit_template
2191   (cp_parser *, tree);
2192
2193 /* Classes [gram.class] */
2194
2195 static tree cp_parser_class_name
2196   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2197 static tree cp_parser_class_specifier
2198   (cp_parser *);
2199 static tree cp_parser_class_head
2200   (cp_parser *, bool *);
2201 static enum tag_types cp_parser_class_key
2202   (cp_parser *);
2203 static void cp_parser_type_parameter_key
2204   (cp_parser* parser);
2205 static void cp_parser_member_specification_opt
2206   (cp_parser *);
2207 static void cp_parser_member_declaration
2208   (cp_parser *);
2209 static tree cp_parser_pure_specifier
2210   (cp_parser *);
2211 static tree cp_parser_constant_initializer
2212   (cp_parser *);
2213
2214 /* Derived classes [gram.class.derived] */
2215
2216 static tree cp_parser_base_clause
2217   (cp_parser *);
2218 static tree cp_parser_base_specifier
2219   (cp_parser *);
2220
2221 /* Special member functions [gram.special] */
2222
2223 static tree cp_parser_conversion_function_id
2224   (cp_parser *);
2225 static tree cp_parser_conversion_type_id
2226   (cp_parser *);
2227 static cp_declarator *cp_parser_conversion_declarator_opt
2228   (cp_parser *);
2229 static bool cp_parser_ctor_initializer_opt
2230   (cp_parser *);
2231 static void cp_parser_mem_initializer_list
2232   (cp_parser *);
2233 static tree cp_parser_mem_initializer
2234   (cp_parser *);
2235 static tree cp_parser_mem_initializer_id
2236   (cp_parser *);
2237
2238 /* Overloading [gram.over] */
2239
2240 static tree cp_parser_operator_function_id
2241   (cp_parser *);
2242 static tree cp_parser_operator
2243   (cp_parser *);
2244
2245 /* Templates [gram.temp] */
2246
2247 static void cp_parser_template_declaration
2248   (cp_parser *, bool);
2249 static tree cp_parser_template_parameter_list
2250   (cp_parser *);
2251 static tree cp_parser_template_parameter
2252   (cp_parser *, bool *, bool *);
2253 static tree cp_parser_type_parameter
2254   (cp_parser *, bool *);
2255 static tree cp_parser_template_id
2256   (cp_parser *, bool, bool, enum tag_types, bool);
2257 static tree cp_parser_template_name
2258   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2259 static tree cp_parser_template_argument_list
2260   (cp_parser *);
2261 static tree cp_parser_template_argument
2262   (cp_parser *);
2263 static void cp_parser_explicit_instantiation
2264   (cp_parser *);
2265 static void cp_parser_explicit_specialization
2266   (cp_parser *);
2267
2268 /* Exception handling [gram.exception] */
2269
2270 static tree cp_parser_try_block
2271   (cp_parser *);
2272 static bool cp_parser_function_try_block
2273   (cp_parser *);
2274 static void cp_parser_handler_seq
2275   (cp_parser *);
2276 static void cp_parser_handler
2277   (cp_parser *);
2278 static tree cp_parser_exception_declaration
2279   (cp_parser *);
2280 static tree cp_parser_throw_expression
2281   (cp_parser *);
2282 static tree cp_parser_exception_specification_opt
2283   (cp_parser *);
2284 static tree cp_parser_type_id_list
2285   (cp_parser *);
2286
2287 /* GNU Extensions */
2288
2289 static tree cp_parser_asm_specification_opt
2290   (cp_parser *);
2291 static tree cp_parser_asm_operand_list
2292   (cp_parser *);
2293 static tree cp_parser_asm_clobber_list
2294   (cp_parser *);
2295 static tree cp_parser_asm_label_list
2296   (cp_parser *);
2297 static bool cp_next_tokens_can_be_attribute_p
2298   (cp_parser *);
2299 static bool cp_next_tokens_can_be_gnu_attribute_p
2300   (cp_parser *);
2301 static bool cp_next_tokens_can_be_std_attribute_p
2302   (cp_parser *);
2303 static bool cp_nth_tokens_can_be_std_attribute_p
2304   (cp_parser *, size_t);
2305 static bool cp_nth_tokens_can_be_gnu_attribute_p
2306   (cp_parser *, size_t);
2307 static bool cp_nth_tokens_can_be_attribute_p
2308   (cp_parser *, size_t);
2309 static tree cp_parser_attributes_opt
2310   (cp_parser *);
2311 static tree cp_parser_gnu_attributes_opt
2312   (cp_parser *);
2313 static tree cp_parser_gnu_attribute_list
2314   (cp_parser *);
2315 static tree cp_parser_std_attribute
2316   (cp_parser *);
2317 static tree cp_parser_std_attribute_spec
2318   (cp_parser *);
2319 static tree cp_parser_std_attribute_spec_seq
2320   (cp_parser *);
2321 static bool cp_parser_extension_opt
2322   (cp_parser *, int *);
2323 static void cp_parser_label_declaration
2324   (cp_parser *);
2325
2326 /* Transactional Memory Extensions */
2327
2328 static tree cp_parser_transaction
2329   (cp_parser *, enum rid);
2330 static tree cp_parser_transaction_expression
2331   (cp_parser *, enum rid);
2332 static bool cp_parser_function_transaction
2333   (cp_parser *, enum rid);
2334 static tree cp_parser_transaction_cancel
2335   (cp_parser *);
2336
2337 enum pragma_context {
2338   pragma_external,
2339   pragma_member,
2340   pragma_objc_icode,
2341   pragma_stmt,
2342   pragma_compound
2343 };
2344 static bool cp_parser_pragma
2345   (cp_parser *, enum pragma_context);
2346
2347 /* Objective-C++ Productions */
2348
2349 static tree cp_parser_objc_message_receiver
2350   (cp_parser *);
2351 static tree cp_parser_objc_message_args
2352   (cp_parser *);
2353 static tree cp_parser_objc_message_expression
2354   (cp_parser *);
2355 static tree cp_parser_objc_encode_expression
2356   (cp_parser *);
2357 static tree cp_parser_objc_defs_expression
2358   (cp_parser *);
2359 static tree cp_parser_objc_protocol_expression
2360   (cp_parser *);
2361 static tree cp_parser_objc_selector_expression
2362   (cp_parser *);
2363 static tree cp_parser_objc_expression
2364   (cp_parser *);
2365 static bool cp_parser_objc_selector_p
2366   (enum cpp_ttype);
2367 static tree cp_parser_objc_selector
2368   (cp_parser *);
2369 static tree cp_parser_objc_protocol_refs_opt
2370   (cp_parser *);
2371 static void cp_parser_objc_declaration
2372   (cp_parser *, tree);
2373 static tree cp_parser_objc_statement
2374   (cp_parser *);
2375 static bool cp_parser_objc_valid_prefix_attributes
2376   (cp_parser *, tree *);
2377 static void cp_parser_objc_at_property_declaration 
2378   (cp_parser *) ;
2379 static void cp_parser_objc_at_synthesize_declaration 
2380   (cp_parser *) ;
2381 static void cp_parser_objc_at_dynamic_declaration
2382   (cp_parser *) ;
2383 static tree cp_parser_objc_struct_declaration
2384   (cp_parser *) ;
2385
2386 /* Utility Routines */
2387
2388 static tree cp_parser_lookup_name
2389   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2390 static tree cp_parser_lookup_name_simple
2391   (cp_parser *, tree, location_t);
2392 static tree cp_parser_maybe_treat_template_as_class
2393   (tree, bool);
2394 static bool cp_parser_check_declarator_template_parameters
2395   (cp_parser *, cp_declarator *, location_t);
2396 static bool cp_parser_check_template_parameters
2397   (cp_parser *, unsigned, location_t, cp_declarator *);
2398 static tree cp_parser_simple_cast_expression
2399   (cp_parser *);
2400 static tree cp_parser_global_scope_opt
2401   (cp_parser *, bool);
2402 static bool cp_parser_constructor_declarator_p
2403   (cp_parser *, bool);
2404 static tree cp_parser_function_definition_from_specifiers_and_declarator
2405   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2406 static tree cp_parser_function_definition_after_declarator
2407   (cp_parser *, bool);
2408 static void cp_parser_template_declaration_after_export
2409   (cp_parser *, bool);
2410 static void cp_parser_perform_template_parameter_access_checks
2411   (vec<deferred_access_check, va_gc> *);
2412 static tree cp_parser_single_declaration
2413   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2414 static tree cp_parser_functional_cast
2415   (cp_parser *, tree);
2416 static tree cp_parser_save_member_function_body
2417   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2418 static tree cp_parser_save_nsdmi
2419   (cp_parser *);
2420 static tree cp_parser_enclosed_template_argument_list
2421   (cp_parser *);
2422 static void cp_parser_save_default_args
2423   (cp_parser *, tree);
2424 static void cp_parser_late_parsing_for_member
2425   (cp_parser *, tree);
2426 static tree cp_parser_late_parse_one_default_arg
2427   (cp_parser *, tree, tree, tree);
2428 static void cp_parser_late_parsing_nsdmi
2429   (cp_parser *, tree);
2430 static void cp_parser_late_parsing_default_args
2431   (cp_parser *, tree);
2432 static tree cp_parser_sizeof_operand
2433   (cp_parser *, enum rid);
2434 static tree cp_parser_trait_expr
2435   (cp_parser *, enum rid);
2436 static bool cp_parser_declares_only_class_p
2437   (cp_parser *);
2438 static void cp_parser_set_storage_class
2439   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2440 static void cp_parser_set_decl_spec_type
2441   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2442 static void set_and_check_decl_spec_loc
2443   (cp_decl_specifier_seq *decl_specs,
2444    cp_decl_spec ds, cp_token *);
2445 static bool cp_parser_friend_p
2446   (const cp_decl_specifier_seq *);
2447 static void cp_parser_required_error
2448   (cp_parser *, required_token, bool);
2449 static cp_token *cp_parser_require
2450   (cp_parser *, enum cpp_ttype, required_token);
2451 static cp_token *cp_parser_require_keyword
2452   (cp_parser *, enum rid, required_token);
2453 static bool cp_parser_token_starts_function_definition_p
2454   (cp_token *);
2455 static bool cp_parser_next_token_starts_class_definition_p
2456   (cp_parser *);
2457 static bool cp_parser_next_token_ends_template_argument_p
2458   (cp_parser *);
2459 static bool cp_parser_nth_token_starts_template_argument_list_p
2460   (cp_parser *, size_t);
2461 static enum tag_types cp_parser_token_is_class_key
2462   (cp_token *);
2463 static enum tag_types cp_parser_token_is_type_parameter_key
2464   (cp_token *);
2465 static void cp_parser_check_class_key
2466   (enum tag_types, tree type);
2467 static void cp_parser_check_access_in_redeclaration
2468   (tree type, location_t location);
2469 static bool cp_parser_optional_template_keyword
2470   (cp_parser *);
2471 static void cp_parser_pre_parsed_nested_name_specifier
2472   (cp_parser *);
2473 static bool cp_parser_cache_group
2474   (cp_parser *, enum cpp_ttype, unsigned);
2475 static tree cp_parser_cache_defarg
2476   (cp_parser *parser, bool nsdmi);
2477 static void cp_parser_parse_tentatively
2478   (cp_parser *);
2479 static void cp_parser_commit_to_tentative_parse
2480   (cp_parser *);
2481 static void cp_parser_commit_to_topmost_tentative_parse
2482   (cp_parser *);
2483 static void cp_parser_abort_tentative_parse
2484   (cp_parser *);
2485 static bool cp_parser_parse_definitely
2486   (cp_parser *);
2487 static inline bool cp_parser_parsing_tentatively
2488   (cp_parser *);
2489 static bool cp_parser_uncommitted_to_tentative_parse_p
2490   (cp_parser *);
2491 static void cp_parser_error
2492   (cp_parser *, const char *);
2493 static void cp_parser_name_lookup_error
2494   (cp_parser *, tree, tree, name_lookup_error, location_t);
2495 static bool cp_parser_simulate_error
2496   (cp_parser *);
2497 static bool cp_parser_check_type_definition
2498   (cp_parser *);
2499 static void cp_parser_check_for_definition_in_return_type
2500   (cp_declarator *, tree, location_t type_location);
2501 static void cp_parser_check_for_invalid_template_id
2502   (cp_parser *, tree, enum tag_types, location_t location);
2503 static bool cp_parser_non_integral_constant_expression
2504   (cp_parser *, non_integral_constant);
2505 static void cp_parser_diagnose_invalid_type_name
2506   (cp_parser *, tree, location_t);
2507 static bool cp_parser_parse_and_diagnose_invalid_type_name
2508   (cp_parser *);
2509 static int cp_parser_skip_to_closing_parenthesis
2510   (cp_parser *, bool, bool, bool);
2511 static void cp_parser_skip_to_end_of_statement
2512   (cp_parser *);
2513 static void cp_parser_consume_semicolon_at_end_of_statement
2514   (cp_parser *);
2515 static void cp_parser_skip_to_end_of_block_or_statement
2516   (cp_parser *);
2517 static bool cp_parser_skip_to_closing_brace
2518   (cp_parser *);
2519 static void cp_parser_skip_to_end_of_template_parameter_list
2520   (cp_parser *);
2521 static void cp_parser_skip_to_pragma_eol
2522   (cp_parser*, cp_token *);
2523 static bool cp_parser_error_occurred
2524   (cp_parser *);
2525 static bool cp_parser_allow_gnu_extensions_p
2526   (cp_parser *);
2527 static bool cp_parser_is_pure_string_literal
2528   (cp_token *);
2529 static bool cp_parser_is_string_literal
2530   (cp_token *);
2531 static bool cp_parser_is_keyword
2532   (cp_token *, enum rid);
2533 static tree cp_parser_make_typename_type
2534   (cp_parser *, tree, location_t location);
2535 static cp_declarator * cp_parser_make_indirect_declarator
2536   (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2537 static bool cp_parser_compound_literal_p
2538   (cp_parser *);
2539 static bool cp_parser_array_designator_p
2540   (cp_parser *);
2541 static bool cp_parser_skip_to_closing_square_bracket
2542   (cp_parser *);
2543
2544 /* Returns nonzero if we are parsing tentatively.  */
2545
2546 static inline bool
2547 cp_parser_parsing_tentatively (cp_parser* parser)
2548 {
2549   return parser->context->next != NULL;
2550 }
2551
2552 /* Returns nonzero if TOKEN is a string literal.  */
2553
2554 static bool
2555 cp_parser_is_pure_string_literal (cp_token* token)
2556 {
2557   return (token->type == CPP_STRING ||
2558           token->type == CPP_STRING16 ||
2559           token->type == CPP_STRING32 ||
2560           token->type == CPP_WSTRING ||
2561           token->type == CPP_UTF8STRING);
2562 }
2563
2564 /* Returns nonzero if TOKEN is a string literal
2565    of a user-defined string literal.  */
2566
2567 static bool
2568 cp_parser_is_string_literal (cp_token* token)
2569 {
2570   return (cp_parser_is_pure_string_literal (token) ||
2571           token->type == CPP_STRING_USERDEF ||
2572           token->type == CPP_STRING16_USERDEF ||
2573           token->type == CPP_STRING32_USERDEF ||
2574           token->type == CPP_WSTRING_USERDEF ||
2575           token->type == CPP_UTF8STRING_USERDEF);
2576 }
2577
2578 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2579
2580 static bool
2581 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2582 {
2583   return token->keyword == keyword;
2584 }
2585
2586 /* If not parsing tentatively, issue a diagnostic of the form
2587       FILE:LINE: MESSAGE before TOKEN
2588    where TOKEN is the next token in the input stream.  MESSAGE
2589    (specified by the caller) is usually of the form "expected
2590    OTHER-TOKEN".  */
2591
2592 static void
2593 cp_parser_error (cp_parser* parser, const char* gmsgid)
2594 {
2595   if (!cp_parser_simulate_error (parser))
2596     {
2597       cp_token *token = cp_lexer_peek_token (parser->lexer);
2598       /* This diagnostic makes more sense if it is tagged to the line
2599          of the token we just peeked at.  */
2600       cp_lexer_set_source_position_from_token (token);
2601
2602       if (token->type == CPP_PRAGMA)
2603         {
2604           error_at (token->location,
2605                     "%<#pragma%> is not allowed here");
2606           cp_parser_skip_to_pragma_eol (parser, token);
2607           return;
2608         }
2609
2610       c_parse_error (gmsgid,
2611                      /* Because c_parser_error does not understand
2612                         CPP_KEYWORD, keywords are treated like
2613                         identifiers.  */
2614                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2615                      token->u.value, token->flags);
2616     }
2617 }
2618
2619 /* Issue an error about name-lookup failing.  NAME is the
2620    IDENTIFIER_NODE DECL is the result of
2621    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2622    the thing that we hoped to find.  */
2623
2624 static void
2625 cp_parser_name_lookup_error (cp_parser* parser,
2626                              tree name,
2627                              tree decl,
2628                              name_lookup_error desired,
2629                              location_t location)
2630 {
2631   /* If name lookup completely failed, tell the user that NAME was not
2632      declared.  */
2633   if (decl == error_mark_node)
2634     {
2635       if (parser->scope && parser->scope != global_namespace)
2636         error_at (location, "%<%E::%E%> has not been declared",
2637                   parser->scope, name);
2638       else if (parser->scope == global_namespace)
2639         error_at (location, "%<::%E%> has not been declared", name);
2640       else if (parser->object_scope
2641                && !CLASS_TYPE_P (parser->object_scope))
2642         error_at (location, "request for member %qE in non-class type %qT",
2643                   name, parser->object_scope);
2644       else if (parser->object_scope)
2645         error_at (location, "%<%T::%E%> has not been declared",
2646                   parser->object_scope, name);
2647       else
2648         error_at (location, "%qE has not been declared", name);
2649     }
2650   else if (parser->scope && parser->scope != global_namespace)
2651     {
2652       switch (desired)
2653         {
2654           case NLE_TYPE:
2655             error_at (location, "%<%E::%E%> is not a type",
2656                                 parser->scope, name);
2657             break;
2658           case NLE_CXX98:
2659             error_at (location, "%<%E::%E%> is not a class or namespace",
2660                                 parser->scope, name);
2661             break;
2662           case NLE_NOT_CXX98:
2663             error_at (location,
2664                       "%<%E::%E%> is not a class, namespace, or enumeration",
2665                       parser->scope, name);
2666             break;
2667           default:
2668             gcc_unreachable ();
2669             
2670         }
2671     }
2672   else if (parser->scope == global_namespace)
2673     {
2674       switch (desired)
2675         {
2676           case NLE_TYPE:
2677             error_at (location, "%<::%E%> is not a type", name);
2678             break;
2679           case NLE_CXX98:
2680             error_at (location, "%<::%E%> is not a class or namespace", name);
2681             break;
2682           case NLE_NOT_CXX98:
2683             error_at (location,
2684                       "%<::%E%> is not a class, namespace, or enumeration",
2685                       name);
2686             break;
2687           default:
2688             gcc_unreachable ();
2689         }
2690     }
2691   else
2692     {
2693       switch (desired)
2694         {
2695           case NLE_TYPE:
2696             error_at (location, "%qE is not a type", name);
2697             break;
2698           case NLE_CXX98:
2699             error_at (location, "%qE is not a class or namespace", name);
2700             break;
2701           case NLE_NOT_CXX98:
2702             error_at (location,
2703                       "%qE is not a class, namespace, or enumeration", name);
2704             break;
2705           default:
2706             gcc_unreachable ();
2707         }
2708     }
2709 }
2710
2711 /* If we are parsing tentatively, remember that an error has occurred
2712    during this tentative parse.  Returns true if the error was
2713    simulated; false if a message should be issued by the caller.  */
2714
2715 static bool
2716 cp_parser_simulate_error (cp_parser* parser)
2717 {
2718   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2719     {
2720       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2721       return true;
2722     }
2723   return false;
2724 }
2725
2726 /* This function is called when a type is defined.  If type
2727    definitions are forbidden at this point, an error message is
2728    issued.  */
2729
2730 static bool
2731 cp_parser_check_type_definition (cp_parser* parser)
2732 {
2733   /* If types are forbidden here, issue a message.  */
2734   if (parser->type_definition_forbidden_message)
2735     {
2736       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2737          in the message need to be interpreted.  */
2738       error (parser->type_definition_forbidden_message);
2739       return false;
2740     }
2741   return true;
2742 }
2743
2744 /* This function is called when the DECLARATOR is processed.  The TYPE
2745    was a type defined in the decl-specifiers.  If it is invalid to
2746    define a type in the decl-specifiers for DECLARATOR, an error is
2747    issued. TYPE_LOCATION is the location of TYPE and is used
2748    for error reporting.  */
2749
2750 static void
2751 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2752                                                tree type, location_t type_location)
2753 {
2754   /* [dcl.fct] forbids type definitions in return types.
2755      Unfortunately, it's not easy to know whether or not we are
2756      processing a return type until after the fact.  */
2757   while (declarator
2758          && (declarator->kind == cdk_pointer
2759              || declarator->kind == cdk_reference
2760              || declarator->kind == cdk_ptrmem))
2761     declarator = declarator->declarator;
2762   if (declarator
2763       && declarator->kind == cdk_function)
2764     {
2765       error_at (type_location,
2766                 "new types may not be defined in a return type");
2767       inform (type_location, 
2768               "(perhaps a semicolon is missing after the definition of %qT)",
2769               type);
2770     }
2771 }
2772
2773 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2774    "<" in any valid C++ program.  If the next token is indeed "<",
2775    issue a message warning the user about what appears to be an
2776    invalid attempt to form a template-id. LOCATION is the location
2777    of the type-specifier (TYPE) */
2778
2779 static void
2780 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2781                                          tree type,
2782                                          enum tag_types tag_type,
2783                                          location_t location)
2784 {
2785   cp_token_position start = 0;
2786
2787   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2788     {
2789       if (TYPE_P (type))
2790         error_at (location, "%qT is not a template", type);
2791       else if (identifier_p (type))
2792         {
2793           if (tag_type != none_type)
2794             error_at (location, "%qE is not a class template", type);
2795           else
2796             error_at (location, "%qE is not a template", type);
2797         }
2798       else
2799         error_at (location, "invalid template-id");
2800       /* Remember the location of the invalid "<".  */
2801       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2802         start = cp_lexer_token_position (parser->lexer, true);
2803       /* Consume the "<".  */
2804       cp_lexer_consume_token (parser->lexer);
2805       /* Parse the template arguments.  */
2806       cp_parser_enclosed_template_argument_list (parser);
2807       /* Permanently remove the invalid template arguments so that
2808          this error message is not issued again.  */
2809       if (start)
2810         cp_lexer_purge_tokens_after (parser->lexer, start);
2811     }
2812 }
2813
2814 /* If parsing an integral constant-expression, issue an error message
2815    about the fact that THING appeared and return true.  Otherwise,
2816    return false.  In either case, set
2817    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2818
2819 static bool
2820 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2821                                             non_integral_constant thing)
2822 {
2823   parser->non_integral_constant_expression_p = true;
2824   if (parser->integral_constant_expression_p)
2825     {
2826       if (!parser->allow_non_integral_constant_expression_p)
2827         {
2828           const char *msg = NULL;
2829           switch (thing)
2830             {
2831               case NIC_FLOAT:
2832                 error ("floating-point literal "
2833                        "cannot appear in a constant-expression");
2834                 return true;
2835               case NIC_CAST:
2836                 error ("a cast to a type other than an integral or "
2837                        "enumeration type cannot appear in a "
2838                        "constant-expression");
2839                 return true;
2840               case NIC_TYPEID:
2841                 error ("%<typeid%> operator "
2842                        "cannot appear in a constant-expression");
2843                 return true;
2844               case NIC_NCC:
2845                 error ("non-constant compound literals "
2846                        "cannot appear in a constant-expression");
2847                 return true;
2848               case NIC_FUNC_CALL:
2849                 error ("a function call "
2850                        "cannot appear in a constant-expression");
2851                 return true;
2852               case NIC_INC:
2853                 error ("an increment "
2854                        "cannot appear in a constant-expression");
2855                 return true;
2856               case NIC_DEC:
2857                 error ("an decrement "
2858                        "cannot appear in a constant-expression");
2859                 return true;
2860               case NIC_ARRAY_REF:
2861                 error ("an array reference "
2862                        "cannot appear in a constant-expression");
2863                 return true;
2864               case NIC_ADDR_LABEL:
2865                 error ("the address of a label "
2866                        "cannot appear in a constant-expression");
2867                 return true;
2868               case NIC_OVERLOADED:
2869                 error ("calls to overloaded operators "
2870                        "cannot appear in a constant-expression");
2871                 return true;
2872               case NIC_ASSIGNMENT:
2873                 error ("an assignment cannot appear in a constant-expression");
2874                 return true;
2875               case NIC_COMMA:
2876                 error ("a comma operator "
2877                        "cannot appear in a constant-expression");
2878                 return true;
2879               case NIC_CONSTRUCTOR:
2880                 error ("a call to a constructor "
2881                        "cannot appear in a constant-expression");
2882                 return true;
2883               case NIC_TRANSACTION:
2884                 error ("a transaction expression "
2885                        "cannot appear in a constant-expression");
2886                 return true;
2887               case NIC_THIS:
2888                 msg = "this";
2889                 break;
2890               case NIC_FUNC_NAME:
2891                 msg = "__FUNCTION__";
2892                 break;
2893               case NIC_PRETTY_FUNC:
2894                 msg = "__PRETTY_FUNCTION__";
2895                 break;
2896               case NIC_C99_FUNC:
2897                 msg = "__func__";
2898                 break;
2899               case NIC_VA_ARG:
2900                 msg = "va_arg";
2901                 break;
2902               case NIC_ARROW:
2903                 msg = "->";
2904                 break;
2905               case NIC_POINT:
2906                 msg = ".";
2907                 break;
2908               case NIC_STAR:
2909                 msg = "*";
2910                 break;
2911               case NIC_ADDR:
2912                 msg = "&";
2913                 break;
2914               case NIC_PREINCREMENT:
2915                 msg = "++";
2916                 break;
2917               case NIC_PREDECREMENT:
2918                 msg = "--";
2919                 break;
2920               case NIC_NEW:
2921                 msg = "new";
2922                 break;
2923               case NIC_DEL:
2924                 msg = "delete";
2925                 break;
2926               default:
2927                 gcc_unreachable ();
2928             }
2929           if (msg)
2930             error ("%qs cannot appear in a constant-expression", msg);
2931           return true;
2932         }
2933     }
2934   return false;
2935 }
2936
2937 /* Emit a diagnostic for an invalid type name.  This function commits
2938    to the current active tentative parse, if any.  (Otherwise, the
2939    problematic construct might be encountered again later, resulting
2940    in duplicate error messages.) LOCATION is the location of ID.  */
2941
2942 static void
2943 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2944                                       location_t location)
2945 {
2946   tree decl, ambiguous_decls;
2947   cp_parser_commit_to_tentative_parse (parser);
2948   /* Try to lookup the identifier.  */
2949   decl = cp_parser_lookup_name (parser, id, none_type,
2950                                 /*is_template=*/false,
2951                                 /*is_namespace=*/false,
2952                                 /*check_dependency=*/true,
2953                                 &ambiguous_decls, location);
2954   if (ambiguous_decls)
2955     /* If the lookup was ambiguous, an error will already have
2956        been issued.  */
2957     return;
2958   /* If the lookup found a template-name, it means that the user forgot
2959   to specify an argument list. Emit a useful error message.  */
2960   if (TREE_CODE (decl) == TEMPLATE_DECL)
2961     error_at (location,
2962               "invalid use of template-name %qE without an argument list",
2963               decl);
2964   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2965     error_at (location, "invalid use of destructor %qD as a type", id);
2966   else if (TREE_CODE (decl) == TYPE_DECL)
2967     /* Something like 'unsigned A a;'  */
2968     error_at (location, "invalid combination of multiple type-specifiers");
2969   else if (!parser->scope)
2970     {
2971       /* Issue an error message.  */
2972       error_at (location, "%qE does not name a type", id);
2973       /* If we're in a template class, it's possible that the user was
2974          referring to a type from a base class.  For example:
2975
2976            template <typename T> struct A { typedef T X; };
2977            template <typename T> struct B : public A<T> { X x; };
2978
2979          The user should have said "typename A<T>::X".  */
2980       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2981         inform (location, "C++11 %<constexpr%> only available with "
2982                 "-std=c++11 or -std=gnu++11");
2983       else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2984         inform (location, "C++11 %<noexcept%> only available with "
2985                 "-std=c++11 or -std=gnu++11");
2986       else if (cxx_dialect < cxx11
2987                && TREE_CODE (id) == IDENTIFIER_NODE
2988                && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2989         inform (location, "C++11 %<thread_local%> only available with "
2990                 "-std=c++11 or -std=gnu++11");
2991       else if (processing_template_decl && current_class_type
2992                && TYPE_BINFO (current_class_type))
2993         {
2994           tree b;
2995
2996           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2997                b;
2998                b = TREE_CHAIN (b))
2999             {
3000               tree base_type = BINFO_TYPE (b);
3001               if (CLASS_TYPE_P (base_type)
3002                   && dependent_type_p (base_type))
3003                 {
3004                   tree field;
3005                   /* Go from a particular instantiation of the
3006                      template (which will have an empty TYPE_FIELDs),
3007                      to the main version.  */
3008                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3009                   for (field = TYPE_FIELDS (base_type);
3010                        field;
3011                        field = DECL_CHAIN (field))
3012                     if (TREE_CODE (field) == TYPE_DECL
3013                         && DECL_NAME (field) == id)
3014                       {
3015                         inform (location, 
3016                                 "(perhaps %<typename %T::%E%> was intended)",
3017                                 BINFO_TYPE (b), id);
3018                         break;
3019                       }
3020                   if (field)
3021                     break;
3022                 }
3023             }
3024         }
3025     }
3026   /* Here we diagnose qualified-ids where the scope is actually correct,
3027      but the identifier does not resolve to a valid type name.  */
3028   else if (parser->scope != error_mark_node)
3029     {
3030       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3031         {
3032           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3033             error_at (location_of (id),
3034                       "%qE in namespace %qE does not name a template type",
3035                       id, parser->scope);
3036           else
3037             error_at (location_of (id),
3038                       "%qE in namespace %qE does not name a type",
3039                       id, parser->scope);
3040         }
3041       else if (CLASS_TYPE_P (parser->scope)
3042                && constructor_name_p (id, parser->scope))
3043         {
3044           /* A<T>::A<T>() */
3045           error_at (location, "%<%T::%E%> names the constructor, not"
3046                     " the type", parser->scope, id);
3047           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3048             error_at (location, "and %qT has no template constructors",
3049                       parser->scope);
3050         }
3051       else if (TYPE_P (parser->scope)
3052                && dependent_scope_p (parser->scope))
3053         error_at (location, "need %<typename%> before %<%T::%E%> because "
3054                   "%qT is a dependent scope",
3055                   parser->scope, id, parser->scope);
3056       else if (TYPE_P (parser->scope))
3057         {
3058           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3059             error_at (location_of (id),
3060                       "%qE in %q#T does not name a template type",
3061                       id, parser->scope);
3062           else
3063             error_at (location_of (id),
3064                       "%qE in %q#T does not name a type",
3065                       id, parser->scope);
3066         }
3067       else
3068         gcc_unreachable ();
3069     }
3070 }
3071
3072 /* Check for a common situation where a type-name should be present,
3073    but is not, and issue a sensible error message.  Returns true if an
3074    invalid type-name was detected.
3075
3076    The situation handled by this function are variable declarations of the
3077    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3078    Usually, `ID' should name a type, but if we got here it means that it
3079    does not. We try to emit the best possible error message depending on
3080    how exactly the id-expression looks like.  */
3081
3082 static bool
3083 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3084 {
3085   tree id;
3086   cp_token *token = cp_lexer_peek_token (parser->lexer);
3087
3088   /* Avoid duplicate error about ambiguous lookup.  */
3089   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3090     {
3091       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3092       if (next->type == CPP_NAME && next->error_reported)
3093         goto out;
3094     }
3095
3096   cp_parser_parse_tentatively (parser);
3097   id = cp_parser_id_expression (parser,
3098                                 /*template_keyword_p=*/false,
3099                                 /*check_dependency_p=*/true,
3100                                 /*template_p=*/NULL,
3101                                 /*declarator_p=*/true,
3102                                 /*optional_p=*/false);
3103   /* If the next token is a (, this is a function with no explicit return
3104      type, i.e. constructor, destructor or conversion op.  */
3105   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3106       || TREE_CODE (id) == TYPE_DECL)
3107     {
3108       cp_parser_abort_tentative_parse (parser);
3109       return false;
3110     }
3111   if (!cp_parser_parse_definitely (parser))
3112     return false;
3113
3114   /* Emit a diagnostic for the invalid type.  */
3115   cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3116  out:
3117   /* If we aren't in the middle of a declarator (i.e. in a
3118      parameter-declaration-clause), skip to the end of the declaration;
3119      there's no point in trying to process it.  */
3120   if (!parser->in_declarator_p)
3121     cp_parser_skip_to_end_of_block_or_statement (parser);
3122   return true;
3123 }
3124
3125 /* Consume tokens up to, and including, the next non-nested closing `)'.
3126    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3127    are doing error recovery. Returns -1 if OR_COMMA is true and we
3128    found an unnested comma.  */
3129
3130 static int
3131 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3132                                        bool recovering,
3133                                        bool or_comma,
3134                                        bool consume_paren)
3135 {
3136   unsigned paren_depth = 0;
3137   unsigned brace_depth = 0;
3138   unsigned square_depth = 0;
3139
3140   if (recovering && !or_comma
3141       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3142     return 0;
3143
3144   while (true)
3145     {
3146       cp_token * token = cp_lexer_peek_token (parser->lexer);
3147
3148       switch (token->type)
3149         {
3150         case CPP_EOF:
3151         case CPP_PRAGMA_EOL:
3152           /* If we've run out of tokens, then there is no closing `)'.  */
3153           return 0;
3154
3155         /* This is good for lambda expression capture-lists.  */
3156         case CPP_OPEN_SQUARE:
3157           ++square_depth;
3158           break;
3159         case CPP_CLOSE_SQUARE:
3160           if (!square_depth--)
3161             return 0;
3162           break;
3163
3164         case CPP_SEMICOLON:
3165           /* This matches the processing in skip_to_end_of_statement.  */
3166           if (!brace_depth)
3167             return 0;
3168           break;
3169
3170         case CPP_OPEN_BRACE:
3171           ++brace_depth;
3172           break;
3173         case CPP_CLOSE_BRACE:
3174           if (!brace_depth--)
3175             return 0;
3176           break;
3177
3178         case CPP_COMMA:
3179           if (recovering && or_comma && !brace_depth && !paren_depth
3180               && !square_depth)
3181             return -1;
3182           break;
3183
3184         case CPP_OPEN_PAREN:
3185           if (!brace_depth)
3186             ++paren_depth;
3187           break;
3188
3189         case CPP_CLOSE_PAREN:
3190           if (!brace_depth && !paren_depth--)
3191             {
3192               if (consume_paren)
3193                 cp_lexer_consume_token (parser->lexer);
3194               return 1;
3195             }
3196           break;
3197
3198         default:
3199           break;
3200         }
3201
3202       /* Consume the token.  */
3203       cp_lexer_consume_token (parser->lexer);
3204     }
3205 }
3206
3207 /* Consume tokens until we reach the end of the current statement.
3208    Normally, that will be just before consuming a `;'.  However, if a
3209    non-nested `}' comes first, then we stop before consuming that.  */
3210
3211 static void
3212 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3213 {
3214   unsigned nesting_depth = 0;
3215
3216   /* Unwind generic function template scope if necessary.  */
3217   if (parser->fully_implicit_function_template_p)
3218     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3219
3220   while (true)
3221     {
3222       cp_token *token = cp_lexer_peek_token (parser->lexer);
3223
3224       switch (token->type)
3225         {
3226         case CPP_EOF:
3227         case CPP_PRAGMA_EOL:
3228           /* If we've run out of tokens, stop.  */
3229           return;
3230
3231         case CPP_SEMICOLON:
3232           /* If the next token is a `;', we have reached the end of the
3233              statement.  */
3234           if (!nesting_depth)
3235             return;
3236           break;
3237
3238         case CPP_CLOSE_BRACE:
3239           /* If this is a non-nested '}', stop before consuming it.
3240              That way, when confronted with something like:
3241
3242                { 3 + }
3243
3244              we stop before consuming the closing '}', even though we
3245              have not yet reached a `;'.  */
3246           if (nesting_depth == 0)
3247             return;
3248
3249           /* If it is the closing '}' for a block that we have
3250              scanned, stop -- but only after consuming the token.
3251              That way given:
3252
3253                 void f g () { ... }
3254                 typedef int I;
3255
3256              we will stop after the body of the erroneously declared
3257              function, but before consuming the following `typedef'
3258              declaration.  */
3259           if (--nesting_depth == 0)
3260             {
3261               cp_lexer_consume_token (parser->lexer);
3262               return;
3263             }
3264
3265         case CPP_OPEN_BRACE:
3266           ++nesting_depth;
3267           break;
3268
3269         default:
3270           break;
3271         }
3272
3273       /* Consume the token.  */
3274       cp_lexer_consume_token (parser->lexer);
3275     }
3276 }
3277
3278 /* This function is called at the end of a statement or declaration.
3279    If the next token is a semicolon, it is consumed; otherwise, error
3280    recovery is attempted.  */
3281
3282 static void
3283 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3284 {
3285   /* Look for the trailing `;'.  */
3286   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3287     {
3288       /* If there is additional (erroneous) input, skip to the end of
3289          the statement.  */
3290       cp_parser_skip_to_end_of_statement (parser);
3291       /* If the next token is now a `;', consume it.  */
3292       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3293         cp_lexer_consume_token (parser->lexer);
3294     }
3295 }
3296
3297 /* Skip tokens until we have consumed an entire block, or until we
3298    have consumed a non-nested `;'.  */
3299
3300 static void
3301 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3302 {
3303   int nesting_depth = 0;
3304
3305   /* Unwind generic function template scope if necessary.  */
3306   if (parser->fully_implicit_function_template_p)
3307     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3308
3309   while (nesting_depth >= 0)
3310     {
3311       cp_token *token = cp_lexer_peek_token (parser->lexer);
3312
3313       switch (token->type)
3314         {
3315         case CPP_EOF:
3316         case CPP_PRAGMA_EOL:
3317           /* If we've run out of tokens, stop.  */
3318           return;
3319
3320         case CPP_SEMICOLON:
3321           /* Stop if this is an unnested ';'. */
3322           if (!nesting_depth)
3323             nesting_depth = -1;
3324           break;
3325
3326         case CPP_CLOSE_BRACE:
3327           /* Stop if this is an unnested '}', or closes the outermost
3328              nesting level.  */
3329           nesting_depth--;
3330           if (nesting_depth < 0)
3331             return;
3332           if (!nesting_depth)
3333             nesting_depth = -1;
3334           break;
3335
3336         case CPP_OPEN_BRACE:
3337           /* Nest. */
3338           nesting_depth++;
3339           break;
3340
3341         default:
3342           break;
3343         }
3344
3345       /* Consume the token.  */
3346       cp_lexer_consume_token (parser->lexer);
3347     }
3348 }
3349
3350 /* Skip tokens until a non-nested closing curly brace is the next
3351    token, or there are no more tokens. Return true in the first case,
3352    false otherwise.  */
3353
3354 static bool
3355 cp_parser_skip_to_closing_brace (cp_parser *parser)
3356 {
3357   unsigned nesting_depth = 0;
3358
3359   while (true)
3360     {
3361       cp_token *token = cp_lexer_peek_token (parser->lexer);
3362
3363       switch (token->type)
3364         {
3365         case CPP_EOF:
3366         case CPP_PRAGMA_EOL:
3367           /* If we've run out of tokens, stop.  */
3368           return false;
3369
3370         case CPP_CLOSE_BRACE:
3371           /* If the next token is a non-nested `}', then we have reached
3372              the end of the current block.  */
3373           if (nesting_depth-- == 0)
3374             return true;
3375           break;
3376
3377         case CPP_OPEN_BRACE:
3378           /* If it the next token is a `{', then we are entering a new
3379              block.  Consume the entire block.  */
3380           ++nesting_depth;
3381           break;
3382
3383         default:
3384           break;
3385         }
3386
3387       /* Consume the token.  */
3388       cp_lexer_consume_token (parser->lexer);
3389     }
3390 }
3391
3392 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3393    parameter is the PRAGMA token, allowing us to purge the entire pragma
3394    sequence.  */
3395
3396 static void
3397 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3398 {
3399   cp_token *token;
3400
3401   parser->lexer->in_pragma = false;
3402
3403   do
3404     token = cp_lexer_consume_token (parser->lexer);
3405   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3406
3407   /* Ensure that the pragma is not parsed again.  */
3408   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3409 }
3410
3411 /* Require pragma end of line, resyncing with it as necessary.  The
3412    arguments are as for cp_parser_skip_to_pragma_eol.  */
3413
3414 static void
3415 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3416 {
3417   parser->lexer->in_pragma = false;
3418   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3419     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3420 }
3421
3422 /* This is a simple wrapper around make_typename_type. When the id is
3423    an unresolved identifier node, we can provide a superior diagnostic
3424    using cp_parser_diagnose_invalid_type_name.  */
3425
3426 static tree
3427 cp_parser_make_typename_type (cp_parser *parser, tree id,
3428                               location_t id_location)
3429 {
3430   tree result;
3431   if (identifier_p (id))
3432     {
3433       result = make_typename_type (parser->scope, id, typename_type,
3434                                    /*complain=*/tf_none);
3435       if (result == error_mark_node)
3436         cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3437       return result;
3438     }
3439   return make_typename_type (parser->scope, id, typename_type, tf_error);
3440 }
3441
3442 /* This is a wrapper around the
3443    make_{pointer,ptrmem,reference}_declarator functions that decides
3444    which one to call based on the CODE and CLASS_TYPE arguments. The
3445    CODE argument should be one of the values returned by
3446    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3447    appertain to the pointer or reference.  */
3448
3449 static cp_declarator *
3450 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3451                                     cp_cv_quals cv_qualifiers,
3452                                     cp_declarator *target,
3453                                     tree attributes)
3454 {
3455   if (code == ERROR_MARK)
3456     return cp_error_declarator;
3457
3458   if (code == INDIRECT_REF)
3459     if (class_type == NULL_TREE)
3460       return make_pointer_declarator (cv_qualifiers, target, attributes);
3461     else
3462       return make_ptrmem_declarator (cv_qualifiers, class_type,
3463                                      target, attributes);
3464   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3465     return make_reference_declarator (cv_qualifiers, target,
3466                                       false, attributes);
3467   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3468     return make_reference_declarator (cv_qualifiers, target,
3469                                       true, attributes);
3470   gcc_unreachable ();
3471 }
3472
3473 /* Create a new C++ parser.  */
3474
3475 static cp_parser *
3476 cp_parser_new (void)
3477 {
3478   cp_parser *parser;
3479   cp_lexer *lexer;
3480   unsigned i;
3481
3482   /* cp_lexer_new_main is called before doing GC allocation because
3483      cp_lexer_new_main might load a PCH file.  */
3484   lexer = cp_lexer_new_main ();
3485
3486   /* Initialize the binops_by_token so that we can get the tree
3487      directly from the token.  */
3488   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3489     binops_by_token[binops[i].token_type] = binops[i];
3490
3491   parser = ggc_cleared_alloc<cp_parser> ();
3492   parser->lexer = lexer;
3493   parser->context = cp_parser_context_new (NULL);
3494
3495   /* For now, we always accept GNU extensions.  */
3496   parser->allow_gnu_extensions_p = 1;
3497
3498   /* The `>' token is a greater-than operator, not the end of a
3499      template-id.  */
3500   parser->greater_than_is_operator_p = true;
3501
3502   parser->default_arg_ok_p = true;
3503
3504   /* We are not parsing a constant-expression.  */
3505   parser->integral_constant_expression_p = false;
3506   parser->allow_non_integral_constant_expression_p = false;
3507   parser->non_integral_constant_expression_p = false;
3508
3509   /* Local variable names are not forbidden.  */
3510   parser->local_variables_forbidden_p = false;
3511
3512   /* We are not processing an `extern "C"' declaration.  */
3513   parser->in_unbraced_linkage_specification_p = false;
3514
3515   /* We are not processing a declarator.  */
3516   parser->in_declarator_p = false;
3517
3518   /* We are not processing a template-argument-list.  */
3519   parser->in_template_argument_list_p = false;
3520
3521   /* We are not in an iteration statement.  */
3522   parser->in_statement = 0;
3523
3524   /* We are not in a switch statement.  */
3525   parser->in_switch_statement_p = false;
3526
3527   /* We are not parsing a type-id inside an expression.  */
3528   parser->in_type_id_in_expr_p = false;
3529
3530   /* Declarations aren't implicitly extern "C".  */
3531   parser->implicit_extern_c = false;
3532
3533   /* String literals should be translated to the execution character set.  */
3534   parser->translate_strings_p = true;
3535
3536   /* We are not parsing a function body.  */
3537   parser->in_function_body = false;
3538
3539   /* We can correct until told otherwise.  */
3540   parser->colon_corrects_to_scope_p = true;
3541
3542   /* The unparsed function queue is empty.  */
3543   push_unparsed_function_queues (parser);
3544
3545   /* There are no classes being defined.  */
3546   parser->num_classes_being_defined = 0;
3547
3548   /* No template parameters apply.  */
3549   parser->num_template_parameter_lists = 0;
3550
3551   /* Not declaring an implicit function template.  */
3552   parser->auto_is_implicit_function_template_parm_p = false;
3553   parser->fully_implicit_function_template_p = false;
3554   parser->implicit_template_parms = 0;
3555   parser->implicit_template_scope = 0;
3556
3557   return parser;
3558 }
3559
3560 /* Create a cp_lexer structure which will emit the tokens in CACHE
3561    and push it onto the parser's lexer stack.  This is used for delayed
3562    parsing of in-class method bodies and default arguments, and should
3563    not be confused with tentative parsing.  */
3564 static void
3565 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3566 {
3567   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3568   lexer->next = parser->lexer;
3569   parser->lexer = lexer;
3570
3571   /* Move the current source position to that of the first token in the
3572      new lexer.  */
3573   cp_lexer_set_source_position_from_token (lexer->next_token);
3574 }
3575
3576 /* Pop the top lexer off the parser stack.  This is never used for the
3577    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3578 static void
3579 cp_parser_pop_lexer (cp_parser *parser)
3580 {
3581   cp_lexer *lexer = parser->lexer;
3582   parser->lexer = lexer->next;
3583   cp_lexer_destroy (lexer);
3584
3585   /* Put the current source position back where it was before this
3586      lexer was pushed.  */
3587   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3588 }
3589
3590 /* Lexical conventions [gram.lex]  */
3591
3592 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3593    identifier.  */
3594
3595 static tree
3596 cp_parser_identifier (cp_parser* parser)
3597 {
3598   cp_token *token;
3599
3600   /* Look for the identifier.  */
3601   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3602   /* Return the value.  */
3603   return token ? token->u.value : error_mark_node;
3604 }
3605
3606 /* Parse a sequence of adjacent string constants.  Returns a
3607    TREE_STRING representing the combined, nul-terminated string
3608    constant.  If TRANSLATE is true, translate the string to the
3609    execution character set.  If WIDE_OK is true, a wide string is
3610    invalid here.
3611
3612    C++98 [lex.string] says that if a narrow string literal token is
3613    adjacent to a wide string literal token, the behavior is undefined.
3614    However, C99 6.4.5p4 says that this results in a wide string literal.
3615    We follow C99 here, for consistency with the C front end.
3616
3617    This code is largely lifted from lex_string() in c-lex.c.
3618
3619    FUTURE: ObjC++ will need to handle @-strings here.  */
3620 static tree
3621 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3622                           bool lookup_udlit = true)
3623 {
3624   tree value;
3625   size_t count;
3626   struct obstack str_ob;
3627   cpp_string str, istr, *strs;
3628   cp_token *tok;
3629   enum cpp_ttype type, curr_type;
3630   int have_suffix_p = 0;
3631   tree string_tree;
3632   tree suffix_id = NULL_TREE;
3633   bool curr_tok_is_userdef_p = false;
3634
3635   tok = cp_lexer_peek_token (parser->lexer);
3636   if (!cp_parser_is_string_literal (tok))
3637     {
3638       cp_parser_error (parser, "expected string-literal");
3639       return error_mark_node;
3640     }
3641
3642   if (cpp_userdef_string_p (tok->type))
3643     {
3644       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3645       curr_type = cpp_userdef_string_remove_type (tok->type);
3646       curr_tok_is_userdef_p = true;
3647     }
3648   else
3649     {
3650       string_tree = tok->u.value;
3651       curr_type = tok->type;
3652     }
3653   type = curr_type;
3654
3655   /* Try to avoid the overhead of creating and destroying an obstack
3656      for the common case of just one string.  */
3657   if (!cp_parser_is_string_literal
3658       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3659     {
3660       cp_lexer_consume_token (parser->lexer);
3661
3662       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3663       str.len = TREE_STRING_LENGTH (string_tree);
3664       count = 1;
3665
3666       if (curr_tok_is_userdef_p)
3667         {
3668           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3669           have_suffix_p = 1;
3670           curr_type = cpp_userdef_string_remove_type (tok->type);
3671         }
3672       else
3673         curr_type = tok->type;
3674
3675       strs = &str;
3676     }
3677   else
3678     {
3679       gcc_obstack_init (&str_ob);
3680       count = 0;
3681
3682       do
3683         {
3684           cp_lexer_consume_token (parser->lexer);
3685           count++;
3686           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3687           str.len = TREE_STRING_LENGTH (string_tree);
3688
3689           if (curr_tok_is_userdef_p)
3690             {
3691               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3692               if (have_suffix_p == 0)
3693                 {
3694                   suffix_id = curr_suffix_id;
3695                   have_suffix_p = 1;
3696                 }
3697               else if (have_suffix_p == 1
3698                        && curr_suffix_id != suffix_id)
3699                 {
3700                   error ("inconsistent user-defined literal suffixes"
3701                          " %qD and %qD in string literal",
3702                          suffix_id, curr_suffix_id);
3703                   have_suffix_p = -1;
3704                 }
3705               curr_type = cpp_userdef_string_remove_type (tok->type);
3706             }
3707           else
3708             curr_type = tok->type;
3709
3710           if (type != curr_type)
3711             {
3712               if (type == CPP_STRING)
3713                 type = curr_type;
3714               else if (curr_type != CPP_STRING)
3715                 error_at (tok->location,
3716                           "unsupported non-standard concatenation "
3717                           "of string literals");
3718             }
3719
3720           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3721
3722           tok = cp_lexer_peek_token (parser->lexer);
3723           if (cpp_userdef_string_p (tok->type))
3724             {
3725               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3726               curr_type = cpp_userdef_string_remove_type (tok->type);
3727               curr_tok_is_userdef_p = true;
3728             }
3729           else
3730             {
3731               string_tree = tok->u.value;
3732               curr_type = tok->type;
3733               curr_tok_is_userdef_p = false;
3734             }
3735         }
3736       while (cp_parser_is_string_literal (tok));
3737
3738       strs = (cpp_string *) obstack_finish (&str_ob);
3739     }
3740
3741   if (type != CPP_STRING && !wide_ok)
3742     {
3743       cp_parser_error (parser, "a wide string is invalid in this context");
3744       type = CPP_STRING;
3745     }
3746
3747   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3748       (parse_in, strs, count, &istr, type))
3749     {
3750       value = build_string (istr.len, (const char *)istr.text);
3751       free (CONST_CAST (unsigned char *, istr.text));
3752
3753       switch (type)
3754         {
3755         default:
3756         case CPP_STRING:
3757         case CPP_UTF8STRING:
3758           TREE_TYPE (value) = char_array_type_node;
3759           break;
3760         case CPP_STRING16:
3761           TREE_TYPE (value) = char16_array_type_node;
3762           break;
3763         case CPP_STRING32:
3764           TREE_TYPE (value) = char32_array_type_node;
3765           break;
3766         case CPP_WSTRING:
3767           TREE_TYPE (value) = wchar_array_type_node;
3768           break;
3769         }
3770
3771       value = fix_string_type (value);
3772
3773       if (have_suffix_p)
3774         {
3775           tree literal = build_userdef_literal (suffix_id, value,
3776                                                 OT_NONE, NULL_TREE);
3777           if (lookup_udlit)
3778             value = cp_parser_userdef_string_literal (literal);
3779           else
3780             value = literal;
3781         }
3782     }
3783   else
3784     /* cpp_interpret_string has issued an error.  */
3785     value = error_mark_node;
3786
3787   if (count > 1)
3788     obstack_free (&str_ob, 0);
3789
3790   return value;
3791 }
3792
3793 /* Look up a literal operator with the name and the exact arguments.  */
3794
3795 static tree
3796 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3797 {
3798   tree decl, fns;
3799   decl = lookup_name (name);
3800   if (!decl || !is_overloaded_fn (decl))
3801     return error_mark_node;
3802
3803   for (fns = decl; fns; fns = OVL_NEXT (fns))
3804     {
3805       unsigned int ix;
3806       bool found = true;
3807       tree fn = OVL_CURRENT (fns);
3808       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3809       if (parmtypes != NULL_TREE)
3810         {
3811           for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3812                ++ix, parmtypes = TREE_CHAIN (parmtypes))
3813             {
3814               tree tparm = TREE_VALUE (parmtypes);
3815               tree targ = TREE_TYPE ((*args)[ix]);
3816               bool ptr = TYPE_PTR_P (tparm);
3817               bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3818               if ((ptr || arr || !same_type_p (tparm, targ))
3819                   && (!ptr || !arr
3820                       || !same_type_p (TREE_TYPE (tparm),
3821                                        TREE_TYPE (targ))))
3822                 found = false;
3823             }
3824           if (found
3825               && ix == vec_safe_length (args)
3826               /* May be this should be sufficient_parms_p instead,
3827                  depending on how exactly should user-defined literals
3828                  work in presence of default arguments on the literal
3829                  operator parameters.  */
3830               && parmtypes == void_list_node)
3831             return decl;
3832         }
3833     }
3834
3835   return error_mark_node;
3836 }
3837
3838 /* Parse a user-defined char constant.  Returns a call to a user-defined
3839    literal operator taking the character as an argument.  */
3840
3841 static tree
3842 cp_parser_userdef_char_literal (cp_parser *parser)
3843 {
3844   cp_token *token = cp_lexer_consume_token (parser->lexer);
3845   tree literal = token->u.value;
3846   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3847   tree value = USERDEF_LITERAL_VALUE (literal);
3848   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3849   tree decl, result;
3850
3851   /* Build up a call to the user-defined operator  */
3852   /* Lookup the name we got back from the id-expression.  */
3853   vec<tree, va_gc> *args = make_tree_vector ();
3854   vec_safe_push (args, value);
3855   decl = lookup_literal_operator (name, args);
3856   if (!decl || decl == error_mark_node)
3857     {
3858       error ("unable to find character literal operator %qD with %qT argument",
3859              name, TREE_TYPE (value));
3860       release_tree_vector (args);
3861       return error_mark_node;
3862     }
3863   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3864   release_tree_vector (args);
3865   return result;
3866 }
3867
3868 /* A subroutine of cp_parser_userdef_numeric_literal to
3869    create a char... template parameter pack from a string node.  */
3870
3871 static tree
3872 make_char_string_pack (tree value)
3873 {
3874   tree charvec;
3875   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3876   const char *str = TREE_STRING_POINTER (value);
3877   int i, len = TREE_STRING_LENGTH (value) - 1;
3878   tree argvec = make_tree_vec (1);
3879
3880   /* Fill in CHARVEC with all of the parameters.  */
3881   charvec = make_tree_vec (len);
3882   for (i = 0; i < len; ++i)
3883     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3884
3885   /* Build the argument packs.  */
3886   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3887   TREE_TYPE (argpack) = char_type_node;
3888
3889   TREE_VEC_ELT (argvec, 0) = argpack;
3890
3891   return argvec;
3892 }
3893
3894 /* A subroutine of cp_parser_userdef_numeric_literal to
3895    create a char... template parameter pack from a string node.  */
3896
3897 static tree
3898 make_string_pack (tree value)
3899 {
3900   tree charvec;
3901   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3902   const unsigned char *str
3903     = (const unsigned char *) TREE_STRING_POINTER (value);
3904   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3905   int len = TREE_STRING_LENGTH (value) / sz - 1;
3906   tree argvec = make_tree_vec (2);
3907
3908   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3909   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3910
3911   /* First template parm is character type.  */
3912   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3913
3914   /* Fill in CHARVEC with all of the parameters.  */
3915   charvec = make_tree_vec (len);
3916   for (int i = 0; i < len; ++i)
3917     TREE_VEC_ELT (charvec, i)
3918       = double_int_to_tree (str_char_type_node,
3919                             double_int::from_buffer (str + i * sz, sz));
3920
3921   /* Build the argument packs.  */
3922   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3923   TREE_TYPE (argpack) = str_char_type_node;
3924
3925   TREE_VEC_ELT (argvec, 1) = argpack;
3926
3927   return argvec;
3928 }
3929
3930 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3931    literal operator.  */
3932
3933 static tree
3934 cp_parser_userdef_numeric_literal (cp_parser *parser)
3935 {
3936   cp_token *token = cp_lexer_consume_token (parser->lexer);
3937   tree literal = token->u.value;
3938   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3939   tree value = USERDEF_LITERAL_VALUE (literal);
3940   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3941   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3942   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3943   tree decl, result;
3944   vec<tree, va_gc> *args;
3945
3946   /* Look for a literal operator taking the exact type of numeric argument
3947      as the literal value.  */
3948   args = make_tree_vector ();
3949   vec_safe_push (args, value);
3950   decl = lookup_literal_operator (name, args);
3951   if (decl && decl != error_mark_node)
3952     {
3953       result = finish_call_expr (decl, &args, false, true,
3954                                  tf_warning_or_error);
3955
3956       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3957         {
3958           warning_at (token->location, OPT_Woverflow,
3959                       "integer literal exceeds range of %qT type",
3960                       long_long_unsigned_type_node);
3961         }
3962       else
3963         {
3964           if (overflow > 0)
3965             warning_at (token->location, OPT_Woverflow,
3966                         "floating literal exceeds range of %qT type",
3967                         long_double_type_node);
3968           else if (overflow < 0)
3969             warning_at (token->location, OPT_Woverflow,
3970                         "floating literal truncated to zero");
3971         }
3972
3973       release_tree_vector (args);
3974       return result;
3975     }
3976   release_tree_vector (args);
3977
3978   /* If the numeric argument didn't work, look for a raw literal
3979      operator taking a const char* argument consisting of the number
3980      in string format.  */
3981   args = make_tree_vector ();
3982   vec_safe_push (args, num_string);
3983   decl = lookup_literal_operator (name, args);
3984   if (decl && decl != error_mark_node)
3985     {
3986       result = finish_call_expr (decl, &args, false, true,
3987                                  tf_warning_or_error);
3988       release_tree_vector (args);
3989       return result;
3990     }
3991   release_tree_vector (args);
3992
3993   /* If the raw literal didn't work, look for a non-type template
3994      function with parameter pack char....  Call the function with
3995      template parameter characters representing the number.  */
3996   args = make_tree_vector ();
3997   decl = lookup_literal_operator (name, args);
3998   if (decl && decl != error_mark_node)
3999     {
4000       tree tmpl_args = make_char_string_pack (num_string);
4001       decl = lookup_template_function (decl, tmpl_args);
4002       result = finish_call_expr (decl, &args, false, true,
4003                                  tf_warning_or_error);
4004       release_tree_vector (args);
4005       return result;
4006     }
4007
4008   release_tree_vector (args);
4009
4010   error ("unable to find numeric literal operator %qD", name);
4011   if (!cpp_get_options (parse_in)->ext_numeric_literals)
4012     inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4013             "to enable more built-in suffixes");
4014   return error_mark_node;
4015 }
4016
4017 /* Parse a user-defined string constant.  Returns a call to a user-defined
4018    literal operator taking a character pointer and the length of the string
4019    as arguments.  */
4020
4021 static tree
4022 cp_parser_userdef_string_literal (tree literal)
4023 {
4024   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4025   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4026   tree value = USERDEF_LITERAL_VALUE (literal);
4027   int len = TREE_STRING_LENGTH (value)
4028         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4029   tree decl, result;
4030   vec<tree, va_gc> *args;
4031
4032   /* Build up a call to the user-defined operator.  */
4033   /* Lookup the name we got back from the id-expression.  */
4034   args = make_tree_vector ();
4035   vec_safe_push (args, value);
4036   vec_safe_push (args, build_int_cst (size_type_node, len));
4037   decl = lookup_literal_operator (name, args);
4038
4039   if (decl && decl != error_mark_node)
4040     {
4041       result = finish_call_expr (decl, &args, false, true,
4042                                  tf_warning_or_error);
4043       release_tree_vector (args);
4044       return result;
4045     }
4046   release_tree_vector (args);
4047
4048   /* Look for a template function with typename parameter CharT
4049      and parameter pack CharT...  Call the function with
4050      template parameter characters representing the string.  */
4051   args = make_tree_vector ();
4052   decl = lookup_literal_operator (name, args);
4053   if (decl && decl != error_mark_node)
4054     {
4055       tree tmpl_args = make_string_pack (value);
4056       decl = lookup_template_function (decl, tmpl_args);
4057       result = finish_call_expr (decl, &args, false, true,
4058                                  tf_warning_or_error);
4059       release_tree_vector (args);
4060       return result;
4061     }
4062   release_tree_vector (args);
4063
4064   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4065          name, TREE_TYPE (value), size_type_node);
4066   return error_mark_node;
4067 }
4068
4069
4070 /* Basic concepts [gram.basic]  */
4071
4072 /* Parse a translation-unit.
4073
4074    translation-unit:
4075      declaration-seq [opt]
4076
4077    Returns TRUE if all went well.  */
4078
4079 static bool
4080 cp_parser_translation_unit (cp_parser* parser)
4081 {
4082   /* The address of the first non-permanent object on the declarator
4083      obstack.  */
4084   static void *declarator_obstack_base;
4085
4086   bool success;
4087
4088   /* Create the declarator obstack, if necessary.  */
4089   if (!cp_error_declarator)
4090     {
4091       gcc_obstack_init (&declarator_obstack);
4092       /* Create the error declarator.  */
4093       cp_error_declarator = make_declarator (cdk_error);
4094       /* Create the empty parameter list.  */
4095       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4096       /* Remember where the base of the declarator obstack lies.  */
4097       declarator_obstack_base = obstack_next_free (&declarator_obstack);
4098     }
4099
4100   cp_parser_declaration_seq_opt (parser);
4101
4102   /* If there are no tokens left then all went well.  */
4103   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4104     {
4105       /* Get rid of the token array; we don't need it any more.  */
4106       cp_lexer_destroy (parser->lexer);
4107       parser->lexer = NULL;
4108
4109       /* This file might have been a context that's implicitly extern
4110          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
4111       if (parser->implicit_extern_c)
4112         {
4113           pop_lang_context ();
4114           parser->implicit_extern_c = false;
4115         }
4116
4117       /* Finish up.  */
4118       finish_translation_unit ();
4119
4120       success = true;
4121     }
4122   else
4123     {
4124       cp_parser_error (parser, "expected declaration");
4125       success = false;
4126     }
4127
4128   /* Make sure the declarator obstack was fully cleaned up.  */
4129   gcc_assert (obstack_next_free (&declarator_obstack)
4130               == declarator_obstack_base);
4131
4132   /* All went well.  */
4133   return success;
4134 }
4135
4136 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4137    decltype context.  */
4138
4139 static inline tsubst_flags_t
4140 complain_flags (bool decltype_p)
4141 {
4142   tsubst_flags_t complain = tf_warning_or_error;
4143   if (decltype_p)
4144     complain |= tf_decltype;
4145   return complain;
4146 }
4147
4148 /* We're about to parse a collection of statements.  If we're currently
4149    parsing tentatively, set up a firewall so that any nested
4150    cp_parser_commit_to_tentative_parse won't affect the current context.  */
4151
4152 static cp_token_position
4153 cp_parser_start_tentative_firewall (cp_parser *parser)
4154 {
4155   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4156     return 0;
4157
4158   cp_parser_parse_tentatively (parser);
4159   cp_parser_commit_to_topmost_tentative_parse (parser);
4160   return cp_lexer_token_position (parser->lexer, false);
4161 }
4162
4163 /* We've finished parsing the collection of statements.  Wrap up the
4164    firewall and replace the relevant tokens with the parsed form.  */
4165
4166 static void
4167 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4168                                   tree expr)
4169 {
4170   if (!start)
4171     return;
4172
4173   /* Finish the firewall level.  */
4174   cp_parser_parse_definitely (parser);
4175   /* And remember the result of the parse for when we try again.  */
4176   cp_token *token = cp_lexer_token_at (parser->lexer, start);
4177   token->type = CPP_PREPARSED_EXPR;
4178   token->u.value = expr;
4179   token->keyword = RID_MAX;
4180   cp_lexer_purge_tokens_after (parser->lexer, start);
4181 }
4182
4183 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4184    enclosing parentheses.  */
4185
4186 static tree
4187 cp_parser_statement_expr (cp_parser *parser)
4188 {
4189   cp_token_position start = cp_parser_start_tentative_firewall (parser);
4190
4191   /* Consume the '('.  */
4192   cp_lexer_consume_token (parser->lexer);
4193   /* Start the statement-expression.  */
4194   tree expr = begin_stmt_expr ();
4195   /* Parse the compound-statement.  */
4196   cp_parser_compound_statement (parser, expr, false, false);
4197   /* Finish up.  */
4198   expr = finish_stmt_expr (expr, false);
4199   /* Consume the ')'.  */
4200   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4201     cp_parser_skip_to_end_of_statement (parser);
4202
4203   cp_parser_end_tentative_firewall (parser, start, expr);
4204   return expr;
4205 }
4206
4207 /* Expressions [gram.expr] */
4208
4209 /* Parse a primary-expression.
4210
4211    primary-expression:
4212      literal
4213      this
4214      ( expression )
4215      id-expression
4216      lambda-expression (C++11)
4217
4218    GNU Extensions:
4219
4220    primary-expression:
4221      ( compound-statement )
4222      __builtin_va_arg ( assignment-expression , type-id )
4223      __builtin_offsetof ( type-id , offsetof-expression )
4224
4225    C++ Extensions:
4226      __has_nothrow_assign ( type-id )   
4227      __has_nothrow_constructor ( type-id )
4228      __has_nothrow_copy ( type-id )
4229      __has_trivial_assign ( type-id )   
4230      __has_trivial_constructor ( type-id )
4231      __has_trivial_copy ( type-id )
4232      __has_trivial_destructor ( type-id )
4233      __has_virtual_destructor ( type-id )     
4234      __is_abstract ( type-id )
4235      __is_base_of ( type-id , type-id )
4236      __is_class ( type-id )
4237      __is_empty ( type-id )
4238      __is_enum ( type-id )
4239      __is_final ( type-id )
4240      __is_literal_type ( type-id )
4241      __is_pod ( type-id )
4242      __is_polymorphic ( type-id )
4243      __is_std_layout ( type-id )
4244      __is_trivial ( type-id )
4245      __is_union ( type-id )
4246
4247    Objective-C++ Extension:
4248
4249    primary-expression:
4250      objc-expression
4251
4252    literal:
4253      __null
4254
4255    ADDRESS_P is true iff this expression was immediately preceded by
4256    "&" and therefore might denote a pointer-to-member.  CAST_P is true
4257    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
4258    true iff this expression is a template argument.
4259
4260    Returns a representation of the expression.  Upon return, *IDK
4261    indicates what kind of id-expression (if any) was present.  */
4262
4263 static tree
4264 cp_parser_primary_expression (cp_parser *parser,
4265                               bool address_p,
4266                               bool cast_p,
4267                               bool template_arg_p,
4268                               bool decltype_p,
4269                               cp_id_kind *idk)
4270 {
4271   cp_token *token = NULL;
4272
4273   /* Assume the primary expression is not an id-expression.  */
4274   *idk = CP_ID_KIND_NONE;
4275
4276   /* Peek at the next token.  */
4277   token = cp_lexer_peek_token (parser->lexer);
4278   switch ((int) token->type)
4279     {
4280       /* literal:
4281            integer-literal
4282            character-literal
4283            floating-literal
4284            string-literal
4285            boolean-literal
4286            pointer-literal
4287            user-defined-literal  */
4288     case CPP_CHAR:
4289     case CPP_CHAR16:
4290     case CPP_CHAR32:
4291     case CPP_WCHAR:
4292     case CPP_NUMBER:
4293     case CPP_PREPARSED_EXPR:
4294       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4295         return cp_parser_userdef_numeric_literal (parser);
4296       token = cp_lexer_consume_token (parser->lexer);
4297       if (TREE_CODE (token->u.value) == FIXED_CST)
4298         {
4299           error_at (token->location,
4300                     "fixed-point types not supported in C++");
4301           return error_mark_node;
4302         }
4303       /* Floating-point literals are only allowed in an integral
4304          constant expression if they are cast to an integral or
4305          enumeration type.  */
4306       if (TREE_CODE (token->u.value) == REAL_CST
4307           && parser->integral_constant_expression_p
4308           && pedantic)
4309         {
4310           /* CAST_P will be set even in invalid code like "int(2.7 +
4311              ...)".   Therefore, we have to check that the next token
4312              is sure to end the cast.  */
4313           if (cast_p)
4314             {
4315               cp_token *next_token;
4316
4317               next_token = cp_lexer_peek_token (parser->lexer);
4318               if (/* The comma at the end of an
4319                      enumerator-definition.  */
4320                   next_token->type != CPP_COMMA
4321                   /* The curly brace at the end of an enum-specifier.  */
4322                   && next_token->type != CPP_CLOSE_BRACE
4323                   /* The end of a statement.  */
4324                   && next_token->type != CPP_SEMICOLON
4325                   /* The end of the cast-expression.  */
4326                   && next_token->type != CPP_CLOSE_PAREN
4327                   /* The end of an array bound.  */
4328                   && next_token->type != CPP_CLOSE_SQUARE
4329                   /* The closing ">" in a template-argument-list.  */
4330                   && (next_token->type != CPP_GREATER
4331                       || parser->greater_than_is_operator_p)
4332                   /* C++0x only: A ">>" treated like two ">" tokens,
4333                      in a template-argument-list.  */
4334                   && (next_token->type != CPP_RSHIFT
4335                       || (cxx_dialect == cxx98)
4336                       || parser->greater_than_is_operator_p))
4337                 cast_p = false;
4338             }
4339
4340           /* If we are within a cast, then the constraint that the
4341              cast is to an integral or enumeration type will be
4342              checked at that point.  If we are not within a cast, then
4343              this code is invalid.  */
4344           if (!cast_p)
4345             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4346         }
4347       return token->u.value;
4348
4349     case CPP_CHAR_USERDEF:
4350     case CPP_CHAR16_USERDEF:
4351     case CPP_CHAR32_USERDEF:
4352     case CPP_WCHAR_USERDEF:
4353       return cp_parser_userdef_char_literal (parser);
4354
4355     case CPP_STRING:
4356     case CPP_STRING16:
4357     case CPP_STRING32:
4358     case CPP_WSTRING:
4359     case CPP_UTF8STRING:
4360     case CPP_STRING_USERDEF:
4361     case CPP_STRING16_USERDEF:
4362     case CPP_STRING32_USERDEF:
4363     case CPP_WSTRING_USERDEF:
4364     case CPP_UTF8STRING_USERDEF:
4365       /* ??? Should wide strings be allowed when parser->translate_strings_p
4366          is false (i.e. in attributes)?  If not, we can kill the third
4367          argument to cp_parser_string_literal.  */
4368       return cp_parser_string_literal (parser,
4369                                        parser->translate_strings_p,
4370                                        true);
4371
4372     case CPP_OPEN_PAREN:
4373       /* If we see `( { ' then we are looking at the beginning of
4374          a GNU statement-expression.  */
4375       if (cp_parser_allow_gnu_extensions_p (parser)
4376           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4377         {
4378           /* Statement-expressions are not allowed by the standard.  */
4379           pedwarn (token->location, OPT_Wpedantic,
4380                    "ISO C++ forbids braced-groups within expressions");
4381
4382           /* And they're not allowed outside of a function-body; you
4383              cannot, for example, write:
4384
4385              int i = ({ int j = 3; j + 1; });
4386
4387              at class or namespace scope.  */
4388           if (!parser->in_function_body
4389               || parser->in_template_argument_list_p)
4390             {
4391               error_at (token->location,
4392                         "statement-expressions are not allowed outside "
4393                         "functions nor in template-argument lists");
4394               cp_parser_skip_to_end_of_block_or_statement (parser);
4395               if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4396                 cp_lexer_consume_token (parser->lexer);
4397               return error_mark_node;
4398             }
4399           else
4400             return cp_parser_statement_expr (parser);
4401         }
4402       /* Otherwise it's a normal parenthesized expression.  */
4403       {
4404         tree expr;
4405         bool saved_greater_than_is_operator_p;
4406
4407         /* Consume the `('.  */
4408         cp_lexer_consume_token (parser->lexer);
4409         /* Within a parenthesized expression, a `>' token is always
4410            the greater-than operator.  */
4411         saved_greater_than_is_operator_p
4412           = parser->greater_than_is_operator_p;
4413         parser->greater_than_is_operator_p = true;
4414
4415         /* Parse the parenthesized expression.  */
4416         expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4417         /* Let the front end know that this expression was
4418            enclosed in parentheses. This matters in case, for
4419            example, the expression is of the form `A::B', since
4420            `&A::B' might be a pointer-to-member, but `&(A::B)' is
4421            not.  */
4422         expr = finish_parenthesized_expr (expr);
4423         /* DR 705: Wrapping an unqualified name in parentheses
4424            suppresses arg-dependent lookup.  We want to pass back
4425            CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4426            (c++/37862), but none of the others.  */
4427         if (*idk != CP_ID_KIND_QUALIFIED)
4428           *idk = CP_ID_KIND_NONE;
4429
4430         /* The `>' token might be the end of a template-id or
4431            template-parameter-list now.  */
4432         parser->greater_than_is_operator_p
4433           = saved_greater_than_is_operator_p;
4434         /* Consume the `)'.  */
4435         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4436           cp_parser_skip_to_end_of_statement (parser);
4437
4438         return expr;
4439       }
4440
4441     case CPP_OPEN_SQUARE:
4442       {
4443         if (c_dialect_objc ())
4444           {
4445             /* We might have an Objective-C++ message. */
4446             cp_parser_parse_tentatively (parser);
4447             tree msg = cp_parser_objc_message_expression (parser);
4448             /* If that works out, we're done ... */
4449             if (cp_parser_parse_definitely (parser))
4450               return msg;
4451             /* ... else, fall though to see if it's a lambda.  */
4452           }
4453         tree lam = cp_parser_lambda_expression (parser);
4454         /* Don't warn about a failed tentative parse.  */
4455         if (cp_parser_error_occurred (parser))
4456           return error_mark_node;
4457         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4458         return lam;
4459       }
4460
4461     case CPP_OBJC_STRING:
4462       if (c_dialect_objc ())
4463         /* We have an Objective-C++ string literal. */
4464         return cp_parser_objc_expression (parser);
4465       cp_parser_error (parser, "expected primary-expression");
4466       return error_mark_node;
4467
4468     case CPP_KEYWORD:
4469       switch (token->keyword)
4470         {
4471           /* These two are the boolean literals.  */
4472         case RID_TRUE:
4473           cp_lexer_consume_token (parser->lexer);
4474           return boolean_true_node;
4475         case RID_FALSE:
4476           cp_lexer_consume_token (parser->lexer);
4477           return boolean_false_node;
4478
4479           /* The `__null' literal.  */
4480         case RID_NULL:
4481           cp_lexer_consume_token (parser->lexer);
4482           return null_node;
4483
4484           /* The `nullptr' literal.  */
4485         case RID_NULLPTR:
4486           cp_lexer_consume_token (parser->lexer);
4487           return nullptr_node;
4488
4489           /* Recognize the `this' keyword.  */
4490         case RID_THIS:
4491           cp_lexer_consume_token (parser->lexer);
4492           if (parser->local_variables_forbidden_p)
4493             {
4494               error_at (token->location,
4495                         "%<this%> may not be used in this context");
4496               return error_mark_node;
4497             }
4498           /* Pointers cannot appear in constant-expressions.  */
4499           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4500             return error_mark_node;
4501           return finish_this_expr ();
4502
4503           /* The `operator' keyword can be the beginning of an
4504              id-expression.  */
4505         case RID_OPERATOR:
4506           goto id_expression;
4507
4508         case RID_FUNCTION_NAME:
4509         case RID_PRETTY_FUNCTION_NAME:
4510         case RID_C99_FUNCTION_NAME:
4511           {
4512             non_integral_constant name;
4513
4514             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4515                __func__ are the names of variables -- but they are
4516                treated specially.  Therefore, they are handled here,
4517                rather than relying on the generic id-expression logic
4518                below.  Grammatically, these names are id-expressions.
4519
4520                Consume the token.  */
4521             token = cp_lexer_consume_token (parser->lexer);
4522
4523             switch (token->keyword)
4524               {
4525               case RID_FUNCTION_NAME:
4526                 name = NIC_FUNC_NAME;
4527                 break;
4528               case RID_PRETTY_FUNCTION_NAME:
4529                 name = NIC_PRETTY_FUNC;
4530                 break;
4531               case RID_C99_FUNCTION_NAME:
4532                 name = NIC_C99_FUNC;
4533                 break;
4534               default:
4535                 gcc_unreachable ();
4536               }
4537
4538             if (cp_parser_non_integral_constant_expression (parser, name))
4539               return error_mark_node;
4540
4541             /* Look up the name.  */
4542             return finish_fname (token->u.value);
4543           }
4544
4545         case RID_VA_ARG:
4546           {
4547             tree expression;
4548             tree type;
4549             source_location type_location;
4550
4551             /* The `__builtin_va_arg' construct is used to handle
4552                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4553             cp_lexer_consume_token (parser->lexer);
4554             /* Look for the opening `('.  */
4555             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4556             /* Now, parse the assignment-expression.  */
4557             expression = cp_parser_assignment_expression (parser);
4558             /* Look for the `,'.  */
4559             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4560             type_location = cp_lexer_peek_token (parser->lexer)->location;
4561             /* Parse the type-id.  */
4562             type = cp_parser_type_id (parser);
4563             /* Look for the closing `)'.  */
4564             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4565             /* Using `va_arg' in a constant-expression is not
4566                allowed.  */
4567             if (cp_parser_non_integral_constant_expression (parser,
4568                                                             NIC_VA_ARG))
4569               return error_mark_node;
4570             return build_x_va_arg (type_location, expression, type);
4571           }
4572
4573         case RID_OFFSETOF:
4574           return cp_parser_builtin_offsetof (parser);
4575
4576         case RID_HAS_NOTHROW_ASSIGN:
4577         case RID_HAS_NOTHROW_CONSTRUCTOR:
4578         case RID_HAS_NOTHROW_COPY:        
4579         case RID_HAS_TRIVIAL_ASSIGN:
4580         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4581         case RID_HAS_TRIVIAL_COPY:        
4582         case RID_HAS_TRIVIAL_DESTRUCTOR:
4583         case RID_HAS_VIRTUAL_DESTRUCTOR:
4584         case RID_IS_ABSTRACT:
4585         case RID_IS_BASE_OF:
4586         case RID_IS_CLASS:
4587         case RID_IS_EMPTY:
4588         case RID_IS_ENUM:
4589         case RID_IS_FINAL:
4590         case RID_IS_LITERAL_TYPE:
4591         case RID_IS_POD:
4592         case RID_IS_POLYMORPHIC:
4593         case RID_IS_STD_LAYOUT:
4594         case RID_IS_TRIVIAL:
4595         case RID_IS_TRIVIALLY_ASSIGNABLE:
4596         case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4597         case RID_IS_TRIVIALLY_COPYABLE:
4598         case RID_IS_UNION:
4599           return cp_parser_trait_expr (parser, token->keyword);
4600
4601         /* Objective-C++ expressions.  */
4602         case RID_AT_ENCODE:
4603         case RID_AT_PROTOCOL:
4604         case RID_AT_SELECTOR:
4605           return cp_parser_objc_expression (parser);
4606
4607         case RID_TEMPLATE:
4608           if (parser->in_function_body
4609               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4610                   == CPP_LESS))
4611             {
4612               error_at (token->location,
4613                         "a template declaration cannot appear at block scope");
4614               cp_parser_skip_to_end_of_block_or_statement (parser);
4615               return error_mark_node;
4616             }
4617         default:
4618           cp_parser_error (parser, "expected primary-expression");
4619           return error_mark_node;
4620         }
4621
4622       /* An id-expression can start with either an identifier, a
4623          `::' as the beginning of a qualified-id, or the "operator"
4624          keyword.  */
4625     case CPP_NAME:
4626     case CPP_SCOPE:
4627     case CPP_TEMPLATE_ID:
4628     case CPP_NESTED_NAME_SPECIFIER:
4629       {
4630         tree id_expression;
4631         tree decl;
4632         const char *error_msg;
4633         bool template_p;
4634         bool done;
4635         cp_token *id_expr_token;
4636
4637       id_expression:
4638         /* Parse the id-expression.  */
4639         id_expression
4640           = cp_parser_id_expression (parser,
4641                                      /*template_keyword_p=*/false,
4642                                      /*check_dependency_p=*/true,
4643                                      &template_p,
4644                                      /*declarator_p=*/false,
4645                                      /*optional_p=*/false);
4646         if (id_expression == error_mark_node)
4647           return error_mark_node;
4648         id_expr_token = token;
4649         token = cp_lexer_peek_token (parser->lexer);
4650         done = (token->type != CPP_OPEN_SQUARE
4651                 && token->type != CPP_OPEN_PAREN
4652                 && token->type != CPP_DOT
4653                 && token->type != CPP_DEREF
4654                 && token->type != CPP_PLUS_PLUS
4655                 && token->type != CPP_MINUS_MINUS);
4656         /* If we have a template-id, then no further lookup is
4657            required.  If the template-id was for a template-class, we
4658            will sometimes have a TYPE_DECL at this point.  */
4659         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4660                  || TREE_CODE (id_expression) == TYPE_DECL)
4661           decl = id_expression;
4662         /* Look up the name.  */
4663         else
4664           {
4665             tree ambiguous_decls;
4666
4667             /* If we already know that this lookup is ambiguous, then
4668                we've already issued an error message; there's no reason
4669                to check again.  */
4670             if (id_expr_token->type == CPP_NAME
4671                 && id_expr_token->error_reported)
4672               {
4673                 cp_parser_simulate_error (parser);
4674                 return error_mark_node;
4675               }
4676
4677             decl = cp_parser_lookup_name (parser, id_expression,
4678                                           none_type,
4679                                           template_p,
4680                                           /*is_namespace=*/false,
4681                                           /*check_dependency=*/true,
4682                                           &ambiguous_decls,
4683                                           id_expr_token->location);
4684             /* If the lookup was ambiguous, an error will already have
4685                been issued.  */
4686             if (ambiguous_decls)
4687               return error_mark_node;
4688
4689             /* In Objective-C++, we may have an Objective-C 2.0
4690                dot-syntax for classes here.  */
4691             if (c_dialect_objc ()
4692                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4693                 && TREE_CODE (decl) == TYPE_DECL
4694                 && objc_is_class_name (decl))
4695               {
4696                 tree component;
4697                 cp_lexer_consume_token (parser->lexer);
4698                 component = cp_parser_identifier (parser);
4699                 if (component == error_mark_node)
4700                   return error_mark_node;
4701
4702                 return objc_build_class_component_ref (id_expression, component);
4703               }
4704
4705             /* In Objective-C++, an instance variable (ivar) may be preferred
4706                to whatever cp_parser_lookup_name() found.  */
4707             decl = objc_lookup_ivar (decl, id_expression);
4708
4709             /* If name lookup gives us a SCOPE_REF, then the
4710                qualifying scope was dependent.  */
4711             if (TREE_CODE (decl) == SCOPE_REF)
4712               {
4713                 /* At this point, we do not know if DECL is a valid
4714                    integral constant expression.  We assume that it is
4715                    in fact such an expression, so that code like:
4716
4717                       template <int N> struct A {
4718                         int a[B<N>::i];
4719                       };
4720                      
4721                    is accepted.  At template-instantiation time, we
4722                    will check that B<N>::i is actually a constant.  */
4723                 return decl;
4724               }
4725             /* Check to see if DECL is a local variable in a context
4726                where that is forbidden.  */
4727             if (parser->local_variables_forbidden_p
4728                 && local_variable_p (decl))
4729               {
4730                 /* It might be that we only found DECL because we are
4731                    trying to be generous with pre-ISO scoping rules.
4732                    For example, consider:
4733
4734                      int i;
4735                      void g() {
4736                        for (int i = 0; i < 10; ++i) {}
4737                        extern void f(int j = i);
4738                      }
4739
4740                    Here, name look up will originally find the out
4741                    of scope `i'.  We need to issue a warning message,
4742                    but then use the global `i'.  */
4743                 decl = check_for_out_of_scope_variable (decl);
4744                 if (local_variable_p (decl))
4745                   {
4746                     error_at (id_expr_token->location,
4747                               "local variable %qD may not appear in this context",
4748                               decl);
4749                     return error_mark_node;
4750                   }
4751               }
4752           }
4753
4754         decl = (finish_id_expression
4755                 (id_expression, decl, parser->scope,
4756                  idk,
4757                  parser->integral_constant_expression_p,
4758                  parser->allow_non_integral_constant_expression_p,
4759                  &parser->non_integral_constant_expression_p,
4760                  template_p, done, address_p,
4761                  template_arg_p,
4762                  &error_msg,
4763                  id_expr_token->location));
4764         if (error_msg)
4765           cp_parser_error (parser, error_msg);
4766         return decl;
4767       }
4768
4769       /* Anything else is an error.  */
4770     default:
4771       cp_parser_error (parser, "expected primary-expression");
4772       return error_mark_node;
4773     }
4774 }
4775
4776 static inline tree
4777 cp_parser_primary_expression (cp_parser *parser,
4778                               bool address_p,
4779                               bool cast_p,
4780                               bool template_arg_p,
4781                               cp_id_kind *idk)
4782 {
4783   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4784                                        /*decltype*/false, idk);
4785 }
4786
4787 /* Parse an id-expression.
4788
4789    id-expression:
4790      unqualified-id
4791      qualified-id
4792
4793    qualified-id:
4794      :: [opt] nested-name-specifier template [opt] unqualified-id
4795      :: identifier
4796      :: operator-function-id
4797      :: template-id
4798
4799    Return a representation of the unqualified portion of the
4800    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4801    a `::' or nested-name-specifier.
4802
4803    Often, if the id-expression was a qualified-id, the caller will
4804    want to make a SCOPE_REF to represent the qualified-id.  This
4805    function does not do this in order to avoid wastefully creating
4806    SCOPE_REFs when they are not required.
4807
4808    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4809    `template' keyword.
4810
4811    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4812    uninstantiated templates.
4813
4814    If *TEMPLATE_P is non-NULL, it is set to true iff the
4815    `template' keyword is used to explicitly indicate that the entity
4816    named is a template.
4817
4818    If DECLARATOR_P is true, the id-expression is appearing as part of
4819    a declarator, rather than as part of an expression.  */
4820
4821 static tree
4822 cp_parser_id_expression (cp_parser *parser,
4823                          bool template_keyword_p,
4824                          bool check_dependency_p,
4825                          bool *template_p,
4826                          bool declarator_p,
4827                          bool optional_p)
4828 {
4829   bool global_scope_p;
4830   bool nested_name_specifier_p;
4831
4832   /* Assume the `template' keyword was not used.  */
4833   if (template_p)
4834     *template_p = template_keyword_p;
4835
4836   /* Look for the optional `::' operator.  */
4837   global_scope_p
4838     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4839        != NULL_TREE);
4840   /* Look for the optional nested-name-specifier.  */
4841   nested_name_specifier_p
4842     = (cp_parser_nested_name_specifier_opt (parser,
4843                                             /*typename_keyword_p=*/false,
4844                                             check_dependency_p,
4845                                             /*type_p=*/false,
4846                                             declarator_p)
4847        != NULL_TREE);
4848   /* If there is a nested-name-specifier, then we are looking at
4849      the first qualified-id production.  */
4850   if (nested_name_specifier_p)
4851     {
4852       tree saved_scope;
4853       tree saved_object_scope;
4854       tree saved_qualifying_scope;
4855       tree unqualified_id;
4856       bool is_template;
4857
4858       /* See if the next token is the `template' keyword.  */
4859       if (!template_p)
4860         template_p = &is_template;
4861       *template_p = cp_parser_optional_template_keyword (parser);
4862       /* Name lookup we do during the processing of the
4863          unqualified-id might obliterate SCOPE.  */
4864       saved_scope = parser->scope;
4865       saved_object_scope = parser->object_scope;
4866       saved_qualifying_scope = parser->qualifying_scope;
4867       /* Process the final unqualified-id.  */
4868       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4869                                                  check_dependency_p,
4870                                                  declarator_p,
4871                                                  /*optional_p=*/false);
4872       /* Restore the SAVED_SCOPE for our caller.  */
4873       parser->scope = saved_scope;
4874       parser->object_scope = saved_object_scope;
4875       parser->qualifying_scope = saved_qualifying_scope;
4876
4877       return unqualified_id;
4878     }
4879   /* Otherwise, if we are in global scope, then we are looking at one
4880      of the other qualified-id productions.  */
4881   else if (global_scope_p)
4882     {
4883       cp_token *token;
4884       tree id;
4885
4886       /* Peek at the next token.  */
4887       token = cp_lexer_peek_token (parser->lexer);
4888
4889       /* If it's an identifier, and the next token is not a "<", then
4890          we can avoid the template-id case.  This is an optimization
4891          for this common case.  */
4892       if (token->type == CPP_NAME
4893           && !cp_parser_nth_token_starts_template_argument_list_p
4894                (parser, 2))
4895         return cp_parser_identifier (parser);
4896
4897       cp_parser_parse_tentatively (parser);
4898       /* Try a template-id.  */
4899       id = cp_parser_template_id (parser,
4900                                   /*template_keyword_p=*/false,
4901                                   /*check_dependency_p=*/true,
4902                                   none_type,
4903                                   declarator_p);
4904       /* If that worked, we're done.  */
4905       if (cp_parser_parse_definitely (parser))
4906         return id;
4907
4908       /* Peek at the next token.  (Changes in the token buffer may
4909          have invalidated the pointer obtained above.)  */
4910       token = cp_lexer_peek_token (parser->lexer);
4911
4912       switch (token->type)
4913         {
4914         case CPP_NAME:
4915           return cp_parser_identifier (parser);
4916
4917         case CPP_KEYWORD:
4918           if (token->keyword == RID_OPERATOR)
4919             return cp_parser_operator_function_id (parser);
4920           /* Fall through.  */
4921
4922         default:
4923           cp_parser_error (parser, "expected id-expression");
4924           return error_mark_node;
4925         }
4926     }
4927   else
4928     return cp_parser_unqualified_id (parser, template_keyword_p,
4929                                      /*check_dependency_p=*/true,
4930                                      declarator_p,
4931                                      optional_p);
4932 }
4933
4934 /* Parse an unqualified-id.
4935
4936    unqualified-id:
4937      identifier
4938      operator-function-id
4939      conversion-function-id
4940      ~ class-name
4941      template-id
4942
4943    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4944    keyword, in a construct like `A::template ...'.
4945
4946    Returns a representation of unqualified-id.  For the `identifier'
4947    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4948    production a BIT_NOT_EXPR is returned; the operand of the
4949    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4950    other productions, see the documentation accompanying the
4951    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4952    names are looked up in uninstantiated templates.  If DECLARATOR_P
4953    is true, the unqualified-id is appearing as part of a declarator,
4954    rather than as part of an expression.  */
4955
4956 static tree
4957 cp_parser_unqualified_id (cp_parser* parser,
4958                           bool template_keyword_p,
4959                           bool check_dependency_p,
4960                           bool declarator_p,
4961                           bool optional_p)
4962 {
4963   cp_token *token;
4964
4965   /* Peek at the next token.  */
4966   token = cp_lexer_peek_token (parser->lexer);
4967
4968   switch ((int) token->type)
4969     {
4970     case CPP_NAME:
4971       {
4972         tree id;
4973
4974         /* We don't know yet whether or not this will be a
4975            template-id.  */
4976         cp_parser_parse_tentatively (parser);
4977         /* Try a template-id.  */
4978         id = cp_parser_template_id (parser, template_keyword_p,
4979                                     check_dependency_p,
4980                                     none_type,
4981                                     declarator_p);
4982         /* If it worked, we're done.  */
4983         if (cp_parser_parse_definitely (parser))
4984           return id;
4985         /* Otherwise, it's an ordinary identifier.  */
4986         return cp_parser_identifier (parser);
4987       }
4988
4989     case CPP_TEMPLATE_ID:
4990       return cp_parser_template_id (parser, template_keyword_p,
4991                                     check_dependency_p,
4992                                     none_type,
4993                                     declarator_p);
4994
4995     case CPP_COMPL:
4996       {
4997         tree type_decl;
4998         tree qualifying_scope;
4999         tree object_scope;
5000         tree scope;
5001         bool done;
5002
5003         /* Consume the `~' token.  */
5004         cp_lexer_consume_token (parser->lexer);
5005         /* Parse the class-name.  The standard, as written, seems to
5006            say that:
5007
5008              template <typename T> struct S { ~S (); };
5009              template <typename T> S<T>::~S() {}
5010
5011            is invalid, since `~' must be followed by a class-name, but
5012            `S<T>' is dependent, and so not known to be a class.
5013            That's not right; we need to look in uninstantiated
5014            templates.  A further complication arises from:
5015
5016              template <typename T> void f(T t) {
5017                t.T::~T();
5018              }
5019
5020            Here, it is not possible to look up `T' in the scope of `T'
5021            itself.  We must look in both the current scope, and the
5022            scope of the containing complete expression.
5023
5024            Yet another issue is:
5025
5026              struct S {
5027                int S;
5028                ~S();
5029              };
5030
5031              S::~S() {}
5032
5033            The standard does not seem to say that the `S' in `~S'
5034            should refer to the type `S' and not the data member
5035            `S::S'.  */
5036
5037         /* DR 244 says that we look up the name after the "~" in the
5038            same scope as we looked up the qualifying name.  That idea
5039            isn't fully worked out; it's more complicated than that.  */
5040         scope = parser->scope;
5041         object_scope = parser->object_scope;
5042         qualifying_scope = parser->qualifying_scope;
5043
5044         /* Check for invalid scopes.  */
5045         if (scope == error_mark_node)
5046           {
5047             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5048               cp_lexer_consume_token (parser->lexer);
5049             return error_mark_node;
5050           }
5051         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5052           {
5053             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5054               error_at (token->location,
5055                         "scope %qT before %<~%> is not a class-name",
5056                         scope);
5057             cp_parser_simulate_error (parser);
5058             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5059               cp_lexer_consume_token (parser->lexer);
5060             return error_mark_node;
5061           }
5062         gcc_assert (!scope || TYPE_P (scope));
5063
5064         /* If the name is of the form "X::~X" it's OK even if X is a
5065            typedef.  */
5066         token = cp_lexer_peek_token (parser->lexer);
5067         if (scope
5068             && token->type == CPP_NAME
5069             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5070                 != CPP_LESS)
5071             && (token->u.value == TYPE_IDENTIFIER (scope)
5072                 || (CLASS_TYPE_P (scope)
5073                     && constructor_name_p (token->u.value, scope))))
5074           {
5075             cp_lexer_consume_token (parser->lexer);
5076             return build_nt (BIT_NOT_EXPR, scope);
5077           }
5078
5079         /* ~auto means the destructor of whatever the object is.  */
5080         if (cp_parser_is_keyword (token, RID_AUTO))
5081           {
5082             if (cxx_dialect < cxx14)
5083               pedwarn (input_location, 0,
5084                        "%<~auto%> only available with "
5085                        "-std=c++14 or -std=gnu++14");
5086             cp_lexer_consume_token (parser->lexer);
5087             return build_nt (BIT_NOT_EXPR, make_auto ());
5088           }
5089
5090         /* If there was an explicit qualification (S::~T), first look
5091            in the scope given by the qualification (i.e., S).
5092
5093            Note: in the calls to cp_parser_class_name below we pass
5094            typename_type so that lookup finds the injected-class-name
5095            rather than the constructor.  */
5096         done = false;
5097         type_decl = NULL_TREE;
5098         if (scope)
5099           {
5100             cp_parser_parse_tentatively (parser);
5101             type_decl = cp_parser_class_name (parser,
5102                                               /*typename_keyword_p=*/false,
5103                                               /*template_keyword_p=*/false,
5104                                               typename_type,
5105                                               /*check_dependency=*/false,
5106                                               /*class_head_p=*/false,
5107                                               declarator_p);
5108             if (cp_parser_parse_definitely (parser))
5109               done = true;
5110           }
5111         /* In "N::S::~S", look in "N" as well.  */
5112         if (!done && scope && qualifying_scope)
5113           {
5114             cp_parser_parse_tentatively (parser);
5115             parser->scope = qualifying_scope;
5116             parser->object_scope = NULL_TREE;
5117             parser->qualifying_scope = NULL_TREE;
5118             type_decl
5119               = cp_parser_class_name (parser,
5120                                       /*typename_keyword_p=*/false,
5121                                       /*template_keyword_p=*/false,
5122                                       typename_type,
5123                                       /*check_dependency=*/false,
5124                                       /*class_head_p=*/false,
5125                                       declarator_p);
5126             if (cp_parser_parse_definitely (parser))
5127               done = true;
5128           }
5129         /* In "p->S::~T", look in the scope given by "*p" as well.  */
5130         else if (!done && object_scope)
5131           {
5132             cp_parser_parse_tentatively (parser);
5133             parser->scope = object_scope;
5134             parser->object_scope = NULL_TREE;
5135             parser->qualifying_scope = NULL_TREE;
5136             type_decl
5137               = cp_parser_class_name (parser,
5138                                       /*typename_keyword_p=*/false,
5139                                       /*template_keyword_p=*/false,
5140                                       typename_type,
5141                                       /*check_dependency=*/false,
5142                                       /*class_head_p=*/false,
5143                                       declarator_p);
5144             if (cp_parser_parse_definitely (parser))
5145               done = true;
5146           }
5147         /* Look in the surrounding context.  */
5148         if (!done)
5149           {
5150             parser->scope = NULL_TREE;
5151             parser->object_scope = NULL_TREE;
5152             parser->qualifying_scope = NULL_TREE;
5153             if (processing_template_decl)
5154               cp_parser_parse_tentatively (parser);
5155             type_decl
5156               = cp_parser_class_name (parser,
5157                                       /*typename_keyword_p=*/false,
5158                                       /*template_keyword_p=*/false,
5159                                       typename_type,
5160                                       /*check_dependency=*/false,
5161                                       /*class_head_p=*/false,
5162                                       declarator_p);
5163             if (processing_template_decl
5164                 && ! cp_parser_parse_definitely (parser))
5165               {
5166                 /* We couldn't find a type with this name, so just accept
5167                    it and check for a match at instantiation time.  */
5168                 type_decl = cp_parser_identifier (parser);
5169                 if (type_decl != error_mark_node)
5170                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5171                 return type_decl;
5172               }
5173           }
5174         /* If an error occurred, assume that the name of the
5175            destructor is the same as the name of the qualifying
5176            class.  That allows us to keep parsing after running
5177            into ill-formed destructor names.  */
5178         if (type_decl == error_mark_node && scope)
5179           return build_nt (BIT_NOT_EXPR, scope);
5180         else if (type_decl == error_mark_node)
5181           return error_mark_node;
5182
5183         /* Check that destructor name and scope match.  */
5184         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5185           {
5186             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5187               error_at (token->location,
5188                         "declaration of %<~%T%> as member of %qT",
5189                         type_decl, scope);
5190             cp_parser_simulate_error (parser);
5191             return error_mark_node;
5192           }
5193
5194         /* [class.dtor]
5195
5196            A typedef-name that names a class shall not be used as the
5197            identifier in the declarator for a destructor declaration.  */
5198         if (declarator_p
5199             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5200             && !DECL_SELF_REFERENCE_P (type_decl)
5201             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5202           error_at (token->location,
5203                     "typedef-name %qD used as destructor declarator",
5204                     type_decl);
5205
5206         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5207       }
5208
5209     case CPP_KEYWORD:
5210       if (token->keyword == RID_OPERATOR)
5211         {
5212           tree id;
5213
5214           /* This could be a template-id, so we try that first.  */
5215           cp_parser_parse_tentatively (parser);
5216           /* Try a template-id.  */
5217           id = cp_parser_template_id (parser, template_keyword_p,
5218                                       /*check_dependency_p=*/true,
5219                                       none_type,
5220                                       declarator_p);
5221           /* If that worked, we're done.  */
5222           if (cp_parser_parse_definitely (parser))
5223             return id;
5224           /* We still don't know whether we're looking at an
5225              operator-function-id or a conversion-function-id.  */
5226           cp_parser_parse_tentatively (parser);
5227           /* Try an operator-function-id.  */
5228           id = cp_parser_operator_function_id (parser);
5229           /* If that didn't work, try a conversion-function-id.  */
5230           if (!cp_parser_parse_definitely (parser))
5231             id = cp_parser_conversion_function_id (parser);
5232           else if (UDLIT_OPER_P (id))
5233             {
5234               /* 17.6.3.3.5  */
5235               const char *name = UDLIT_OP_SUFFIX (id);
5236               if (name[0] != '_' && !in_system_header_at (input_location)
5237                   && declarator_p)
5238                 warning (0, "literal operator suffixes not preceded by %<_%>"
5239                             " are reserved for future standardization");
5240             }
5241
5242           return id;
5243         }
5244       /* Fall through.  */
5245
5246     default:
5247       if (optional_p)
5248         return NULL_TREE;
5249       cp_parser_error (parser, "expected unqualified-id");
5250       return error_mark_node;
5251     }
5252 }
5253
5254 /* Parse an (optional) nested-name-specifier.
5255
5256    nested-name-specifier: [C++98]
5257      class-or-namespace-name :: nested-name-specifier [opt]
5258      class-or-namespace-name :: template nested-name-specifier [opt]
5259
5260    nested-name-specifier: [C++0x]
5261      type-name ::
5262      namespace-name ::
5263      nested-name-specifier identifier ::
5264      nested-name-specifier template [opt] simple-template-id ::
5265
5266    PARSER->SCOPE should be set appropriately before this function is
5267    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5268    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
5269    in name lookups.
5270
5271    Sets PARSER->SCOPE to the class (TYPE) or namespace
5272    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5273    it unchanged if there is no nested-name-specifier.  Returns the new
5274    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5275
5276    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5277    part of a declaration and/or decl-specifier.  */
5278
5279 static tree
5280 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5281                                      bool typename_keyword_p,
5282                                      bool check_dependency_p,
5283                                      bool type_p,
5284                                      bool is_declaration)
5285 {
5286   bool success = false;
5287   cp_token_position start = 0;
5288   cp_token *token;
5289
5290   /* Remember where the nested-name-specifier starts.  */
5291   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5292     {
5293       start = cp_lexer_token_position (parser->lexer, false);
5294       push_deferring_access_checks (dk_deferred);
5295     }
5296
5297   while (true)
5298     {
5299       tree new_scope;
5300       tree old_scope;
5301       tree saved_qualifying_scope;
5302       bool template_keyword_p;
5303
5304       /* Spot cases that cannot be the beginning of a
5305          nested-name-specifier.  */
5306       token = cp_lexer_peek_token (parser->lexer);
5307
5308       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5309          the already parsed nested-name-specifier.  */
5310       if (token->type == CPP_NESTED_NAME_SPECIFIER)
5311         {
5312           /* Grab the nested-name-specifier and continue the loop.  */
5313           cp_parser_pre_parsed_nested_name_specifier (parser);
5314           /* If we originally encountered this nested-name-specifier
5315              with IS_DECLARATION set to false, we will not have
5316              resolved TYPENAME_TYPEs, so we must do so here.  */
5317           if (is_declaration
5318               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5319             {
5320               new_scope = resolve_typename_type (parser->scope,
5321                                                  /*only_current_p=*/false);
5322               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5323                 parser->scope = new_scope;
5324             }
5325           success = true;
5326           continue;
5327         }
5328
5329       /* Spot cases that cannot be the beginning of a
5330          nested-name-specifier.  On the second and subsequent times
5331          through the loop, we look for the `template' keyword.  */
5332       if (success && token->keyword == RID_TEMPLATE)
5333         ;
5334       /* A template-id can start a nested-name-specifier.  */
5335       else if (token->type == CPP_TEMPLATE_ID)
5336         ;
5337       /* DR 743: decltype can be used in a nested-name-specifier.  */
5338       else if (token_is_decltype (token))
5339         ;
5340       else
5341         {
5342           /* If the next token is not an identifier, then it is
5343              definitely not a type-name or namespace-name.  */
5344           if (token->type != CPP_NAME)
5345             break;
5346           /* If the following token is neither a `<' (to begin a
5347              template-id), nor a `::', then we are not looking at a
5348              nested-name-specifier.  */
5349           token = cp_lexer_peek_nth_token (parser->lexer, 2);
5350
5351           if (token->type == CPP_COLON
5352               && parser->colon_corrects_to_scope_p
5353               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5354             {
5355               error_at (token->location,
5356                         "found %<:%> in nested-name-specifier, expected %<::%>");
5357               token->type = CPP_SCOPE;
5358             }
5359
5360           if (token->type != CPP_SCOPE
5361               && !cp_parser_nth_token_starts_template_argument_list_p
5362                   (parser, 2))
5363             break;
5364         }
5365
5366       /* The nested-name-specifier is optional, so we parse
5367          tentatively.  */
5368       cp_parser_parse_tentatively (parser);
5369
5370       /* Look for the optional `template' keyword, if this isn't the
5371          first time through the loop.  */
5372       if (success)
5373         template_keyword_p = cp_parser_optional_template_keyword (parser);
5374       else
5375         template_keyword_p = false;
5376
5377       /* Save the old scope since the name lookup we are about to do
5378          might destroy it.  */
5379       old_scope = parser->scope;
5380       saved_qualifying_scope = parser->qualifying_scope;
5381       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5382          look up names in "X<T>::I" in order to determine that "Y" is
5383          a template.  So, if we have a typename at this point, we make
5384          an effort to look through it.  */
5385       if (is_declaration
5386           && !typename_keyword_p
5387           && parser->scope
5388           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5389         parser->scope = resolve_typename_type (parser->scope,
5390                                                /*only_current_p=*/false);
5391       /* Parse the qualifying entity.  */
5392       new_scope
5393         = cp_parser_qualifying_entity (parser,
5394                                        typename_keyword_p,
5395                                        template_keyword_p,
5396                                        check_dependency_p,
5397                                        type_p,
5398                                        is_declaration);
5399       /* Look for the `::' token.  */
5400       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5401
5402       /* If we found what we wanted, we keep going; otherwise, we're
5403          done.  */
5404       if (!cp_parser_parse_definitely (parser))
5405         {
5406           bool error_p = false;
5407
5408           /* Restore the OLD_SCOPE since it was valid before the
5409              failed attempt at finding the last
5410              class-or-namespace-name.  */
5411           parser->scope = old_scope;
5412           parser->qualifying_scope = saved_qualifying_scope;
5413
5414           /* If the next token is a decltype, and the one after that is a
5415              `::', then the decltype has failed to resolve to a class or
5416              enumeration type.  Give this error even when parsing
5417              tentatively since it can't possibly be valid--and we're going
5418              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5419              won't get another chance.*/
5420           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5421               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5422                   == CPP_SCOPE))
5423             {
5424               token = cp_lexer_consume_token (parser->lexer);
5425               error_at (token->location, "decltype evaluates to %qT, "
5426                         "which is not a class or enumeration type",
5427                         token->u.value);
5428               parser->scope = error_mark_node;
5429               error_p = true;
5430               /* As below.  */
5431               success = true;
5432               cp_lexer_consume_token (parser->lexer);
5433             }
5434
5435           if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5436               && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5437             {
5438               /* If we have a non-type template-id followed by ::, it can't
5439                  possibly be valid.  */
5440               token = cp_lexer_peek_token (parser->lexer);
5441               tree tid = token->u.tree_check_value->value;
5442               if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5443                   && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5444                 {
5445                   tree tmpl = NULL_TREE;
5446                   if (is_overloaded_fn (tid))
5447                     {
5448                       tree fns = get_fns (tid);
5449                       if (!OVL_CHAIN (fns))
5450                         tmpl = OVL_CURRENT (fns);
5451                       error_at (token->location, "function template-id %qD "
5452                                 "in nested-name-specifier", tid);
5453                     }
5454                   else
5455                     {
5456                       /* Variable template.  */
5457                       tmpl = TREE_OPERAND (tid, 0);
5458                       gcc_assert (variable_template_p (tmpl));
5459                       error_at (token->location, "variable template-id %qD "
5460                                 "in nested-name-specifier", tid);
5461                     }
5462                   if (tmpl)
5463                     inform (DECL_SOURCE_LOCATION (tmpl),
5464                             "%qD declared here", tmpl);
5465
5466                   parser->scope = error_mark_node;
5467                   error_p = true;
5468                   /* As below.  */
5469                   success = true;
5470                   cp_lexer_consume_token (parser->lexer);
5471                   cp_lexer_consume_token (parser->lexer);
5472                 }
5473             }
5474
5475           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5476             break;
5477           /* If the next token is an identifier, and the one after
5478              that is a `::', then any valid interpretation would have
5479              found a class-or-namespace-name.  */
5480           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5481                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5482                      == CPP_SCOPE)
5483                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5484                      != CPP_COMPL))
5485             {
5486               token = cp_lexer_consume_token (parser->lexer);
5487               if (!error_p)
5488                 {
5489                   if (!token->error_reported)
5490                     {
5491                       tree decl;
5492                       tree ambiguous_decls;
5493
5494                       decl = cp_parser_lookup_name (parser, token->u.value,
5495                                                     none_type,
5496                                                     /*is_template=*/false,
5497                                                     /*is_namespace=*/false,
5498                                                     /*check_dependency=*/true,
5499                                                     &ambiguous_decls,
5500                                                     token->location);
5501                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5502                         error_at (token->location,
5503                                   "%qD used without template parameters",
5504                                   decl);
5505                       else if (ambiguous_decls)
5506                         {
5507                           // cp_parser_lookup_name has the same diagnostic,
5508                           // thus make sure to emit it at most once.
5509                           if (cp_parser_uncommitted_to_tentative_parse_p
5510                               (parser))
5511                             {
5512                               error_at (token->location,
5513                                         "reference to %qD is ambiguous",
5514                                         token->u.value);
5515                               print_candidates (ambiguous_decls);
5516                             }
5517                           decl = error_mark_node;
5518                         }
5519                       else
5520                         {
5521                           if (cxx_dialect != cxx98)
5522                             cp_parser_name_lookup_error
5523                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5524                              token->location);
5525                           else
5526                             cp_parser_name_lookup_error
5527                             (parser, token->u.value, decl, NLE_CXX98,
5528                              token->location);
5529                         }
5530                     }
5531                   parser->scope = error_mark_node;
5532                   error_p = true;
5533                   /* Treat this as a successful nested-name-specifier
5534                      due to:
5535
5536                      [basic.lookup.qual]
5537
5538                      If the name found is not a class-name (clause
5539                      _class_) or namespace-name (_namespace.def_), the
5540                      program is ill-formed.  */
5541                   success = true;
5542                 }
5543               cp_lexer_consume_token (parser->lexer);
5544             }
5545           break;
5546         }
5547       /* We've found one valid nested-name-specifier.  */
5548       success = true;
5549       /* Name lookup always gives us a DECL.  */
5550       if (TREE_CODE (new_scope) == TYPE_DECL)
5551         new_scope = TREE_TYPE (new_scope);
5552       /* Uses of "template" must be followed by actual templates.  */
5553       if (template_keyword_p
5554           && !(CLASS_TYPE_P (new_scope)
5555                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5556                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5557                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5558           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5559                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5560                    == TEMPLATE_ID_EXPR)))
5561         permerror (input_location, TYPE_P (new_scope)
5562                    ? G_("%qT is not a template")
5563                    : G_("%qD is not a template"),
5564                    new_scope);
5565       /* If it is a class scope, try to complete it; we are about to
5566          be looking up names inside the class.  */
5567       if (TYPE_P (new_scope)
5568           /* Since checking types for dependency can be expensive,
5569              avoid doing it if the type is already complete.  */
5570           && !COMPLETE_TYPE_P (new_scope)
5571           /* Do not try to complete dependent types.  */
5572           && !dependent_type_p (new_scope))
5573         {
5574           new_scope = complete_type (new_scope);
5575           /* If it is a typedef to current class, use the current
5576              class instead, as the typedef won't have any names inside
5577              it yet.  */
5578           if (!COMPLETE_TYPE_P (new_scope)
5579               && currently_open_class (new_scope))
5580             new_scope = TYPE_MAIN_VARIANT (new_scope);
5581         }
5582       /* Make sure we look in the right scope the next time through
5583          the loop.  */
5584       parser->scope = new_scope;
5585     }
5586
5587   /* If parsing tentatively, replace the sequence of tokens that makes
5588      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5589      token.  That way, should we re-parse the token stream, we will
5590      not have to repeat the effort required to do the parse, nor will
5591      we issue duplicate error messages.  */
5592   if (success && start)
5593     {
5594       cp_token *token;
5595
5596       token = cp_lexer_token_at (parser->lexer, start);
5597       /* Reset the contents of the START token.  */
5598       token->type = CPP_NESTED_NAME_SPECIFIER;
5599       /* Retrieve any deferred checks.  Do not pop this access checks yet
5600          so the memory will not be reclaimed during token replacing below.  */
5601       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5602       token->u.tree_check_value->value = parser->scope;
5603       token->u.tree_check_value->checks = get_deferred_access_checks ();
5604       token->u.tree_check_value->qualifying_scope =
5605         parser->qualifying_scope;
5606       token->keyword = RID_MAX;
5607
5608       /* Purge all subsequent tokens.  */
5609       cp_lexer_purge_tokens_after (parser->lexer, start);
5610     }
5611
5612   if (start)
5613     pop_to_parent_deferring_access_checks ();
5614
5615   return success ? parser->scope : NULL_TREE;
5616 }
5617
5618 /* Parse a nested-name-specifier.  See
5619    cp_parser_nested_name_specifier_opt for details.  This function
5620    behaves identically, except that it will an issue an error if no
5621    nested-name-specifier is present.  */
5622
5623 static tree
5624 cp_parser_nested_name_specifier (cp_parser *parser,
5625                                  bool typename_keyword_p,
5626                                  bool check_dependency_p,
5627                                  bool type_p,
5628                                  bool is_declaration)
5629 {
5630   tree scope;
5631
5632   /* Look for the nested-name-specifier.  */
5633   scope = cp_parser_nested_name_specifier_opt (parser,
5634                                                typename_keyword_p,
5635                                                check_dependency_p,
5636                                                type_p,
5637                                                is_declaration);
5638   /* If it was not present, issue an error message.  */
5639   if (!scope)
5640     {
5641       cp_parser_error (parser, "expected nested-name-specifier");
5642       parser->scope = NULL_TREE;
5643     }
5644
5645   return scope;
5646 }
5647
5648 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5649    this is either a class-name or a namespace-name (which corresponds
5650    to the class-or-namespace-name production in the grammar). For
5651    C++0x, it can also be a type-name that refers to an enumeration
5652    type or a simple-template-id.
5653
5654    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5655    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5656    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5657    TYPE_P is TRUE iff the next name should be taken as a class-name,
5658    even the same name is declared to be another entity in the same
5659    scope.
5660
5661    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5662    specified by the class-or-namespace-name.  If neither is found the
5663    ERROR_MARK_NODE is returned.  */
5664
5665 static tree
5666 cp_parser_qualifying_entity (cp_parser *parser,
5667                              bool typename_keyword_p,
5668                              bool template_keyword_p,
5669                              bool check_dependency_p,
5670                              bool type_p,
5671                              bool is_declaration)
5672 {
5673   tree saved_scope;
5674   tree saved_qualifying_scope;
5675   tree saved_object_scope;
5676   tree scope;
5677   bool only_class_p;
5678   bool successful_parse_p;
5679
5680   /* DR 743: decltype can appear in a nested-name-specifier.  */
5681   if (cp_lexer_next_token_is_decltype (parser->lexer))
5682     {
5683       scope = cp_parser_decltype (parser);
5684       if (TREE_CODE (scope) != ENUMERAL_TYPE
5685           && !MAYBE_CLASS_TYPE_P (scope))
5686         {
5687           cp_parser_simulate_error (parser);
5688           return error_mark_node;
5689         }
5690       if (TYPE_NAME (scope))
5691         scope = TYPE_NAME (scope);
5692       return scope;
5693     }
5694
5695   /* Before we try to parse the class-name, we must save away the
5696      current PARSER->SCOPE since cp_parser_class_name will destroy
5697      it.  */
5698   saved_scope = parser->scope;
5699   saved_qualifying_scope = parser->qualifying_scope;
5700   saved_object_scope = parser->object_scope;
5701   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5702      there is no need to look for a namespace-name.  */
5703   only_class_p = template_keyword_p 
5704     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5705   if (!only_class_p)
5706     cp_parser_parse_tentatively (parser);
5707   scope = cp_parser_class_name (parser,
5708                                 typename_keyword_p,
5709                                 template_keyword_p,
5710                                 type_p ? class_type : none_type,
5711                                 check_dependency_p,
5712                                 /*class_head_p=*/false,
5713                                 is_declaration);
5714   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5715   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5716   if (!only_class_p 
5717       && cxx_dialect != cxx98
5718       && !successful_parse_p)
5719     {
5720       /* Restore the saved scope.  */
5721       parser->scope = saved_scope;
5722       parser->qualifying_scope = saved_qualifying_scope;
5723       parser->object_scope = saved_object_scope;
5724
5725       /* Parse tentatively.  */
5726       cp_parser_parse_tentatively (parser);
5727      
5728       /* Parse a type-name  */
5729       scope = cp_parser_type_name (parser);
5730
5731       /* "If the name found does not designate a namespace or a class,
5732          enumeration, or dependent type, the program is ill-formed."
5733
5734          We cover classes and dependent types above and namespaces below,
5735          so this code is only looking for enums.  */
5736       if (!scope || TREE_CODE (scope) != TYPE_DECL
5737           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5738         cp_parser_simulate_error (parser);
5739
5740       successful_parse_p = cp_parser_parse_definitely (parser);
5741     }
5742   /* If that didn't work, try for a namespace-name.  */
5743   if (!only_class_p && !successful_parse_p)
5744     {
5745       /* Restore the saved scope.  */
5746       parser->scope = saved_scope;
5747       parser->qualifying_scope = saved_qualifying_scope;
5748       parser->object_scope = saved_object_scope;
5749       /* If we are not looking at an identifier followed by the scope
5750          resolution operator, then this is not part of a
5751          nested-name-specifier.  (Note that this function is only used
5752          to parse the components of a nested-name-specifier.)  */
5753       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5754           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5755         return error_mark_node;
5756       scope = cp_parser_namespace_name (parser);
5757     }
5758
5759   return scope;
5760 }
5761
5762 /* Return true if we are looking at a compound-literal, false otherwise.  */
5763
5764 static bool
5765 cp_parser_compound_literal_p (cp_parser *parser)
5766 {
5767   /* Consume the `('.  */
5768   cp_lexer_consume_token (parser->lexer);
5769
5770   cp_lexer_save_tokens (parser->lexer);
5771
5772   /* Skip tokens until the next token is a closing parenthesis.
5773      If we find the closing `)', and the next token is a `{', then
5774      we are looking at a compound-literal.  */
5775   bool compound_literal_p
5776     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5777                                               /*consume_paren=*/true)
5778        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5779   
5780   /* Roll back the tokens we skipped.  */
5781   cp_lexer_rollback_tokens (parser->lexer);
5782
5783   return compound_literal_p;
5784 }
5785
5786 /* Parse a postfix-expression.
5787
5788    postfix-expression:
5789      primary-expression
5790      postfix-expression [ expression ]
5791      postfix-expression ( expression-list [opt] )
5792      simple-type-specifier ( expression-list [opt] )
5793      typename :: [opt] nested-name-specifier identifier
5794        ( expression-list [opt] )
5795      typename :: [opt] nested-name-specifier template [opt] template-id
5796        ( expression-list [opt] )
5797      postfix-expression . template [opt] id-expression
5798      postfix-expression -> template [opt] id-expression
5799      postfix-expression . pseudo-destructor-name
5800      postfix-expression -> pseudo-destructor-name
5801      postfix-expression ++
5802      postfix-expression --
5803      dynamic_cast < type-id > ( expression )
5804      static_cast < type-id > ( expression )
5805      reinterpret_cast < type-id > ( expression )
5806      const_cast < type-id > ( expression )
5807      typeid ( expression )
5808      typeid ( type-id )
5809
5810    GNU Extension:
5811
5812    postfix-expression:
5813      ( type-id ) { initializer-list , [opt] }
5814
5815    This extension is a GNU version of the C99 compound-literal
5816    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5817    but they are essentially the same concept.)
5818
5819    If ADDRESS_P is true, the postfix expression is the operand of the
5820    `&' operator.  CAST_P is true if this expression is the target of a
5821    cast.
5822
5823    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5824    class member access expressions [expr.ref].
5825
5826    Returns a representation of the expression.  */
5827
5828 static tree
5829 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5830                               bool member_access_only_p, bool decltype_p,
5831                               cp_id_kind * pidk_return)
5832 {
5833   cp_token *token;
5834   location_t loc;
5835   enum rid keyword;
5836   cp_id_kind idk = CP_ID_KIND_NONE;
5837   tree postfix_expression = NULL_TREE;
5838   bool is_member_access = false;
5839   int saved_in_statement = -1;
5840
5841   /* Peek at the next token.  */
5842   token = cp_lexer_peek_token (parser->lexer);
5843   loc = token->location;
5844   /* Some of the productions are determined by keywords.  */
5845   keyword = token->keyword;
5846   switch (keyword)
5847     {
5848     case RID_DYNCAST:
5849     case RID_STATCAST:
5850     case RID_REINTCAST:
5851     case RID_CONSTCAST:
5852       {
5853         tree type;
5854         tree expression;
5855         const char *saved_message;
5856         bool saved_in_type_id_in_expr_p;
5857
5858         /* All of these can be handled in the same way from the point
5859            of view of parsing.  Begin by consuming the token
5860            identifying the cast.  */
5861         cp_lexer_consume_token (parser->lexer);
5862
5863         /* New types cannot be defined in the cast.  */
5864         saved_message = parser->type_definition_forbidden_message;
5865         parser->type_definition_forbidden_message
5866           = G_("types may not be defined in casts");
5867
5868         /* Look for the opening `<'.  */
5869         cp_parser_require (parser, CPP_LESS, RT_LESS);
5870         /* Parse the type to which we are casting.  */
5871         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5872         parser->in_type_id_in_expr_p = true;
5873         type = cp_parser_type_id (parser);
5874         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5875         /* Look for the closing `>'.  */
5876         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5877         /* Restore the old message.  */
5878         parser->type_definition_forbidden_message = saved_message;
5879
5880         bool saved_greater_than_is_operator_p
5881           = parser->greater_than_is_operator_p;
5882         parser->greater_than_is_operator_p = true;
5883
5884         /* And the expression which is being cast.  */
5885         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5886         expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5887         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5888
5889         parser->greater_than_is_operator_p
5890           = saved_greater_than_is_operator_p;
5891
5892         /* Only type conversions to integral or enumeration types
5893            can be used in constant-expressions.  */
5894         if (!cast_valid_in_integral_constant_expression_p (type)
5895             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5896           return error_mark_node;
5897
5898         switch (keyword)
5899           {
5900           case RID_DYNCAST:
5901             postfix_expression
5902               = build_dynamic_cast (type, expression, tf_warning_or_error);
5903             break;
5904           case RID_STATCAST:
5905             postfix_expression
5906               = build_static_cast (type, expression, tf_warning_or_error);
5907             break;
5908           case RID_REINTCAST:
5909             postfix_expression
5910               = build_reinterpret_cast (type, expression, 
5911                                         tf_warning_or_error);
5912             break;
5913           case RID_CONSTCAST:
5914             postfix_expression
5915               = build_const_cast (type, expression, tf_warning_or_error);
5916             break;
5917           default:
5918             gcc_unreachable ();
5919           }
5920       }
5921       break;
5922
5923     case RID_TYPEID:
5924       {
5925         tree type;
5926         const char *saved_message;
5927         bool saved_in_type_id_in_expr_p;
5928
5929         /* Consume the `typeid' token.  */
5930         cp_lexer_consume_token (parser->lexer);
5931         /* Look for the `(' token.  */
5932         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5933         /* Types cannot be defined in a `typeid' expression.  */
5934         saved_message = parser->type_definition_forbidden_message;
5935         parser->type_definition_forbidden_message
5936           = G_("types may not be defined in a %<typeid%> expression");
5937         /* We can't be sure yet whether we're looking at a type-id or an
5938            expression.  */
5939         cp_parser_parse_tentatively (parser);
5940         /* Try a type-id first.  */
5941         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5942         parser->in_type_id_in_expr_p = true;
5943         type = cp_parser_type_id (parser);
5944         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5945         /* Look for the `)' token.  Otherwise, we can't be sure that
5946            we're not looking at an expression: consider `typeid (int
5947            (3))', for example.  */
5948         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5949         /* If all went well, simply lookup the type-id.  */
5950         if (cp_parser_parse_definitely (parser))
5951           postfix_expression = get_typeid (type, tf_warning_or_error);
5952         /* Otherwise, fall back to the expression variant.  */
5953         else
5954           {
5955             tree expression;
5956
5957             /* Look for an expression.  */
5958             expression = cp_parser_expression (parser, & idk);
5959             /* Compute its typeid.  */
5960             postfix_expression = build_typeid (expression, tf_warning_or_error);
5961             /* Look for the `)' token.  */
5962             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5963           }
5964         /* Restore the saved message.  */
5965         parser->type_definition_forbidden_message = saved_message;
5966         /* `typeid' may not appear in an integral constant expression.  */
5967         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5968           return error_mark_node;
5969       }
5970       break;
5971
5972     case RID_TYPENAME:
5973       {
5974         tree type;
5975         /* The syntax permitted here is the same permitted for an
5976            elaborated-type-specifier.  */
5977         type = cp_parser_elaborated_type_specifier (parser,
5978                                                     /*is_friend=*/false,
5979                                                     /*is_declaration=*/false);
5980         postfix_expression = cp_parser_functional_cast (parser, type);
5981       }
5982       break;
5983
5984     case RID_CILK_SPAWN:
5985       {
5986         cp_lexer_consume_token (parser->lexer);
5987         token = cp_lexer_peek_token (parser->lexer);
5988         if (token->type == CPP_SEMICOLON)
5989           {
5990             error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5991                       "an expression");
5992             postfix_expression = error_mark_node;
5993             break;
5994           }
5995         else if (!current_function_decl)
5996           {
5997             error_at (token->location, "%<_Cilk_spawn%> may only be used "
5998                       "inside a function");
5999             postfix_expression = error_mark_node;
6000             break;
6001           }
6002         else
6003           {
6004             /* Consecutive _Cilk_spawns are not allowed in a statement.  */
6005             saved_in_statement = parser->in_statement;
6006             parser->in_statement |= IN_CILK_SPAWN;
6007           }
6008         cfun->calls_cilk_spawn = 1;
6009         postfix_expression = 
6010           cp_parser_postfix_expression (parser, false, false, 
6011                                         false, false, &idk);
6012         if (!flag_cilkplus)
6013           {
6014             error_at (token->location, "-fcilkplus must be enabled to use"
6015                       " %<_Cilk_spawn%>");
6016             cfun->calls_cilk_spawn = 0;
6017           }
6018         else if (saved_in_statement & IN_CILK_SPAWN)
6019           {
6020             error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6021                       "are not permitted");
6022             postfix_expression = error_mark_node;
6023             cfun->calls_cilk_spawn = 0; 
6024           }
6025         else
6026           {
6027             postfix_expression = build_cilk_spawn (token->location, 
6028                                                    postfix_expression);
6029             if (postfix_expression != error_mark_node) 
6030               SET_EXPR_LOCATION (postfix_expression, input_location);
6031             parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6032           }
6033         break;
6034       }
6035
6036     case RID_BUILTIN_SHUFFLE:
6037       {
6038         vec<tree, va_gc> *vec;
6039         unsigned int i;
6040         tree p;
6041
6042         cp_lexer_consume_token (parser->lexer);
6043         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6044                     /*cast_p=*/false, /*allow_expansion_p=*/true,
6045                     /*non_constant_p=*/NULL);
6046         if (vec == NULL)
6047           return error_mark_node;
6048
6049         FOR_EACH_VEC_ELT (*vec, i, p)
6050           mark_exp_read (p);
6051
6052         if (vec->length () == 2)
6053           return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6054                                          tf_warning_or_error);
6055         else if (vec->length () == 3)
6056           return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6057                                          tf_warning_or_error);
6058         else
6059         {
6060           error_at (loc, "wrong number of arguments to "
6061               "%<__builtin_shuffle%>");
6062           return error_mark_node;
6063         }
6064         break;
6065       }
6066
6067     default:
6068       {
6069         tree type;
6070
6071         /* If the next thing is a simple-type-specifier, we may be
6072            looking at a functional cast.  We could also be looking at
6073            an id-expression.  So, we try the functional cast, and if
6074            that doesn't work we fall back to the primary-expression.  */
6075         cp_parser_parse_tentatively (parser);
6076         /* Look for the simple-type-specifier.  */
6077         type = cp_parser_simple_type_specifier (parser,
6078                                                 /*decl_specs=*/NULL,
6079                                                 CP_PARSER_FLAGS_NONE);
6080         /* Parse the cast itself.  */
6081         if (!cp_parser_error_occurred (parser))
6082           postfix_expression
6083             = cp_parser_functional_cast (parser, type);
6084         /* If that worked, we're done.  */
6085         if (cp_parser_parse_definitely (parser))
6086           break;
6087
6088         /* If the functional-cast didn't work out, try a
6089            compound-literal.  */
6090         if (cp_parser_allow_gnu_extensions_p (parser)
6091             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6092           {
6093             tree initializer = NULL_TREE;
6094
6095             cp_parser_parse_tentatively (parser);
6096
6097             /* Avoid calling cp_parser_type_id pointlessly, see comment
6098                in cp_parser_cast_expression about c++/29234.  */
6099             if (!cp_parser_compound_literal_p (parser))
6100               cp_parser_simulate_error (parser);
6101             else
6102               {
6103                 /* Parse the type.  */
6104                 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6105                 parser->in_type_id_in_expr_p = true;
6106                 type = cp_parser_type_id (parser);
6107                 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6108                 /* Look for the `)'.  */
6109                 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6110               }
6111
6112             /* If things aren't going well, there's no need to
6113                keep going.  */
6114             if (!cp_parser_error_occurred (parser))
6115               {
6116                 bool non_constant_p;
6117                 /* Parse the brace-enclosed initializer list.  */
6118                 initializer = cp_parser_braced_list (parser,
6119                                                      &non_constant_p);
6120               }
6121             /* If that worked, we're definitely looking at a
6122                compound-literal expression.  */
6123             if (cp_parser_parse_definitely (parser))
6124               {
6125                 /* Warn the user that a compound literal is not
6126                    allowed in standard C++.  */
6127                 pedwarn (input_location, OPT_Wpedantic,
6128                          "ISO C++ forbids compound-literals");
6129                 /* For simplicity, we disallow compound literals in
6130                    constant-expressions.  We could
6131                    allow compound literals of integer type, whose
6132                    initializer was a constant, in constant
6133                    expressions.  Permitting that usage, as a further
6134                    extension, would not change the meaning of any
6135                    currently accepted programs.  (Of course, as
6136                    compound literals are not part of ISO C++, the
6137                    standard has nothing to say.)  */
6138                 if (cp_parser_non_integral_constant_expression (parser,
6139                                                                 NIC_NCC))
6140                   {
6141                     postfix_expression = error_mark_node;
6142                     break;
6143                   }
6144                 /* Form the representation of the compound-literal.  */
6145                 postfix_expression
6146                   = finish_compound_literal (type, initializer,
6147                                              tf_warning_or_error);
6148                 break;
6149               }
6150           }
6151
6152         /* It must be a primary-expression.  */
6153         postfix_expression
6154           = cp_parser_primary_expression (parser, address_p, cast_p,
6155                                           /*template_arg_p=*/false,
6156                                           decltype_p,
6157                                           &idk);
6158       }
6159       break;
6160     }
6161
6162   /* Note that we don't need to worry about calling build_cplus_new on a
6163      class-valued CALL_EXPR in decltype when it isn't the end of the
6164      postfix-expression; unary_complex_lvalue will take care of that for
6165      all these cases.  */
6166
6167   /* Keep looping until the postfix-expression is complete.  */
6168   while (true)
6169     {
6170       if (idk == CP_ID_KIND_UNQUALIFIED
6171           && identifier_p (postfix_expression)
6172           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6173         /* It is not a Koenig lookup function call.  */
6174         postfix_expression
6175           = unqualified_name_lookup_error (postfix_expression);
6176
6177       /* Peek at the next token.  */
6178       token = cp_lexer_peek_token (parser->lexer);
6179
6180       switch (token->type)
6181         {
6182         case CPP_OPEN_SQUARE:
6183           if (cp_next_tokens_can_be_std_attribute_p (parser))
6184             {
6185               cp_parser_error (parser,
6186                                "two consecutive %<[%> shall "
6187                                "only introduce an attribute");
6188               return error_mark_node;
6189             }
6190           postfix_expression
6191             = cp_parser_postfix_open_square_expression (parser,
6192                                                         postfix_expression,
6193                                                         false,
6194                                                         decltype_p);
6195           idk = CP_ID_KIND_NONE;
6196           is_member_access = false;
6197           break;
6198
6199         case CPP_OPEN_PAREN:
6200           /* postfix-expression ( expression-list [opt] ) */
6201           {
6202             bool koenig_p;
6203             bool is_builtin_constant_p;
6204             bool saved_integral_constant_expression_p = false;
6205             bool saved_non_integral_constant_expression_p = false;
6206             tsubst_flags_t complain = complain_flags (decltype_p);
6207             vec<tree, va_gc> *args;
6208
6209             is_member_access = false;
6210
6211             is_builtin_constant_p
6212               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6213             if (is_builtin_constant_p)
6214               {
6215                 /* The whole point of __builtin_constant_p is to allow
6216                    non-constant expressions to appear as arguments.  */
6217                 saved_integral_constant_expression_p
6218                   = parser->integral_constant_expression_p;
6219                 saved_non_integral_constant_expression_p
6220                   = parser->non_integral_constant_expression_p;
6221                 parser->integral_constant_expression_p = false;
6222               }
6223             args = (cp_parser_parenthesized_expression_list
6224                     (parser, non_attr,
6225                      /*cast_p=*/false, /*allow_expansion_p=*/true,
6226                      /*non_constant_p=*/NULL,
6227                      /*want_literal_zero_p=*/warn_memset_transposed_args));
6228             if (is_builtin_constant_p)
6229               {
6230                 parser->integral_constant_expression_p
6231                   = saved_integral_constant_expression_p;
6232                 parser->non_integral_constant_expression_p
6233                   = saved_non_integral_constant_expression_p;
6234               }
6235
6236             if (args == NULL)
6237               {
6238                 postfix_expression = error_mark_node;
6239                 break;
6240               }
6241
6242             /* Function calls are not permitted in
6243                constant-expressions.  */
6244             if (! builtin_valid_in_constant_expr_p (postfix_expression)
6245                 && cp_parser_non_integral_constant_expression (parser,
6246                                                                NIC_FUNC_CALL))
6247               {
6248                 postfix_expression = error_mark_node;
6249                 release_tree_vector (args);
6250                 break;
6251               }
6252
6253             koenig_p = false;
6254             if (idk == CP_ID_KIND_UNQUALIFIED
6255                 || idk == CP_ID_KIND_TEMPLATE_ID)
6256               {
6257                 if (identifier_p (postfix_expression))
6258                   {
6259                     if (!args->is_empty ())
6260                       {
6261                         koenig_p = true;
6262                         if (!any_type_dependent_arguments_p (args))
6263                           postfix_expression
6264                             = perform_koenig_lookup (postfix_expression, args,
6265                                                      complain);
6266                       }
6267                     else
6268                       postfix_expression
6269                         = unqualified_fn_lookup_error (postfix_expression);
6270                   }
6271                 /* We do not perform argument-dependent lookup if
6272                    normal lookup finds a non-function, in accordance
6273                    with the expected resolution of DR 218.  */
6274                 else if (!args->is_empty ()
6275                          && is_overloaded_fn (postfix_expression))
6276                   {
6277                     tree fn = get_first_fn (postfix_expression);
6278                     fn = STRIP_TEMPLATE (fn);
6279
6280                     /* Do not do argument dependent lookup if regular
6281                        lookup finds a member function or a block-scope
6282                        function declaration.  [basic.lookup.argdep]/3  */
6283                     if (!DECL_FUNCTION_MEMBER_P (fn)
6284                         && !DECL_LOCAL_FUNCTION_P (fn))
6285                       {
6286                         koenig_p = true;
6287                         if (!any_type_dependent_arguments_p (args))
6288                           postfix_expression
6289                             = perform_koenig_lookup (postfix_expression, args,
6290                                                      complain);
6291                       }
6292                   }
6293               }
6294
6295             if (warn_memset_transposed_args)
6296               {
6297                 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6298                     && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6299                     && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6300                     && vec_safe_length (args) == 3
6301                     && integer_zerop ((*args)[2])
6302                     && LITERAL_ZERO_P ((*args)[2])
6303                     && !(integer_zerop ((*args)[1])
6304                          && LITERAL_ZERO_P ((*args)[1])))
6305                   warning (OPT_Wmemset_transposed_args,
6306                            "%<memset%> used with constant zero length "
6307                            "parameter; this could be due to transposed "
6308                            "parameters");
6309
6310                 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6311                    to avoid leaking those into folder and middle-end.  */
6312                 unsigned int i;
6313                 tree arg;
6314                 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6315                   if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6316                     (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6317               }
6318
6319             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6320               {
6321                 tree instance = TREE_OPERAND (postfix_expression, 0);
6322                 tree fn = TREE_OPERAND (postfix_expression, 1);
6323
6324                 if (processing_template_decl
6325                     && (type_dependent_expression_p (instance)
6326                         || (!BASELINK_P (fn)
6327                             && TREE_CODE (fn) != FIELD_DECL)
6328                         || type_dependent_expression_p (fn)
6329                         || any_type_dependent_arguments_p (args)))
6330                   {
6331                     postfix_expression
6332                       = build_nt_call_vec (postfix_expression, args);
6333                     release_tree_vector (args);
6334                     break;
6335                   }
6336
6337                 if (BASELINK_P (fn))
6338                   {
6339                   postfix_expression
6340                     = (build_new_method_call
6341                        (instance, fn, &args, NULL_TREE,
6342                         (idk == CP_ID_KIND_QUALIFIED
6343                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6344                          : LOOKUP_NORMAL),
6345                         /*fn_p=*/NULL,
6346                         complain));
6347                   }
6348                 else
6349                   postfix_expression
6350                     = finish_call_expr (postfix_expression, &args,
6351                                         /*disallow_virtual=*/false,
6352                                         /*koenig_p=*/false,
6353                                         complain);
6354               }
6355             else if (TREE_CODE (postfix_expression) == OFFSET_REF
6356                      || TREE_CODE (postfix_expression) == MEMBER_REF
6357                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6358               postfix_expression = (build_offset_ref_call_from_tree
6359                                     (postfix_expression, &args,
6360                                      complain));
6361             else if (idk == CP_ID_KIND_QUALIFIED)
6362               /* A call to a static class member, or a namespace-scope
6363                  function.  */
6364               postfix_expression
6365                 = finish_call_expr (postfix_expression, &args,
6366                                     /*disallow_virtual=*/true,
6367                                     koenig_p,
6368                                     complain);
6369             else
6370               /* All other function calls.  */
6371               postfix_expression
6372                 = finish_call_expr (postfix_expression, &args,
6373                                     /*disallow_virtual=*/false,
6374                                     koenig_p,
6375                                     complain);
6376
6377             protected_set_expr_location (postfix_expression, token->location);
6378
6379             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
6380             idk = CP_ID_KIND_NONE;
6381
6382             release_tree_vector (args);
6383           }
6384           break;
6385
6386         case CPP_DOT:
6387         case CPP_DEREF:
6388           /* postfix-expression . template [opt] id-expression
6389              postfix-expression . pseudo-destructor-name
6390              postfix-expression -> template [opt] id-expression
6391              postfix-expression -> pseudo-destructor-name */
6392
6393           /* Consume the `.' or `->' operator.  */
6394           cp_lexer_consume_token (parser->lexer);
6395
6396           postfix_expression
6397             = cp_parser_postfix_dot_deref_expression (parser, token->type,
6398                                                       postfix_expression,
6399                                                       false, &idk, loc);
6400
6401           is_member_access = true;
6402           break;
6403
6404         case CPP_PLUS_PLUS:
6405           /* postfix-expression ++  */
6406           /* Consume the `++' token.  */
6407           cp_lexer_consume_token (parser->lexer);
6408           /* Generate a representation for the complete expression.  */
6409           postfix_expression
6410             = finish_increment_expr (postfix_expression,
6411                                      POSTINCREMENT_EXPR);
6412           /* Increments may not appear in constant-expressions.  */
6413           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6414             postfix_expression = error_mark_node;
6415           idk = CP_ID_KIND_NONE;
6416           is_member_access = false;
6417           break;
6418
6419         case CPP_MINUS_MINUS:
6420           /* postfix-expression -- */
6421           /* Consume the `--' token.  */
6422           cp_lexer_consume_token (parser->lexer);
6423           /* Generate a representation for the complete expression.  */
6424           postfix_expression
6425             = finish_increment_expr (postfix_expression,
6426                                      POSTDECREMENT_EXPR);
6427           /* Decrements may not appear in constant-expressions.  */
6428           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6429             postfix_expression = error_mark_node;
6430           idk = CP_ID_KIND_NONE;
6431           is_member_access = false;
6432           break;
6433
6434         default:
6435           if (pidk_return != NULL)
6436             * pidk_return = idk;
6437           if (member_access_only_p)
6438             return is_member_access? postfix_expression : error_mark_node;
6439           else
6440             return postfix_expression;
6441         }
6442     }
6443
6444   /* We should never get here.  */
6445   gcc_unreachable ();
6446   return error_mark_node;
6447 }
6448
6449 /* This function parses Cilk Plus array notations.  If a normal array expr. is
6450    parsed then the array index is passed back to the caller through *INIT_INDEX 
6451    and the function returns a NULL_TREE.  If array notation expr. is parsed, 
6452    then *INIT_INDEX is ignored by the caller and the function returns 
6453    a tree of type ARRAY_NOTATION_REF.  If some error occurred it returns 
6454    error_mark_node.  */
6455
6456 static tree
6457 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6458                           tree array_value)
6459 {
6460   cp_token *token = NULL;
6461   tree length_index, stride = NULL_TREE, value_tree, array_type;
6462   if (!array_value || array_value == error_mark_node)
6463     {
6464       cp_parser_skip_to_end_of_statement (parser);
6465       return error_mark_node;
6466     }
6467
6468   array_type = TREE_TYPE (array_value);
6469   
6470   bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6471   parser->colon_corrects_to_scope_p = false;
6472   token = cp_lexer_peek_token (parser->lexer);
6473   
6474   if (!token)
6475     {
6476       cp_parser_error (parser, "expected %<:%> or numeral");
6477       return error_mark_node;
6478     }
6479   else if (token->type == CPP_COLON)
6480     {
6481       /* Consume the ':'.  */
6482       cp_lexer_consume_token (parser->lexer);
6483       
6484       /* If we are here, then we have a case like this A[:].  */
6485       if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6486         {
6487           cp_parser_error (parser, "expected %<]%>");
6488           cp_parser_skip_to_end_of_statement (parser);
6489           return error_mark_node;
6490         }
6491       *init_index = NULL_TREE;
6492       stride = NULL_TREE;
6493       length_index = NULL_TREE;
6494     }
6495   else
6496     {
6497       /* If we are here, then there are three valid possibilities:
6498          1. ARRAY [ EXP ]
6499          2. ARRAY [ EXP : EXP ]
6500          3. ARRAY [ EXP : EXP : EXP ]  */
6501
6502       *init_index = cp_parser_expression (parser);      
6503       if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6504         {  
6505           /* This indicates that we have a normal array expression.  */
6506           parser->colon_corrects_to_scope_p = saved_colon_corrects;
6507           return NULL_TREE;
6508         }
6509       
6510       /* Consume the ':'.  */
6511       cp_lexer_consume_token (parser->lexer);
6512       length_index = cp_parser_expression (parser);
6513       if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6514         {
6515           cp_lexer_consume_token (parser->lexer);
6516           stride = cp_parser_expression (parser);
6517         }
6518     }
6519   parser->colon_corrects_to_scope_p = saved_colon_corrects;
6520
6521   if (*init_index == error_mark_node || length_index == error_mark_node
6522       || stride == error_mark_node || array_type == error_mark_node)
6523     {
6524       if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6525         cp_lexer_consume_token (parser->lexer);
6526       return error_mark_node;
6527     }
6528   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6529
6530   value_tree = build_array_notation_ref (loc, array_value, *init_index, 
6531                                          length_index, stride, array_type);
6532   return value_tree;
6533 }
6534
6535 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6536    by cp_parser_builtin_offsetof.  We're looking for
6537
6538      postfix-expression [ expression ]
6539      postfix-expression [ braced-init-list ] (C++11)
6540
6541    FOR_OFFSETOF is set if we're being called in that context, which
6542    changes how we deal with integer constant expressions.  */
6543
6544 static tree
6545 cp_parser_postfix_open_square_expression (cp_parser *parser,
6546                                           tree postfix_expression,
6547                                           bool for_offsetof,
6548                                           bool decltype_p)
6549 {
6550   tree index = NULL_TREE;
6551   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6552   bool saved_greater_than_is_operator_p;
6553
6554   /* Consume the `[' token.  */
6555   cp_lexer_consume_token (parser->lexer);
6556
6557   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6558   parser->greater_than_is_operator_p = true;
6559
6560   /* Parse the index expression.  */
6561   /* ??? For offsetof, there is a question of what to allow here.  If
6562      offsetof is not being used in an integral constant expression context,
6563      then we *could* get the right answer by computing the value at runtime.
6564      If we are in an integral constant expression context, then we might
6565      could accept any constant expression; hard to say without analysis.
6566      Rather than open the barn door too wide right away, allow only integer
6567      constant expressions here.  */
6568   if (for_offsetof)
6569     index = cp_parser_constant_expression (parser);
6570   else
6571     {
6572       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6573         {
6574           bool expr_nonconst_p;
6575           cp_lexer_set_source_position (parser->lexer);
6576           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6577           index = cp_parser_braced_list (parser, &expr_nonconst_p);
6578           if (flag_cilkplus
6579               && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6580             {
6581               error_at (cp_lexer_peek_token (parser->lexer)->location,
6582                         "braced list index is not allowed with array "
6583                         "notation");
6584               cp_parser_skip_to_end_of_statement (parser);
6585               return error_mark_node;
6586             }
6587         }
6588       else if (flag_cilkplus)
6589         {
6590           /* Here are have these two options:
6591              ARRAY[EXP : EXP]        - Array notation expr with default
6592              stride of 1.
6593              ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6594              stride.  */
6595           tree an_exp = cp_parser_array_notation (loc, parser, &index, 
6596                                                   postfix_expression);
6597           if (an_exp)
6598             return an_exp;
6599         }
6600       else
6601         index = cp_parser_expression (parser);
6602     }
6603
6604   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6605
6606   /* Look for the closing `]'.  */
6607   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6608
6609   /* Build the ARRAY_REF.  */
6610   postfix_expression = grok_array_decl (loc, postfix_expression,
6611                                         index, decltype_p);
6612
6613   /* When not doing offsetof, array references are not permitted in
6614      constant-expressions.  */
6615   if (!for_offsetof
6616       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6617     postfix_expression = error_mark_node;
6618
6619   return postfix_expression;
6620 }
6621
6622 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6623    by cp_parser_builtin_offsetof.  We're looking for
6624
6625      postfix-expression . template [opt] id-expression
6626      postfix-expression . pseudo-destructor-name
6627      postfix-expression -> template [opt] id-expression
6628      postfix-expression -> pseudo-destructor-name
6629
6630    FOR_OFFSETOF is set if we're being called in that context.  That sorta
6631    limits what of the above we'll actually accept, but nevermind.
6632    TOKEN_TYPE is the "." or "->" token, which will already have been
6633    removed from the stream.  */
6634
6635 static tree
6636 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6637                                         enum cpp_ttype token_type,
6638                                         tree postfix_expression,
6639                                         bool for_offsetof, cp_id_kind *idk,
6640                                         location_t location)
6641 {
6642   tree name;
6643   bool dependent_p;
6644   bool pseudo_destructor_p;
6645   tree scope = NULL_TREE;
6646
6647   /* If this is a `->' operator, dereference the pointer.  */
6648   if (token_type == CPP_DEREF)
6649     postfix_expression = build_x_arrow (location, postfix_expression,
6650                                         tf_warning_or_error);
6651   /* Check to see whether or not the expression is type-dependent.  */
6652   dependent_p = type_dependent_expression_p (postfix_expression);
6653   /* The identifier following the `->' or `.' is not qualified.  */
6654   parser->scope = NULL_TREE;
6655   parser->qualifying_scope = NULL_TREE;
6656   parser->object_scope = NULL_TREE;
6657   *idk = CP_ID_KIND_NONE;
6658
6659   /* Enter the scope corresponding to the type of the object
6660      given by the POSTFIX_EXPRESSION.  */
6661   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6662     {
6663       scope = TREE_TYPE (postfix_expression);
6664       /* According to the standard, no expression should ever have
6665          reference type.  Unfortunately, we do not currently match
6666          the standard in this respect in that our internal representation
6667          of an expression may have reference type even when the standard
6668          says it does not.  Therefore, we have to manually obtain the
6669          underlying type here.  */
6670       scope = non_reference (scope);
6671       /* The type of the POSTFIX_EXPRESSION must be complete.  */
6672       if (scope == unknown_type_node)
6673         {
6674           error_at (location, "%qE does not have class type",
6675                     postfix_expression);
6676           scope = NULL_TREE;
6677         }
6678       /* Unlike the object expression in other contexts, *this is not
6679          required to be of complete type for purposes of class member
6680          access (5.2.5) outside the member function body.  */
6681       else if (postfix_expression != current_class_ref
6682                && !(processing_template_decl && scope == current_class_type))
6683         scope = complete_type_or_else (scope, NULL_TREE);
6684       /* Let the name lookup machinery know that we are processing a
6685          class member access expression.  */
6686       parser->context->object_type = scope;
6687       /* If something went wrong, we want to be able to discern that case,
6688          as opposed to the case where there was no SCOPE due to the type
6689          of expression being dependent.  */
6690       if (!scope)
6691         scope = error_mark_node;
6692       /* If the SCOPE was erroneous, make the various semantic analysis
6693          functions exit quickly -- and without issuing additional error
6694          messages.  */
6695       if (scope == error_mark_node)
6696         postfix_expression = error_mark_node;
6697     }
6698
6699   /* Assume this expression is not a pseudo-destructor access.  */
6700   pseudo_destructor_p = false;
6701
6702   /* If the SCOPE is a scalar type, then, if this is a valid program,
6703      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
6704      is type dependent, it can be pseudo-destructor-name or something else.
6705      Try to parse it as pseudo-destructor-name first.  */
6706   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6707     {
6708       tree s;
6709       tree type;
6710
6711       cp_parser_parse_tentatively (parser);
6712       /* Parse the pseudo-destructor-name.  */
6713       s = NULL_TREE;
6714       cp_parser_pseudo_destructor_name (parser, postfix_expression,
6715                                         &s, &type);
6716       if (dependent_p
6717           && (cp_parser_error_occurred (parser)
6718               || !SCALAR_TYPE_P (type)))
6719         cp_parser_abort_tentative_parse (parser);
6720       else if (cp_parser_parse_definitely (parser))
6721         {
6722           pseudo_destructor_p = true;
6723           postfix_expression
6724             = finish_pseudo_destructor_expr (postfix_expression,
6725                                              s, type, location);
6726         }
6727     }
6728
6729   if (!pseudo_destructor_p)
6730     {
6731       /* If the SCOPE is not a scalar type, we are looking at an
6732          ordinary class member access expression, rather than a
6733          pseudo-destructor-name.  */
6734       bool template_p;
6735       cp_token *token = cp_lexer_peek_token (parser->lexer);
6736       /* Parse the id-expression.  */
6737       name = (cp_parser_id_expression
6738               (parser,
6739                cp_parser_optional_template_keyword (parser),
6740                /*check_dependency_p=*/true,
6741                &template_p,
6742                /*declarator_p=*/false,
6743                /*optional_p=*/false));
6744       /* In general, build a SCOPE_REF if the member name is qualified.
6745          However, if the name was not dependent and has already been
6746          resolved; there is no need to build the SCOPE_REF.  For example;
6747
6748              struct X { void f(); };
6749              template <typename T> void f(T* t) { t->X::f(); }
6750
6751          Even though "t" is dependent, "X::f" is not and has been resolved
6752          to a BASELINK; there is no need to include scope information.  */
6753
6754       /* But we do need to remember that there was an explicit scope for
6755          virtual function calls.  */
6756       if (parser->scope)
6757         *idk = CP_ID_KIND_QUALIFIED;
6758
6759       /* If the name is a template-id that names a type, we will get a
6760          TYPE_DECL here.  That is invalid code.  */
6761       if (TREE_CODE (name) == TYPE_DECL)
6762         {
6763           error_at (token->location, "invalid use of %qD", name);
6764           postfix_expression = error_mark_node;
6765         }
6766       else
6767         {
6768           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6769             {
6770               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6771                 {
6772                   error_at (token->location, "%<%D::%D%> is not a class member",
6773                             parser->scope, name);
6774                   postfix_expression = error_mark_node;
6775                 }
6776               else
6777                 name = build_qualified_name (/*type=*/NULL_TREE,
6778                                              parser->scope,
6779                                              name,
6780                                              template_p);
6781               parser->scope = NULL_TREE;
6782               parser->qualifying_scope = NULL_TREE;
6783               parser->object_scope = NULL_TREE;
6784             }
6785           if (parser->scope && name && BASELINK_P (name))
6786             adjust_result_of_qualified_name_lookup
6787               (name, parser->scope, scope);
6788           postfix_expression
6789             = finish_class_member_access_expr (postfix_expression, name,
6790                                                template_p, 
6791                                                tf_warning_or_error);
6792         }
6793     }
6794
6795   /* We no longer need to look up names in the scope of the object on
6796      the left-hand side of the `.' or `->' operator.  */
6797   parser->context->object_type = NULL_TREE;
6798
6799   /* Outside of offsetof, these operators may not appear in
6800      constant-expressions.  */
6801   if (!for_offsetof
6802       && (cp_parser_non_integral_constant_expression
6803           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6804     postfix_expression = error_mark_node;
6805
6806   return postfix_expression;
6807 }
6808
6809 /* Cache of LITERAL_ZERO_P constants.  */
6810
6811 static GTY(()) tree literal_zeros[itk_none];
6812
6813 /* Parse a parenthesized expression-list.
6814
6815    expression-list:
6816      assignment-expression
6817      expression-list, assignment-expression
6818
6819    attribute-list:
6820      expression-list
6821      identifier
6822      identifier, expression-list
6823
6824    CAST_P is true if this expression is the target of a cast.
6825
6826    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6827    argument pack.
6828
6829    Returns a vector of trees.  Each element is a representation of an
6830    assignment-expression.  NULL is returned if the ( and or ) are
6831    missing.  An empty, but allocated, vector is returned on no
6832    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6833    if we are parsing an attribute list for an attribute that wants a
6834    plain identifier argument, normal_attr for an attribute that wants
6835    an expression, or non_attr if we aren't parsing an attribute list.  If
6836    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6837    not all of the expressions in the list were constant.
6838    WANT_LITERAL_ZERO_P is true if the caller is interested in
6839    LITERAL_ZERO_P INTEGER_CSTs.  FIXME: once we don't fold everything
6840    immediately, this can be removed.  */
6841
6842 static vec<tree, va_gc> *
6843 cp_parser_parenthesized_expression_list (cp_parser* parser,
6844                                          int is_attribute_list,
6845                                          bool cast_p,
6846                                          bool allow_expansion_p,
6847                                          bool *non_constant_p,
6848                                          bool want_literal_zero_p)
6849 {
6850   vec<tree, va_gc> *expression_list;
6851   bool fold_expr_p = is_attribute_list != non_attr;
6852   tree identifier = NULL_TREE;
6853   bool saved_greater_than_is_operator_p;
6854
6855   /* Assume all the expressions will be constant.  */
6856   if (non_constant_p)
6857     *non_constant_p = false;
6858
6859   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6860     return NULL;
6861
6862   expression_list = make_tree_vector ();
6863
6864   /* Within a parenthesized expression, a `>' token is always
6865      the greater-than operator.  */
6866   saved_greater_than_is_operator_p
6867     = parser->greater_than_is_operator_p;
6868   parser->greater_than_is_operator_p = true;
6869
6870   /* Consume expressions until there are no more.  */
6871   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6872     while (true)
6873       {
6874         tree expr;
6875
6876         /* At the beginning of attribute lists, check to see if the
6877            next token is an identifier.  */
6878         if (is_attribute_list == id_attr
6879             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6880           {
6881             cp_token *token;
6882
6883             /* Consume the identifier.  */
6884             token = cp_lexer_consume_token (parser->lexer);
6885             /* Save the identifier.  */
6886             identifier = token->u.value;
6887           }
6888         else
6889           {
6890             bool expr_non_constant_p;
6891
6892             /* Parse the next assignment-expression.  */
6893             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6894               {
6895                 /* A braced-init-list.  */
6896                 cp_lexer_set_source_position (parser->lexer);
6897                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6898                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6899                 if (non_constant_p && expr_non_constant_p)
6900                   *non_constant_p = true;
6901               }
6902             else if (non_constant_p)
6903               {
6904                 expr = (cp_parser_constant_expression
6905                         (parser, /*allow_non_constant_p=*/true,
6906                          &expr_non_constant_p));
6907                 if (expr_non_constant_p)
6908                   *non_constant_p = true;
6909               }
6910             else
6911               {
6912                 expr = NULL_TREE;
6913                 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6914                 switch (tok->type)
6915                   {
6916                   case CPP_NUMBER:
6917                   case CPP_CHAR:
6918                   case CPP_WCHAR:
6919                   case CPP_CHAR16:
6920                   case CPP_CHAR32:
6921                     /* If a parameter is literal zero alone, remember it
6922                        for -Wmemset-transposed-args warning.  */
6923                     if (integer_zerop (tok->u.value)
6924                         && !TREE_OVERFLOW (tok->u.value)
6925                         && want_literal_zero_p
6926                         && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6927                             == CPP_COMMA
6928                             || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6929                                == CPP_CLOSE_PAREN))
6930                       {
6931                         unsigned int i;
6932                         for (i = 0; i < itk_none; ++i)
6933                           if (TREE_TYPE (tok->u.value) == integer_types[i])
6934                             break;
6935                         if (i < itk_none && literal_zeros[i])
6936                           expr = literal_zeros[i];
6937                         else
6938                           {
6939                             expr = copy_node (tok->u.value);
6940                             LITERAL_ZERO_P (expr) = 1;
6941                             if (i < itk_none)
6942                               literal_zeros[i] = expr;
6943                           }
6944                         /* Consume the 0 token (or '\0', 0LL etc.).  */
6945                         cp_lexer_consume_token (parser->lexer);
6946                       }
6947                     break;
6948                   default:
6949                     break;
6950                   }
6951                 if (expr == NULL_TREE)
6952                   expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6953                                                           cast_p);
6954               }
6955
6956             if (fold_expr_p)
6957               expr = instantiate_non_dependent_expr (expr);
6958
6959             /* If we have an ellipsis, then this is an expression
6960                expansion.  */
6961             if (allow_expansion_p
6962                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6963               {
6964                 /* Consume the `...'.  */
6965                 cp_lexer_consume_token (parser->lexer);
6966
6967                 /* Build the argument pack.  */
6968                 expr = make_pack_expansion (expr);
6969               }
6970
6971              /* Add it to the list.  We add error_mark_node
6972                 expressions to the list, so that we can still tell if
6973                 the correct form for a parenthesized expression-list
6974                 is found. That gives better errors.  */
6975             vec_safe_push (expression_list, expr);
6976
6977             if (expr == error_mark_node)
6978               goto skip_comma;
6979           }
6980
6981         /* After the first item, attribute lists look the same as
6982            expression lists.  */
6983         is_attribute_list = non_attr;
6984
6985       get_comma:;
6986         /* If the next token isn't a `,', then we are done.  */
6987         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6988           break;
6989
6990         /* Otherwise, consume the `,' and keep going.  */
6991         cp_lexer_consume_token (parser->lexer);
6992       }
6993
6994   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6995     {
6996       int ending;
6997
6998     skip_comma:;
6999       /* We try and resync to an unnested comma, as that will give the
7000          user better diagnostics.  */
7001       ending = cp_parser_skip_to_closing_parenthesis (parser,
7002                                                       /*recovering=*/true,
7003                                                       /*or_comma=*/true,
7004                                                       /*consume_paren=*/true);
7005       if (ending < 0)
7006         goto get_comma;
7007       if (!ending)
7008         {
7009           parser->greater_than_is_operator_p
7010             = saved_greater_than_is_operator_p;
7011           return NULL;
7012         }
7013     }
7014
7015   parser->greater_than_is_operator_p
7016     = saved_greater_than_is_operator_p;
7017
7018   if (identifier)
7019     vec_safe_insert (expression_list, 0, identifier);
7020
7021   return expression_list;
7022 }
7023
7024 /* Parse a pseudo-destructor-name.
7025
7026    pseudo-destructor-name:
7027      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7028      :: [opt] nested-name-specifier template template-id :: ~ type-name
7029      :: [opt] nested-name-specifier [opt] ~ type-name
7030
7031    If either of the first two productions is used, sets *SCOPE to the
7032    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7033    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7034    or ERROR_MARK_NODE if the parse fails.  */
7035
7036 static void
7037 cp_parser_pseudo_destructor_name (cp_parser* parser,
7038                                   tree object,
7039                                   tree* scope,
7040                                   tree* type)
7041 {
7042   bool nested_name_specifier_p;
7043
7044   /* Handle ~auto.  */
7045   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7046       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7047       && !type_dependent_expression_p (object))
7048     {
7049       if (cxx_dialect < cxx14)
7050         pedwarn (input_location, 0,
7051                  "%<~auto%> only available with "
7052                  "-std=c++14 or -std=gnu++14");
7053       cp_lexer_consume_token (parser->lexer);
7054       cp_lexer_consume_token (parser->lexer);
7055       *scope = NULL_TREE;
7056       *type = TREE_TYPE (object);
7057       return;
7058     }
7059
7060   /* Assume that things will not work out.  */
7061   *type = error_mark_node;
7062
7063   /* Look for the optional `::' operator.  */
7064   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7065   /* Look for the optional nested-name-specifier.  */
7066   nested_name_specifier_p
7067     = (cp_parser_nested_name_specifier_opt (parser,
7068                                             /*typename_keyword_p=*/false,
7069                                             /*check_dependency_p=*/true,
7070                                             /*type_p=*/false,
7071                                             /*is_declaration=*/false)
7072        != NULL_TREE);
7073   /* Now, if we saw a nested-name-specifier, we might be doing the
7074      second production.  */
7075   if (nested_name_specifier_p
7076       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7077     {
7078       /* Consume the `template' keyword.  */
7079       cp_lexer_consume_token (parser->lexer);
7080       /* Parse the template-id.  */
7081       cp_parser_template_id (parser,
7082                              /*template_keyword_p=*/true,
7083                              /*check_dependency_p=*/false,
7084                              class_type,
7085                              /*is_declaration=*/true);
7086       /* Look for the `::' token.  */
7087       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7088     }
7089   /* If the next token is not a `~', then there might be some
7090      additional qualification.  */
7091   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7092     {
7093       /* At this point, we're looking for "type-name :: ~".  The type-name
7094          must not be a class-name, since this is a pseudo-destructor.  So,
7095          it must be either an enum-name, or a typedef-name -- both of which
7096          are just identifiers.  So, we peek ahead to check that the "::"
7097          and "~" tokens are present; if they are not, then we can avoid
7098          calling type_name.  */
7099       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7100           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7101           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7102         {
7103           cp_parser_error (parser, "non-scalar type");
7104           return;
7105         }
7106
7107       /* Look for the type-name.  */
7108       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7109       if (*scope == error_mark_node)
7110         return;
7111
7112       /* Look for the `::' token.  */
7113       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7114     }
7115   else
7116     *scope = NULL_TREE;
7117
7118   /* Look for the `~'.  */
7119   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7120
7121   /* Once we see the ~, this has to be a pseudo-destructor.  */
7122   if (!processing_template_decl && !cp_parser_error_occurred (parser))
7123     cp_parser_commit_to_topmost_tentative_parse (parser);
7124
7125   /* Look for the type-name again.  We are not responsible for
7126      checking that it matches the first type-name.  */
7127   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7128 }
7129
7130 /* Parse a unary-expression.
7131
7132    unary-expression:
7133      postfix-expression
7134      ++ cast-expression
7135      -- cast-expression
7136      unary-operator cast-expression
7137      sizeof unary-expression
7138      sizeof ( type-id )
7139      alignof ( type-id )  [C++0x]
7140      new-expression
7141      delete-expression
7142
7143    GNU Extensions:
7144
7145    unary-expression:
7146      __extension__ cast-expression
7147      __alignof__ unary-expression
7148      __alignof__ ( type-id )
7149      alignof unary-expression  [C++0x]
7150      __real__ cast-expression
7151      __imag__ cast-expression
7152      && identifier
7153      sizeof ( type-id ) { initializer-list , [opt] }
7154      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7155      __alignof__ ( type-id ) { initializer-list , [opt] }
7156
7157    ADDRESS_P is true iff the unary-expression is appearing as the
7158    operand of the `&' operator.   CAST_P is true if this expression is
7159    the target of a cast.
7160
7161    Returns a representation of the expression.  */
7162
7163 static tree
7164 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7165                             bool address_p, bool cast_p, bool decltype_p)
7166 {
7167   cp_token *token;
7168   enum tree_code unary_operator;
7169
7170   /* Peek at the next token.  */
7171   token = cp_lexer_peek_token (parser->lexer);
7172   /* Some keywords give away the kind of expression.  */
7173   if (token->type == CPP_KEYWORD)
7174     {
7175       enum rid keyword = token->keyword;
7176
7177       switch (keyword)
7178         {
7179         case RID_ALIGNOF:
7180         case RID_SIZEOF:
7181           {
7182             tree operand, ret;
7183             enum tree_code op;
7184             location_t first_loc;
7185
7186             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7187             /* Consume the token.  */
7188             cp_lexer_consume_token (parser->lexer);
7189             first_loc = cp_lexer_peek_token (parser->lexer)->location;
7190             /* Parse the operand.  */
7191             operand = cp_parser_sizeof_operand (parser, keyword);
7192
7193             if (TYPE_P (operand))
7194               ret = cxx_sizeof_or_alignof_type (operand, op, true);
7195             else
7196               {
7197                 /* ISO C++ defines alignof only with types, not with
7198                    expressions. So pedwarn if alignof is used with a non-
7199                    type expression. However, __alignof__ is ok.  */
7200                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7201                   pedwarn (token->location, OPT_Wpedantic,
7202                            "ISO C++ does not allow %<alignof%> "
7203                            "with a non-type");
7204
7205                 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7206               }
7207             /* For SIZEOF_EXPR, just issue diagnostics, but keep
7208                SIZEOF_EXPR with the original operand.  */
7209             if (op == SIZEOF_EXPR && ret != error_mark_node)
7210               {
7211                 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7212                   {
7213                     if (!processing_template_decl && TYPE_P (operand))
7214                       {
7215                         ret = build_min (SIZEOF_EXPR, size_type_node,
7216                                          build1 (NOP_EXPR, operand,
7217                                                  error_mark_node));
7218                         SIZEOF_EXPR_TYPE_P (ret) = 1;
7219                       }
7220                     else
7221                       ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7222                     TREE_SIDE_EFFECTS (ret) = 0;
7223                     TREE_READONLY (ret) = 1;
7224                   }
7225                 SET_EXPR_LOCATION (ret, first_loc);
7226               }
7227             return ret;
7228           }
7229
7230         case RID_NEW:
7231           return cp_parser_new_expression (parser);
7232
7233         case RID_DELETE:
7234           return cp_parser_delete_expression (parser);
7235
7236         case RID_EXTENSION:
7237           {
7238             /* The saved value of the PEDANTIC flag.  */
7239             int saved_pedantic;
7240             tree expr;
7241
7242             /* Save away the PEDANTIC flag.  */
7243             cp_parser_extension_opt (parser, &saved_pedantic);
7244             /* Parse the cast-expression.  */
7245             expr = cp_parser_simple_cast_expression (parser);
7246             /* Restore the PEDANTIC flag.  */
7247             pedantic = saved_pedantic;
7248
7249             return expr;
7250           }
7251
7252         case RID_REALPART:
7253         case RID_IMAGPART:
7254           {
7255             tree expression;
7256
7257             /* Consume the `__real__' or `__imag__' token.  */
7258             cp_lexer_consume_token (parser->lexer);
7259             /* Parse the cast-expression.  */
7260             expression = cp_parser_simple_cast_expression (parser);
7261             /* Create the complete representation.  */
7262             return build_x_unary_op (token->location,
7263                                      (keyword == RID_REALPART
7264                                       ? REALPART_EXPR : IMAGPART_EXPR),
7265                                      expression,
7266                                      tf_warning_or_error);
7267           }
7268           break;
7269
7270         case RID_TRANSACTION_ATOMIC:
7271         case RID_TRANSACTION_RELAXED:
7272           return cp_parser_transaction_expression (parser, keyword);
7273
7274         case RID_NOEXCEPT:
7275           {
7276             tree expr;
7277             const char *saved_message;
7278             bool saved_integral_constant_expression_p;
7279             bool saved_non_integral_constant_expression_p;
7280             bool saved_greater_than_is_operator_p;
7281
7282             cp_lexer_consume_token (parser->lexer);
7283             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7284
7285             saved_message = parser->type_definition_forbidden_message;
7286             parser->type_definition_forbidden_message
7287               = G_("types may not be defined in %<noexcept%> expressions");
7288
7289             saved_integral_constant_expression_p
7290               = parser->integral_constant_expression_p;
7291             saved_non_integral_constant_expression_p
7292               = parser->non_integral_constant_expression_p;
7293             parser->integral_constant_expression_p = false;
7294
7295             saved_greater_than_is_operator_p
7296               = parser->greater_than_is_operator_p;
7297             parser->greater_than_is_operator_p = true;
7298
7299             ++cp_unevaluated_operand;
7300             ++c_inhibit_evaluation_warnings;
7301             ++cp_noexcept_operand;
7302             expr = cp_parser_expression (parser);
7303             --cp_noexcept_operand;
7304             --c_inhibit_evaluation_warnings;
7305             --cp_unevaluated_operand;
7306
7307             parser->greater_than_is_operator_p
7308               = saved_greater_than_is_operator_p;
7309
7310             parser->integral_constant_expression_p
7311               = saved_integral_constant_expression_p;
7312             parser->non_integral_constant_expression_p
7313               = saved_non_integral_constant_expression_p;
7314
7315             parser->type_definition_forbidden_message = saved_message;
7316
7317             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7318             return finish_noexcept_expr (expr, tf_warning_or_error);
7319           }
7320
7321         default:
7322           break;
7323         }
7324     }
7325
7326   /* Look for the `:: new' and `:: delete', which also signal the
7327      beginning of a new-expression, or delete-expression,
7328      respectively.  If the next token is `::', then it might be one of
7329      these.  */
7330   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7331     {
7332       enum rid keyword;
7333
7334       /* See if the token after the `::' is one of the keywords in
7335          which we're interested.  */
7336       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7337       /* If it's `new', we have a new-expression.  */
7338       if (keyword == RID_NEW)
7339         return cp_parser_new_expression (parser);
7340       /* Similarly, for `delete'.  */
7341       else if (keyword == RID_DELETE)
7342         return cp_parser_delete_expression (parser);
7343     }
7344
7345   /* Look for a unary operator.  */
7346   unary_operator = cp_parser_unary_operator (token);
7347   /* The `++' and `--' operators can be handled similarly, even though
7348      they are not technically unary-operators in the grammar.  */
7349   if (unary_operator == ERROR_MARK)
7350     {
7351       if (token->type == CPP_PLUS_PLUS)
7352         unary_operator = PREINCREMENT_EXPR;
7353       else if (token->type == CPP_MINUS_MINUS)
7354         unary_operator = PREDECREMENT_EXPR;
7355       /* Handle the GNU address-of-label extension.  */
7356       else if (cp_parser_allow_gnu_extensions_p (parser)
7357                && token->type == CPP_AND_AND)
7358         {
7359           tree identifier;
7360           tree expression;
7361           location_t loc = token->location;
7362
7363           /* Consume the '&&' token.  */
7364           cp_lexer_consume_token (parser->lexer);
7365           /* Look for the identifier.  */
7366           identifier = cp_parser_identifier (parser);
7367           /* Create an expression representing the address.  */
7368           expression = finish_label_address_expr (identifier, loc);
7369           if (cp_parser_non_integral_constant_expression (parser,
7370                                                           NIC_ADDR_LABEL))
7371             expression = error_mark_node;
7372           return expression;
7373         }
7374     }
7375   if (unary_operator != ERROR_MARK)
7376     {
7377       tree cast_expression;
7378       tree expression = error_mark_node;
7379       non_integral_constant non_constant_p = NIC_NONE;
7380       location_t loc = token->location;
7381       tsubst_flags_t complain = complain_flags (decltype_p);
7382
7383       /* Consume the operator token.  */
7384       token = cp_lexer_consume_token (parser->lexer);
7385       /* Parse the cast-expression.  */
7386       cast_expression
7387         = cp_parser_cast_expression (parser,
7388                                      unary_operator == ADDR_EXPR,
7389                                      /*cast_p=*/false,
7390                                      /*decltype*/false,
7391                                      pidk);
7392       /* Now, build an appropriate representation.  */
7393       switch (unary_operator)
7394         {
7395         case INDIRECT_REF:
7396           non_constant_p = NIC_STAR;
7397           expression = build_x_indirect_ref (loc, cast_expression,
7398                                              RO_UNARY_STAR,
7399                                              complain);
7400           break;
7401
7402         case ADDR_EXPR:
7403            non_constant_p = NIC_ADDR;
7404           /* Fall through.  */
7405         case BIT_NOT_EXPR:
7406           expression = build_x_unary_op (loc, unary_operator,
7407                                          cast_expression,
7408                                          complain);
7409           break;
7410
7411         case PREINCREMENT_EXPR:
7412         case PREDECREMENT_EXPR:
7413           non_constant_p = unary_operator == PREINCREMENT_EXPR
7414                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7415           /* Fall through.  */
7416         case UNARY_PLUS_EXPR:
7417         case NEGATE_EXPR:
7418         case TRUTH_NOT_EXPR:
7419           expression = finish_unary_op_expr (loc, unary_operator,
7420                                              cast_expression, complain);
7421           break;
7422
7423         default:
7424           gcc_unreachable ();
7425         }
7426
7427       if (non_constant_p != NIC_NONE
7428           && cp_parser_non_integral_constant_expression (parser,
7429                                                          non_constant_p))
7430         expression = error_mark_node;
7431
7432       return expression;
7433     }
7434
7435   return cp_parser_postfix_expression (parser, address_p, cast_p,
7436                                        /*member_access_only_p=*/false,
7437                                        decltype_p,
7438                                        pidk);
7439 }
7440
7441 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
7442    unary-operator, the corresponding tree code is returned.  */
7443
7444 static enum tree_code
7445 cp_parser_unary_operator (cp_token* token)
7446 {
7447   switch (token->type)
7448     {
7449     case CPP_MULT:
7450       return INDIRECT_REF;
7451
7452     case CPP_AND:
7453       return ADDR_EXPR;
7454
7455     case CPP_PLUS:
7456       return UNARY_PLUS_EXPR;
7457
7458     case CPP_MINUS:
7459       return NEGATE_EXPR;
7460
7461     case CPP_NOT:
7462       return TRUTH_NOT_EXPR;
7463
7464     case CPP_COMPL:
7465       return BIT_NOT_EXPR;
7466
7467     default:
7468       return ERROR_MARK;
7469     }
7470 }
7471
7472 /* Parse a new-expression.
7473
7474    new-expression:
7475      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7476      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7477
7478    Returns a representation of the expression.  */
7479
7480 static tree
7481 cp_parser_new_expression (cp_parser* parser)
7482 {
7483   bool global_scope_p;
7484   vec<tree, va_gc> *placement;
7485   tree type;
7486   vec<tree, va_gc> *initializer;
7487   tree nelts = NULL_TREE;
7488   tree ret;
7489
7490   /* Look for the optional `::' operator.  */
7491   global_scope_p
7492     = (cp_parser_global_scope_opt (parser,
7493                                    /*current_scope_valid_p=*/false)
7494        != NULL_TREE);
7495   /* Look for the `new' operator.  */
7496   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7497   /* There's no easy way to tell a new-placement from the
7498      `( type-id )' construct.  */
7499   cp_parser_parse_tentatively (parser);
7500   /* Look for a new-placement.  */
7501   placement = cp_parser_new_placement (parser);
7502   /* If that didn't work out, there's no new-placement.  */
7503   if (!cp_parser_parse_definitely (parser))
7504     {
7505       if (placement != NULL)
7506         release_tree_vector (placement);
7507       placement = NULL;
7508     }
7509
7510   /* If the next token is a `(', then we have a parenthesized
7511      type-id.  */
7512   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7513     {
7514       cp_token *token;
7515       const char *saved_message = parser->type_definition_forbidden_message;
7516
7517       /* Consume the `('.  */
7518       cp_lexer_consume_token (parser->lexer);
7519
7520       /* Parse the type-id.  */
7521       parser->type_definition_forbidden_message
7522         = G_("types may not be defined in a new-expression");
7523       type = cp_parser_type_id (parser);
7524       parser->type_definition_forbidden_message = saved_message;
7525
7526       /* Look for the closing `)'.  */
7527       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7528       token = cp_lexer_peek_token (parser->lexer);
7529       /* There should not be a direct-new-declarator in this production,
7530          but GCC used to allowed this, so we check and emit a sensible error
7531          message for this case.  */
7532       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7533         {
7534           error_at (token->location,
7535                     "array bound forbidden after parenthesized type-id");
7536           inform (token->location, 
7537                   "try removing the parentheses around the type-id");
7538           cp_parser_direct_new_declarator (parser);
7539         }
7540     }
7541   /* Otherwise, there must be a new-type-id.  */
7542   else
7543     type = cp_parser_new_type_id (parser, &nelts);
7544
7545   /* If the next token is a `(' or '{', then we have a new-initializer.  */
7546   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7547       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7548     initializer = cp_parser_new_initializer (parser);
7549   else
7550     initializer = NULL;
7551
7552   /* A new-expression may not appear in an integral constant
7553      expression.  */
7554   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7555     ret = error_mark_node;
7556   else
7557     {
7558       /* Create a representation of the new-expression.  */
7559       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7560                        tf_warning_or_error);
7561     }
7562
7563   if (placement != NULL)
7564     release_tree_vector (placement);
7565   if (initializer != NULL)
7566     release_tree_vector (initializer);
7567
7568   return ret;
7569 }
7570
7571 /* Parse a new-placement.
7572
7573    new-placement:
7574      ( expression-list )
7575
7576    Returns the same representation as for an expression-list.  */
7577
7578 static vec<tree, va_gc> *
7579 cp_parser_new_placement (cp_parser* parser)
7580 {
7581   vec<tree, va_gc> *expression_list;
7582
7583   /* Parse the expression-list.  */
7584   expression_list = (cp_parser_parenthesized_expression_list
7585                      (parser, non_attr, /*cast_p=*/false,
7586                       /*allow_expansion_p=*/true,
7587                       /*non_constant_p=*/NULL));
7588
7589   return expression_list;
7590 }
7591
7592 /* Parse a new-type-id.
7593
7594    new-type-id:
7595      type-specifier-seq new-declarator [opt]
7596
7597    Returns the TYPE allocated.  If the new-type-id indicates an array
7598    type, *NELTS is set to the number of elements in the last array
7599    bound; the TYPE will not include the last array bound.  */
7600
7601 static tree
7602 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7603 {
7604   cp_decl_specifier_seq type_specifier_seq;
7605   cp_declarator *new_declarator;
7606   cp_declarator *declarator;
7607   cp_declarator *outer_declarator;
7608   const char *saved_message;
7609
7610   /* The type-specifier sequence must not contain type definitions.
7611      (It cannot contain declarations of new types either, but if they
7612      are not definitions we will catch that because they are not
7613      complete.)  */
7614   saved_message = parser->type_definition_forbidden_message;
7615   parser->type_definition_forbidden_message
7616     = G_("types may not be defined in a new-type-id");
7617   /* Parse the type-specifier-seq.  */
7618   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7619                                 /*is_trailing_return=*/false,
7620                                 &type_specifier_seq);
7621   /* Restore the old message.  */
7622   parser->type_definition_forbidden_message = saved_message;
7623
7624   if (type_specifier_seq.type == error_mark_node)
7625     return error_mark_node;
7626
7627   /* Parse the new-declarator.  */
7628   new_declarator = cp_parser_new_declarator_opt (parser);
7629
7630   /* Determine the number of elements in the last array dimension, if
7631      any.  */
7632   *nelts = NULL_TREE;
7633   /* Skip down to the last array dimension.  */
7634   declarator = new_declarator;
7635   outer_declarator = NULL;
7636   while (declarator && (declarator->kind == cdk_pointer
7637                         || declarator->kind == cdk_ptrmem))
7638     {
7639       outer_declarator = declarator;
7640       declarator = declarator->declarator;
7641     }
7642   while (declarator
7643          && declarator->kind == cdk_array
7644          && declarator->declarator
7645          && declarator->declarator->kind == cdk_array)
7646     {
7647       outer_declarator = declarator;
7648       declarator = declarator->declarator;
7649     }
7650
7651   if (declarator && declarator->kind == cdk_array)
7652     {
7653       *nelts = declarator->u.array.bounds;
7654       if (*nelts == error_mark_node)
7655         *nelts = integer_one_node;
7656
7657       if (outer_declarator)
7658         outer_declarator->declarator = declarator->declarator;
7659       else
7660         new_declarator = NULL;
7661     }
7662
7663   return groktypename (&type_specifier_seq, new_declarator, false);
7664 }
7665
7666 /* Parse an (optional) new-declarator.
7667
7668    new-declarator:
7669      ptr-operator new-declarator [opt]
7670      direct-new-declarator
7671
7672    Returns the declarator.  */
7673
7674 static cp_declarator *
7675 cp_parser_new_declarator_opt (cp_parser* parser)
7676 {
7677   enum tree_code code;
7678   tree type, std_attributes = NULL_TREE;
7679   cp_cv_quals cv_quals;  
7680
7681   /* We don't know if there's a ptr-operator next, or not.  */
7682   cp_parser_parse_tentatively (parser);
7683   /* Look for a ptr-operator.  */
7684   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7685   /* If that worked, look for more new-declarators.  */
7686   if (cp_parser_parse_definitely (parser))
7687     {
7688       cp_declarator *declarator;
7689
7690       /* Parse another optional declarator.  */
7691       declarator = cp_parser_new_declarator_opt (parser);
7692
7693       declarator = cp_parser_make_indirect_declarator
7694         (code, type, cv_quals, declarator, std_attributes);
7695
7696       return declarator;
7697     }
7698
7699   /* If the next token is a `[', there is a direct-new-declarator.  */
7700   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7701     return cp_parser_direct_new_declarator (parser);
7702
7703   return NULL;
7704 }
7705
7706 /* Parse a direct-new-declarator.
7707
7708    direct-new-declarator:
7709      [ expression ]
7710      direct-new-declarator [constant-expression]
7711
7712    */
7713
7714 static cp_declarator *
7715 cp_parser_direct_new_declarator (cp_parser* parser)
7716 {
7717   cp_declarator *declarator = NULL;
7718
7719   while (true)
7720     {
7721       tree expression;
7722       cp_token *token;
7723
7724       /* Look for the opening `['.  */
7725       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7726
7727       token = cp_lexer_peek_token (parser->lexer);
7728       expression = cp_parser_expression (parser);
7729       /* The standard requires that the expression have integral
7730          type.  DR 74 adds enumeration types.  We believe that the
7731          real intent is that these expressions be handled like the
7732          expression in a `switch' condition, which also allows
7733          classes with a single conversion to integral or
7734          enumeration type.  */
7735       if (!processing_template_decl)
7736         {
7737           expression
7738             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7739                                           expression,
7740                                           /*complain=*/true);
7741           if (!expression)
7742             {
7743               error_at (token->location,
7744                         "expression in new-declarator must have integral "
7745                         "or enumeration type");
7746               expression = error_mark_node;
7747             }
7748         }
7749
7750       /* Look for the closing `]'.  */
7751       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7752
7753       /* Add this bound to the declarator.  */
7754       declarator = make_array_declarator (declarator, expression);
7755
7756       /* If the next token is not a `[', then there are no more
7757          bounds.  */
7758       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7759         break;
7760     }
7761
7762   return declarator;
7763 }
7764
7765 /* Parse a new-initializer.
7766
7767    new-initializer:
7768      ( expression-list [opt] )
7769      braced-init-list
7770
7771    Returns a representation of the expression-list.  */
7772
7773 static vec<tree, va_gc> *
7774 cp_parser_new_initializer (cp_parser* parser)
7775 {
7776   vec<tree, va_gc> *expression_list;
7777
7778   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7779     {
7780       tree t;
7781       bool expr_non_constant_p;
7782       cp_lexer_set_source_position (parser->lexer);
7783       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7784       t = cp_parser_braced_list (parser, &expr_non_constant_p);
7785       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7786       expression_list = make_tree_vector_single (t);
7787     }
7788   else
7789     expression_list = (cp_parser_parenthesized_expression_list
7790                        (parser, non_attr, /*cast_p=*/false,
7791                         /*allow_expansion_p=*/true,
7792                         /*non_constant_p=*/NULL));
7793
7794   return expression_list;
7795 }
7796
7797 /* Parse a delete-expression.
7798
7799    delete-expression:
7800      :: [opt] delete cast-expression
7801      :: [opt] delete [ ] cast-expression
7802
7803    Returns a representation of the expression.  */
7804
7805 static tree
7806 cp_parser_delete_expression (cp_parser* parser)
7807 {
7808   bool global_scope_p;
7809   bool array_p;
7810   tree expression;
7811
7812   /* Look for the optional `::' operator.  */
7813   global_scope_p
7814     = (cp_parser_global_scope_opt (parser,
7815                                    /*current_scope_valid_p=*/false)
7816        != NULL_TREE);
7817   /* Look for the `delete' keyword.  */
7818   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7819   /* See if the array syntax is in use.  */
7820   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7821     {
7822       /* Consume the `[' token.  */
7823       cp_lexer_consume_token (parser->lexer);
7824       /* Look for the `]' token.  */
7825       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7826       /* Remember that this is the `[]' construct.  */
7827       array_p = true;
7828     }
7829   else
7830     array_p = false;
7831
7832   /* Parse the cast-expression.  */
7833   expression = cp_parser_simple_cast_expression (parser);
7834
7835   /* A delete-expression may not appear in an integral constant
7836      expression.  */
7837   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7838     return error_mark_node;
7839
7840   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7841                         tf_warning_or_error);
7842 }
7843
7844 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7845    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7846    0 otherwise.  */
7847
7848 static int
7849 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7850 {
7851   cp_token *token = cp_lexer_peek_token (parser->lexer);
7852   switch (token->type)
7853     {
7854     case CPP_COMMA:
7855     case CPP_SEMICOLON:
7856     case CPP_QUERY:
7857     case CPP_COLON:
7858     case CPP_CLOSE_SQUARE:
7859     case CPP_CLOSE_PAREN:
7860     case CPP_CLOSE_BRACE:
7861     case CPP_OPEN_BRACE:
7862     case CPP_DOT:
7863     case CPP_DOT_STAR:
7864     case CPP_DEREF:
7865     case CPP_DEREF_STAR:
7866     case CPP_DIV:
7867     case CPP_MOD:
7868     case CPP_LSHIFT:
7869     case CPP_RSHIFT:
7870     case CPP_LESS:
7871     case CPP_GREATER:
7872     case CPP_LESS_EQ:
7873     case CPP_GREATER_EQ:
7874     case CPP_EQ_EQ:
7875     case CPP_NOT_EQ:
7876     case CPP_EQ:
7877     case CPP_MULT_EQ:
7878     case CPP_DIV_EQ:
7879     case CPP_MOD_EQ:
7880     case CPP_PLUS_EQ:
7881     case CPP_MINUS_EQ:
7882     case CPP_RSHIFT_EQ:
7883     case CPP_LSHIFT_EQ:
7884     case CPP_AND_EQ:
7885     case CPP_XOR_EQ:
7886     case CPP_OR_EQ:
7887     case CPP_XOR:
7888     case CPP_OR:
7889     case CPP_OR_OR:
7890     case CPP_EOF:
7891     case CPP_ELLIPSIS:
7892       return 0;
7893
7894     case CPP_OPEN_PAREN:
7895       /* In ((type ()) () the last () isn't a valid cast-expression,
7896          so the whole must be parsed as postfix-expression.  */
7897       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7898              != CPP_CLOSE_PAREN;
7899
7900     case CPP_OPEN_SQUARE:
7901       /* '[' may start a primary-expression in obj-c++ and in C++11,
7902          as a lambda-expression, eg, '(void)[]{}'.  */
7903       if (cxx_dialect >= cxx11)
7904         return -1;
7905       return c_dialect_objc ();
7906
7907     case CPP_PLUS_PLUS:
7908     case CPP_MINUS_MINUS:
7909       /* '++' and '--' may or may not start a cast-expression:
7910
7911          struct T { void operator++(int); };
7912          void f() { (T())++; }
7913
7914          vs
7915
7916          int a;
7917          (int)++a;  */
7918       return -1;
7919
7920     default:
7921       return 1;
7922     }
7923 }
7924
7925 /* Parse a cast-expression.
7926
7927    cast-expression:
7928      unary-expression
7929      ( type-id ) cast-expression
7930
7931    ADDRESS_P is true iff the unary-expression is appearing as the
7932    operand of the `&' operator.   CAST_P is true if this expression is
7933    the target of a cast.
7934
7935    Returns a representation of the expression.  */
7936
7937 static tree
7938 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7939                            bool decltype_p, cp_id_kind * pidk)
7940 {
7941   /* If it's a `(', then we might be looking at a cast.  */
7942   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7943     {
7944       tree type = NULL_TREE;
7945       tree expr = NULL_TREE;
7946       int cast_expression = 0;
7947       const char *saved_message;
7948
7949       /* There's no way to know yet whether or not this is a cast.
7950          For example, `(int (3))' is a unary-expression, while `(int)
7951          3' is a cast.  So, we resort to parsing tentatively.  */
7952       cp_parser_parse_tentatively (parser);
7953       /* Types may not be defined in a cast.  */
7954       saved_message = parser->type_definition_forbidden_message;
7955       parser->type_definition_forbidden_message
7956         = G_("types may not be defined in casts");
7957       /* Consume the `('.  */
7958       cp_lexer_consume_token (parser->lexer);
7959       /* A very tricky bit is that `(struct S) { 3 }' is a
7960          compound-literal (which we permit in C++ as an extension).
7961          But, that construct is not a cast-expression -- it is a
7962          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7963          is legal; if the compound-literal were a cast-expression,
7964          you'd need an extra set of parentheses.)  But, if we parse
7965          the type-id, and it happens to be a class-specifier, then we
7966          will commit to the parse at that point, because we cannot
7967          undo the action that is done when creating a new class.  So,
7968          then we cannot back up and do a postfix-expression.
7969
7970          Another tricky case is the following (c++/29234):
7971
7972          struct S { void operator () (); };
7973
7974          void foo ()
7975          {
7976            ( S()() );
7977          }
7978
7979          As a type-id we parse the parenthesized S()() as a function
7980          returning a function, groktypename complains and we cannot
7981          back up in this case either.
7982
7983          Therefore, we scan ahead to the closing `)', and check to see
7984          if the tokens after the `)' can start a cast-expression.  Otherwise
7985          we are dealing with an unary-expression, a postfix-expression
7986          or something else.
7987
7988          Yet another tricky case, in C++11, is the following (c++/54891):
7989
7990          (void)[]{};
7991
7992          The issue is that usually, besides the case of lambda-expressions,
7993          the parenthesized type-id cannot be followed by '[', and, eg, we
7994          want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7995          Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7996          we don't commit, we try a cast-expression, then an unary-expression.
7997
7998          Save tokens so that we can put them back.  */
7999       cp_lexer_save_tokens (parser->lexer);
8000
8001       /* We may be looking at a cast-expression.  */
8002       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8003                                                  /*consume_paren=*/true))
8004         cast_expression
8005           = cp_parser_tokens_start_cast_expression (parser);
8006
8007       /* Roll back the tokens we skipped.  */
8008       cp_lexer_rollback_tokens (parser->lexer);
8009       /* If we aren't looking at a cast-expression, simulate an error so
8010          that the call to cp_parser_error_occurred below returns true.  */
8011       if (!cast_expression)
8012         cp_parser_simulate_error (parser);
8013       else
8014         {
8015           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8016           parser->in_type_id_in_expr_p = true;
8017           /* Look for the type-id.  */
8018           type = cp_parser_type_id (parser);
8019           /* Look for the closing `)'.  */
8020           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8021           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8022         }
8023
8024       /* Restore the saved message.  */
8025       parser->type_definition_forbidden_message = saved_message;
8026
8027       /* At this point this can only be either a cast or a
8028          parenthesized ctor such as `(T ())' that looks like a cast to
8029          function returning T.  */
8030       if (!cp_parser_error_occurred (parser))
8031         {
8032           /* Only commit if the cast-expression doesn't start with
8033              '++', '--', or '[' in C++11.  */
8034           if (cast_expression > 0)
8035             cp_parser_commit_to_topmost_tentative_parse (parser);
8036
8037           expr = cp_parser_cast_expression (parser,
8038                                             /*address_p=*/false,
8039                                             /*cast_p=*/true,
8040                                             /*decltype_p=*/false,
8041                                             pidk);
8042
8043           if (cp_parser_parse_definitely (parser))
8044             {
8045               /* Warn about old-style casts, if so requested.  */
8046               if (warn_old_style_cast
8047                   && !in_system_header_at (input_location)
8048                   && !VOID_TYPE_P (type)
8049                   && current_lang_name != lang_name_c)
8050                 warning (OPT_Wold_style_cast, "use of old-style cast");
8051
8052               /* Only type conversions to integral or enumeration types
8053                  can be used in constant-expressions.  */
8054               if (!cast_valid_in_integral_constant_expression_p (type)
8055                   && cp_parser_non_integral_constant_expression (parser,
8056                                                                  NIC_CAST))
8057                 return error_mark_node;
8058
8059               /* Perform the cast.  */
8060               expr = build_c_cast (input_location, type, expr);
8061               return expr;
8062             }
8063         }
8064       else 
8065         cp_parser_abort_tentative_parse (parser);
8066     }
8067
8068   /* If we get here, then it's not a cast, so it must be a
8069      unary-expression.  */
8070   return cp_parser_unary_expression (parser, pidk, address_p,
8071                                      cast_p, decltype_p);
8072 }
8073
8074 /* Parse a binary expression of the general form:
8075
8076    pm-expression:
8077      cast-expression
8078      pm-expression .* cast-expression
8079      pm-expression ->* cast-expression
8080
8081    multiplicative-expression:
8082      pm-expression
8083      multiplicative-expression * pm-expression
8084      multiplicative-expression / pm-expression
8085      multiplicative-expression % pm-expression
8086
8087    additive-expression:
8088      multiplicative-expression
8089      additive-expression + multiplicative-expression
8090      additive-expression - multiplicative-expression
8091
8092    shift-expression:
8093      additive-expression
8094      shift-expression << additive-expression
8095      shift-expression >> additive-expression
8096
8097    relational-expression:
8098      shift-expression
8099      relational-expression < shift-expression
8100      relational-expression > shift-expression
8101      relational-expression <= shift-expression
8102      relational-expression >= shift-expression
8103
8104   GNU Extension:
8105
8106    relational-expression:
8107      relational-expression <? shift-expression
8108      relational-expression >? shift-expression
8109
8110    equality-expression:
8111      relational-expression
8112      equality-expression == relational-expression
8113      equality-expression != relational-expression
8114
8115    and-expression:
8116      equality-expression
8117      and-expression & equality-expression
8118
8119    exclusive-or-expression:
8120      and-expression
8121      exclusive-or-expression ^ and-expression
8122
8123    inclusive-or-expression:
8124      exclusive-or-expression
8125      inclusive-or-expression | exclusive-or-expression
8126
8127    logical-and-expression:
8128      inclusive-or-expression
8129      logical-and-expression && inclusive-or-expression
8130
8131    logical-or-expression:
8132      logical-and-expression
8133      logical-or-expression || logical-and-expression
8134
8135    All these are implemented with a single function like:
8136
8137    binary-expression:
8138      simple-cast-expression
8139      binary-expression <token> binary-expression
8140
8141    CAST_P is true if this expression is the target of a cast.
8142
8143    The binops_by_token map is used to get the tree codes for each <token> type.
8144    binary-expressions are associated according to a precedence table.  */
8145
8146 #define TOKEN_PRECEDENCE(token)                              \
8147 (((token->type == CPP_GREATER                                \
8148    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8149   && !parser->greater_than_is_operator_p)                    \
8150  ? PREC_NOT_OPERATOR                                         \
8151  : binops_by_token[token->type].prec)
8152
8153 static tree
8154 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8155                              bool no_toplevel_fold_p,
8156                              bool decltype_p,
8157                              enum cp_parser_prec prec,
8158                              cp_id_kind * pidk)
8159 {
8160   cp_parser_expression_stack stack;
8161   cp_parser_expression_stack_entry *sp = &stack[0];
8162   cp_parser_expression_stack_entry current;
8163   tree rhs;
8164   cp_token *token;
8165   enum tree_code rhs_type;
8166   enum cp_parser_prec new_prec, lookahead_prec;
8167   tree overload;
8168
8169   /* Parse the first expression.  */
8170   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8171                       ? TRUTH_NOT_EXPR : ERROR_MARK);
8172   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8173                                            cast_p, decltype_p, pidk);
8174   current.prec = prec;
8175
8176   if (cp_parser_error_occurred (parser))
8177     return error_mark_node;
8178
8179   for (;;)
8180     {
8181       /* Get an operator token.  */
8182       token = cp_lexer_peek_token (parser->lexer);
8183
8184       if (warn_cxx0x_compat
8185           && token->type == CPP_RSHIFT
8186           && !parser->greater_than_is_operator_p)
8187         {
8188           if (warning_at (token->location, OPT_Wc__0x_compat,
8189                           "%<>>%> operator is treated"
8190                           " as two right angle brackets in C++11"))
8191             inform (token->location,
8192                     "suggest parentheses around %<>>%> expression");
8193         }
8194
8195       new_prec = TOKEN_PRECEDENCE (token);
8196
8197       /* Popping an entry off the stack means we completed a subexpression:
8198          - either we found a token which is not an operator (`>' where it is not
8199            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8200            will happen repeatedly;
8201          - or, we found an operator which has lower priority.  This is the case
8202            where the recursive descent *ascends*, as in `3 * 4 + 5' after
8203            parsing `3 * 4'.  */
8204       if (new_prec <= current.prec)
8205         {
8206           if (sp == stack)
8207             break;
8208           else
8209             goto pop;
8210         }
8211
8212      get_rhs:
8213       current.tree_type = binops_by_token[token->type].tree_type;
8214       current.loc = token->location;
8215
8216       /* We used the operator token.  */
8217       cp_lexer_consume_token (parser->lexer);
8218
8219       /* For "false && x" or "true || x", x will never be executed;
8220          disable warnings while evaluating it.  */
8221       if (current.tree_type == TRUTH_ANDIF_EXPR)
8222         c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8223       else if (current.tree_type == TRUTH_ORIF_EXPR)
8224         c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8225
8226       /* Extract another operand.  It may be the RHS of this expression
8227          or the LHS of a new, higher priority expression.  */
8228       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8229                   ? TRUTH_NOT_EXPR : ERROR_MARK);
8230       rhs = cp_parser_simple_cast_expression (parser);
8231
8232       /* Get another operator token.  Look up its precedence to avoid
8233          building a useless (immediately popped) stack entry for common
8234          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
8235       token = cp_lexer_peek_token (parser->lexer);
8236       lookahead_prec = TOKEN_PRECEDENCE (token);
8237       if (lookahead_prec > new_prec)
8238         {
8239           /* ... and prepare to parse the RHS of the new, higher priority
8240              expression.  Since precedence levels on the stack are
8241              monotonically increasing, we do not have to care about
8242              stack overflows.  */
8243           *sp = current;
8244           ++sp;
8245           current.lhs = rhs;
8246           current.lhs_type = rhs_type;
8247           current.prec = new_prec;
8248           new_prec = lookahead_prec;
8249           goto get_rhs;
8250
8251          pop:
8252           lookahead_prec = new_prec;
8253           /* If the stack is not empty, we have parsed into LHS the right side
8254              (`4' in the example above) of an expression we had suspended.
8255              We can use the information on the stack to recover the LHS (`3')
8256              from the stack together with the tree code (`MULT_EXPR'), and
8257              the precedence of the higher level subexpression
8258              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
8259              which will be used to actually build the additive expression.  */
8260           rhs = current.lhs;
8261           rhs_type = current.lhs_type;
8262           --sp;
8263           current = *sp;
8264         }
8265
8266       /* Undo the disabling of warnings done above.  */
8267       if (current.tree_type == TRUTH_ANDIF_EXPR)
8268         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8269       else if (current.tree_type == TRUTH_ORIF_EXPR)
8270         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8271
8272       if (warn_logical_not_paren
8273           && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8274           && current.lhs_type == TRUTH_NOT_EXPR
8275           /* Avoid warning for !!x == y.  */
8276           && (TREE_CODE (current.lhs) != NE_EXPR
8277               || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8278           && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8279               || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8280                   /* Avoid warning for !b == y where b is boolean.  */
8281                   && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8282                       || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8283                           != BOOLEAN_TYPE))))
8284           /* Avoid warning for !!b == y where b is boolean.  */
8285           && (!DECL_P (current.lhs)
8286               || TREE_TYPE (current.lhs) == NULL_TREE
8287               || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8288         warn_logical_not_parentheses (current.loc, current.tree_type,
8289                                       maybe_constant_value (rhs));
8290
8291       overload = NULL;
8292       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8293          ERROR_MARK for everything that is not a binary expression.
8294          This makes warn_about_parentheses miss some warnings that
8295          involve unary operators.  For unary expressions we should
8296          pass the correct tree_code unless the unary expression was
8297          surrounded by parentheses.
8298       */
8299       if (no_toplevel_fold_p
8300           && lookahead_prec <= current.prec
8301           && sp == stack)
8302         current.lhs = build2 (current.tree_type,
8303                               TREE_CODE_CLASS (current.tree_type)
8304                               == tcc_comparison
8305                               ? boolean_type_node : TREE_TYPE (current.lhs),
8306                               current.lhs, rhs);
8307       else
8308         current.lhs = build_x_binary_op (current.loc, current.tree_type,
8309                                          current.lhs, current.lhs_type,
8310                                          rhs, rhs_type, &overload,
8311                                          complain_flags (decltype_p));
8312       current.lhs_type = current.tree_type;
8313       if (EXPR_P (current.lhs))
8314         SET_EXPR_LOCATION (current.lhs, current.loc);
8315
8316       /* If the binary operator required the use of an overloaded operator,
8317          then this expression cannot be an integral constant-expression.
8318          An overloaded operator can be used even if both operands are
8319          otherwise permissible in an integral constant-expression if at
8320          least one of the operands is of enumeration type.  */
8321
8322       if (overload
8323           && cp_parser_non_integral_constant_expression (parser,
8324                                                          NIC_OVERLOADED))
8325         return error_mark_node;
8326     }
8327
8328   return current.lhs;
8329 }
8330
8331 static tree
8332 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8333                              bool no_toplevel_fold_p,
8334                              enum cp_parser_prec prec,
8335                              cp_id_kind * pidk)
8336 {
8337   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8338                                       /*decltype*/false, prec, pidk);
8339 }
8340
8341 /* Parse the `? expression : assignment-expression' part of a
8342    conditional-expression.  The LOGICAL_OR_EXPR is the
8343    logical-or-expression that started the conditional-expression.
8344    Returns a representation of the entire conditional-expression.
8345
8346    This routine is used by cp_parser_assignment_expression.
8347
8348      ? expression : assignment-expression
8349
8350    GNU Extensions:
8351
8352      ? : assignment-expression */
8353
8354 static tree
8355 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8356 {
8357   tree expr;
8358   tree assignment_expr;
8359   struct cp_token *token;
8360   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8361
8362   /* Consume the `?' token.  */
8363   cp_lexer_consume_token (parser->lexer);
8364   token = cp_lexer_peek_token (parser->lexer);
8365   if (cp_parser_allow_gnu_extensions_p (parser)
8366       && token->type == CPP_COLON)
8367     {
8368       pedwarn (token->location, OPT_Wpedantic, 
8369                "ISO C++ does not allow ?: with omitted middle operand");
8370       /* Implicit true clause.  */
8371       expr = NULL_TREE;
8372       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8373       warn_for_omitted_condop (token->location, logical_or_expr);
8374     }
8375   else
8376     {
8377       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8378       parser->colon_corrects_to_scope_p = false;
8379       /* Parse the expression.  */
8380       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8381       expr = cp_parser_expression (parser);
8382       c_inhibit_evaluation_warnings +=
8383         ((logical_or_expr == truthvalue_true_node)
8384          - (logical_or_expr == truthvalue_false_node));
8385       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8386     }
8387
8388   /* The next token should be a `:'.  */
8389   cp_parser_require (parser, CPP_COLON, RT_COLON);
8390   /* Parse the assignment-expression.  */
8391   assignment_expr = cp_parser_assignment_expression (parser);
8392   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8393
8394   /* Build the conditional-expression.  */
8395   return build_x_conditional_expr (loc, logical_or_expr,
8396                                    expr,
8397                                    assignment_expr,
8398                                    tf_warning_or_error);
8399 }
8400
8401 /* Parse an assignment-expression.
8402
8403    assignment-expression:
8404      conditional-expression
8405      logical-or-expression assignment-operator assignment_expression
8406      throw-expression
8407
8408    CAST_P is true if this expression is the target of a cast.
8409    DECLTYPE_P is true if this expression is the operand of decltype.
8410
8411    Returns a representation for the expression.  */
8412
8413 static tree
8414 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8415                                  bool cast_p, bool decltype_p)
8416 {
8417   tree expr;
8418
8419   /* If the next token is the `throw' keyword, then we're looking at
8420      a throw-expression.  */
8421   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8422     expr = cp_parser_throw_expression (parser);
8423   /* Otherwise, it must be that we are looking at a
8424      logical-or-expression.  */
8425   else
8426     {
8427       /* Parse the binary expressions (logical-or-expression).  */
8428       expr = cp_parser_binary_expression (parser, cast_p, false,
8429                                           decltype_p,
8430                                           PREC_NOT_OPERATOR, pidk);
8431       /* If the next token is a `?' then we're actually looking at a
8432          conditional-expression.  */
8433       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8434         return cp_parser_question_colon_clause (parser, expr);
8435       else
8436         {
8437           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8438
8439           /* If it's an assignment-operator, we're using the second
8440              production.  */
8441           enum tree_code assignment_operator
8442             = cp_parser_assignment_operator_opt (parser);
8443           if (assignment_operator != ERROR_MARK)
8444             {
8445               bool non_constant_p;
8446               location_t saved_input_location;
8447
8448               /* Parse the right-hand side of the assignment.  */
8449               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8450
8451               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8452                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8453
8454               /* An assignment may not appear in a
8455                  constant-expression.  */
8456               if (cp_parser_non_integral_constant_expression (parser,
8457                                                               NIC_ASSIGNMENT))
8458                 return error_mark_node;
8459               /* Build the assignment expression.  Its default
8460                  location is the location of the '=' token.  */
8461               saved_input_location = input_location;
8462               input_location = loc;
8463               expr = build_x_modify_expr (loc, expr,
8464                                           assignment_operator,
8465                                           rhs,
8466                                           complain_flags (decltype_p));
8467               input_location = saved_input_location;
8468             }
8469         }
8470     }
8471
8472   return expr;
8473 }
8474
8475 /* Parse an (optional) assignment-operator.
8476
8477    assignment-operator: one of
8478      = *= /= %= += -= >>= <<= &= ^= |=
8479
8480    GNU Extension:
8481
8482    assignment-operator: one of
8483      <?= >?=
8484
8485    If the next token is an assignment operator, the corresponding tree
8486    code is returned, and the token is consumed.  For example, for
8487    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
8488    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
8489    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
8490    operator, ERROR_MARK is returned.  */
8491
8492 static enum tree_code
8493 cp_parser_assignment_operator_opt (cp_parser* parser)
8494 {
8495   enum tree_code op;
8496   cp_token *token;
8497
8498   /* Peek at the next token.  */
8499   token = cp_lexer_peek_token (parser->lexer);
8500
8501   switch (token->type)
8502     {
8503     case CPP_EQ:
8504       op = NOP_EXPR;
8505       break;
8506
8507     case CPP_MULT_EQ:
8508       op = MULT_EXPR;
8509       break;
8510
8511     case CPP_DIV_EQ:
8512       op = TRUNC_DIV_EXPR;
8513       break;
8514
8515     case CPP_MOD_EQ:
8516       op = TRUNC_MOD_EXPR;
8517       break;
8518
8519     case CPP_PLUS_EQ:
8520       op = PLUS_EXPR;
8521       break;
8522
8523     case CPP_MINUS_EQ:
8524       op = MINUS_EXPR;
8525       break;
8526
8527     case CPP_RSHIFT_EQ:
8528       op = RSHIFT_EXPR;
8529       break;
8530
8531     case CPP_LSHIFT_EQ:
8532       op = LSHIFT_EXPR;
8533       break;
8534
8535     case CPP_AND_EQ:
8536       op = BIT_AND_EXPR;
8537       break;
8538
8539     case CPP_XOR_EQ:
8540       op = BIT_XOR_EXPR;
8541       break;
8542
8543     case CPP_OR_EQ:
8544       op = BIT_IOR_EXPR;
8545       break;
8546
8547     default:
8548       /* Nothing else is an assignment operator.  */
8549       op = ERROR_MARK;
8550     }
8551
8552   /* If it was an assignment operator, consume it.  */
8553   if (op != ERROR_MARK)
8554     cp_lexer_consume_token (parser->lexer);
8555
8556   return op;
8557 }
8558
8559 /* Parse an expression.
8560
8561    expression:
8562      assignment-expression
8563      expression , assignment-expression
8564
8565    CAST_P is true if this expression is the target of a cast.
8566    DECLTYPE_P is true if this expression is the immediate operand of decltype,
8567      except possibly parenthesized or on the RHS of a comma (N3276).
8568
8569    Returns a representation of the expression.  */
8570
8571 static tree
8572 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8573                       bool cast_p, bool decltype_p)
8574 {
8575   tree expression = NULL_TREE;
8576   location_t loc = UNKNOWN_LOCATION;
8577
8578   while (true)
8579     {
8580       tree assignment_expression;
8581
8582       /* Parse the next assignment-expression.  */
8583       assignment_expression
8584         = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8585
8586       /* We don't create a temporary for a call that is the immediate operand
8587          of decltype or on the RHS of a comma.  But when we see a comma, we
8588          need to create a temporary for a call on the LHS.  */
8589       if (decltype_p && !processing_template_decl
8590           && TREE_CODE (assignment_expression) == CALL_EXPR
8591           && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8592           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8593         assignment_expression
8594           = build_cplus_new (TREE_TYPE (assignment_expression),
8595                              assignment_expression, tf_warning_or_error);
8596
8597       /* If this is the first assignment-expression, we can just
8598          save it away.  */
8599       if (!expression)
8600         expression = assignment_expression;
8601       else
8602         expression = build_x_compound_expr (loc, expression,
8603                                             assignment_expression,
8604                                             complain_flags (decltype_p));
8605       /* If the next token is not a comma, then we are done with the
8606          expression.  */
8607       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8608         break;
8609       /* Consume the `,'.  */
8610       loc = cp_lexer_peek_token (parser->lexer)->location;
8611       cp_lexer_consume_token (parser->lexer);
8612       /* A comma operator cannot appear in a constant-expression.  */
8613       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8614         expression = error_mark_node;
8615     }
8616
8617   return expression;
8618 }
8619
8620 /* Parse a constant-expression.
8621
8622    constant-expression:
8623      conditional-expression
8624
8625   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8626   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
8627   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
8628   is false, NON_CONSTANT_P should be NULL.  */
8629
8630 static tree
8631 cp_parser_constant_expression (cp_parser* parser,
8632                                bool allow_non_constant_p,
8633                                bool *non_constant_p)
8634 {
8635   bool saved_integral_constant_expression_p;
8636   bool saved_allow_non_integral_constant_expression_p;
8637   bool saved_non_integral_constant_expression_p;
8638   tree expression;
8639
8640   /* It might seem that we could simply parse the
8641      conditional-expression, and then check to see if it were
8642      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
8643      one that the compiler can figure out is constant, possibly after
8644      doing some simplifications or optimizations.  The standard has a
8645      precise definition of constant-expression, and we must honor
8646      that, even though it is somewhat more restrictive.
8647
8648      For example:
8649
8650        int i[(2, 3)];
8651
8652      is not a legal declaration, because `(2, 3)' is not a
8653      constant-expression.  The `,' operator is forbidden in a
8654      constant-expression.  However, GCC's constant-folding machinery
8655      will fold this operation to an INTEGER_CST for `3'.  */
8656
8657   /* Save the old settings.  */
8658   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8659   saved_allow_non_integral_constant_expression_p
8660     = parser->allow_non_integral_constant_expression_p;
8661   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8662   /* We are now parsing a constant-expression.  */
8663   parser->integral_constant_expression_p = true;
8664   parser->allow_non_integral_constant_expression_p
8665     = (allow_non_constant_p || cxx_dialect >= cxx11);
8666   parser->non_integral_constant_expression_p = false;
8667   /* Although the grammar says "conditional-expression", we parse an
8668      "assignment-expression", which also permits "throw-expression"
8669      and the use of assignment operators.  In the case that
8670      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8671      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
8672      actually essential that we look for an assignment-expression.
8673      For example, cp_parser_initializer_clauses uses this function to
8674      determine whether a particular assignment-expression is in fact
8675      constant.  */
8676   expression = cp_parser_assignment_expression (parser);
8677   /* Restore the old settings.  */
8678   parser->integral_constant_expression_p
8679     = saved_integral_constant_expression_p;
8680   parser->allow_non_integral_constant_expression_p
8681     = saved_allow_non_integral_constant_expression_p;
8682   if (cxx_dialect >= cxx11)
8683     {
8684       /* Require an rvalue constant expression here; that's what our
8685          callers expect.  Reference constant expressions are handled
8686          separately in e.g. cp_parser_template_argument.  */
8687       bool is_const = potential_rvalue_constant_expression (expression);
8688       parser->non_integral_constant_expression_p = !is_const;
8689       if (!is_const && !allow_non_constant_p)
8690         require_potential_rvalue_constant_expression (expression);
8691     }
8692   if (allow_non_constant_p)
8693     *non_constant_p = parser->non_integral_constant_expression_p;
8694   parser->non_integral_constant_expression_p
8695     = saved_non_integral_constant_expression_p;
8696
8697   return expression;
8698 }
8699
8700 /* Parse __builtin_offsetof.
8701
8702    offsetof-expression:
8703      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8704
8705    offsetof-member-designator:
8706      id-expression
8707      | offsetof-member-designator "." id-expression
8708      | offsetof-member-designator "[" expression "]"
8709      | offsetof-member-designator "->" id-expression  */
8710
8711 static tree
8712 cp_parser_builtin_offsetof (cp_parser *parser)
8713 {
8714   int save_ice_p, save_non_ice_p;
8715   tree type, expr;
8716   cp_id_kind dummy;
8717   cp_token *token;
8718
8719   /* We're about to accept non-integral-constant things, but will
8720      definitely yield an integral constant expression.  Save and
8721      restore these values around our local parsing.  */
8722   save_ice_p = parser->integral_constant_expression_p;
8723   save_non_ice_p = parser->non_integral_constant_expression_p;
8724
8725   /* Consume the "__builtin_offsetof" token.  */
8726   cp_lexer_consume_token (parser->lexer);
8727   /* Consume the opening `('.  */
8728   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8729   /* Parse the type-id.  */
8730   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8731   type = cp_parser_type_id (parser);
8732   /* Look for the `,'.  */
8733   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8734   token = cp_lexer_peek_token (parser->lexer);
8735
8736   /* Build the (type *)null that begins the traditional offsetof macro.  */
8737   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8738                             tf_warning_or_error);
8739
8740   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
8741   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8742                                                  true, &dummy, token->location);
8743   while (true)
8744     {
8745       token = cp_lexer_peek_token (parser->lexer);
8746       switch (token->type)
8747         {
8748         case CPP_OPEN_SQUARE:
8749           /* offsetof-member-designator "[" expression "]" */
8750           expr = cp_parser_postfix_open_square_expression (parser, expr,
8751                                                            true, false);
8752           break;
8753
8754         case CPP_DEREF:
8755           /* offsetof-member-designator "->" identifier */
8756           expr = grok_array_decl (token->location, expr,
8757                                   integer_zero_node, false);
8758           /* FALLTHRU */
8759
8760         case CPP_DOT:
8761           /* offsetof-member-designator "." identifier */
8762           cp_lexer_consume_token (parser->lexer);
8763           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8764                                                          expr, true, &dummy,
8765                                                          token->location);
8766           break;
8767
8768         case CPP_CLOSE_PAREN:
8769           /* Consume the ")" token.  */
8770           cp_lexer_consume_token (parser->lexer);
8771           goto success;
8772
8773         default:
8774           /* Error.  We know the following require will fail, but
8775              that gives the proper error message.  */
8776           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8777           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8778           expr = error_mark_node;
8779           goto failure;
8780         }
8781     }
8782
8783  success:
8784   expr = finish_offsetof (expr, loc);
8785
8786  failure:
8787   parser->integral_constant_expression_p = save_ice_p;
8788   parser->non_integral_constant_expression_p = save_non_ice_p;
8789
8790   return expr;
8791 }
8792
8793 /* Parse a trait expression.
8794
8795    Returns a representation of the expression, the underlying type
8796    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
8797
8798 static tree
8799 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8800 {
8801   cp_trait_kind kind;
8802   tree type1, type2 = NULL_TREE;
8803   bool binary = false;
8804   bool variadic = false;
8805
8806   switch (keyword)
8807     {
8808     case RID_HAS_NOTHROW_ASSIGN:
8809       kind = CPTK_HAS_NOTHROW_ASSIGN;
8810       break;
8811     case RID_HAS_NOTHROW_CONSTRUCTOR:
8812       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8813       break;
8814     case RID_HAS_NOTHROW_COPY:
8815       kind = CPTK_HAS_NOTHROW_COPY;
8816       break;
8817     case RID_HAS_TRIVIAL_ASSIGN:
8818       kind = CPTK_HAS_TRIVIAL_ASSIGN;
8819       break;
8820     case RID_HAS_TRIVIAL_CONSTRUCTOR:
8821       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8822       break;
8823     case RID_HAS_TRIVIAL_COPY:
8824       kind = CPTK_HAS_TRIVIAL_COPY;
8825       break;
8826     case RID_HAS_TRIVIAL_DESTRUCTOR:
8827       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8828       break;
8829     case RID_HAS_VIRTUAL_DESTRUCTOR:
8830       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8831       break;
8832     case RID_IS_ABSTRACT:
8833       kind = CPTK_IS_ABSTRACT;
8834       break;
8835     case RID_IS_BASE_OF:
8836       kind = CPTK_IS_BASE_OF;
8837       binary = true;
8838       break;
8839     case RID_IS_CLASS:
8840       kind = CPTK_IS_CLASS;
8841       break;
8842     case RID_IS_EMPTY:
8843       kind = CPTK_IS_EMPTY;
8844       break;
8845     case RID_IS_ENUM:
8846       kind = CPTK_IS_ENUM;
8847       break;
8848     case RID_IS_FINAL:
8849       kind = CPTK_IS_FINAL;
8850       break;
8851     case RID_IS_LITERAL_TYPE:
8852       kind = CPTK_IS_LITERAL_TYPE;
8853       break;
8854     case RID_IS_POD:
8855       kind = CPTK_IS_POD;
8856       break;
8857     case RID_IS_POLYMORPHIC:
8858       kind = CPTK_IS_POLYMORPHIC;
8859       break;
8860     case RID_IS_STD_LAYOUT:
8861       kind = CPTK_IS_STD_LAYOUT;
8862       break;
8863     case RID_IS_TRIVIAL:
8864       kind = CPTK_IS_TRIVIAL;
8865       break;
8866     case RID_IS_TRIVIALLY_ASSIGNABLE:
8867       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8868       binary = true;
8869       break;
8870     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8871       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8872       variadic = true;
8873       break;
8874     case RID_IS_TRIVIALLY_COPYABLE:
8875       kind = CPTK_IS_TRIVIALLY_COPYABLE;
8876       break;
8877     case RID_IS_UNION:
8878       kind = CPTK_IS_UNION;
8879       break;
8880     case RID_UNDERLYING_TYPE:
8881       kind = CPTK_UNDERLYING_TYPE;
8882       break;
8883     case RID_BASES:
8884       kind = CPTK_BASES;
8885       break;
8886     case RID_DIRECT_BASES:
8887       kind = CPTK_DIRECT_BASES;
8888       break;
8889     default:
8890       gcc_unreachable ();
8891     }
8892
8893   /* Consume the token.  */
8894   cp_lexer_consume_token (parser->lexer);
8895
8896   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8897
8898   type1 = cp_parser_type_id (parser);
8899
8900   if (type1 == error_mark_node)
8901     return error_mark_node;
8902
8903   if (binary)
8904     {
8905       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8906  
8907       type2 = cp_parser_type_id (parser);
8908
8909       if (type2 == error_mark_node)
8910         return error_mark_node;
8911     }
8912   else if (variadic)
8913     {
8914       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8915         {
8916           cp_lexer_consume_token (parser->lexer);
8917           tree elt = cp_parser_type_id (parser);
8918           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8919             {
8920               cp_lexer_consume_token (parser->lexer);
8921               elt = make_pack_expansion (elt);
8922             }
8923           if (elt == error_mark_node)
8924             return error_mark_node;
8925           type2 = tree_cons (NULL_TREE, elt, type2);
8926         }
8927     }
8928
8929   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8930
8931   /* Complete the trait expression, which may mean either processing
8932      the trait expr now or saving it for template instantiation.  */
8933   switch(kind)
8934     {
8935     case CPTK_UNDERLYING_TYPE:
8936       return finish_underlying_type (type1);
8937     case CPTK_BASES:
8938       return finish_bases (type1, false);
8939     case CPTK_DIRECT_BASES:
8940       return finish_bases (type1, true);
8941     default:
8942       return finish_trait_expr (kind, type1, type2);
8943     }
8944 }
8945
8946 /* Lambdas that appear in variable initializer or default argument scope
8947    get that in their mangling, so we need to record it.  We might as well
8948    use the count for function and namespace scopes as well.  */
8949 static GTY(()) tree lambda_scope;
8950 static GTY(()) int lambda_count;
8951 typedef struct GTY(()) tree_int
8952 {
8953   tree t;
8954   int i;
8955 } tree_int;
8956 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8957
8958 static void
8959 start_lambda_scope (tree decl)
8960 {
8961   tree_int ti;
8962   gcc_assert (decl);
8963   /* Once we're inside a function, we ignore other scopes and just push
8964      the function again so that popping works properly.  */
8965   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8966     decl = current_function_decl;
8967   ti.t = lambda_scope;
8968   ti.i = lambda_count;
8969   vec_safe_push (lambda_scope_stack, ti);
8970   if (lambda_scope != decl)
8971     {
8972       /* Don't reset the count if we're still in the same function.  */
8973       lambda_scope = decl;
8974       lambda_count = 0;
8975     }
8976 }
8977
8978 static void
8979 record_lambda_scope (tree lambda)
8980 {
8981   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8982   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8983 }
8984
8985 static void
8986 finish_lambda_scope (void)
8987 {
8988   tree_int *p = &lambda_scope_stack->last ();
8989   if (lambda_scope != p->t)
8990     {
8991       lambda_scope = p->t;
8992       lambda_count = p->i;
8993     }
8994   lambda_scope_stack->pop ();
8995 }
8996
8997 /* Parse a lambda expression.
8998
8999    lambda-expression:
9000      lambda-introducer lambda-declarator [opt] compound-statement
9001
9002    Returns a representation of the expression.  */
9003
9004 static tree
9005 cp_parser_lambda_expression (cp_parser* parser)
9006 {
9007   tree lambda_expr = build_lambda_expr ();
9008   tree type;
9009   bool ok = true;
9010   cp_token *token = cp_lexer_peek_token (parser->lexer);
9011   cp_token_position start = 0;
9012
9013   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9014
9015   if (cp_unevaluated_operand)
9016     {
9017       if (!token->error_reported)
9018         {
9019           error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9020                     "lambda-expression in unevaluated context");
9021           token->error_reported = true;
9022         }
9023       ok = false;
9024     }
9025   else if (parser->in_template_argument_list_p)
9026     {
9027       if (!token->error_reported)
9028         {
9029           error_at (token->location, "lambda-expression in template-argument");
9030           token->error_reported = true;
9031         }
9032       ok = false;
9033     }
9034
9035   /* We may be in the middle of deferred access check.  Disable
9036      it now.  */
9037   push_deferring_access_checks (dk_no_deferred);
9038
9039   cp_parser_lambda_introducer (parser, lambda_expr);
9040
9041   type = begin_lambda_type (lambda_expr);
9042   if (type == error_mark_node)
9043     return error_mark_node;
9044
9045   record_lambda_scope (lambda_expr);
9046
9047   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
9048   determine_visibility (TYPE_NAME (type));
9049
9050   /* Now that we've started the type, add the capture fields for any
9051      explicit captures.  */
9052   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9053
9054   {
9055     /* Inside the class, surrounding template-parameter-lists do not apply.  */
9056     unsigned int saved_num_template_parameter_lists
9057         = parser->num_template_parameter_lists;
9058     unsigned char in_statement = parser->in_statement;
9059     bool in_switch_statement_p = parser->in_switch_statement_p;
9060     bool fully_implicit_function_template_p
9061         = parser->fully_implicit_function_template_p;
9062     tree implicit_template_parms = parser->implicit_template_parms;
9063     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9064     bool auto_is_implicit_function_template_parm_p
9065         = parser->auto_is_implicit_function_template_parm_p;
9066
9067     parser->num_template_parameter_lists = 0;
9068     parser->in_statement = 0;
9069     parser->in_switch_statement_p = false;
9070     parser->fully_implicit_function_template_p = false;
9071     parser->implicit_template_parms = 0;
9072     parser->implicit_template_scope = 0;
9073     parser->auto_is_implicit_function_template_parm_p = false;
9074
9075     /* By virtue of defining a local class, a lambda expression has access to
9076        the private variables of enclosing classes.  */
9077
9078     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9079
9080     if (ok)
9081       {
9082         if (!cp_parser_error_occurred (parser)
9083             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9084             && cp_parser_start_tentative_firewall (parser))
9085           start = token;
9086         cp_parser_lambda_body (parser, lambda_expr);
9087       }
9088     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9089       {
9090         if (cp_parser_skip_to_closing_brace (parser))
9091           cp_lexer_consume_token (parser->lexer);
9092       }
9093
9094     /* The capture list was built up in reverse order; fix that now.  */
9095     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9096       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9097
9098     if (ok)
9099       maybe_add_lambda_conv_op (type);
9100
9101     type = finish_struct (type, /*attributes=*/NULL_TREE);
9102
9103     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9104     parser->in_statement = in_statement;
9105     parser->in_switch_statement_p = in_switch_statement_p;
9106     parser->fully_implicit_function_template_p
9107         = fully_implicit_function_template_p;
9108     parser->implicit_template_parms = implicit_template_parms;
9109     parser->implicit_template_scope = implicit_template_scope;
9110     parser->auto_is_implicit_function_template_parm_p
9111         = auto_is_implicit_function_template_parm_p;
9112   }
9113
9114   pop_deferring_access_checks ();
9115
9116   /* This field is only used during parsing of the lambda.  */
9117   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9118
9119   /* This lambda shouldn't have any proxies left at this point.  */
9120   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9121   /* And now that we're done, push proxies for an enclosing lambda.  */
9122   insert_pending_capture_proxies ();
9123
9124   if (ok)
9125     lambda_expr = build_lambda_object (lambda_expr);
9126   else
9127     lambda_expr = error_mark_node;
9128
9129   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9130
9131   return lambda_expr;
9132 }
9133
9134 /* Parse the beginning of a lambda expression.
9135
9136    lambda-introducer:
9137      [ lambda-capture [opt] ]
9138
9139    LAMBDA_EXPR is the current representation of the lambda expression.  */
9140
9141 static void
9142 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9143 {
9144   /* Need commas after the first capture.  */
9145   bool first = true;
9146
9147   /* Eat the leading `['.  */
9148   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9149
9150   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
9151   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9152       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9153     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9154   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9155     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9156
9157   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9158     {
9159       cp_lexer_consume_token (parser->lexer);
9160       first = false;
9161     }
9162
9163   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9164     {
9165       cp_token* capture_token;
9166       tree capture_id;
9167       tree capture_init_expr;
9168       cp_id_kind idk = CP_ID_KIND_NONE;
9169       bool explicit_init_p = false;
9170
9171       enum capture_kind_type
9172       {
9173         BY_COPY,
9174         BY_REFERENCE
9175       };
9176       enum capture_kind_type capture_kind = BY_COPY;
9177
9178       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9179         {
9180           error ("expected end of capture-list");
9181           return;
9182         }
9183
9184       if (first)
9185         first = false;
9186       else
9187         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9188
9189       /* Possibly capture `this'.  */
9190       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9191         {
9192           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9193           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9194             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9195                      "with by-copy capture default");
9196           cp_lexer_consume_token (parser->lexer);
9197           add_capture (lambda_expr,
9198                        /*id=*/this_identifier,
9199                        /*initializer=*/finish_this_expr(),
9200                        /*by_reference_p=*/false,
9201                        explicit_init_p);
9202           continue;
9203         }
9204
9205       /* Remember whether we want to capture as a reference or not.  */
9206       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9207         {
9208           capture_kind = BY_REFERENCE;
9209           cp_lexer_consume_token (parser->lexer);
9210         }
9211
9212       /* Get the identifier.  */
9213       capture_token = cp_lexer_peek_token (parser->lexer);
9214       capture_id = cp_parser_identifier (parser);
9215
9216       if (capture_id == error_mark_node)
9217         /* Would be nice to have a cp_parser_skip_to_closing_x for general
9218            delimiters, but I modified this to stop on unnested ']' as well.  It
9219            was already changed to stop on unnested '}', so the
9220            "closing_parenthesis" name is no more misleading with my change.  */
9221         {
9222           cp_parser_skip_to_closing_parenthesis (parser,
9223                                                  /*recovering=*/true,
9224                                                  /*or_comma=*/true,
9225                                                  /*consume_paren=*/true);
9226           break;
9227         }
9228
9229       /* Find the initializer for this capture.  */
9230       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9231           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9232           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9233         {
9234           bool direct, non_constant;
9235           /* An explicit initializer exists.  */
9236           if (cxx_dialect < cxx14)
9237             pedwarn (input_location, 0,
9238                      "lambda capture initializers "
9239                      "only available with -std=c++14 or -std=gnu++14");
9240           capture_init_expr = cp_parser_initializer (parser, &direct,
9241                                                      &non_constant);
9242           explicit_init_p = true;
9243           if (capture_init_expr == NULL_TREE)
9244             {
9245               error ("empty initializer for lambda init-capture");
9246               capture_init_expr = error_mark_node;
9247             }
9248         }
9249       else
9250         {
9251           const char* error_msg;
9252
9253           /* Turn the identifier into an id-expression.  */
9254           capture_init_expr
9255             = cp_parser_lookup_name_simple (parser, capture_id,
9256                                             capture_token->location);
9257
9258           if (capture_init_expr == error_mark_node)
9259             {
9260               unqualified_name_lookup_error (capture_id);
9261               continue;
9262             }
9263           else if (DECL_P (capture_init_expr)
9264                    && (!VAR_P (capture_init_expr)
9265                        && TREE_CODE (capture_init_expr) != PARM_DECL))
9266             {
9267               error_at (capture_token->location,
9268                         "capture of non-variable %qD ",
9269                         capture_init_expr);
9270               inform (0, "%q+#D declared here", capture_init_expr);
9271               continue;
9272             }
9273           if (VAR_P (capture_init_expr)
9274               && decl_storage_duration (capture_init_expr) != dk_auto)
9275             {
9276               if (pedwarn (capture_token->location, 0, "capture of variable "
9277                            "%qD with non-automatic storage duration",
9278                            capture_init_expr))
9279                 inform (0, "%q+#D declared here", capture_init_expr);
9280               continue;
9281             }
9282
9283           capture_init_expr
9284             = finish_id_expression
9285                 (capture_id,
9286                  capture_init_expr,
9287                  parser->scope,
9288                  &idk,
9289                  /*integral_constant_expression_p=*/false,
9290                  /*allow_non_integral_constant_expression_p=*/false,
9291                  /*non_integral_constant_expression_p=*/NULL,
9292                  /*template_p=*/false,
9293                  /*done=*/true,
9294                  /*address_p=*/false,
9295                  /*template_arg_p=*/false,
9296                  &error_msg,
9297                  capture_token->location);
9298
9299           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9300             {
9301               cp_lexer_consume_token (parser->lexer);
9302               capture_init_expr = make_pack_expansion (capture_init_expr);
9303             }
9304           else
9305             check_for_bare_parameter_packs (capture_init_expr);
9306         }
9307
9308       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9309           && !explicit_init_p)
9310         {
9311           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9312               && capture_kind == BY_COPY)
9313             pedwarn (capture_token->location, 0, "explicit by-copy capture "
9314                      "of %qD redundant with by-copy capture default",
9315                      capture_id);
9316           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9317               && capture_kind == BY_REFERENCE)
9318             pedwarn (capture_token->location, 0, "explicit by-reference "
9319                      "capture of %qD redundant with by-reference capture "
9320                      "default", capture_id);
9321         }
9322
9323       add_capture (lambda_expr,
9324                    capture_id,
9325                    capture_init_expr,
9326                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
9327                    explicit_init_p);
9328     }
9329
9330   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9331 }
9332
9333 /* Parse the (optional) middle of a lambda expression.
9334
9335    lambda-declarator:
9336      < template-parameter-list [opt] >
9337      ( parameter-declaration-clause [opt] )
9338        attribute-specifier [opt]
9339        mutable [opt]
9340        exception-specification [opt]
9341        lambda-return-type-clause [opt]
9342
9343    LAMBDA_EXPR is the current representation of the lambda expression.  */
9344
9345 static bool
9346 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9347 {
9348   /* 5.1.1.4 of the standard says:
9349        If a lambda-expression does not include a lambda-declarator, it is as if
9350        the lambda-declarator were ().
9351      This means an empty parameter list, no attributes, and no exception
9352      specification.  */
9353   tree param_list = void_list_node;
9354   tree attributes = NULL_TREE;
9355   tree exception_spec = NULL_TREE;
9356   tree template_param_list = NULL_TREE;
9357
9358   /* The template-parameter-list is optional, but must begin with
9359      an opening angle if present.  */
9360   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9361     {
9362       if (cxx_dialect < cxx14)
9363         pedwarn (parser->lexer->next_token->location, 0,
9364                  "lambda templates are only available with "
9365                  "-std=c++14 or -std=gnu++14");
9366
9367       cp_lexer_consume_token (parser->lexer);
9368
9369       template_param_list = cp_parser_template_parameter_list (parser);
9370
9371       cp_parser_skip_to_end_of_template_parameter_list (parser);
9372
9373       /* We just processed one more parameter list.  */
9374       ++parser->num_template_parameter_lists;
9375     }
9376
9377   /* The parameter-declaration-clause is optional (unless
9378      template-parameter-list was given), but must begin with an
9379      opening parenthesis if present.  */
9380   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9381     {
9382       cp_lexer_consume_token (parser->lexer);
9383
9384       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9385
9386       /* Parse parameters.  */
9387       param_list = cp_parser_parameter_declaration_clause (parser);
9388
9389       /* Default arguments shall not be specified in the
9390          parameter-declaration-clause of a lambda-declarator.  */
9391       for (tree t = param_list; t; t = TREE_CHAIN (t))
9392         if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9393           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9394                    "default argument specified for lambda parameter");
9395
9396       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9397
9398       attributes = cp_parser_attributes_opt (parser);
9399
9400       /* Parse optional `mutable' keyword.  */
9401       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9402         {
9403           cp_lexer_consume_token (parser->lexer);
9404           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9405         }
9406
9407       /* Parse optional exception specification.  */
9408       exception_spec = cp_parser_exception_specification_opt (parser);
9409
9410       /* Parse optional trailing return type.  */
9411       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9412         {
9413           cp_lexer_consume_token (parser->lexer);
9414           LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9415             = cp_parser_trailing_type_id (parser);
9416         }
9417
9418       /* The function parameters must be in scope all the way until after the
9419          trailing-return-type in case of decltype.  */
9420       pop_bindings_and_leave_scope ();
9421     }
9422   else if (template_param_list != NULL_TREE) // generate diagnostic
9423     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9424
9425   /* Create the function call operator.
9426
9427      Messing with declarators like this is no uglier than building up the
9428      FUNCTION_DECL by hand, and this is less likely to get out of sync with
9429      other code.  */
9430   {
9431     cp_decl_specifier_seq return_type_specs;
9432     cp_declarator* declarator;
9433     tree fco;
9434     int quals;
9435     void *p;
9436
9437     clear_decl_specs (&return_type_specs);
9438     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9439       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9440     else
9441       /* Maybe we will deduce the return type later.  */
9442       return_type_specs.type = make_auto ();
9443
9444     p = obstack_alloc (&declarator_obstack, 0);
9445
9446     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9447                                      sfk_none);
9448
9449     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9450              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9451     declarator = make_call_declarator (declarator, param_list, quals,
9452                                        VIRT_SPEC_UNSPECIFIED,
9453                                        REF_QUAL_NONE,
9454                                        exception_spec,
9455                                        /*late_return_type=*/NULL_TREE);
9456     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9457
9458     fco = grokmethod (&return_type_specs,
9459                       declarator,
9460                       attributes);
9461     if (fco != error_mark_node)
9462       {
9463         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9464         DECL_ARTIFICIAL (fco) = 1;
9465         /* Give the object parameter a different name.  */
9466         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9467         if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9468           TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9469       }
9470     if (template_param_list)
9471       {
9472         fco = finish_member_template_decl (fco);
9473         finish_template_decl (template_param_list);
9474         --parser->num_template_parameter_lists;
9475       }
9476     else if (parser->fully_implicit_function_template_p)
9477       fco = finish_fully_implicit_template (parser, fco);
9478
9479     finish_member_declaration (fco);
9480
9481     obstack_free (&declarator_obstack, p);
9482
9483     return (fco != error_mark_node);
9484   }
9485 }
9486
9487 /* Parse the body of a lambda expression, which is simply
9488
9489    compound-statement
9490
9491    but which requires special handling.
9492    LAMBDA_EXPR is the current representation of the lambda expression.  */
9493
9494 static void
9495 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9496 {
9497   bool nested = (current_function_decl != NULL_TREE);
9498   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9499   if (nested)
9500     push_function_context ();
9501   else
9502     /* Still increment function_depth so that we don't GC in the
9503        middle of an expression.  */
9504     ++function_depth;
9505   /* Clear this in case we're in the middle of a default argument.  */
9506   parser->local_variables_forbidden_p = false;
9507
9508   /* Finish the function call operator
9509      - class_specifier
9510      + late_parsing_for_member
9511      + function_definition_after_declarator
9512      + ctor_initializer_opt_and_function_body  */
9513   {
9514     tree fco = lambda_function (lambda_expr);
9515     tree body;
9516     bool done = false;
9517     tree compound_stmt;
9518     tree cap;
9519
9520     /* Let the front end know that we are going to be defining this
9521        function.  */
9522     start_preparsed_function (fco,
9523                               NULL_TREE,
9524                               SF_PRE_PARSED | SF_INCLASS_INLINE);
9525
9526     start_lambda_scope (fco);
9527     body = begin_function_body ();
9528
9529     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9530       goto out;
9531
9532     /* Push the proxies for any explicit captures.  */
9533     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9534          cap = TREE_CHAIN (cap))
9535       build_capture_proxy (TREE_PURPOSE (cap));
9536
9537     compound_stmt = begin_compound_stmt (0);
9538
9539     /* 5.1.1.4 of the standard says:
9540          If a lambda-expression does not include a trailing-return-type, it
9541          is as if the trailing-return-type denotes the following type:
9542           * if the compound-statement is of the form
9543                { return attribute-specifier [opt] expression ; }
9544              the type of the returned expression after lvalue-to-rvalue
9545              conversion (_conv.lval_ 4.1), array-to-pointer conversion
9546              (_conv.array_ 4.2), and function-to-pointer conversion
9547              (_conv.func_ 4.3);
9548           * otherwise, void.  */
9549
9550     /* In a lambda that has neither a lambda-return-type-clause
9551        nor a deducible form, errors should be reported for return statements
9552        in the body.  Since we used void as the placeholder return type, parsing
9553        the body as usual will give such desired behavior.  */
9554     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9555         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9556         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9557       {
9558         tree expr = NULL_TREE;
9559         cp_id_kind idk = CP_ID_KIND_NONE;
9560
9561         /* Parse tentatively in case there's more after the initial return
9562            statement.  */
9563         cp_parser_parse_tentatively (parser);
9564
9565         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9566
9567         expr = cp_parser_expression (parser, &idk);
9568
9569         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9570         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9571
9572         if (cp_parser_parse_definitely (parser))
9573           {
9574             if (!processing_template_decl)
9575               apply_deduced_return_type (fco, lambda_return_type (expr));
9576
9577             /* Will get error here if type not deduced yet.  */
9578             finish_return_stmt (expr);
9579
9580             done = true;
9581           }
9582       }
9583
9584     if (!done)
9585       {
9586         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9587           cp_parser_label_declaration (parser);
9588         cp_parser_statement_seq_opt (parser, NULL_TREE);
9589         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9590       }
9591
9592     finish_compound_stmt (compound_stmt);
9593
9594   out:
9595     finish_function_body (body);
9596     finish_lambda_scope ();
9597
9598     /* Finish the function and generate code for it if necessary.  */
9599     tree fn = finish_function (/*inline*/2);
9600
9601     /* Only expand if the call op is not a template.  */
9602     if (!DECL_TEMPLATE_INFO (fco))
9603       expand_or_defer_fn (fn);
9604   }
9605
9606   parser->local_variables_forbidden_p = local_variables_forbidden_p;
9607   if (nested)
9608     pop_function_context();
9609   else
9610     --function_depth;
9611 }
9612
9613 /* Statements [gram.stmt.stmt]  */
9614
9615 /* Parse a statement.
9616
9617    statement:
9618      labeled-statement
9619      expression-statement
9620      compound-statement
9621      selection-statement
9622      iteration-statement
9623      jump-statement
9624      declaration-statement
9625      try-block
9626
9627   C++11:
9628
9629   statement:
9630     labeled-statement
9631     attribute-specifier-seq (opt) expression-statement
9632     attribute-specifier-seq (opt) compound-statement
9633     attribute-specifier-seq (opt) selection-statement
9634     attribute-specifier-seq (opt) iteration-statement
9635     attribute-specifier-seq (opt) jump-statement
9636     declaration-statement
9637     attribute-specifier-seq (opt) try-block
9638
9639   TM Extension:
9640
9641    statement:
9642      atomic-statement
9643
9644   IN_COMPOUND is true when the statement is nested inside a
9645   cp_parser_compound_statement; this matters for certain pragmas.
9646
9647   If IF_P is not NULL, *IF_P is set to indicate whether the statement
9648   is a (possibly labeled) if statement which is not enclosed in braces
9649   and has an else clause.  This is used to implement -Wparentheses.  */
9650
9651 static void
9652 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9653                      bool in_compound, bool *if_p)
9654 {
9655   tree statement, std_attrs = NULL_TREE;
9656   cp_token *token;
9657   location_t statement_location, attrs_location;
9658
9659  restart:
9660   if (if_p != NULL)
9661     *if_p = false;
9662   /* There is no statement yet.  */
9663   statement = NULL_TREE;
9664
9665   saved_token_sentinel saved_tokens (parser->lexer);
9666   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9667   if (c_dialect_objc ())
9668     /* In obj-c++, seeing '[[' might be the either the beginning of
9669        c++11 attributes, or a nested objc-message-expression.  So
9670        let's parse the c++11 attributes tentatively.  */
9671     cp_parser_parse_tentatively (parser);
9672   std_attrs = cp_parser_std_attribute_spec_seq (parser);
9673   if (c_dialect_objc ())
9674     {
9675       if (!cp_parser_parse_definitely (parser))
9676         std_attrs = NULL_TREE;
9677     }
9678
9679   /* Peek at the next token.  */
9680   token = cp_lexer_peek_token (parser->lexer);
9681   /* Remember the location of the first token in the statement.  */
9682   statement_location = token->location;
9683   /* If this is a keyword, then that will often determine what kind of
9684      statement we have.  */
9685   if (token->type == CPP_KEYWORD)
9686     {
9687       enum rid keyword = token->keyword;
9688
9689       switch (keyword)
9690         {
9691         case RID_CASE:
9692         case RID_DEFAULT:
9693           /* Looks like a labeled-statement with a case label.
9694              Parse the label, and then use tail recursion to parse
9695              the statement.  */
9696           cp_parser_label_for_labeled_statement (parser, std_attrs);
9697           goto restart;
9698
9699         case RID_IF:
9700         case RID_SWITCH:
9701           statement = cp_parser_selection_statement (parser, if_p);
9702           break;
9703
9704         case RID_WHILE:
9705         case RID_DO:
9706         case RID_FOR:
9707           statement = cp_parser_iteration_statement (parser, false);
9708           break;
9709
9710         case RID_CILK_FOR:
9711           if (!flag_cilkplus)
9712             {
9713               error_at (cp_lexer_peek_token (parser->lexer)->location,
9714                         "-fcilkplus must be enabled to use %<_Cilk_for%>");
9715               cp_lexer_consume_token (parser->lexer);
9716               statement = error_mark_node;
9717             }
9718           else
9719             statement = cp_parser_cilk_for (parser, integer_zero_node);
9720           break;
9721
9722         case RID_BREAK:
9723         case RID_CONTINUE:
9724         case RID_RETURN:
9725         case RID_GOTO:
9726           statement = cp_parser_jump_statement (parser);
9727           break;
9728
9729         case RID_CILK_SYNC:
9730           cp_lexer_consume_token (parser->lexer);
9731           if (flag_cilkplus)
9732             {
9733               tree sync_expr = build_cilk_sync ();
9734               SET_EXPR_LOCATION (sync_expr,
9735                                  token->location);
9736               statement = finish_expr_stmt (sync_expr);
9737             }
9738           else
9739             {
9740               error_at (token->location, "-fcilkplus must be enabled to use"
9741                         " %<_Cilk_sync%>");
9742               statement = error_mark_node;
9743             }
9744           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9745           break;
9746
9747           /* Objective-C++ exception-handling constructs.  */
9748         case RID_AT_TRY:
9749         case RID_AT_CATCH:
9750         case RID_AT_FINALLY:
9751         case RID_AT_SYNCHRONIZED:
9752         case RID_AT_THROW:
9753           statement = cp_parser_objc_statement (parser);
9754           break;
9755
9756         case RID_TRY:
9757           statement = cp_parser_try_block (parser);
9758           break;
9759
9760         case RID_NAMESPACE:
9761           /* This must be a namespace alias definition.  */
9762           cp_parser_declaration_statement (parser);
9763           return;
9764           
9765         case RID_TRANSACTION_ATOMIC:
9766         case RID_TRANSACTION_RELAXED:
9767           statement = cp_parser_transaction (parser, keyword);
9768           break;
9769         case RID_TRANSACTION_CANCEL:
9770           statement = cp_parser_transaction_cancel (parser);
9771           break;
9772
9773         default:
9774           /* It might be a keyword like `int' that can start a
9775              declaration-statement.  */
9776           break;
9777         }
9778     }
9779   else if (token->type == CPP_NAME)
9780     {
9781       /* If the next token is a `:', then we are looking at a
9782          labeled-statement.  */
9783       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9784       if (token->type == CPP_COLON)
9785         {
9786           /* Looks like a labeled-statement with an ordinary label.
9787              Parse the label, and then use tail recursion to parse
9788              the statement.  */
9789
9790           cp_parser_label_for_labeled_statement (parser, std_attrs);
9791           goto restart;
9792         }
9793     }
9794   /* Anything that starts with a `{' must be a compound-statement.  */
9795   else if (token->type == CPP_OPEN_BRACE)
9796     statement = cp_parser_compound_statement (parser, NULL, false, false);
9797   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9798      a statement all its own.  */
9799   else if (token->type == CPP_PRAGMA)
9800     {
9801       /* Only certain OpenMP pragmas are attached to statements, and thus
9802          are considered statements themselves.  All others are not.  In
9803          the context of a compound, accept the pragma as a "statement" and
9804          return so that we can check for a close brace.  Otherwise we
9805          require a real statement and must go back and read one.  */
9806       if (in_compound)
9807         cp_parser_pragma (parser, pragma_compound);
9808       else if (!cp_parser_pragma (parser, pragma_stmt))
9809         goto restart;
9810       return;
9811     }
9812   else if (token->type == CPP_EOF)
9813     {
9814       cp_parser_error (parser, "expected statement");
9815       return;
9816     }
9817
9818   /* Everything else must be a declaration-statement or an
9819      expression-statement.  Try for the declaration-statement
9820      first, unless we are looking at a `;', in which case we know that
9821      we have an expression-statement.  */
9822   if (!statement)
9823     {
9824       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9825         {
9826           if (std_attrs != NULL_TREE)
9827             {
9828               /*  Attributes should be parsed as part of the the
9829                   declaration, so let's un-parse them.  */
9830               saved_tokens.rollback();
9831               std_attrs = NULL_TREE;
9832             }
9833
9834           cp_parser_parse_tentatively (parser);
9835           /* Try to parse the declaration-statement.  */
9836           cp_parser_declaration_statement (parser);
9837           /* If that worked, we're done.  */
9838           if (cp_parser_parse_definitely (parser))
9839             return;
9840         }
9841       /* Look for an expression-statement instead.  */
9842       statement = cp_parser_expression_statement (parser, in_statement_expr);
9843     }
9844
9845   /* Set the line number for the statement.  */
9846   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9847     SET_EXPR_LOCATION (statement, statement_location);
9848
9849   /* Note that for now, we don't do anything with c++11 statements
9850      parsed at this level.  */
9851   if (std_attrs != NULL_TREE)
9852     warning_at (attrs_location,
9853                 OPT_Wattributes,
9854                 "attributes at the beginning of statement are ignored");
9855 }
9856
9857 /* Parse the label for a labeled-statement, i.e.
9858
9859    identifier :
9860    case constant-expression :
9861    default :
9862
9863    GNU Extension:
9864    case constant-expression ... constant-expression : statement
9865
9866    When a label is parsed without errors, the label is added to the
9867    parse tree by the finish_* functions, so this function doesn't
9868    have to return the label.  */
9869
9870 static void
9871 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9872 {
9873   cp_token *token;
9874   tree label = NULL_TREE;
9875   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9876
9877   /* The next token should be an identifier.  */
9878   token = cp_lexer_peek_token (parser->lexer);
9879   if (token->type != CPP_NAME
9880       && token->type != CPP_KEYWORD)
9881     {
9882       cp_parser_error (parser, "expected labeled-statement");
9883       return;
9884     }
9885
9886   parser->colon_corrects_to_scope_p = false;
9887   switch (token->keyword)
9888     {
9889     case RID_CASE:
9890       {
9891         tree expr, expr_hi;
9892         cp_token *ellipsis;
9893
9894         /* Consume the `case' token.  */
9895         cp_lexer_consume_token (parser->lexer);
9896         /* Parse the constant-expression.  */
9897         expr = cp_parser_constant_expression (parser);
9898         if (check_for_bare_parameter_packs (expr))
9899           expr = error_mark_node;
9900
9901         ellipsis = cp_lexer_peek_token (parser->lexer);
9902         if (ellipsis->type == CPP_ELLIPSIS)
9903           {
9904             /* Consume the `...' token.  */
9905             cp_lexer_consume_token (parser->lexer);
9906             expr_hi = cp_parser_constant_expression (parser);
9907             if (check_for_bare_parameter_packs (expr_hi))
9908               expr_hi = error_mark_node;
9909
9910             /* We don't need to emit warnings here, as the common code
9911                will do this for us.  */
9912           }
9913         else
9914           expr_hi = NULL_TREE;
9915
9916         if (parser->in_switch_statement_p)
9917           finish_case_label (token->location, expr, expr_hi);
9918         else
9919           error_at (token->location,
9920                     "case label %qE not within a switch statement",
9921                     expr);
9922       }
9923       break;
9924
9925     case RID_DEFAULT:
9926       /* Consume the `default' token.  */
9927       cp_lexer_consume_token (parser->lexer);
9928
9929       if (parser->in_switch_statement_p)
9930         finish_case_label (token->location, NULL_TREE, NULL_TREE);
9931       else
9932         error_at (token->location, "case label not within a switch statement");
9933       break;
9934
9935     default:
9936       /* Anything else must be an ordinary label.  */
9937       label = finish_label_stmt (cp_parser_identifier (parser));
9938       break;
9939     }
9940
9941   /* Require the `:' token.  */
9942   cp_parser_require (parser, CPP_COLON, RT_COLON);
9943
9944   /* An ordinary label may optionally be followed by attributes.
9945      However, this is only permitted if the attributes are then
9946      followed by a semicolon.  This is because, for backward
9947      compatibility, when parsing
9948        lab: __attribute__ ((unused)) int i;
9949      we want the attribute to attach to "i", not "lab".  */
9950   if (label != NULL_TREE
9951       && cp_next_tokens_can_be_gnu_attribute_p (parser))
9952     {
9953       tree attrs;
9954       cp_parser_parse_tentatively (parser);
9955       attrs = cp_parser_gnu_attributes_opt (parser);
9956       if (attrs == NULL_TREE
9957           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9958         cp_parser_abort_tentative_parse (parser);
9959       else if (!cp_parser_parse_definitely (parser))
9960         ;
9961       else
9962         attributes = chainon (attributes, attrs);
9963     }
9964
9965   if (attributes != NULL_TREE)
9966     cplus_decl_attributes (&label, attributes, 0);
9967
9968   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9969 }
9970
9971 /* Parse an expression-statement.
9972
9973    expression-statement:
9974      expression [opt] ;
9975
9976    Returns the new EXPR_STMT -- or NULL_TREE if the expression
9977    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9978    indicates whether this expression-statement is part of an
9979    expression statement.  */
9980
9981 static tree
9982 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9983 {
9984   tree statement = NULL_TREE;
9985   cp_token *token = cp_lexer_peek_token (parser->lexer);
9986
9987   /* If the next token is a ';', then there is no expression
9988      statement.  */
9989   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9990     {
9991       statement = cp_parser_expression (parser);
9992       if (statement == error_mark_node
9993           && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9994         {
9995           cp_parser_skip_to_end_of_block_or_statement (parser);
9996           return error_mark_node;
9997         }
9998     }
9999
10000   /* Give a helpful message for "A<T>::type t;" and the like.  */
10001   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10002       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10003     {
10004       if (TREE_CODE (statement) == SCOPE_REF)
10005         error_at (token->location, "need %<typename%> before %qE because "
10006                   "%qT is a dependent scope",
10007                   statement, TREE_OPERAND (statement, 0));
10008       else if (is_overloaded_fn (statement)
10009                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10010         {
10011           /* A::A a; */
10012           tree fn = get_first_fn (statement);
10013           error_at (token->location,
10014                     "%<%T::%D%> names the constructor, not the type",
10015                     DECL_CONTEXT (fn), DECL_NAME (fn));
10016         }
10017     }
10018
10019   /* Consume the final `;'.  */
10020   cp_parser_consume_semicolon_at_end_of_statement (parser);
10021
10022   if (in_statement_expr
10023       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10024     /* This is the final expression statement of a statement
10025        expression.  */
10026     statement = finish_stmt_expr_expr (statement, in_statement_expr);
10027   else if (statement)
10028     statement = finish_expr_stmt (statement);
10029
10030   return statement;
10031 }
10032
10033 /* Parse a compound-statement.
10034
10035    compound-statement:
10036      { statement-seq [opt] }
10037
10038    GNU extension:
10039
10040    compound-statement:
10041      { label-declaration-seq [opt] statement-seq [opt] }
10042
10043    label-declaration-seq:
10044      label-declaration
10045      label-declaration-seq label-declaration
10046
10047    Returns a tree representing the statement.  */
10048
10049 static tree
10050 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10051                               bool in_try, bool function_body)
10052 {
10053   tree compound_stmt;
10054
10055   /* Consume the `{'.  */
10056   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10057     return error_mark_node;
10058   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10059       && !function_body && cxx_dialect < cxx14)
10060     pedwarn (input_location, OPT_Wpedantic,
10061              "compound-statement in constexpr function");
10062   /* Begin the compound-statement.  */
10063   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10064   /* If the next keyword is `__label__' we have a label declaration.  */
10065   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10066     cp_parser_label_declaration (parser);
10067   /* Parse an (optional) statement-seq.  */
10068   cp_parser_statement_seq_opt (parser, in_statement_expr);
10069   /* Finish the compound-statement.  */
10070   finish_compound_stmt (compound_stmt);
10071   /* Consume the `}'.  */
10072   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10073
10074   return compound_stmt;
10075 }
10076
10077 /* Parse an (optional) statement-seq.
10078
10079    statement-seq:
10080      statement
10081      statement-seq [opt] statement  */
10082
10083 static void
10084 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10085 {
10086   /* Scan statements until there aren't any more.  */
10087   while (true)
10088     {
10089       cp_token *token = cp_lexer_peek_token (parser->lexer);
10090
10091       /* If we are looking at a `}', then we have run out of
10092          statements; the same is true if we have reached the end
10093          of file, or have stumbled upon a stray '@end'.  */
10094       if (token->type == CPP_CLOSE_BRACE
10095           || token->type == CPP_EOF
10096           || token->type == CPP_PRAGMA_EOL
10097           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10098         break;
10099       
10100       /* If we are in a compound statement and find 'else' then
10101          something went wrong.  */
10102       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10103         {
10104           if (parser->in_statement & IN_IF_STMT) 
10105             break;
10106           else
10107             {
10108               token = cp_lexer_consume_token (parser->lexer);
10109               error_at (token->location, "%<else%> without a previous %<if%>");
10110             }
10111         }
10112
10113       /* Parse the statement.  */
10114       cp_parser_statement (parser, in_statement_expr, true, NULL);
10115     }
10116 }
10117
10118 /* Parse a selection-statement.
10119
10120    selection-statement:
10121      if ( condition ) statement
10122      if ( condition ) statement else statement
10123      switch ( condition ) statement
10124
10125    Returns the new IF_STMT or SWITCH_STMT.
10126
10127    If IF_P is not NULL, *IF_P is set to indicate whether the statement
10128    is a (possibly labeled) if statement which is not enclosed in
10129    braces and has an else clause.  This is used to implement
10130    -Wparentheses.  */
10131
10132 static tree
10133 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10134 {
10135   cp_token *token;
10136   enum rid keyword;
10137
10138   if (if_p != NULL)
10139     *if_p = false;
10140
10141   /* Peek at the next token.  */
10142   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10143
10144   /* See what kind of keyword it is.  */
10145   keyword = token->keyword;
10146   switch (keyword)
10147     {
10148     case RID_IF:
10149     case RID_SWITCH:
10150       {
10151         tree statement;
10152         tree condition;
10153
10154         /* Look for the `('.  */
10155         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10156           {
10157             cp_parser_skip_to_end_of_statement (parser);
10158             return error_mark_node;
10159           }
10160
10161         /* Begin the selection-statement.  */
10162         if (keyword == RID_IF)
10163           statement = begin_if_stmt ();
10164         else
10165           statement = begin_switch_stmt ();
10166
10167         /* Parse the condition.  */
10168         condition = cp_parser_condition (parser);
10169         /* Look for the `)'.  */
10170         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10171           cp_parser_skip_to_closing_parenthesis (parser, true, false,
10172                                                  /*consume_paren=*/true);
10173
10174         if (keyword == RID_IF)
10175           {
10176             bool nested_if;
10177             unsigned char in_statement;
10178
10179             /* Add the condition.  */
10180             finish_if_stmt_cond (condition, statement);
10181
10182             /* Parse the then-clause.  */
10183             in_statement = parser->in_statement;
10184             parser->in_statement |= IN_IF_STMT;
10185             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10186               {
10187                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10188                 add_stmt (build_empty_stmt (loc));
10189                 cp_lexer_consume_token (parser->lexer);
10190                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10191                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
10192                               "empty body in an %<if%> statement");
10193                 nested_if = false;
10194               }
10195             else
10196               cp_parser_implicitly_scoped_statement (parser, &nested_if);
10197             parser->in_statement = in_statement;
10198
10199             finish_then_clause (statement);
10200
10201             /* If the next token is `else', parse the else-clause.  */
10202             if (cp_lexer_next_token_is_keyword (parser->lexer,
10203                                                 RID_ELSE))
10204               {
10205                 /* Consume the `else' keyword.  */
10206                 cp_lexer_consume_token (parser->lexer);
10207                 begin_else_clause (statement);
10208                 /* Parse the else-clause.  */
10209                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10210                   {
10211                     location_t loc;
10212                     loc = cp_lexer_peek_token (parser->lexer)->location;
10213                     warning_at (loc,
10214                                 OPT_Wempty_body, "suggest braces around "
10215                                 "empty body in an %<else%> statement");
10216                     add_stmt (build_empty_stmt (loc));
10217                     cp_lexer_consume_token (parser->lexer);
10218                   }
10219                 else
10220                   cp_parser_implicitly_scoped_statement (parser, NULL);
10221
10222                 finish_else_clause (statement);
10223
10224                 /* If we are currently parsing a then-clause, then
10225                    IF_P will not be NULL.  We set it to true to
10226                    indicate that this if statement has an else clause.
10227                    This may trigger the Wparentheses warning below
10228                    when we get back up to the parent if statement.  */
10229                 if (if_p != NULL)
10230                   *if_p = true;
10231               }
10232             else
10233               {
10234                 /* This if statement does not have an else clause.  If
10235                    NESTED_IF is true, then the then-clause is an if
10236                    statement which does have an else clause.  We warn
10237                    about the potential ambiguity.  */
10238                 if (nested_if)
10239                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10240                               "suggest explicit braces to avoid ambiguous"
10241                               " %<else%>");
10242               }
10243
10244             /* Now we're all done with the if-statement.  */
10245             finish_if_stmt (statement);
10246           }
10247         else
10248           {
10249             bool in_switch_statement_p;
10250             unsigned char in_statement;
10251
10252             /* Add the condition.  */
10253             finish_switch_cond (condition, statement);
10254
10255             /* Parse the body of the switch-statement.  */
10256             in_switch_statement_p = parser->in_switch_statement_p;
10257             in_statement = parser->in_statement;
10258             parser->in_switch_statement_p = true;
10259             parser->in_statement |= IN_SWITCH_STMT;
10260             cp_parser_implicitly_scoped_statement (parser, NULL);
10261             parser->in_switch_statement_p = in_switch_statement_p;
10262             parser->in_statement = in_statement;
10263
10264             /* Now we're all done with the switch-statement.  */
10265             finish_switch_stmt (statement);
10266           }
10267
10268         return statement;
10269       }
10270       break;
10271
10272     default:
10273       cp_parser_error (parser, "expected selection-statement");
10274       return error_mark_node;
10275     }
10276 }
10277
10278 /* Parse a condition.
10279
10280    condition:
10281      expression
10282      type-specifier-seq declarator = initializer-clause
10283      type-specifier-seq declarator braced-init-list
10284
10285    GNU Extension:
10286
10287    condition:
10288      type-specifier-seq declarator asm-specification [opt]
10289        attributes [opt] = assignment-expression
10290
10291    Returns the expression that should be tested.  */
10292
10293 static tree
10294 cp_parser_condition (cp_parser* parser)
10295 {
10296   cp_decl_specifier_seq type_specifiers;
10297   const char *saved_message;
10298   int declares_class_or_enum;
10299
10300   /* Try the declaration first.  */
10301   cp_parser_parse_tentatively (parser);
10302   /* New types are not allowed in the type-specifier-seq for a
10303      condition.  */
10304   saved_message = parser->type_definition_forbidden_message;
10305   parser->type_definition_forbidden_message
10306     = G_("types may not be defined in conditions");
10307   /* Parse the type-specifier-seq.  */
10308   cp_parser_decl_specifier_seq (parser,
10309                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10310                                 &type_specifiers,
10311                                 &declares_class_or_enum);
10312   /* Restore the saved message.  */
10313   parser->type_definition_forbidden_message = saved_message;
10314   /* If all is well, we might be looking at a declaration.  */
10315   if (!cp_parser_error_occurred (parser))
10316     {
10317       tree decl;
10318       tree asm_specification;
10319       tree attributes;
10320       cp_declarator *declarator;
10321       tree initializer = NULL_TREE;
10322
10323       /* Parse the declarator.  */
10324       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10325                                          /*ctor_dtor_or_conv_p=*/NULL,
10326                                          /*parenthesized_p=*/NULL,
10327                                          /*member_p=*/false,
10328                                          /*friend_p=*/false);
10329       /* Parse the attributes.  */
10330       attributes = cp_parser_attributes_opt (parser);
10331       /* Parse the asm-specification.  */
10332       asm_specification = cp_parser_asm_specification_opt (parser);
10333       /* If the next token is not an `=' or '{', then we might still be
10334          looking at an expression.  For example:
10335
10336            if (A(a).x)
10337
10338          looks like a decl-specifier-seq and a declarator -- but then
10339          there is no `=', so this is an expression.  */
10340       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10341           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10342         cp_parser_simulate_error (parser);
10343         
10344       /* If we did see an `=' or '{', then we are looking at a declaration
10345          for sure.  */
10346       if (cp_parser_parse_definitely (parser))
10347         {
10348           tree pushed_scope;
10349           bool non_constant_p;
10350           bool flags = LOOKUP_ONLYCONVERTING;
10351
10352           /* Create the declaration.  */
10353           decl = start_decl (declarator, &type_specifiers,
10354                              /*initialized_p=*/true,
10355                              attributes, /*prefix_attributes=*/NULL_TREE,
10356                              &pushed_scope);
10357
10358           /* Parse the initializer.  */
10359           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10360             {
10361               initializer = cp_parser_braced_list (parser, &non_constant_p);
10362               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10363               flags = 0;
10364             }
10365           else
10366             {
10367               /* Consume the `='.  */
10368               cp_parser_require (parser, CPP_EQ, RT_EQ);
10369               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10370             }
10371           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10372             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10373
10374           /* Process the initializer.  */
10375           cp_finish_decl (decl,
10376                           initializer, !non_constant_p,
10377                           asm_specification,
10378                           flags);
10379
10380           if (pushed_scope)
10381             pop_scope (pushed_scope);
10382
10383           return convert_from_reference (decl);
10384         }
10385     }
10386   /* If we didn't even get past the declarator successfully, we are
10387      definitely not looking at a declaration.  */
10388   else
10389     cp_parser_abort_tentative_parse (parser);
10390
10391   /* Otherwise, we are looking at an expression.  */
10392   return cp_parser_expression (parser);
10393 }
10394
10395 /* Parses a for-statement or range-for-statement until the closing ')',
10396    not included. */
10397
10398 static tree
10399 cp_parser_for (cp_parser *parser, bool ivdep)
10400 {
10401   tree init, scope, decl;
10402   bool is_range_for;
10403
10404   /* Begin the for-statement.  */
10405   scope = begin_for_scope (&init);
10406
10407   /* Parse the initialization.  */
10408   is_range_for = cp_parser_for_init_statement (parser, &decl);
10409
10410   if (is_range_for)
10411     return cp_parser_range_for (parser, scope, init, decl, ivdep);
10412   else
10413     return cp_parser_c_for (parser, scope, init, ivdep);
10414 }
10415
10416 static tree
10417 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10418 {
10419   /* Normal for loop */
10420   tree condition = NULL_TREE;
10421   tree expression = NULL_TREE;
10422   tree stmt;
10423
10424   stmt = begin_for_stmt (scope, init);
10425   /* The for-init-statement has already been parsed in
10426      cp_parser_for_init_statement, so no work is needed here.  */
10427   finish_for_init_stmt (stmt);
10428
10429   /* If there's a condition, process it.  */
10430   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10431     condition = cp_parser_condition (parser);
10432   else if (ivdep)
10433     {
10434       cp_parser_error (parser, "missing loop condition in loop with "
10435                        "%<GCC ivdep%> pragma");
10436       condition = error_mark_node;
10437     }
10438   finish_for_cond (condition, stmt, ivdep);
10439   /* Look for the `;'.  */
10440   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10441
10442   /* If there's an expression, process it.  */
10443   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10444     expression = cp_parser_expression (parser);
10445   finish_for_expr (expression, stmt);
10446
10447   return stmt;
10448 }
10449
10450 /* Tries to parse a range-based for-statement:
10451
10452   range-based-for:
10453     decl-specifier-seq declarator : expression
10454
10455   The decl-specifier-seq declarator and the `:' are already parsed by
10456   cp_parser_for_init_statement. If processing_template_decl it returns a
10457   newly created RANGE_FOR_STMT; if not, it is converted to a
10458   regular FOR_STMT.  */
10459
10460 static tree
10461 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10462                      bool ivdep)
10463 {
10464   tree stmt, range_expr;
10465
10466   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10467     {
10468       bool expr_non_constant_p;
10469       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10470     }
10471   else
10472     range_expr = cp_parser_expression (parser);
10473
10474   /* If in template, STMT is converted to a normal for-statement
10475      at instantiation. If not, it is done just ahead. */
10476   if (processing_template_decl)
10477     {
10478       if (check_for_bare_parameter_packs (range_expr))
10479         range_expr = error_mark_node;
10480       stmt = begin_range_for_stmt (scope, init);
10481       if (ivdep)
10482         RANGE_FOR_IVDEP (stmt) = 1;
10483       finish_range_for_decl (stmt, range_decl, range_expr);
10484       if (!type_dependent_expression_p (range_expr)
10485           /* do_auto_deduction doesn't mess with template init-lists.  */
10486           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10487         do_range_for_auto_deduction (range_decl, range_expr);
10488     }
10489   else
10490     {
10491       stmt = begin_for_stmt (scope, init);
10492       stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10493     }
10494   return stmt;
10495 }
10496
10497 /* Subroutine of cp_convert_range_for: given the initializer expression,
10498    builds up the range temporary.  */
10499
10500 static tree
10501 build_range_temp (tree range_expr)
10502 {
10503   tree range_type, range_temp;
10504
10505   /* Find out the type deduced by the declaration
10506      `auto &&__range = range_expr'.  */
10507   range_type = cp_build_reference_type (make_auto (), true);
10508   range_type = do_auto_deduction (range_type, range_expr,
10509                                   type_uses_auto (range_type));
10510
10511   /* Create the __range variable.  */
10512   range_temp = build_decl (input_location, VAR_DECL,
10513                            get_identifier ("__for_range"), range_type);
10514   TREE_USED (range_temp) = 1;
10515   DECL_ARTIFICIAL (range_temp) = 1;
10516
10517   return range_temp;
10518 }
10519
10520 /* Used by cp_parser_range_for in template context: we aren't going to
10521    do a full conversion yet, but we still need to resolve auto in the
10522    type of the for-range-declaration if present.  This is basically
10523    a shortcut version of cp_convert_range_for.  */
10524
10525 static void
10526 do_range_for_auto_deduction (tree decl, tree range_expr)
10527 {
10528   tree auto_node = type_uses_auto (TREE_TYPE (decl));
10529   if (auto_node)
10530     {
10531       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10532       range_temp = convert_from_reference (build_range_temp (range_expr));
10533       iter_type = (cp_parser_perform_range_for_lookup
10534                    (range_temp, &begin_dummy, &end_dummy));
10535       if (iter_type)
10536         {
10537           iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10538                                   iter_type);
10539           iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10540                                             tf_warning_or_error);
10541           TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10542                                                 iter_decl, auto_node);
10543         }
10544     }
10545 }
10546
10547 /* Converts a range-based for-statement into a normal
10548    for-statement, as per the definition.
10549
10550       for (RANGE_DECL : RANGE_EXPR)
10551         BLOCK
10552
10553    should be equivalent to:
10554
10555       {
10556         auto &&__range = RANGE_EXPR;
10557         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10558               __begin != __end;
10559               ++__begin)
10560           {
10561               RANGE_DECL = *__begin;
10562               BLOCK
10563           }
10564       }
10565
10566    If RANGE_EXPR is an array:
10567         BEGIN_EXPR = __range
10568         END_EXPR = __range + ARRAY_SIZE(__range)
10569    Else if RANGE_EXPR has a member 'begin' or 'end':
10570         BEGIN_EXPR = __range.begin()
10571         END_EXPR = __range.end()
10572    Else:
10573         BEGIN_EXPR = begin(__range)
10574         END_EXPR = end(__range);
10575
10576    If __range has a member 'begin' but not 'end', or vice versa, we must
10577    still use the second alternative (it will surely fail, however).
10578    When calling begin()/end() in the third alternative we must use
10579    argument dependent lookup, but always considering 'std' as an associated
10580    namespace.  */
10581
10582 tree
10583 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10584                       bool ivdep)
10585 {
10586   tree begin, end;
10587   tree iter_type, begin_expr, end_expr;
10588   tree condition, expression;
10589
10590   if (range_decl == error_mark_node || range_expr == error_mark_node)
10591     /* If an error happened previously do nothing or else a lot of
10592        unhelpful errors would be issued.  */
10593     begin_expr = end_expr = iter_type = error_mark_node;
10594   else
10595     {
10596       tree range_temp;
10597
10598       if (TREE_CODE (range_expr) == VAR_DECL
10599           && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10600         /* Can't bind a reference to an array of runtime bound.  */
10601         range_temp = range_expr;
10602       else
10603         {
10604           range_temp = build_range_temp (range_expr);
10605           pushdecl (range_temp);
10606           cp_finish_decl (range_temp, range_expr,
10607                           /*is_constant_init*/false, NULL_TREE,
10608                           LOOKUP_ONLYCONVERTING);
10609           range_temp = convert_from_reference (range_temp);
10610         }
10611       iter_type = cp_parser_perform_range_for_lookup (range_temp,
10612                                                       &begin_expr, &end_expr);
10613     }
10614
10615   /* The new for initialization statement.  */
10616   begin = build_decl (input_location, VAR_DECL,
10617                       get_identifier ("__for_begin"), iter_type);
10618   TREE_USED (begin) = 1;
10619   DECL_ARTIFICIAL (begin) = 1;
10620   pushdecl (begin);
10621   cp_finish_decl (begin, begin_expr,
10622                   /*is_constant_init*/false, NULL_TREE,
10623                   LOOKUP_ONLYCONVERTING);
10624
10625   end = build_decl (input_location, VAR_DECL,
10626                     get_identifier ("__for_end"), iter_type);
10627   TREE_USED (end) = 1;
10628   DECL_ARTIFICIAL (end) = 1;
10629   pushdecl (end);
10630   cp_finish_decl (end, end_expr,
10631                   /*is_constant_init*/false, NULL_TREE,
10632                   LOOKUP_ONLYCONVERTING);
10633
10634   finish_for_init_stmt (statement);
10635
10636   /* The new for condition.  */
10637   condition = build_x_binary_op (input_location, NE_EXPR,
10638                                  begin, ERROR_MARK,
10639                                  end, ERROR_MARK,
10640                                  NULL, tf_warning_or_error);
10641   finish_for_cond (condition, statement, ivdep);
10642
10643   /* The new increment expression.  */
10644   expression = finish_unary_op_expr (input_location,
10645                                      PREINCREMENT_EXPR, begin,
10646                                      tf_warning_or_error);
10647   finish_for_expr (expression, statement);
10648
10649   /* The declaration is initialized with *__begin inside the loop body.  */
10650   cp_finish_decl (range_decl,
10651                   build_x_indirect_ref (input_location, begin, RO_NULL,
10652                                         tf_warning_or_error),
10653                   /*is_constant_init*/false, NULL_TREE,
10654                   LOOKUP_ONLYCONVERTING);
10655
10656   return statement;
10657 }
10658
10659 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10660    We need to solve both at the same time because the method used
10661    depends on the existence of members begin or end.
10662    Returns the type deduced for the iterator expression.  */
10663
10664 static tree
10665 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10666 {
10667   if (error_operand_p (range))
10668     {
10669       *begin = *end = error_mark_node;
10670       return error_mark_node;
10671     }
10672
10673   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10674     {
10675       error ("range-based %<for%> expression of type %qT "
10676              "has incomplete type", TREE_TYPE (range));
10677       *begin = *end = error_mark_node;
10678       return error_mark_node;
10679     }
10680   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10681     {
10682       /* If RANGE is an array, we will use pointer arithmetic.  */
10683       *begin = range;
10684       *end = build_binary_op (input_location, PLUS_EXPR,
10685                               range,
10686                               array_type_nelts_top (TREE_TYPE (range)),
10687                               0);
10688       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10689     }
10690   else
10691     {
10692       /* If it is not an array, we must do a bit of magic.  */
10693       tree id_begin, id_end;
10694       tree member_begin, member_end;
10695
10696       *begin = *end = error_mark_node;
10697
10698       id_begin = get_identifier ("begin");
10699       id_end = get_identifier ("end");
10700       member_begin = lookup_member (TREE_TYPE (range), id_begin,
10701                                     /*protect=*/2, /*want_type=*/false,
10702                                     tf_warning_or_error);
10703       member_end = lookup_member (TREE_TYPE (range), id_end,
10704                                   /*protect=*/2, /*want_type=*/false,
10705                                   tf_warning_or_error);
10706
10707       if (member_begin != NULL_TREE || member_end != NULL_TREE)
10708         {
10709           /* Use the member functions.  */
10710           if (member_begin != NULL_TREE)
10711             *begin = cp_parser_range_for_member_function (range, id_begin);
10712           else
10713             error ("range-based %<for%> expression of type %qT has an "
10714                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10715
10716           if (member_end != NULL_TREE)
10717             *end = cp_parser_range_for_member_function (range, id_end);
10718           else
10719             error ("range-based %<for%> expression of type %qT has a "
10720                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10721         }
10722       else
10723         {
10724           /* Use global functions with ADL.  */
10725           vec<tree, va_gc> *vec;
10726           vec = make_tree_vector ();
10727
10728           vec_safe_push (vec, range);
10729
10730           member_begin = perform_koenig_lookup (id_begin, vec,
10731                                                 tf_warning_or_error);
10732           *begin = finish_call_expr (member_begin, &vec, false, true,
10733                                      tf_warning_or_error);
10734           member_end = perform_koenig_lookup (id_end, vec,
10735                                               tf_warning_or_error);
10736           *end = finish_call_expr (member_end, &vec, false, true,
10737                                    tf_warning_or_error);
10738
10739           release_tree_vector (vec);
10740         }
10741
10742       /* Last common checks.  */
10743       if (*begin == error_mark_node || *end == error_mark_node)
10744         {
10745           /* If one of the expressions is an error do no more checks.  */
10746           *begin = *end = error_mark_node;
10747           return error_mark_node;
10748         }
10749       else if (type_dependent_expression_p (*begin)
10750                || type_dependent_expression_p (*end))
10751         /* Can happen, when, eg, in a template context, Koenig lookup
10752            can't resolve begin/end (c++/58503).  */
10753         return NULL_TREE;
10754       else
10755         {
10756           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10757           /* The unqualified type of the __begin and __end temporaries should
10758              be the same, as required by the multiple auto declaration.  */
10759           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10760             error ("inconsistent begin/end types in range-based %<for%> "
10761                    "statement: %qT and %qT",
10762                    TREE_TYPE (*begin), TREE_TYPE (*end));
10763           return iter_type;
10764         }
10765     }
10766 }
10767
10768 /* Helper function for cp_parser_perform_range_for_lookup.
10769    Builds a tree for RANGE.IDENTIFIER().  */
10770
10771 static tree
10772 cp_parser_range_for_member_function (tree range, tree identifier)
10773 {
10774   tree member, res;
10775   vec<tree, va_gc> *vec;
10776
10777   member = finish_class_member_access_expr (range, identifier,
10778                                             false, tf_warning_or_error);
10779   if (member == error_mark_node)
10780     return error_mark_node;
10781
10782   vec = make_tree_vector ();
10783   res = finish_call_expr (member, &vec,
10784                           /*disallow_virtual=*/false,
10785                           /*koenig_p=*/false,
10786                           tf_warning_or_error);
10787   release_tree_vector (vec);
10788   return res;
10789 }
10790
10791 /* Parse an iteration-statement.
10792
10793    iteration-statement:
10794      while ( condition ) statement
10795      do statement while ( expression ) ;
10796      for ( for-init-statement condition [opt] ; expression [opt] )
10797        statement
10798
10799    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
10800
10801 static tree
10802 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10803 {
10804   cp_token *token;
10805   enum rid keyword;
10806   tree statement;
10807   unsigned char in_statement;
10808
10809   /* Peek at the next token.  */
10810   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10811   if (!token)
10812     return error_mark_node;
10813
10814   /* Remember whether or not we are already within an iteration
10815      statement.  */
10816   in_statement = parser->in_statement;
10817
10818   /* See what kind of keyword it is.  */
10819   keyword = token->keyword;
10820   switch (keyword)
10821     {
10822     case RID_WHILE:
10823       {
10824         tree condition;
10825
10826         /* Begin the while-statement.  */
10827         statement = begin_while_stmt ();
10828         /* Look for the `('.  */
10829         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10830         /* Parse the condition.  */
10831         condition = cp_parser_condition (parser);
10832         finish_while_stmt_cond (condition, statement, ivdep);
10833         /* Look for the `)'.  */
10834         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10835         /* Parse the dependent statement.  */
10836         parser->in_statement = IN_ITERATION_STMT;
10837         cp_parser_already_scoped_statement (parser);
10838         parser->in_statement = in_statement;
10839         /* We're done with the while-statement.  */
10840         finish_while_stmt (statement);
10841       }
10842       break;
10843
10844     case RID_DO:
10845       {
10846         tree expression;
10847
10848         /* Begin the do-statement.  */
10849         statement = begin_do_stmt ();
10850         /* Parse the body of the do-statement.  */
10851         parser->in_statement = IN_ITERATION_STMT;
10852         cp_parser_implicitly_scoped_statement (parser, NULL);
10853         parser->in_statement = in_statement;
10854         finish_do_body (statement);
10855         /* Look for the `while' keyword.  */
10856         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10857         /* Look for the `('.  */
10858         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10859         /* Parse the expression.  */
10860         expression = cp_parser_expression (parser);
10861         /* We're done with the do-statement.  */
10862         finish_do_stmt (expression, statement, ivdep);
10863         /* Look for the `)'.  */
10864         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10865         /* Look for the `;'.  */
10866         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10867       }
10868       break;
10869
10870     case RID_FOR:
10871       {
10872         /* Look for the `('.  */
10873         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10874
10875         statement = cp_parser_for (parser, ivdep);
10876
10877         /* Look for the `)'.  */
10878         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10879
10880         /* Parse the body of the for-statement.  */
10881         parser->in_statement = IN_ITERATION_STMT;
10882         cp_parser_already_scoped_statement (parser);
10883         parser->in_statement = in_statement;
10884
10885         /* We're done with the for-statement.  */
10886         finish_for_stmt (statement);
10887       }
10888       break;
10889
10890     default:
10891       cp_parser_error (parser, "expected iteration-statement");
10892       statement = error_mark_node;
10893       break;
10894     }
10895
10896   return statement;
10897 }
10898
10899 /* Parse a for-init-statement or the declarator of a range-based-for.
10900    Returns true if a range-based-for declaration is seen.
10901
10902    for-init-statement:
10903      expression-statement
10904      simple-declaration  */
10905
10906 static bool
10907 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10908 {
10909   /* If the next token is a `;', then we have an empty
10910      expression-statement.  Grammatically, this is also a
10911      simple-declaration, but an invalid one, because it does not
10912      declare anything.  Therefore, if we did not handle this case
10913      specially, we would issue an error message about an invalid
10914      declaration.  */
10915   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10916     {
10917       bool is_range_for = false;
10918       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10919
10920       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10921           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10922         {
10923           /* N3994 -- for (id : init) ... */
10924           if (cxx_dialect < cxx1z)
10925             pedwarn (input_location, 0, "range-based for loop without a "
10926                      "type-specifier only available with "
10927                      "-std=c++1z or -std=gnu++1z");
10928           tree name = cp_parser_identifier (parser);
10929           tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10930           *decl = build_decl (input_location, VAR_DECL, name, type);
10931           pushdecl (*decl);
10932           cp_lexer_consume_token (parser->lexer);
10933           return true;
10934         }
10935
10936       /* A colon is used in range-based for.  */
10937       parser->colon_corrects_to_scope_p = false;
10938
10939       /* We're going to speculatively look for a declaration, falling back
10940          to an expression, if necessary.  */
10941       cp_parser_parse_tentatively (parser);
10942       /* Parse the declaration.  */
10943       cp_parser_simple_declaration (parser,
10944                                     /*function_definition_allowed_p=*/false,
10945                                     decl);
10946       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10947       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10948         {
10949           /* It is a range-for, consume the ':' */
10950           cp_lexer_consume_token (parser->lexer);
10951           is_range_for = true;
10952           if (cxx_dialect < cxx11)
10953             {
10954               pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10955                        "range-based %<for%> loops only available with "
10956                        "-std=c++11 or -std=gnu++11");
10957               *decl = error_mark_node;
10958             }
10959         }
10960       else
10961           /* The ';' is not consumed yet because we told
10962              cp_parser_simple_declaration not to.  */
10963           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10964
10965       if (cp_parser_parse_definitely (parser))
10966         return is_range_for;
10967       /* If the tentative parse failed, then we shall need to look for an
10968          expression-statement.  */
10969     }
10970   /* If we are here, it is an expression-statement.  */
10971   cp_parser_expression_statement (parser, NULL_TREE);
10972   return false;
10973 }
10974
10975 /* Parse a jump-statement.
10976
10977    jump-statement:
10978      break ;
10979      continue ;
10980      return expression [opt] ;
10981      return braced-init-list ;
10982      goto identifier ;
10983
10984    GNU extension:
10985
10986    jump-statement:
10987      goto * expression ;
10988
10989    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
10990
10991 static tree
10992 cp_parser_jump_statement (cp_parser* parser)
10993 {
10994   tree statement = error_mark_node;
10995   cp_token *token;
10996   enum rid keyword;
10997   unsigned char in_statement;
10998
10999   /* Peek at the next token.  */
11000   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11001   if (!token)
11002     return error_mark_node;
11003
11004   /* See what kind of keyword it is.  */
11005   keyword = token->keyword;
11006   switch (keyword)
11007     {
11008     case RID_BREAK:
11009       in_statement = parser->in_statement & ~IN_IF_STMT;      
11010       switch (in_statement)
11011         {
11012         case 0:
11013           error_at (token->location, "break statement not within loop or switch");
11014           break;
11015         default:
11016           gcc_assert ((in_statement & IN_SWITCH_STMT)
11017                       || in_statement == IN_ITERATION_STMT);
11018           statement = finish_break_stmt ();
11019           if (in_statement == IN_ITERATION_STMT)
11020             break_maybe_infinite_loop ();
11021           break;
11022         case IN_OMP_BLOCK:
11023           error_at (token->location, "invalid exit from OpenMP structured block");
11024           break;
11025         case IN_OMP_FOR:
11026           error_at (token->location, "break statement used with OpenMP for loop");
11027           break;
11028         case IN_CILK_SIMD_FOR:
11029           error_at (token->location, "break statement used with Cilk Plus for loop");
11030           break;
11031         }
11032       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11033       break;
11034
11035     case RID_CONTINUE:
11036       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11037         {
11038         case 0:
11039           error_at (token->location, "continue statement not within a loop");
11040           break;
11041         case IN_CILK_SIMD_FOR:
11042           error_at (token->location,
11043                     "continue statement within %<#pragma simd%> loop body");
11044           /* Fall through.  */
11045         case IN_ITERATION_STMT:
11046         case IN_OMP_FOR:
11047           statement = finish_continue_stmt ();
11048           break;
11049         case IN_OMP_BLOCK:
11050           error_at (token->location, "invalid exit from OpenMP structured block");
11051           break;
11052         default:
11053           gcc_unreachable ();
11054         }
11055       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11056       break;
11057
11058     case RID_RETURN:
11059       {
11060         tree expr;
11061         bool expr_non_constant_p;
11062
11063         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11064           {
11065             cp_lexer_set_source_position (parser->lexer);
11066             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11067             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11068           }
11069         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11070           expr = cp_parser_expression (parser);
11071         else
11072           /* If the next token is a `;', then there is no
11073              expression.  */
11074           expr = NULL_TREE;
11075         /* Build the return-statement.  */
11076         statement = finish_return_stmt (expr);
11077         /* Look for the final `;'.  */
11078         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11079       }
11080       break;
11081
11082     case RID_GOTO:
11083       if (parser->in_function_body
11084           && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11085         {
11086           error ("%<goto%> in %<constexpr%> function");
11087           cp_function_chain->invalid_constexpr = true;
11088         }
11089
11090       /* Create the goto-statement.  */
11091       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11092         {
11093           /* Issue a warning about this use of a GNU extension.  */
11094           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11095           /* Consume the '*' token.  */
11096           cp_lexer_consume_token (parser->lexer);
11097           /* Parse the dependent expression.  */
11098           finish_goto_stmt (cp_parser_expression (parser));
11099         }
11100       else
11101         finish_goto_stmt (cp_parser_identifier (parser));
11102       /* Look for the final `;'.  */
11103       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11104       break;
11105
11106     default:
11107       cp_parser_error (parser, "expected jump-statement");
11108       break;
11109     }
11110
11111   return statement;
11112 }
11113
11114 /* Parse a declaration-statement.
11115
11116    declaration-statement:
11117      block-declaration  */
11118
11119 static void
11120 cp_parser_declaration_statement (cp_parser* parser)
11121 {
11122   void *p;
11123
11124   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11125   p = obstack_alloc (&declarator_obstack, 0);
11126
11127  /* Parse the block-declaration.  */
11128   cp_parser_block_declaration (parser, /*statement_p=*/true);
11129
11130   /* Free any declarators allocated.  */
11131   obstack_free (&declarator_obstack, p);
11132 }
11133
11134 /* Some dependent statements (like `if (cond) statement'), are
11135    implicitly in their own scope.  In other words, if the statement is
11136    a single statement (as opposed to a compound-statement), it is
11137    none-the-less treated as if it were enclosed in braces.  Any
11138    declarations appearing in the dependent statement are out of scope
11139    after control passes that point.  This function parses a statement,
11140    but ensures that is in its own scope, even if it is not a
11141    compound-statement.
11142
11143    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11144    is a (possibly labeled) if statement which is not enclosed in
11145    braces and has an else clause.  This is used to implement
11146    -Wparentheses.
11147
11148    Returns the new statement.  */
11149
11150 static tree
11151 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11152 {
11153   tree statement;
11154
11155   if (if_p != NULL)
11156     *if_p = false;
11157
11158   /* Mark if () ; with a special NOP_EXPR.  */
11159   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11160     {
11161       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11162       cp_lexer_consume_token (parser->lexer);
11163       statement = add_stmt (build_empty_stmt (loc));
11164     }
11165   /* if a compound is opened, we simply parse the statement directly.  */
11166   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11167     statement = cp_parser_compound_statement (parser, NULL, false, false);
11168   /* If the token is not a `{', then we must take special action.  */
11169   else
11170     {
11171       /* Create a compound-statement.  */
11172       statement = begin_compound_stmt (0);
11173       /* Parse the dependent-statement.  */
11174       cp_parser_statement (parser, NULL_TREE, false, if_p);
11175       /* Finish the dummy compound-statement.  */
11176       finish_compound_stmt (statement);
11177     }
11178
11179   /* Return the statement.  */
11180   return statement;
11181 }
11182
11183 /* For some dependent statements (like `while (cond) statement'), we
11184    have already created a scope.  Therefore, even if the dependent
11185    statement is a compound-statement, we do not want to create another
11186    scope.  */
11187
11188 static void
11189 cp_parser_already_scoped_statement (cp_parser* parser)
11190 {
11191   /* If the token is a `{', then we must take special action.  */
11192   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11193     cp_parser_statement (parser, NULL_TREE, false, NULL);
11194   else
11195     {
11196       /* Avoid calling cp_parser_compound_statement, so that we
11197          don't create a new scope.  Do everything else by hand.  */
11198       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11199       /* If the next keyword is `__label__' we have a label declaration.  */
11200       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11201         cp_parser_label_declaration (parser);
11202       /* Parse an (optional) statement-seq.  */
11203       cp_parser_statement_seq_opt (parser, NULL_TREE);
11204       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11205     }
11206 }
11207
11208 /* Declarations [gram.dcl.dcl] */
11209
11210 /* Parse an optional declaration-sequence.
11211
11212    declaration-seq:
11213      declaration
11214      declaration-seq declaration  */
11215
11216 static void
11217 cp_parser_declaration_seq_opt (cp_parser* parser)
11218 {
11219   while (true)
11220     {
11221       cp_token *token;
11222
11223       token = cp_lexer_peek_token (parser->lexer);
11224
11225       if (token->type == CPP_CLOSE_BRACE
11226           || token->type == CPP_EOF
11227           || token->type == CPP_PRAGMA_EOL)
11228         break;
11229
11230       if (token->type == CPP_SEMICOLON)
11231         {
11232           /* A declaration consisting of a single semicolon is
11233              invalid.  Allow it unless we're being pedantic.  */
11234           cp_lexer_consume_token (parser->lexer);
11235           if (!in_system_header_at (input_location))
11236             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11237           continue;
11238         }
11239
11240       /* If we're entering or exiting a region that's implicitly
11241          extern "C", modify the lang context appropriately.  */
11242       if (!parser->implicit_extern_c && token->implicit_extern_c)
11243         {
11244           push_lang_context (lang_name_c);
11245           parser->implicit_extern_c = true;
11246         }
11247       else if (parser->implicit_extern_c && !token->implicit_extern_c)
11248         {
11249           pop_lang_context ();
11250           parser->implicit_extern_c = false;
11251         }
11252
11253       if (token->type == CPP_PRAGMA)
11254         {
11255           /* A top-level declaration can consist solely of a #pragma.
11256              A nested declaration cannot, so this is done here and not
11257              in cp_parser_declaration.  (A #pragma at block scope is
11258              handled in cp_parser_statement.)  */
11259           cp_parser_pragma (parser, pragma_external);
11260           continue;
11261         }
11262
11263       /* Parse the declaration itself.  */
11264       cp_parser_declaration (parser);
11265     }
11266 }
11267
11268 /* Parse a declaration.
11269
11270    declaration:
11271      block-declaration
11272      function-definition
11273      template-declaration
11274      explicit-instantiation
11275      explicit-specialization
11276      linkage-specification
11277      namespace-definition
11278
11279    GNU extension:
11280
11281    declaration:
11282       __extension__ declaration */
11283
11284 static void
11285 cp_parser_declaration (cp_parser* parser)
11286 {
11287   cp_token token1;
11288   cp_token token2;
11289   int saved_pedantic;
11290   void *p;
11291   tree attributes = NULL_TREE;
11292
11293   /* Check for the `__extension__' keyword.  */
11294   if (cp_parser_extension_opt (parser, &saved_pedantic))
11295     {
11296       /* Parse the qualified declaration.  */
11297       cp_parser_declaration (parser);
11298       /* Restore the PEDANTIC flag.  */
11299       pedantic = saved_pedantic;
11300
11301       return;
11302     }
11303
11304   /* Try to figure out what kind of declaration is present.  */
11305   token1 = *cp_lexer_peek_token (parser->lexer);
11306
11307   if (token1.type != CPP_EOF)
11308     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11309   else
11310     {
11311       token2.type = CPP_EOF;
11312       token2.keyword = RID_MAX;
11313     }
11314
11315   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11316   p = obstack_alloc (&declarator_obstack, 0);
11317
11318   /* If the next token is `extern' and the following token is a string
11319      literal, then we have a linkage specification.  */
11320   if (token1.keyword == RID_EXTERN
11321       && cp_parser_is_pure_string_literal (&token2))
11322     cp_parser_linkage_specification (parser);
11323   /* If the next token is `template', then we have either a template
11324      declaration, an explicit instantiation, or an explicit
11325      specialization.  */
11326   else if (token1.keyword == RID_TEMPLATE)
11327     {
11328       /* `template <>' indicates a template specialization.  */
11329       if (token2.type == CPP_LESS
11330           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11331         cp_parser_explicit_specialization (parser);
11332       /* `template <' indicates a template declaration.  */
11333       else if (token2.type == CPP_LESS)
11334         cp_parser_template_declaration (parser, /*member_p=*/false);
11335       /* Anything else must be an explicit instantiation.  */
11336       else
11337         cp_parser_explicit_instantiation (parser);
11338     }
11339   /* If the next token is `export', then we have a template
11340      declaration.  */
11341   else if (token1.keyword == RID_EXPORT)
11342     cp_parser_template_declaration (parser, /*member_p=*/false);
11343   /* If the next token is `extern', 'static' or 'inline' and the one
11344      after that is `template', we have a GNU extended explicit
11345      instantiation directive.  */
11346   else if (cp_parser_allow_gnu_extensions_p (parser)
11347            && (token1.keyword == RID_EXTERN
11348                || token1.keyword == RID_STATIC
11349                || token1.keyword == RID_INLINE)
11350            && token2.keyword == RID_TEMPLATE)
11351     cp_parser_explicit_instantiation (parser);
11352   /* If the next token is `namespace', check for a named or unnamed
11353      namespace definition.  */
11354   else if (token1.keyword == RID_NAMESPACE
11355            && (/* A named namespace definition.  */
11356                (token2.type == CPP_NAME
11357                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11358                     != CPP_EQ))
11359                /* An unnamed namespace definition.  */
11360                || token2.type == CPP_OPEN_BRACE
11361                || token2.keyword == RID_ATTRIBUTE))
11362     cp_parser_namespace_definition (parser);
11363   /* An inline (associated) namespace definition.  */
11364   else if (token1.keyword == RID_INLINE
11365            && token2.keyword == RID_NAMESPACE)
11366     cp_parser_namespace_definition (parser);
11367   /* Objective-C++ declaration/definition.  */
11368   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11369     cp_parser_objc_declaration (parser, NULL_TREE);
11370   else if (c_dialect_objc ()
11371            && token1.keyword == RID_ATTRIBUTE
11372            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11373     cp_parser_objc_declaration (parser, attributes);
11374   /* We must have either a block declaration or a function
11375      definition.  */
11376   else
11377     /* Try to parse a block-declaration, or a function-definition.  */
11378     cp_parser_block_declaration (parser, /*statement_p=*/false);
11379
11380   /* Free any declarators allocated.  */
11381   obstack_free (&declarator_obstack, p);
11382 }
11383
11384 /* Parse a block-declaration.
11385
11386    block-declaration:
11387      simple-declaration
11388      asm-definition
11389      namespace-alias-definition
11390      using-declaration
11391      using-directive
11392
11393    GNU Extension:
11394
11395    block-declaration:
11396      __extension__ block-declaration
11397
11398    C++0x Extension:
11399
11400    block-declaration:
11401      static_assert-declaration
11402
11403    If STATEMENT_P is TRUE, then this block-declaration is occurring as
11404    part of a declaration-statement.  */
11405
11406 static void
11407 cp_parser_block_declaration (cp_parser *parser,
11408                              bool      statement_p)
11409 {
11410   cp_token *token1;
11411   int saved_pedantic;
11412
11413   /* Check for the `__extension__' keyword.  */
11414   if (cp_parser_extension_opt (parser, &saved_pedantic))
11415     {
11416       /* Parse the qualified declaration.  */
11417       cp_parser_block_declaration (parser, statement_p);
11418       /* Restore the PEDANTIC flag.  */
11419       pedantic = saved_pedantic;
11420
11421       return;
11422     }
11423
11424   /* Peek at the next token to figure out which kind of declaration is
11425      present.  */
11426   token1 = cp_lexer_peek_token (parser->lexer);
11427
11428   /* If the next keyword is `asm', we have an asm-definition.  */
11429   if (token1->keyword == RID_ASM)
11430     {
11431       if (statement_p)
11432         cp_parser_commit_to_tentative_parse (parser);
11433       cp_parser_asm_definition (parser);
11434     }
11435   /* If the next keyword is `namespace', we have a
11436      namespace-alias-definition.  */
11437   else if (token1->keyword == RID_NAMESPACE)
11438     cp_parser_namespace_alias_definition (parser);
11439   /* If the next keyword is `using', we have a
11440      using-declaration, a using-directive, or an alias-declaration.  */
11441   else if (token1->keyword == RID_USING)
11442     {
11443       cp_token *token2;
11444
11445       if (statement_p)
11446         cp_parser_commit_to_tentative_parse (parser);
11447       /* If the token after `using' is `namespace', then we have a
11448          using-directive.  */
11449       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11450       if (token2->keyword == RID_NAMESPACE)
11451         cp_parser_using_directive (parser);
11452       /* If the second token after 'using' is '=', then we have an
11453          alias-declaration.  */
11454       else if (cxx_dialect >= cxx11
11455                && token2->type == CPP_NAME
11456                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11457                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11458         cp_parser_alias_declaration (parser);
11459       /* Otherwise, it's a using-declaration.  */
11460       else
11461         cp_parser_using_declaration (parser,
11462                                      /*access_declaration_p=*/false);
11463     }
11464   /* If the next keyword is `__label__' we have a misplaced label
11465      declaration.  */
11466   else if (token1->keyword == RID_LABEL)
11467     {
11468       cp_lexer_consume_token (parser->lexer);
11469       error_at (token1->location, "%<__label__%> not at the beginning of a block");
11470       cp_parser_skip_to_end_of_statement (parser);
11471       /* If the next token is now a `;', consume it.  */
11472       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11473         cp_lexer_consume_token (parser->lexer);
11474     }
11475   /* If the next token is `static_assert' we have a static assertion.  */
11476   else if (token1->keyword == RID_STATIC_ASSERT)
11477     cp_parser_static_assert (parser, /*member_p=*/false);
11478   /* Anything else must be a simple-declaration.  */
11479   else
11480     cp_parser_simple_declaration (parser, !statement_p,
11481                                   /*maybe_range_for_decl*/NULL);
11482 }
11483
11484 /* Parse a simple-declaration.
11485
11486    simple-declaration:
11487      decl-specifier-seq [opt] init-declarator-list [opt] ;
11488
11489    init-declarator-list:
11490      init-declarator
11491      init-declarator-list , init-declarator
11492
11493    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11494    function-definition as a simple-declaration.
11495
11496    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11497    parsed declaration if it is an uninitialized single declarator not followed
11498    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11499    if present, will not be consumed.  */
11500
11501 static void
11502 cp_parser_simple_declaration (cp_parser* parser,
11503                               bool function_definition_allowed_p,
11504                               tree *maybe_range_for_decl)
11505 {
11506   cp_decl_specifier_seq decl_specifiers;
11507   int declares_class_or_enum;
11508   bool saw_declarator;
11509   location_t comma_loc = UNKNOWN_LOCATION;
11510   location_t init_loc = UNKNOWN_LOCATION;
11511
11512   if (maybe_range_for_decl)
11513     *maybe_range_for_decl = NULL_TREE;
11514
11515   /* Defer access checks until we know what is being declared; the
11516      checks for names appearing in the decl-specifier-seq should be
11517      done as if we were in the scope of the thing being declared.  */
11518   push_deferring_access_checks (dk_deferred);
11519
11520   /* Parse the decl-specifier-seq.  We have to keep track of whether
11521      or not the decl-specifier-seq declares a named class or
11522      enumeration type, since that is the only case in which the
11523      init-declarator-list is allowed to be empty.
11524
11525      [dcl.dcl]
11526
11527      In a simple-declaration, the optional init-declarator-list can be
11528      omitted only when declaring a class or enumeration, that is when
11529      the decl-specifier-seq contains either a class-specifier, an
11530      elaborated-type-specifier, or an enum-specifier.  */
11531   cp_parser_decl_specifier_seq (parser,
11532                                 CP_PARSER_FLAGS_OPTIONAL,
11533                                 &decl_specifiers,
11534                                 &declares_class_or_enum);
11535   /* We no longer need to defer access checks.  */
11536   stop_deferring_access_checks ();
11537
11538   /* In a block scope, a valid declaration must always have a
11539      decl-specifier-seq.  By not trying to parse declarators, we can
11540      resolve the declaration/expression ambiguity more quickly.  */
11541   if (!function_definition_allowed_p
11542       && !decl_specifiers.any_specifiers_p)
11543     {
11544       cp_parser_error (parser, "expected declaration");
11545       goto done;
11546     }
11547
11548   /* If the next two tokens are both identifiers, the code is
11549      erroneous. The usual cause of this situation is code like:
11550
11551        T t;
11552
11553      where "T" should name a type -- but does not.  */
11554   if (!decl_specifiers.any_type_specifiers_p
11555       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11556     {
11557       /* If parsing tentatively, we should commit; we really are
11558          looking at a declaration.  */
11559       cp_parser_commit_to_tentative_parse (parser);
11560       /* Give up.  */
11561       goto done;
11562     }
11563
11564   /* If we have seen at least one decl-specifier, and the next token
11565      is not a parenthesis, then we must be looking at a declaration.
11566      (After "int (" we might be looking at a functional cast.)  */
11567   if (decl_specifiers.any_specifiers_p
11568       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11569       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11570       && !cp_parser_error_occurred (parser))
11571     cp_parser_commit_to_tentative_parse (parser);
11572
11573   /* Keep going until we hit the `;' at the end of the simple
11574      declaration.  */
11575   saw_declarator = false;
11576   while (cp_lexer_next_token_is_not (parser->lexer,
11577                                      CPP_SEMICOLON))
11578     {
11579       cp_token *token;
11580       bool function_definition_p;
11581       tree decl;
11582
11583       if (saw_declarator)
11584         {
11585           /* If we are processing next declarator, comma is expected */
11586           token = cp_lexer_peek_token (parser->lexer);
11587           gcc_assert (token->type == CPP_COMMA);
11588           cp_lexer_consume_token (parser->lexer);
11589           if (maybe_range_for_decl)
11590             {
11591               *maybe_range_for_decl = error_mark_node;
11592               if (comma_loc == UNKNOWN_LOCATION)
11593                 comma_loc = token->location;
11594             }
11595         }
11596       else
11597         saw_declarator = true;
11598
11599       /* Parse the init-declarator.  */
11600       decl = cp_parser_init_declarator (parser, &decl_specifiers,
11601                                         /*checks=*/NULL,
11602                                         function_definition_allowed_p,
11603                                         /*member_p=*/false,
11604                                         declares_class_or_enum,
11605                                         &function_definition_p,
11606                                         maybe_range_for_decl,
11607                                         &init_loc);
11608       /* If an error occurred while parsing tentatively, exit quickly.
11609          (That usually happens when in the body of a function; each
11610          statement is treated as a declaration-statement until proven
11611          otherwise.)  */
11612       if (cp_parser_error_occurred (parser))
11613         goto done;
11614       /* Handle function definitions specially.  */
11615       if (function_definition_p)
11616         {
11617           /* If the next token is a `,', then we are probably
11618              processing something like:
11619
11620                void f() {}, *p;
11621
11622              which is erroneous.  */
11623           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11624             {
11625               cp_token *token = cp_lexer_peek_token (parser->lexer);
11626               error_at (token->location,
11627                         "mixing"
11628                         " declarations and function-definitions is forbidden");
11629             }
11630           /* Otherwise, we're done with the list of declarators.  */
11631           else
11632             {
11633               pop_deferring_access_checks ();
11634               return;
11635             }
11636         }
11637       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11638         *maybe_range_for_decl = decl;
11639       /* The next token should be either a `,' or a `;'.  */
11640       token = cp_lexer_peek_token (parser->lexer);
11641       /* If it's a `,', there are more declarators to come.  */
11642       if (token->type == CPP_COMMA)
11643         /* will be consumed next time around */;
11644       /* If it's a `;', we are done.  */
11645       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11646         break;
11647       /* Anything else is an error.  */
11648       else
11649         {
11650           /* If we have already issued an error message we don't need
11651              to issue another one.  */
11652           if (decl != error_mark_node
11653               || cp_parser_uncommitted_to_tentative_parse_p (parser))
11654             cp_parser_error (parser, "expected %<,%> or %<;%>");
11655           /* Skip tokens until we reach the end of the statement.  */
11656           cp_parser_skip_to_end_of_statement (parser);
11657           /* If the next token is now a `;', consume it.  */
11658           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11659             cp_lexer_consume_token (parser->lexer);
11660           goto done;
11661         }
11662       /* After the first time around, a function-definition is not
11663          allowed -- even if it was OK at first.  For example:
11664
11665            int i, f() {}
11666
11667          is not valid.  */
11668       function_definition_allowed_p = false;
11669     }
11670
11671   /* Issue an error message if no declarators are present, and the
11672      decl-specifier-seq does not itself declare a class or
11673      enumeration: [dcl.dcl]/3.  */
11674   if (!saw_declarator)
11675     {
11676       if (cp_parser_declares_only_class_p (parser))
11677         {
11678           if (!declares_class_or_enum
11679               && decl_specifiers.type
11680               && OVERLOAD_TYPE_P (decl_specifiers.type))
11681             /* Ensure an error is issued anyway when finish_decltype_type,
11682                called via cp_parser_decl_specifier_seq, returns a class or
11683                an enumeration (c++/51786).  */
11684             decl_specifiers.type = NULL_TREE;
11685           shadow_tag (&decl_specifiers);
11686         }
11687       /* Perform any deferred access checks.  */
11688       perform_deferred_access_checks (tf_warning_or_error);
11689     }
11690
11691   /* Consume the `;'.  */
11692   if (!maybe_range_for_decl)
11693     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11694   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11695     {
11696       if (init_loc != UNKNOWN_LOCATION)
11697         error_at (init_loc, "initializer in range-based %<for%> loop");
11698       if (comma_loc != UNKNOWN_LOCATION)
11699         error_at (comma_loc,
11700                   "multiple declarations in range-based %<for%> loop");
11701     }
11702
11703  done:
11704   pop_deferring_access_checks ();
11705 }
11706
11707 /* Parse a decl-specifier-seq.
11708
11709    decl-specifier-seq:
11710      decl-specifier-seq [opt] decl-specifier
11711      decl-specifier attribute-specifier-seq [opt] (C++11)
11712
11713    decl-specifier:
11714      storage-class-specifier
11715      type-specifier
11716      function-specifier
11717      friend
11718      typedef
11719
11720    GNU Extension:
11721
11722    decl-specifier:
11723      attributes
11724
11725    Set *DECL_SPECS to a representation of the decl-specifier-seq.
11726
11727    The parser flags FLAGS is used to control type-specifier parsing.
11728
11729    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11730    flags:
11731
11732      1: one of the decl-specifiers is an elaborated-type-specifier
11733         (i.e., a type declaration)
11734      2: one of the decl-specifiers is an enum-specifier or a
11735         class-specifier (i.e., a type definition)
11736
11737    */
11738
11739 static void
11740 cp_parser_decl_specifier_seq (cp_parser* parser,
11741                               cp_parser_flags flags,
11742                               cp_decl_specifier_seq *decl_specs,
11743                               int* declares_class_or_enum)
11744 {
11745   bool constructor_possible_p = !parser->in_declarator_p;
11746   bool found_decl_spec = false;
11747   cp_token *start_token = NULL;
11748   cp_decl_spec ds;
11749
11750   /* Clear DECL_SPECS.  */
11751   clear_decl_specs (decl_specs);
11752
11753   /* Assume no class or enumeration type is declared.  */
11754   *declares_class_or_enum = 0;
11755
11756   /* Keep reading specifiers until there are no more to read.  */
11757   while (true)
11758     {
11759       bool constructor_p;
11760       cp_token *token;
11761       ds = ds_last;
11762
11763       /* Peek at the next token.  */
11764       token = cp_lexer_peek_token (parser->lexer);
11765
11766       /* Save the first token of the decl spec list for error
11767          reporting.  */
11768       if (!start_token)
11769         start_token = token;
11770       /* Handle attributes.  */
11771       if (cp_next_tokens_can_be_attribute_p (parser))
11772         {
11773           /* Parse the attributes.  */
11774           tree attrs = cp_parser_attributes_opt (parser);
11775
11776           /* In a sequence of declaration specifiers, c++11 attributes
11777              appertain to the type that precede them. In that case
11778              [dcl.spec]/1 says:
11779
11780                  The attribute-specifier-seq affects the type only for
11781                  the declaration it appears in, not other declarations
11782                  involving the same type.
11783
11784              But for now let's force the user to position the
11785              attribute either at the beginning of the declaration or
11786              after the declarator-id, which would clearly mean that it
11787              applies to the declarator.  */
11788           if (cxx11_attribute_p (attrs))
11789             {
11790               if (!found_decl_spec)
11791                 /* The c++11 attribute is at the beginning of the
11792                    declaration.  It appertains to the entity being
11793                    declared.  */;
11794               else
11795                 {
11796                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11797                     {
11798                       /*  This is an attribute following a
11799                           class-specifier.  */
11800                       if (decl_specs->type_definition_p)
11801                         warn_misplaced_attr_for_class_type (token->location,
11802                                                             decl_specs->type);
11803                       attrs = NULL_TREE;
11804                     }
11805                   else
11806                     {
11807                       decl_specs->std_attributes
11808                         = chainon (decl_specs->std_attributes,
11809                                    attrs);
11810                       if (decl_specs->locations[ds_std_attribute] == 0)
11811                         decl_specs->locations[ds_std_attribute] = token->location;
11812                     }
11813                   continue;
11814                 }
11815             }
11816
11817             decl_specs->attributes
11818               = chainon (decl_specs->attributes,
11819                          attrs);
11820           if (decl_specs->locations[ds_attribute] == 0)
11821             decl_specs->locations[ds_attribute] = token->location;
11822           continue;
11823         }
11824       /* Assume we will find a decl-specifier keyword.  */
11825       found_decl_spec = true;
11826       /* If the next token is an appropriate keyword, we can simply
11827          add it to the list.  */
11828       switch (token->keyword)
11829         {
11830           /* decl-specifier:
11831                friend
11832                constexpr */
11833         case RID_FRIEND:
11834           if (!at_class_scope_p ())
11835             {
11836               error_at (token->location, "%<friend%> used outside of class");
11837               cp_lexer_purge_token (parser->lexer);
11838             }
11839           else
11840             {
11841               ds = ds_friend;
11842               /* Consume the token.  */
11843               cp_lexer_consume_token (parser->lexer);
11844             }
11845           break;
11846
11847         case RID_CONSTEXPR:
11848           ds = ds_constexpr;
11849           cp_lexer_consume_token (parser->lexer);
11850           break;
11851
11852           /* function-specifier:
11853                inline
11854                virtual
11855                explicit  */
11856         case RID_INLINE:
11857         case RID_VIRTUAL:
11858         case RID_EXPLICIT:
11859           cp_parser_function_specifier_opt (parser, decl_specs);
11860           break;
11861
11862           /* decl-specifier:
11863                typedef  */
11864         case RID_TYPEDEF:
11865           ds = ds_typedef;
11866           /* Consume the token.  */
11867           cp_lexer_consume_token (parser->lexer);
11868           /* A constructor declarator cannot appear in a typedef.  */
11869           constructor_possible_p = false;
11870           /* The "typedef" keyword can only occur in a declaration; we
11871              may as well commit at this point.  */
11872           cp_parser_commit_to_tentative_parse (parser);
11873
11874           if (decl_specs->storage_class != sc_none)
11875             decl_specs->conflicting_specifiers_p = true;
11876           break;
11877
11878           /* storage-class-specifier:
11879                auto
11880                register
11881                static
11882                extern
11883                mutable
11884
11885              GNU Extension:
11886                thread  */
11887         case RID_AUTO:
11888           if (cxx_dialect == cxx98) 
11889             {
11890               /* Consume the token.  */
11891               cp_lexer_consume_token (parser->lexer);
11892
11893               /* Complain about `auto' as a storage specifier, if
11894                  we're complaining about C++0x compatibility.  */
11895               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11896                           " changes meaning in C++11; please remove it");
11897
11898               /* Set the storage class anyway.  */
11899               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11900                                            token);
11901             }
11902           else
11903             /* C++0x auto type-specifier.  */
11904             found_decl_spec = false;
11905           break;
11906
11907         case RID_REGISTER:
11908         case RID_STATIC:
11909         case RID_EXTERN:
11910         case RID_MUTABLE:
11911           /* Consume the token.  */
11912           cp_lexer_consume_token (parser->lexer);
11913           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11914                                        token);
11915           break;
11916         case RID_THREAD:
11917           /* Consume the token.  */
11918           ds = ds_thread;
11919           cp_lexer_consume_token (parser->lexer);
11920           break;
11921
11922         default:
11923           /* We did not yet find a decl-specifier yet.  */
11924           found_decl_spec = false;
11925           break;
11926         }
11927
11928       if (found_decl_spec
11929           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11930           && token->keyword != RID_CONSTEXPR)
11931         error ("decl-specifier invalid in condition");
11932
11933       if (ds != ds_last)
11934         set_and_check_decl_spec_loc (decl_specs, ds, token);
11935
11936       /* Constructors are a special case.  The `S' in `S()' is not a
11937          decl-specifier; it is the beginning of the declarator.  */
11938       constructor_p
11939         = (!found_decl_spec
11940            && constructor_possible_p
11941            && (cp_parser_constructor_declarator_p
11942                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11943
11944       /* If we don't have a DECL_SPEC yet, then we must be looking at
11945          a type-specifier.  */
11946       if (!found_decl_spec && !constructor_p)
11947         {
11948           int decl_spec_declares_class_or_enum;
11949           bool is_cv_qualifier;
11950           tree type_spec;
11951
11952           type_spec
11953             = cp_parser_type_specifier (parser, flags,
11954                                         decl_specs,
11955                                         /*is_declaration=*/true,
11956                                         &decl_spec_declares_class_or_enum,
11957                                         &is_cv_qualifier);
11958           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11959
11960           /* If this type-specifier referenced a user-defined type
11961              (a typedef, class-name, etc.), then we can't allow any
11962              more such type-specifiers henceforth.
11963
11964              [dcl.spec]
11965
11966              The longest sequence of decl-specifiers that could
11967              possibly be a type name is taken as the
11968              decl-specifier-seq of a declaration.  The sequence shall
11969              be self-consistent as described below.
11970
11971              [dcl.type]
11972
11973              As a general rule, at most one type-specifier is allowed
11974              in the complete decl-specifier-seq of a declaration.  The
11975              only exceptions are the following:
11976
11977              -- const or volatile can be combined with any other
11978                 type-specifier.
11979
11980              -- signed or unsigned can be combined with char, long,
11981                 short, or int.
11982
11983              -- ..
11984
11985              Example:
11986
11987                typedef char* Pc;
11988                void g (const int Pc);
11989
11990              Here, Pc is *not* part of the decl-specifier seq; it's
11991              the declarator.  Therefore, once we see a type-specifier
11992              (other than a cv-qualifier), we forbid any additional
11993              user-defined types.  We *do* still allow things like `int
11994              int' to be considered a decl-specifier-seq, and issue the
11995              error message later.  */
11996           if (type_spec && !is_cv_qualifier)
11997             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11998           /* A constructor declarator cannot follow a type-specifier.  */
11999           if (type_spec)
12000             {
12001               constructor_possible_p = false;
12002               found_decl_spec = true;
12003               if (!is_cv_qualifier)
12004                 decl_specs->any_type_specifiers_p = true;
12005             }
12006         }
12007
12008       /* If we still do not have a DECL_SPEC, then there are no more
12009          decl-specifiers.  */
12010       if (!found_decl_spec)
12011         break;
12012
12013       decl_specs->any_specifiers_p = true;
12014       /* After we see one decl-specifier, further decl-specifiers are
12015          always optional.  */
12016       flags |= CP_PARSER_FLAGS_OPTIONAL;
12017     }
12018
12019   /* Don't allow a friend specifier with a class definition.  */
12020   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12021       && (*declares_class_or_enum & 2))
12022     error_at (decl_specs->locations[ds_friend],
12023               "class definition may not be declared a friend");
12024 }
12025
12026 /* Parse an (optional) storage-class-specifier.
12027
12028    storage-class-specifier:
12029      auto
12030      register
12031      static
12032      extern
12033      mutable
12034
12035    GNU Extension:
12036
12037    storage-class-specifier:
12038      thread
12039
12040    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
12041
12042 static tree
12043 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12044 {
12045   switch (cp_lexer_peek_token (parser->lexer)->keyword)
12046     {
12047     case RID_AUTO:
12048       if (cxx_dialect != cxx98)
12049         return NULL_TREE;
12050       /* Fall through for C++98.  */
12051
12052     case RID_REGISTER:
12053     case RID_STATIC:
12054     case RID_EXTERN:
12055     case RID_MUTABLE:
12056     case RID_THREAD:
12057       /* Consume the token.  */
12058       return cp_lexer_consume_token (parser->lexer)->u.value;
12059
12060     default:
12061       return NULL_TREE;
12062     }
12063 }
12064
12065 /* Parse an (optional) function-specifier.
12066
12067    function-specifier:
12068      inline
12069      virtual
12070      explicit
12071
12072    Returns an IDENTIFIER_NODE corresponding to the keyword used.
12073    Updates DECL_SPECS, if it is non-NULL.  */
12074
12075 static tree
12076 cp_parser_function_specifier_opt (cp_parser* parser,
12077                                   cp_decl_specifier_seq *decl_specs)
12078 {
12079   cp_token *token = cp_lexer_peek_token (parser->lexer);
12080   switch (token->keyword)
12081     {
12082     case RID_INLINE:
12083       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12084       break;
12085
12086     case RID_VIRTUAL:
12087       /* 14.5.2.3 [temp.mem]
12088
12089          A member function template shall not be virtual.  */
12090       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12091         error_at (token->location, "templates may not be %<virtual%>");
12092       else
12093         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12094       break;
12095
12096     case RID_EXPLICIT:
12097       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12098       break;
12099
12100     default:
12101       return NULL_TREE;
12102     }
12103
12104   /* Consume the token.  */
12105   return cp_lexer_consume_token (parser->lexer)->u.value;
12106 }
12107
12108 /* Parse a linkage-specification.
12109
12110    linkage-specification:
12111      extern string-literal { declaration-seq [opt] }
12112      extern string-literal declaration  */
12113
12114 static void
12115 cp_parser_linkage_specification (cp_parser* parser)
12116 {
12117   tree linkage;
12118
12119   /* Look for the `extern' keyword.  */
12120   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12121
12122   /* Look for the string-literal.  */
12123   linkage = cp_parser_string_literal (parser, false, false);
12124
12125   /* Transform the literal into an identifier.  If the literal is a
12126      wide-character string, or contains embedded NULs, then we can't
12127      handle it as the user wants.  */
12128   if (strlen (TREE_STRING_POINTER (linkage))
12129       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12130     {
12131       cp_parser_error (parser, "invalid linkage-specification");
12132       /* Assume C++ linkage.  */
12133       linkage = lang_name_cplusplus;
12134     }
12135   else
12136     linkage = get_identifier (TREE_STRING_POINTER (linkage));
12137
12138   /* We're now using the new linkage.  */
12139   push_lang_context (linkage);
12140
12141   /* If the next token is a `{', then we're using the first
12142      production.  */
12143   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12144     {
12145       cp_ensure_no_omp_declare_simd (parser);
12146
12147       /* Consume the `{' token.  */
12148       cp_lexer_consume_token (parser->lexer);
12149       /* Parse the declarations.  */
12150       cp_parser_declaration_seq_opt (parser);
12151       /* Look for the closing `}'.  */
12152       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12153     }
12154   /* Otherwise, there's just one declaration.  */
12155   else
12156     {
12157       bool saved_in_unbraced_linkage_specification_p;
12158
12159       saved_in_unbraced_linkage_specification_p
12160         = parser->in_unbraced_linkage_specification_p;
12161       parser->in_unbraced_linkage_specification_p = true;
12162       cp_parser_declaration (parser);
12163       parser->in_unbraced_linkage_specification_p
12164         = saved_in_unbraced_linkage_specification_p;
12165     }
12166
12167   /* We're done with the linkage-specification.  */
12168   pop_lang_context ();
12169 }
12170
12171 /* Parse a static_assert-declaration.
12172
12173    static_assert-declaration:
12174      static_assert ( constant-expression , string-literal ) ; 
12175
12176    If MEMBER_P, this static_assert is a class member.  */
12177
12178 static void 
12179 cp_parser_static_assert(cp_parser *parser, bool member_p)
12180 {
12181   tree condition;
12182   tree message;
12183   cp_token *token;
12184   location_t saved_loc;
12185   bool dummy;
12186
12187   /* Peek at the `static_assert' token so we can keep track of exactly
12188      where the static assertion started.  */
12189   token = cp_lexer_peek_token (parser->lexer);
12190   saved_loc = token->location;
12191
12192   /* Look for the `static_assert' keyword.  */
12193   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
12194                                   RT_STATIC_ASSERT))
12195     return;
12196
12197   /*  We know we are in a static assertion; commit to any tentative
12198       parse.  */
12199   if (cp_parser_parsing_tentatively (parser))
12200     cp_parser_commit_to_tentative_parse (parser);
12201
12202   /* Parse the `(' starting the static assertion condition.  */
12203   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12204
12205   /* Parse the constant-expression.  Allow a non-constant expression
12206      here in order to give better diagnostics in finish_static_assert.  */
12207   condition = 
12208     cp_parser_constant_expression (parser,
12209                                    /*allow_non_constant_p=*/true,
12210                                    /*non_constant_p=*/&dummy);
12211
12212   /* Parse the separating `,'.  */
12213   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12214
12215   /* Parse the string-literal message.  */
12216   message = cp_parser_string_literal (parser, 
12217                                       /*translate=*/false,
12218                                       /*wide_ok=*/true);
12219
12220   /* A `)' completes the static assertion.  */
12221   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12222     cp_parser_skip_to_closing_parenthesis (parser, 
12223                                            /*recovering=*/true, 
12224                                            /*or_comma=*/false,
12225                                            /*consume_paren=*/true);
12226
12227   /* A semicolon terminates the declaration.  */
12228   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12229
12230   /* Complete the static assertion, which may mean either processing 
12231      the static assert now or saving it for template instantiation.  */
12232   finish_static_assert (condition, message, saved_loc, member_p);
12233 }
12234
12235 /* Parse the expression in decltype ( expression ).  */
12236
12237 static tree
12238 cp_parser_decltype_expr (cp_parser *parser,
12239                          bool &id_expression_or_member_access_p)
12240 {
12241   cp_token *id_expr_start_token;
12242   tree expr;
12243
12244   /* First, try parsing an id-expression.  */
12245   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12246   cp_parser_parse_tentatively (parser);
12247   expr = cp_parser_id_expression (parser,
12248                                   /*template_keyword_p=*/false,
12249                                   /*check_dependency_p=*/true,
12250                                   /*template_p=*/NULL,
12251                                   /*declarator_p=*/false,
12252                                   /*optional_p=*/false);
12253
12254   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12255     {
12256       bool non_integral_constant_expression_p = false;
12257       tree id_expression = expr;
12258       cp_id_kind idk;
12259       const char *error_msg;
12260
12261       if (identifier_p (expr))
12262         /* Lookup the name we got back from the id-expression.  */
12263         expr = cp_parser_lookup_name_simple (parser, expr,
12264                                              id_expr_start_token->location);
12265
12266       if (expr
12267           && expr != error_mark_node
12268           && TREE_CODE (expr) != TYPE_DECL
12269           && (TREE_CODE (expr) != BIT_NOT_EXPR
12270               || !TYPE_P (TREE_OPERAND (expr, 0)))
12271           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12272         {
12273           /* Complete lookup of the id-expression.  */
12274           expr = (finish_id_expression
12275                   (id_expression, expr, parser->scope, &idk,
12276                    /*integral_constant_expression_p=*/false,
12277                    /*allow_non_integral_constant_expression_p=*/true,
12278                    &non_integral_constant_expression_p,
12279                    /*template_p=*/false,
12280                    /*done=*/true,
12281                    /*address_p=*/false,
12282                    /*template_arg_p=*/false,
12283                    &error_msg,
12284                    id_expr_start_token->location));
12285
12286           if (expr == error_mark_node)
12287             /* We found an id-expression, but it was something that we
12288                should not have found. This is an error, not something
12289                we can recover from, so note that we found an
12290                id-expression and we'll recover as gracefully as
12291                possible.  */
12292             id_expression_or_member_access_p = true;
12293         }
12294
12295       if (expr 
12296           && expr != error_mark_node
12297           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12298         /* We have an id-expression.  */
12299         id_expression_or_member_access_p = true;
12300     }
12301
12302   if (!id_expression_or_member_access_p)
12303     {
12304       /* Abort the id-expression parse.  */
12305       cp_parser_abort_tentative_parse (parser);
12306
12307       /* Parsing tentatively, again.  */
12308       cp_parser_parse_tentatively (parser);
12309
12310       /* Parse a class member access.  */
12311       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12312                                            /*cast_p=*/false, /*decltype*/true,
12313                                            /*member_access_only_p=*/true, NULL);
12314
12315       if (expr 
12316           && expr != error_mark_node
12317           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12318         /* We have an id-expression.  */
12319         id_expression_or_member_access_p = true;
12320     }
12321
12322   if (id_expression_or_member_access_p)
12323     /* We have parsed the complete id-expression or member access.  */
12324     cp_parser_parse_definitely (parser);
12325   else
12326     {
12327       /* Abort our attempt to parse an id-expression or member access
12328          expression.  */
12329       cp_parser_abort_tentative_parse (parser);
12330
12331       /* Parse a full expression.  */
12332       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12333                                    /*decltype_p=*/true);
12334     }
12335
12336   return expr;
12337 }
12338
12339 /* Parse a `decltype' type. Returns the type.
12340
12341    simple-type-specifier:
12342      decltype ( expression )
12343    C++14 proposal:
12344      decltype ( auto )  */
12345
12346 static tree
12347 cp_parser_decltype (cp_parser *parser)
12348 {
12349   tree expr;
12350   bool id_expression_or_member_access_p = false;
12351   const char *saved_message;
12352   bool saved_integral_constant_expression_p;
12353   bool saved_non_integral_constant_expression_p;
12354   bool saved_greater_than_is_operator_p;
12355   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12356
12357   if (start_token->type == CPP_DECLTYPE)
12358     {
12359       /* Already parsed.  */
12360       cp_lexer_consume_token (parser->lexer);
12361       return start_token->u.value;
12362     }
12363
12364   /* Look for the `decltype' token.  */
12365   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12366     return error_mark_node;
12367
12368   /* Parse the opening `('.  */
12369   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12370     return error_mark_node;
12371
12372   /* decltype (auto) */
12373   if (cxx_dialect >= cxx14
12374       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12375     {
12376       cp_lexer_consume_token (parser->lexer);
12377       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12378         return error_mark_node;
12379       expr = make_decltype_auto ();
12380       AUTO_IS_DECLTYPE (expr) = true;
12381       goto rewrite;
12382     }
12383
12384   /* Types cannot be defined in a `decltype' expression.  Save away the
12385      old message.  */
12386   saved_message = parser->type_definition_forbidden_message;
12387
12388   /* And create the new one.  */
12389   parser->type_definition_forbidden_message
12390     = G_("types may not be defined in %<decltype%> expressions");
12391
12392   /* The restrictions on constant-expressions do not apply inside
12393      decltype expressions.  */
12394   saved_integral_constant_expression_p
12395     = parser->integral_constant_expression_p;
12396   saved_non_integral_constant_expression_p
12397     = parser->non_integral_constant_expression_p;
12398   parser->integral_constant_expression_p = false;
12399
12400   /* Within a parenthesized expression, a `>' token is always
12401      the greater-than operator.  */
12402   saved_greater_than_is_operator_p
12403     = parser->greater_than_is_operator_p;
12404   parser->greater_than_is_operator_p = true;
12405
12406   /* Do not actually evaluate the expression.  */
12407   ++cp_unevaluated_operand;
12408
12409   /* Do not warn about problems with the expression.  */
12410   ++c_inhibit_evaluation_warnings;
12411
12412   expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12413
12414   /* Go back to evaluating expressions.  */
12415   --cp_unevaluated_operand;
12416   --c_inhibit_evaluation_warnings;
12417
12418   /* The `>' token might be the end of a template-id or
12419      template-parameter-list now.  */
12420   parser->greater_than_is_operator_p
12421     = saved_greater_than_is_operator_p;
12422
12423   /* Restore the old message and the integral constant expression
12424      flags.  */
12425   parser->type_definition_forbidden_message = saved_message;
12426   parser->integral_constant_expression_p
12427     = saved_integral_constant_expression_p;
12428   parser->non_integral_constant_expression_p
12429     = saved_non_integral_constant_expression_p;
12430
12431   /* Parse to the closing `)'.  */
12432   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12433     {
12434       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12435                                              /*consume_paren=*/true);
12436       return error_mark_node;
12437     }
12438
12439   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12440                                tf_warning_or_error);
12441
12442  rewrite:
12443   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12444      it again.  */
12445   start_token->type = CPP_DECLTYPE;
12446   start_token->u.value = expr;
12447   start_token->keyword = RID_MAX;
12448   cp_lexer_purge_tokens_after (parser->lexer, start_token);
12449
12450   return expr;
12451 }
12452
12453 /* Special member functions [gram.special] */
12454
12455 /* Parse a conversion-function-id.
12456
12457    conversion-function-id:
12458      operator conversion-type-id
12459
12460    Returns an IDENTIFIER_NODE representing the operator.  */
12461
12462 static tree
12463 cp_parser_conversion_function_id (cp_parser* parser)
12464 {
12465   tree type;
12466   tree saved_scope;
12467   tree saved_qualifying_scope;
12468   tree saved_object_scope;
12469   tree pushed_scope = NULL_TREE;
12470
12471   /* Look for the `operator' token.  */
12472   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12473     return error_mark_node;
12474   /* When we parse the conversion-type-id, the current scope will be
12475      reset.  However, we need that information in able to look up the
12476      conversion function later, so we save it here.  */
12477   saved_scope = parser->scope;
12478   saved_qualifying_scope = parser->qualifying_scope;
12479   saved_object_scope = parser->object_scope;
12480   /* We must enter the scope of the class so that the names of
12481      entities declared within the class are available in the
12482      conversion-type-id.  For example, consider:
12483
12484        struct S {
12485          typedef int I;
12486          operator I();
12487        };
12488
12489        S::operator I() { ... }
12490
12491      In order to see that `I' is a type-name in the definition, we
12492      must be in the scope of `S'.  */
12493   if (saved_scope)
12494     pushed_scope = push_scope (saved_scope);
12495   /* Parse the conversion-type-id.  */
12496   type = cp_parser_conversion_type_id (parser);
12497   /* Leave the scope of the class, if any.  */
12498   if (pushed_scope)
12499     pop_scope (pushed_scope);
12500   /* Restore the saved scope.  */
12501   parser->scope = saved_scope;
12502   parser->qualifying_scope = saved_qualifying_scope;
12503   parser->object_scope = saved_object_scope;
12504   /* If the TYPE is invalid, indicate failure.  */
12505   if (type == error_mark_node)
12506     return error_mark_node;
12507   return mangle_conv_op_name_for_type (type);
12508 }
12509
12510 /* Parse a conversion-type-id:
12511
12512    conversion-type-id:
12513      type-specifier-seq conversion-declarator [opt]
12514
12515    Returns the TYPE specified.  */
12516
12517 static tree
12518 cp_parser_conversion_type_id (cp_parser* parser)
12519 {
12520   tree attributes;
12521   cp_decl_specifier_seq type_specifiers;
12522   cp_declarator *declarator;
12523   tree type_specified;
12524   const char *saved_message;
12525
12526   /* Parse the attributes.  */
12527   attributes = cp_parser_attributes_opt (parser);
12528
12529   saved_message = parser->type_definition_forbidden_message;
12530   parser->type_definition_forbidden_message
12531     = G_("types may not be defined in a conversion-type-id");
12532
12533   /* Parse the type-specifiers.  */
12534   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12535                                 /*is_trailing_return=*/false,
12536                                 &type_specifiers);
12537
12538   parser->type_definition_forbidden_message = saved_message;
12539
12540   /* If that didn't work, stop.  */
12541   if (type_specifiers.type == error_mark_node)
12542     return error_mark_node;
12543   /* Parse the conversion-declarator.  */
12544   declarator = cp_parser_conversion_declarator_opt (parser);
12545
12546   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
12547                                     /*initialized=*/0, &attributes);
12548   if (attributes)
12549     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12550
12551   /* Don't give this error when parsing tentatively.  This happens to
12552      work because we always parse this definitively once.  */
12553   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12554       && type_uses_auto (type_specified))
12555     {
12556       if (cxx_dialect < cxx14)
12557         {
12558           error ("invalid use of %<auto%> in conversion operator");
12559           return error_mark_node;
12560         }
12561       else if (template_parm_scope_p ())
12562         warning (0, "use of %<auto%> in member template "
12563                  "conversion operator can never be deduced");
12564     }
12565
12566   return type_specified;
12567 }
12568
12569 /* Parse an (optional) conversion-declarator.
12570
12571    conversion-declarator:
12572      ptr-operator conversion-declarator [opt]
12573
12574    */
12575
12576 static cp_declarator *
12577 cp_parser_conversion_declarator_opt (cp_parser* parser)
12578 {
12579   enum tree_code code;
12580   tree class_type, std_attributes = NULL_TREE;
12581   cp_cv_quals cv_quals;
12582
12583   /* We don't know if there's a ptr-operator next, or not.  */
12584   cp_parser_parse_tentatively (parser);
12585   /* Try the ptr-operator.  */
12586   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12587                                  &std_attributes);
12588   /* If it worked, look for more conversion-declarators.  */
12589   if (cp_parser_parse_definitely (parser))
12590     {
12591       cp_declarator *declarator;
12592
12593       /* Parse another optional declarator.  */
12594       declarator = cp_parser_conversion_declarator_opt (parser);
12595
12596       declarator = cp_parser_make_indirect_declarator
12597         (code, class_type, cv_quals, declarator, std_attributes);
12598
12599       return declarator;
12600    }
12601
12602   return NULL;
12603 }
12604
12605 /* Parse an (optional) ctor-initializer.
12606
12607    ctor-initializer:
12608      : mem-initializer-list
12609
12610    Returns TRUE iff the ctor-initializer was actually present.  */
12611
12612 static bool
12613 cp_parser_ctor_initializer_opt (cp_parser* parser)
12614 {
12615   /* If the next token is not a `:', then there is no
12616      ctor-initializer.  */
12617   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12618     {
12619       /* Do default initialization of any bases and members.  */
12620       if (DECL_CONSTRUCTOR_P (current_function_decl))
12621         finish_mem_initializers (NULL_TREE);
12622
12623       return false;
12624     }
12625
12626   /* Consume the `:' token.  */
12627   cp_lexer_consume_token (parser->lexer);
12628   /* And the mem-initializer-list.  */
12629   cp_parser_mem_initializer_list (parser);
12630
12631   return true;
12632 }
12633
12634 /* Parse a mem-initializer-list.
12635
12636    mem-initializer-list:
12637      mem-initializer ... [opt]
12638      mem-initializer ... [opt] , mem-initializer-list  */
12639
12640 static void
12641 cp_parser_mem_initializer_list (cp_parser* parser)
12642 {
12643   tree mem_initializer_list = NULL_TREE;
12644   tree target_ctor = error_mark_node;
12645   cp_token *token = cp_lexer_peek_token (parser->lexer);
12646
12647   /* Let the semantic analysis code know that we are starting the
12648      mem-initializer-list.  */
12649   if (!DECL_CONSTRUCTOR_P (current_function_decl))
12650     error_at (token->location,
12651               "only constructors take member initializers");
12652
12653   /* Loop through the list.  */
12654   while (true)
12655     {
12656       tree mem_initializer;
12657
12658       token = cp_lexer_peek_token (parser->lexer);
12659       /* Parse the mem-initializer.  */
12660       mem_initializer = cp_parser_mem_initializer (parser);
12661       /* If the next token is a `...', we're expanding member initializers. */
12662       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12663         {
12664           /* Consume the `...'. */
12665           cp_lexer_consume_token (parser->lexer);
12666
12667           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12668              can be expanded but members cannot. */
12669           if (mem_initializer != error_mark_node
12670               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12671             {
12672               error_at (token->location,
12673                         "cannot expand initializer for member %<%D%>",
12674                         TREE_PURPOSE (mem_initializer));
12675               mem_initializer = error_mark_node;
12676             }
12677
12678           /* Construct the pack expansion type. */
12679           if (mem_initializer != error_mark_node)
12680             mem_initializer = make_pack_expansion (mem_initializer);
12681         }
12682       if (target_ctor != error_mark_node
12683           && mem_initializer != error_mark_node)
12684         {
12685           error ("mem-initializer for %qD follows constructor delegation",
12686                  TREE_PURPOSE (mem_initializer));
12687           mem_initializer = error_mark_node;
12688         }
12689       /* Look for a target constructor. */
12690       if (mem_initializer != error_mark_node
12691           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12692           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12693         {
12694           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12695           if (mem_initializer_list)
12696             {
12697               error ("constructor delegation follows mem-initializer for %qD",
12698                      TREE_PURPOSE (mem_initializer_list));
12699               mem_initializer = error_mark_node;
12700             }
12701           target_ctor = mem_initializer;
12702         }
12703       /* Add it to the list, unless it was erroneous.  */
12704       if (mem_initializer != error_mark_node)
12705         {
12706           TREE_CHAIN (mem_initializer) = mem_initializer_list;
12707           mem_initializer_list = mem_initializer;
12708         }
12709       /* If the next token is not a `,', we're done.  */
12710       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12711         break;
12712       /* Consume the `,' token.  */
12713       cp_lexer_consume_token (parser->lexer);
12714     }
12715
12716   /* Perform semantic analysis.  */
12717   if (DECL_CONSTRUCTOR_P (current_function_decl))
12718     finish_mem_initializers (mem_initializer_list);
12719 }
12720
12721 /* Parse a mem-initializer.
12722
12723    mem-initializer:
12724      mem-initializer-id ( expression-list [opt] )
12725      mem-initializer-id braced-init-list
12726
12727    GNU extension:
12728
12729    mem-initializer:
12730      ( expression-list [opt] )
12731
12732    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
12733    class) or FIELD_DECL (for a non-static data member) to initialize;
12734    the TREE_VALUE is the expression-list.  An empty initialization
12735    list is represented by void_list_node.  */
12736
12737 static tree
12738 cp_parser_mem_initializer (cp_parser* parser)
12739 {
12740   tree mem_initializer_id;
12741   tree expression_list;
12742   tree member;
12743   cp_token *token = cp_lexer_peek_token (parser->lexer);
12744
12745   /* Find out what is being initialized.  */
12746   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12747     {
12748       permerror (token->location,
12749                  "anachronistic old-style base class initializer");
12750       mem_initializer_id = NULL_TREE;
12751     }
12752   else
12753     {
12754       mem_initializer_id = cp_parser_mem_initializer_id (parser);
12755       if (mem_initializer_id == error_mark_node)
12756         return mem_initializer_id;
12757     }
12758   member = expand_member_init (mem_initializer_id);
12759   if (member && !DECL_P (member))
12760     in_base_initializer = 1;
12761
12762   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12763     {
12764       bool expr_non_constant_p;
12765       cp_lexer_set_source_position (parser->lexer);
12766       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12767       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12768       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12769       expression_list = build_tree_list (NULL_TREE, expression_list);
12770     }
12771   else
12772     {
12773       vec<tree, va_gc> *vec;
12774       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12775                                                      /*cast_p=*/false,
12776                                                      /*allow_expansion_p=*/true,
12777                                                      /*non_constant_p=*/NULL);
12778       if (vec == NULL)
12779         return error_mark_node;
12780       expression_list = build_tree_list_vec (vec);
12781       release_tree_vector (vec);
12782     }
12783
12784   if (expression_list == error_mark_node)
12785     return error_mark_node;
12786   if (!expression_list)
12787     expression_list = void_type_node;
12788
12789   in_base_initializer = 0;
12790
12791   return member ? build_tree_list (member, expression_list) : error_mark_node;
12792 }
12793
12794 /* Parse a mem-initializer-id.
12795
12796    mem-initializer-id:
12797      :: [opt] nested-name-specifier [opt] class-name
12798      identifier
12799
12800    Returns a TYPE indicating the class to be initializer for the first
12801    production.  Returns an IDENTIFIER_NODE indicating the data member
12802    to be initialized for the second production.  */
12803
12804 static tree
12805 cp_parser_mem_initializer_id (cp_parser* parser)
12806 {
12807   bool global_scope_p;
12808   bool nested_name_specifier_p;
12809   bool template_p = false;
12810   tree id;
12811
12812   cp_token *token = cp_lexer_peek_token (parser->lexer);
12813
12814   /* `typename' is not allowed in this context ([temp.res]).  */
12815   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12816     {
12817       error_at (token->location, 
12818                 "keyword %<typename%> not allowed in this context (a qualified "
12819                 "member initializer is implicitly a type)");
12820       cp_lexer_consume_token (parser->lexer);
12821     }
12822   /* Look for the optional `::' operator.  */
12823   global_scope_p
12824     = (cp_parser_global_scope_opt (parser,
12825                                    /*current_scope_valid_p=*/false)
12826        != NULL_TREE);
12827   /* Look for the optional nested-name-specifier.  The simplest way to
12828      implement:
12829
12830        [temp.res]
12831
12832        The keyword `typename' is not permitted in a base-specifier or
12833        mem-initializer; in these contexts a qualified name that
12834        depends on a template-parameter is implicitly assumed to be a
12835        type name.
12836
12837      is to assume that we have seen the `typename' keyword at this
12838      point.  */
12839   nested_name_specifier_p
12840     = (cp_parser_nested_name_specifier_opt (parser,
12841                                             /*typename_keyword_p=*/true,
12842                                             /*check_dependency_p=*/true,
12843                                             /*type_p=*/true,
12844                                             /*is_declaration=*/true)
12845        != NULL_TREE);
12846   if (nested_name_specifier_p)
12847     template_p = cp_parser_optional_template_keyword (parser);
12848   /* If there is a `::' operator or a nested-name-specifier, then we
12849      are definitely looking for a class-name.  */
12850   if (global_scope_p || nested_name_specifier_p)
12851     return cp_parser_class_name (parser,
12852                                  /*typename_keyword_p=*/true,
12853                                  /*template_keyword_p=*/template_p,
12854                                  typename_type,
12855                                  /*check_dependency_p=*/true,
12856                                  /*class_head_p=*/false,
12857                                  /*is_declaration=*/true);
12858   /* Otherwise, we could also be looking for an ordinary identifier.  */
12859   cp_parser_parse_tentatively (parser);
12860   /* Try a class-name.  */
12861   id = cp_parser_class_name (parser,
12862                              /*typename_keyword_p=*/true,
12863                              /*template_keyword_p=*/false,
12864                              none_type,
12865                              /*check_dependency_p=*/true,
12866                              /*class_head_p=*/false,
12867                              /*is_declaration=*/true);
12868   /* If we found one, we're done.  */
12869   if (cp_parser_parse_definitely (parser))
12870     return id;
12871   /* Otherwise, look for an ordinary identifier.  */
12872   return cp_parser_identifier (parser);
12873 }
12874
12875 /* Overloading [gram.over] */
12876
12877 /* Parse an operator-function-id.
12878
12879    operator-function-id:
12880      operator operator
12881
12882    Returns an IDENTIFIER_NODE for the operator which is a
12883    human-readable spelling of the identifier, e.g., `operator +'.  */
12884
12885 static tree
12886 cp_parser_operator_function_id (cp_parser* parser)
12887 {
12888   /* Look for the `operator' keyword.  */
12889   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12890     return error_mark_node;
12891   /* And then the name of the operator itself.  */
12892   return cp_parser_operator (parser);
12893 }
12894
12895 /* Return an identifier node for a user-defined literal operator.
12896    The suffix identifier is chained to the operator name identifier.  */
12897
12898 static tree
12899 cp_literal_operator_id (const char* name)
12900 {
12901   tree identifier;
12902   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12903                               + strlen (name) + 10);
12904   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12905   identifier = get_identifier (buffer);
12906
12907   return identifier;
12908 }
12909
12910 /* Parse an operator.
12911
12912    operator:
12913      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12914      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12915      || ++ -- , ->* -> () []
12916
12917    GNU Extensions:
12918
12919    operator:
12920      <? >? <?= >?=
12921
12922    Returns an IDENTIFIER_NODE for the operator which is a
12923    human-readable spelling of the identifier, e.g., `operator +'.  */
12924
12925 static tree
12926 cp_parser_operator (cp_parser* parser)
12927 {
12928   tree id = NULL_TREE;
12929   cp_token *token;
12930   bool utf8 = false;
12931
12932   /* Peek at the next token.  */
12933   token = cp_lexer_peek_token (parser->lexer);
12934   /* Figure out which operator we have.  */
12935   switch (token->type)
12936     {
12937     case CPP_KEYWORD:
12938       {
12939         enum tree_code op;
12940
12941         /* The keyword should be either `new' or `delete'.  */
12942         if (token->keyword == RID_NEW)
12943           op = NEW_EXPR;
12944         else if (token->keyword == RID_DELETE)
12945           op = DELETE_EXPR;
12946         else
12947           break;
12948
12949         /* Consume the `new' or `delete' token.  */
12950         cp_lexer_consume_token (parser->lexer);
12951
12952         /* Peek at the next token.  */
12953         token = cp_lexer_peek_token (parser->lexer);
12954         /* If it's a `[' token then this is the array variant of the
12955            operator.  */
12956         if (token->type == CPP_OPEN_SQUARE)
12957           {
12958             /* Consume the `[' token.  */
12959             cp_lexer_consume_token (parser->lexer);
12960             /* Look for the `]' token.  */
12961             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12962             id = ansi_opname (op == NEW_EXPR
12963                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12964           }
12965         /* Otherwise, we have the non-array variant.  */
12966         else
12967           id = ansi_opname (op);
12968
12969         return id;
12970       }
12971
12972     case CPP_PLUS:
12973       id = ansi_opname (PLUS_EXPR);
12974       break;
12975
12976     case CPP_MINUS:
12977       id = ansi_opname (MINUS_EXPR);
12978       break;
12979
12980     case CPP_MULT:
12981       id = ansi_opname (MULT_EXPR);
12982       break;
12983
12984     case CPP_DIV:
12985       id = ansi_opname (TRUNC_DIV_EXPR);
12986       break;
12987
12988     case CPP_MOD:
12989       id = ansi_opname (TRUNC_MOD_EXPR);
12990       break;
12991
12992     case CPP_XOR:
12993       id = ansi_opname (BIT_XOR_EXPR);
12994       break;
12995
12996     case CPP_AND:
12997       id = ansi_opname (BIT_AND_EXPR);
12998       break;
12999
13000     case CPP_OR:
13001       id = ansi_opname (BIT_IOR_EXPR);
13002       break;
13003
13004     case CPP_COMPL:
13005       id = ansi_opname (BIT_NOT_EXPR);
13006       break;
13007
13008     case CPP_NOT:
13009       id = ansi_opname (TRUTH_NOT_EXPR);
13010       break;
13011
13012     case CPP_EQ:
13013       id = ansi_assopname (NOP_EXPR);
13014       break;
13015
13016     case CPP_LESS:
13017       id = ansi_opname (LT_EXPR);
13018       break;
13019
13020     case CPP_GREATER:
13021       id = ansi_opname (GT_EXPR);
13022       break;
13023
13024     case CPP_PLUS_EQ:
13025       id = ansi_assopname (PLUS_EXPR);
13026       break;
13027
13028     case CPP_MINUS_EQ:
13029       id = ansi_assopname (MINUS_EXPR);
13030       break;
13031
13032     case CPP_MULT_EQ:
13033       id = ansi_assopname (MULT_EXPR);
13034       break;
13035
13036     case CPP_DIV_EQ:
13037       id = ansi_assopname (TRUNC_DIV_EXPR);
13038       break;
13039
13040     case CPP_MOD_EQ:
13041       id = ansi_assopname (TRUNC_MOD_EXPR);
13042       break;
13043
13044     case CPP_XOR_EQ:
13045       id = ansi_assopname (BIT_XOR_EXPR);
13046       break;
13047
13048     case CPP_AND_EQ:
13049       id = ansi_assopname (BIT_AND_EXPR);
13050       break;
13051
13052     case CPP_OR_EQ:
13053       id = ansi_assopname (BIT_IOR_EXPR);
13054       break;
13055
13056     case CPP_LSHIFT:
13057       id = ansi_opname (LSHIFT_EXPR);
13058       break;
13059
13060     case CPP_RSHIFT:
13061       id = ansi_opname (RSHIFT_EXPR);
13062       break;
13063
13064     case CPP_LSHIFT_EQ:
13065       id = ansi_assopname (LSHIFT_EXPR);
13066       break;
13067
13068     case CPP_RSHIFT_EQ:
13069       id = ansi_assopname (RSHIFT_EXPR);
13070       break;
13071
13072     case CPP_EQ_EQ:
13073       id = ansi_opname (EQ_EXPR);
13074       break;
13075
13076     case CPP_NOT_EQ:
13077       id = ansi_opname (NE_EXPR);
13078       break;
13079
13080     case CPP_LESS_EQ:
13081       id = ansi_opname (LE_EXPR);
13082       break;
13083
13084     case CPP_GREATER_EQ:
13085       id = ansi_opname (GE_EXPR);
13086       break;
13087
13088     case CPP_AND_AND:
13089       id = ansi_opname (TRUTH_ANDIF_EXPR);
13090       break;
13091
13092     case CPP_OR_OR:
13093       id = ansi_opname (TRUTH_ORIF_EXPR);
13094       break;
13095
13096     case CPP_PLUS_PLUS:
13097       id = ansi_opname (POSTINCREMENT_EXPR);
13098       break;
13099
13100     case CPP_MINUS_MINUS:
13101       id = ansi_opname (PREDECREMENT_EXPR);
13102       break;
13103
13104     case CPP_COMMA:
13105       id = ansi_opname (COMPOUND_EXPR);
13106       break;
13107
13108     case CPP_DEREF_STAR:
13109       id = ansi_opname (MEMBER_REF);
13110       break;
13111
13112     case CPP_DEREF:
13113       id = ansi_opname (COMPONENT_REF);
13114       break;
13115
13116     case CPP_OPEN_PAREN:
13117       /* Consume the `('.  */
13118       cp_lexer_consume_token (parser->lexer);
13119       /* Look for the matching `)'.  */
13120       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13121       return ansi_opname (CALL_EXPR);
13122
13123     case CPP_OPEN_SQUARE:
13124       /* Consume the `['.  */
13125       cp_lexer_consume_token (parser->lexer);
13126       /* Look for the matching `]'.  */
13127       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13128       return ansi_opname (ARRAY_REF);
13129
13130     case CPP_UTF8STRING:
13131     case CPP_UTF8STRING_USERDEF:
13132       utf8 = true;
13133     case CPP_STRING:
13134     case CPP_WSTRING:
13135     case CPP_STRING16:
13136     case CPP_STRING32:
13137     case CPP_STRING_USERDEF:
13138     case CPP_WSTRING_USERDEF:
13139     case CPP_STRING16_USERDEF:
13140     case CPP_STRING32_USERDEF:
13141       {
13142         tree str, string_tree;
13143         int sz, len;
13144
13145         if (cxx_dialect == cxx98)
13146           maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13147
13148         /* Consume the string.  */
13149         str = cp_parser_string_literal (parser, /*translate=*/true,
13150                                       /*wide_ok=*/true, /*lookup_udlit=*/false);
13151         if (str == error_mark_node)
13152           return error_mark_node;
13153         else if (TREE_CODE (str) == USERDEF_LITERAL)
13154           {
13155             string_tree = USERDEF_LITERAL_VALUE (str);
13156             id = USERDEF_LITERAL_SUFFIX_ID (str);
13157           }
13158         else
13159           {
13160             string_tree = str;
13161             /* Look for the suffix identifier.  */
13162             token = cp_lexer_peek_token (parser->lexer);
13163             if (token->type == CPP_NAME)
13164               id = cp_parser_identifier (parser);
13165             else if (token->type == CPP_KEYWORD)
13166               {
13167                 error ("unexpected keyword;"
13168                        " remove space between quotes and suffix identifier");
13169                 return error_mark_node;
13170               }
13171             else
13172               {
13173                 error ("expected suffix identifier");
13174                 return error_mark_node;
13175               }
13176           }
13177         sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13178                                (TREE_TYPE (TREE_TYPE (string_tree))));
13179         len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13180         if (len != 0)
13181           {
13182             error ("expected empty string after %<operator%> keyword");
13183             return error_mark_node;
13184           }
13185         if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13186             != char_type_node)
13187           {
13188             error ("invalid encoding prefix in literal operator");
13189             return error_mark_node;
13190           }
13191         if (id != error_mark_node)
13192           {
13193             const char *name = IDENTIFIER_POINTER (id);
13194             id = cp_literal_operator_id (name);
13195           }
13196         return id;
13197       }
13198
13199     default:
13200       /* Anything else is an error.  */
13201       break;
13202     }
13203
13204   /* If we have selected an identifier, we need to consume the
13205      operator token.  */
13206   if (id)
13207     cp_lexer_consume_token (parser->lexer);
13208   /* Otherwise, no valid operator name was present.  */
13209   else
13210     {
13211       cp_parser_error (parser, "expected operator");
13212       id = error_mark_node;
13213     }
13214
13215   return id;
13216 }
13217
13218 /* Parse a template-declaration.
13219
13220    template-declaration:
13221      export [opt] template < template-parameter-list > declaration
13222
13223    If MEMBER_P is TRUE, this template-declaration occurs within a
13224    class-specifier.
13225
13226    The grammar rule given by the standard isn't correct.  What
13227    is really meant is:
13228
13229    template-declaration:
13230      export [opt] template-parameter-list-seq
13231        decl-specifier-seq [opt] init-declarator [opt] ;
13232      export [opt] template-parameter-list-seq
13233        function-definition
13234
13235    template-parameter-list-seq:
13236      template-parameter-list-seq [opt]
13237      template < template-parameter-list >  */
13238
13239 static void
13240 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13241 {
13242   /* Check for `export'.  */
13243   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13244     {
13245       /* Consume the `export' token.  */
13246       cp_lexer_consume_token (parser->lexer);
13247       /* Warn that we do not support `export'.  */
13248       warning (0, "keyword %<export%> not implemented, and will be ignored");
13249     }
13250
13251   cp_parser_template_declaration_after_export (parser, member_p);
13252 }
13253
13254 /* Parse a template-parameter-list.
13255
13256    template-parameter-list:
13257      template-parameter
13258      template-parameter-list , template-parameter
13259
13260    Returns a TREE_LIST.  Each node represents a template parameter.
13261    The nodes are connected via their TREE_CHAINs.  */
13262
13263 static tree
13264 cp_parser_template_parameter_list (cp_parser* parser)
13265 {
13266   tree parameter_list = NULL_TREE;
13267
13268   begin_template_parm_list ();
13269
13270   /* The loop below parses the template parms.  We first need to know
13271      the total number of template parms to be able to compute proper
13272      canonical types of each dependent type. So after the loop, when
13273      we know the total number of template parms,
13274      end_template_parm_list computes the proper canonical types and
13275      fixes up the dependent types accordingly.  */
13276   while (true)
13277     {
13278       tree parameter;
13279       bool is_non_type;
13280       bool is_parameter_pack;
13281       location_t parm_loc;
13282
13283       /* Parse the template-parameter.  */
13284       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13285       parameter = cp_parser_template_parameter (parser, 
13286                                                 &is_non_type,
13287                                                 &is_parameter_pack);
13288       /* Add it to the list.  */
13289       if (parameter != error_mark_node)
13290         parameter_list = process_template_parm (parameter_list,
13291                                                 parm_loc,
13292                                                 parameter,
13293                                                 is_non_type,
13294                                                 is_parameter_pack);
13295       else
13296        {
13297          tree err_parm = build_tree_list (parameter, parameter);
13298          parameter_list = chainon (parameter_list, err_parm);
13299        }
13300
13301       /* If the next token is not a `,', we're done.  */
13302       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13303         break;
13304       /* Otherwise, consume the `,' token.  */
13305       cp_lexer_consume_token (parser->lexer);
13306     }
13307
13308   return end_template_parm_list (parameter_list);
13309 }
13310
13311 /* Parse a template-parameter.
13312
13313    template-parameter:
13314      type-parameter
13315      parameter-declaration
13316
13317    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
13318    the parameter.  The TREE_PURPOSE is the default value, if any.
13319    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
13320    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
13321    set to true iff this parameter is a parameter pack. */
13322
13323 static tree
13324 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13325                               bool *is_parameter_pack)
13326 {
13327   cp_token *token;
13328   cp_parameter_declarator *parameter_declarator;
13329   cp_declarator *id_declarator;
13330   tree parm;
13331
13332   /* Assume it is a type parameter or a template parameter.  */
13333   *is_non_type = false;
13334   /* Assume it not a parameter pack. */
13335   *is_parameter_pack = false;
13336   /* Peek at the next token.  */
13337   token = cp_lexer_peek_token (parser->lexer);
13338   /* If it is `class' or `template', we have a type-parameter.  */
13339   if (token->keyword == RID_TEMPLATE)
13340     return cp_parser_type_parameter (parser, is_parameter_pack);
13341   /* If it is `class' or `typename' we do not know yet whether it is a
13342      type parameter or a non-type parameter.  Consider:
13343
13344        template <typename T, typename T::X X> ...
13345
13346      or:
13347
13348        template <class C, class D*> ...
13349
13350      Here, the first parameter is a type parameter, and the second is
13351      a non-type parameter.  We can tell by looking at the token after
13352      the identifier -- if it is a `,', `=', or `>' then we have a type
13353      parameter.  */
13354   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13355     {
13356       /* Peek at the token after `class' or `typename'.  */
13357       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13358       /* If it's an ellipsis, we have a template type parameter
13359          pack. */
13360       if (token->type == CPP_ELLIPSIS)
13361         return cp_parser_type_parameter (parser, is_parameter_pack);
13362       /* If it's an identifier, skip it.  */
13363       if (token->type == CPP_NAME)
13364         token = cp_lexer_peek_nth_token (parser->lexer, 3);
13365       /* Now, see if the token looks like the end of a template
13366          parameter.  */
13367       if (token->type == CPP_COMMA
13368           || token->type == CPP_EQ
13369           || token->type == CPP_GREATER)
13370         return cp_parser_type_parameter (parser, is_parameter_pack);
13371     }
13372
13373   /* Otherwise, it is a non-type parameter.
13374
13375      [temp.param]
13376
13377      When parsing a default template-argument for a non-type
13378      template-parameter, the first non-nested `>' is taken as the end
13379      of the template parameter-list rather than a greater-than
13380      operator.  */
13381   *is_non_type = true;
13382   parameter_declarator
13383      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13384                                         /*parenthesized_p=*/NULL);
13385
13386   if (!parameter_declarator)
13387     return error_mark_node;
13388
13389   /* If the parameter declaration is marked as a parameter pack, set
13390      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13391      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13392      grokdeclarator. */
13393   if (parameter_declarator->declarator
13394       && parameter_declarator->declarator->parameter_pack_p)
13395     {
13396       *is_parameter_pack = true;
13397       parameter_declarator->declarator->parameter_pack_p = false;
13398     }
13399
13400   if (parameter_declarator->default_argument)
13401     {
13402       /* Can happen in some cases of erroneous input (c++/34892).  */
13403       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13404         /* Consume the `...' for better error recovery.  */
13405         cp_lexer_consume_token (parser->lexer);
13406     }
13407   /* If the next token is an ellipsis, and we don't already have it
13408      marked as a parameter pack, then we have a parameter pack (that
13409      has no declarator).  */
13410   else if (!*is_parameter_pack
13411            && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13412            && (declarator_can_be_parameter_pack
13413                (parameter_declarator->declarator)))
13414     {
13415       /* Consume the `...'.  */
13416       cp_lexer_consume_token (parser->lexer);
13417       maybe_warn_variadic_templates ();
13418       
13419       *is_parameter_pack = true;
13420     }
13421   /* We might end up with a pack expansion as the type of the non-type
13422      template parameter, in which case this is a non-type template
13423      parameter pack.  */
13424   else if (parameter_declarator->decl_specifiers.type
13425            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13426     {
13427       *is_parameter_pack = true;
13428       parameter_declarator->decl_specifiers.type = 
13429         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13430     }
13431
13432   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13433     {
13434       /* Parameter packs cannot have default arguments.  However, a
13435          user may try to do so, so we'll parse them and give an
13436          appropriate diagnostic here.  */
13437
13438       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13439       
13440       /* Find the name of the parameter pack.  */     
13441       id_declarator = parameter_declarator->declarator;
13442       while (id_declarator && id_declarator->kind != cdk_id)
13443         id_declarator = id_declarator->declarator;
13444       
13445       if (id_declarator && id_declarator->kind == cdk_id)
13446         error_at (start_token->location,
13447                   "template parameter pack %qD cannot have a default argument",
13448                   id_declarator->u.id.unqualified_name);
13449       else
13450         error_at (start_token->location,
13451                   "template parameter pack cannot have a default argument");
13452       
13453       /* Parse the default argument, but throw away the result.  */
13454       cp_parser_default_argument (parser, /*template_parm_p=*/true);
13455     }
13456
13457   parm = grokdeclarator (parameter_declarator->declarator,
13458                          &parameter_declarator->decl_specifiers,
13459                          TPARM, /*initialized=*/0,
13460                          /*attrlist=*/NULL);
13461   if (parm == error_mark_node)
13462     return error_mark_node;
13463
13464   return build_tree_list (parameter_declarator->default_argument, parm);
13465 }
13466
13467 /* Parse a type-parameter.
13468
13469    type-parameter:
13470      class identifier [opt]
13471      class identifier [opt] = type-id
13472      typename identifier [opt]
13473      typename identifier [opt] = type-id
13474      template < template-parameter-list > class identifier [opt]
13475      template < template-parameter-list > class identifier [opt]
13476        = id-expression
13477
13478    GNU Extension (variadic templates):
13479
13480    type-parameter:
13481      class ... identifier [opt]
13482      typename ... identifier [opt]
13483
13484    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
13485    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
13486    the declaration of the parameter.
13487
13488    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13489
13490 static tree
13491 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13492 {
13493   cp_token *token;
13494   tree parameter;
13495
13496   /* Look for a keyword to tell us what kind of parameter this is.  */
13497   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13498   if (!token)
13499     return error_mark_node;
13500
13501   switch (token->keyword)
13502     {
13503     case RID_CLASS:
13504     case RID_TYPENAME:
13505       {
13506         tree identifier;
13507         tree default_argument;
13508
13509         /* If the next token is an ellipsis, we have a template
13510            argument pack. */
13511         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13512           {
13513             /* Consume the `...' token. */
13514             cp_lexer_consume_token (parser->lexer);
13515             maybe_warn_variadic_templates ();
13516
13517             *is_parameter_pack = true;
13518           }
13519
13520         /* If the next token is an identifier, then it names the
13521            parameter.  */
13522         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13523           identifier = cp_parser_identifier (parser);
13524         else
13525           identifier = NULL_TREE;
13526
13527         /* Create the parameter.  */
13528         parameter = finish_template_type_parm (class_type_node, identifier);
13529
13530         /* If the next token is an `=', we have a default argument.  */
13531         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13532           {
13533             /* Consume the `=' token.  */
13534             cp_lexer_consume_token (parser->lexer);
13535             /* Parse the default-argument.  */
13536             push_deferring_access_checks (dk_no_deferred);
13537             default_argument = cp_parser_type_id (parser);
13538
13539             /* Template parameter packs cannot have default
13540                arguments. */
13541             if (*is_parameter_pack)
13542               {
13543                 if (identifier)
13544                   error_at (token->location,
13545                             "template parameter pack %qD cannot have a "
13546                             "default argument", identifier);
13547                 else
13548                   error_at (token->location,
13549                             "template parameter packs cannot have "
13550                             "default arguments");
13551                 default_argument = NULL_TREE;
13552               }
13553             else if (check_for_bare_parameter_packs (default_argument))
13554               default_argument = error_mark_node;
13555             pop_deferring_access_checks ();
13556           }
13557         else
13558           default_argument = NULL_TREE;
13559
13560         /* Create the combined representation of the parameter and the
13561            default argument.  */
13562         parameter = build_tree_list (default_argument, parameter);
13563       }
13564       break;
13565
13566     case RID_TEMPLATE:
13567       {
13568         tree identifier;
13569         tree default_argument;
13570
13571         /* Look for the `<'.  */
13572         cp_parser_require (parser, CPP_LESS, RT_LESS);
13573         /* Parse the template-parameter-list.  */
13574         cp_parser_template_parameter_list (parser);
13575         /* Look for the `>'.  */
13576         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13577         /* Look for the `class' or 'typename' keywords.  */
13578         cp_parser_type_parameter_key (parser);
13579         /* If the next token is an ellipsis, we have a template
13580            argument pack. */
13581         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13582           {
13583             /* Consume the `...' token. */
13584             cp_lexer_consume_token (parser->lexer);
13585             maybe_warn_variadic_templates ();
13586
13587             *is_parameter_pack = true;
13588           }
13589         /* If the next token is an `=', then there is a
13590            default-argument.  If the next token is a `>', we are at
13591            the end of the parameter-list.  If the next token is a `,',
13592            then we are at the end of this parameter.  */
13593         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13594             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13595             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13596           {
13597             identifier = cp_parser_identifier (parser);
13598             /* Treat invalid names as if the parameter were nameless.  */
13599             if (identifier == error_mark_node)
13600               identifier = NULL_TREE;
13601           }
13602         else
13603           identifier = NULL_TREE;
13604
13605         /* Create the template parameter.  */
13606         parameter = finish_template_template_parm (class_type_node,
13607                                                    identifier);
13608
13609         /* If the next token is an `=', then there is a
13610            default-argument.  */
13611         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13612           {
13613             bool is_template;
13614
13615             /* Consume the `='.  */
13616             cp_lexer_consume_token (parser->lexer);
13617             /* Parse the id-expression.  */
13618             push_deferring_access_checks (dk_no_deferred);
13619             /* save token before parsing the id-expression, for error
13620                reporting */
13621             token = cp_lexer_peek_token (parser->lexer);
13622             default_argument
13623               = cp_parser_id_expression (parser,
13624                                          /*template_keyword_p=*/false,
13625                                          /*check_dependency_p=*/true,
13626                                          /*template_p=*/&is_template,
13627                                          /*declarator_p=*/false,
13628                                          /*optional_p=*/false);
13629             if (TREE_CODE (default_argument) == TYPE_DECL)
13630               /* If the id-expression was a template-id that refers to
13631                  a template-class, we already have the declaration here,
13632                  so no further lookup is needed.  */
13633                  ;
13634             else
13635               /* Look up the name.  */
13636               default_argument
13637                 = cp_parser_lookup_name (parser, default_argument,
13638                                          none_type,
13639                                          /*is_template=*/is_template,
13640                                          /*is_namespace=*/false,
13641                                          /*check_dependency=*/true,
13642                                          /*ambiguous_decls=*/NULL,
13643                                          token->location);
13644             /* See if the default argument is valid.  */
13645             default_argument
13646               = check_template_template_default_arg (default_argument);
13647
13648             /* Template parameter packs cannot have default
13649                arguments. */
13650             if (*is_parameter_pack)
13651               {
13652                 if (identifier)
13653                   error_at (token->location,
13654                             "template parameter pack %qD cannot "
13655                             "have a default argument",
13656                             identifier);
13657                 else
13658                   error_at (token->location, "template parameter packs cannot "
13659                             "have default arguments");
13660                 default_argument = NULL_TREE;
13661               }
13662             pop_deferring_access_checks ();
13663           }
13664         else
13665           default_argument = NULL_TREE;
13666
13667         /* Create the combined representation of the parameter and the
13668            default argument.  */
13669         parameter = build_tree_list (default_argument, parameter);
13670       }
13671       break;
13672
13673     default:
13674       gcc_unreachable ();
13675       break;
13676     }
13677
13678   return parameter;
13679 }
13680
13681 /* Parse a template-id.
13682
13683    template-id:
13684      template-name < template-argument-list [opt] >
13685
13686    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13687    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
13688    returned.  Otherwise, if the template-name names a function, or set
13689    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
13690    names a class, returns a TYPE_DECL for the specialization.
13691
13692    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13693    uninstantiated templates.  */
13694
13695 static tree
13696 cp_parser_template_id (cp_parser *parser,
13697                        bool template_keyword_p,
13698                        bool check_dependency_p,
13699                        enum tag_types tag_type,
13700                        bool is_declaration)
13701 {
13702   int i;
13703   tree templ;
13704   tree arguments;
13705   tree template_id;
13706   cp_token_position start_of_id = 0;
13707   deferred_access_check *chk;
13708   vec<deferred_access_check, va_gc> *access_check;
13709   cp_token *next_token = NULL, *next_token_2 = NULL;
13710   bool is_identifier;
13711
13712   /* If the next token corresponds to a template-id, there is no need
13713      to reparse it.  */
13714   next_token = cp_lexer_peek_token (parser->lexer);
13715   if (next_token->type == CPP_TEMPLATE_ID)
13716     {
13717       struct tree_check *check_value;
13718
13719       /* Get the stored value.  */
13720       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13721       /* Perform any access checks that were deferred.  */
13722       access_check = check_value->checks;
13723       if (access_check)
13724         {
13725           FOR_EACH_VEC_ELT (*access_check, i, chk)
13726             perform_or_defer_access_check (chk->binfo,
13727                                            chk->decl,
13728                                            chk->diag_decl,
13729                                            tf_warning_or_error);
13730         }
13731       /* Return the stored value.  */
13732       return check_value->value;
13733     }
13734
13735   /* Avoid performing name lookup if there is no possibility of
13736      finding a template-id.  */
13737   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13738       || (next_token->type == CPP_NAME
13739           && !cp_parser_nth_token_starts_template_argument_list_p
13740                (parser, 2)))
13741     {
13742       cp_parser_error (parser, "expected template-id");
13743       return error_mark_node;
13744     }
13745
13746   /* Remember where the template-id starts.  */
13747   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13748     start_of_id = cp_lexer_token_position (parser->lexer, false);
13749
13750   push_deferring_access_checks (dk_deferred);
13751
13752   /* Parse the template-name.  */
13753   is_identifier = false;
13754   templ = cp_parser_template_name (parser, template_keyword_p,
13755                                    check_dependency_p,
13756                                    is_declaration,
13757                                    tag_type,
13758                                    &is_identifier);
13759   if (templ == error_mark_node || is_identifier)
13760     {
13761       pop_deferring_access_checks ();
13762       return templ;
13763     }
13764
13765   /* If we find the sequence `[:' after a template-name, it's probably
13766      a digraph-typo for `< ::'. Substitute the tokens and check if we can
13767      parse correctly the argument list.  */
13768   next_token = cp_lexer_peek_token (parser->lexer);
13769   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13770   if (next_token->type == CPP_OPEN_SQUARE
13771       && next_token->flags & DIGRAPH
13772       && next_token_2->type == CPP_COLON
13773       && !(next_token_2->flags & PREV_WHITE))
13774     {
13775       cp_parser_parse_tentatively (parser);
13776       /* Change `:' into `::'.  */
13777       next_token_2->type = CPP_SCOPE;
13778       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13779          CPP_LESS.  */
13780       cp_lexer_consume_token (parser->lexer);
13781
13782       /* Parse the arguments.  */
13783       arguments = cp_parser_enclosed_template_argument_list (parser);
13784       if (!cp_parser_parse_definitely (parser))
13785         {
13786           /* If we couldn't parse an argument list, then we revert our changes
13787              and return simply an error. Maybe this is not a template-id
13788              after all.  */
13789           next_token_2->type = CPP_COLON;
13790           cp_parser_error (parser, "expected %<<%>");
13791           pop_deferring_access_checks ();
13792           return error_mark_node;
13793         }
13794       /* Otherwise, emit an error about the invalid digraph, but continue
13795          parsing because we got our argument list.  */
13796       if (permerror (next_token->location,
13797                      "%<<::%> cannot begin a template-argument list"))
13798         {
13799           static bool hint = false;
13800           inform (next_token->location,
13801                   "%<<:%> is an alternate spelling for %<[%>."
13802                   " Insert whitespace between %<<%> and %<::%>");
13803           if (!hint && !flag_permissive)
13804             {
13805               inform (next_token->location, "(if you use %<-fpermissive%> "
13806                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13807                       "accept your code)");
13808               hint = true;
13809             }
13810         }
13811     }
13812   else
13813     {
13814       /* Look for the `<' that starts the template-argument-list.  */
13815       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13816         {
13817           pop_deferring_access_checks ();
13818           return error_mark_node;
13819         }
13820       /* Parse the arguments.  */
13821       arguments = cp_parser_enclosed_template_argument_list (parser);
13822     }
13823
13824   /* Build a representation of the specialization.  */
13825   if (identifier_p (templ))
13826     template_id = build_min_nt_loc (next_token->location,
13827                                     TEMPLATE_ID_EXPR,
13828                                     templ, arguments);
13829   else if (DECL_TYPE_TEMPLATE_P (templ)
13830            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13831     {
13832       bool entering_scope;
13833       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13834          template (rather than some instantiation thereof) only if
13835          is not nested within some other construct.  For example, in
13836          "template <typename T> void f(T) { A<T>::", A<T> is just an
13837          instantiation of A.  */
13838       entering_scope = (template_parm_scope_p ()
13839                         && cp_lexer_next_token_is (parser->lexer,
13840                                                    CPP_SCOPE));
13841       template_id
13842         = finish_template_type (templ, arguments, entering_scope);
13843     }
13844   else if (variable_template_p (templ))
13845     {
13846       template_id = lookup_template_variable (templ, arguments);
13847     }
13848   else
13849     {
13850       /* If it's not a class-template or a template-template, it should be
13851          a function-template.  */
13852       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13853                    || TREE_CODE (templ) == OVERLOAD
13854                    || BASELINK_P (templ)));
13855
13856       template_id = lookup_template_function (templ, arguments);
13857     }
13858
13859   /* If parsing tentatively, replace the sequence of tokens that makes
13860      up the template-id with a CPP_TEMPLATE_ID token.  That way,
13861      should we re-parse the token stream, we will not have to repeat
13862      the effort required to do the parse, nor will we issue duplicate
13863      error messages about problems during instantiation of the
13864      template.  */
13865   if (start_of_id
13866       /* Don't do this if we had a parse error in a declarator; re-parsing
13867          might succeed if a name changes meaning (60361).  */
13868       && !(cp_parser_error_occurred (parser)
13869            && cp_parser_parsing_tentatively (parser)
13870            && parser->in_declarator_p))
13871     {
13872       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13873
13874       /* Reset the contents of the START_OF_ID token.  */
13875       token->type = CPP_TEMPLATE_ID;
13876       /* Retrieve any deferred checks.  Do not pop this access checks yet
13877          so the memory will not be reclaimed during token replacing below.  */
13878       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13879       token->u.tree_check_value->value = template_id;
13880       token->u.tree_check_value->checks = get_deferred_access_checks ();
13881       token->keyword = RID_MAX;
13882
13883       /* Purge all subsequent tokens.  */
13884       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13885
13886       /* ??? Can we actually assume that, if template_id ==
13887          error_mark_node, we will have issued a diagnostic to the
13888          user, as opposed to simply marking the tentative parse as
13889          failed?  */
13890       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13891         error_at (token->location, "parse error in template argument list");
13892     }
13893
13894   pop_to_parent_deferring_access_checks ();
13895   return template_id;
13896 }
13897
13898 /* Parse a template-name.
13899
13900    template-name:
13901      identifier
13902
13903    The standard should actually say:
13904
13905    template-name:
13906      identifier
13907      operator-function-id
13908
13909    A defect report has been filed about this issue.
13910
13911    A conversion-function-id cannot be a template name because they cannot
13912    be part of a template-id. In fact, looking at this code:
13913
13914    a.operator K<int>()
13915
13916    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13917    It is impossible to call a templated conversion-function-id with an
13918    explicit argument list, since the only allowed template parameter is
13919    the type to which it is converting.
13920
13921    If TEMPLATE_KEYWORD_P is true, then we have just seen the
13922    `template' keyword, in a construction like:
13923
13924      T::template f<3>()
13925
13926    In that case `f' is taken to be a template-name, even though there
13927    is no way of knowing for sure.
13928
13929    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13930    name refers to a set of overloaded functions, at least one of which
13931    is a template, or an IDENTIFIER_NODE with the name of the template,
13932    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
13933    names are looked up inside uninstantiated templates.  */
13934
13935 static tree
13936 cp_parser_template_name (cp_parser* parser,
13937                          bool template_keyword_p,
13938                          bool check_dependency_p,
13939                          bool is_declaration,
13940                          enum tag_types tag_type,
13941                          bool *is_identifier)
13942 {
13943   tree identifier;
13944   tree decl;
13945   tree fns;
13946   cp_token *token = cp_lexer_peek_token (parser->lexer);
13947
13948   /* If the next token is `operator', then we have either an
13949      operator-function-id or a conversion-function-id.  */
13950   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13951     {
13952       /* We don't know whether we're looking at an
13953          operator-function-id or a conversion-function-id.  */
13954       cp_parser_parse_tentatively (parser);
13955       /* Try an operator-function-id.  */
13956       identifier = cp_parser_operator_function_id (parser);
13957       /* If that didn't work, try a conversion-function-id.  */
13958       if (!cp_parser_parse_definitely (parser))
13959         {
13960           cp_parser_error (parser, "expected template-name");
13961           return error_mark_node;
13962         }
13963     }
13964   /* Look for the identifier.  */
13965   else
13966     identifier = cp_parser_identifier (parser);
13967
13968   /* If we didn't find an identifier, we don't have a template-id.  */
13969   if (identifier == error_mark_node)
13970     return error_mark_node;
13971
13972   /* If the name immediately followed the `template' keyword, then it
13973      is a template-name.  However, if the next token is not `<', then
13974      we do not treat it as a template-name, since it is not being used
13975      as part of a template-id.  This enables us to handle constructs
13976      like:
13977
13978        template <typename T> struct S { S(); };
13979        template <typename T> S<T>::S();
13980
13981      correctly.  We would treat `S' as a template -- if it were `S<T>'
13982      -- but we do not if there is no `<'.  */
13983
13984   if (processing_template_decl
13985       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13986     {
13987       /* In a declaration, in a dependent context, we pretend that the
13988          "template" keyword was present in order to improve error
13989          recovery.  For example, given:
13990
13991            template <typename T> void f(T::X<int>);
13992
13993          we want to treat "X<int>" as a template-id.  */
13994       if (is_declaration
13995           && !template_keyword_p
13996           && parser->scope && TYPE_P (parser->scope)
13997           && check_dependency_p
13998           && dependent_scope_p (parser->scope)
13999           /* Do not do this for dtors (or ctors), since they never
14000              need the template keyword before their name.  */
14001           && !constructor_name_p (identifier, parser->scope))
14002         {
14003           cp_token_position start = 0;
14004
14005           /* Explain what went wrong.  */
14006           error_at (token->location, "non-template %qD used as template",
14007                     identifier);
14008           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14009                   parser->scope, identifier);
14010           /* If parsing tentatively, find the location of the "<" token.  */
14011           if (cp_parser_simulate_error (parser))
14012             start = cp_lexer_token_position (parser->lexer, true);
14013           /* Parse the template arguments so that we can issue error
14014              messages about them.  */
14015           cp_lexer_consume_token (parser->lexer);
14016           cp_parser_enclosed_template_argument_list (parser);
14017           /* Skip tokens until we find a good place from which to
14018              continue parsing.  */
14019           cp_parser_skip_to_closing_parenthesis (parser,
14020                                                  /*recovering=*/true,
14021                                                  /*or_comma=*/true,
14022                                                  /*consume_paren=*/false);
14023           /* If parsing tentatively, permanently remove the
14024              template argument list.  That will prevent duplicate
14025              error messages from being issued about the missing
14026              "template" keyword.  */
14027           if (start)
14028             cp_lexer_purge_tokens_after (parser->lexer, start);
14029           if (is_identifier)
14030             *is_identifier = true;
14031           return identifier;
14032         }
14033
14034       /* If the "template" keyword is present, then there is generally
14035          no point in doing name-lookup, so we just return IDENTIFIER.
14036          But, if the qualifying scope is non-dependent then we can
14037          (and must) do name-lookup normally.  */
14038       if (template_keyword_p
14039           && (!parser->scope
14040               || (TYPE_P (parser->scope)
14041                   && dependent_type_p (parser->scope))))
14042         return identifier;
14043     }
14044
14045   /* Look up the name.  */
14046   decl = cp_parser_lookup_name (parser, identifier,
14047                                 tag_type,
14048                                 /*is_template=*/true,
14049                                 /*is_namespace=*/false,
14050                                 check_dependency_p,
14051                                 /*ambiguous_decls=*/NULL,
14052                                 token->location);
14053
14054   decl = strip_using_decl (decl);
14055
14056   /* If DECL is a template, then the name was a template-name.  */
14057   if (TREE_CODE (decl) == TEMPLATE_DECL)
14058     {
14059       if (TREE_DEPRECATED (decl)
14060           && deprecated_state != DEPRECATED_SUPPRESS)
14061         warn_deprecated_use (decl, NULL_TREE);
14062     }
14063   else
14064     {
14065       tree fn = NULL_TREE;
14066
14067       /* The standard does not explicitly indicate whether a name that
14068          names a set of overloaded declarations, some of which are
14069          templates, is a template-name.  However, such a name should
14070          be a template-name; otherwise, there is no way to form a
14071          template-id for the overloaded templates.  */
14072       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14073       if (TREE_CODE (fns) == OVERLOAD)
14074         for (fn = fns; fn; fn = OVL_NEXT (fn))
14075           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14076             break;
14077
14078       if (!fn)
14079         {
14080           /* The name does not name a template.  */
14081           cp_parser_error (parser, "expected template-name");
14082           return error_mark_node;
14083         }
14084     }
14085
14086   /* If DECL is dependent, and refers to a function, then just return
14087      its name; we will look it up again during template instantiation.  */
14088   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14089     {
14090       tree scope = ovl_scope (decl);
14091       if (TYPE_P (scope) && dependent_type_p (scope))
14092         return identifier;
14093     }
14094
14095   return decl;
14096 }
14097
14098 /* Parse a template-argument-list.
14099
14100    template-argument-list:
14101      template-argument ... [opt]
14102      template-argument-list , template-argument ... [opt]
14103
14104    Returns a TREE_VEC containing the arguments.  */
14105
14106 static tree
14107 cp_parser_template_argument_list (cp_parser* parser)
14108 {
14109   tree fixed_args[10];
14110   unsigned n_args = 0;
14111   unsigned alloced = 10;
14112   tree *arg_ary = fixed_args;
14113   tree vec;
14114   bool saved_in_template_argument_list_p;
14115   bool saved_ice_p;
14116   bool saved_non_ice_p;
14117
14118   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14119   parser->in_template_argument_list_p = true;
14120   /* Even if the template-id appears in an integral
14121      constant-expression, the contents of the argument list do
14122      not.  */
14123   saved_ice_p = parser->integral_constant_expression_p;
14124   parser->integral_constant_expression_p = false;
14125   saved_non_ice_p = parser->non_integral_constant_expression_p;
14126   parser->non_integral_constant_expression_p = false;
14127
14128   /* Parse the arguments.  */
14129   do
14130     {
14131       tree argument;
14132
14133       if (n_args)
14134         /* Consume the comma.  */
14135         cp_lexer_consume_token (parser->lexer);
14136
14137       /* Parse the template-argument.  */
14138       argument = cp_parser_template_argument (parser);
14139
14140       /* If the next token is an ellipsis, we're expanding a template
14141          argument pack. */
14142       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14143         {
14144           if (argument == error_mark_node)
14145             {
14146               cp_token *token = cp_lexer_peek_token (parser->lexer);
14147               error_at (token->location,
14148                         "expected parameter pack before %<...%>");
14149             }
14150           /* Consume the `...' token. */
14151           cp_lexer_consume_token (parser->lexer);
14152
14153           /* Make the argument into a TYPE_PACK_EXPANSION or
14154              EXPR_PACK_EXPANSION. */
14155           argument = make_pack_expansion (argument);
14156         }
14157
14158       if (n_args == alloced)
14159         {
14160           alloced *= 2;
14161
14162           if (arg_ary == fixed_args)
14163             {
14164               arg_ary = XNEWVEC (tree, alloced);
14165               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14166             }
14167           else
14168             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14169         }
14170       arg_ary[n_args++] = argument;
14171     }
14172   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14173
14174   vec = make_tree_vec (n_args);
14175
14176   while (n_args--)
14177     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14178
14179   if (arg_ary != fixed_args)
14180     free (arg_ary);
14181   parser->non_integral_constant_expression_p = saved_non_ice_p;
14182   parser->integral_constant_expression_p = saved_ice_p;
14183   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14184 #ifdef ENABLE_CHECKING
14185   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14186 #endif
14187   return vec;
14188 }
14189
14190 /* Parse a template-argument.
14191
14192    template-argument:
14193      assignment-expression
14194      type-id
14195      id-expression
14196
14197    The representation is that of an assignment-expression, type-id, or
14198    id-expression -- except that the qualified id-expression is
14199    evaluated, so that the value returned is either a DECL or an
14200    OVERLOAD.
14201
14202    Although the standard says "assignment-expression", it forbids
14203    throw-expressions or assignments in the template argument.
14204    Therefore, we use "conditional-expression" instead.  */
14205
14206 static tree
14207 cp_parser_template_argument (cp_parser* parser)
14208 {
14209   tree argument;
14210   bool template_p;
14211   bool address_p;
14212   bool maybe_type_id = false;
14213   cp_token *token = NULL, *argument_start_token = NULL;
14214   location_t loc = 0;
14215   cp_id_kind idk;
14216
14217   /* There's really no way to know what we're looking at, so we just
14218      try each alternative in order.
14219
14220        [temp.arg]
14221
14222        In a template-argument, an ambiguity between a type-id and an
14223        expression is resolved to a type-id, regardless of the form of
14224        the corresponding template-parameter.
14225
14226      Therefore, we try a type-id first.  */
14227   cp_parser_parse_tentatively (parser);
14228   argument = cp_parser_template_type_arg (parser);
14229   /* If there was no error parsing the type-id but the next token is a
14230      '>>', our behavior depends on which dialect of C++ we're
14231      parsing. In C++98, we probably found a typo for '> >'. But there
14232      are type-id which are also valid expressions. For instance:
14233
14234      struct X { int operator >> (int); };
14235      template <int V> struct Foo {};
14236      Foo<X () >> 5> r;
14237
14238      Here 'X()' is a valid type-id of a function type, but the user just
14239      wanted to write the expression "X() >> 5". Thus, we remember that we
14240      found a valid type-id, but we still try to parse the argument as an
14241      expression to see what happens. 
14242
14243      In C++0x, the '>>' will be considered two separate '>'
14244      tokens.  */
14245   if (!cp_parser_error_occurred (parser)
14246       && cxx_dialect == cxx98
14247       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14248     {
14249       maybe_type_id = true;
14250       cp_parser_abort_tentative_parse (parser);
14251     }
14252   else
14253     {
14254       /* If the next token isn't a `,' or a `>', then this argument wasn't
14255       really finished. This means that the argument is not a valid
14256       type-id.  */
14257       if (!cp_parser_next_token_ends_template_argument_p (parser))
14258         cp_parser_error (parser, "expected template-argument");
14259       /* If that worked, we're done.  */
14260       if (cp_parser_parse_definitely (parser))
14261         return argument;
14262     }
14263   /* We're still not sure what the argument will be.  */
14264   cp_parser_parse_tentatively (parser);
14265   /* Try a template.  */
14266   argument_start_token = cp_lexer_peek_token (parser->lexer);
14267   argument = cp_parser_id_expression (parser,
14268                                       /*template_keyword_p=*/false,
14269                                       /*check_dependency_p=*/true,
14270                                       &template_p,
14271                                       /*declarator_p=*/false,
14272                                       /*optional_p=*/false);
14273   /* If the next token isn't a `,' or a `>', then this argument wasn't
14274      really finished.  */
14275   if (!cp_parser_next_token_ends_template_argument_p (parser))
14276     cp_parser_error (parser, "expected template-argument");
14277   if (!cp_parser_error_occurred (parser))
14278     {
14279       /* Figure out what is being referred to.  If the id-expression
14280          was for a class template specialization, then we will have a
14281          TYPE_DECL at this point.  There is no need to do name lookup
14282          at this point in that case.  */
14283       if (TREE_CODE (argument) != TYPE_DECL)
14284         argument = cp_parser_lookup_name (parser, argument,
14285                                           none_type,
14286                                           /*is_template=*/template_p,
14287                                           /*is_namespace=*/false,
14288                                           /*check_dependency=*/true,
14289                                           /*ambiguous_decls=*/NULL,
14290                                           argument_start_token->location);
14291       if (TREE_CODE (argument) != TEMPLATE_DECL
14292           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14293         cp_parser_error (parser, "expected template-name");
14294     }
14295   if (cp_parser_parse_definitely (parser))
14296     {
14297       if (TREE_DEPRECATED (argument))
14298         warn_deprecated_use (argument, NULL_TREE);
14299       return argument;
14300     }
14301   /* It must be a non-type argument.  There permitted cases are given
14302      in [temp.arg.nontype]:
14303
14304      -- an integral constant-expression of integral or enumeration
14305         type; or
14306
14307      -- the name of a non-type template-parameter; or
14308
14309      -- the name of an object or function with external linkage...
14310
14311      -- the address of an object or function with external linkage...
14312
14313      -- a pointer to member...  */
14314   /* Look for a non-type template parameter.  */
14315   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14316     {
14317       cp_parser_parse_tentatively (parser);
14318       argument = cp_parser_primary_expression (parser,
14319                                                /*address_p=*/false,
14320                                                /*cast_p=*/false,
14321                                                /*template_arg_p=*/true,
14322                                                &idk);
14323       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14324           || !cp_parser_next_token_ends_template_argument_p (parser))
14325         cp_parser_simulate_error (parser);
14326       if (cp_parser_parse_definitely (parser))
14327         return argument;
14328     }
14329
14330   /* If the next token is "&", the argument must be the address of an
14331      object or function with external linkage.  */
14332   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14333   if (address_p)
14334     {
14335       loc = cp_lexer_peek_token (parser->lexer)->location;
14336       cp_lexer_consume_token (parser->lexer);
14337     }
14338   /* See if we might have an id-expression.  */
14339   token = cp_lexer_peek_token (parser->lexer);
14340   if (token->type == CPP_NAME
14341       || token->keyword == RID_OPERATOR
14342       || token->type == CPP_SCOPE
14343       || token->type == CPP_TEMPLATE_ID
14344       || token->type == CPP_NESTED_NAME_SPECIFIER)
14345     {
14346       cp_parser_parse_tentatively (parser);
14347       argument = cp_parser_primary_expression (parser,
14348                                                address_p,
14349                                                /*cast_p=*/false,
14350                                                /*template_arg_p=*/true,
14351                                                &idk);
14352       if (cp_parser_error_occurred (parser)
14353           || !cp_parser_next_token_ends_template_argument_p (parser))
14354         cp_parser_abort_tentative_parse (parser);
14355       else
14356         {
14357           tree probe;
14358
14359           if (INDIRECT_REF_P (argument))
14360             {
14361               /* Strip the dereference temporarily.  */
14362               gcc_assert (REFERENCE_REF_P (argument));
14363               argument = TREE_OPERAND (argument, 0);
14364             }
14365
14366           /* If we're in a template, we represent a qualified-id referring
14367              to a static data member as a SCOPE_REF even if the scope isn't
14368              dependent so that we can check access control later.  */
14369           probe = argument;
14370           if (TREE_CODE (probe) == SCOPE_REF)
14371             probe = TREE_OPERAND (probe, 1);
14372           if (VAR_P (probe))
14373             {
14374               /* A variable without external linkage might still be a
14375                  valid constant-expression, so no error is issued here
14376                  if the external-linkage check fails.  */
14377               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14378                 cp_parser_simulate_error (parser);
14379             }
14380           else if (is_overloaded_fn (argument))
14381             /* All overloaded functions are allowed; if the external
14382                linkage test does not pass, an error will be issued
14383                later.  */
14384             ;
14385           else if (address_p
14386                    && (TREE_CODE (argument) == OFFSET_REF
14387                        || TREE_CODE (argument) == SCOPE_REF))
14388             /* A pointer-to-member.  */
14389             ;
14390           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14391             ;
14392           else
14393             cp_parser_simulate_error (parser);
14394
14395           if (cp_parser_parse_definitely (parser))
14396             {
14397               if (address_p)
14398                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14399                                              tf_warning_or_error);
14400               else
14401                 argument = convert_from_reference (argument);
14402               return argument;
14403             }
14404         }
14405     }
14406   /* If the argument started with "&", there are no other valid
14407      alternatives at this point.  */
14408   if (address_p)
14409     {
14410       cp_parser_error (parser, "invalid non-type template argument");
14411       return error_mark_node;
14412     }
14413
14414   /* If the argument wasn't successfully parsed as a type-id followed
14415      by '>>', the argument can only be a constant expression now.
14416      Otherwise, we try parsing the constant-expression tentatively,
14417      because the argument could really be a type-id.  */
14418   if (maybe_type_id)
14419     cp_parser_parse_tentatively (parser);
14420   argument = cp_parser_constant_expression (parser);
14421
14422   if (!maybe_type_id)
14423     return argument;
14424   if (!cp_parser_next_token_ends_template_argument_p (parser))
14425     cp_parser_error (parser, "expected template-argument");
14426   if (cp_parser_parse_definitely (parser))
14427     return argument;
14428   /* We did our best to parse the argument as a non type-id, but that
14429      was the only alternative that matched (albeit with a '>' after
14430      it). We can assume it's just a typo from the user, and a
14431      diagnostic will then be issued.  */
14432   return cp_parser_template_type_arg (parser);
14433 }
14434
14435 /* Parse an explicit-instantiation.
14436
14437    explicit-instantiation:
14438      template declaration
14439
14440    Although the standard says `declaration', what it really means is:
14441
14442    explicit-instantiation:
14443      template decl-specifier-seq [opt] declarator [opt] ;
14444
14445    Things like `template int S<int>::i = 5, int S<double>::j;' are not
14446    supposed to be allowed.  A defect report has been filed about this
14447    issue.
14448
14449    GNU Extension:
14450
14451    explicit-instantiation:
14452      storage-class-specifier template
14453        decl-specifier-seq [opt] declarator [opt] ;
14454      function-specifier template
14455        decl-specifier-seq [opt] declarator [opt] ;  */
14456
14457 static void
14458 cp_parser_explicit_instantiation (cp_parser* parser)
14459 {
14460   int declares_class_or_enum;
14461   cp_decl_specifier_seq decl_specifiers;
14462   tree extension_specifier = NULL_TREE;
14463
14464   timevar_push (TV_TEMPLATE_INST);
14465
14466   /* Look for an (optional) storage-class-specifier or
14467      function-specifier.  */
14468   if (cp_parser_allow_gnu_extensions_p (parser))
14469     {
14470       extension_specifier
14471         = cp_parser_storage_class_specifier_opt (parser);
14472       if (!extension_specifier)
14473         extension_specifier
14474           = cp_parser_function_specifier_opt (parser,
14475                                               /*decl_specs=*/NULL);
14476     }
14477
14478   /* Look for the `template' keyword.  */
14479   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14480   /* Let the front end know that we are processing an explicit
14481      instantiation.  */
14482   begin_explicit_instantiation ();
14483   /* [temp.explicit] says that we are supposed to ignore access
14484      control while processing explicit instantiation directives.  */
14485   push_deferring_access_checks (dk_no_check);
14486   /* Parse a decl-specifier-seq.  */
14487   cp_parser_decl_specifier_seq (parser,
14488                                 CP_PARSER_FLAGS_OPTIONAL,
14489                                 &decl_specifiers,
14490                                 &declares_class_or_enum);
14491   /* If there was exactly one decl-specifier, and it declared a class,
14492      and there's no declarator, then we have an explicit type
14493      instantiation.  */
14494   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14495     {
14496       tree type;
14497
14498       type = check_tag_decl (&decl_specifiers,
14499                              /*explicit_type_instantiation_p=*/true);
14500       /* Turn access control back on for names used during
14501          template instantiation.  */
14502       pop_deferring_access_checks ();
14503       if (type)
14504         do_type_instantiation (type, extension_specifier,
14505                                /*complain=*/tf_error);
14506     }
14507   else
14508     {
14509       cp_declarator *declarator;
14510       tree decl;
14511
14512       /* Parse the declarator.  */
14513       declarator
14514         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14515                                 /*ctor_dtor_or_conv_p=*/NULL,
14516                                 /*parenthesized_p=*/NULL,
14517                                 /*member_p=*/false,
14518                                 /*friend_p=*/false);
14519       if (declares_class_or_enum & 2)
14520         cp_parser_check_for_definition_in_return_type (declarator,
14521                                                        decl_specifiers.type,
14522                                                        decl_specifiers.locations[ds_type_spec]);
14523       if (declarator != cp_error_declarator)
14524         {
14525           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14526             permerror (decl_specifiers.locations[ds_inline],
14527                        "explicit instantiation shall not use"
14528                        " %<inline%> specifier");
14529           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14530             permerror (decl_specifiers.locations[ds_constexpr],
14531                        "explicit instantiation shall not use"
14532                        " %<constexpr%> specifier");
14533
14534           decl = grokdeclarator (declarator, &decl_specifiers,
14535                                  NORMAL, 0, &decl_specifiers.attributes);
14536           /* Turn access control back on for names used during
14537              template instantiation.  */
14538           pop_deferring_access_checks ();
14539           /* Do the explicit instantiation.  */
14540           do_decl_instantiation (decl, extension_specifier);
14541         }
14542       else
14543         {
14544           pop_deferring_access_checks ();
14545           /* Skip the body of the explicit instantiation.  */
14546           cp_parser_skip_to_end_of_statement (parser);
14547         }
14548     }
14549   /* We're done with the instantiation.  */
14550   end_explicit_instantiation ();
14551
14552   cp_parser_consume_semicolon_at_end_of_statement (parser);
14553
14554   timevar_pop (TV_TEMPLATE_INST);
14555 }
14556
14557 /* Parse an explicit-specialization.
14558
14559    explicit-specialization:
14560      template < > declaration
14561
14562    Although the standard says `declaration', what it really means is:
14563
14564    explicit-specialization:
14565      template <> decl-specifier [opt] init-declarator [opt] ;
14566      template <> function-definition
14567      template <> explicit-specialization
14568      template <> template-declaration  */
14569
14570 static void
14571 cp_parser_explicit_specialization (cp_parser* parser)
14572 {
14573   bool need_lang_pop;
14574   cp_token *token = cp_lexer_peek_token (parser->lexer);
14575
14576   /* Look for the `template' keyword.  */
14577   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14578   /* Look for the `<'.  */
14579   cp_parser_require (parser, CPP_LESS, RT_LESS);
14580   /* Look for the `>'.  */
14581   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14582   /* We have processed another parameter list.  */
14583   ++parser->num_template_parameter_lists;
14584   /* [temp]
14585
14586      A template ... explicit specialization ... shall not have C
14587      linkage.  */
14588   if (current_lang_name == lang_name_c)
14589     {
14590       error_at (token->location, "template specialization with C linkage");
14591       /* Give it C++ linkage to avoid confusing other parts of the
14592          front end.  */
14593       push_lang_context (lang_name_cplusplus);
14594       need_lang_pop = true;
14595     }
14596   else
14597     need_lang_pop = false;
14598   /* Let the front end know that we are beginning a specialization.  */
14599   if (!begin_specialization ())
14600     {
14601       end_specialization ();
14602       return;
14603     }
14604
14605   /* If the next keyword is `template', we need to figure out whether
14606      or not we're looking a template-declaration.  */
14607   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14608     {
14609       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14610           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14611         cp_parser_template_declaration_after_export (parser,
14612                                                      /*member_p=*/false);
14613       else
14614         cp_parser_explicit_specialization (parser);
14615     }
14616   else
14617     /* Parse the dependent declaration.  */
14618     cp_parser_single_declaration (parser,
14619                                   /*checks=*/NULL,
14620                                   /*member_p=*/false,
14621                                   /*explicit_specialization_p=*/true,
14622                                   /*friend_p=*/NULL);
14623   /* We're done with the specialization.  */
14624   end_specialization ();
14625   /* For the erroneous case of a template with C linkage, we pushed an
14626      implicit C++ linkage scope; exit that scope now.  */
14627   if (need_lang_pop)
14628     pop_lang_context ();
14629   /* We're done with this parameter list.  */
14630   --parser->num_template_parameter_lists;
14631 }
14632
14633 /* Parse a type-specifier.
14634
14635    type-specifier:
14636      simple-type-specifier
14637      class-specifier
14638      enum-specifier
14639      elaborated-type-specifier
14640      cv-qualifier
14641
14642    GNU Extension:
14643
14644    type-specifier:
14645      __complex__
14646
14647    Returns a representation of the type-specifier.  For a
14648    class-specifier, enum-specifier, or elaborated-type-specifier, a
14649    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14650
14651    The parser flags FLAGS is used to control type-specifier parsing.
14652
14653    If IS_DECLARATION is TRUE, then this type-specifier is appearing
14654    in a decl-specifier-seq.
14655
14656    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14657    class-specifier, enum-specifier, or elaborated-type-specifier, then
14658    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
14659    if a type is declared; 2 if it is defined.  Otherwise, it is set to
14660    zero.
14661
14662    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14663    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
14664    is set to FALSE.  */
14665
14666 static tree
14667 cp_parser_type_specifier (cp_parser* parser,
14668                           cp_parser_flags flags,
14669                           cp_decl_specifier_seq *decl_specs,
14670                           bool is_declaration,
14671                           int* declares_class_or_enum,
14672                           bool* is_cv_qualifier)
14673 {
14674   tree type_spec = NULL_TREE;
14675   cp_token *token;
14676   enum rid keyword;
14677   cp_decl_spec ds = ds_last;
14678
14679   /* Assume this type-specifier does not declare a new type.  */
14680   if (declares_class_or_enum)
14681     *declares_class_or_enum = 0;
14682   /* And that it does not specify a cv-qualifier.  */
14683   if (is_cv_qualifier)
14684     *is_cv_qualifier = false;
14685   /* Peek at the next token.  */
14686   token = cp_lexer_peek_token (parser->lexer);
14687
14688   /* If we're looking at a keyword, we can use that to guide the
14689      production we choose.  */
14690   keyword = token->keyword;
14691   switch (keyword)
14692     {
14693     case RID_ENUM:
14694       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14695         goto elaborated_type_specifier;
14696
14697       /* Look for the enum-specifier.  */
14698       type_spec = cp_parser_enum_specifier (parser);
14699       /* If that worked, we're done.  */
14700       if (type_spec)
14701         {
14702           if (declares_class_or_enum)
14703             *declares_class_or_enum = 2;
14704           if (decl_specs)
14705             cp_parser_set_decl_spec_type (decl_specs,
14706                                           type_spec,
14707                                           token,
14708                                           /*type_definition_p=*/true);
14709           return type_spec;
14710         }
14711       else
14712         goto elaborated_type_specifier;
14713
14714       /* Any of these indicate either a class-specifier, or an
14715          elaborated-type-specifier.  */
14716     case RID_CLASS:
14717     case RID_STRUCT:
14718     case RID_UNION:
14719       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14720         goto elaborated_type_specifier;
14721
14722       /* Parse tentatively so that we can back up if we don't find a
14723          class-specifier.  */
14724       cp_parser_parse_tentatively (parser);
14725       /* Look for the class-specifier.  */
14726       type_spec = cp_parser_class_specifier (parser);
14727       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14728       /* If that worked, we're done.  */
14729       if (cp_parser_parse_definitely (parser))
14730         {
14731           if (declares_class_or_enum)
14732             *declares_class_or_enum = 2;
14733           if (decl_specs)
14734             cp_parser_set_decl_spec_type (decl_specs,
14735                                           type_spec,
14736                                           token,
14737                                           /*type_definition_p=*/true);
14738           return type_spec;
14739         }
14740
14741       /* Fall through.  */
14742     elaborated_type_specifier:
14743       /* We're declaring (not defining) a class or enum.  */
14744       if (declares_class_or_enum)
14745         *declares_class_or_enum = 1;
14746
14747       /* Fall through.  */
14748     case RID_TYPENAME:
14749       /* Look for an elaborated-type-specifier.  */
14750       type_spec
14751         = (cp_parser_elaborated_type_specifier
14752            (parser,
14753             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14754             is_declaration));
14755       if (decl_specs)
14756         cp_parser_set_decl_spec_type (decl_specs,
14757                                       type_spec,
14758                                       token,
14759                                       /*type_definition_p=*/false);
14760       return type_spec;
14761
14762     case RID_CONST:
14763       ds = ds_const;
14764       if (is_cv_qualifier)
14765         *is_cv_qualifier = true;
14766       break;
14767
14768     case RID_VOLATILE:
14769       ds = ds_volatile;
14770       if (is_cv_qualifier)
14771         *is_cv_qualifier = true;
14772       break;
14773
14774     case RID_RESTRICT:
14775       ds = ds_restrict;
14776       if (is_cv_qualifier)
14777         *is_cv_qualifier = true;
14778       break;
14779
14780     case RID_COMPLEX:
14781       /* The `__complex__' keyword is a GNU extension.  */
14782       ds = ds_complex;
14783       break;
14784
14785     default:
14786       break;
14787     }
14788
14789   /* Handle simple keywords.  */
14790   if (ds != ds_last)
14791     {
14792       if (decl_specs)
14793         {
14794           set_and_check_decl_spec_loc (decl_specs, ds, token);
14795           decl_specs->any_specifiers_p = true;
14796         }
14797       return cp_lexer_consume_token (parser->lexer)->u.value;
14798     }
14799
14800   /* If we do not already have a type-specifier, assume we are looking
14801      at a simple-type-specifier.  */
14802   type_spec = cp_parser_simple_type_specifier (parser,
14803                                                decl_specs,
14804                                                flags);
14805
14806   /* If we didn't find a type-specifier, and a type-specifier was not
14807      optional in this context, issue an error message.  */
14808   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14809     {
14810       cp_parser_error (parser, "expected type specifier");
14811       return error_mark_node;
14812     }
14813
14814   return type_spec;
14815 }
14816
14817 /* Parse a simple-type-specifier.
14818
14819    simple-type-specifier:
14820      :: [opt] nested-name-specifier [opt] type-name
14821      :: [opt] nested-name-specifier template template-id
14822      char
14823      wchar_t
14824      bool
14825      short
14826      int
14827      long
14828      signed
14829      unsigned
14830      float
14831      double
14832      void
14833
14834    C++0x Extension:
14835
14836    simple-type-specifier:
14837      auto
14838      decltype ( expression )   
14839      char16_t
14840      char32_t
14841      __underlying_type ( type-id )
14842
14843    GNU Extension:
14844
14845    simple-type-specifier:
14846      __int128
14847      __typeof__ unary-expression
14848      __typeof__ ( type-id )
14849      __typeof__ ( type-id ) { initializer-list , [opt] }
14850
14851    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
14852    appropriately updated.  */
14853
14854 static tree
14855 cp_parser_simple_type_specifier (cp_parser* parser,
14856                                  cp_decl_specifier_seq *decl_specs,
14857                                  cp_parser_flags flags)
14858 {
14859   tree type = NULL_TREE;
14860   cp_token *token;
14861   int idx;
14862
14863   /* Peek at the next token.  */
14864   token = cp_lexer_peek_token (parser->lexer);
14865
14866   /* If we're looking at a keyword, things are easy.  */
14867   switch (token->keyword)
14868     {
14869     case RID_CHAR:
14870       if (decl_specs)
14871         decl_specs->explicit_char_p = true;
14872       type = char_type_node;
14873       break;
14874     case RID_CHAR16:
14875       type = char16_type_node;
14876       break;
14877     case RID_CHAR32:
14878       type = char32_type_node;
14879       break;
14880     case RID_WCHAR:
14881       type = wchar_type_node;
14882       break;
14883     case RID_BOOL:
14884       type = boolean_type_node;
14885       break;
14886     case RID_SHORT:
14887       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14888       type = short_integer_type_node;
14889       break;
14890     case RID_INT:
14891       if (decl_specs)
14892         decl_specs->explicit_int_p = true;
14893       type = integer_type_node;
14894       break;
14895     case RID_INT_N_0:
14896     case RID_INT_N_1:
14897     case RID_INT_N_2:
14898     case RID_INT_N_3:
14899       idx = token->keyword - RID_INT_N_0;
14900       if (! int_n_enabled_p [idx])
14901         break;
14902       if (decl_specs)
14903         {
14904           decl_specs->explicit_intN_p = true;
14905           decl_specs->int_n_idx = idx;
14906         }
14907       type = int_n_trees [idx].signed_type;
14908       break;
14909     case RID_LONG:
14910       if (decl_specs)
14911         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14912       type = long_integer_type_node;
14913       break;
14914     case RID_SIGNED:
14915       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14916       type = integer_type_node;
14917       break;
14918     case RID_UNSIGNED:
14919       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14920       type = unsigned_type_node;
14921       break;
14922     case RID_FLOAT:
14923       type = float_type_node;
14924       break;
14925     case RID_DOUBLE:
14926       type = double_type_node;
14927       break;
14928     case RID_VOID:
14929       type = void_type_node;
14930       break;
14931
14932     case RID_AUTO:
14933       maybe_warn_cpp0x (CPP0X_AUTO);
14934       if (parser->auto_is_implicit_function_template_parm_p)
14935         {
14936           /* The 'auto' might be the placeholder return type for a function decl
14937              with trailing return type.  */
14938           bool have_trailing_return_fn_decl = false;
14939           if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14940               == CPP_OPEN_PAREN)
14941             {
14942               cp_parser_parse_tentatively (parser);
14943               cp_lexer_consume_token (parser->lexer);
14944               cp_lexer_consume_token (parser->lexer);
14945               if (cp_parser_skip_to_closing_parenthesis (parser,
14946                                                          /*recovering*/false,
14947                                                          /*or_comma*/false,
14948                                                          /*consume_paren*/true))
14949                 have_trailing_return_fn_decl
14950                   = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
14951               cp_parser_abort_tentative_parse (parser);
14952             }
14953
14954           if (have_trailing_return_fn_decl)
14955             {
14956               type = make_auto ();
14957               break;
14958             }
14959
14960           if (cxx_dialect >= cxx14)
14961             type = synthesize_implicit_template_parm (parser);
14962           else
14963             type = error_mark_node;
14964
14965           if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14966             {
14967               if (cxx_dialect < cxx14)
14968                 error_at (token->location,
14969                          "use of %<auto%> in lambda parameter declaration "
14970                          "only available with "
14971                          "-std=c++14 or -std=gnu++14");
14972             }
14973           else if (cxx_dialect < cxx14)
14974             error_at (token->location,
14975                      "use of %<auto%> in parameter declaration "
14976                      "only available with "
14977                      "-std=c++14 or -std=gnu++14");
14978           else
14979             pedwarn (token->location, OPT_Wpedantic,
14980                      "ISO C++ forbids use of %<auto%> in parameter "
14981                      "declaration");
14982         }
14983       else
14984         type = make_auto ();
14985       break;
14986
14987     case RID_DECLTYPE:
14988       /* Since DR 743, decltype can either be a simple-type-specifier by
14989          itself or begin a nested-name-specifier.  Parsing it will replace
14990          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14991          handling below decide what to do.  */
14992       cp_parser_decltype (parser);
14993       cp_lexer_set_token_position (parser->lexer, token);
14994       break;
14995
14996     case RID_TYPEOF:
14997       /* Consume the `typeof' token.  */
14998       cp_lexer_consume_token (parser->lexer);
14999       /* Parse the operand to `typeof'.  */
15000       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15001       /* If it is not already a TYPE, take its type.  */
15002       if (!TYPE_P (type))
15003         type = finish_typeof (type);
15004
15005       if (decl_specs)
15006         cp_parser_set_decl_spec_type (decl_specs, type,
15007                                       token,
15008                                       /*type_definition_p=*/false);
15009
15010       return type;
15011
15012     case RID_UNDERLYING_TYPE:
15013       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15014       if (decl_specs)
15015         cp_parser_set_decl_spec_type (decl_specs, type,
15016                                       token,
15017                                       /*type_definition_p=*/false);
15018
15019       return type;
15020
15021     case RID_BASES:
15022     case RID_DIRECT_BASES:
15023       type = cp_parser_trait_expr (parser, token->keyword);
15024       if (decl_specs)
15025        cp_parser_set_decl_spec_type (decl_specs, type,
15026                                      token,
15027                                      /*type_definition_p=*/false);
15028       return type;
15029     default:
15030       break;
15031     }
15032
15033   /* If token is an already-parsed decltype not followed by ::,
15034      it's a simple-type-specifier.  */
15035   if (token->type == CPP_DECLTYPE
15036       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15037     {
15038       type = token->u.value;
15039       if (decl_specs)
15040         {
15041           cp_parser_set_decl_spec_type (decl_specs, type,
15042                                         token,
15043                                         /*type_definition_p=*/false);
15044           /* Remember that we are handling a decltype in order to
15045              implement the resolution of DR 1510 when the argument
15046              isn't instantiation dependent.  */
15047           decl_specs->decltype_p = true;
15048         }
15049       cp_lexer_consume_token (parser->lexer);
15050       return type;
15051     }
15052
15053   /* If the type-specifier was for a built-in type, we're done.  */
15054   if (type)
15055     {
15056       /* Record the type.  */
15057       if (decl_specs
15058           && (token->keyword != RID_SIGNED
15059               && token->keyword != RID_UNSIGNED
15060               && token->keyword != RID_SHORT
15061               && token->keyword != RID_LONG))
15062         cp_parser_set_decl_spec_type (decl_specs,
15063                                       type,
15064                                       token,
15065                                       /*type_definition_p=*/false);
15066       if (decl_specs)
15067         decl_specs->any_specifiers_p = true;
15068
15069       /* Consume the token.  */
15070       cp_lexer_consume_token (parser->lexer);
15071
15072       if (type == error_mark_node)
15073         return error_mark_node;
15074
15075       /* There is no valid C++ program where a non-template type is
15076          followed by a "<".  That usually indicates that the user thought
15077          that the type was a template.  */
15078       cp_parser_check_for_invalid_template_id (parser, type, none_type,
15079                                                token->location);
15080
15081       return TYPE_NAME (type);
15082     }
15083
15084   /* The type-specifier must be a user-defined type.  */
15085   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15086     {
15087       bool qualified_p;
15088       bool global_p;
15089
15090       /* Don't gobble tokens or issue error messages if this is an
15091          optional type-specifier.  */
15092       if (flags & CP_PARSER_FLAGS_OPTIONAL)
15093         cp_parser_parse_tentatively (parser);
15094
15095       /* Look for the optional `::' operator.  */
15096       global_p
15097         = (cp_parser_global_scope_opt (parser,
15098                                        /*current_scope_valid_p=*/false)
15099            != NULL_TREE);
15100       /* Look for the nested-name specifier.  */
15101       qualified_p
15102         = (cp_parser_nested_name_specifier_opt (parser,
15103                                                 /*typename_keyword_p=*/false,
15104                                                 /*check_dependency_p=*/true,
15105                                                 /*type_p=*/false,
15106                                                 /*is_declaration=*/false)
15107            != NULL_TREE);
15108       token = cp_lexer_peek_token (parser->lexer);
15109       /* If we have seen a nested-name-specifier, and the next token
15110          is `template', then we are using the template-id production.  */
15111       if (parser->scope
15112           && cp_parser_optional_template_keyword (parser))
15113         {
15114           /* Look for the template-id.  */
15115           type = cp_parser_template_id (parser,
15116                                         /*template_keyword_p=*/true,
15117                                         /*check_dependency_p=*/true,
15118                                         none_type,
15119                                         /*is_declaration=*/false);
15120           /* If the template-id did not name a type, we are out of
15121              luck.  */
15122           if (TREE_CODE (type) != TYPE_DECL)
15123             {
15124               cp_parser_error (parser, "expected template-id for type");
15125               type = NULL_TREE;
15126             }
15127         }
15128       /* Otherwise, look for a type-name.  */
15129       else
15130         type = cp_parser_type_name (parser);
15131       /* Keep track of all name-lookups performed in class scopes.  */
15132       if (type
15133           && !global_p
15134           && !qualified_p
15135           && TREE_CODE (type) == TYPE_DECL
15136           && identifier_p (DECL_NAME (type)))
15137         maybe_note_name_used_in_class (DECL_NAME (type), type);
15138       /* If it didn't work out, we don't have a TYPE.  */
15139       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15140           && !cp_parser_parse_definitely (parser))
15141         type = NULL_TREE;
15142       if (type && decl_specs)
15143         cp_parser_set_decl_spec_type (decl_specs, type,
15144                                       token,
15145                                       /*type_definition_p=*/false);
15146     }
15147
15148   /* If we didn't get a type-name, issue an error message.  */
15149   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15150     {
15151       cp_parser_error (parser, "expected type-name");
15152       return error_mark_node;
15153     }
15154
15155   if (type && type != error_mark_node)
15156     {
15157       /* See if TYPE is an Objective-C type, and if so, parse and
15158          accept any protocol references following it.  Do this before
15159          the cp_parser_check_for_invalid_template_id() call, because
15160          Objective-C types can be followed by '<...>' which would
15161          enclose protocol names rather than template arguments, and so
15162          everything is fine.  */
15163       if (c_dialect_objc () && !parser->scope
15164           && (objc_is_id (type) || objc_is_class_name (type)))
15165         {
15166           tree protos = cp_parser_objc_protocol_refs_opt (parser);
15167           tree qual_type = objc_get_protocol_qualified_type (type, protos);
15168
15169           /* Clobber the "unqualified" type previously entered into
15170              DECL_SPECS with the new, improved protocol-qualified version.  */
15171           if (decl_specs)
15172             decl_specs->type = qual_type;
15173
15174           return qual_type;
15175         }
15176
15177       /* There is no valid C++ program where a non-template type is
15178          followed by a "<".  That usually indicates that the user
15179          thought that the type was a template.  */
15180       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15181                                                none_type,
15182                                                token->location);
15183     }
15184
15185   return type;
15186 }
15187
15188 /* Parse a type-name.
15189
15190    type-name:
15191      class-name
15192      enum-name
15193      typedef-name
15194      simple-template-id [in c++0x]
15195
15196    enum-name:
15197      identifier
15198
15199    typedef-name:
15200      identifier
15201
15202    Returns a TYPE_DECL for the type.  */
15203
15204 static tree
15205 cp_parser_type_name (cp_parser* parser)
15206 {
15207   tree type_decl;
15208
15209   /* We can't know yet whether it is a class-name or not.  */
15210   cp_parser_parse_tentatively (parser);
15211   /* Try a class-name.  */
15212   type_decl = cp_parser_class_name (parser,
15213                                     /*typename_keyword_p=*/false,
15214                                     /*template_keyword_p=*/false,
15215                                     none_type,
15216                                     /*check_dependency_p=*/true,
15217                                     /*class_head_p=*/false,
15218                                     /*is_declaration=*/false);
15219   /* If it's not a class-name, keep looking.  */
15220   if (!cp_parser_parse_definitely (parser))
15221     {
15222       if (cxx_dialect < cxx11)
15223         /* It must be a typedef-name or an enum-name.  */
15224         return cp_parser_nonclass_name (parser);
15225
15226       cp_parser_parse_tentatively (parser);
15227       /* It is either a simple-template-id representing an
15228          instantiation of an alias template...  */
15229       type_decl = cp_parser_template_id (parser,
15230                                          /*template_keyword_p=*/false,
15231                                          /*check_dependency_p=*/true,
15232                                          none_type,
15233                                          /*is_declaration=*/false);
15234       /* Note that this must be an instantiation of an alias template
15235          because [temp.names]/6 says:
15236          
15237              A template-id that names an alias template specialization
15238              is a type-name.
15239
15240          Whereas [temp.names]/7 says:
15241          
15242              A simple-template-id that names a class template
15243              specialization is a class-name.  */
15244       if (type_decl != NULL_TREE
15245           && TREE_CODE (type_decl) == TYPE_DECL
15246           && TYPE_DECL_ALIAS_P (type_decl))
15247         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15248       else
15249         cp_parser_simulate_error (parser);
15250
15251       if (!cp_parser_parse_definitely (parser))
15252         /* ... Or a typedef-name or an enum-name.  */
15253         return cp_parser_nonclass_name (parser);
15254     }
15255
15256   return type_decl;
15257 }
15258
15259 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15260
15261    enum-name:
15262      identifier
15263
15264    typedef-name:
15265      identifier
15266
15267    Returns a TYPE_DECL for the type.  */
15268
15269 static tree
15270 cp_parser_nonclass_name (cp_parser* parser)
15271 {
15272   tree type_decl;
15273   tree identifier;
15274
15275   cp_token *token = cp_lexer_peek_token (parser->lexer);
15276   identifier = cp_parser_identifier (parser);
15277   if (identifier == error_mark_node)
15278     return error_mark_node;
15279
15280   /* Look up the type-name.  */
15281   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15282
15283   type_decl = strip_using_decl (type_decl);
15284   
15285   if (TREE_CODE (type_decl) != TYPE_DECL
15286       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15287     {
15288       /* See if this is an Objective-C type.  */
15289       tree protos = cp_parser_objc_protocol_refs_opt (parser);
15290       tree type = objc_get_protocol_qualified_type (identifier, protos);
15291       if (type)
15292         type_decl = TYPE_NAME (type);
15293     }
15294
15295   /* Issue an error if we did not find a type-name.  */
15296   if (TREE_CODE (type_decl) != TYPE_DECL
15297       /* In Objective-C, we have the complication that class names are
15298          normally type names and start declarations (eg, the
15299          "NSObject" in "NSObject *object;"), but can be used in an
15300          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15301          is an expression.  So, a classname followed by a dot is not a
15302          valid type-name.  */
15303       || (objc_is_class_name (TREE_TYPE (type_decl))
15304           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15305     {
15306       if (!cp_parser_simulate_error (parser))
15307         cp_parser_name_lookup_error (parser, identifier, type_decl,
15308                                      NLE_TYPE, token->location);
15309       return error_mark_node;
15310     }
15311   /* Remember that the name was used in the definition of the
15312      current class so that we can check later to see if the
15313      meaning would have been different after the class was
15314      entirely defined.  */
15315   else if (type_decl != error_mark_node
15316            && !parser->scope)
15317     maybe_note_name_used_in_class (identifier, type_decl);
15318   
15319   return type_decl;
15320 }
15321
15322 /* Parse an elaborated-type-specifier.  Note that the grammar given
15323    here incorporates the resolution to DR68.
15324
15325    elaborated-type-specifier:
15326      class-key :: [opt] nested-name-specifier [opt] identifier
15327      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15328      enum-key :: [opt] nested-name-specifier [opt] identifier
15329      typename :: [opt] nested-name-specifier identifier
15330      typename :: [opt] nested-name-specifier template [opt]
15331        template-id
15332
15333    GNU extension:
15334
15335    elaborated-type-specifier:
15336      class-key attributes :: [opt] nested-name-specifier [opt] identifier
15337      class-key attributes :: [opt] nested-name-specifier [opt]
15338                template [opt] template-id
15339      enum attributes :: [opt] nested-name-specifier [opt] identifier
15340
15341    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15342    declared `friend'.  If IS_DECLARATION is TRUE, then this
15343    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15344    something is being declared.
15345
15346    Returns the TYPE specified.  */
15347
15348 static tree
15349 cp_parser_elaborated_type_specifier (cp_parser* parser,
15350                                      bool is_friend,
15351                                      bool is_declaration)
15352 {
15353   enum tag_types tag_type;
15354   tree identifier;
15355   tree type = NULL_TREE;
15356   tree attributes = NULL_TREE;
15357   tree globalscope;
15358   cp_token *token = NULL;
15359
15360   /* See if we're looking at the `enum' keyword.  */
15361   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15362     {
15363       /* Consume the `enum' token.  */
15364       cp_lexer_consume_token (parser->lexer);
15365       /* Remember that it's an enumeration type.  */
15366       tag_type = enum_type;
15367       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15368          enums) is used here.  */
15369       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15370           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15371         {
15372             pedwarn (input_location, 0, "elaborated-type-specifier "
15373                       "for a scoped enum must not use the %<%D%> keyword",
15374                       cp_lexer_peek_token (parser->lexer)->u.value);
15375           /* Consume the `struct' or `class' and parse it anyway.  */
15376           cp_lexer_consume_token (parser->lexer);
15377         }
15378       /* Parse the attributes.  */
15379       attributes = cp_parser_attributes_opt (parser);
15380     }
15381   /* Or, it might be `typename'.  */
15382   else if (cp_lexer_next_token_is_keyword (parser->lexer,
15383                                            RID_TYPENAME))
15384     {
15385       /* Consume the `typename' token.  */
15386       cp_lexer_consume_token (parser->lexer);
15387       /* Remember that it's a `typename' type.  */
15388       tag_type = typename_type;
15389     }
15390   /* Otherwise it must be a class-key.  */
15391   else
15392     {
15393       tag_type = cp_parser_class_key (parser);
15394       if (tag_type == none_type)
15395         return error_mark_node;
15396       /* Parse the attributes.  */
15397       attributes = cp_parser_attributes_opt (parser);
15398     }
15399
15400   /* Look for the `::' operator.  */
15401   globalscope =  cp_parser_global_scope_opt (parser,
15402                                              /*current_scope_valid_p=*/false);
15403   /* Look for the nested-name-specifier.  */
15404   if (tag_type == typename_type && !globalscope)
15405     {
15406       if (!cp_parser_nested_name_specifier (parser,
15407                                            /*typename_keyword_p=*/true,
15408                                            /*check_dependency_p=*/true,
15409                                            /*type_p=*/true,
15410                                             is_declaration))
15411         return error_mark_node;
15412     }
15413   else
15414     /* Even though `typename' is not present, the proposed resolution
15415        to Core Issue 180 says that in `class A<T>::B', `B' should be
15416        considered a type-name, even if `A<T>' is dependent.  */
15417     cp_parser_nested_name_specifier_opt (parser,
15418                                          /*typename_keyword_p=*/true,
15419                                          /*check_dependency_p=*/true,
15420                                          /*type_p=*/true,
15421                                          is_declaration);
15422  /* For everything but enumeration types, consider a template-id.
15423     For an enumeration type, consider only a plain identifier.  */
15424   if (tag_type != enum_type)
15425     {
15426       bool template_p = false;
15427       tree decl;
15428
15429       /* Allow the `template' keyword.  */
15430       template_p = cp_parser_optional_template_keyword (parser);
15431       /* If we didn't see `template', we don't know if there's a
15432          template-id or not.  */
15433       if (!template_p)
15434         cp_parser_parse_tentatively (parser);
15435       /* Parse the template-id.  */
15436       token = cp_lexer_peek_token (parser->lexer);
15437       decl = cp_parser_template_id (parser, template_p,
15438                                     /*check_dependency_p=*/true,
15439                                     tag_type,
15440                                     is_declaration);
15441       /* If we didn't find a template-id, look for an ordinary
15442          identifier.  */
15443       if (!template_p && !cp_parser_parse_definitely (parser))
15444         ;
15445       /* We can get here when cp_parser_template_id, called by
15446          cp_parser_class_name with tag_type == none_type, succeeds
15447          and caches a BASELINK.  Then, when called again here,
15448          instead of failing and returning an error_mark_node
15449          returns it (see template/typename17.C in C++11).
15450          ??? Could we diagnose this earlier?  */
15451       else if (tag_type == typename_type && BASELINK_P (decl))
15452         {
15453           cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15454           type = error_mark_node;
15455         }
15456       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15457          in effect, then we must assume that, upon instantiation, the
15458          template will correspond to a class.  */
15459       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15460                && tag_type == typename_type)
15461         type = make_typename_type (parser->scope, decl,
15462                                    typename_type,
15463                                    /*complain=*/tf_error);
15464       /* If the `typename' keyword is in effect and DECL is not a type
15465          decl, then type is non existent.   */
15466       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15467         ; 
15468       else if (TREE_CODE (decl) == TYPE_DECL)
15469         type = check_elaborated_type_specifier (tag_type, decl,
15470                                                 /*allow_template_p=*/true);
15471       else if (decl == error_mark_node)
15472         type = error_mark_node; 
15473     }
15474
15475   if (!type)
15476     {
15477       token = cp_lexer_peek_token (parser->lexer);
15478       identifier = cp_parser_identifier (parser);
15479
15480       if (identifier == error_mark_node)
15481         {
15482           parser->scope = NULL_TREE;
15483           return error_mark_node;
15484         }
15485
15486       /* For a `typename', we needn't call xref_tag.  */
15487       if (tag_type == typename_type
15488           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15489         return cp_parser_make_typename_type (parser, identifier,
15490                                              token->location);
15491
15492       /* Template parameter lists apply only if we are not within a
15493          function parameter list.  */
15494       bool template_parm_lists_apply
15495           = parser->num_template_parameter_lists;
15496       if (template_parm_lists_apply)
15497         for (cp_binding_level *s = current_binding_level;
15498              s && s->kind != sk_template_parms;
15499              s = s->level_chain)
15500           if (s->kind == sk_function_parms)
15501             template_parm_lists_apply = false;
15502
15503       /* Look up a qualified name in the usual way.  */
15504       if (parser->scope)
15505         {
15506           tree decl;
15507           tree ambiguous_decls;
15508
15509           decl = cp_parser_lookup_name (parser, identifier,
15510                                         tag_type,
15511                                         /*is_template=*/false,
15512                                         /*is_namespace=*/false,
15513                                         /*check_dependency=*/true,
15514                                         &ambiguous_decls,
15515                                         token->location);
15516
15517           /* If the lookup was ambiguous, an error will already have been
15518              issued.  */
15519           if (ambiguous_decls)
15520             return error_mark_node;
15521
15522           /* If we are parsing friend declaration, DECL may be a
15523              TEMPLATE_DECL tree node here.  However, we need to check
15524              whether this TEMPLATE_DECL results in valid code.  Consider
15525              the following example:
15526
15527                namespace N {
15528                  template <class T> class C {};
15529                }
15530                class X {
15531                  template <class T> friend class N::C; // #1, valid code
15532                };
15533                template <class T> class Y {
15534                  friend class N::C;                    // #2, invalid code
15535                };
15536
15537              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15538              name lookup of `N::C'.  We see that friend declaration must
15539              be template for the code to be valid.  Note that
15540              processing_template_decl does not work here since it is
15541              always 1 for the above two cases.  */
15542
15543           decl = (cp_parser_maybe_treat_template_as_class
15544                   (decl, /*tag_name_p=*/is_friend
15545                          && template_parm_lists_apply));
15546
15547           if (TREE_CODE (decl) != TYPE_DECL)
15548             {
15549               cp_parser_diagnose_invalid_type_name (parser,
15550                                                     identifier,
15551                                                     token->location);
15552               return error_mark_node;
15553             }
15554
15555           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15556             {
15557               bool allow_template = (template_parm_lists_apply
15558                                      || DECL_SELF_REFERENCE_P (decl));
15559               type = check_elaborated_type_specifier (tag_type, decl,
15560                                                       allow_template);
15561
15562               if (type == error_mark_node)
15563                 return error_mark_node;
15564             }
15565
15566           /* Forward declarations of nested types, such as
15567
15568                class C1::C2;
15569                class C1::C2::C3;
15570
15571              are invalid unless all components preceding the final '::'
15572              are complete.  If all enclosing types are complete, these
15573              declarations become merely pointless.
15574
15575              Invalid forward declarations of nested types are errors
15576              caught elsewhere in parsing.  Those that are pointless arrive
15577              here.  */
15578
15579           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15580               && !is_friend && !processing_explicit_instantiation)
15581             warning (0, "declaration %qD does not declare anything", decl);
15582
15583           type = TREE_TYPE (decl);
15584         }
15585       else
15586         {
15587           /* An elaborated-type-specifier sometimes introduces a new type and
15588              sometimes names an existing type.  Normally, the rule is that it
15589              introduces a new type only if there is not an existing type of
15590              the same name already in scope.  For example, given:
15591
15592                struct S {};
15593                void f() { struct S s; }
15594
15595              the `struct S' in the body of `f' is the same `struct S' as in
15596              the global scope; the existing definition is used.  However, if
15597              there were no global declaration, this would introduce a new
15598              local class named `S'.
15599
15600              An exception to this rule applies to the following code:
15601
15602                namespace N { struct S; }
15603
15604              Here, the elaborated-type-specifier names a new type
15605              unconditionally; even if there is already an `S' in the
15606              containing scope this declaration names a new type.
15607              This exception only applies if the elaborated-type-specifier
15608              forms the complete declaration:
15609
15610                [class.name]
15611
15612                A declaration consisting solely of `class-key identifier ;' is
15613                either a redeclaration of the name in the current scope or a
15614                forward declaration of the identifier as a class name.  It
15615                introduces the name into the current scope.
15616
15617              We are in this situation precisely when the next token is a `;'.
15618
15619              An exception to the exception is that a `friend' declaration does
15620              *not* name a new type; i.e., given:
15621
15622                struct S { friend struct T; };
15623
15624              `T' is not a new type in the scope of `S'.
15625
15626              Also, `new struct S' or `sizeof (struct S)' never results in the
15627              definition of a new type; a new type can only be declared in a
15628              declaration context.  */
15629
15630           tag_scope ts;
15631           bool template_p;
15632
15633           if (is_friend)
15634             /* Friends have special name lookup rules.  */
15635             ts = ts_within_enclosing_non_class;
15636           else if (is_declaration
15637                    && cp_lexer_next_token_is (parser->lexer,
15638                                               CPP_SEMICOLON))
15639             /* This is a `class-key identifier ;' */
15640             ts = ts_current;
15641           else
15642             ts = ts_global;
15643
15644           template_p =
15645             (template_parm_lists_apply
15646              && (cp_parser_next_token_starts_class_definition_p (parser)
15647                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15648           /* An unqualified name was used to reference this type, so
15649              there were no qualifying templates.  */
15650           if (template_parm_lists_apply
15651               && !cp_parser_check_template_parameters (parser,
15652                                                        /*num_templates=*/0,
15653                                                        token->location,
15654                                                        /*declarator=*/NULL))
15655             return error_mark_node;
15656           type = xref_tag (tag_type, identifier, ts, template_p);
15657         }
15658     }
15659
15660   if (type == error_mark_node)
15661     return error_mark_node;
15662
15663   /* Allow attributes on forward declarations of classes.  */
15664   if (attributes)
15665     {
15666       if (TREE_CODE (type) == TYPENAME_TYPE)
15667         warning (OPT_Wattributes,
15668                  "attributes ignored on uninstantiated type");
15669       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15670                && ! processing_explicit_instantiation)
15671         warning (OPT_Wattributes,
15672                  "attributes ignored on template instantiation");
15673       else if (is_declaration && cp_parser_declares_only_class_p (parser))
15674         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15675       else
15676         warning (OPT_Wattributes,
15677                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15678     }
15679
15680   if (tag_type != enum_type)
15681     {
15682       /* Indicate whether this class was declared as a `class' or as a
15683          `struct'.  */
15684       if (TREE_CODE (type) == RECORD_TYPE)
15685         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15686       cp_parser_check_class_key (tag_type, type);
15687     }
15688
15689   /* A "<" cannot follow an elaborated type specifier.  If that
15690      happens, the user was probably trying to form a template-id.  */
15691   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15692                                            token->location);
15693
15694   return type;
15695 }
15696
15697 /* Parse an enum-specifier.
15698
15699    enum-specifier:
15700      enum-head { enumerator-list [opt] }
15701      enum-head { enumerator-list , } [C++0x]
15702
15703    enum-head:
15704      enum-key identifier [opt] enum-base [opt]
15705      enum-key nested-name-specifier identifier enum-base [opt]
15706
15707    enum-key:
15708      enum
15709      enum class   [C++0x]
15710      enum struct  [C++0x]
15711
15712    enum-base:   [C++0x]
15713      : type-specifier-seq
15714
15715    opaque-enum-specifier:
15716      enum-key identifier enum-base [opt] ;
15717
15718    GNU Extensions:
15719      enum-key attributes[opt] identifier [opt] enum-base [opt] 
15720        { enumerator-list [opt] }attributes[opt]
15721      enum-key attributes[opt] identifier [opt] enum-base [opt]
15722        { enumerator-list, }attributes[opt] [C++0x]
15723
15724    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15725    if the token stream isn't an enum-specifier after all.  */
15726
15727 static tree
15728 cp_parser_enum_specifier (cp_parser* parser)
15729 {
15730   tree identifier;
15731   tree type = NULL_TREE;
15732   tree prev_scope;
15733   tree nested_name_specifier = NULL_TREE;
15734   tree attributes;
15735   bool scoped_enum_p = false;
15736   bool has_underlying_type = false;
15737   bool nested_being_defined = false;
15738   bool new_value_list = false;
15739   bool is_new_type = false;
15740   bool is_anonymous = false;
15741   tree underlying_type = NULL_TREE;
15742   cp_token *type_start_token = NULL;
15743   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15744
15745   parser->colon_corrects_to_scope_p = false;
15746
15747   /* Parse tentatively so that we can back up if we don't find a
15748      enum-specifier.  */
15749   cp_parser_parse_tentatively (parser);
15750
15751   /* Caller guarantees that the current token is 'enum', an identifier
15752      possibly follows, and the token after that is an opening brace.
15753      If we don't have an identifier, fabricate an anonymous name for
15754      the enumeration being defined.  */
15755   cp_lexer_consume_token (parser->lexer);
15756
15757   /* Parse the "class" or "struct", which indicates a scoped
15758      enumeration type in C++0x.  */
15759   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15760       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15761     {
15762       if (cxx_dialect < cxx11)
15763         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15764
15765       /* Consume the `struct' or `class' token.  */
15766       cp_lexer_consume_token (parser->lexer);
15767
15768       scoped_enum_p = true;
15769     }
15770
15771   attributes = cp_parser_attributes_opt (parser);
15772
15773   /* Clear the qualification.  */
15774   parser->scope = NULL_TREE;
15775   parser->qualifying_scope = NULL_TREE;
15776   parser->object_scope = NULL_TREE;
15777
15778   /* Figure out in what scope the declaration is being placed.  */
15779   prev_scope = current_scope ();
15780
15781   type_start_token = cp_lexer_peek_token (parser->lexer);
15782
15783   push_deferring_access_checks (dk_no_check);
15784   nested_name_specifier
15785       = cp_parser_nested_name_specifier_opt (parser,
15786                                              /*typename_keyword_p=*/true,
15787                                              /*check_dependency_p=*/false,
15788                                              /*type_p=*/false,
15789                                              /*is_declaration=*/false);
15790
15791   if (nested_name_specifier)
15792     {
15793       tree name;
15794
15795       identifier = cp_parser_identifier (parser);
15796       name =  cp_parser_lookup_name (parser, identifier,
15797                                      enum_type,
15798                                      /*is_template=*/false,
15799                                      /*is_namespace=*/false,
15800                                      /*check_dependency=*/true,
15801                                      /*ambiguous_decls=*/NULL,
15802                                      input_location);
15803       if (name && name != error_mark_node)
15804         {
15805           type = TREE_TYPE (name);
15806           if (TREE_CODE (type) == TYPENAME_TYPE)
15807             {
15808               /* Are template enums allowed in ISO? */
15809               if (template_parm_scope_p ())
15810                 pedwarn (type_start_token->location, OPT_Wpedantic,
15811                          "%qD is an enumeration template", name);
15812               /* ignore a typename reference, for it will be solved by name
15813                  in start_enum.  */
15814               type = NULL_TREE;
15815             }
15816         }
15817       else if (nested_name_specifier == error_mark_node)
15818         /* We already issued an error.  */;
15819       else
15820         error_at (type_start_token->location,
15821                   "%qD is not an enumerator-name", identifier);
15822     }
15823   else
15824     {
15825       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15826         identifier = cp_parser_identifier (parser);
15827       else
15828         {
15829           identifier = make_anon_name ();
15830           is_anonymous = true;
15831           if (scoped_enum_p)
15832             error_at (type_start_token->location,
15833                       "anonymous scoped enum is not allowed");
15834         }
15835     }
15836   pop_deferring_access_checks ();
15837
15838   /* Check for the `:' that denotes a specified underlying type in C++0x.
15839      Note that a ':' could also indicate a bitfield width, however.  */
15840   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15841     {
15842       cp_decl_specifier_seq type_specifiers;
15843
15844       /* Consume the `:'.  */
15845       cp_lexer_consume_token (parser->lexer);
15846
15847       /* Parse the type-specifier-seq.  */
15848       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15849                                     /*is_trailing_return=*/false,
15850                                     &type_specifiers);
15851
15852       /* At this point this is surely not elaborated type specifier.  */
15853       if (!cp_parser_parse_definitely (parser))
15854         return NULL_TREE;
15855
15856       if (cxx_dialect < cxx11)
15857         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15858
15859       has_underlying_type = true;
15860
15861       /* If that didn't work, stop.  */
15862       if (type_specifiers.type != error_mark_node)
15863         {
15864           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15865                                             /*initialized=*/0, NULL);
15866           if (underlying_type == error_mark_node
15867               || check_for_bare_parameter_packs (underlying_type))
15868             underlying_type = NULL_TREE;
15869         }
15870     }
15871
15872   /* Look for the `{' but don't consume it yet.  */
15873   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15874     {
15875       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15876         {
15877           cp_parser_error (parser, "expected %<{%>");
15878           if (has_underlying_type)
15879             {
15880               type = NULL_TREE;
15881               goto out;
15882             }
15883         }
15884       /* An opaque-enum-specifier must have a ';' here.  */
15885       if ((scoped_enum_p || underlying_type)
15886           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15887         {
15888           cp_parser_error (parser, "expected %<;%> or %<{%>");
15889           if (has_underlying_type)
15890             {
15891               type = NULL_TREE;
15892               goto out;
15893             }
15894         }
15895     }
15896
15897   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15898     return NULL_TREE;
15899
15900   if (nested_name_specifier)
15901     {
15902       if (CLASS_TYPE_P (nested_name_specifier))
15903         {
15904           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15905           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15906           push_scope (nested_name_specifier);
15907         }
15908       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15909         {
15910           push_nested_namespace (nested_name_specifier);
15911         }
15912     }
15913
15914   /* Issue an error message if type-definitions are forbidden here.  */
15915   if (!cp_parser_check_type_definition (parser))
15916     type = error_mark_node;
15917   else
15918     /* Create the new type.  We do this before consuming the opening
15919        brace so the enum will be recorded as being on the line of its
15920        tag (or the 'enum' keyword, if there is no tag).  */
15921     type = start_enum (identifier, type, underlying_type,
15922                        scoped_enum_p, &is_new_type);
15923
15924   /* If the next token is not '{' it is an opaque-enum-specifier or an
15925      elaborated-type-specifier.  */
15926   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15927     {
15928       timevar_push (TV_PARSE_ENUM);
15929       if (nested_name_specifier
15930           && nested_name_specifier != error_mark_node)
15931         {
15932           /* The following catches invalid code such as:
15933              enum class S<int>::E { A, B, C }; */
15934           if (!processing_specialization
15935               && CLASS_TYPE_P (nested_name_specifier)
15936               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15937             error_at (type_start_token->location, "cannot add an enumerator "
15938                       "list to a template instantiation");
15939
15940           if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15941             {
15942               error_at (type_start_token->location,
15943                         "%<%T::%E%> has not been declared",
15944                         TYPE_CONTEXT (nested_name_specifier),
15945                         nested_name_specifier);
15946               type = error_mark_node;
15947             }
15948           /* If that scope does not contain the scope in which the
15949              class was originally declared, the program is invalid.  */
15950           else if (prev_scope && !is_ancestor (prev_scope,
15951                                                nested_name_specifier))
15952             {
15953               if (at_namespace_scope_p ())
15954                 error_at (type_start_token->location,
15955                           "declaration of %qD in namespace %qD which does not "
15956                           "enclose %qD",
15957                           type, prev_scope, nested_name_specifier);
15958               else
15959                 error_at (type_start_token->location,
15960                           "declaration of %qD in %qD which does not "
15961                           "enclose %qD",
15962                           type, prev_scope, nested_name_specifier);
15963               type = error_mark_node;
15964             }
15965         }
15966
15967       if (scoped_enum_p)
15968         begin_scope (sk_scoped_enum, type);
15969
15970       /* Consume the opening brace.  */
15971       cp_lexer_consume_token (parser->lexer);
15972
15973       if (type == error_mark_node)
15974         ; /* Nothing to add */
15975       else if (OPAQUE_ENUM_P (type)
15976                || (cxx_dialect > cxx98 && processing_specialization))
15977         {
15978           new_value_list = true;
15979           SET_OPAQUE_ENUM_P (type, false);
15980           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15981         }
15982       else
15983         {
15984           error_at (type_start_token->location,
15985                     "multiple definition of %q#T", type);
15986           inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15987                   "previous definition here");
15988           type = error_mark_node;
15989         }
15990
15991       if (type == error_mark_node)
15992         cp_parser_skip_to_end_of_block_or_statement (parser);
15993       /* If the next token is not '}', then there are some enumerators.  */
15994       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15995         {
15996           if (is_anonymous && !scoped_enum_p)
15997             pedwarn (type_start_token->location, OPT_Wpedantic,
15998                      "ISO C++ forbids empty anonymous enum");
15999         }
16000       else
16001         cp_parser_enumerator_list (parser, type);
16002
16003       /* Consume the final '}'.  */
16004       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16005
16006       if (scoped_enum_p)
16007         finish_scope ();
16008       timevar_pop (TV_PARSE_ENUM);
16009     }
16010   else
16011     {
16012       /* If a ';' follows, then it is an opaque-enum-specifier
16013         and additional restrictions apply.  */
16014       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16015         {
16016           if (is_anonymous)
16017             error_at (type_start_token->location,
16018                       "opaque-enum-specifier without name");
16019           else if (nested_name_specifier)
16020             error_at (type_start_token->location,
16021                       "opaque-enum-specifier must use a simple identifier");
16022         }
16023     }
16024
16025   /* Look for trailing attributes to apply to this enumeration, and
16026      apply them if appropriate.  */
16027   if (cp_parser_allow_gnu_extensions_p (parser))
16028     {
16029       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16030       trailing_attr = chainon (trailing_attr, attributes);
16031       cplus_decl_attributes (&type,
16032                              trailing_attr,
16033                              (int) ATTR_FLAG_TYPE_IN_PLACE);
16034     }
16035
16036   /* Finish up the enumeration.  */
16037   if (type != error_mark_node)
16038     {
16039       if (new_value_list)
16040         finish_enum_value_list (type);
16041       if (is_new_type)
16042         finish_enum (type);
16043     }
16044
16045   if (nested_name_specifier)
16046     {
16047       if (CLASS_TYPE_P (nested_name_specifier))
16048         {
16049           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16050           pop_scope (nested_name_specifier);
16051         }
16052       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16053         {
16054           pop_nested_namespace (nested_name_specifier);
16055         }
16056     }
16057  out:
16058   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16059   return type;
16060 }
16061
16062 /* Parse an enumerator-list.  The enumerators all have the indicated
16063    TYPE.
16064
16065    enumerator-list:
16066      enumerator-definition
16067      enumerator-list , enumerator-definition  */
16068
16069 static void
16070 cp_parser_enumerator_list (cp_parser* parser, tree type)
16071 {
16072   while (true)
16073     {
16074       /* Parse an enumerator-definition.  */
16075       cp_parser_enumerator_definition (parser, type);
16076
16077       /* If the next token is not a ',', we've reached the end of
16078          the list.  */
16079       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16080         break;
16081       /* Otherwise, consume the `,' and keep going.  */
16082       cp_lexer_consume_token (parser->lexer);
16083       /* If the next token is a `}', there is a trailing comma.  */
16084       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16085         {
16086           if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16087             pedwarn (input_location, OPT_Wpedantic,
16088                      "comma at end of enumerator list");
16089           break;
16090         }
16091     }
16092 }
16093
16094 /* Parse an enumerator-definition.  The enumerator has the indicated
16095    TYPE.
16096
16097    enumerator-definition:
16098      enumerator
16099      enumerator = constant-expression
16100
16101    enumerator:
16102      identifier  */
16103
16104 static void
16105 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16106 {
16107   tree identifier;
16108   tree value;
16109   location_t loc;
16110
16111   /* Save the input location because we are interested in the location
16112      of the identifier and not the location of the explicit value.  */
16113   loc = cp_lexer_peek_token (parser->lexer)->location;
16114
16115   /* Look for the identifier.  */
16116   identifier = cp_parser_identifier (parser);
16117   if (identifier == error_mark_node)
16118     return;
16119
16120   /* If the next token is an '=', then there is an explicit value.  */
16121   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16122     {
16123       /* Consume the `=' token.  */
16124       cp_lexer_consume_token (parser->lexer);
16125       /* Parse the value.  */
16126       value = cp_parser_constant_expression (parser);
16127     }
16128   else
16129     value = NULL_TREE;
16130
16131   /* If we are processing a template, make sure the initializer of the
16132      enumerator doesn't contain any bare template parameter pack.  */
16133   if (check_for_bare_parameter_packs (value))
16134     value = error_mark_node;
16135
16136   /* Create the enumerator.  */
16137   build_enumerator (identifier, value, type, loc);
16138 }
16139
16140 /* Parse a namespace-name.
16141
16142    namespace-name:
16143      original-namespace-name
16144      namespace-alias
16145
16146    Returns the NAMESPACE_DECL for the namespace.  */
16147
16148 static tree
16149 cp_parser_namespace_name (cp_parser* parser)
16150 {
16151   tree identifier;
16152   tree namespace_decl;
16153
16154   cp_token *token = cp_lexer_peek_token (parser->lexer);
16155
16156   /* Get the name of the namespace.  */
16157   identifier = cp_parser_identifier (parser);
16158   if (identifier == error_mark_node)
16159     return error_mark_node;
16160
16161   /* Look up the identifier in the currently active scope.  Look only
16162      for namespaces, due to:
16163
16164        [basic.lookup.udir]
16165
16166        When looking up a namespace-name in a using-directive or alias
16167        definition, only namespace names are considered.
16168
16169      And:
16170
16171        [basic.lookup.qual]
16172
16173        During the lookup of a name preceding the :: scope resolution
16174        operator, object, function, and enumerator names are ignored.
16175
16176      (Note that cp_parser_qualifying_entity only calls this
16177      function if the token after the name is the scope resolution
16178      operator.)  */
16179   namespace_decl = cp_parser_lookup_name (parser, identifier,
16180                                           none_type,
16181                                           /*is_template=*/false,
16182                                           /*is_namespace=*/true,
16183                                           /*check_dependency=*/true,
16184                                           /*ambiguous_decls=*/NULL,
16185                                           token->location);
16186   /* If it's not a namespace, issue an error.  */
16187   if (namespace_decl == error_mark_node
16188       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16189     {
16190       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16191         error_at (token->location, "%qD is not a namespace-name", identifier);
16192       cp_parser_error (parser, "expected namespace-name");
16193       namespace_decl = error_mark_node;
16194     }
16195
16196   return namespace_decl;
16197 }
16198
16199 /* Parse a namespace-definition.
16200
16201    namespace-definition:
16202      named-namespace-definition
16203      unnamed-namespace-definition
16204
16205    named-namespace-definition:
16206      original-namespace-definition
16207      extension-namespace-definition
16208
16209    original-namespace-definition:
16210      namespace identifier { namespace-body }
16211
16212    extension-namespace-definition:
16213      namespace original-namespace-name { namespace-body }
16214
16215    unnamed-namespace-definition:
16216      namespace { namespace-body } */
16217
16218 static void
16219 cp_parser_namespace_definition (cp_parser* parser)
16220 {
16221   tree identifier, attribs;
16222   bool has_visibility;
16223   bool is_inline;
16224
16225   cp_ensure_no_omp_declare_simd (parser);
16226   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16227     {
16228       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16229       is_inline = true;
16230       cp_lexer_consume_token (parser->lexer);
16231     }
16232   else
16233     is_inline = false;
16234
16235   /* Look for the `namespace' keyword.  */
16236   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16237
16238   /* Get the name of the namespace.  We do not attempt to distinguish
16239      between an original-namespace-definition and an
16240      extension-namespace-definition at this point.  The semantic
16241      analysis routines are responsible for that.  */
16242   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16243     identifier = cp_parser_identifier (parser);
16244   else
16245     identifier = NULL_TREE;
16246
16247   /* Parse any specified attributes.  */
16248   attribs = cp_parser_attributes_opt (parser);
16249
16250   /* Look for the `{' to start the namespace.  */
16251   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16252   /* Start the namespace.  */
16253   push_namespace (identifier);
16254
16255   /* "inline namespace" is equivalent to a stub namespace definition
16256      followed by a strong using directive.  */
16257   if (is_inline)
16258     {
16259       tree name_space = current_namespace;
16260       /* Set up namespace association.  */
16261       DECL_NAMESPACE_ASSOCIATIONS (name_space)
16262         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16263                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
16264       /* Import the contents of the inline namespace.  */
16265       pop_namespace ();
16266       do_using_directive (name_space);
16267       push_namespace (identifier);
16268     }
16269
16270   has_visibility = handle_namespace_attrs (current_namespace, attribs);
16271
16272   /* Parse the body of the namespace.  */
16273   cp_parser_namespace_body (parser);
16274
16275   if (has_visibility)
16276     pop_visibility (1);
16277
16278   /* Finish the namespace.  */
16279   pop_namespace ();
16280   /* Look for the final `}'.  */
16281   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16282 }
16283
16284 /* Parse a namespace-body.
16285
16286    namespace-body:
16287      declaration-seq [opt]  */
16288
16289 static void
16290 cp_parser_namespace_body (cp_parser* parser)
16291 {
16292   cp_parser_declaration_seq_opt (parser);
16293 }
16294
16295 /* Parse a namespace-alias-definition.
16296
16297    namespace-alias-definition:
16298      namespace identifier = qualified-namespace-specifier ;  */
16299
16300 static void
16301 cp_parser_namespace_alias_definition (cp_parser* parser)
16302 {
16303   tree identifier;
16304   tree namespace_specifier;
16305
16306   cp_token *token = cp_lexer_peek_token (parser->lexer);
16307
16308   /* Look for the `namespace' keyword.  */
16309   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16310   /* Look for the identifier.  */
16311   identifier = cp_parser_identifier (parser);
16312   if (identifier == error_mark_node)
16313     return;
16314   /* Look for the `=' token.  */
16315   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16316       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
16317     {
16318       error_at (token->location, "%<namespace%> definition is not allowed here");
16319       /* Skip the definition.  */
16320       cp_lexer_consume_token (parser->lexer);
16321       if (cp_parser_skip_to_closing_brace (parser))
16322         cp_lexer_consume_token (parser->lexer);
16323       return;
16324     }
16325   cp_parser_require (parser, CPP_EQ, RT_EQ);
16326   /* Look for the qualified-namespace-specifier.  */
16327   namespace_specifier
16328     = cp_parser_qualified_namespace_specifier (parser);
16329   /* Look for the `;' token.  */
16330   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16331
16332   /* Register the alias in the symbol table.  */
16333   do_namespace_alias (identifier, namespace_specifier);
16334 }
16335
16336 /* Parse a qualified-namespace-specifier.
16337
16338    qualified-namespace-specifier:
16339      :: [opt] nested-name-specifier [opt] namespace-name
16340
16341    Returns a NAMESPACE_DECL corresponding to the specified
16342    namespace.  */
16343
16344 static tree
16345 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16346 {
16347   /* Look for the optional `::'.  */
16348   cp_parser_global_scope_opt (parser,
16349                               /*current_scope_valid_p=*/false);
16350
16351   /* Look for the optional nested-name-specifier.  */
16352   cp_parser_nested_name_specifier_opt (parser,
16353                                        /*typename_keyword_p=*/false,
16354                                        /*check_dependency_p=*/true,
16355                                        /*type_p=*/false,
16356                                        /*is_declaration=*/true);
16357
16358   return cp_parser_namespace_name (parser);
16359 }
16360
16361 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16362    access declaration.
16363
16364    using-declaration:
16365      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16366      using :: unqualified-id ;  
16367
16368    access-declaration:
16369      qualified-id ;  
16370
16371    */
16372
16373 static bool
16374 cp_parser_using_declaration (cp_parser* parser, 
16375                              bool access_declaration_p)
16376 {
16377   cp_token *token;
16378   bool typename_p = false;
16379   bool global_scope_p;
16380   tree decl;
16381   tree identifier;
16382   tree qscope;
16383   int oldcount = errorcount;
16384   cp_token *diag_token = NULL;
16385
16386   if (access_declaration_p)
16387     {
16388       diag_token = cp_lexer_peek_token (parser->lexer);
16389       cp_parser_parse_tentatively (parser);
16390     }
16391   else
16392     {
16393       /* Look for the `using' keyword.  */
16394       cp_parser_require_keyword (parser, RID_USING, RT_USING);
16395       
16396       /* Peek at the next token.  */
16397       token = cp_lexer_peek_token (parser->lexer);
16398       /* See if it's `typename'.  */
16399       if (token->keyword == RID_TYPENAME)
16400         {
16401           /* Remember that we've seen it.  */
16402           typename_p = true;
16403           /* Consume the `typename' token.  */
16404           cp_lexer_consume_token (parser->lexer);
16405         }
16406     }
16407
16408   /* Look for the optional global scope qualification.  */
16409   global_scope_p
16410     = (cp_parser_global_scope_opt (parser,
16411                                    /*current_scope_valid_p=*/false)
16412        != NULL_TREE);
16413
16414   /* If we saw `typename', or didn't see `::', then there must be a
16415      nested-name-specifier present.  */
16416   if (typename_p || !global_scope_p)
16417     {
16418       qscope = cp_parser_nested_name_specifier (parser, typename_p,
16419                                                 /*check_dependency_p=*/true,
16420                                                 /*type_p=*/false,
16421                                                 /*is_declaration=*/true);
16422       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16423         {
16424           cp_parser_skip_to_end_of_block_or_statement (parser);
16425           return false;
16426         }
16427     }
16428   /* Otherwise, we could be in either of the two productions.  In that
16429      case, treat the nested-name-specifier as optional.  */
16430   else
16431     qscope = cp_parser_nested_name_specifier_opt (parser,
16432                                                   /*typename_keyword_p=*/false,
16433                                                   /*check_dependency_p=*/true,
16434                                                   /*type_p=*/false,
16435                                                   /*is_declaration=*/true);
16436   if (!qscope)
16437     qscope = global_namespace;
16438   else if (UNSCOPED_ENUM_P (qscope))
16439     qscope = CP_TYPE_CONTEXT (qscope);
16440
16441   if (access_declaration_p && cp_parser_error_occurred (parser))
16442     /* Something has already gone wrong; there's no need to parse
16443        further.  Since an error has occurred, the return value of
16444        cp_parser_parse_definitely will be false, as required.  */
16445     return cp_parser_parse_definitely (parser);
16446
16447   token = cp_lexer_peek_token (parser->lexer);
16448   /* Parse the unqualified-id.  */
16449   identifier = cp_parser_unqualified_id (parser,
16450                                          /*template_keyword_p=*/false,
16451                                          /*check_dependency_p=*/true,
16452                                          /*declarator_p=*/true,
16453                                          /*optional_p=*/false);
16454
16455   if (access_declaration_p)
16456     {
16457       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16458         cp_parser_simulate_error (parser);
16459       if (!cp_parser_parse_definitely (parser))
16460         return false;
16461     }
16462
16463   /* The function we call to handle a using-declaration is different
16464      depending on what scope we are in.  */
16465   if (qscope == error_mark_node || identifier == error_mark_node)
16466     ;
16467   else if (!identifier_p (identifier)
16468            && TREE_CODE (identifier) != BIT_NOT_EXPR)
16469     /* [namespace.udecl]
16470
16471        A using declaration shall not name a template-id.  */
16472     error_at (token->location,
16473               "a template-id may not appear in a using-declaration");
16474   else
16475     {
16476       if (at_class_scope_p ())
16477         {
16478           /* Create the USING_DECL.  */
16479           decl = do_class_using_decl (parser->scope, identifier);
16480
16481           if (decl && typename_p)
16482             USING_DECL_TYPENAME_P (decl) = 1;
16483
16484           if (check_for_bare_parameter_packs (decl))
16485             {
16486               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16487               return false;
16488             }
16489           else
16490             /* Add it to the list of members in this class.  */
16491             finish_member_declaration (decl);
16492         }
16493       else
16494         {
16495           decl = cp_parser_lookup_name_simple (parser,
16496                                                identifier,
16497                                                token->location);
16498           if (decl == error_mark_node)
16499             cp_parser_name_lookup_error (parser, identifier,
16500                                          decl, NLE_NULL,
16501                                          token->location);
16502           else if (check_for_bare_parameter_packs (decl))
16503             {
16504               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16505               return false;
16506             }
16507           else if (!at_namespace_scope_p ())
16508             do_local_using_decl (decl, qscope, identifier);
16509           else
16510             do_toplevel_using_decl (decl, qscope, identifier);
16511         }
16512     }
16513
16514   /* Look for the final `;'.  */
16515   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16516
16517   if (access_declaration_p && errorcount == oldcount)
16518     warning_at (diag_token->location, OPT_Wdeprecated,
16519                 "access declarations are deprecated "
16520                 "in favour of using-declarations; "
16521                 "suggestion: add the %<using%> keyword");
16522
16523   return true;
16524 }
16525
16526 /* Parse an alias-declaration.
16527
16528    alias-declaration:
16529      using identifier attribute-specifier-seq [opt] = type-id  */
16530
16531 static tree
16532 cp_parser_alias_declaration (cp_parser* parser)
16533 {
16534   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16535   location_t id_location;
16536   cp_declarator *declarator;
16537   cp_decl_specifier_seq decl_specs;
16538   bool member_p;
16539   const char *saved_message = NULL;
16540
16541   /* Look for the `using' keyword.  */
16542   cp_token *using_token
16543     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16544   if (using_token == NULL)
16545     return error_mark_node;
16546
16547   id_location = cp_lexer_peek_token (parser->lexer)->location;
16548   id = cp_parser_identifier (parser);
16549   if (id == error_mark_node)
16550     return error_mark_node;
16551
16552   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16553   attributes = cp_parser_attributes_opt (parser);
16554   if (attributes == error_mark_node)
16555     return error_mark_node;
16556
16557   cp_parser_require (parser, CPP_EQ, RT_EQ);
16558
16559   if (cp_parser_error_occurred (parser))
16560     return error_mark_node;
16561
16562   cp_parser_commit_to_tentative_parse (parser);
16563
16564   /* Now we are going to parse the type-id of the declaration.  */
16565
16566   /*
16567     [dcl.type]/3 says:
16568
16569         "A type-specifier-seq shall not define a class or enumeration
16570          unless it appears in the type-id of an alias-declaration (7.1.3) that
16571          is not the declaration of a template-declaration."
16572
16573     In other words, if we currently are in an alias template, the
16574     type-id should not define a type.
16575
16576     So let's set parser->type_definition_forbidden_message in that
16577     case; cp_parser_check_type_definition (called by
16578     cp_parser_class_specifier) will then emit an error if a type is
16579     defined in the type-id.  */
16580   if (parser->num_template_parameter_lists)
16581     {
16582       saved_message = parser->type_definition_forbidden_message;
16583       parser->type_definition_forbidden_message =
16584         G_("types may not be defined in alias template declarations");
16585     }
16586
16587   type = cp_parser_type_id (parser);
16588
16589   /* Restore the error message if need be.  */
16590   if (parser->num_template_parameter_lists)
16591     parser->type_definition_forbidden_message = saved_message;
16592
16593   if (type == error_mark_node
16594       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16595     {
16596       cp_parser_skip_to_end_of_block_or_statement (parser);
16597       return error_mark_node;
16598     }
16599
16600   /* A typedef-name can also be introduced by an alias-declaration. The
16601      identifier following the using keyword becomes a typedef-name. It has
16602      the same semantics as if it were introduced by the typedef
16603      specifier. In particular, it does not define a new type and it shall
16604      not appear in the type-id.  */
16605
16606   clear_decl_specs (&decl_specs);
16607   decl_specs.type = type;
16608   if (attributes != NULL_TREE)
16609     {
16610       decl_specs.attributes = attributes;
16611       set_and_check_decl_spec_loc (&decl_specs,
16612                                    ds_attribute,
16613                                    attrs_token);
16614     }
16615   set_and_check_decl_spec_loc (&decl_specs,
16616                                ds_typedef,
16617                                using_token);
16618   set_and_check_decl_spec_loc (&decl_specs,
16619                                ds_alias,
16620                                using_token);
16621
16622   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16623   declarator->id_loc = id_location;
16624
16625   member_p = at_class_scope_p ();
16626   if (member_p)
16627     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16628                       NULL_TREE, attributes);
16629   else
16630     decl = start_decl (declarator, &decl_specs, 0,
16631                        attributes, NULL_TREE, &pushed_scope);
16632   if (decl == error_mark_node)
16633     return decl;
16634
16635   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16636
16637   if (pushed_scope)
16638     pop_scope (pushed_scope);
16639
16640   /* If decl is a template, return its TEMPLATE_DECL so that it gets
16641      added into the symbol table; otherwise, return the TYPE_DECL.  */
16642   if (DECL_LANG_SPECIFIC (decl)
16643       && DECL_TEMPLATE_INFO (decl)
16644       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16645     {
16646       decl = DECL_TI_TEMPLATE (decl);
16647       if (member_p)
16648         check_member_template (decl);
16649     }
16650
16651   return decl;
16652 }
16653
16654 /* Parse a using-directive.
16655
16656    using-directive:
16657      using namespace :: [opt] nested-name-specifier [opt]
16658        namespace-name ;  */
16659
16660 static void
16661 cp_parser_using_directive (cp_parser* parser)
16662 {
16663   tree namespace_decl;
16664   tree attribs;
16665
16666   /* Look for the `using' keyword.  */
16667   cp_parser_require_keyword (parser, RID_USING, RT_USING);
16668   /* And the `namespace' keyword.  */
16669   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16670   /* Look for the optional `::' operator.  */
16671   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16672   /* And the optional nested-name-specifier.  */
16673   cp_parser_nested_name_specifier_opt (parser,
16674                                        /*typename_keyword_p=*/false,
16675                                        /*check_dependency_p=*/true,
16676                                        /*type_p=*/false,
16677                                        /*is_declaration=*/true);
16678   /* Get the namespace being used.  */
16679   namespace_decl = cp_parser_namespace_name (parser);
16680   /* And any specified attributes.  */
16681   attribs = cp_parser_attributes_opt (parser);
16682   /* Update the symbol table.  */
16683   parse_using_directive (namespace_decl, attribs);
16684   /* Look for the final `;'.  */
16685   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16686 }
16687
16688 /* Parse an asm-definition.
16689
16690    asm-definition:
16691      asm ( string-literal ) ;
16692
16693    GNU Extension:
16694
16695    asm-definition:
16696      asm volatile [opt] ( string-literal ) ;
16697      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16698      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16699                           : asm-operand-list [opt] ) ;
16700      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16701                           : asm-operand-list [opt]
16702                           : asm-clobber-list [opt] ) ;
16703      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16704                                : asm-clobber-list [opt]
16705                                : asm-goto-list ) ;  */
16706
16707 static void
16708 cp_parser_asm_definition (cp_parser* parser)
16709 {
16710   tree string;
16711   tree outputs = NULL_TREE;
16712   tree inputs = NULL_TREE;
16713   tree clobbers = NULL_TREE;
16714   tree labels = NULL_TREE;
16715   tree asm_stmt;
16716   bool volatile_p = false;
16717   bool extended_p = false;
16718   bool invalid_inputs_p = false;
16719   bool invalid_outputs_p = false;
16720   bool goto_p = false;
16721   required_token missing = RT_NONE;
16722
16723   /* Look for the `asm' keyword.  */
16724   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16725
16726   if (parser->in_function_body
16727       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16728     {
16729       error ("%<asm%> in %<constexpr%> function");
16730       cp_function_chain->invalid_constexpr = true;
16731     }
16732
16733   /* See if the next token is `volatile'.  */
16734   if (cp_parser_allow_gnu_extensions_p (parser)
16735       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16736     {
16737       /* Remember that we saw the `volatile' keyword.  */
16738       volatile_p = true;
16739       /* Consume the token.  */
16740       cp_lexer_consume_token (parser->lexer);
16741     }
16742   if (cp_parser_allow_gnu_extensions_p (parser)
16743       && parser->in_function_body
16744       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16745     {
16746       /* Remember that we saw the `goto' keyword.  */
16747       goto_p = true;
16748       /* Consume the token.  */
16749       cp_lexer_consume_token (parser->lexer);
16750     }
16751   /* Look for the opening `('.  */
16752   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16753     return;
16754   /* Look for the string.  */
16755   string = cp_parser_string_literal (parser, false, false);
16756   if (string == error_mark_node)
16757     {
16758       cp_parser_skip_to_closing_parenthesis (parser, true, false,
16759                                              /*consume_paren=*/true);
16760       return;
16761     }
16762
16763   /* If we're allowing GNU extensions, check for the extended assembly
16764      syntax.  Unfortunately, the `:' tokens need not be separated by
16765      a space in C, and so, for compatibility, we tolerate that here
16766      too.  Doing that means that we have to treat the `::' operator as
16767      two `:' tokens.  */
16768   if (cp_parser_allow_gnu_extensions_p (parser)
16769       && parser->in_function_body
16770       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16771           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16772     {
16773       bool inputs_p = false;
16774       bool clobbers_p = false;
16775       bool labels_p = false;
16776
16777       /* The extended syntax was used.  */
16778       extended_p = true;
16779
16780       /* Look for outputs.  */
16781       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16782         {
16783           /* Consume the `:'.  */
16784           cp_lexer_consume_token (parser->lexer);
16785           /* Parse the output-operands.  */
16786           if (cp_lexer_next_token_is_not (parser->lexer,
16787                                           CPP_COLON)
16788               && cp_lexer_next_token_is_not (parser->lexer,
16789                                              CPP_SCOPE)
16790               && cp_lexer_next_token_is_not (parser->lexer,
16791                                              CPP_CLOSE_PAREN)
16792               && !goto_p)
16793             outputs = cp_parser_asm_operand_list (parser);
16794
16795             if (outputs == error_mark_node)
16796               invalid_outputs_p = true;
16797         }
16798       /* If the next token is `::', there are no outputs, and the
16799          next token is the beginning of the inputs.  */
16800       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16801         /* The inputs are coming next.  */
16802         inputs_p = true;
16803
16804       /* Look for inputs.  */
16805       if (inputs_p
16806           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16807         {
16808           /* Consume the `:' or `::'.  */
16809           cp_lexer_consume_token (parser->lexer);
16810           /* Parse the output-operands.  */
16811           if (cp_lexer_next_token_is_not (parser->lexer,
16812                                           CPP_COLON)
16813               && cp_lexer_next_token_is_not (parser->lexer,
16814                                              CPP_SCOPE)
16815               && cp_lexer_next_token_is_not (parser->lexer,
16816                                              CPP_CLOSE_PAREN))
16817             inputs = cp_parser_asm_operand_list (parser);
16818
16819             if (inputs == error_mark_node)
16820               invalid_inputs_p = true;
16821         }
16822       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16823         /* The clobbers are coming next.  */
16824         clobbers_p = true;
16825
16826       /* Look for clobbers.  */
16827       if (clobbers_p
16828           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16829         {
16830           clobbers_p = true;
16831           /* Consume the `:' or `::'.  */
16832           cp_lexer_consume_token (parser->lexer);
16833           /* Parse the clobbers.  */
16834           if (cp_lexer_next_token_is_not (parser->lexer,
16835                                           CPP_COLON)
16836               && cp_lexer_next_token_is_not (parser->lexer,
16837                                              CPP_CLOSE_PAREN))
16838             clobbers = cp_parser_asm_clobber_list (parser);
16839         }
16840       else if (goto_p
16841                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16842         /* The labels are coming next.  */
16843         labels_p = true;
16844
16845       /* Look for labels.  */
16846       if (labels_p
16847           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16848         {
16849           labels_p = true;
16850           /* Consume the `:' or `::'.  */
16851           cp_lexer_consume_token (parser->lexer);
16852           /* Parse the labels.  */
16853           labels = cp_parser_asm_label_list (parser);
16854         }
16855
16856       if (goto_p && !labels_p)
16857         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16858     }
16859   else if (goto_p)
16860     missing = RT_COLON_SCOPE;
16861
16862   /* Look for the closing `)'.  */
16863   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16864                           missing ? missing : RT_CLOSE_PAREN))
16865     cp_parser_skip_to_closing_parenthesis (parser, true, false,
16866                                            /*consume_paren=*/true);
16867   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16868
16869   if (!invalid_inputs_p && !invalid_outputs_p)
16870     {
16871       /* Create the ASM_EXPR.  */
16872       if (parser->in_function_body)
16873         {
16874           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16875                                       inputs, clobbers, labels);
16876           /* If the extended syntax was not used, mark the ASM_EXPR.  */
16877           if (!extended_p)
16878             {
16879               tree temp = asm_stmt;
16880               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16881                 temp = TREE_OPERAND (temp, 0);
16882
16883               ASM_INPUT_P (temp) = 1;
16884             }
16885         }
16886       else
16887         symtab->finalize_toplevel_asm (string);
16888     }
16889 }
16890
16891 /* Declarators [gram.dcl.decl] */
16892
16893 /* Parse an init-declarator.
16894
16895    init-declarator:
16896      declarator initializer [opt]
16897
16898    GNU Extension:
16899
16900    init-declarator:
16901      declarator asm-specification [opt] attributes [opt] initializer [opt]
16902
16903    function-definition:
16904      decl-specifier-seq [opt] declarator ctor-initializer [opt]
16905        function-body
16906      decl-specifier-seq [opt] declarator function-try-block
16907
16908    GNU Extension:
16909
16910    function-definition:
16911      __extension__ function-definition
16912
16913    TM Extension:
16914
16915    function-definition:
16916      decl-specifier-seq [opt] declarator function-transaction-block
16917
16918    The DECL_SPECIFIERS apply to this declarator.  Returns a
16919    representation of the entity declared.  If MEMBER_P is TRUE, then
16920    this declarator appears in a class scope.  The new DECL created by
16921    this declarator is returned.
16922
16923    The CHECKS are access checks that should be performed once we know
16924    what entity is being declared (and, therefore, what classes have
16925    befriended it).
16926
16927    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16928    for a function-definition here as well.  If the declarator is a
16929    declarator for a function-definition, *FUNCTION_DEFINITION_P will
16930    be TRUE upon return.  By that point, the function-definition will
16931    have been completely parsed.
16932
16933    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16934    is FALSE.
16935
16936    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16937    parsed declaration if it is an uninitialized single declarator not followed
16938    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16939    if present, will not be consumed.  If returned, this declarator will be
16940    created with SD_INITIALIZED but will not call cp_finish_decl.
16941
16942    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16943    and there is an initializer, the pointed location_t is set to the
16944    location of the '=' or `(', or '{' in C++11 token introducing the
16945    initializer.  */
16946
16947 static tree
16948 cp_parser_init_declarator (cp_parser* parser,
16949                            cp_decl_specifier_seq *decl_specifiers,
16950                            vec<deferred_access_check, va_gc> *checks,
16951                            bool function_definition_allowed_p,
16952                            bool member_p,
16953                            int declares_class_or_enum,
16954                            bool* function_definition_p,
16955                            tree* maybe_range_for_decl,
16956                            location_t* init_loc)
16957 {
16958   cp_token *token = NULL, *asm_spec_start_token = NULL,
16959            *attributes_start_token = NULL;
16960   cp_declarator *declarator;
16961   tree prefix_attributes;
16962   tree attributes = NULL;
16963   tree asm_specification;
16964   tree initializer;
16965   tree decl = NULL_TREE;
16966   tree scope;
16967   int is_initialized;
16968   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
16969      initialized with "= ..", CPP_OPEN_PAREN if initialized with
16970      "(...)".  */
16971   enum cpp_ttype initialization_kind;
16972   bool is_direct_init = false;
16973   bool is_non_constant_init;
16974   int ctor_dtor_or_conv_p;
16975   bool friend_p = cp_parser_friend_p (decl_specifiers);
16976   tree pushed_scope = NULL_TREE;
16977   bool range_for_decl_p = false;
16978   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16979   location_t tmp_init_loc = UNKNOWN_LOCATION;
16980
16981   /* Gather the attributes that were provided with the
16982      decl-specifiers.  */
16983   prefix_attributes = decl_specifiers->attributes;
16984
16985   /* Assume that this is not the declarator for a function
16986      definition.  */
16987   if (function_definition_p)
16988     *function_definition_p = false;
16989
16990   /* Default arguments are only permitted for function parameters.  */
16991   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16992     parser->default_arg_ok_p = false;
16993
16994   /* Defer access checks while parsing the declarator; we cannot know
16995      what names are accessible until we know what is being
16996      declared.  */
16997   resume_deferring_access_checks ();
16998
16999   /* Parse the declarator.  */
17000   token = cp_lexer_peek_token (parser->lexer);
17001   declarator
17002     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17003                             &ctor_dtor_or_conv_p,
17004                             /*parenthesized_p=*/NULL,
17005                             member_p, friend_p);
17006   /* Gather up the deferred checks.  */
17007   stop_deferring_access_checks ();
17008
17009   parser->default_arg_ok_p = saved_default_arg_ok_p;
17010
17011   /* If the DECLARATOR was erroneous, there's no need to go
17012      further.  */
17013   if (declarator == cp_error_declarator)
17014     return error_mark_node;
17015
17016   /* Check that the number of template-parameter-lists is OK.  */
17017   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17018                                                        token->location))
17019     return error_mark_node;
17020
17021   if (declares_class_or_enum & 2)
17022     cp_parser_check_for_definition_in_return_type (declarator,
17023                                                    decl_specifiers->type,
17024                                                    decl_specifiers->locations[ds_type_spec]);
17025
17026   /* Figure out what scope the entity declared by the DECLARATOR is
17027      located in.  `grokdeclarator' sometimes changes the scope, so
17028      we compute it now.  */
17029   scope = get_scope_of_declarator (declarator);
17030
17031   /* Perform any lookups in the declared type which were thought to be
17032      dependent, but are not in the scope of the declarator.  */
17033   decl_specifiers->type
17034     = maybe_update_decl_type (decl_specifiers->type, scope);
17035
17036   /* If we're allowing GNU extensions, look for an
17037      asm-specification.  */
17038   if (cp_parser_allow_gnu_extensions_p (parser))
17039     {
17040       /* Look for an asm-specification.  */
17041       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17042       asm_specification = cp_parser_asm_specification_opt (parser);
17043     }
17044   else
17045     asm_specification = NULL_TREE;
17046
17047   /* Look for attributes.  */
17048   attributes_start_token = cp_lexer_peek_token (parser->lexer);
17049   attributes = cp_parser_attributes_opt (parser);
17050
17051   /* Peek at the next token.  */
17052   token = cp_lexer_peek_token (parser->lexer);
17053
17054   bool bogus_implicit_tmpl = false;
17055
17056   if (function_declarator_p (declarator))
17057     {
17058       /* Check to see if the token indicates the start of a
17059          function-definition.  */
17060       if (cp_parser_token_starts_function_definition_p (token))
17061         {
17062           if (!function_definition_allowed_p)
17063             {
17064               /* If a function-definition should not appear here, issue an
17065                  error message.  */
17066               cp_parser_error (parser,
17067                                "a function-definition is not allowed here");
17068               return error_mark_node;
17069             }
17070
17071           location_t func_brace_location
17072             = cp_lexer_peek_token (parser->lexer)->location;
17073
17074           /* Neither attributes nor an asm-specification are allowed
17075              on a function-definition.  */
17076           if (asm_specification)
17077             error_at (asm_spec_start_token->location,
17078                       "an asm-specification is not allowed "
17079                       "on a function-definition");
17080           if (attributes)
17081             error_at (attributes_start_token->location,
17082                       "attributes are not allowed "
17083                       "on a function-definition");
17084           /* This is a function-definition.  */
17085           *function_definition_p = true;
17086
17087           /* Parse the function definition.  */
17088           if (member_p)
17089             decl = cp_parser_save_member_function_body (parser,
17090                                                         decl_specifiers,
17091                                                         declarator,
17092                                                         prefix_attributes);
17093           else
17094             decl =
17095               (cp_parser_function_definition_from_specifiers_and_declarator
17096                (parser, decl_specifiers, prefix_attributes, declarator));
17097
17098           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17099             {
17100               /* This is where the prologue starts...  */
17101               DECL_STRUCT_FUNCTION (decl)->function_start_locus
17102                 = func_brace_location;
17103             }
17104
17105           return decl;
17106         }
17107     }
17108   else if (parser->fully_implicit_function_template_p)
17109     {
17110       /* A non-template declaration involving a function parameter list
17111          containing an implicit template parameter will be made into a
17112          template.  If the resulting declaration is not going to be an
17113          actual function then finish the template scope here to prevent it.
17114          An error message will be issued once we have a decl to talk about.
17115
17116          FIXME probably we should do type deduction rather than create an
17117          implicit template, but the standard currently doesn't allow it. */
17118       bogus_implicit_tmpl = true;
17119       finish_fully_implicit_template (parser, NULL_TREE);
17120     }
17121
17122   /* [dcl.dcl]
17123
17124      Only in function declarations for constructors, destructors, and
17125      type conversions can the decl-specifier-seq be omitted.
17126
17127      We explicitly postpone this check past the point where we handle
17128      function-definitions because we tolerate function-definitions
17129      that are missing their return types in some modes.  */
17130   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17131     {
17132       cp_parser_error (parser,
17133                        "expected constructor, destructor, or type conversion");
17134       return error_mark_node;
17135     }
17136
17137   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
17138   if (token->type == CPP_EQ
17139       || token->type == CPP_OPEN_PAREN
17140       || token->type == CPP_OPEN_BRACE)
17141     {
17142       is_initialized = SD_INITIALIZED;
17143       initialization_kind = token->type;
17144       if (maybe_range_for_decl)
17145         *maybe_range_for_decl = error_mark_node;
17146       tmp_init_loc = token->location;
17147       if (init_loc && *init_loc == UNKNOWN_LOCATION)
17148         *init_loc = tmp_init_loc;
17149
17150       if (token->type == CPP_EQ
17151           && function_declarator_p (declarator))
17152         {
17153           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17154           if (t2->keyword == RID_DEFAULT)
17155             is_initialized = SD_DEFAULTED;
17156           else if (t2->keyword == RID_DELETE)
17157             is_initialized = SD_DELETED;
17158         }
17159     }
17160   else
17161     {
17162       /* If the init-declarator isn't initialized and isn't followed by a
17163          `,' or `;', it's not a valid init-declarator.  */
17164       if (token->type != CPP_COMMA
17165           && token->type != CPP_SEMICOLON)
17166         {
17167           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17168             range_for_decl_p = true;
17169           else
17170             {
17171               if (!maybe_range_for_decl)
17172                 cp_parser_error (parser, "expected initializer");
17173               return error_mark_node;
17174             }
17175         }
17176       is_initialized = SD_UNINITIALIZED;
17177       initialization_kind = CPP_EOF;
17178     }
17179
17180   /* Because start_decl has side-effects, we should only call it if we
17181      know we're going ahead.  By this point, we know that we cannot
17182      possibly be looking at any other construct.  */
17183   cp_parser_commit_to_tentative_parse (parser);
17184
17185   /* Enter the newly declared entry in the symbol table.  If we're
17186      processing a declaration in a class-specifier, we wait until
17187      after processing the initializer.  */
17188   if (!member_p)
17189     {
17190       if (parser->in_unbraced_linkage_specification_p)
17191         decl_specifiers->storage_class = sc_extern;
17192       decl = start_decl (declarator, decl_specifiers,
17193                          range_for_decl_p? SD_INITIALIZED : is_initialized,
17194                          attributes, prefix_attributes, &pushed_scope);
17195       cp_finalize_omp_declare_simd (parser, decl);
17196       /* Adjust location of decl if declarator->id_loc is more appropriate:
17197          set, and decl wasn't merged with another decl, in which case its
17198          location would be different from input_location, and more accurate.  */
17199       if (DECL_P (decl)
17200           && declarator->id_loc != UNKNOWN_LOCATION
17201           && DECL_SOURCE_LOCATION (decl) == input_location)
17202         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17203     }
17204   else if (scope)
17205     /* Enter the SCOPE.  That way unqualified names appearing in the
17206        initializer will be looked up in SCOPE.  */
17207     pushed_scope = push_scope (scope);
17208
17209   /* Perform deferred access control checks, now that we know in which
17210      SCOPE the declared entity resides.  */
17211   if (!member_p && decl)
17212     {
17213       tree saved_current_function_decl = NULL_TREE;
17214
17215       /* If the entity being declared is a function, pretend that we
17216          are in its scope.  If it is a `friend', it may have access to
17217          things that would not otherwise be accessible.  */
17218       if (TREE_CODE (decl) == FUNCTION_DECL)
17219         {
17220           saved_current_function_decl = current_function_decl;
17221           current_function_decl = decl;
17222         }
17223
17224       /* Perform access checks for template parameters.  */
17225       cp_parser_perform_template_parameter_access_checks (checks);
17226
17227       /* Perform the access control checks for the declarator and the
17228          decl-specifiers.  */
17229       perform_deferred_access_checks (tf_warning_or_error);
17230
17231       /* Restore the saved value.  */
17232       if (TREE_CODE (decl) == FUNCTION_DECL)
17233         current_function_decl = saved_current_function_decl;
17234     }
17235
17236   /* Parse the initializer.  */
17237   initializer = NULL_TREE;
17238   is_direct_init = false;
17239   is_non_constant_init = true;
17240   if (is_initialized)
17241     {
17242       if (function_declarator_p (declarator))
17243         {
17244            if (initialization_kind == CPP_EQ)
17245              initializer = cp_parser_pure_specifier (parser);
17246            else
17247              {
17248                /* If the declaration was erroneous, we don't really
17249                   know what the user intended, so just silently
17250                   consume the initializer.  */
17251                if (decl != error_mark_node)
17252                  error_at (tmp_init_loc, "initializer provided for function");
17253                cp_parser_skip_to_closing_parenthesis (parser,
17254                                                       /*recovering=*/true,
17255                                                       /*or_comma=*/false,
17256                                                       /*consume_paren=*/true);
17257              }
17258         }
17259       else
17260         {
17261           /* We want to record the extra mangling scope for in-class
17262              initializers of class members and initializers of static data
17263              member templates.  The former involves deferring
17264              parsing of the initializer until end of class as with default
17265              arguments.  So right here we only handle the latter.  */
17266           if (!member_p && processing_template_decl)
17267             start_lambda_scope (decl);
17268           initializer = cp_parser_initializer (parser,
17269                                                &is_direct_init,
17270                                                &is_non_constant_init);
17271           if (!member_p && processing_template_decl)
17272             finish_lambda_scope ();
17273           if (initializer == error_mark_node)
17274             cp_parser_skip_to_end_of_statement (parser);
17275         }
17276     }
17277
17278   /* The old parser allows attributes to appear after a parenthesized
17279      initializer.  Mark Mitchell proposed removing this functionality
17280      on the GCC mailing lists on 2002-08-13.  This parser accepts the
17281      attributes -- but ignores them.  */
17282   if (cp_parser_allow_gnu_extensions_p (parser)
17283       && initialization_kind == CPP_OPEN_PAREN)
17284     if (cp_parser_attributes_opt (parser))
17285       warning (OPT_Wattributes,
17286                "attributes after parenthesized initializer ignored");
17287
17288   /* And now complain about a non-function implicit template.  */
17289   if (bogus_implicit_tmpl)
17290     error_at (DECL_SOURCE_LOCATION (decl),
17291               "non-function %qD declared as implicit template", decl);
17292
17293   /* For an in-class declaration, use `grokfield' to create the
17294      declaration.  */
17295   if (member_p)
17296     {
17297       if (pushed_scope)
17298         {
17299           pop_scope (pushed_scope);
17300           pushed_scope = NULL_TREE;
17301         }
17302       decl = grokfield (declarator, decl_specifiers,
17303                         initializer, !is_non_constant_init,
17304                         /*asmspec=*/NULL_TREE,
17305                         chainon (attributes, prefix_attributes));
17306       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17307         cp_parser_save_default_args (parser, decl);
17308       cp_finalize_omp_declare_simd (parser, decl);
17309     }
17310
17311   /* Finish processing the declaration.  But, skip member
17312      declarations.  */
17313   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17314     {
17315       cp_finish_decl (decl,
17316                       initializer, !is_non_constant_init,
17317                       asm_specification,
17318                       /* If the initializer is in parentheses, then this is
17319                          a direct-initialization, which means that an
17320                          `explicit' constructor is OK.  Otherwise, an
17321                          `explicit' constructor cannot be used.  */
17322                       ((is_direct_init || !is_initialized)
17323                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17324     }
17325   else if ((cxx_dialect != cxx98) && friend_p
17326            && decl && TREE_CODE (decl) == FUNCTION_DECL)
17327     /* Core issue #226 (C++0x only): A default template-argument
17328        shall not be specified in a friend class template
17329        declaration. */
17330     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
17331                              /*is_partial=*/false, /*is_friend_decl=*/1);
17332
17333   if (!friend_p && pushed_scope)
17334     pop_scope (pushed_scope);
17335
17336   if (function_declarator_p (declarator)
17337       && parser->fully_implicit_function_template_p)
17338     {
17339       if (member_p)
17340         decl = finish_fully_implicit_template (parser, decl);
17341       else
17342         finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17343     }
17344
17345   return decl;
17346 }
17347
17348 /* Parse a declarator.
17349
17350    declarator:
17351      direct-declarator
17352      ptr-operator declarator
17353
17354    abstract-declarator:
17355      ptr-operator abstract-declarator [opt]
17356      direct-abstract-declarator
17357
17358    GNU Extensions:
17359
17360    declarator:
17361      attributes [opt] direct-declarator
17362      attributes [opt] ptr-operator declarator
17363
17364    abstract-declarator:
17365      attributes [opt] ptr-operator abstract-declarator [opt]
17366      attributes [opt] direct-abstract-declarator
17367
17368    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17369    detect constructor, destructor or conversion operators. It is set
17370    to -1 if the declarator is a name, and +1 if it is a
17371    function. Otherwise it is set to zero. Usually you just want to
17372    test for >0, but internally the negative value is used.
17373
17374    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17375    a decl-specifier-seq unless it declares a constructor, destructor,
17376    or conversion.  It might seem that we could check this condition in
17377    semantic analysis, rather than parsing, but that makes it difficult
17378    to handle something like `f()'.  We want to notice that there are
17379    no decl-specifiers, and therefore realize that this is an
17380    expression, not a declaration.)
17381
17382    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17383    the declarator is a direct-declarator of the form "(...)".
17384
17385    MEMBER_P is true iff this declarator is a member-declarator.
17386
17387    FRIEND_P is true iff this declarator is a friend.  */
17388
17389 static cp_declarator *
17390 cp_parser_declarator (cp_parser* parser,
17391                       cp_parser_declarator_kind dcl_kind,
17392                       int* ctor_dtor_or_conv_p,
17393                       bool* parenthesized_p,
17394                       bool member_p, bool friend_p)
17395 {
17396   cp_declarator *declarator;
17397   enum tree_code code;
17398   cp_cv_quals cv_quals;
17399   tree class_type;
17400   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17401
17402   /* Assume this is not a constructor, destructor, or type-conversion
17403      operator.  */
17404   if (ctor_dtor_or_conv_p)
17405     *ctor_dtor_or_conv_p = 0;
17406
17407   if (cp_parser_allow_gnu_extensions_p (parser))
17408     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17409
17410   /* Check for the ptr-operator production.  */
17411   cp_parser_parse_tentatively (parser);
17412   /* Parse the ptr-operator.  */
17413   code = cp_parser_ptr_operator (parser,
17414                                  &class_type,
17415                                  &cv_quals,
17416                                  &std_attributes);
17417
17418   /* If that worked, then we have a ptr-operator.  */
17419   if (cp_parser_parse_definitely (parser))
17420     {
17421       /* If a ptr-operator was found, then this declarator was not
17422          parenthesized.  */
17423       if (parenthesized_p)
17424         *parenthesized_p = true;
17425       /* The dependent declarator is optional if we are parsing an
17426          abstract-declarator.  */
17427       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17428         cp_parser_parse_tentatively (parser);
17429
17430       /* Parse the dependent declarator.  */
17431       declarator = cp_parser_declarator (parser, dcl_kind,
17432                                          /*ctor_dtor_or_conv_p=*/NULL,
17433                                          /*parenthesized_p=*/NULL,
17434                                          /*member_p=*/false,
17435                                          friend_p);
17436
17437       /* If we are parsing an abstract-declarator, we must handle the
17438          case where the dependent declarator is absent.  */
17439       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17440           && !cp_parser_parse_definitely (parser))
17441         declarator = NULL;
17442
17443       declarator = cp_parser_make_indirect_declarator
17444         (code, class_type, cv_quals, declarator, std_attributes);
17445     }
17446   /* Everything else is a direct-declarator.  */
17447   else
17448     {
17449       if (parenthesized_p)
17450         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17451                                                    CPP_OPEN_PAREN);
17452       declarator = cp_parser_direct_declarator (parser, dcl_kind,
17453                                                 ctor_dtor_or_conv_p,
17454                                                 member_p, friend_p);
17455     }
17456
17457   if (gnu_attributes && declarator && declarator != cp_error_declarator)
17458     declarator->attributes = gnu_attributes;
17459   return declarator;
17460 }
17461
17462 /* Parse a direct-declarator or direct-abstract-declarator.
17463
17464    direct-declarator:
17465      declarator-id
17466      direct-declarator ( parameter-declaration-clause )
17467        cv-qualifier-seq [opt]
17468        ref-qualifier [opt]
17469        exception-specification [opt]
17470      direct-declarator [ constant-expression [opt] ]
17471      ( declarator )
17472
17473    direct-abstract-declarator:
17474      direct-abstract-declarator [opt]
17475        ( parameter-declaration-clause )
17476        cv-qualifier-seq [opt]
17477        ref-qualifier [opt]
17478        exception-specification [opt]
17479      direct-abstract-declarator [opt] [ constant-expression [opt] ]
17480      ( abstract-declarator )
17481
17482    Returns a representation of the declarator.  DCL_KIND is
17483    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17484    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
17485    we are parsing a direct-declarator.  It is
17486    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17487    of ambiguity we prefer an abstract declarator, as per
17488    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17489    as for cp_parser_declarator.  */
17490
17491 static cp_declarator *
17492 cp_parser_direct_declarator (cp_parser* parser,
17493                              cp_parser_declarator_kind dcl_kind,
17494                              int* ctor_dtor_or_conv_p,
17495                              bool member_p, bool friend_p)
17496 {
17497   cp_token *token;
17498   cp_declarator *declarator = NULL;
17499   tree scope = NULL_TREE;
17500   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17501   bool saved_in_declarator_p = parser->in_declarator_p;
17502   bool first = true;
17503   tree pushed_scope = NULL_TREE;
17504
17505   while (true)
17506     {
17507       /* Peek at the next token.  */
17508       token = cp_lexer_peek_token (parser->lexer);
17509       if (token->type == CPP_OPEN_PAREN)
17510         {
17511           /* This is either a parameter-declaration-clause, or a
17512              parenthesized declarator. When we know we are parsing a
17513              named declarator, it must be a parenthesized declarator
17514              if FIRST is true. For instance, `(int)' is a
17515              parameter-declaration-clause, with an omitted
17516              direct-abstract-declarator. But `((*))', is a
17517              parenthesized abstract declarator. Finally, when T is a
17518              template parameter `(T)' is a
17519              parameter-declaration-clause, and not a parenthesized
17520              named declarator.
17521
17522              We first try and parse a parameter-declaration-clause,
17523              and then try a nested declarator (if FIRST is true).
17524
17525              It is not an error for it not to be a
17526              parameter-declaration-clause, even when FIRST is
17527              false. Consider,
17528
17529                int i (int);
17530                int i (3);
17531
17532              The first is the declaration of a function while the
17533              second is the definition of a variable, including its
17534              initializer.
17535
17536              Having seen only the parenthesis, we cannot know which of
17537              these two alternatives should be selected.  Even more
17538              complex are examples like:
17539
17540                int i (int (a));
17541                int i (int (3));
17542
17543              The former is a function-declaration; the latter is a
17544              variable initialization.
17545
17546              Thus again, we try a parameter-declaration-clause, and if
17547              that fails, we back out and return.  */
17548
17549           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17550             {
17551               tree params;
17552               bool is_declarator = false;
17553
17554               /* In a member-declarator, the only valid interpretation
17555                  of a parenthesis is the start of a
17556                  parameter-declaration-clause.  (It is invalid to
17557                  initialize a static data member with a parenthesized
17558                  initializer; only the "=" form of initialization is
17559                  permitted.)  */
17560               if (!member_p)
17561                 cp_parser_parse_tentatively (parser);
17562
17563               /* Consume the `('.  */
17564               cp_lexer_consume_token (parser->lexer);
17565               if (first)
17566                 {
17567                   /* If this is going to be an abstract declarator, we're
17568                      in a declarator and we can't have default args.  */
17569                   parser->default_arg_ok_p = false;
17570                   parser->in_declarator_p = true;
17571                 }
17572
17573               begin_scope (sk_function_parms, NULL_TREE);
17574
17575               /* Parse the parameter-declaration-clause.  */
17576               params = cp_parser_parameter_declaration_clause (parser);
17577
17578               /* Consume the `)'.  */
17579               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17580
17581               /* If all went well, parse the cv-qualifier-seq,
17582                  ref-qualifier and the exception-specification.  */
17583               if (member_p || cp_parser_parse_definitely (parser))
17584                 {
17585                   cp_cv_quals cv_quals;
17586                   cp_virt_specifiers virt_specifiers;
17587                   cp_ref_qualifier ref_qual;
17588                   tree exception_specification;
17589                   tree late_return;
17590                   tree attrs;
17591                   bool memfn = (member_p || (pushed_scope
17592                                              && CLASS_TYPE_P (pushed_scope)));
17593
17594                   is_declarator = true;
17595
17596                   if (ctor_dtor_or_conv_p)
17597                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17598                   first = false;
17599
17600                   /* Parse the cv-qualifier-seq.  */
17601                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17602                   /* Parse the ref-qualifier. */
17603                   ref_qual = cp_parser_ref_qualifier_opt (parser);
17604                   /* And the exception-specification.  */
17605                   exception_specification
17606                     = cp_parser_exception_specification_opt (parser);
17607
17608                   attrs = cp_parser_std_attribute_spec_seq (parser);
17609
17610                   /* In here, we handle cases where attribute is used after
17611                      the function declaration.  For example:
17612                      void func (int x) __attribute__((vector(..)));  */
17613                   if (flag_cilkplus
17614                       && cp_next_tokens_can_be_gnu_attribute_p (parser))
17615                     {
17616                       cp_parser_parse_tentatively (parser);
17617                       tree attr = cp_parser_gnu_attributes_opt (parser);
17618                       if (cp_lexer_next_token_is_not (parser->lexer,
17619                                                       CPP_SEMICOLON)
17620                           && cp_lexer_next_token_is_not (parser->lexer,
17621                                                          CPP_OPEN_BRACE))
17622                         cp_parser_abort_tentative_parse (parser);
17623                       else if (!cp_parser_parse_definitely (parser))
17624                         ;
17625                       else
17626                         attrs = chainon (attr, attrs);
17627                     }
17628                   late_return = (cp_parser_late_return_type_opt
17629                                  (parser, declarator,
17630                                   memfn ? cv_quals : -1));
17631
17632
17633                   /* Parse the virt-specifier-seq.  */
17634                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17635
17636                   /* Create the function-declarator.  */
17637                   declarator = make_call_declarator (declarator,
17638                                                      params,
17639                                                      cv_quals,
17640                                                      virt_specifiers,
17641                                                      ref_qual,
17642                                                      exception_specification,
17643                                                      late_return);
17644                   declarator->std_attributes = attrs;
17645                   /* Any subsequent parameter lists are to do with
17646                      return type, so are not those of the declared
17647                      function.  */
17648                   parser->default_arg_ok_p = false;
17649                 }
17650
17651               /* Remove the function parms from scope.  */
17652               pop_bindings_and_leave_scope ();
17653
17654               if (is_declarator)
17655                 /* Repeat the main loop.  */
17656                 continue;
17657             }
17658
17659           /* If this is the first, we can try a parenthesized
17660              declarator.  */
17661           if (first)
17662             {
17663               bool saved_in_type_id_in_expr_p;
17664
17665               parser->default_arg_ok_p = saved_default_arg_ok_p;
17666               parser->in_declarator_p = saved_in_declarator_p;
17667
17668               /* Consume the `('.  */
17669               cp_lexer_consume_token (parser->lexer);
17670               /* Parse the nested declarator.  */
17671               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17672               parser->in_type_id_in_expr_p = true;
17673               declarator
17674                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17675                                         /*parenthesized_p=*/NULL,
17676                                         member_p, friend_p);
17677               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17678               first = false;
17679               /* Expect a `)'.  */
17680               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17681                 declarator = cp_error_declarator;
17682               if (declarator == cp_error_declarator)
17683                 break;
17684
17685               goto handle_declarator;
17686             }
17687           /* Otherwise, we must be done.  */
17688           else
17689             break;
17690         }
17691       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17692                && token->type == CPP_OPEN_SQUARE
17693                && !cp_next_tokens_can_be_attribute_p (parser))
17694         {
17695           /* Parse an array-declarator.  */
17696           tree bounds, attrs;
17697
17698           if (ctor_dtor_or_conv_p)
17699             *ctor_dtor_or_conv_p = 0;
17700
17701           first = false;
17702           parser->default_arg_ok_p = false;
17703           parser->in_declarator_p = true;
17704           /* Consume the `['.  */
17705           cp_lexer_consume_token (parser->lexer);
17706           /* Peek at the next token.  */
17707           token = cp_lexer_peek_token (parser->lexer);
17708           /* If the next token is `]', then there is no
17709              constant-expression.  */
17710           if (token->type != CPP_CLOSE_SQUARE)
17711             {
17712               bool non_constant_p;
17713               bounds
17714                 = cp_parser_constant_expression (parser,
17715                                                  /*allow_non_constant=*/true,
17716                                                  &non_constant_p);
17717               if (!non_constant_p)
17718                 /* OK */;
17719               else if (error_operand_p (bounds))
17720                 /* Already gave an error.  */;
17721               else if (!parser->in_function_body
17722                        || current_binding_level->kind == sk_function_parms)
17723                 {
17724                   /* Normally, the array bound must be an integral constant
17725                      expression.  However, as an extension, we allow VLAs
17726                      in function scopes as long as they aren't part of a
17727                      parameter declaration.  */
17728                   cp_parser_error (parser,
17729                                    "array bound is not an integer constant");
17730                   bounds = error_mark_node;
17731                 }
17732               else if (processing_template_decl
17733                        && !type_dependent_expression_p (bounds))
17734                 {
17735                   /* Remember this wasn't a constant-expression.  */
17736                   bounds = build_nop (TREE_TYPE (bounds), bounds);
17737                   TREE_SIDE_EFFECTS (bounds) = 1;
17738                 }
17739             }
17740           else
17741             bounds = NULL_TREE;
17742           /* Look for the closing `]'.  */
17743           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17744             {
17745               declarator = cp_error_declarator;
17746               break;
17747             }
17748
17749           attrs = cp_parser_std_attribute_spec_seq (parser);
17750           declarator = make_array_declarator (declarator, bounds);
17751           declarator->std_attributes = attrs;
17752         }
17753       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17754         {
17755           {
17756             tree qualifying_scope;
17757             tree unqualified_name;
17758             tree attrs;
17759             special_function_kind sfk;
17760             bool abstract_ok;
17761             bool pack_expansion_p = false;
17762             cp_token *declarator_id_start_token;
17763
17764             /* Parse a declarator-id */
17765             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17766             if (abstract_ok)
17767               {
17768                 cp_parser_parse_tentatively (parser);
17769
17770                 /* If we see an ellipsis, we should be looking at a
17771                    parameter pack. */
17772                 if (token->type == CPP_ELLIPSIS)
17773                   {
17774                     /* Consume the `...' */
17775                     cp_lexer_consume_token (parser->lexer);
17776
17777                     pack_expansion_p = true;
17778                   }
17779               }
17780
17781             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17782             unqualified_name
17783               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17784             qualifying_scope = parser->scope;
17785             if (abstract_ok)
17786               {
17787                 bool okay = false;
17788
17789                 if (!unqualified_name && pack_expansion_p)
17790                   {
17791                     /* Check whether an error occurred. */
17792                     okay = !cp_parser_error_occurred (parser);
17793
17794                     /* We already consumed the ellipsis to mark a
17795                        parameter pack, but we have no way to report it,
17796                        so abort the tentative parse. We will be exiting
17797                        immediately anyway. */
17798                     cp_parser_abort_tentative_parse (parser);
17799                   }
17800                 else
17801                   okay = cp_parser_parse_definitely (parser);
17802
17803                 if (!okay)
17804                   unqualified_name = error_mark_node;
17805                 else if (unqualified_name
17806                          && (qualifying_scope
17807                              || (!identifier_p (unqualified_name))))
17808                   {
17809                     cp_parser_error (parser, "expected unqualified-id");
17810                     unqualified_name = error_mark_node;
17811                   }
17812               }
17813
17814             if (!unqualified_name)
17815               return NULL;
17816             if (unqualified_name == error_mark_node)
17817               {
17818                 declarator = cp_error_declarator;
17819                 pack_expansion_p = false;
17820                 declarator->parameter_pack_p = false;
17821                 break;
17822               }
17823
17824             attrs = cp_parser_std_attribute_spec_seq (parser);
17825
17826             if (qualifying_scope && at_namespace_scope_p ()
17827                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17828               {
17829                 /* In the declaration of a member of a template class
17830                    outside of the class itself, the SCOPE will sometimes
17831                    be a TYPENAME_TYPE.  For example, given:
17832
17833                    template <typename T>
17834                    int S<T>::R::i = 3;
17835
17836                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
17837                    this context, we must resolve S<T>::R to an ordinary
17838                    type, rather than a typename type.
17839
17840                    The reason we normally avoid resolving TYPENAME_TYPEs
17841                    is that a specialization of `S' might render
17842                    `S<T>::R' not a type.  However, if `S' is
17843                    specialized, then this `i' will not be used, so there
17844                    is no harm in resolving the types here.  */
17845                 tree type;
17846
17847                 /* Resolve the TYPENAME_TYPE.  */
17848                 type = resolve_typename_type (qualifying_scope,
17849                                               /*only_current_p=*/false);
17850                 /* If that failed, the declarator is invalid.  */
17851                 if (TREE_CODE (type) == TYPENAME_TYPE)
17852                   {
17853                     if (typedef_variant_p (type))
17854                       error_at (declarator_id_start_token->location,
17855                                 "cannot define member of dependent typedef "
17856                                 "%qT", type);
17857                     else
17858                       error_at (declarator_id_start_token->location,
17859                                 "%<%T::%E%> is not a type",
17860                                 TYPE_CONTEXT (qualifying_scope),
17861                                 TYPE_IDENTIFIER (qualifying_scope));
17862                   }
17863                 qualifying_scope = type;
17864               }
17865
17866             sfk = sfk_none;
17867
17868             if (unqualified_name)
17869               {
17870                 tree class_type;
17871
17872                 if (qualifying_scope
17873                     && CLASS_TYPE_P (qualifying_scope))
17874                   class_type = qualifying_scope;
17875                 else
17876                   class_type = current_class_type;
17877
17878                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17879                   {
17880                     tree name_type = TREE_TYPE (unqualified_name);
17881                     if (class_type && same_type_p (name_type, class_type))
17882                       {
17883                         if (qualifying_scope
17884                             && CLASSTYPE_USE_TEMPLATE (name_type))
17885                           {
17886                             error_at (declarator_id_start_token->location,
17887                                       "invalid use of constructor as a template");
17888                             inform (declarator_id_start_token->location,
17889                                     "use %<%T::%D%> instead of %<%T::%D%> to "
17890                                     "name the constructor in a qualified name",
17891                                     class_type,
17892                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17893                                     class_type, name_type);
17894                             declarator = cp_error_declarator;
17895                             break;
17896                           }
17897                         else
17898                           unqualified_name = constructor_name (class_type);
17899                       }
17900                     else
17901                       {
17902                         /* We do not attempt to print the declarator
17903                            here because we do not have enough
17904                            information about its original syntactic
17905                            form.  */
17906                         cp_parser_error (parser, "invalid declarator");
17907                         declarator = cp_error_declarator;
17908                         break;
17909                       }
17910                   }
17911
17912                 if (class_type)
17913                   {
17914                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17915                       sfk = sfk_destructor;
17916                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17917                       sfk = sfk_conversion;
17918                     else if (/* There's no way to declare a constructor
17919                                 for an anonymous type, even if the type
17920                                 got a name for linkage purposes.  */
17921                              !TYPE_WAS_ANONYMOUS (class_type)
17922                              /* Handle correctly (c++/19200):
17923
17924                                 struct S {
17925                                   struct T{};
17926                                   friend void S(T);
17927                                 };
17928
17929                                 and also:
17930
17931                                 namespace N {
17932                                   void S();
17933                                 }
17934
17935                                 struct S {
17936                                   friend void N::S();
17937                                 };  */
17938                              && !(friend_p
17939                                   && class_type != qualifying_scope)
17940                              && constructor_name_p (unqualified_name,
17941                                                     class_type))
17942                       {
17943                         unqualified_name = constructor_name (class_type);
17944                         sfk = sfk_constructor;
17945                       }
17946                     else if (is_overloaded_fn (unqualified_name)
17947                              && DECL_CONSTRUCTOR_P (get_first_fn
17948                                                     (unqualified_name)))
17949                       sfk = sfk_constructor;
17950
17951                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
17952                       *ctor_dtor_or_conv_p = -1;
17953                   }
17954               }
17955             declarator = make_id_declarator (qualifying_scope,
17956                                              unqualified_name,
17957                                              sfk);
17958             declarator->std_attributes = attrs;
17959             declarator->id_loc = token->location;
17960             declarator->parameter_pack_p = pack_expansion_p;
17961
17962             if (pack_expansion_p)
17963               maybe_warn_variadic_templates ();
17964           }
17965
17966         handle_declarator:;
17967           scope = get_scope_of_declarator (declarator);
17968           if (scope)
17969             {
17970               /* Any names that appear after the declarator-id for a
17971                  member are looked up in the containing scope.  */
17972               if (at_function_scope_p ())
17973                 {
17974                   /* But declarations with qualified-ids can't appear in a
17975                      function.  */
17976                   cp_parser_error (parser, "qualified-id in declaration");
17977                   declarator = cp_error_declarator;
17978                   break;
17979                 }
17980               pushed_scope = push_scope (scope);
17981             }
17982           parser->in_declarator_p = true;
17983           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17984               || (declarator && declarator->kind == cdk_id))
17985             /* Default args are only allowed on function
17986                declarations.  */
17987             parser->default_arg_ok_p = saved_default_arg_ok_p;
17988           else
17989             parser->default_arg_ok_p = false;
17990
17991           first = false;
17992         }
17993       /* We're done.  */
17994       else
17995         break;
17996     }
17997
17998   /* For an abstract declarator, we might wind up with nothing at this
17999      point.  That's an error; the declarator is not optional.  */
18000   if (!declarator)
18001     cp_parser_error (parser, "expected declarator");
18002
18003   /* If we entered a scope, we must exit it now.  */
18004   if (pushed_scope)
18005     pop_scope (pushed_scope);
18006
18007   parser->default_arg_ok_p = saved_default_arg_ok_p;
18008   parser->in_declarator_p = saved_in_declarator_p;
18009
18010   return declarator;
18011 }
18012
18013 /* Parse a ptr-operator.
18014
18015    ptr-operator:
18016      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18017      * cv-qualifier-seq [opt]
18018      &
18019      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18020      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18021
18022    GNU Extension:
18023
18024    ptr-operator:
18025      & cv-qualifier-seq [opt]
18026
18027    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18028    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18029    an rvalue reference. In the case of a pointer-to-member, *TYPE is
18030    filled in with the TYPE containing the member.  *CV_QUALS is
18031    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18032    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
18033    Note that the tree codes returned by this function have nothing
18034    to do with the types of trees that will be eventually be created
18035    to represent the pointer or reference type being parsed. They are
18036    just constants with suggestive names. */
18037 static enum tree_code
18038 cp_parser_ptr_operator (cp_parser* parser,
18039                         tree* type,
18040                         cp_cv_quals *cv_quals,
18041                         tree *attributes)
18042 {
18043   enum tree_code code = ERROR_MARK;
18044   cp_token *token;
18045   tree attrs = NULL_TREE;
18046
18047   /* Assume that it's not a pointer-to-member.  */
18048   *type = NULL_TREE;
18049   /* And that there are no cv-qualifiers.  */
18050   *cv_quals = TYPE_UNQUALIFIED;
18051
18052   /* Peek at the next token.  */
18053   token = cp_lexer_peek_token (parser->lexer);
18054
18055   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
18056   if (token->type == CPP_MULT)
18057     code = INDIRECT_REF;
18058   else if (token->type == CPP_AND)
18059     code = ADDR_EXPR;
18060   else if ((cxx_dialect != cxx98) &&
18061            token->type == CPP_AND_AND) /* C++0x only */
18062     code = NON_LVALUE_EXPR;
18063
18064   if (code != ERROR_MARK)
18065     {
18066       /* Consume the `*', `&' or `&&'.  */
18067       cp_lexer_consume_token (parser->lexer);
18068
18069       /* A `*' can be followed by a cv-qualifier-seq, and so can a
18070          `&', if we are allowing GNU extensions.  (The only qualifier
18071          that can legally appear after `&' is `restrict', but that is
18072          enforced during semantic analysis.  */
18073       if (code == INDIRECT_REF
18074           || cp_parser_allow_gnu_extensions_p (parser))
18075         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18076
18077       attrs = cp_parser_std_attribute_spec_seq (parser);
18078       if (attributes != NULL)
18079         *attributes = attrs;
18080     }
18081   else
18082     {
18083       /* Try the pointer-to-member case.  */
18084       cp_parser_parse_tentatively (parser);
18085       /* Look for the optional `::' operator.  */
18086       cp_parser_global_scope_opt (parser,
18087                                   /*current_scope_valid_p=*/false);
18088       /* Look for the nested-name specifier.  */
18089       token = cp_lexer_peek_token (parser->lexer);
18090       cp_parser_nested_name_specifier (parser,
18091                                        /*typename_keyword_p=*/false,
18092                                        /*check_dependency_p=*/true,
18093                                        /*type_p=*/false,
18094                                        /*is_declaration=*/false);
18095       /* If we found it, and the next token is a `*', then we are
18096          indeed looking at a pointer-to-member operator.  */
18097       if (!cp_parser_error_occurred (parser)
18098           && cp_parser_require (parser, CPP_MULT, RT_MULT))
18099         {
18100           /* Indicate that the `*' operator was used.  */
18101           code = INDIRECT_REF;
18102
18103           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18104             error_at (token->location, "%qD is a namespace", parser->scope);
18105           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18106             error_at (token->location, "cannot form pointer to member of "
18107                       "non-class %q#T", parser->scope);
18108           else
18109             {
18110               /* The type of which the member is a member is given by the
18111                  current SCOPE.  */
18112               *type = parser->scope;
18113               /* The next name will not be qualified.  */
18114               parser->scope = NULL_TREE;
18115               parser->qualifying_scope = NULL_TREE;
18116               parser->object_scope = NULL_TREE;
18117               /* Look for optional c++11 attributes.  */
18118               attrs = cp_parser_std_attribute_spec_seq (parser);
18119               if (attributes != NULL)
18120                 *attributes = attrs;
18121               /* Look for the optional cv-qualifier-seq.  */
18122               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18123             }
18124         }
18125       /* If that didn't work we don't have a ptr-operator.  */
18126       if (!cp_parser_parse_definitely (parser))
18127         cp_parser_error (parser, "expected ptr-operator");
18128     }
18129
18130   return code;
18131 }
18132
18133 /* Parse an (optional) cv-qualifier-seq.
18134
18135    cv-qualifier-seq:
18136      cv-qualifier cv-qualifier-seq [opt]
18137
18138    cv-qualifier:
18139      const
18140      volatile
18141
18142    GNU Extension:
18143
18144    cv-qualifier:
18145      __restrict__
18146
18147    Returns a bitmask representing the cv-qualifiers.  */
18148
18149 static cp_cv_quals
18150 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18151 {
18152   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18153
18154   while (true)
18155     {
18156       cp_token *token;
18157       cp_cv_quals cv_qualifier;
18158
18159       /* Peek at the next token.  */
18160       token = cp_lexer_peek_token (parser->lexer);
18161       /* See if it's a cv-qualifier.  */
18162       switch (token->keyword)
18163         {
18164         case RID_CONST:
18165           cv_qualifier = TYPE_QUAL_CONST;
18166           break;
18167
18168         case RID_VOLATILE:
18169           cv_qualifier = TYPE_QUAL_VOLATILE;
18170           break;
18171
18172         case RID_RESTRICT:
18173           cv_qualifier = TYPE_QUAL_RESTRICT;
18174           break;
18175
18176         default:
18177           cv_qualifier = TYPE_UNQUALIFIED;
18178           break;
18179         }
18180
18181       if (!cv_qualifier)
18182         break;
18183
18184       if (cv_quals & cv_qualifier)
18185         {
18186           error_at (token->location, "duplicate cv-qualifier");
18187           cp_lexer_purge_token (parser->lexer);
18188         }
18189       else
18190         {
18191           cp_lexer_consume_token (parser->lexer);
18192           cv_quals |= cv_qualifier;
18193         }
18194     }
18195
18196   return cv_quals;
18197 }
18198
18199 /* Parse an (optional) ref-qualifier
18200
18201    ref-qualifier:
18202      &
18203      &&
18204
18205    Returns cp_ref_qualifier representing ref-qualifier. */
18206
18207 static cp_ref_qualifier
18208 cp_parser_ref_qualifier_opt (cp_parser* parser)
18209 {
18210   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18211
18212   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
18213   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18214     return ref_qual;
18215
18216   while (true)
18217     {
18218       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18219       cp_token *token = cp_lexer_peek_token (parser->lexer);
18220
18221       switch (token->type)
18222         {
18223         case CPP_AND:
18224           curr_ref_qual = REF_QUAL_LVALUE;
18225           break;
18226
18227         case CPP_AND_AND:
18228           curr_ref_qual = REF_QUAL_RVALUE;
18229           break;
18230
18231         default:
18232           curr_ref_qual = REF_QUAL_NONE;
18233           break;
18234         }
18235
18236       if (!curr_ref_qual)
18237         break;
18238       else if (ref_qual)
18239         {
18240           error_at (token->location, "multiple ref-qualifiers");
18241           cp_lexer_purge_token (parser->lexer);
18242         }
18243       else
18244         {
18245           ref_qual = curr_ref_qual;
18246           cp_lexer_consume_token (parser->lexer);
18247         }
18248     }
18249
18250   return ref_qual;
18251 }
18252
18253 /* Parse an (optional) virt-specifier-seq.
18254
18255    virt-specifier-seq:
18256      virt-specifier virt-specifier-seq [opt]
18257
18258    virt-specifier:
18259      override
18260      final
18261
18262    Returns a bitmask representing the virt-specifiers.  */
18263
18264 static cp_virt_specifiers
18265 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18266 {
18267   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18268
18269   while (true)
18270     {
18271       cp_token *token;
18272       cp_virt_specifiers virt_specifier;
18273
18274       /* Peek at the next token.  */
18275       token = cp_lexer_peek_token (parser->lexer);
18276       /* See if it's a virt-specifier-qualifier.  */
18277       if (token->type != CPP_NAME)
18278         break;
18279       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18280         {
18281           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18282           virt_specifier = VIRT_SPEC_OVERRIDE;
18283         }
18284       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18285         {
18286           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18287           virt_specifier = VIRT_SPEC_FINAL;
18288         }
18289       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18290         {
18291           virt_specifier = VIRT_SPEC_FINAL;
18292         }
18293       else
18294         break;
18295
18296       if (virt_specifiers & virt_specifier)
18297         {
18298           error_at (token->location, "duplicate virt-specifier");
18299           cp_lexer_purge_token (parser->lexer);
18300         }
18301       else
18302         {
18303           cp_lexer_consume_token (parser->lexer);
18304           virt_specifiers |= virt_specifier;
18305         }
18306     }
18307   return virt_specifiers;
18308 }
18309
18310 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18311    is in scope even though it isn't real.  */
18312
18313 void
18314 inject_this_parameter (tree ctype, cp_cv_quals quals)
18315 {
18316   tree this_parm;
18317
18318   if (current_class_ptr)
18319     {
18320       /* We don't clear this between NSDMIs.  Is it already what we want?  */
18321       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18322       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18323           && cp_type_quals (type) == quals)
18324         return;
18325     }
18326
18327   this_parm = build_this_parm (ctype, quals);
18328   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
18329   current_class_ptr = NULL_TREE;
18330   current_class_ref
18331     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18332   current_class_ptr = this_parm;
18333 }
18334
18335 /* Return true iff our current scope is a non-static data member
18336    initializer.  */
18337
18338 bool
18339 parsing_nsdmi (void)
18340 {
18341   /* We recognize NSDMI context by the context-less 'this' pointer set up
18342      by the function above.  */
18343   if (current_class_ptr
18344       && TREE_CODE (current_class_ptr) == PARM_DECL
18345       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18346     return true;
18347   return false;
18348 }
18349
18350 /* Parse a late-specified return type, if any.  This is not a separate
18351    non-terminal, but part of a function declarator, which looks like
18352
18353    -> trailing-type-specifier-seq abstract-declarator(opt)
18354
18355    Returns the type indicated by the type-id.
18356
18357    In addition to this this parses any queued up omp declare simd
18358    clauses and Cilk Plus SIMD-enabled function's vector attributes.
18359
18360    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18361    function.  */
18362
18363 static tree
18364 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18365                                 cp_cv_quals quals)
18366 {
18367   cp_token *token;
18368   tree type = NULL_TREE;
18369   bool declare_simd_p = (parser->omp_declare_simd
18370                          && declarator
18371                          && declarator->kind == cdk_id);
18372
18373   bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info 
18374                                 && declarator && declarator->kind == cdk_id);
18375   
18376   /* Peek at the next token.  */
18377   token = cp_lexer_peek_token (parser->lexer);
18378   /* A late-specified return type is indicated by an initial '->'. */
18379   if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18380     return NULL_TREE;
18381
18382   tree save_ccp = current_class_ptr;
18383   tree save_ccr = current_class_ref;
18384   if (quals >= 0)
18385     {
18386       /* DR 1207: 'this' is in scope in the trailing return type.  */
18387       inject_this_parameter (current_class_type, quals);
18388     }
18389
18390   if (token->type == CPP_DEREF)
18391     {
18392       /* Consume the ->.  */
18393       cp_lexer_consume_token (parser->lexer);
18394
18395       type = cp_parser_trailing_type_id (parser);
18396     }
18397
18398   if (cilk_simd_fn_vector_p)
18399     declarator->std_attributes
18400       = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18401                                                   declarator->std_attributes);
18402   if (declare_simd_p)
18403     declarator->std_attributes
18404       = cp_parser_late_parsing_omp_declare_simd (parser,
18405                                                  declarator->std_attributes);
18406
18407   if (quals >= 0)
18408     {
18409       current_class_ptr = save_ccp;
18410       current_class_ref = save_ccr;
18411     }
18412
18413   return type;
18414 }
18415
18416 /* Parse a declarator-id.
18417
18418    declarator-id:
18419      id-expression
18420      :: [opt] nested-name-specifier [opt] type-name
18421
18422    In the `id-expression' case, the value returned is as for
18423    cp_parser_id_expression if the id-expression was an unqualified-id.
18424    If the id-expression was a qualified-id, then a SCOPE_REF is
18425    returned.  The first operand is the scope (either a NAMESPACE_DECL
18426    or TREE_TYPE), but the second is still just a representation of an
18427    unqualified-id.  */
18428
18429 static tree
18430 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18431 {
18432   tree id;
18433   /* The expression must be an id-expression.  Assume that qualified
18434      names are the names of types so that:
18435
18436        template <class T>
18437        int S<T>::R::i = 3;
18438
18439      will work; we must treat `S<T>::R' as the name of a type.
18440      Similarly, assume that qualified names are templates, where
18441      required, so that:
18442
18443        template <class T>
18444        int S<T>::R<T>::i = 3;
18445
18446      will work, too.  */
18447   id = cp_parser_id_expression (parser,
18448                                 /*template_keyword_p=*/false,
18449                                 /*check_dependency_p=*/false,
18450                                 /*template_p=*/NULL,
18451                                 /*declarator_p=*/true,
18452                                 optional_p);
18453   if (id && BASELINK_P (id))
18454     id = BASELINK_FUNCTIONS (id);
18455   return id;
18456 }
18457
18458 /* Parse a type-id.
18459
18460    type-id:
18461      type-specifier-seq abstract-declarator [opt]
18462
18463    Returns the TYPE specified.  */
18464
18465 static tree
18466 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18467                      bool is_trailing_return)
18468 {
18469   cp_decl_specifier_seq type_specifier_seq;
18470   cp_declarator *abstract_declarator;
18471
18472   /* Parse the type-specifier-seq.  */
18473   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18474                                 is_trailing_return,
18475                                 &type_specifier_seq);
18476   if (type_specifier_seq.type == error_mark_node)
18477     return error_mark_node;
18478
18479   /* There might or might not be an abstract declarator.  */
18480   cp_parser_parse_tentatively (parser);
18481   /* Look for the declarator.  */
18482   abstract_declarator
18483     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18484                             /*parenthesized_p=*/NULL,
18485                             /*member_p=*/false,
18486                             /*friend_p=*/false);
18487   /* Check to see if there really was a declarator.  */
18488   if (!cp_parser_parse_definitely (parser))
18489     abstract_declarator = NULL;
18490
18491   if (type_specifier_seq.type
18492       /* None of the valid uses of 'auto' in C++14 involve the type-id
18493          nonterminal, but it is valid in a trailing-return-type.  */
18494       && !(cxx_dialect >= cxx14 && is_trailing_return)
18495       && type_uses_auto (type_specifier_seq.type))
18496     {
18497       /* A type-id with type 'auto' is only ok if the abstract declarator
18498          is a function declarator with a late-specified return type.  */
18499       if (abstract_declarator
18500           && abstract_declarator->kind == cdk_function
18501           && abstract_declarator->u.function.late_return_type)
18502         /* OK */;
18503       else
18504         {
18505           error ("invalid use of %<auto%>");
18506           return error_mark_node;
18507         }
18508     }
18509   
18510   return groktypename (&type_specifier_seq, abstract_declarator,
18511                        is_template_arg);
18512 }
18513
18514 static tree cp_parser_type_id (cp_parser *parser)
18515 {
18516   return cp_parser_type_id_1 (parser, false, false);
18517 }
18518
18519 static tree cp_parser_template_type_arg (cp_parser *parser)
18520 {
18521   tree r;
18522   const char *saved_message = parser->type_definition_forbidden_message;
18523   parser->type_definition_forbidden_message
18524     = G_("types may not be defined in template arguments");
18525   r = cp_parser_type_id_1 (parser, true, false);
18526   parser->type_definition_forbidden_message = saved_message;
18527   if (cxx_dialect >= cxx14 && type_uses_auto (r))
18528     {
18529       error ("invalid use of %<auto%> in template argument");
18530       r = error_mark_node;
18531     }
18532   return r;
18533 }
18534
18535 static tree cp_parser_trailing_type_id (cp_parser *parser)
18536 {
18537   return cp_parser_type_id_1 (parser, false, true);
18538 }
18539
18540 /* Parse a type-specifier-seq.
18541
18542    type-specifier-seq:
18543      type-specifier type-specifier-seq [opt]
18544
18545    GNU extension:
18546
18547    type-specifier-seq:
18548      attributes type-specifier-seq [opt]
18549
18550    If IS_DECLARATION is true, we are at the start of a "condition" or
18551    exception-declaration, so we might be followed by a declarator-id.
18552
18553    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18554    i.e. we've just seen "->".
18555
18556    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
18557
18558 static void
18559 cp_parser_type_specifier_seq (cp_parser* parser,
18560                               bool is_declaration,
18561                               bool is_trailing_return,
18562                               cp_decl_specifier_seq *type_specifier_seq)
18563 {
18564   bool seen_type_specifier = false;
18565   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18566   cp_token *start_token = NULL;
18567
18568   /* Clear the TYPE_SPECIFIER_SEQ.  */
18569   clear_decl_specs (type_specifier_seq);
18570
18571   /* In the context of a trailing return type, enum E { } is an
18572      elaborated-type-specifier followed by a function-body, not an
18573      enum-specifier.  */
18574   if (is_trailing_return)
18575     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18576
18577   /* Parse the type-specifiers and attributes.  */
18578   while (true)
18579     {
18580       tree type_specifier;
18581       bool is_cv_qualifier;
18582
18583       /* Check for attributes first.  */
18584       if (cp_next_tokens_can_be_attribute_p (parser))
18585         {
18586           type_specifier_seq->attributes =
18587             chainon (type_specifier_seq->attributes,
18588                      cp_parser_attributes_opt (parser));
18589           continue;
18590         }
18591
18592       /* record the token of the beginning of the type specifier seq,
18593          for error reporting purposes*/
18594      if (!start_token)
18595        start_token = cp_lexer_peek_token (parser->lexer);
18596
18597       /* Look for the type-specifier.  */
18598       type_specifier = cp_parser_type_specifier (parser,
18599                                                  flags,
18600                                                  type_specifier_seq,
18601                                                  /*is_declaration=*/false,
18602                                                  NULL,
18603                                                  &is_cv_qualifier);
18604       if (!type_specifier)
18605         {
18606           /* If the first type-specifier could not be found, this is not a
18607              type-specifier-seq at all.  */
18608           if (!seen_type_specifier)
18609             {
18610               /* Set in_declarator_p to avoid skipping to the semicolon.  */
18611               int in_decl = parser->in_declarator_p;
18612               parser->in_declarator_p = true;
18613
18614               if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18615                   || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18616                 cp_parser_error (parser, "expected type-specifier");
18617
18618               parser->in_declarator_p = in_decl;
18619
18620               type_specifier_seq->type = error_mark_node;
18621               return;
18622             }
18623           /* If subsequent type-specifiers could not be found, the
18624              type-specifier-seq is complete.  */
18625           break;
18626         }
18627
18628       seen_type_specifier = true;
18629       /* The standard says that a condition can be:
18630
18631             type-specifier-seq declarator = assignment-expression
18632
18633          However, given:
18634
18635            struct S {};
18636            if (int S = ...)
18637
18638          we should treat the "S" as a declarator, not as a
18639          type-specifier.  The standard doesn't say that explicitly for
18640          type-specifier-seq, but it does say that for
18641          decl-specifier-seq in an ordinary declaration.  Perhaps it
18642          would be clearer just to allow a decl-specifier-seq here, and
18643          then add a semantic restriction that if any decl-specifiers
18644          that are not type-specifiers appear, the program is invalid.  */
18645       if (is_declaration && !is_cv_qualifier)
18646         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18647     }
18648 }
18649
18650 /* Return whether the function currently being declared has an associated
18651    template parameter list.  */
18652
18653 static bool
18654 function_being_declared_is_template_p (cp_parser* parser)
18655 {
18656   if (!current_template_parms || processing_template_parmlist)
18657     return false;
18658
18659   if (parser->implicit_template_scope)
18660     return true;
18661
18662   if (at_class_scope_p ()
18663       && TYPE_BEING_DEFINED (current_class_type))
18664     return parser->num_template_parameter_lists != 0;
18665
18666   return ((int) parser->num_template_parameter_lists > template_class_depth
18667           (current_class_type));
18668 }
18669
18670 /* Parse a parameter-declaration-clause.
18671
18672    parameter-declaration-clause:
18673      parameter-declaration-list [opt] ... [opt]
18674      parameter-declaration-list , ...
18675
18676    Returns a representation for the parameter declarations.  A return
18677    value of NULL indicates a parameter-declaration-clause consisting
18678    only of an ellipsis.  */
18679
18680 static tree
18681 cp_parser_parameter_declaration_clause (cp_parser* parser)
18682 {
18683   tree parameters;
18684   cp_token *token;
18685   bool ellipsis_p;
18686   bool is_error;
18687
18688   struct cleanup {
18689     cp_parser* parser;
18690     int auto_is_implicit_function_template_parm_p;
18691     ~cleanup() {
18692       parser->auto_is_implicit_function_template_parm_p
18693         = auto_is_implicit_function_template_parm_p;
18694     }
18695   } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18696
18697   (void) cleanup;
18698
18699   if (!processing_specialization
18700       && !processing_template_parmlist
18701       && !processing_explicit_instantiation)
18702     if (!current_function_decl
18703         || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18704       parser->auto_is_implicit_function_template_parm_p = true;
18705
18706   /* Peek at the next token.  */
18707   token = cp_lexer_peek_token (parser->lexer);
18708   /* Check for trivial parameter-declaration-clauses.  */
18709   if (token->type == CPP_ELLIPSIS)
18710     {
18711       /* Consume the `...' token.  */
18712       cp_lexer_consume_token (parser->lexer);
18713       return NULL_TREE;
18714     }
18715   else if (token->type == CPP_CLOSE_PAREN)
18716     /* There are no parameters.  */
18717     {
18718 #ifndef NO_IMPLICIT_EXTERN_C
18719       if (in_system_header_at (input_location)
18720           && current_class_type == NULL
18721           && current_lang_name == lang_name_c)
18722         return NULL_TREE;
18723       else
18724 #endif
18725         return void_list_node;
18726     }
18727   /* Check for `(void)', too, which is a special case.  */
18728   else if (token->keyword == RID_VOID
18729            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18730                == CPP_CLOSE_PAREN))
18731     {
18732       /* Consume the `void' token.  */
18733       cp_lexer_consume_token (parser->lexer);
18734       /* There are no parameters.  */
18735       return void_list_node;
18736     }
18737
18738   /* Parse the parameter-declaration-list.  */
18739   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18740   /* If a parse error occurred while parsing the
18741      parameter-declaration-list, then the entire
18742      parameter-declaration-clause is erroneous.  */
18743   if (is_error)
18744     return NULL;
18745
18746   /* Peek at the next token.  */
18747   token = cp_lexer_peek_token (parser->lexer);
18748   /* If it's a `,', the clause should terminate with an ellipsis.  */
18749   if (token->type == CPP_COMMA)
18750     {
18751       /* Consume the `,'.  */
18752       cp_lexer_consume_token (parser->lexer);
18753       /* Expect an ellipsis.  */
18754       ellipsis_p
18755         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18756     }
18757   /* It might also be `...' if the optional trailing `,' was
18758      omitted.  */
18759   else if (token->type == CPP_ELLIPSIS)
18760     {
18761       /* Consume the `...' token.  */
18762       cp_lexer_consume_token (parser->lexer);
18763       /* And remember that we saw it.  */
18764       ellipsis_p = true;
18765     }
18766   else
18767     ellipsis_p = false;
18768
18769   /* Finish the parameter list.  */
18770   if (!ellipsis_p)
18771     parameters = chainon (parameters, void_list_node);
18772
18773   return parameters;
18774 }
18775
18776 /* Parse a parameter-declaration-list.
18777
18778    parameter-declaration-list:
18779      parameter-declaration
18780      parameter-declaration-list , parameter-declaration
18781
18782    Returns a representation of the parameter-declaration-list, as for
18783    cp_parser_parameter_declaration_clause.  However, the
18784    `void_list_node' is never appended to the list.  Upon return,
18785    *IS_ERROR will be true iff an error occurred.  */
18786
18787 static tree
18788 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18789 {
18790   tree parameters = NULL_TREE;
18791   tree *tail = &parameters;
18792   bool saved_in_unbraced_linkage_specification_p;
18793   int index = 0;
18794
18795   /* Assume all will go well.  */
18796   *is_error = false;
18797   /* The special considerations that apply to a function within an
18798      unbraced linkage specifications do not apply to the parameters
18799      to the function.  */
18800   saved_in_unbraced_linkage_specification_p
18801     = parser->in_unbraced_linkage_specification_p;
18802   parser->in_unbraced_linkage_specification_p = false;
18803
18804   /* Look for more parameters.  */
18805   while (true)
18806     {
18807       cp_parameter_declarator *parameter;
18808       tree decl = error_mark_node;
18809       bool parenthesized_p = false;
18810       int template_parm_idx = (function_being_declared_is_template_p (parser)?
18811                                TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18812                                                 (current_template_parms)) : 0);
18813
18814       /* Parse the parameter.  */
18815       parameter
18816         = cp_parser_parameter_declaration (parser,
18817                                            /*template_parm_p=*/false,
18818                                            &parenthesized_p);
18819
18820       /* We don't know yet if the enclosing context is deprecated, so wait
18821          and warn in grokparms if appropriate.  */
18822       deprecated_state = DEPRECATED_SUPPRESS;
18823
18824       if (parameter)
18825         {
18826           /* If a function parameter pack was specified and an implicit template
18827              parameter was introduced during cp_parser_parameter_declaration,
18828              change any implicit parameters introduced into packs.  */
18829           if (parser->implicit_template_parms
18830               && parameter->declarator
18831               && parameter->declarator->parameter_pack_p)
18832             {
18833               int latest_template_parm_idx = TREE_VEC_LENGTH
18834                 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18835
18836               if (latest_template_parm_idx != template_parm_idx)
18837                 parameter->decl_specifiers.type = convert_generic_types_to_packs
18838                   (parameter->decl_specifiers.type,
18839                    template_parm_idx, latest_template_parm_idx);
18840             }
18841
18842           decl = grokdeclarator (parameter->declarator,
18843                                  &parameter->decl_specifiers,
18844                                  PARM,
18845                                  parameter->default_argument != NULL_TREE,
18846                                  &parameter->decl_specifiers.attributes);
18847         }
18848
18849       deprecated_state = DEPRECATED_NORMAL;
18850
18851       /* If a parse error occurred parsing the parameter declaration,
18852          then the entire parameter-declaration-list is erroneous.  */
18853       if (decl == error_mark_node)
18854         {
18855           *is_error = true;
18856           parameters = error_mark_node;
18857           break;
18858         }
18859
18860       if (parameter->decl_specifiers.attributes)
18861         cplus_decl_attributes (&decl,
18862                                parameter->decl_specifiers.attributes,
18863                                0);
18864       if (DECL_NAME (decl))
18865         decl = pushdecl (decl);
18866
18867       if (decl != error_mark_node)
18868         {
18869           retrofit_lang_decl (decl);
18870           DECL_PARM_INDEX (decl) = ++index;
18871           DECL_PARM_LEVEL (decl) = function_parm_depth ();
18872         }
18873
18874       /* Add the new parameter to the list.  */
18875       *tail = build_tree_list (parameter->default_argument, decl);
18876       tail = &TREE_CHAIN (*tail);
18877
18878       /* Peek at the next token.  */
18879       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18880           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18881           /* These are for Objective-C++ */
18882           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18883           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18884         /* The parameter-declaration-list is complete.  */
18885         break;
18886       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18887         {
18888           cp_token *token;
18889
18890           /* Peek at the next token.  */
18891           token = cp_lexer_peek_nth_token (parser->lexer, 2);
18892           /* If it's an ellipsis, then the list is complete.  */
18893           if (token->type == CPP_ELLIPSIS)
18894             break;
18895           /* Otherwise, there must be more parameters.  Consume the
18896              `,'.  */
18897           cp_lexer_consume_token (parser->lexer);
18898           /* When parsing something like:
18899
18900                 int i(float f, double d)
18901
18902              we can tell after seeing the declaration for "f" that we
18903              are not looking at an initialization of a variable "i",
18904              but rather at the declaration of a function "i".
18905
18906              Due to the fact that the parsing of template arguments
18907              (as specified to a template-id) requires backtracking we
18908              cannot use this technique when inside a template argument
18909              list.  */
18910           if (!parser->in_template_argument_list_p
18911               && !parser->in_type_id_in_expr_p
18912               && cp_parser_uncommitted_to_tentative_parse_p (parser)
18913               /* However, a parameter-declaration of the form
18914                  "float(f)" (which is a valid declaration of a
18915                  parameter "f") can also be interpreted as an
18916                  expression (the conversion of "f" to "float").  */
18917               && !parenthesized_p)
18918             cp_parser_commit_to_tentative_parse (parser);
18919         }
18920       else
18921         {
18922           cp_parser_error (parser, "expected %<,%> or %<...%>");
18923           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18924             cp_parser_skip_to_closing_parenthesis (parser,
18925                                                    /*recovering=*/true,
18926                                                    /*or_comma=*/false,
18927                                                    /*consume_paren=*/false);
18928           break;
18929         }
18930     }
18931
18932   parser->in_unbraced_linkage_specification_p
18933     = saved_in_unbraced_linkage_specification_p;
18934
18935   /* Reset implicit_template_scope if we are about to leave the function
18936      parameter list that introduced it.  Note that for out-of-line member
18937      definitions, there will be one or more class scopes before we get to
18938      the template parameter scope.  */
18939
18940   if (cp_binding_level *its = parser->implicit_template_scope)
18941     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18942       {
18943         while (maybe_its->kind == sk_class)
18944           maybe_its = maybe_its->level_chain;
18945         if (maybe_its == its)
18946           {
18947             parser->implicit_template_parms = 0;
18948             parser->implicit_template_scope = 0;
18949           }
18950       }
18951
18952   return parameters;
18953 }
18954
18955 /* Parse a parameter declaration.
18956
18957    parameter-declaration:
18958      decl-specifier-seq ... [opt] declarator
18959      decl-specifier-seq declarator = assignment-expression
18960      decl-specifier-seq ... [opt] abstract-declarator [opt]
18961      decl-specifier-seq abstract-declarator [opt] = assignment-expression
18962
18963    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18964    declares a template parameter.  (In that case, a non-nested `>'
18965    token encountered during the parsing of the assignment-expression
18966    is not interpreted as a greater-than operator.)
18967
18968    Returns a representation of the parameter, or NULL if an error
18969    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18970    true iff the declarator is of the form "(p)".  */
18971
18972 static cp_parameter_declarator *
18973 cp_parser_parameter_declaration (cp_parser *parser,
18974                                  bool template_parm_p,
18975                                  bool *parenthesized_p)
18976 {
18977   int declares_class_or_enum;
18978   cp_decl_specifier_seq decl_specifiers;
18979   cp_declarator *declarator;
18980   tree default_argument;
18981   cp_token *token = NULL, *declarator_token_start = NULL;
18982   const char *saved_message;
18983
18984   /* In a template parameter, `>' is not an operator.
18985
18986      [temp.param]
18987
18988      When parsing a default template-argument for a non-type
18989      template-parameter, the first non-nested `>' is taken as the end
18990      of the template parameter-list rather than a greater-than
18991      operator.  */
18992
18993   /* Type definitions may not appear in parameter types.  */
18994   saved_message = parser->type_definition_forbidden_message;
18995   parser->type_definition_forbidden_message
18996     = G_("types may not be defined in parameter types");
18997
18998   /* Parse the declaration-specifiers.  */
18999   cp_parser_decl_specifier_seq (parser,
19000                                 CP_PARSER_FLAGS_NONE,
19001                                 &decl_specifiers,
19002                                 &declares_class_or_enum);
19003
19004   /* Complain about missing 'typename' or other invalid type names.  */
19005   if (!decl_specifiers.any_type_specifiers_p
19006       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19007     decl_specifiers.type = error_mark_node;
19008
19009   /* If an error occurred, there's no reason to attempt to parse the
19010      rest of the declaration.  */
19011   if (cp_parser_error_occurred (parser))
19012     {
19013       parser->type_definition_forbidden_message = saved_message;
19014       return NULL;
19015     }
19016
19017   /* Peek at the next token.  */
19018   token = cp_lexer_peek_token (parser->lexer);
19019
19020   /* If the next token is a `)', `,', `=', `>', or `...', then there
19021      is no declarator. However, when variadic templates are enabled,
19022      there may be a declarator following `...'.  */
19023   if (token->type == CPP_CLOSE_PAREN
19024       || token->type == CPP_COMMA
19025       || token->type == CPP_EQ
19026       || token->type == CPP_GREATER)
19027     {
19028       declarator = NULL;
19029       if (parenthesized_p)
19030         *parenthesized_p = false;
19031     }
19032   /* Otherwise, there should be a declarator.  */
19033   else
19034     {
19035       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19036       parser->default_arg_ok_p = false;
19037
19038       /* After seeing a decl-specifier-seq, if the next token is not a
19039          "(", there is no possibility that the code is a valid
19040          expression.  Therefore, if parsing tentatively, we commit at
19041          this point.  */
19042       if (!parser->in_template_argument_list_p
19043           /* In an expression context, having seen:
19044
19045                (int((char ...
19046
19047              we cannot be sure whether we are looking at a
19048              function-type (taking a "char" as a parameter) or a cast
19049              of some object of type "char" to "int".  */
19050           && !parser->in_type_id_in_expr_p
19051           && cp_parser_uncommitted_to_tentative_parse_p (parser)
19052           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19053           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19054         cp_parser_commit_to_tentative_parse (parser);
19055       /* Parse the declarator.  */
19056       declarator_token_start = token;
19057       declarator = cp_parser_declarator (parser,
19058                                          CP_PARSER_DECLARATOR_EITHER,
19059                                          /*ctor_dtor_or_conv_p=*/NULL,
19060                                          parenthesized_p,
19061                                          /*member_p=*/false,
19062                                          /*friend_p=*/false);
19063       parser->default_arg_ok_p = saved_default_arg_ok_p;
19064       /* After the declarator, allow more attributes.  */
19065       decl_specifiers.attributes
19066         = chainon (decl_specifiers.attributes,
19067                    cp_parser_attributes_opt (parser));
19068     }
19069
19070   /* If the next token is an ellipsis, and we have not seen a
19071      declarator name, and the type of the declarator contains parameter
19072      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19073      a parameter pack expansion expression. Otherwise, leave the
19074      ellipsis for a C-style variadic function. */
19075   token = cp_lexer_peek_token (parser->lexer);
19076   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19077     {
19078       tree type = decl_specifiers.type;
19079
19080       if (type && DECL_P (type))
19081         type = TREE_TYPE (type);
19082
19083       if (type
19084           && TREE_CODE (type) != TYPE_PACK_EXPANSION
19085           && declarator_can_be_parameter_pack (declarator)
19086           && (!declarator || !declarator->parameter_pack_p)
19087           && uses_parameter_packs (type))
19088         {
19089           /* Consume the `...'. */
19090           cp_lexer_consume_token (parser->lexer);
19091           maybe_warn_variadic_templates ();
19092           
19093           /* Build a pack expansion type */
19094           if (declarator)
19095             declarator->parameter_pack_p = true;
19096           else
19097             decl_specifiers.type = make_pack_expansion (type);
19098         }
19099     }
19100
19101   /* The restriction on defining new types applies only to the type
19102      of the parameter, not to the default argument.  */
19103   parser->type_definition_forbidden_message = saved_message;
19104
19105   /* If the next token is `=', then process a default argument.  */
19106   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19107     {
19108       token = cp_lexer_peek_token (parser->lexer);
19109       /* If we are defining a class, then the tokens that make up the
19110          default argument must be saved and processed later.  */
19111       if (!template_parm_p && at_class_scope_p ()
19112           && TYPE_BEING_DEFINED (current_class_type)
19113           && !LAMBDA_TYPE_P (current_class_type))
19114         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19115       /* Outside of a class definition, we can just parse the
19116          assignment-expression.  */
19117       else
19118         default_argument
19119           = cp_parser_default_argument (parser, template_parm_p);
19120
19121       if (!parser->default_arg_ok_p)
19122         {
19123           if (flag_permissive)
19124             warning (0, "deprecated use of default argument for parameter of non-function");
19125           else
19126             {
19127               error_at (token->location,
19128                         "default arguments are only "
19129                         "permitted for function parameters");
19130               default_argument = NULL_TREE;
19131             }
19132         }
19133       else if ((declarator && declarator->parameter_pack_p)
19134                || (decl_specifiers.type
19135                    && PACK_EXPANSION_P (decl_specifiers.type)))
19136         {
19137           /* Find the name of the parameter pack.  */     
19138           cp_declarator *id_declarator = declarator;
19139           while (id_declarator && id_declarator->kind != cdk_id)
19140             id_declarator = id_declarator->declarator;
19141           
19142           if (id_declarator && id_declarator->kind == cdk_id)
19143             error_at (declarator_token_start->location,
19144                       template_parm_p
19145                       ? G_("template parameter pack %qD "
19146                            "cannot have a default argument")
19147                       : G_("parameter pack %qD cannot have "
19148                            "a default argument"),
19149                       id_declarator->u.id.unqualified_name);
19150           else
19151             error_at (declarator_token_start->location,
19152                       template_parm_p
19153                       ? G_("template parameter pack cannot have "
19154                            "a default argument")
19155                       : G_("parameter pack cannot have a "
19156                            "default argument"));
19157
19158           default_argument = NULL_TREE;
19159         }
19160     }
19161   else
19162     default_argument = NULL_TREE;
19163
19164   return make_parameter_declarator (&decl_specifiers,
19165                                     declarator,
19166                                     default_argument);
19167 }
19168
19169 /* Parse a default argument and return it.
19170
19171    TEMPLATE_PARM_P is true if this is a default argument for a
19172    non-type template parameter.  */
19173 static tree
19174 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19175 {
19176   tree default_argument = NULL_TREE;
19177   bool saved_greater_than_is_operator_p;
19178   bool saved_local_variables_forbidden_p;
19179   bool non_constant_p, is_direct_init;
19180
19181   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19182      set correctly.  */
19183   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19184   parser->greater_than_is_operator_p = !template_parm_p;
19185   /* Local variable names (and the `this' keyword) may not
19186      appear in a default argument.  */
19187   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19188   parser->local_variables_forbidden_p = true;
19189   /* Parse the assignment-expression.  */
19190   if (template_parm_p)
19191     push_deferring_access_checks (dk_no_deferred);
19192   tree saved_class_ptr = NULL_TREE;
19193   tree saved_class_ref = NULL_TREE;
19194   /* The "this" pointer is not valid in a default argument.  */
19195   if (cfun)
19196     {
19197       saved_class_ptr = current_class_ptr;
19198       cp_function_chain->x_current_class_ptr = NULL_TREE;
19199       saved_class_ref = current_class_ref;
19200       cp_function_chain->x_current_class_ref = NULL_TREE;
19201     }
19202   default_argument
19203     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19204   /* Restore the "this" pointer.  */
19205   if (cfun)
19206     {
19207       cp_function_chain->x_current_class_ptr = saved_class_ptr;
19208       cp_function_chain->x_current_class_ref = saved_class_ref;
19209     }
19210   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19211     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19212   if (template_parm_p)
19213     pop_deferring_access_checks ();
19214   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19215   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19216
19217   return default_argument;
19218 }
19219
19220 /* Parse a function-body.
19221
19222    function-body:
19223      compound_statement  */
19224
19225 static void
19226 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19227 {
19228   cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19229 }
19230
19231 /* Parse a ctor-initializer-opt followed by a function-body.  Return
19232    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
19233    is true we are parsing a function-try-block.  */
19234
19235 static bool
19236 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19237                                                   bool in_function_try_block)
19238 {
19239   tree body, list;
19240   bool ctor_initializer_p;
19241   const bool check_body_p =
19242      DECL_CONSTRUCTOR_P (current_function_decl)
19243      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19244   tree last = NULL;
19245
19246   /* Begin the function body.  */
19247   body = begin_function_body ();
19248   /* Parse the optional ctor-initializer.  */
19249   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19250
19251   /* If we're parsing a constexpr constructor definition, we need
19252      to check that the constructor body is indeed empty.  However,
19253      before we get to cp_parser_function_body lot of junk has been
19254      generated, so we can't just check that we have an empty block.
19255      Rather we take a snapshot of the outermost block, and check whether
19256      cp_parser_function_body changed its state.  */
19257   if (check_body_p)
19258     {
19259       list = cur_stmt_list;
19260       if (STATEMENT_LIST_TAIL (list))
19261         last = STATEMENT_LIST_TAIL (list)->stmt;
19262     }
19263   /* Parse the function-body.  */
19264   cp_parser_function_body (parser, in_function_try_block);
19265   if (check_body_p)
19266     check_constexpr_ctor_body (last, list, /*complain=*/true);
19267   /* Finish the function body.  */
19268   finish_function_body (body);
19269
19270   return ctor_initializer_p;
19271 }
19272
19273 /* Parse an initializer.
19274
19275    initializer:
19276      = initializer-clause
19277      ( expression-list )
19278
19279    Returns an expression representing the initializer.  If no
19280    initializer is present, NULL_TREE is returned.
19281
19282    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19283    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
19284    set to TRUE if there is no initializer present.  If there is an
19285    initializer, and it is not a constant-expression, *NON_CONSTANT_P
19286    is set to true; otherwise it is set to false.  */
19287
19288 static tree
19289 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19290                        bool* non_constant_p)
19291 {
19292   cp_token *token;
19293   tree init;
19294
19295   /* Peek at the next token.  */
19296   token = cp_lexer_peek_token (parser->lexer);
19297
19298   /* Let our caller know whether or not this initializer was
19299      parenthesized.  */
19300   *is_direct_init = (token->type != CPP_EQ);
19301   /* Assume that the initializer is constant.  */
19302   *non_constant_p = false;
19303
19304   if (token->type == CPP_EQ)
19305     {
19306       /* Consume the `='.  */
19307       cp_lexer_consume_token (parser->lexer);
19308       /* Parse the initializer-clause.  */
19309       init = cp_parser_initializer_clause (parser, non_constant_p);
19310     }
19311   else if (token->type == CPP_OPEN_PAREN)
19312     {
19313       vec<tree, va_gc> *vec;
19314       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19315                                                      /*cast_p=*/false,
19316                                                      /*allow_expansion_p=*/true,
19317                                                      non_constant_p);
19318       if (vec == NULL)
19319         return error_mark_node;
19320       init = build_tree_list_vec (vec);
19321       release_tree_vector (vec);
19322     }
19323   else if (token->type == CPP_OPEN_BRACE)
19324     {
19325       cp_lexer_set_source_position (parser->lexer);
19326       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19327       init = cp_parser_braced_list (parser, non_constant_p);
19328       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19329     }
19330   else
19331     {
19332       /* Anything else is an error.  */
19333       cp_parser_error (parser, "expected initializer");
19334       init = error_mark_node;
19335     }
19336
19337   return init;
19338 }
19339
19340 /* Parse an initializer-clause.
19341
19342    initializer-clause:
19343      assignment-expression
19344      braced-init-list
19345
19346    Returns an expression representing the initializer.
19347
19348    If the `assignment-expression' production is used the value
19349    returned is simply a representation for the expression.
19350
19351    Otherwise, calls cp_parser_braced_list.  */
19352
19353 static tree
19354 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19355 {
19356   tree initializer;
19357
19358   /* Assume the expression is constant.  */
19359   *non_constant_p = false;
19360
19361   /* If it is not a `{', then we are looking at an
19362      assignment-expression.  */
19363   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19364     {
19365       initializer
19366         = cp_parser_constant_expression (parser,
19367                                         /*allow_non_constant_p=*/true,
19368                                         non_constant_p);
19369     }
19370   else
19371     initializer = cp_parser_braced_list (parser, non_constant_p);
19372
19373   return initializer;
19374 }
19375
19376 /* Parse a brace-enclosed initializer list.
19377
19378    braced-init-list:
19379      { initializer-list , [opt] }
19380      { }
19381
19382    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
19383    the elements of the initializer-list (or NULL, if the last
19384    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
19385    NULL_TREE.  There is no way to detect whether or not the optional
19386    trailing `,' was provided.  NON_CONSTANT_P is as for
19387    cp_parser_initializer.  */     
19388
19389 static tree
19390 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19391 {
19392   tree initializer;
19393
19394   /* Consume the `{' token.  */
19395   cp_lexer_consume_token (parser->lexer);
19396   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
19397   initializer = make_node (CONSTRUCTOR);
19398   /* If it's not a `}', then there is a non-trivial initializer.  */
19399   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19400     {
19401       /* Parse the initializer list.  */
19402       CONSTRUCTOR_ELTS (initializer)
19403         = cp_parser_initializer_list (parser, non_constant_p);
19404       /* A trailing `,' token is allowed.  */
19405       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19406         cp_lexer_consume_token (parser->lexer);
19407     }
19408   else
19409     *non_constant_p = false;
19410   /* Now, there should be a trailing `}'.  */
19411   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19412   TREE_TYPE (initializer) = init_list_type_node;
19413   return initializer;
19414 }
19415
19416 /* Consume tokens up to, and including, the next non-nested closing `]'.
19417    Returns true iff we found a closing `]'.  */
19418
19419 static bool
19420 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19421 {
19422   unsigned square_depth = 0;
19423
19424   while (true)
19425     {
19426       cp_token * token = cp_lexer_peek_token (parser->lexer);
19427
19428       switch (token->type)
19429         {
19430         case CPP_EOF:
19431         case CPP_PRAGMA_EOL:
19432           /* If we've run out of tokens, then there is no closing `]'.  */
19433           return false;
19434
19435         case CPP_OPEN_SQUARE:
19436           ++square_depth;
19437           break;
19438
19439         case CPP_CLOSE_SQUARE:
19440           if (!square_depth--)
19441             {
19442               cp_lexer_consume_token (parser->lexer);
19443               return true;
19444             }
19445           break;
19446
19447         default:
19448           break;
19449         }
19450
19451       /* Consume the token.  */
19452       cp_lexer_consume_token (parser->lexer);
19453     }
19454 }
19455
19456 /* Return true if we are looking at an array-designator, false otherwise.  */
19457
19458 static bool
19459 cp_parser_array_designator_p (cp_parser *parser)
19460 {
19461   /* Consume the `['.  */
19462   cp_lexer_consume_token (parser->lexer);
19463
19464   cp_lexer_save_tokens (parser->lexer);
19465
19466   /* Skip tokens until the next token is a closing square bracket.
19467      If we find the closing `]', and the next token is a `=', then
19468      we are looking at an array designator.  */
19469   bool array_designator_p
19470     = (cp_parser_skip_to_closing_square_bracket (parser)
19471        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19472   
19473   /* Roll back the tokens we skipped.  */
19474   cp_lexer_rollback_tokens (parser->lexer);
19475
19476   return array_designator_p;
19477 }
19478
19479 /* Parse an initializer-list.
19480
19481    initializer-list:
19482      initializer-clause ... [opt]
19483      initializer-list , initializer-clause ... [opt]
19484
19485    GNU Extension:
19486
19487    initializer-list:
19488      designation initializer-clause ...[opt]
19489      initializer-list , designation initializer-clause ...[opt]
19490
19491    designation:
19492      . identifier =
19493      identifier :
19494      [ constant-expression ] =
19495
19496    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
19497    for the initializer.  If the INDEX of the elt is non-NULL, it is the
19498    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
19499    as for cp_parser_initializer.  */
19500
19501 static vec<constructor_elt, va_gc> *
19502 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19503 {
19504   vec<constructor_elt, va_gc> *v = NULL;
19505
19506   /* Assume all of the expressions are constant.  */
19507   *non_constant_p = false;
19508
19509   /* Parse the rest of the list.  */
19510   while (true)
19511     {
19512       cp_token *token;
19513       tree designator;
19514       tree initializer;
19515       bool clause_non_constant_p;
19516
19517       /* If the next token is an identifier and the following one is a
19518          colon, we are looking at the GNU designated-initializer
19519          syntax.  */
19520       if (cp_parser_allow_gnu_extensions_p (parser)
19521           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19522           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19523         {
19524           /* Warn the user that they are using an extension.  */
19525           pedwarn (input_location, OPT_Wpedantic, 
19526                    "ISO C++ does not allow designated initializers");
19527           /* Consume the identifier.  */
19528           designator = cp_lexer_consume_token (parser->lexer)->u.value;
19529           /* Consume the `:'.  */
19530           cp_lexer_consume_token (parser->lexer);
19531         }
19532       /* Also handle the C99 syntax, '. id ='.  */
19533       else if (cp_parser_allow_gnu_extensions_p (parser)
19534                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19535                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19536                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19537         {
19538           /* Warn the user that they are using an extension.  */
19539           pedwarn (input_location, OPT_Wpedantic,
19540                    "ISO C++ does not allow C99 designated initializers");
19541           /* Consume the `.'.  */
19542           cp_lexer_consume_token (parser->lexer);
19543           /* Consume the identifier.  */
19544           designator = cp_lexer_consume_token (parser->lexer)->u.value;
19545           /* Consume the `='.  */
19546           cp_lexer_consume_token (parser->lexer);
19547         }
19548       /* Also handle C99 array designators, '[ const ] ='.  */
19549       else if (cp_parser_allow_gnu_extensions_p (parser)
19550                && !c_dialect_objc ()
19551                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19552         {
19553           /* In C++11, [ could start a lambda-introducer.  */
19554           bool non_const = false;
19555
19556           cp_parser_parse_tentatively (parser);
19557
19558           if (!cp_parser_array_designator_p (parser))
19559             {
19560               cp_parser_simulate_error (parser);
19561               designator = NULL_TREE;
19562             }
19563           else
19564             {
19565               designator = cp_parser_constant_expression (parser, true,
19566                                                           &non_const);
19567               cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19568               cp_parser_require (parser, CPP_EQ, RT_EQ);
19569             }
19570
19571           if (!cp_parser_parse_definitely (parser))
19572             designator = NULL_TREE;
19573           else if (non_const)
19574             require_potential_rvalue_constant_expression (designator);
19575         }
19576       else
19577         designator = NULL_TREE;
19578
19579       /* Parse the initializer.  */
19580       initializer = cp_parser_initializer_clause (parser,
19581                                                   &clause_non_constant_p);
19582       /* If any clause is non-constant, so is the entire initializer.  */
19583       if (clause_non_constant_p)
19584         *non_constant_p = true;
19585
19586       /* If we have an ellipsis, this is an initializer pack
19587          expansion.  */
19588       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19589         {
19590           /* Consume the `...'.  */
19591           cp_lexer_consume_token (parser->lexer);
19592
19593           /* Turn the initializer into an initializer expansion.  */
19594           initializer = make_pack_expansion (initializer);
19595         }
19596
19597       /* Add it to the vector.  */
19598       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19599
19600       /* If the next token is not a comma, we have reached the end of
19601          the list.  */
19602       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19603         break;
19604
19605       /* Peek at the next token.  */
19606       token = cp_lexer_peek_nth_token (parser->lexer, 2);
19607       /* If the next token is a `}', then we're still done.  An
19608          initializer-clause can have a trailing `,' after the
19609          initializer-list and before the closing `}'.  */
19610       if (token->type == CPP_CLOSE_BRACE)
19611         break;
19612
19613       /* Consume the `,' token.  */
19614       cp_lexer_consume_token (parser->lexer);
19615     }
19616
19617   return v;
19618 }
19619
19620 /* Classes [gram.class] */
19621
19622 /* Parse a class-name.
19623
19624    class-name:
19625      identifier
19626      template-id
19627
19628    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19629    to indicate that names looked up in dependent types should be
19630    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
19631    keyword has been used to indicate that the name that appears next
19632    is a template.  TAG_TYPE indicates the explicit tag given before
19633    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
19634    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
19635    is the class being defined in a class-head.
19636
19637    Returns the TYPE_DECL representing the class.  */
19638
19639 static tree
19640 cp_parser_class_name (cp_parser *parser,
19641                       bool typename_keyword_p,
19642                       bool template_keyword_p,
19643                       enum tag_types tag_type,
19644                       bool check_dependency_p,
19645                       bool class_head_p,
19646                       bool is_declaration)
19647 {
19648   tree decl;
19649   tree scope;
19650   bool typename_p;
19651   cp_token *token;
19652   tree identifier = NULL_TREE;
19653
19654   /* All class-names start with an identifier.  */
19655   token = cp_lexer_peek_token (parser->lexer);
19656   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19657     {
19658       cp_parser_error (parser, "expected class-name");
19659       return error_mark_node;
19660     }
19661
19662   /* PARSER->SCOPE can be cleared when parsing the template-arguments
19663      to a template-id, so we save it here.  */
19664   scope = parser->scope;
19665   if (scope == error_mark_node)
19666     return error_mark_node;
19667
19668   /* Any name names a type if we're following the `typename' keyword
19669      in a qualified name where the enclosing scope is type-dependent.  */
19670   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19671                 && dependent_type_p (scope));
19672   /* Handle the common case (an identifier, but not a template-id)
19673      efficiently.  */
19674   if (token->type == CPP_NAME
19675       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19676     {
19677       cp_token *identifier_token;
19678       bool ambiguous_p;
19679
19680       /* Look for the identifier.  */
19681       identifier_token = cp_lexer_peek_token (parser->lexer);
19682       ambiguous_p = identifier_token->error_reported;
19683       identifier = cp_parser_identifier (parser);
19684       /* If the next token isn't an identifier, we are certainly not
19685          looking at a class-name.  */
19686       if (identifier == error_mark_node)
19687         decl = error_mark_node;
19688       /* If we know this is a type-name, there's no need to look it
19689          up.  */
19690       else if (typename_p)
19691         decl = identifier;
19692       else
19693         {
19694           tree ambiguous_decls;
19695           /* If we already know that this lookup is ambiguous, then
19696              we've already issued an error message; there's no reason
19697              to check again.  */
19698           if (ambiguous_p)
19699             {
19700               cp_parser_simulate_error (parser);
19701               return error_mark_node;
19702             }
19703           /* If the next token is a `::', then the name must be a type
19704              name.
19705
19706              [basic.lookup.qual]
19707
19708              During the lookup for a name preceding the :: scope
19709              resolution operator, object, function, and enumerator
19710              names are ignored.  */
19711           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19712             tag_type = typename_type;
19713           /* Look up the name.  */
19714           decl = cp_parser_lookup_name (parser, identifier,
19715                                         tag_type,
19716                                         /*is_template=*/false,
19717                                         /*is_namespace=*/false,
19718                                         check_dependency_p,
19719                                         &ambiguous_decls,
19720                                         identifier_token->location);
19721           if (ambiguous_decls)
19722             {
19723               if (cp_parser_parsing_tentatively (parser))
19724                 cp_parser_simulate_error (parser);
19725               return error_mark_node;
19726             }
19727         }
19728     }
19729   else
19730     {
19731       /* Try a template-id.  */
19732       decl = cp_parser_template_id (parser, template_keyword_p,
19733                                     check_dependency_p,
19734                                     tag_type,
19735                                     is_declaration);
19736       if (decl == error_mark_node)
19737         return error_mark_node;
19738     }
19739
19740   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19741
19742   /* If this is a typename, create a TYPENAME_TYPE.  */
19743   if (typename_p && decl != error_mark_node)
19744     {
19745       decl = make_typename_type (scope, decl, typename_type,
19746                                  /*complain=*/tf_error);
19747       if (decl != error_mark_node)
19748         decl = TYPE_NAME (decl);
19749     }
19750
19751   decl = strip_using_decl (decl);
19752
19753   /* Check to see that it is really the name of a class.  */
19754   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19755       && identifier_p (TREE_OPERAND (decl, 0))
19756       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19757     /* Situations like this:
19758
19759          template <typename T> struct A {
19760            typename T::template X<int>::I i;
19761          };
19762
19763        are problematic.  Is `T::template X<int>' a class-name?  The
19764        standard does not seem to be definitive, but there is no other
19765        valid interpretation of the following `::'.  Therefore, those
19766        names are considered class-names.  */
19767     {
19768       decl = make_typename_type (scope, decl, tag_type, tf_error);
19769       if (decl != error_mark_node)
19770         decl = TYPE_NAME (decl);
19771     }
19772   else if (TREE_CODE (decl) != TYPE_DECL
19773            || TREE_TYPE (decl) == error_mark_node
19774            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19775            /* In Objective-C 2.0, a classname followed by '.' starts a
19776               dot-syntax expression, and it's not a type-name.  */
19777            || (c_dialect_objc ()
19778                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
19779                && objc_is_class_name (decl)))
19780     decl = error_mark_node;
19781
19782   if (decl == error_mark_node)
19783     cp_parser_error (parser, "expected class-name");
19784   else if (identifier && !parser->scope)
19785     maybe_note_name_used_in_class (identifier, decl);
19786
19787   return decl;
19788 }
19789
19790 /* Parse a class-specifier.
19791
19792    class-specifier:
19793      class-head { member-specification [opt] }
19794
19795    Returns the TREE_TYPE representing the class.  */
19796
19797 static tree
19798 cp_parser_class_specifier_1 (cp_parser* parser)
19799 {
19800   tree type;
19801   tree attributes = NULL_TREE;
19802   bool nested_name_specifier_p;
19803   unsigned saved_num_template_parameter_lists;
19804   bool saved_in_function_body;
19805   unsigned char in_statement;
19806   bool in_switch_statement_p;
19807   bool saved_in_unbraced_linkage_specification_p;
19808   tree old_scope = NULL_TREE;
19809   tree scope = NULL_TREE;
19810   cp_token *closing_brace;
19811
19812   push_deferring_access_checks (dk_no_deferred);
19813
19814   /* Parse the class-head.  */
19815   type = cp_parser_class_head (parser,
19816                                &nested_name_specifier_p);
19817   /* If the class-head was a semantic disaster, skip the entire body
19818      of the class.  */
19819   if (!type)
19820     {
19821       cp_parser_skip_to_end_of_block_or_statement (parser);
19822       pop_deferring_access_checks ();
19823       return error_mark_node;
19824     }
19825
19826   /* Look for the `{'.  */
19827   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19828     {
19829       pop_deferring_access_checks ();
19830       return error_mark_node;
19831     }
19832
19833   cp_ensure_no_omp_declare_simd (parser);
19834
19835   /* Issue an error message if type-definitions are forbidden here.  */
19836   cp_parser_check_type_definition (parser);
19837   /* Remember that we are defining one more class.  */
19838   ++parser->num_classes_being_defined;
19839   /* Inside the class, surrounding template-parameter-lists do not
19840      apply.  */
19841   saved_num_template_parameter_lists
19842     = parser->num_template_parameter_lists;
19843   parser->num_template_parameter_lists = 0;
19844   /* We are not in a function body.  */
19845   saved_in_function_body = parser->in_function_body;
19846   parser->in_function_body = false;
19847   /* Or in a loop.  */
19848   in_statement = parser->in_statement;
19849   parser->in_statement = 0;
19850   /* Or in a switch.  */
19851   in_switch_statement_p = parser->in_switch_statement_p;
19852   parser->in_switch_statement_p = false;
19853   /* We are not immediately inside an extern "lang" block.  */
19854   saved_in_unbraced_linkage_specification_p
19855     = parser->in_unbraced_linkage_specification_p;
19856   parser->in_unbraced_linkage_specification_p = false;
19857
19858   /* Start the class.  */
19859   if (nested_name_specifier_p)
19860     {
19861       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19862       old_scope = push_inner_scope (scope);
19863     }
19864   type = begin_class_definition (type);
19865
19866   if (type == error_mark_node)
19867     /* If the type is erroneous, skip the entire body of the class.  */
19868     cp_parser_skip_to_closing_brace (parser);
19869   else
19870     /* Parse the member-specification.  */
19871     cp_parser_member_specification_opt (parser);
19872
19873   /* Look for the trailing `}'.  */
19874   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19875   /* Look for trailing attributes to apply to this class.  */
19876   if (cp_parser_allow_gnu_extensions_p (parser))
19877     attributes = cp_parser_gnu_attributes_opt (parser);
19878   if (type != error_mark_node)
19879     type = finish_struct (type, attributes);
19880   if (nested_name_specifier_p)
19881     pop_inner_scope (old_scope, scope);
19882
19883   /* We've finished a type definition.  Check for the common syntax
19884      error of forgetting a semicolon after the definition.  We need to
19885      be careful, as we can't just check for not-a-semicolon and be done
19886      with it; the user might have typed:
19887
19888      class X { } c = ...;
19889      class X { } *p = ...;
19890
19891      and so forth.  Instead, enumerate all the possible tokens that
19892      might follow this production; if we don't see one of them, then
19893      complain and silently insert the semicolon.  */
19894   {
19895     cp_token *token = cp_lexer_peek_token (parser->lexer);
19896     bool want_semicolon = true;
19897
19898     if (cp_next_tokens_can_be_std_attribute_p (parser))
19899       /* Don't try to parse c++11 attributes here.  As per the
19900          grammar, that should be a task for
19901          cp_parser_decl_specifier_seq.  */
19902       want_semicolon = false;
19903
19904     switch (token->type)
19905       {
19906       case CPP_NAME:
19907       case CPP_SEMICOLON:
19908       case CPP_MULT:
19909       case CPP_AND:
19910       case CPP_OPEN_PAREN:
19911       case CPP_CLOSE_PAREN:
19912       case CPP_COMMA:
19913         want_semicolon = false;
19914         break;
19915
19916         /* While it's legal for type qualifiers and storage class
19917            specifiers to follow type definitions in the grammar, only
19918            compiler testsuites contain code like that.  Assume that if
19919            we see such code, then what we're really seeing is a case
19920            like:
19921
19922            class X { }
19923            const <type> var = ...;
19924
19925            or
19926
19927            class Y { }
19928            static <type> func (...) ...
19929
19930            i.e. the qualifier or specifier applies to the next
19931            declaration.  To do so, however, we need to look ahead one
19932            more token to see if *that* token is a type specifier.
19933
19934            This code could be improved to handle:
19935
19936            class Z { }
19937            static const <type> var = ...;  */
19938       case CPP_KEYWORD:
19939         if (keyword_is_decl_specifier (token->keyword))
19940           {
19941             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19942
19943             /* Handling user-defined types here would be nice, but very
19944                tricky.  */
19945             want_semicolon
19946               = (lookahead->type == CPP_KEYWORD
19947                  && keyword_begins_type_specifier (lookahead->keyword));
19948           }
19949         break;
19950       default:
19951         break;
19952       }
19953
19954     /* If we don't have a type, then something is very wrong and we
19955        shouldn't try to do anything clever.  Likewise for not seeing the
19956        closing brace.  */
19957     if (closing_brace && TYPE_P (type) && want_semicolon)
19958       {
19959         cp_token_position prev
19960           = cp_lexer_previous_token_position (parser->lexer);
19961         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19962         location_t loc = prev_token->location;
19963
19964         if (CLASSTYPE_DECLARED_CLASS (type))
19965           error_at (loc, "expected %<;%> after class definition");
19966         else if (TREE_CODE (type) == RECORD_TYPE)
19967           error_at (loc, "expected %<;%> after struct definition");
19968         else if (TREE_CODE (type) == UNION_TYPE)
19969           error_at (loc, "expected %<;%> after union definition");
19970         else
19971           gcc_unreachable ();
19972
19973         /* Unget one token and smash it to look as though we encountered
19974            a semicolon in the input stream.  */
19975         cp_lexer_set_token_position (parser->lexer, prev);
19976         token = cp_lexer_peek_token (parser->lexer);
19977         token->type = CPP_SEMICOLON;
19978         token->keyword = RID_MAX;
19979       }
19980   }
19981
19982   /* If this class is not itself within the scope of another class,
19983      then we need to parse the bodies of all of the queued function
19984      definitions.  Note that the queued functions defined in a class
19985      are not always processed immediately following the
19986      class-specifier for that class.  Consider:
19987
19988        struct A {
19989          struct B { void f() { sizeof (A); } };
19990        };
19991
19992      If `f' were processed before the processing of `A' were
19993      completed, there would be no way to compute the size of `A'.
19994      Note that the nesting we are interested in here is lexical --
19995      not the semantic nesting given by TYPE_CONTEXT.  In particular,
19996      for:
19997
19998        struct A { struct B; };
19999        struct A::B { void f() { } };
20000
20001      there is no need to delay the parsing of `A::B::f'.  */
20002   if (--parser->num_classes_being_defined == 0)
20003     {
20004       tree decl;
20005       tree class_type = NULL_TREE;
20006       tree pushed_scope = NULL_TREE;
20007       unsigned ix;
20008       cp_default_arg_entry *e;
20009       tree save_ccp, save_ccr;
20010
20011       /* In a first pass, parse default arguments to the functions.
20012          Then, in a second pass, parse the bodies of the functions.
20013          This two-phased approach handles cases like:
20014
20015             struct S {
20016               void f() { g(); }
20017               void g(int i = 3);
20018             };
20019
20020          */
20021       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20022         {
20023           decl = e->decl;
20024           /* If there are default arguments that have not yet been processed,
20025              take care of them now.  */
20026           if (class_type != e->class_type)
20027             {
20028               if (pushed_scope)
20029                 pop_scope (pushed_scope);
20030               class_type = e->class_type;
20031               pushed_scope = push_scope (class_type);
20032             }
20033           /* Make sure that any template parameters are in scope.  */
20034           maybe_begin_member_template_processing (decl);
20035           /* Parse the default argument expressions.  */
20036           cp_parser_late_parsing_default_args (parser, decl);
20037           /* Remove any template parameters from the symbol table.  */
20038           maybe_end_member_template_processing ();
20039         }
20040       vec_safe_truncate (unparsed_funs_with_default_args, 0);
20041       /* Now parse any NSDMIs.  */
20042       save_ccp = current_class_ptr;
20043       save_ccr = current_class_ref;
20044       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20045         {
20046           if (class_type != DECL_CONTEXT (decl))
20047             {
20048               if (pushed_scope)
20049                 pop_scope (pushed_scope);
20050               class_type = DECL_CONTEXT (decl);
20051               pushed_scope = push_scope (class_type);
20052             }
20053           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20054           cp_parser_late_parsing_nsdmi (parser, decl);
20055         }
20056       vec_safe_truncate (unparsed_nsdmis, 0);
20057       current_class_ptr = save_ccp;
20058       current_class_ref = save_ccr;
20059       if (pushed_scope)
20060         pop_scope (pushed_scope);
20061
20062       /* Now do some post-NSDMI bookkeeping.  */
20063       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20064         after_nsdmi_defaulted_late_checks (class_type);
20065       vec_safe_truncate (unparsed_classes, 0);
20066       after_nsdmi_defaulted_late_checks (type);
20067
20068       /* Now parse the body of the functions.  */
20069       if (flag_openmp)
20070         {
20071           /* OpenMP UDRs need to be parsed before all other functions.  */
20072           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20073             if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20074               cp_parser_late_parsing_for_member (parser, decl);
20075           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20076             if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20077               cp_parser_late_parsing_for_member (parser, decl);
20078         }
20079       else
20080         FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20081           cp_parser_late_parsing_for_member (parser, decl);
20082       vec_safe_truncate (unparsed_funs_with_definitions, 0);
20083     }
20084   else
20085     vec_safe_push (unparsed_classes, type);
20086
20087   /* Put back any saved access checks.  */
20088   pop_deferring_access_checks ();
20089
20090   /* Restore saved state.  */
20091   parser->in_switch_statement_p = in_switch_statement_p;
20092   parser->in_statement = in_statement;
20093   parser->in_function_body = saved_in_function_body;
20094   parser->num_template_parameter_lists
20095     = saved_num_template_parameter_lists;
20096   parser->in_unbraced_linkage_specification_p
20097     = saved_in_unbraced_linkage_specification_p;
20098
20099   return type;
20100 }
20101
20102 static tree
20103 cp_parser_class_specifier (cp_parser* parser)
20104 {
20105   tree ret;
20106   timevar_push (TV_PARSE_STRUCT);
20107   ret = cp_parser_class_specifier_1 (parser);
20108   timevar_pop (TV_PARSE_STRUCT);
20109   return ret;
20110 }
20111
20112 /* Parse a class-head.
20113
20114    class-head:
20115      class-key identifier [opt] base-clause [opt]
20116      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20117      class-key nested-name-specifier [opt] template-id
20118        base-clause [opt]
20119
20120    class-virt-specifier:
20121      final
20122
20123    GNU Extensions:
20124      class-key attributes identifier [opt] base-clause [opt]
20125      class-key attributes nested-name-specifier identifier base-clause [opt]
20126      class-key attributes nested-name-specifier [opt] template-id
20127        base-clause [opt]
20128
20129    Upon return BASES is initialized to the list of base classes (or
20130    NULL, if there are none) in the same form returned by
20131    cp_parser_base_clause.
20132
20133    Returns the TYPE of the indicated class.  Sets
20134    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20135    involving a nested-name-specifier was used, and FALSE otherwise.
20136
20137    Returns error_mark_node if this is not a class-head.
20138
20139    Returns NULL_TREE if the class-head is syntactically valid, but
20140    semantically invalid in a way that means we should skip the entire
20141    body of the class.  */
20142
20143 static tree
20144 cp_parser_class_head (cp_parser* parser,
20145                       bool* nested_name_specifier_p)
20146 {
20147   tree nested_name_specifier;
20148   enum tag_types class_key;
20149   tree id = NULL_TREE;
20150   tree type = NULL_TREE;
20151   tree attributes;
20152   tree bases;
20153   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20154   bool template_id_p = false;
20155   bool qualified_p = false;
20156   bool invalid_nested_name_p = false;
20157   bool invalid_explicit_specialization_p = false;
20158   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20159   tree pushed_scope = NULL_TREE;
20160   unsigned num_templates;
20161   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20162   /* Assume no nested-name-specifier will be present.  */
20163   *nested_name_specifier_p = false;
20164   /* Assume no template parameter lists will be used in defining the
20165      type.  */
20166   num_templates = 0;
20167   parser->colon_corrects_to_scope_p = false;
20168
20169   /* Look for the class-key.  */
20170   class_key = cp_parser_class_key (parser);
20171   if (class_key == none_type)
20172     return error_mark_node;
20173
20174   /* Parse the attributes.  */
20175   attributes = cp_parser_attributes_opt (parser);
20176
20177   /* If the next token is `::', that is invalid -- but sometimes
20178      people do try to write:
20179
20180        struct ::S {};
20181
20182      Handle this gracefully by accepting the extra qualifier, and then
20183      issuing an error about it later if this really is a
20184      class-head.  If it turns out just to be an elaborated type
20185      specifier, remain silent.  */
20186   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20187     qualified_p = true;
20188
20189   push_deferring_access_checks (dk_no_check);
20190
20191   /* Determine the name of the class.  Begin by looking for an
20192      optional nested-name-specifier.  */
20193   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20194   nested_name_specifier
20195     = cp_parser_nested_name_specifier_opt (parser,
20196                                            /*typename_keyword_p=*/false,
20197                                            /*check_dependency_p=*/false,
20198                                            /*type_p=*/true,
20199                                            /*is_declaration=*/false);
20200   /* If there was a nested-name-specifier, then there *must* be an
20201      identifier.  */
20202   if (nested_name_specifier)
20203     {
20204       type_start_token = cp_lexer_peek_token (parser->lexer);
20205       /* Although the grammar says `identifier', it really means
20206          `class-name' or `template-name'.  You are only allowed to
20207          define a class that has already been declared with this
20208          syntax.
20209
20210          The proposed resolution for Core Issue 180 says that wherever
20211          you see `class T::X' you should treat `X' as a type-name.
20212
20213          It is OK to define an inaccessible class; for example:
20214
20215            class A { class B; };
20216            class A::B {};
20217
20218          We do not know if we will see a class-name, or a
20219          template-name.  We look for a class-name first, in case the
20220          class-name is a template-id; if we looked for the
20221          template-name first we would stop after the template-name.  */
20222       cp_parser_parse_tentatively (parser);
20223       type = cp_parser_class_name (parser,
20224                                    /*typename_keyword_p=*/false,
20225                                    /*template_keyword_p=*/false,
20226                                    class_type,
20227                                    /*check_dependency_p=*/false,
20228                                    /*class_head_p=*/true,
20229                                    /*is_declaration=*/false);
20230       /* If that didn't work, ignore the nested-name-specifier.  */
20231       if (!cp_parser_parse_definitely (parser))
20232         {
20233           invalid_nested_name_p = true;
20234           type_start_token = cp_lexer_peek_token (parser->lexer);
20235           id = cp_parser_identifier (parser);
20236           if (id == error_mark_node)
20237             id = NULL_TREE;
20238         }
20239       /* If we could not find a corresponding TYPE, treat this
20240          declaration like an unqualified declaration.  */
20241       if (type == error_mark_node)
20242         nested_name_specifier = NULL_TREE;
20243       /* Otherwise, count the number of templates used in TYPE and its
20244          containing scopes.  */
20245       else
20246         {
20247           tree scope;
20248
20249           for (scope = TREE_TYPE (type);
20250                scope && TREE_CODE (scope) != NAMESPACE_DECL;
20251                scope = get_containing_scope (scope))
20252             if (TYPE_P (scope)
20253                 && CLASS_TYPE_P (scope)
20254                 && CLASSTYPE_TEMPLATE_INFO (scope)
20255                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20256                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20257                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20258               ++num_templates;
20259         }
20260     }
20261   /* Otherwise, the identifier is optional.  */
20262   else
20263     {
20264       /* We don't know whether what comes next is a template-id,
20265          an identifier, or nothing at all.  */
20266       cp_parser_parse_tentatively (parser);
20267       /* Check for a template-id.  */
20268       type_start_token = cp_lexer_peek_token (parser->lexer);
20269       id = cp_parser_template_id (parser,
20270                                   /*template_keyword_p=*/false,
20271                                   /*check_dependency_p=*/true,
20272                                   class_key,
20273                                   /*is_declaration=*/true);
20274       /* If that didn't work, it could still be an identifier.  */
20275       if (!cp_parser_parse_definitely (parser))
20276         {
20277           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20278             {
20279               type_start_token = cp_lexer_peek_token (parser->lexer);
20280               id = cp_parser_identifier (parser);
20281             }
20282           else
20283             id = NULL_TREE;
20284         }
20285       else
20286         {
20287           template_id_p = true;
20288           ++num_templates;
20289         }
20290     }
20291
20292   pop_deferring_access_checks ();
20293
20294   if (id)
20295     {
20296       cp_parser_check_for_invalid_template_id (parser, id,
20297                                                class_key,
20298                                                type_start_token->location);
20299     }
20300   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20301
20302   /* If it's not a `:' or a `{' then we can't really be looking at a
20303      class-head, since a class-head only appears as part of a
20304      class-specifier.  We have to detect this situation before calling
20305      xref_tag, since that has irreversible side-effects.  */
20306   if (!cp_parser_next_token_starts_class_definition_p (parser))
20307     {
20308       cp_parser_error (parser, "expected %<{%> or %<:%>");
20309       type = error_mark_node;
20310       goto out;
20311     }
20312
20313   /* At this point, we're going ahead with the class-specifier, even
20314      if some other problem occurs.  */
20315   cp_parser_commit_to_tentative_parse (parser);
20316   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20317     {
20318       cp_parser_error (parser,
20319                        "cannot specify %<override%> for a class");
20320       type = error_mark_node;
20321       goto out;
20322     }
20323   /* Issue the error about the overly-qualified name now.  */
20324   if (qualified_p)
20325     {
20326       cp_parser_error (parser,
20327                        "global qualification of class name is invalid");
20328       type = error_mark_node;
20329       goto out;
20330     }
20331   else if (invalid_nested_name_p)
20332     {
20333       cp_parser_error (parser,
20334                        "qualified name does not name a class");
20335       type = error_mark_node;
20336       goto out;
20337     }
20338   else if (nested_name_specifier)
20339     {
20340       tree scope;
20341
20342       /* Reject typedef-names in class heads.  */
20343       if (!DECL_IMPLICIT_TYPEDEF_P (type))
20344         {
20345           error_at (type_start_token->location,
20346                     "invalid class name in declaration of %qD",
20347                     type);
20348           type = NULL_TREE;
20349           goto done;
20350         }
20351
20352       /* Figure out in what scope the declaration is being placed.  */
20353       scope = current_scope ();
20354       /* If that scope does not contain the scope in which the
20355          class was originally declared, the program is invalid.  */
20356       if (scope && !is_ancestor (scope, nested_name_specifier))
20357         {
20358           if (at_namespace_scope_p ())
20359             error_at (type_start_token->location,
20360                       "declaration of %qD in namespace %qD which does not "
20361                       "enclose %qD",
20362                       type, scope, nested_name_specifier);
20363           else
20364             error_at (type_start_token->location,
20365                       "declaration of %qD in %qD which does not enclose %qD",
20366                       type, scope, nested_name_specifier);
20367           type = NULL_TREE;
20368           goto done;
20369         }
20370       /* [dcl.meaning]
20371
20372          A declarator-id shall not be qualified except for the
20373          definition of a ... nested class outside of its class
20374          ... [or] the definition or explicit instantiation of a
20375          class member of a namespace outside of its namespace.  */
20376       if (scope == nested_name_specifier)
20377         {
20378           permerror (nested_name_specifier_token_start->location,
20379                      "extra qualification not allowed");
20380           nested_name_specifier = NULL_TREE;
20381           num_templates = 0;
20382         }
20383     }
20384   /* An explicit-specialization must be preceded by "template <>".  If
20385      it is not, try to recover gracefully.  */
20386   if (at_namespace_scope_p ()
20387       && parser->num_template_parameter_lists == 0
20388       && template_id_p)
20389     {
20390       error_at (type_start_token->location,
20391                 "an explicit specialization must be preceded by %<template <>%>");
20392       invalid_explicit_specialization_p = true;
20393       /* Take the same action that would have been taken by
20394          cp_parser_explicit_specialization.  */
20395       ++parser->num_template_parameter_lists;
20396       begin_specialization ();
20397     }
20398   /* There must be no "return" statements between this point and the
20399      end of this function; set "type "to the correct return value and
20400      use "goto done;" to return.  */
20401   /* Make sure that the right number of template parameters were
20402      present.  */
20403   if (!cp_parser_check_template_parameters (parser, num_templates,
20404                                             type_start_token->location,
20405                                             /*declarator=*/NULL))
20406     {
20407       /* If something went wrong, there is no point in even trying to
20408          process the class-definition.  */
20409       type = NULL_TREE;
20410       goto done;
20411     }
20412
20413   /* Look up the type.  */
20414   if (template_id_p)
20415     {
20416       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20417           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20418               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20419         {
20420           error_at (type_start_token->location,
20421                     "function template %qD redeclared as a class template", id);
20422           type = error_mark_node;
20423         }
20424       else
20425         {
20426           type = TREE_TYPE (id);
20427           type = maybe_process_partial_specialization (type);
20428         }
20429       if (nested_name_specifier)
20430         pushed_scope = push_scope (nested_name_specifier);
20431     }
20432   else if (nested_name_specifier)
20433     {
20434       tree class_type;
20435
20436       /* Given:
20437
20438             template <typename T> struct S { struct T };
20439             template <typename T> struct S<T>::T { };
20440
20441          we will get a TYPENAME_TYPE when processing the definition of
20442          `S::T'.  We need to resolve it to the actual type before we
20443          try to define it.  */
20444       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20445         {
20446           class_type = resolve_typename_type (TREE_TYPE (type),
20447                                               /*only_current_p=*/false);
20448           if (TREE_CODE (class_type) != TYPENAME_TYPE)
20449             type = TYPE_NAME (class_type);
20450           else
20451             {
20452               cp_parser_error (parser, "could not resolve typename type");
20453               type = error_mark_node;
20454             }
20455         }
20456
20457       if (maybe_process_partial_specialization (TREE_TYPE (type))
20458           == error_mark_node)
20459         {
20460           type = NULL_TREE;
20461           goto done;
20462         }
20463
20464       class_type = current_class_type;
20465       /* Enter the scope indicated by the nested-name-specifier.  */
20466       pushed_scope = push_scope (nested_name_specifier);
20467       /* Get the canonical version of this type.  */
20468       type = TYPE_MAIN_DECL (TREE_TYPE (type));
20469       /* Call push_template_decl if it seems like we should be defining a
20470          template either from the template headers or the type we're
20471          defining, so that we diagnose both extra and missing headers.  */
20472       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20473            || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20474           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20475         {
20476           type = push_template_decl (type);
20477           if (type == error_mark_node)
20478             {
20479               type = NULL_TREE;
20480               goto done;
20481             }
20482         }
20483
20484       type = TREE_TYPE (type);
20485       *nested_name_specifier_p = true;
20486     }
20487   else      /* The name is not a nested name.  */
20488     {
20489       /* If the class was unnamed, create a dummy name.  */
20490       if (!id)
20491         id = make_anon_name ();
20492       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20493                        parser->num_template_parameter_lists);
20494     }
20495
20496   /* Indicate whether this class was declared as a `class' or as a
20497      `struct'.  */
20498   if (TREE_CODE (type) == RECORD_TYPE)
20499     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20500   cp_parser_check_class_key (class_key, type);
20501
20502   /* If this type was already complete, and we see another definition,
20503      that's an error.  */
20504   if (type != error_mark_node && COMPLETE_TYPE_P (type))
20505     {
20506       error_at (type_start_token->location, "redefinition of %q#T",
20507                 type);
20508       error_at (type_start_token->location, "previous definition of %q+#T",
20509                 type);
20510       type = NULL_TREE;
20511       goto done;
20512     }
20513   else if (type == error_mark_node)
20514     type = NULL_TREE;
20515
20516   if (type)
20517     {
20518       /* Apply attributes now, before any use of the class as a template
20519          argument in its base list.  */
20520       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20521       fixup_attribute_variants (type);
20522     }
20523
20524   /* We will have entered the scope containing the class; the names of
20525      base classes should be looked up in that context.  For example:
20526
20527        struct A { struct B {}; struct C; };
20528        struct A::C : B {};
20529
20530      is valid.  */
20531
20532   /* Get the list of base-classes, if there is one.  */
20533   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20534     {
20535       /* PR59482: enter the class scope so that base-specifiers are looked
20536          up correctly.  */
20537       if (type)
20538         pushclass (type);
20539       bases = cp_parser_base_clause (parser);
20540       /* PR59482: get out of the previously pushed class scope so that the
20541          subsequent pops pop the right thing.  */
20542       if (type)
20543         popclass ();
20544     }
20545   else
20546     bases = NULL_TREE;
20547
20548   /* If we're really defining a class, process the base classes.
20549      If they're invalid, fail.  */
20550   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20551       && !xref_basetypes (type, bases))
20552     type = NULL_TREE;
20553
20554  done:
20555   /* Leave the scope given by the nested-name-specifier.  We will
20556      enter the class scope itself while processing the members.  */
20557   if (pushed_scope)
20558     pop_scope (pushed_scope);
20559
20560   if (invalid_explicit_specialization_p)
20561     {
20562       end_specialization ();
20563       --parser->num_template_parameter_lists;
20564     }
20565
20566   if (type)
20567     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20568   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20569     CLASSTYPE_FINAL (type) = 1;
20570  out:
20571   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20572   return type;
20573 }
20574
20575 /* Parse a class-key.
20576
20577    class-key:
20578      class
20579      struct
20580      union
20581
20582    Returns the kind of class-key specified, or none_type to indicate
20583    error.  */
20584
20585 static enum tag_types
20586 cp_parser_class_key (cp_parser* parser)
20587 {
20588   cp_token *token;
20589   enum tag_types tag_type;
20590
20591   /* Look for the class-key.  */
20592   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20593   if (!token)
20594     return none_type;
20595
20596   /* Check to see if the TOKEN is a class-key.  */
20597   tag_type = cp_parser_token_is_class_key (token);
20598   if (!tag_type)
20599     cp_parser_error (parser, "expected class-key");
20600   return tag_type;
20601 }
20602
20603 /* Parse a type-parameter-key.
20604
20605    type-parameter-key:
20606      class
20607      typename
20608  */
20609
20610 static void
20611 cp_parser_type_parameter_key (cp_parser* parser)
20612 {
20613   /* Look for the type-parameter-key.  */
20614   enum tag_types tag_type = none_type;
20615   cp_token *token = cp_lexer_peek_token (parser->lexer);
20616   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20617     {
20618       cp_lexer_consume_token (parser->lexer);
20619       if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20620         /* typename is not allowed in a template template parameter
20621            by the standard until C++1Z.  */
20622         pedwarn (token->location, OPT_Wpedantic, 
20623                  "ISO C++ forbids typename key in template template parameter;"
20624                  " use -std=c++1z or -std=gnu++1z");
20625     }
20626   else
20627     cp_parser_error (parser, "expected %<class%> or %<typename%>");
20628
20629   return;
20630 }
20631
20632 /* Parse an (optional) member-specification.
20633
20634    member-specification:
20635      member-declaration member-specification [opt]
20636      access-specifier : member-specification [opt]  */
20637
20638 static void
20639 cp_parser_member_specification_opt (cp_parser* parser)
20640 {
20641   while (true)
20642     {
20643       cp_token *token;
20644       enum rid keyword;
20645
20646       /* Peek at the next token.  */
20647       token = cp_lexer_peek_token (parser->lexer);
20648       /* If it's a `}', or EOF then we've seen all the members.  */
20649       if (token->type == CPP_CLOSE_BRACE
20650           || token->type == CPP_EOF
20651           || token->type == CPP_PRAGMA_EOL)
20652         break;
20653
20654       /* See if this token is a keyword.  */
20655       keyword = token->keyword;
20656       switch (keyword)
20657         {
20658         case RID_PUBLIC:
20659         case RID_PROTECTED:
20660         case RID_PRIVATE:
20661           /* Consume the access-specifier.  */
20662           cp_lexer_consume_token (parser->lexer);
20663           /* Remember which access-specifier is active.  */
20664           current_access_specifier = token->u.value;
20665           /* Look for the `:'.  */
20666           cp_parser_require (parser, CPP_COLON, RT_COLON);
20667           break;
20668
20669         default:
20670           /* Accept #pragmas at class scope.  */
20671           if (token->type == CPP_PRAGMA)
20672             {
20673               cp_parser_pragma (parser, pragma_member);
20674               break;
20675             }
20676
20677           /* Otherwise, the next construction must be a
20678              member-declaration.  */
20679           cp_parser_member_declaration (parser);
20680         }
20681     }
20682 }
20683
20684 /* Parse a member-declaration.
20685
20686    member-declaration:
20687      decl-specifier-seq [opt] member-declarator-list [opt] ;
20688      function-definition ; [opt]
20689      :: [opt] nested-name-specifier template [opt] unqualified-id ;
20690      using-declaration
20691      template-declaration
20692      alias-declaration
20693
20694    member-declarator-list:
20695      member-declarator
20696      member-declarator-list , member-declarator
20697
20698    member-declarator:
20699      declarator pure-specifier [opt]
20700      declarator constant-initializer [opt]
20701      identifier [opt] : constant-expression
20702
20703    GNU Extensions:
20704
20705    member-declaration:
20706      __extension__ member-declaration
20707
20708    member-declarator:
20709      declarator attributes [opt] pure-specifier [opt]
20710      declarator attributes [opt] constant-initializer [opt]
20711      identifier [opt] attributes [opt] : constant-expression  
20712
20713    C++0x Extensions:
20714
20715    member-declaration:
20716      static_assert-declaration  */
20717
20718 static void
20719 cp_parser_member_declaration (cp_parser* parser)
20720 {
20721   cp_decl_specifier_seq decl_specifiers;
20722   tree prefix_attributes;
20723   tree decl;
20724   int declares_class_or_enum;
20725   bool friend_p;
20726   cp_token *token = NULL;
20727   cp_token *decl_spec_token_start = NULL;
20728   cp_token *initializer_token_start = NULL;
20729   int saved_pedantic;
20730   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20731
20732   /* Check for the `__extension__' keyword.  */
20733   if (cp_parser_extension_opt (parser, &saved_pedantic))
20734     {
20735       /* Recurse.  */
20736       cp_parser_member_declaration (parser);
20737       /* Restore the old value of the PEDANTIC flag.  */
20738       pedantic = saved_pedantic;
20739
20740       return;
20741     }
20742
20743   /* Check for a template-declaration.  */
20744   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20745     {
20746       /* An explicit specialization here is an error condition, and we
20747          expect the specialization handler to detect and report this.  */
20748       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20749           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20750         cp_parser_explicit_specialization (parser);
20751       else
20752         cp_parser_template_declaration (parser, /*member_p=*/true);
20753
20754       return;
20755     }
20756
20757   /* Check for a using-declaration.  */
20758   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20759     {
20760       if (cxx_dialect < cxx11)
20761         {
20762           /* Parse the using-declaration.  */
20763           cp_parser_using_declaration (parser,
20764                                        /*access_declaration_p=*/false);
20765           return;
20766         }
20767       else
20768         {
20769           tree decl;
20770           bool alias_decl_expected;
20771           cp_parser_parse_tentatively (parser);
20772           decl = cp_parser_alias_declaration (parser);
20773           /* Note that if we actually see the '=' token after the
20774              identifier, cp_parser_alias_declaration commits the
20775              tentative parse.  In that case, we really expects an
20776              alias-declaration.  Otherwise, we expect a using
20777              declaration.  */
20778           alias_decl_expected =
20779             !cp_parser_uncommitted_to_tentative_parse_p (parser);
20780           cp_parser_parse_definitely (parser);
20781
20782           if (alias_decl_expected)
20783             finish_member_declaration (decl);
20784           else
20785             cp_parser_using_declaration (parser,
20786                                          /*access_declaration_p=*/false);
20787           return;
20788         }
20789     }
20790
20791   /* Check for @defs.  */
20792   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20793     {
20794       tree ivar, member;
20795       tree ivar_chains = cp_parser_objc_defs_expression (parser);
20796       ivar = ivar_chains;
20797       while (ivar)
20798         {
20799           member = ivar;
20800           ivar = TREE_CHAIN (member);
20801           TREE_CHAIN (member) = NULL_TREE;
20802           finish_member_declaration (member);
20803         }
20804       return;
20805     }
20806
20807   /* If the next token is `static_assert' we have a static assertion.  */
20808   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20809     {
20810       cp_parser_static_assert (parser, /*member_p=*/true);
20811       return;
20812     }
20813
20814   parser->colon_corrects_to_scope_p = false;
20815
20816   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20817       goto out;
20818
20819   /* Parse the decl-specifier-seq.  */
20820   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20821   cp_parser_decl_specifier_seq (parser,
20822                                 CP_PARSER_FLAGS_OPTIONAL,
20823                                 &decl_specifiers,
20824                                 &declares_class_or_enum);
20825   /* Check for an invalid type-name.  */
20826   if (!decl_specifiers.any_type_specifiers_p
20827       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20828     goto out;
20829   /* If there is no declarator, then the decl-specifier-seq should
20830      specify a type.  */
20831   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20832     {
20833       /* If there was no decl-specifier-seq, and the next token is a
20834          `;', then we have something like:
20835
20836            struct S { ; };
20837
20838          [class.mem]
20839
20840          Each member-declaration shall declare at least one member
20841          name of the class.  */
20842       if (!decl_specifiers.any_specifiers_p)
20843         {
20844           cp_token *token = cp_lexer_peek_token (parser->lexer);
20845           if (!in_system_header_at (token->location))
20846             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20847         }
20848       else
20849         {
20850           tree type;
20851
20852           /* See if this declaration is a friend.  */
20853           friend_p = cp_parser_friend_p (&decl_specifiers);
20854           /* If there were decl-specifiers, check to see if there was
20855              a class-declaration.  */
20856           type = check_tag_decl (&decl_specifiers,
20857                                  /*explicit_type_instantiation_p=*/false);
20858           /* Nested classes have already been added to the class, but
20859              a `friend' needs to be explicitly registered.  */
20860           if (friend_p)
20861             {
20862               /* If the `friend' keyword was present, the friend must
20863                  be introduced with a class-key.  */
20864                if (!declares_class_or_enum && cxx_dialect < cxx11)
20865                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20866                           "in C++03 a class-key must be used "
20867                           "when declaring a friend");
20868                /* In this case:
20869
20870                     template <typename T> struct A {
20871                       friend struct A<T>::B;
20872                     };
20873
20874                   A<T>::B will be represented by a TYPENAME_TYPE, and
20875                   therefore not recognized by check_tag_decl.  */
20876                if (!type)
20877                  {
20878                    type = decl_specifiers.type;
20879                    if (type && TREE_CODE (type) == TYPE_DECL)
20880                      type = TREE_TYPE (type);
20881                  }
20882                if (!type || !TYPE_P (type))
20883                  error_at (decl_spec_token_start->location,
20884                            "friend declaration does not name a class or "
20885                            "function");
20886                else
20887                  make_friend_class (current_class_type, type,
20888                                     /*complain=*/true);
20889             }
20890           /* If there is no TYPE, an error message will already have
20891              been issued.  */
20892           else if (!type || type == error_mark_node)
20893             ;
20894           /* An anonymous aggregate has to be handled specially; such
20895              a declaration really declares a data member (with a
20896              particular type), as opposed to a nested class.  */
20897           else if (ANON_AGGR_TYPE_P (type))
20898             {
20899               /* C++11 9.5/6.  */
20900               if (decl_specifiers.storage_class != sc_none)
20901                 error_at (decl_spec_token_start->location,
20902                           "a storage class on an anonymous aggregate "
20903                           "in class scope is not allowed");
20904
20905               /* Remove constructors and such from TYPE, now that we
20906                  know it is an anonymous aggregate.  */
20907               fixup_anonymous_aggr (type);
20908               /* And make the corresponding data member.  */
20909               decl = build_decl (decl_spec_token_start->location,
20910                                  FIELD_DECL, NULL_TREE, type);
20911               /* Add it to the class.  */
20912               finish_member_declaration (decl);
20913             }
20914           else
20915             cp_parser_check_access_in_redeclaration
20916                                               (TYPE_NAME (type),
20917                                                decl_spec_token_start->location);
20918         }
20919     }
20920   else
20921     {
20922       bool assume_semicolon = false;
20923
20924       /* Clear attributes from the decl_specifiers but keep them
20925          around as prefix attributes that apply them to the entity
20926          being declared.  */
20927       prefix_attributes = decl_specifiers.attributes;
20928       decl_specifiers.attributes = NULL_TREE;
20929
20930       /* See if these declarations will be friends.  */
20931       friend_p = cp_parser_friend_p (&decl_specifiers);
20932
20933       /* Keep going until we hit the `;' at the end of the
20934          declaration.  */
20935       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20936         {
20937           tree attributes = NULL_TREE;
20938           tree first_attribute;
20939
20940           /* Peek at the next token.  */
20941           token = cp_lexer_peek_token (parser->lexer);
20942
20943           /* Check for a bitfield declaration.  */
20944           if (token->type == CPP_COLON
20945               || (token->type == CPP_NAME
20946                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20947                   == CPP_COLON))
20948             {
20949               tree identifier;
20950               tree width;
20951
20952               /* Get the name of the bitfield.  Note that we cannot just
20953                  check TOKEN here because it may have been invalidated by
20954                  the call to cp_lexer_peek_nth_token above.  */
20955               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20956                 identifier = cp_parser_identifier (parser);
20957               else
20958                 identifier = NULL_TREE;
20959
20960               /* Consume the `:' token.  */
20961               cp_lexer_consume_token (parser->lexer);
20962               /* Get the width of the bitfield.  */
20963               width
20964                 = cp_parser_constant_expression (parser);
20965
20966               /* Look for attributes that apply to the bitfield.  */
20967               attributes = cp_parser_attributes_opt (parser);
20968               /* Remember which attributes are prefix attributes and
20969                  which are not.  */
20970               first_attribute = attributes;
20971               /* Combine the attributes.  */
20972               attributes = chainon (prefix_attributes, attributes);
20973
20974               /* Create the bitfield declaration.  */
20975               decl = grokbitfield (identifier
20976                                    ? make_id_declarator (NULL_TREE,
20977                                                          identifier,
20978                                                          sfk_none)
20979                                    : NULL,
20980                                    &decl_specifiers,
20981                                    width,
20982                                    attributes);
20983             }
20984           else
20985             {
20986               cp_declarator *declarator;
20987               tree initializer;
20988               tree asm_specification;
20989               int ctor_dtor_or_conv_p;
20990
20991               /* Parse the declarator.  */
20992               declarator
20993                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20994                                         &ctor_dtor_or_conv_p,
20995                                         /*parenthesized_p=*/NULL,
20996                                         /*member_p=*/true,
20997                                         friend_p);
20998
20999               /* If something went wrong parsing the declarator, make sure
21000                  that we at least consume some tokens.  */
21001               if (declarator == cp_error_declarator)
21002                 {
21003                   /* Skip to the end of the statement.  */
21004                   cp_parser_skip_to_end_of_statement (parser);
21005                   /* If the next token is not a semicolon, that is
21006                      probably because we just skipped over the body of
21007                      a function.  So, we consume a semicolon if
21008                      present, but do not issue an error message if it
21009                      is not present.  */
21010                   if (cp_lexer_next_token_is (parser->lexer,
21011                                               CPP_SEMICOLON))
21012                     cp_lexer_consume_token (parser->lexer);
21013                   goto out;
21014                 }
21015
21016               if (declares_class_or_enum & 2)
21017                 cp_parser_check_for_definition_in_return_type
21018                                             (declarator, decl_specifiers.type,
21019                                              decl_specifiers.locations[ds_type_spec]);
21020
21021               /* Look for an asm-specification.  */
21022               asm_specification = cp_parser_asm_specification_opt (parser);
21023               /* Look for attributes that apply to the declaration.  */
21024               attributes = cp_parser_attributes_opt (parser);
21025               /* Remember which attributes are prefix attributes and
21026                  which are not.  */
21027               first_attribute = attributes;
21028               /* Combine the attributes.  */
21029               attributes = chainon (prefix_attributes, attributes);
21030
21031               /* If it's an `=', then we have a constant-initializer or a
21032                  pure-specifier.  It is not correct to parse the
21033                  initializer before registering the member declaration
21034                  since the member declaration should be in scope while
21035                  its initializer is processed.  However, the rest of the
21036                  front end does not yet provide an interface that allows
21037                  us to handle this correctly.  */
21038               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21039                 {
21040                   /* In [class.mem]:
21041
21042                      A pure-specifier shall be used only in the declaration of
21043                      a virtual function.
21044
21045                      A member-declarator can contain a constant-initializer
21046                      only if it declares a static member of integral or
21047                      enumeration type.
21048
21049                      Therefore, if the DECLARATOR is for a function, we look
21050                      for a pure-specifier; otherwise, we look for a
21051                      constant-initializer.  When we call `grokfield', it will
21052                      perform more stringent semantics checks.  */
21053                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
21054                   if (function_declarator_p (declarator)
21055                       || (decl_specifiers.type
21056                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21057                           && declarator->kind == cdk_id
21058                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21059                               == FUNCTION_TYPE)))
21060                     initializer = cp_parser_pure_specifier (parser);
21061                   else if (decl_specifiers.storage_class != sc_static)
21062                     initializer = cp_parser_save_nsdmi (parser);
21063                   else if (cxx_dialect >= cxx11)
21064                     {
21065                       bool nonconst;
21066                       /* Don't require a constant rvalue in C++11, since we
21067                          might want a reference constant.  We'll enforce
21068                          constancy later.  */
21069                       cp_lexer_consume_token (parser->lexer);
21070                       /* Parse the initializer.  */
21071                       initializer = cp_parser_initializer_clause (parser,
21072                                                                   &nonconst);
21073                     }
21074                   else
21075                     /* Parse the initializer.  */
21076                     initializer = cp_parser_constant_initializer (parser);
21077                 }
21078               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21079                        && !function_declarator_p (declarator))
21080                 {
21081                   bool x;
21082                   if (decl_specifiers.storage_class != sc_static)
21083                     initializer = cp_parser_save_nsdmi (parser);
21084                   else
21085                     initializer = cp_parser_initializer (parser, &x, &x);
21086                 }
21087               /* Otherwise, there is no initializer.  */
21088               else
21089                 initializer = NULL_TREE;
21090
21091               /* See if we are probably looking at a function
21092                  definition.  We are certainly not looking at a
21093                  member-declarator.  Calling `grokfield' has
21094                  side-effects, so we must not do it unless we are sure
21095                  that we are looking at a member-declarator.  */
21096               if (cp_parser_token_starts_function_definition_p
21097                   (cp_lexer_peek_token (parser->lexer)))
21098                 {
21099                   /* The grammar does not allow a pure-specifier to be
21100                      used when a member function is defined.  (It is
21101                      possible that this fact is an oversight in the
21102                      standard, since a pure function may be defined
21103                      outside of the class-specifier.  */
21104                   if (initializer && initializer_token_start)
21105                     error_at (initializer_token_start->location,
21106                               "pure-specifier on function-definition");
21107                   decl = cp_parser_save_member_function_body (parser,
21108                                                               &decl_specifiers,
21109                                                               declarator,
21110                                                               attributes);
21111                   if (parser->fully_implicit_function_template_p)
21112                     decl = finish_fully_implicit_template (parser, decl);
21113                   /* If the member was not a friend, declare it here.  */
21114                   if (!friend_p)
21115                     finish_member_declaration (decl);
21116                   /* Peek at the next token.  */
21117                   token = cp_lexer_peek_token (parser->lexer);
21118                   /* If the next token is a semicolon, consume it.  */
21119                   if (token->type == CPP_SEMICOLON)
21120                     cp_lexer_consume_token (parser->lexer);
21121                   goto out;
21122                 }
21123               else
21124                 if (declarator->kind == cdk_function)
21125                   declarator->id_loc = token->location;
21126               /* Create the declaration.  */
21127               decl = grokfield (declarator, &decl_specifiers,
21128                                 initializer, /*init_const_expr_p=*/true,
21129                                 asm_specification, attributes);
21130               if (parser->fully_implicit_function_template_p)
21131                 {
21132                   if (friend_p)
21133                     finish_fully_implicit_template (parser, 0);
21134                   else
21135                     decl = finish_fully_implicit_template (parser, decl);
21136                 }
21137             }
21138
21139           cp_finalize_omp_declare_simd (parser, decl);
21140
21141           /* Reset PREFIX_ATTRIBUTES.  */
21142           while (attributes && TREE_CHAIN (attributes) != first_attribute)
21143             attributes = TREE_CHAIN (attributes);
21144           if (attributes)
21145             TREE_CHAIN (attributes) = NULL_TREE;
21146
21147           /* If there is any qualification still in effect, clear it
21148              now; we will be starting fresh with the next declarator.  */
21149           parser->scope = NULL_TREE;
21150           parser->qualifying_scope = NULL_TREE;
21151           parser->object_scope = NULL_TREE;
21152           /* If it's a `,', then there are more declarators.  */
21153           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21154             {
21155               cp_lexer_consume_token (parser->lexer);
21156               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21157                 {
21158                   cp_token *token = cp_lexer_previous_token (parser->lexer);
21159                   error_at (token->location,
21160                             "stray %<,%> at end of member declaration");
21161                 }
21162             }
21163           /* If the next token isn't a `;', then we have a parse error.  */
21164           else if (cp_lexer_next_token_is_not (parser->lexer,
21165                                                CPP_SEMICOLON))
21166             {
21167               /* The next token might be a ways away from where the
21168                  actual semicolon is missing.  Find the previous token
21169                  and use that for our error position.  */
21170               cp_token *token = cp_lexer_previous_token (parser->lexer);
21171               error_at (token->location,
21172                         "expected %<;%> at end of member declaration");
21173
21174               /* Assume that the user meant to provide a semicolon.  If
21175                  we were to cp_parser_skip_to_end_of_statement, we might
21176                  skip to a semicolon inside a member function definition
21177                  and issue nonsensical error messages.  */
21178               assume_semicolon = true;
21179             }
21180
21181           if (decl)
21182             {
21183               /* Add DECL to the list of members.  */
21184               if (!friend_p
21185                   /* Explicitly include, eg, NSDMIs, for better error
21186                      recovery (c++/58650).  */
21187                   || !DECL_DECLARES_FUNCTION_P (decl))
21188                 finish_member_declaration (decl);
21189
21190               if (TREE_CODE (decl) == FUNCTION_DECL)
21191                 cp_parser_save_default_args (parser, decl);
21192               else if (TREE_CODE (decl) == FIELD_DECL
21193                        && !DECL_C_BIT_FIELD (decl)
21194                        && DECL_INITIAL (decl))
21195                 /* Add DECL to the queue of NSDMI to be parsed later.  */
21196                 vec_safe_push (unparsed_nsdmis, decl);
21197             }
21198
21199           if (assume_semicolon)
21200             goto out;
21201         }
21202     }
21203
21204   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21205  out:
21206   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21207 }
21208
21209 /* Parse a pure-specifier.
21210
21211    pure-specifier:
21212      = 0
21213
21214    Returns INTEGER_ZERO_NODE if a pure specifier is found.
21215    Otherwise, ERROR_MARK_NODE is returned.  */
21216
21217 static tree
21218 cp_parser_pure_specifier (cp_parser* parser)
21219 {
21220   cp_token *token;
21221
21222   /* Look for the `=' token.  */
21223   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21224     return error_mark_node;
21225   /* Look for the `0' token.  */
21226   token = cp_lexer_peek_token (parser->lexer);
21227
21228   if (token->type == CPP_EOF
21229       || token->type == CPP_PRAGMA_EOL)
21230     return error_mark_node;
21231
21232   cp_lexer_consume_token (parser->lexer);
21233
21234   /* Accept = default or = delete in c++0x mode.  */
21235   if (token->keyword == RID_DEFAULT
21236       || token->keyword == RID_DELETE)
21237     {
21238       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21239       return token->u.value;
21240     }
21241
21242   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
21243   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21244     {
21245       cp_parser_error (parser,
21246                        "invalid pure specifier (only %<= 0%> is allowed)");
21247       cp_parser_skip_to_end_of_statement (parser);
21248       return error_mark_node;
21249     }
21250   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21251     {
21252       error_at (token->location, "templates may not be %<virtual%>");
21253       return error_mark_node;
21254     }
21255
21256   return integer_zero_node;
21257 }
21258
21259 /* Parse a constant-initializer.
21260
21261    constant-initializer:
21262      = constant-expression
21263
21264    Returns a representation of the constant-expression.  */
21265
21266 static tree
21267 cp_parser_constant_initializer (cp_parser* parser)
21268 {
21269   /* Look for the `=' token.  */
21270   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21271     return error_mark_node;
21272
21273   /* It is invalid to write:
21274
21275        struct S { static const int i = { 7 }; };
21276
21277      */
21278   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21279     {
21280       cp_parser_error (parser,
21281                        "a brace-enclosed initializer is not allowed here");
21282       /* Consume the opening brace.  */
21283       cp_lexer_consume_token (parser->lexer);
21284       /* Skip the initializer.  */
21285       cp_parser_skip_to_closing_brace (parser);
21286       /* Look for the trailing `}'.  */
21287       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21288
21289       return error_mark_node;
21290     }
21291
21292   return cp_parser_constant_expression (parser);
21293 }
21294
21295 /* Derived classes [gram.class.derived] */
21296
21297 /* Parse a base-clause.
21298
21299    base-clause:
21300      : base-specifier-list
21301
21302    base-specifier-list:
21303      base-specifier ... [opt]
21304      base-specifier-list , base-specifier ... [opt]
21305
21306    Returns a TREE_LIST representing the base-classes, in the order in
21307    which they were declared.  The representation of each node is as
21308    described by cp_parser_base_specifier.
21309
21310    In the case that no bases are specified, this function will return
21311    NULL_TREE, not ERROR_MARK_NODE.  */
21312
21313 static tree
21314 cp_parser_base_clause (cp_parser* parser)
21315 {
21316   tree bases = NULL_TREE;
21317
21318   /* Look for the `:' that begins the list.  */
21319   cp_parser_require (parser, CPP_COLON, RT_COLON);
21320
21321   /* Scan the base-specifier-list.  */
21322   while (true)
21323     {
21324       cp_token *token;
21325       tree base;
21326       bool pack_expansion_p = false;
21327
21328       /* Look for the base-specifier.  */
21329       base = cp_parser_base_specifier (parser);
21330       /* Look for the (optional) ellipsis. */
21331       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21332         {
21333           /* Consume the `...'. */
21334           cp_lexer_consume_token (parser->lexer);
21335
21336           pack_expansion_p = true;
21337         }
21338
21339       /* Add BASE to the front of the list.  */
21340       if (base && base != error_mark_node)
21341         {
21342           if (pack_expansion_p)
21343             /* Make this a pack expansion type. */
21344             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21345
21346           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21347             {
21348               TREE_CHAIN (base) = bases;
21349               bases = base;
21350             }
21351         }
21352       /* Peek at the next token.  */
21353       token = cp_lexer_peek_token (parser->lexer);
21354       /* If it's not a comma, then the list is complete.  */
21355       if (token->type != CPP_COMMA)
21356         break;
21357       /* Consume the `,'.  */
21358       cp_lexer_consume_token (parser->lexer);
21359     }
21360
21361   /* PARSER->SCOPE may still be non-NULL at this point, if the last
21362      base class had a qualified name.  However, the next name that
21363      appears is certainly not qualified.  */
21364   parser->scope = NULL_TREE;
21365   parser->qualifying_scope = NULL_TREE;
21366   parser->object_scope = NULL_TREE;
21367
21368   return nreverse (bases);
21369 }
21370
21371 /* Parse a base-specifier.
21372
21373    base-specifier:
21374      :: [opt] nested-name-specifier [opt] class-name
21375      virtual access-specifier [opt] :: [opt] nested-name-specifier
21376        [opt] class-name
21377      access-specifier virtual [opt] :: [opt] nested-name-specifier
21378        [opt] class-name
21379
21380    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
21381    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21382    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
21383    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
21384
21385 static tree
21386 cp_parser_base_specifier (cp_parser* parser)
21387 {
21388   cp_token *token;
21389   bool done = false;
21390   bool virtual_p = false;
21391   bool duplicate_virtual_error_issued_p = false;
21392   bool duplicate_access_error_issued_p = false;
21393   bool class_scope_p, template_p;
21394   tree access = access_default_node;
21395   tree type;
21396
21397   /* Process the optional `virtual' and `access-specifier'.  */
21398   while (!done)
21399     {
21400       /* Peek at the next token.  */
21401       token = cp_lexer_peek_token (parser->lexer);
21402       /* Process `virtual'.  */
21403       switch (token->keyword)
21404         {
21405         case RID_VIRTUAL:
21406           /* If `virtual' appears more than once, issue an error.  */
21407           if (virtual_p && !duplicate_virtual_error_issued_p)
21408             {
21409               cp_parser_error (parser,
21410                                "%<virtual%> specified more than once in base-specified");
21411               duplicate_virtual_error_issued_p = true;
21412             }
21413
21414           virtual_p = true;
21415
21416           /* Consume the `virtual' token.  */
21417           cp_lexer_consume_token (parser->lexer);
21418
21419           break;
21420
21421         case RID_PUBLIC:
21422         case RID_PROTECTED:
21423         case RID_PRIVATE:
21424           /* If more than one access specifier appears, issue an
21425              error.  */
21426           if (access != access_default_node
21427               && !duplicate_access_error_issued_p)
21428             {
21429               cp_parser_error (parser,
21430                                "more than one access specifier in base-specified");
21431               duplicate_access_error_issued_p = true;
21432             }
21433
21434           access = ridpointers[(int) token->keyword];
21435
21436           /* Consume the access-specifier.  */
21437           cp_lexer_consume_token (parser->lexer);
21438
21439           break;
21440
21441         default:
21442           done = true;
21443           break;
21444         }
21445     }
21446   /* It is not uncommon to see programs mechanically, erroneously, use
21447      the 'typename' keyword to denote (dependent) qualified types
21448      as base classes.  */
21449   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21450     {
21451       token = cp_lexer_peek_token (parser->lexer);
21452       if (!processing_template_decl)
21453         error_at (token->location,
21454                   "keyword %<typename%> not allowed outside of templates");
21455       else
21456         error_at (token->location,
21457                   "keyword %<typename%> not allowed in this context "
21458                   "(the base class is implicitly a type)");
21459       cp_lexer_consume_token (parser->lexer);
21460     }
21461
21462   /* Look for the optional `::' operator.  */
21463   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21464   /* Look for the nested-name-specifier.  The simplest way to
21465      implement:
21466
21467        [temp.res]
21468
21469        The keyword `typename' is not permitted in a base-specifier or
21470        mem-initializer; in these contexts a qualified name that
21471        depends on a template-parameter is implicitly assumed to be a
21472        type name.
21473
21474      is to pretend that we have seen the `typename' keyword at this
21475      point.  */
21476   cp_parser_nested_name_specifier_opt (parser,
21477                                        /*typename_keyword_p=*/true,
21478                                        /*check_dependency_p=*/true,
21479                                        typename_type,
21480                                        /*is_declaration=*/true);
21481   /* If the base class is given by a qualified name, assume that names
21482      we see are type names or templates, as appropriate.  */
21483   class_scope_p = (parser->scope && TYPE_P (parser->scope));
21484   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21485
21486   if (!parser->scope
21487       && cp_lexer_next_token_is_decltype (parser->lexer))
21488     /* DR 950 allows decltype as a base-specifier.  */
21489     type = cp_parser_decltype (parser);
21490   else
21491     {
21492       /* Otherwise, look for the class-name.  */
21493       type = cp_parser_class_name (parser,
21494                                    class_scope_p,
21495                                    template_p,
21496                                    typename_type,
21497                                    /*check_dependency_p=*/true,
21498                                    /*class_head_p=*/false,
21499                                    /*is_declaration=*/true);
21500       type = TREE_TYPE (type);
21501     }
21502
21503   if (type == error_mark_node)
21504     return error_mark_node;
21505
21506   return finish_base_specifier (type, access, virtual_p);
21507 }
21508
21509 /* Exception handling [gram.exception] */
21510
21511 /* Parse an (optional) noexcept-specification.
21512
21513    noexcept-specification:
21514      noexcept ( constant-expression ) [opt]
21515
21516    If no noexcept-specification is present, returns NULL_TREE.
21517    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21518    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21519    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
21520    Otherwise, returns a noexcept specification unless RETURN_COND is true,
21521    in which case a boolean condition is returned instead.  */
21522
21523 static tree
21524 cp_parser_noexcept_specification_opt (cp_parser* parser,
21525                                       bool require_constexpr,
21526                                       bool* consumed_expr,
21527                                       bool return_cond)
21528 {
21529   cp_token *token;
21530   const char *saved_message;
21531
21532   /* Peek at the next token.  */
21533   token = cp_lexer_peek_token (parser->lexer);
21534
21535   /* Is it a noexcept-specification?  */
21536   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21537     {
21538       tree expr;
21539       cp_lexer_consume_token (parser->lexer);
21540
21541       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21542         {
21543           cp_lexer_consume_token (parser->lexer);
21544
21545           if (require_constexpr)
21546             {
21547               /* Types may not be defined in an exception-specification.  */
21548               saved_message = parser->type_definition_forbidden_message;
21549               parser->type_definition_forbidden_message
21550               = G_("types may not be defined in an exception-specification");
21551
21552               expr = cp_parser_constant_expression (parser);
21553
21554               /* Restore the saved message.  */
21555               parser->type_definition_forbidden_message = saved_message;
21556             }
21557           else
21558             {
21559               expr = cp_parser_expression (parser);
21560               *consumed_expr = true;
21561             }
21562
21563           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21564         }
21565       else
21566         {
21567           expr = boolean_true_node;
21568           if (!require_constexpr)
21569             *consumed_expr = false;
21570         }
21571
21572       /* We cannot build a noexcept-spec right away because this will check
21573          that expr is a constexpr.  */
21574       if (!return_cond)
21575         return build_noexcept_spec (expr, tf_warning_or_error);
21576       else
21577         return expr;
21578     }
21579   else
21580     return NULL_TREE;
21581 }
21582
21583 /* Parse an (optional) exception-specification.
21584
21585    exception-specification:
21586      throw ( type-id-list [opt] )
21587
21588    Returns a TREE_LIST representing the exception-specification.  The
21589    TREE_VALUE of each node is a type.  */
21590
21591 static tree
21592 cp_parser_exception_specification_opt (cp_parser* parser)
21593 {
21594   cp_token *token;
21595   tree type_id_list;
21596   const char *saved_message;
21597
21598   /* Peek at the next token.  */
21599   token = cp_lexer_peek_token (parser->lexer);
21600
21601   /* Is it a noexcept-specification?  */
21602   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21603                                                       false);
21604   if (type_id_list != NULL_TREE)
21605     return type_id_list;
21606
21607   /* If it's not `throw', then there's no exception-specification.  */
21608   if (!cp_parser_is_keyword (token, RID_THROW))
21609     return NULL_TREE;
21610
21611 #if 0
21612   /* Enable this once a lot of code has transitioned to noexcept?  */
21613   if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21614     warning (OPT_Wdeprecated, "dynamic exception specifications are "
21615              "deprecated in C++0x; use %<noexcept%> instead");
21616 #endif
21617
21618   /* Consume the `throw'.  */
21619   cp_lexer_consume_token (parser->lexer);
21620
21621   /* Look for the `('.  */
21622   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21623
21624   /* Peek at the next token.  */
21625   token = cp_lexer_peek_token (parser->lexer);
21626   /* If it's not a `)', then there is a type-id-list.  */
21627   if (token->type != CPP_CLOSE_PAREN)
21628     {
21629       /* Types may not be defined in an exception-specification.  */
21630       saved_message = parser->type_definition_forbidden_message;
21631       parser->type_definition_forbidden_message
21632         = G_("types may not be defined in an exception-specification");
21633       /* Parse the type-id-list.  */
21634       type_id_list = cp_parser_type_id_list (parser);
21635       /* Restore the saved message.  */
21636       parser->type_definition_forbidden_message = saved_message;
21637     }
21638   else
21639     type_id_list = empty_except_spec;
21640
21641   /* Look for the `)'.  */
21642   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21643
21644   return type_id_list;
21645 }
21646
21647 /* Parse an (optional) type-id-list.
21648
21649    type-id-list:
21650      type-id ... [opt]
21651      type-id-list , type-id ... [opt]
21652
21653    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
21654    in the order that the types were presented.  */
21655
21656 static tree
21657 cp_parser_type_id_list (cp_parser* parser)
21658 {
21659   tree types = NULL_TREE;
21660
21661   while (true)
21662     {
21663       cp_token *token;
21664       tree type;
21665
21666       /* Get the next type-id.  */
21667       type = cp_parser_type_id (parser);
21668       /* Parse the optional ellipsis. */
21669       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21670         {
21671           /* Consume the `...'. */
21672           cp_lexer_consume_token (parser->lexer);
21673
21674           /* Turn the type into a pack expansion expression. */
21675           type = make_pack_expansion (type);
21676         }
21677       /* Add it to the list.  */
21678       types = add_exception_specifier (types, type, /*complain=*/1);
21679       /* Peek at the next token.  */
21680       token = cp_lexer_peek_token (parser->lexer);
21681       /* If it is not a `,', we are done.  */
21682       if (token->type != CPP_COMMA)
21683         break;
21684       /* Consume the `,'.  */
21685       cp_lexer_consume_token (parser->lexer);
21686     }
21687
21688   return nreverse (types);
21689 }
21690
21691 /* Parse a try-block.
21692
21693    try-block:
21694      try compound-statement handler-seq  */
21695
21696 static tree
21697 cp_parser_try_block (cp_parser* parser)
21698 {
21699   tree try_block;
21700
21701   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21702   if (parser->in_function_body
21703       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21704     error ("%<try%> in %<constexpr%> function");
21705
21706   try_block = begin_try_block ();
21707   cp_parser_compound_statement (parser, NULL, true, false);
21708   finish_try_block (try_block);
21709   cp_parser_handler_seq (parser);
21710   finish_handler_sequence (try_block);
21711
21712   return try_block;
21713 }
21714
21715 /* Parse a function-try-block.
21716
21717    function-try-block:
21718      try ctor-initializer [opt] function-body handler-seq  */
21719
21720 static bool
21721 cp_parser_function_try_block (cp_parser* parser)
21722 {
21723   tree compound_stmt;
21724   tree try_block;
21725   bool ctor_initializer_p;
21726
21727   /* Look for the `try' keyword.  */
21728   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21729     return false;
21730   /* Let the rest of the front end know where we are.  */
21731   try_block = begin_function_try_block (&compound_stmt);
21732   /* Parse the function-body.  */
21733   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21734     (parser, /*in_function_try_block=*/true);
21735   /* We're done with the `try' part.  */
21736   finish_function_try_block (try_block);
21737   /* Parse the handlers.  */
21738   cp_parser_handler_seq (parser);
21739   /* We're done with the handlers.  */
21740   finish_function_handler_sequence (try_block, compound_stmt);
21741
21742   return ctor_initializer_p;
21743 }
21744
21745 /* Parse a handler-seq.
21746
21747    handler-seq:
21748      handler handler-seq [opt]  */
21749
21750 static void
21751 cp_parser_handler_seq (cp_parser* parser)
21752 {
21753   while (true)
21754     {
21755       cp_token *token;
21756
21757       /* Parse the handler.  */
21758       cp_parser_handler (parser);
21759       /* Peek at the next token.  */
21760       token = cp_lexer_peek_token (parser->lexer);
21761       /* If it's not `catch' then there are no more handlers.  */
21762       if (!cp_parser_is_keyword (token, RID_CATCH))
21763         break;
21764     }
21765 }
21766
21767 /* Parse a handler.
21768
21769    handler:
21770      catch ( exception-declaration ) compound-statement  */
21771
21772 static void
21773 cp_parser_handler (cp_parser* parser)
21774 {
21775   tree handler;
21776   tree declaration;
21777
21778   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21779   handler = begin_handler ();
21780   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21781   declaration = cp_parser_exception_declaration (parser);
21782   finish_handler_parms (declaration, handler);
21783   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21784   cp_parser_compound_statement (parser, NULL, false, false);
21785   finish_handler (handler);
21786 }
21787
21788 /* Parse an exception-declaration.
21789
21790    exception-declaration:
21791      type-specifier-seq declarator
21792      type-specifier-seq abstract-declarator
21793      type-specifier-seq
21794      ...
21795
21796    Returns a VAR_DECL for the declaration, or NULL_TREE if the
21797    ellipsis variant is used.  */
21798
21799 static tree
21800 cp_parser_exception_declaration (cp_parser* parser)
21801 {
21802   cp_decl_specifier_seq type_specifiers;
21803   cp_declarator *declarator;
21804   const char *saved_message;
21805
21806   /* If it's an ellipsis, it's easy to handle.  */
21807   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21808     {
21809       /* Consume the `...' token.  */
21810       cp_lexer_consume_token (parser->lexer);
21811       return NULL_TREE;
21812     }
21813
21814   /* Types may not be defined in exception-declarations.  */
21815   saved_message = parser->type_definition_forbidden_message;
21816   parser->type_definition_forbidden_message
21817     = G_("types may not be defined in exception-declarations");
21818
21819   /* Parse the type-specifier-seq.  */
21820   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21821                                 /*is_trailing_return=*/false,
21822                                 &type_specifiers);
21823   /* If it's a `)', then there is no declarator.  */
21824   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21825     declarator = NULL;
21826   else
21827     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21828                                        /*ctor_dtor_or_conv_p=*/NULL,
21829                                        /*parenthesized_p=*/NULL,
21830                                        /*member_p=*/false,
21831                                        /*friend_p=*/false);
21832
21833   /* Restore the saved message.  */
21834   parser->type_definition_forbidden_message = saved_message;
21835
21836   if (!type_specifiers.any_specifiers_p)
21837     return error_mark_node;
21838
21839   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21840 }
21841
21842 /* Parse a throw-expression.
21843
21844    throw-expression:
21845      throw assignment-expression [opt]
21846
21847    Returns a THROW_EXPR representing the throw-expression.  */
21848
21849 static tree
21850 cp_parser_throw_expression (cp_parser* parser)
21851 {
21852   tree expression;
21853   cp_token* token;
21854
21855   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21856   token = cp_lexer_peek_token (parser->lexer);
21857   /* Figure out whether or not there is an assignment-expression
21858      following the "throw" keyword.  */
21859   if (token->type == CPP_COMMA
21860       || token->type == CPP_SEMICOLON
21861       || token->type == CPP_CLOSE_PAREN
21862       || token->type == CPP_CLOSE_SQUARE
21863       || token->type == CPP_CLOSE_BRACE
21864       || token->type == CPP_COLON)
21865     expression = NULL_TREE;
21866   else
21867     expression = cp_parser_assignment_expression (parser);
21868
21869   return build_throw (expression);
21870 }
21871
21872 /* GNU Extensions */
21873
21874 /* Parse an (optional) asm-specification.
21875
21876    asm-specification:
21877      asm ( string-literal )
21878
21879    If the asm-specification is present, returns a STRING_CST
21880    corresponding to the string-literal.  Otherwise, returns
21881    NULL_TREE.  */
21882
21883 static tree
21884 cp_parser_asm_specification_opt (cp_parser* parser)
21885 {
21886   cp_token *token;
21887   tree asm_specification;
21888
21889   /* Peek at the next token.  */
21890   token = cp_lexer_peek_token (parser->lexer);
21891   /* If the next token isn't the `asm' keyword, then there's no
21892      asm-specification.  */
21893   if (!cp_parser_is_keyword (token, RID_ASM))
21894     return NULL_TREE;
21895
21896   /* Consume the `asm' token.  */
21897   cp_lexer_consume_token (parser->lexer);
21898   /* Look for the `('.  */
21899   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21900
21901   /* Look for the string-literal.  */
21902   asm_specification = cp_parser_string_literal (parser, false, false);
21903
21904   /* Look for the `)'.  */
21905   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21906
21907   return asm_specification;
21908 }
21909
21910 /* Parse an asm-operand-list.
21911
21912    asm-operand-list:
21913      asm-operand
21914      asm-operand-list , asm-operand
21915
21916    asm-operand:
21917      string-literal ( expression )
21918      [ string-literal ] string-literal ( expression )
21919
21920    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
21921    each node is the expression.  The TREE_PURPOSE is itself a
21922    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21923    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21924    is a STRING_CST for the string literal before the parenthesis. Returns
21925    ERROR_MARK_NODE if any of the operands are invalid.  */
21926
21927 static tree
21928 cp_parser_asm_operand_list (cp_parser* parser)
21929 {
21930   tree asm_operands = NULL_TREE;
21931   bool invalid_operands = false;
21932
21933   while (true)
21934     {
21935       tree string_literal;
21936       tree expression;
21937       tree name;
21938
21939       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21940         {
21941           /* Consume the `[' token.  */
21942           cp_lexer_consume_token (parser->lexer);
21943           /* Read the operand name.  */
21944           name = cp_parser_identifier (parser);
21945           if (name != error_mark_node)
21946             name = build_string (IDENTIFIER_LENGTH (name),
21947                                  IDENTIFIER_POINTER (name));
21948           /* Look for the closing `]'.  */
21949           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21950         }
21951       else
21952         name = NULL_TREE;
21953       /* Look for the string-literal.  */
21954       string_literal = cp_parser_string_literal (parser, false, false);
21955
21956       /* Look for the `('.  */
21957       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21958       /* Parse the expression.  */
21959       expression = cp_parser_expression (parser);
21960       /* Look for the `)'.  */
21961       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21962
21963       if (name == error_mark_node 
21964           || string_literal == error_mark_node 
21965           || expression == error_mark_node)
21966         invalid_operands = true;
21967
21968       /* Add this operand to the list.  */
21969       asm_operands = tree_cons (build_tree_list (name, string_literal),
21970                                 expression,
21971                                 asm_operands);
21972       /* If the next token is not a `,', there are no more
21973          operands.  */
21974       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21975         break;
21976       /* Consume the `,'.  */
21977       cp_lexer_consume_token (parser->lexer);
21978     }
21979
21980   return invalid_operands ? error_mark_node : nreverse (asm_operands);
21981 }
21982
21983 /* Parse an asm-clobber-list.
21984
21985    asm-clobber-list:
21986      string-literal
21987      asm-clobber-list , string-literal
21988
21989    Returns a TREE_LIST, indicating the clobbers in the order that they
21990    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
21991
21992 static tree
21993 cp_parser_asm_clobber_list (cp_parser* parser)
21994 {
21995   tree clobbers = NULL_TREE;
21996
21997   while (true)
21998     {
21999       tree string_literal;
22000
22001       /* Look for the string literal.  */
22002       string_literal = cp_parser_string_literal (parser, false, false);
22003       /* Add it to the list.  */
22004       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22005       /* If the next token is not a `,', then the list is
22006          complete.  */
22007       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22008         break;
22009       /* Consume the `,' token.  */
22010       cp_lexer_consume_token (parser->lexer);
22011     }
22012
22013   return clobbers;
22014 }
22015
22016 /* Parse an asm-label-list.
22017
22018    asm-label-list:
22019      identifier
22020      asm-label-list , identifier
22021
22022    Returns a TREE_LIST, indicating the labels in the order that they
22023    appeared.  The TREE_VALUE of each node is a label.  */
22024
22025 static tree
22026 cp_parser_asm_label_list (cp_parser* parser)
22027 {
22028   tree labels = NULL_TREE;
22029
22030   while (true)
22031     {
22032       tree identifier, label, name;
22033
22034       /* Look for the identifier.  */
22035       identifier = cp_parser_identifier (parser);
22036       if (!error_operand_p (identifier))
22037         {
22038           label = lookup_label (identifier);
22039           if (TREE_CODE (label) == LABEL_DECL)
22040             {
22041               TREE_USED (label) = 1;
22042               check_goto (label);
22043               name = build_string (IDENTIFIER_LENGTH (identifier),
22044                                    IDENTIFIER_POINTER (identifier));
22045               labels = tree_cons (name, label, labels);
22046             }
22047         }
22048       /* If the next token is not a `,', then the list is
22049          complete.  */
22050       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22051         break;
22052       /* Consume the `,' token.  */
22053       cp_lexer_consume_token (parser->lexer);
22054     }
22055
22056   return nreverse (labels);
22057 }
22058
22059 /* Return TRUE iff the next tokens in the stream are possibly the
22060    beginning of a GNU extension attribute. */
22061
22062 static bool
22063 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22064 {
22065   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22066 }
22067
22068 /* Return TRUE iff the next tokens in the stream are possibly the
22069    beginning of a standard C++-11 attribute specifier.  */
22070
22071 static bool
22072 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22073 {
22074   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22075 }
22076
22077 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22078    beginning of a standard C++-11 attribute specifier.  */
22079
22080 static bool
22081 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22082 {
22083   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22084
22085   return (cxx_dialect >= cxx11
22086           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22087               || (token->type == CPP_OPEN_SQUARE
22088                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22089                   && token->type == CPP_OPEN_SQUARE)));
22090 }
22091
22092 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22093    beginning of a GNU extension attribute.  */
22094
22095 static bool
22096 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22097 {
22098   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22099
22100   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22101 }
22102
22103 /* Return true iff the next tokens can be the beginning of either a
22104    GNU attribute list, or a standard C++11 attribute sequence.  */
22105
22106 static bool
22107 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22108 {
22109   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22110           || cp_next_tokens_can_be_std_attribute_p (parser));
22111 }
22112
22113 /* Return true iff the next Nth tokens can be the beginning of either
22114    a GNU attribute list, or a standard C++11 attribute sequence.  */
22115
22116 static bool
22117 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22118 {
22119   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22120           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22121 }
22122
22123 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22124    of GNU attributes, or return NULL.  */
22125
22126 static tree
22127 cp_parser_attributes_opt (cp_parser *parser)
22128 {
22129   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22130       return cp_parser_gnu_attributes_opt (parser);
22131   return cp_parser_std_attribute_spec_seq (parser);
22132 }
22133
22134 #define CILK_SIMD_FN_CLAUSE_MASK                                  \
22135         ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH)   \
22136          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR)       \
22137          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM)      \
22138          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK)         \
22139          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22140
22141 /* Parses the Cilk Plus SIMD-enabled function's attribute.  Syntax:
22142    vector [(<clauses>)]  */
22143
22144 static void
22145 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22146 {  
22147   bool first_p = parser->cilk_simd_fn_info == NULL;
22148   cp_token *token = v_token;
22149   if (first_p)
22150     {
22151       parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22152       parser->cilk_simd_fn_info->error_seen = false;
22153       parser->cilk_simd_fn_info->fndecl_seen = false;
22154       parser->cilk_simd_fn_info->tokens = vNULL;
22155     }
22156   int paren_scope = 0;
22157   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22158     {
22159       cp_lexer_consume_token (parser->lexer);
22160       v_token = cp_lexer_peek_token (parser->lexer);
22161       paren_scope++;
22162     }
22163   while (paren_scope > 0)
22164     {
22165       token = cp_lexer_peek_token (parser->lexer);
22166       if (token->type == CPP_OPEN_PAREN)
22167         paren_scope++;
22168       else if (token->type == CPP_CLOSE_PAREN)
22169         paren_scope--;
22170       /* Do not push the last ')'  */
22171       if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22172         cp_lexer_consume_token (parser->lexer);
22173     }
22174
22175   token->type = CPP_PRAGMA_EOL;
22176   parser->lexer->next_token = token;
22177   cp_lexer_consume_token (parser->lexer);
22178
22179   struct cp_token_cache *cp
22180     = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22181   parser->cilk_simd_fn_info->tokens.safe_push (cp);
22182 }
22183
22184 /* Parse an (optional) series of attributes.
22185
22186    attributes:
22187      attributes attribute
22188
22189    attribute:
22190      __attribute__ (( attribute-list [opt] ))
22191
22192    The return value is as for cp_parser_gnu_attribute_list.  */
22193
22194 static tree
22195 cp_parser_gnu_attributes_opt (cp_parser* parser)
22196 {
22197   tree attributes = NULL_TREE;
22198
22199   while (true)
22200     {
22201       cp_token *token;
22202       tree attribute_list;
22203       bool ok = true;
22204
22205       /* Peek at the next token.  */
22206       token = cp_lexer_peek_token (parser->lexer);
22207       /* If it's not `__attribute__', then we're done.  */
22208       if (token->keyword != RID_ATTRIBUTE)
22209         break;
22210
22211       /* Consume the `__attribute__' keyword.  */
22212       cp_lexer_consume_token (parser->lexer);
22213       /* Look for the two `(' tokens.  */
22214       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22215       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22216
22217       /* Peek at the next token.  */
22218       token = cp_lexer_peek_token (parser->lexer);
22219       if (token->type != CPP_CLOSE_PAREN)
22220         /* Parse the attribute-list.  */
22221         attribute_list = cp_parser_gnu_attribute_list (parser);
22222       else
22223         /* If the next token is a `)', then there is no attribute
22224            list.  */
22225         attribute_list = NULL;
22226
22227       /* Look for the two `)' tokens.  */
22228       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22229         ok = false;
22230       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22231         ok = false;
22232       if (!ok)
22233         cp_parser_skip_to_end_of_statement (parser);
22234
22235       /* Add these new attributes to the list.  */
22236       attributes = chainon (attributes, attribute_list);
22237     }
22238
22239   return attributes;
22240 }
22241
22242 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22243    "__vector" or "__vector__."  */
22244
22245 static inline bool
22246 is_cilkplus_vector_p (tree name)
22247
22248   if (flag_cilkplus && is_attribute_p ("vector", name)) 
22249     return true;
22250   return false;
22251 }
22252
22253 /* Parse a GNU attribute-list.
22254
22255    attribute-list:
22256      attribute
22257      attribute-list , attribute
22258
22259    attribute:
22260      identifier
22261      identifier ( identifier )
22262      identifier ( identifier , expression-list )
22263      identifier ( expression-list )
22264
22265    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
22266    to an attribute.  The TREE_PURPOSE of each node is the identifier
22267    indicating which attribute is in use.  The TREE_VALUE represents
22268    the arguments, if any.  */
22269
22270 static tree
22271 cp_parser_gnu_attribute_list (cp_parser* parser)
22272 {
22273   tree attribute_list = NULL_TREE;
22274   bool save_translate_strings_p = parser->translate_strings_p;
22275
22276   parser->translate_strings_p = false;
22277   while (true)
22278     {
22279       cp_token *token;
22280       tree identifier;
22281       tree attribute;
22282
22283       /* Look for the identifier.  We also allow keywords here; for
22284          example `__attribute__ ((const))' is legal.  */
22285       token = cp_lexer_peek_token (parser->lexer);
22286       if (token->type == CPP_NAME
22287           || token->type == CPP_KEYWORD)
22288         {
22289           tree arguments = NULL_TREE;
22290
22291           /* Consume the token, but save it since we need it for the
22292              SIMD enabled function parsing.  */
22293           cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22294
22295           /* Save away the identifier that indicates which attribute
22296              this is.  */
22297           identifier = (token->type == CPP_KEYWORD) 
22298             /* For keywords, use the canonical spelling, not the
22299                parsed identifier.  */
22300             ? ridpointers[(int) token->keyword]
22301             : id_token->u.value;
22302           
22303           attribute = build_tree_list (identifier, NULL_TREE);
22304
22305           /* Peek at the next token.  */
22306           token = cp_lexer_peek_token (parser->lexer);
22307           /* If it's an `(', then parse the attribute arguments.  */
22308           if (token->type == CPP_OPEN_PAREN)
22309             {
22310               vec<tree, va_gc> *vec;
22311               int attr_flag = (attribute_takes_identifier_p (identifier)
22312                                ? id_attr : normal_attr);
22313               if (is_cilkplus_vector_p (identifier))
22314                 {
22315                   cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22316                   continue;
22317                 }
22318               else
22319                 vec = cp_parser_parenthesized_expression_list 
22320                   (parser, attr_flag, /*cast_p=*/false, 
22321                    /*allow_expansion_p=*/false, 
22322                    /*non_constant_p=*/NULL);
22323               if (vec == NULL)
22324                 arguments = error_mark_node;
22325               else
22326                 {
22327                   arguments = build_tree_list_vec (vec);
22328                   release_tree_vector (vec);
22329                 }
22330               /* Save the arguments away.  */
22331               TREE_VALUE (attribute) = arguments;
22332             }
22333           else if (is_cilkplus_vector_p (identifier))
22334             {
22335               cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22336               continue;
22337             }
22338
22339           if (arguments != error_mark_node)
22340             {
22341               /* Add this attribute to the list.  */
22342               TREE_CHAIN (attribute) = attribute_list;
22343               attribute_list = attribute;
22344             }
22345
22346           token = cp_lexer_peek_token (parser->lexer);
22347         }
22348       /* Now, look for more attributes.  If the next token isn't a
22349          `,', we're done.  */
22350       if (token->type != CPP_COMMA)
22351         break;
22352
22353       /* Consume the comma and keep going.  */
22354       cp_lexer_consume_token (parser->lexer);
22355     }
22356   parser->translate_strings_p = save_translate_strings_p;
22357
22358   /* We built up the list in reverse order.  */
22359   return nreverse (attribute_list);
22360 }
22361
22362 /*  Parse a standard C++11 attribute.
22363
22364     The returned representation is a TREE_LIST which TREE_PURPOSE is
22365     the scoped name of the attribute, and the TREE_VALUE is its
22366     arguments list.
22367
22368     Note that the scoped name of the attribute is itself a TREE_LIST
22369     which TREE_PURPOSE is the namespace of the attribute, and
22370     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
22371     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22372     and which TREE_PURPOSE is directly the attribute name.
22373
22374     Clients of the attribute code should use get_attribute_namespace
22375     and get_attribute_name to get the actual namespace and name of
22376     attributes, regardless of their being GNU or C++11 attributes.
22377
22378     attribute:
22379       attribute-token attribute-argument-clause [opt]
22380
22381     attribute-token:
22382       identifier
22383       attribute-scoped-token
22384
22385     attribute-scoped-token:
22386       attribute-namespace :: identifier
22387
22388     attribute-namespace:
22389       identifier
22390
22391     attribute-argument-clause:
22392       ( balanced-token-seq )
22393
22394     balanced-token-seq:
22395       balanced-token [opt]
22396       balanced-token-seq balanced-token
22397
22398     balanced-token:
22399       ( balanced-token-seq )
22400       [ balanced-token-seq ]
22401       { balanced-token-seq }.  */
22402
22403 static tree
22404 cp_parser_std_attribute (cp_parser *parser)
22405 {
22406   tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22407   cp_token *token;
22408
22409   /* First, parse name of the the attribute, a.k.a
22410      attribute-token.  */
22411
22412   token = cp_lexer_peek_token (parser->lexer);
22413   if (token->type == CPP_NAME)
22414     attr_id = token->u.value;
22415   else if (token->type == CPP_KEYWORD)
22416     attr_id = ridpointers[(int) token->keyword];
22417   else if (token->flags & NAMED_OP)
22418     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22419
22420   if (attr_id == NULL_TREE)
22421     return NULL_TREE;
22422
22423   cp_lexer_consume_token (parser->lexer);
22424
22425   token = cp_lexer_peek_token (parser->lexer);
22426   if (token->type == CPP_SCOPE)
22427     {
22428       /* We are seeing a scoped attribute token.  */
22429
22430       cp_lexer_consume_token (parser->lexer);
22431       attr_ns = attr_id;
22432
22433       token = cp_lexer_consume_token (parser->lexer);
22434       if (token->type == CPP_NAME)
22435         attr_id = token->u.value;
22436       else if (token->type == CPP_KEYWORD)
22437         attr_id = ridpointers[(int) token->keyword];
22438       else
22439         {
22440           error_at (token->location,
22441                     "expected an identifier for the attribute name");
22442           return error_mark_node;
22443         }
22444       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22445                                    NULL_TREE);
22446       token = cp_lexer_peek_token (parser->lexer);
22447     }
22448   else
22449     {
22450       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22451                                    NULL_TREE);
22452       /* C++11 noreturn attribute is equivalent to GNU's.  */
22453       if (is_attribute_p ("noreturn", attr_id))
22454         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22455       /* C++14 deprecated attribute is equivalent to GNU's.  */
22456       else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22457         {
22458           if (cxx_dialect == cxx11)
22459             pedwarn (token->location, OPT_Wpedantic,
22460                      "%<deprecated%> is a C++14 feature;"
22461                      " use %<gnu::deprecated%>");
22462           TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22463         }
22464     }
22465
22466   /* Now parse the optional argument clause of the attribute.  */
22467
22468   if (token->type != CPP_OPEN_PAREN)
22469     return attribute;
22470
22471   {
22472     vec<tree, va_gc> *vec;
22473     int attr_flag = normal_attr;
22474
22475     if (attr_ns == get_identifier ("gnu")
22476         && attribute_takes_identifier_p (attr_id))
22477       /* A GNU attribute that takes an identifier in parameter.  */
22478       attr_flag = id_attr;
22479
22480     vec = cp_parser_parenthesized_expression_list
22481       (parser, attr_flag, /*cast_p=*/false,
22482        /*allow_expansion_p=*/true,
22483        /*non_constant_p=*/NULL);
22484     if (vec == NULL)
22485       arguments = error_mark_node;
22486     else
22487       {
22488         arguments = build_tree_list_vec (vec);
22489         release_tree_vector (vec);
22490       }
22491
22492     if (arguments == error_mark_node)
22493       attribute = error_mark_node;
22494     else
22495       TREE_VALUE (attribute) = arguments;
22496   }
22497
22498   return attribute;
22499 }
22500
22501 /* Parse a list of standard C++-11 attributes.
22502
22503    attribute-list:
22504      attribute [opt]
22505      attribute-list , attribute[opt]
22506      attribute ...
22507      attribute-list , attribute ...
22508 */
22509
22510 static tree
22511 cp_parser_std_attribute_list (cp_parser *parser)
22512 {
22513   tree attributes = NULL_TREE, attribute = NULL_TREE;
22514   cp_token *token = NULL;
22515
22516   while (true)
22517     {
22518       attribute = cp_parser_std_attribute (parser);
22519       if (attribute == error_mark_node)
22520         break;
22521       if (attribute != NULL_TREE)
22522         {
22523           TREE_CHAIN (attribute) = attributes;
22524           attributes = attribute;
22525         }
22526       token = cp_lexer_peek_token (parser->lexer);
22527       if (token->type == CPP_ELLIPSIS)
22528         {
22529           cp_lexer_consume_token (parser->lexer);
22530           TREE_VALUE (attribute)
22531             = make_pack_expansion (TREE_VALUE (attribute));
22532           token = cp_lexer_peek_token (parser->lexer);
22533         }
22534       if (token->type != CPP_COMMA)
22535         break;
22536       cp_lexer_consume_token (parser->lexer);
22537     }
22538   attributes = nreverse (attributes);
22539   return attributes;
22540 }
22541
22542 /* Parse a standard C++-11 attribute specifier.
22543
22544    attribute-specifier:
22545      [ [ attribute-list ] ]
22546      alignment-specifier
22547
22548    alignment-specifier:
22549      alignas ( type-id ... [opt] )
22550      alignas ( alignment-expression ... [opt] ).  */
22551
22552 static tree
22553 cp_parser_std_attribute_spec (cp_parser *parser)
22554 {
22555   tree attributes = NULL_TREE;
22556   cp_token *token = cp_lexer_peek_token (parser->lexer);
22557
22558   if (token->type == CPP_OPEN_SQUARE
22559       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22560     {
22561       cp_lexer_consume_token (parser->lexer);
22562       cp_lexer_consume_token (parser->lexer);
22563
22564       attributes = cp_parser_std_attribute_list (parser);
22565
22566       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22567           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22568         cp_parser_skip_to_end_of_statement (parser);
22569       else
22570         /* Warn about parsing c++11 attribute in non-c++1 mode, only
22571            when we are sure that we have actually parsed them.  */
22572         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22573     }
22574   else
22575     {
22576       tree alignas_expr;
22577
22578       /* Look for an alignment-specifier.  */
22579
22580       token = cp_lexer_peek_token (parser->lexer);
22581
22582       if (token->type != CPP_KEYWORD
22583           || token->keyword != RID_ALIGNAS)
22584         return NULL_TREE;
22585
22586       cp_lexer_consume_token (parser->lexer);
22587       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22588
22589       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22590         {
22591           cp_parser_error (parser, "expected %<(%>");
22592           return error_mark_node;
22593         }
22594
22595       cp_parser_parse_tentatively (parser);
22596       alignas_expr = cp_parser_type_id (parser);
22597
22598       if (!cp_parser_parse_definitely (parser))
22599         {
22600           gcc_assert (alignas_expr == error_mark_node
22601                       || alignas_expr == NULL_TREE);
22602
22603           alignas_expr =
22604             cp_parser_assignment_expression (parser);
22605           if (alignas_expr == error_mark_node)
22606             cp_parser_skip_to_end_of_statement (parser);
22607           if (alignas_expr == NULL_TREE
22608               || alignas_expr == error_mark_node)
22609             return alignas_expr;
22610         }
22611
22612       alignas_expr = cxx_alignas_expr (alignas_expr);
22613       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
22614
22615       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22616         {
22617           cp_lexer_consume_token (parser->lexer);
22618           alignas_expr = make_pack_expansion (alignas_expr);
22619         }
22620
22621       if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22622         {
22623           cp_parser_error (parser, "expected %<)%>");
22624           return error_mark_node;
22625         }
22626
22627       /* Build the C++-11 representation of an 'aligned'
22628          attribute.  */
22629       attributes =
22630         build_tree_list (build_tree_list (get_identifier ("gnu"),
22631                                           get_identifier ("aligned")),
22632                          alignas_expr);
22633     }
22634
22635   return attributes;
22636 }
22637
22638 /* Parse a standard C++-11 attribute-specifier-seq.
22639
22640    attribute-specifier-seq:
22641      attribute-specifier-seq [opt] attribute-specifier
22642  */
22643
22644 static tree
22645 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22646 {
22647   tree attr_specs = NULL;
22648
22649   while (true)
22650     {
22651       tree attr_spec = cp_parser_std_attribute_spec (parser);
22652       if (attr_spec == NULL_TREE)
22653         break;
22654       if (attr_spec == error_mark_node)
22655         return error_mark_node;
22656
22657       TREE_CHAIN (attr_spec) = attr_specs;
22658       attr_specs = attr_spec;
22659     }
22660
22661   attr_specs = nreverse (attr_specs);
22662   return attr_specs;
22663 }
22664
22665 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
22666    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
22667    current value of the PEDANTIC flag, regardless of whether or not
22668    the `__extension__' keyword is present.  The caller is responsible
22669    for restoring the value of the PEDANTIC flag.  */
22670
22671 static bool
22672 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22673 {
22674   /* Save the old value of the PEDANTIC flag.  */
22675   *saved_pedantic = pedantic;
22676
22677   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22678     {
22679       /* Consume the `__extension__' token.  */
22680       cp_lexer_consume_token (parser->lexer);
22681       /* We're not being pedantic while the `__extension__' keyword is
22682          in effect.  */
22683       pedantic = 0;
22684
22685       return true;
22686     }
22687
22688   return false;
22689 }
22690
22691 /* Parse a label declaration.
22692
22693    label-declaration:
22694      __label__ label-declarator-seq ;
22695
22696    label-declarator-seq:
22697      identifier , label-declarator-seq
22698      identifier  */
22699
22700 static void
22701 cp_parser_label_declaration (cp_parser* parser)
22702 {
22703   /* Look for the `__label__' keyword.  */
22704   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22705
22706   while (true)
22707     {
22708       tree identifier;
22709
22710       /* Look for an identifier.  */
22711       identifier = cp_parser_identifier (parser);
22712       /* If we failed, stop.  */
22713       if (identifier == error_mark_node)
22714         break;
22715       /* Declare it as a label.  */
22716       finish_label_decl (identifier);
22717       /* If the next token is a `;', stop.  */
22718       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22719         break;
22720       /* Look for the `,' separating the label declarations.  */
22721       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22722     }
22723
22724   /* Look for the final `;'.  */
22725   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22726 }
22727
22728 /* Support Functions */
22729
22730 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22731    NAME should have one of the representations used for an
22732    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22733    is returned.  If PARSER->SCOPE is a dependent type, then a
22734    SCOPE_REF is returned.
22735
22736    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22737    returned; the name was already resolved when the TEMPLATE_ID_EXPR
22738    was formed.  Abstractly, such entities should not be passed to this
22739    function, because they do not need to be looked up, but it is
22740    simpler to check for this special case here, rather than at the
22741    call-sites.
22742
22743    In cases not explicitly covered above, this function returns a
22744    DECL, OVERLOAD, or baselink representing the result of the lookup.
22745    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22746    is returned.
22747
22748    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22749    (e.g., "struct") that was used.  In that case bindings that do not
22750    refer to types are ignored.
22751
22752    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22753    ignored.
22754
22755    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22756    are ignored.
22757
22758    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22759    types.
22760
22761    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22762    TREE_LIST of candidates if name-lookup results in an ambiguity, and
22763    NULL_TREE otherwise.  */
22764
22765 static tree
22766 cp_parser_lookup_name (cp_parser *parser, tree name,
22767                        enum tag_types tag_type,
22768                        bool is_template,
22769                        bool is_namespace,
22770                        bool check_dependency,
22771                        tree *ambiguous_decls,
22772                        location_t name_location)
22773 {
22774   tree decl;
22775   tree object_type = parser->context->object_type;
22776
22777   /* Assume that the lookup will be unambiguous.  */
22778   if (ambiguous_decls)
22779     *ambiguous_decls = NULL_TREE;
22780
22781   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22782      no longer valid.  Note that if we are parsing tentatively, and
22783      the parse fails, OBJECT_TYPE will be automatically restored.  */
22784   parser->context->object_type = NULL_TREE;
22785
22786   if (name == error_mark_node)
22787     return error_mark_node;
22788
22789   /* A template-id has already been resolved; there is no lookup to
22790      do.  */
22791   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22792     return name;
22793   if (BASELINK_P (name))
22794     {
22795       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22796                   == TEMPLATE_ID_EXPR);
22797       return name;
22798     }
22799
22800   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
22801      it should already have been checked to make sure that the name
22802      used matches the type being destroyed.  */
22803   if (TREE_CODE (name) == BIT_NOT_EXPR)
22804     {
22805       tree type;
22806
22807       /* Figure out to which type this destructor applies.  */
22808       if (parser->scope)
22809         type = parser->scope;
22810       else if (object_type)
22811         type = object_type;
22812       else
22813         type = current_class_type;
22814       /* If that's not a class type, there is no destructor.  */
22815       if (!type || !CLASS_TYPE_P (type))
22816         return error_mark_node;
22817       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22818         lazily_declare_fn (sfk_destructor, type);
22819       if (!CLASSTYPE_DESTRUCTORS (type))
22820           return error_mark_node;
22821       /* If it was a class type, return the destructor.  */
22822       return CLASSTYPE_DESTRUCTORS (type);
22823     }
22824
22825   /* By this point, the NAME should be an ordinary identifier.  If
22826      the id-expression was a qualified name, the qualifying scope is
22827      stored in PARSER->SCOPE at this point.  */
22828   gcc_assert (identifier_p (name));
22829
22830   /* Perform the lookup.  */
22831   if (parser->scope)
22832     {
22833       bool dependent_p;
22834
22835       if (parser->scope == error_mark_node)
22836         return error_mark_node;
22837
22838       /* If the SCOPE is dependent, the lookup must be deferred until
22839          the template is instantiated -- unless we are explicitly
22840          looking up names in uninstantiated templates.  Even then, we
22841          cannot look up the name if the scope is not a class type; it
22842          might, for example, be a template type parameter.  */
22843       dependent_p = (TYPE_P (parser->scope)
22844                      && dependent_scope_p (parser->scope));
22845       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22846           && dependent_p)
22847         /* Defer lookup.  */
22848         decl = error_mark_node;
22849       else
22850         {
22851           tree pushed_scope = NULL_TREE;
22852
22853           /* If PARSER->SCOPE is a dependent type, then it must be a
22854              class type, and we must not be checking dependencies;
22855              otherwise, we would have processed this lookup above.  So
22856              that PARSER->SCOPE is not considered a dependent base by
22857              lookup_member, we must enter the scope here.  */
22858           if (dependent_p)
22859             pushed_scope = push_scope (parser->scope);
22860
22861           /* If the PARSER->SCOPE is a template specialization, it
22862              may be instantiated during name lookup.  In that case,
22863              errors may be issued.  Even if we rollback the current
22864              tentative parse, those errors are valid.  */
22865           decl = lookup_qualified_name (parser->scope, name,
22866                                         tag_type != none_type,
22867                                         /*complain=*/true);
22868
22869           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22870              lookup result and the nested-name-specifier nominates a class C:
22871                * if the name specified after the nested-name-specifier, when
22872                looked up in C, is the injected-class-name of C (Clause 9), or
22873                * if the name specified after the nested-name-specifier is the
22874                same as the identifier or the simple-template-id's template-
22875                name in the last component of the nested-name-specifier,
22876              the name is instead considered to name the constructor of
22877              class C. [ Note: for example, the constructor is not an
22878              acceptable lookup result in an elaborated-type-specifier so
22879              the constructor would not be used in place of the
22880              injected-class-name. --end note ] Such a constructor name
22881              shall be used only in the declarator-id of a declaration that
22882              names a constructor or in a using-declaration.  */
22883           if (tag_type == none_type
22884               && DECL_SELF_REFERENCE_P (decl)
22885               && same_type_p (DECL_CONTEXT (decl), parser->scope))
22886             decl = lookup_qualified_name (parser->scope, ctor_identifier,
22887                                           tag_type != none_type,
22888                                           /*complain=*/true);
22889
22890           /* If we have a single function from a using decl, pull it out.  */
22891           if (TREE_CODE (decl) == OVERLOAD
22892               && !really_overloaded_fn (decl))
22893             decl = OVL_FUNCTION (decl);
22894
22895           if (pushed_scope)
22896             pop_scope (pushed_scope);
22897         }
22898
22899       /* If the scope is a dependent type and either we deferred lookup or
22900          we did lookup but didn't find the name, rememeber the name.  */
22901       if (decl == error_mark_node && TYPE_P (parser->scope)
22902           && dependent_type_p (parser->scope))
22903         {
22904           if (tag_type)
22905             {
22906               tree type;
22907
22908               /* The resolution to Core Issue 180 says that `struct
22909                  A::B' should be considered a type-name, even if `A'
22910                  is dependent.  */
22911               type = make_typename_type (parser->scope, name, tag_type,
22912                                          /*complain=*/tf_error);
22913               if (type != error_mark_node)
22914                 decl = TYPE_NAME (type);
22915             }
22916           else if (is_template
22917                    && (cp_parser_next_token_ends_template_argument_p (parser)
22918                        || cp_lexer_next_token_is (parser->lexer,
22919                                                   CPP_CLOSE_PAREN)))
22920             decl = make_unbound_class_template (parser->scope,
22921                                                 name, NULL_TREE,
22922                                                 /*complain=*/tf_error);
22923           else
22924             decl = build_qualified_name (/*type=*/NULL_TREE,
22925                                          parser->scope, name,
22926                                          is_template);
22927         }
22928       parser->qualifying_scope = parser->scope;
22929       parser->object_scope = NULL_TREE;
22930     }
22931   else if (object_type)
22932     {
22933       /* Look up the name in the scope of the OBJECT_TYPE, unless the
22934          OBJECT_TYPE is not a class.  */
22935       if (CLASS_TYPE_P (object_type))
22936         /* If the OBJECT_TYPE is a template specialization, it may
22937            be instantiated during name lookup.  In that case, errors
22938            may be issued.  Even if we rollback the current tentative
22939            parse, those errors are valid.  */
22940         decl = lookup_member (object_type,
22941                               name,
22942                               /*protect=*/0,
22943                               tag_type != none_type,
22944                               tf_warning_or_error);
22945       else
22946         decl = NULL_TREE;
22947
22948       if (!decl)
22949         /* Look it up in the enclosing context.  */
22950         decl = lookup_name_real (name, tag_type != none_type,
22951                                  /*nonclass=*/0,
22952                                  /*block_p=*/true, is_namespace, 0);
22953       parser->object_scope = object_type;
22954       parser->qualifying_scope = NULL_TREE;
22955     }
22956   else
22957     {
22958       decl = lookup_name_real (name, tag_type != none_type,
22959                                /*nonclass=*/0,
22960                                /*block_p=*/true, is_namespace, 0);
22961       parser->qualifying_scope = NULL_TREE;
22962       parser->object_scope = NULL_TREE;
22963     }
22964
22965   /* If the lookup failed, let our caller know.  */
22966   if (!decl || decl == error_mark_node)
22967     return error_mark_node;
22968
22969   /* Pull out the template from an injected-class-name (or multiple).  */
22970   if (is_template)
22971     decl = maybe_get_template_decl_from_type_decl (decl);
22972
22973   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
22974   if (TREE_CODE (decl) == TREE_LIST)
22975     {
22976       if (ambiguous_decls)
22977         *ambiguous_decls = decl;
22978       /* The error message we have to print is too complicated for
22979          cp_parser_error, so we incorporate its actions directly.  */
22980       if (!cp_parser_simulate_error (parser))
22981         {
22982           error_at (name_location, "reference to %qD is ambiguous",
22983                     name);
22984           print_candidates (decl);
22985         }
22986       return error_mark_node;
22987     }
22988
22989   gcc_assert (DECL_P (decl)
22990               || TREE_CODE (decl) == OVERLOAD
22991               || TREE_CODE (decl) == SCOPE_REF
22992               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22993               || BASELINK_P (decl));
22994
22995   /* If we have resolved the name of a member declaration, check to
22996      see if the declaration is accessible.  When the name resolves to
22997      set of overloaded functions, accessibility is checked when
22998      overload resolution is done.
22999
23000      During an explicit instantiation, access is not checked at all,
23001      as per [temp.explicit].  */
23002   if (DECL_P (decl))
23003     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23004
23005   maybe_record_typedef_use (decl);
23006
23007   return decl;
23008 }
23009
23010 /* Like cp_parser_lookup_name, but for use in the typical case where
23011    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23012    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
23013
23014 static tree
23015 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23016 {
23017   return cp_parser_lookup_name (parser, name,
23018                                 none_type,
23019                                 /*is_template=*/false,
23020                                 /*is_namespace=*/false,
23021                                 /*check_dependency=*/true,
23022                                 /*ambiguous_decls=*/NULL,
23023                                 location);
23024 }
23025
23026 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23027    the current context, return the TYPE_DECL.  If TAG_NAME_P is
23028    true, the DECL indicates the class being defined in a class-head,
23029    or declared in an elaborated-type-specifier.
23030
23031    Otherwise, return DECL.  */
23032
23033 static tree
23034 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23035 {
23036   /* If the TEMPLATE_DECL is being declared as part of a class-head,
23037      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23038
23039        struct A {
23040          template <typename T> struct B;
23041        };
23042
23043        template <typename T> struct A::B {};
23044
23045      Similarly, in an elaborated-type-specifier:
23046
23047        namespace N { struct X{}; }
23048
23049        struct A {
23050          template <typename T> friend struct N::X;
23051        };
23052
23053      However, if the DECL refers to a class type, and we are in
23054      the scope of the class, then the name lookup automatically
23055      finds the TYPE_DECL created by build_self_reference rather
23056      than a TEMPLATE_DECL.  For example, in:
23057
23058        template <class T> struct S {
23059          S s;
23060        };
23061
23062      there is no need to handle such case.  */
23063
23064   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23065     return DECL_TEMPLATE_RESULT (decl);
23066
23067   return decl;
23068 }
23069
23070 /* If too many, or too few, template-parameter lists apply to the
23071    declarator, issue an error message.  Returns TRUE if all went well,
23072    and FALSE otherwise.  */
23073
23074 static bool
23075 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23076                                                 cp_declarator *declarator,
23077                                                 location_t declarator_location)
23078 {
23079   switch (declarator->kind)
23080     {
23081     case cdk_id:
23082       {
23083         unsigned num_templates = 0;
23084         tree scope = declarator->u.id.qualifying_scope;
23085
23086         if (scope)
23087           num_templates = num_template_headers_for_class (scope);
23088         else if (TREE_CODE (declarator->u.id.unqualified_name)
23089                  == TEMPLATE_ID_EXPR)
23090           /* If the DECLARATOR has the form `X<y>' then it uses one
23091              additional level of template parameters.  */
23092           ++num_templates;
23093
23094         return cp_parser_check_template_parameters 
23095           (parser, num_templates, declarator_location, declarator);
23096       }
23097
23098     case cdk_function:
23099     case cdk_array:
23100     case cdk_pointer:
23101     case cdk_reference:
23102     case cdk_ptrmem:
23103       return (cp_parser_check_declarator_template_parameters
23104               (parser, declarator->declarator, declarator_location));
23105
23106     case cdk_error:
23107       return true;
23108
23109     default:
23110       gcc_unreachable ();
23111     }
23112   return false;
23113 }
23114
23115 /* NUM_TEMPLATES were used in the current declaration.  If that is
23116    invalid, return FALSE and issue an error messages.  Otherwise,
23117    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
23118    declarator and we can print more accurate diagnostics.  */
23119
23120 static bool
23121 cp_parser_check_template_parameters (cp_parser* parser,
23122                                      unsigned num_templates,
23123                                      location_t location,
23124                                      cp_declarator *declarator)
23125 {
23126   /* If there are the same number of template classes and parameter
23127      lists, that's OK.  */
23128   if (parser->num_template_parameter_lists == num_templates)
23129     return true;
23130   /* If there are more, but only one more, then we are referring to a
23131      member template.  That's OK too.  */
23132   if (parser->num_template_parameter_lists == num_templates + 1)
23133     return true;
23134   /* If there are more template classes than parameter lists, we have
23135      something like:
23136
23137        template <class T> void S<T>::R<T>::f ();  */
23138   if (parser->num_template_parameter_lists < num_templates)
23139     {
23140       if (declarator && !current_function_decl)
23141         error_at (location, "specializing member %<%T::%E%> "
23142                   "requires %<template<>%> syntax", 
23143                   declarator->u.id.qualifying_scope,
23144                   declarator->u.id.unqualified_name);
23145       else if (declarator)
23146         error_at (location, "invalid declaration of %<%T::%E%>",
23147                   declarator->u.id.qualifying_scope,
23148                   declarator->u.id.unqualified_name);
23149       else 
23150         error_at (location, "too few template-parameter-lists");
23151       return false;
23152     }
23153   /* Otherwise, there are too many template parameter lists.  We have
23154      something like:
23155
23156      template <class T> template <class U> void S::f();  */
23157   error_at (location, "too many template-parameter-lists");
23158   return false;
23159 }
23160
23161 /* Parse an optional `::' token indicating that the following name is
23162    from the global namespace.  If so, PARSER->SCOPE is set to the
23163    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23164    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23165    Returns the new value of PARSER->SCOPE, if the `::' token is
23166    present, and NULL_TREE otherwise.  */
23167
23168 static tree
23169 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23170 {
23171   cp_token *token;
23172
23173   /* Peek at the next token.  */
23174   token = cp_lexer_peek_token (parser->lexer);
23175   /* If we're looking at a `::' token then we're starting from the
23176      global namespace, not our current location.  */
23177   if (token->type == CPP_SCOPE)
23178     {
23179       /* Consume the `::' token.  */
23180       cp_lexer_consume_token (parser->lexer);
23181       /* Set the SCOPE so that we know where to start the lookup.  */
23182       parser->scope = global_namespace;
23183       parser->qualifying_scope = global_namespace;
23184       parser->object_scope = NULL_TREE;
23185
23186       return parser->scope;
23187     }
23188   else if (!current_scope_valid_p)
23189     {
23190       parser->scope = NULL_TREE;
23191       parser->qualifying_scope = NULL_TREE;
23192       parser->object_scope = NULL_TREE;
23193     }
23194
23195   return NULL_TREE;
23196 }
23197
23198 /* Returns TRUE if the upcoming token sequence is the start of a
23199    constructor declarator.  If FRIEND_P is true, the declarator is
23200    preceded by the `friend' specifier.  */
23201
23202 static bool
23203 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23204 {
23205   bool constructor_p;
23206   bool outside_class_specifier_p;
23207   tree nested_name_specifier;
23208   cp_token *next_token;
23209
23210   /* The common case is that this is not a constructor declarator, so
23211      try to avoid doing lots of work if at all possible.  It's not
23212      valid declare a constructor at function scope.  */
23213   if (parser->in_function_body)
23214     return false;
23215   /* And only certain tokens can begin a constructor declarator.  */
23216   next_token = cp_lexer_peek_token (parser->lexer);
23217   if (next_token->type != CPP_NAME
23218       && next_token->type != CPP_SCOPE
23219       && next_token->type != CPP_NESTED_NAME_SPECIFIER
23220       && next_token->type != CPP_TEMPLATE_ID)
23221     return false;
23222
23223   /* Parse tentatively; we are going to roll back all of the tokens
23224      consumed here.  */
23225   cp_parser_parse_tentatively (parser);
23226   /* Assume that we are looking at a constructor declarator.  */
23227   constructor_p = true;
23228
23229   /* Look for the optional `::' operator.  */
23230   cp_parser_global_scope_opt (parser,
23231                               /*current_scope_valid_p=*/false);
23232   /* Look for the nested-name-specifier.  */
23233   nested_name_specifier
23234     = (cp_parser_nested_name_specifier_opt (parser,
23235                                             /*typename_keyword_p=*/false,
23236                                             /*check_dependency_p=*/false,
23237                                             /*type_p=*/false,
23238                                             /*is_declaration=*/false));
23239
23240   outside_class_specifier_p = (!at_class_scope_p ()
23241                                || !TYPE_BEING_DEFINED (current_class_type)
23242                                || friend_p);
23243
23244   /* Outside of a class-specifier, there must be a
23245      nested-name-specifier.  */
23246   if (!nested_name_specifier && outside_class_specifier_p)
23247     constructor_p = false;
23248   else if (nested_name_specifier == error_mark_node)
23249     constructor_p = false;
23250
23251   /* If we have a class scope, this is easy; DR 147 says that S::S always
23252      names the constructor, and no other qualified name could.  */
23253   if (constructor_p && nested_name_specifier
23254       && CLASS_TYPE_P (nested_name_specifier))
23255     {
23256       tree id = cp_parser_unqualified_id (parser,
23257                                           /*template_keyword_p=*/false,
23258                                           /*check_dependency_p=*/false,
23259                                           /*declarator_p=*/true,
23260                                           /*optional_p=*/false);
23261       if (is_overloaded_fn (id))
23262         id = DECL_NAME (get_first_fn (id));
23263       if (!constructor_name_p (id, nested_name_specifier))
23264         constructor_p = false;
23265     }
23266   /* If we still think that this might be a constructor-declarator,
23267      look for a class-name.  */
23268   else if (constructor_p)
23269     {
23270       /* If we have:
23271
23272            template <typename T> struct S {
23273              S();
23274            };
23275
23276          we must recognize that the nested `S' names a class.  */
23277       tree type_decl;
23278       type_decl = cp_parser_class_name (parser,
23279                                         /*typename_keyword_p=*/false,
23280                                         /*template_keyword_p=*/false,
23281                                         none_type,
23282                                         /*check_dependency_p=*/false,
23283                                         /*class_head_p=*/false,
23284                                         /*is_declaration=*/false);
23285       /* If there was no class-name, then this is not a constructor.
23286          Otherwise, if we are in a class-specifier and we aren't
23287          handling a friend declaration, check that its type matches
23288          current_class_type (c++/38313).  Note: error_mark_node
23289          is left alone for error recovery purposes.  */
23290       constructor_p = (!cp_parser_error_occurred (parser)
23291                        && (outside_class_specifier_p
23292                            || type_decl == error_mark_node
23293                            || same_type_p (current_class_type,
23294                                            TREE_TYPE (type_decl))));
23295
23296       /* If we're still considering a constructor, we have to see a `(',
23297          to begin the parameter-declaration-clause, followed by either a
23298          `)', an `...', or a decl-specifier.  We need to check for a
23299          type-specifier to avoid being fooled into thinking that:
23300
23301            S (f) (int);
23302
23303          is a constructor.  (It is actually a function named `f' that
23304          takes one parameter (of type `int') and returns a value of type
23305          `S'.  */
23306       if (constructor_p
23307           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23308         constructor_p = false;
23309
23310       if (constructor_p
23311           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23312           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23313           /* A parameter declaration begins with a decl-specifier,
23314              which is either the "attribute" keyword, a storage class
23315              specifier, or (usually) a type-specifier.  */
23316           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23317         {
23318           tree type;
23319           tree pushed_scope = NULL_TREE;
23320           unsigned saved_num_template_parameter_lists;
23321
23322           /* Names appearing in the type-specifier should be looked up
23323              in the scope of the class.  */
23324           if (current_class_type)
23325             type = NULL_TREE;
23326           else
23327             {
23328               type = TREE_TYPE (type_decl);
23329               if (TREE_CODE (type) == TYPENAME_TYPE)
23330                 {
23331                   type = resolve_typename_type (type,
23332                                                 /*only_current_p=*/false);
23333                   if (TREE_CODE (type) == TYPENAME_TYPE)
23334                     {
23335                       cp_parser_abort_tentative_parse (parser);
23336                       return false;
23337                     }
23338                 }
23339               pushed_scope = push_scope (type);
23340             }
23341
23342           /* Inside the constructor parameter list, surrounding
23343              template-parameter-lists do not apply.  */
23344           saved_num_template_parameter_lists
23345             = parser->num_template_parameter_lists;
23346           parser->num_template_parameter_lists = 0;
23347
23348           /* Look for the type-specifier.  */
23349           cp_parser_type_specifier (parser,
23350                                     CP_PARSER_FLAGS_NONE,
23351                                     /*decl_specs=*/NULL,
23352                                     /*is_declarator=*/true,
23353                                     /*declares_class_or_enum=*/NULL,
23354                                     /*is_cv_qualifier=*/NULL);
23355
23356           parser->num_template_parameter_lists
23357             = saved_num_template_parameter_lists;
23358
23359           /* Leave the scope of the class.  */
23360           if (pushed_scope)
23361             pop_scope (pushed_scope);
23362
23363           constructor_p = !cp_parser_error_occurred (parser);
23364         }
23365     }
23366
23367   /* We did not really want to consume any tokens.  */
23368   cp_parser_abort_tentative_parse (parser);
23369
23370   return constructor_p;
23371 }
23372
23373 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23374    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
23375    they must be performed once we are in the scope of the function.
23376
23377    Returns the function defined.  */
23378
23379 static tree
23380 cp_parser_function_definition_from_specifiers_and_declarator
23381   (cp_parser* parser,
23382    cp_decl_specifier_seq *decl_specifiers,
23383    tree attributes,
23384    const cp_declarator *declarator)
23385 {
23386   tree fn;
23387   bool success_p;
23388
23389   /* Begin the function-definition.  */
23390   success_p = start_function (decl_specifiers, declarator, attributes);
23391
23392   /* The things we're about to see are not directly qualified by any
23393      template headers we've seen thus far.  */
23394   reset_specialization ();
23395
23396   /* If there were names looked up in the decl-specifier-seq that we
23397      did not check, check them now.  We must wait until we are in the
23398      scope of the function to perform the checks, since the function
23399      might be a friend.  */
23400   perform_deferred_access_checks (tf_warning_or_error);
23401
23402   if (success_p)
23403     {
23404       cp_finalize_omp_declare_simd (parser, current_function_decl);
23405       parser->omp_declare_simd = NULL;
23406     }
23407
23408   if (!success_p)
23409     {
23410       /* Skip the entire function.  */
23411       cp_parser_skip_to_end_of_block_or_statement (parser);
23412       fn = error_mark_node;
23413     }
23414   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23415     {
23416       /* Seen already, skip it.  An error message has already been output.  */
23417       cp_parser_skip_to_end_of_block_or_statement (parser);
23418       fn = current_function_decl;
23419       current_function_decl = NULL_TREE;
23420       /* If this is a function from a class, pop the nested class.  */
23421       if (current_class_name)
23422         pop_nested_class ();
23423     }
23424   else
23425     {
23426       timevar_id_t tv;
23427       if (DECL_DECLARED_INLINE_P (current_function_decl))
23428         tv = TV_PARSE_INLINE;
23429       else
23430         tv = TV_PARSE_FUNC;
23431       timevar_push (tv);
23432       fn = cp_parser_function_definition_after_declarator (parser,
23433                                                          /*inline_p=*/false);
23434       timevar_pop (tv);
23435     }
23436
23437   return fn;
23438 }
23439
23440 /* Parse the part of a function-definition that follows the
23441    declarator.  INLINE_P is TRUE iff this function is an inline
23442    function defined within a class-specifier.
23443
23444    Returns the function defined.  */
23445
23446 static tree
23447 cp_parser_function_definition_after_declarator (cp_parser* parser,
23448                                                 bool inline_p)
23449 {
23450   tree fn;
23451   bool ctor_initializer_p = false;
23452   bool saved_in_unbraced_linkage_specification_p;
23453   bool saved_in_function_body;
23454   unsigned saved_num_template_parameter_lists;
23455   cp_token *token;
23456   bool fully_implicit_function_template_p
23457     = parser->fully_implicit_function_template_p;
23458   parser->fully_implicit_function_template_p = false;
23459   tree implicit_template_parms
23460     = parser->implicit_template_parms;
23461   parser->implicit_template_parms = 0;
23462   cp_binding_level* implicit_template_scope
23463     = parser->implicit_template_scope;
23464   parser->implicit_template_scope = 0;
23465
23466   saved_in_function_body = parser->in_function_body;
23467   parser->in_function_body = true;
23468   /* If the next token is `return', then the code may be trying to
23469      make use of the "named return value" extension that G++ used to
23470      support.  */
23471   token = cp_lexer_peek_token (parser->lexer);
23472   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23473     {
23474       /* Consume the `return' keyword.  */
23475       cp_lexer_consume_token (parser->lexer);
23476       /* Look for the identifier that indicates what value is to be
23477          returned.  */
23478       cp_parser_identifier (parser);
23479       /* Issue an error message.  */
23480       error_at (token->location,
23481                 "named return values are no longer supported");
23482       /* Skip tokens until we reach the start of the function body.  */
23483       while (true)
23484         {
23485           cp_token *token = cp_lexer_peek_token (parser->lexer);
23486           if (token->type == CPP_OPEN_BRACE
23487               || token->type == CPP_EOF
23488               || token->type == CPP_PRAGMA_EOL)
23489             break;
23490           cp_lexer_consume_token (parser->lexer);
23491         }
23492     }
23493   /* The `extern' in `extern "C" void f () { ... }' does not apply to
23494      anything declared inside `f'.  */
23495   saved_in_unbraced_linkage_specification_p
23496     = parser->in_unbraced_linkage_specification_p;
23497   parser->in_unbraced_linkage_specification_p = false;
23498   /* Inside the function, surrounding template-parameter-lists do not
23499      apply.  */
23500   saved_num_template_parameter_lists
23501     = parser->num_template_parameter_lists;
23502   parser->num_template_parameter_lists = 0;
23503
23504   start_lambda_scope (current_function_decl);
23505
23506   /* If the next token is `try', `__transaction_atomic', or
23507      `__transaction_relaxed`, then we are looking at either function-try-block
23508      or function-transaction-block.  Note that all of these include the
23509      function-body.  */
23510   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23511     ctor_initializer_p = cp_parser_function_transaction (parser,
23512         RID_TRANSACTION_ATOMIC);
23513   else if (cp_lexer_next_token_is_keyword (parser->lexer,
23514       RID_TRANSACTION_RELAXED))
23515     ctor_initializer_p = cp_parser_function_transaction (parser,
23516         RID_TRANSACTION_RELAXED);
23517   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23518     ctor_initializer_p = cp_parser_function_try_block (parser);
23519   else
23520     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23521       (parser, /*in_function_try_block=*/false);
23522
23523   finish_lambda_scope ();
23524
23525   /* Finish the function.  */
23526   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23527                         (inline_p ? 2 : 0));
23528   /* Generate code for it, if necessary.  */
23529   expand_or_defer_fn (fn);
23530   /* Restore the saved values.  */
23531   parser->in_unbraced_linkage_specification_p
23532     = saved_in_unbraced_linkage_specification_p;
23533   parser->num_template_parameter_lists
23534     = saved_num_template_parameter_lists;
23535   parser->in_function_body = saved_in_function_body;
23536
23537   parser->fully_implicit_function_template_p
23538     = fully_implicit_function_template_p;
23539   parser->implicit_template_parms
23540     = implicit_template_parms;
23541   parser->implicit_template_scope
23542     = implicit_template_scope;
23543
23544   if (parser->fully_implicit_function_template_p)
23545     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23546
23547   return fn;
23548 }
23549
23550 /* Parse a template-declaration, assuming that the `export' (and
23551    `extern') keywords, if present, has already been scanned.  MEMBER_P
23552    is as for cp_parser_template_declaration.  */
23553
23554 static void
23555 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23556 {
23557   tree decl = NULL_TREE;
23558   vec<deferred_access_check, va_gc> *checks;
23559   tree parameter_list;
23560   bool friend_p = false;
23561   bool need_lang_pop;
23562   cp_token *token;
23563
23564   /* Look for the `template' keyword.  */
23565   token = cp_lexer_peek_token (parser->lexer);
23566   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23567     return;
23568
23569   /* And the `<'.  */
23570   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23571     return;
23572   if (at_class_scope_p () && current_function_decl)
23573     {
23574       /* 14.5.2.2 [temp.mem]
23575
23576          A local class shall not have member templates.  */
23577       error_at (token->location,
23578                 "invalid declaration of member template in local class");
23579       cp_parser_skip_to_end_of_block_or_statement (parser);
23580       return;
23581     }
23582   /* [temp]
23583
23584      A template ... shall not have C linkage.  */
23585   if (current_lang_name == lang_name_c)
23586     {
23587       error_at (token->location, "template with C linkage");
23588       /* Give it C++ linkage to avoid confusing other parts of the
23589          front end.  */
23590       push_lang_context (lang_name_cplusplus);
23591       need_lang_pop = true;
23592     }
23593   else
23594     need_lang_pop = false;
23595
23596   /* We cannot perform access checks on the template parameter
23597      declarations until we know what is being declared, just as we
23598      cannot check the decl-specifier list.  */
23599   push_deferring_access_checks (dk_deferred);
23600
23601   /* If the next token is `>', then we have an invalid
23602      specialization.  Rather than complain about an invalid template
23603      parameter, issue an error message here.  */
23604   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23605     {
23606       cp_parser_error (parser, "invalid explicit specialization");
23607       begin_specialization ();
23608       parameter_list = NULL_TREE;
23609     }
23610   else
23611     {
23612       /* Parse the template parameters.  */
23613       parameter_list = cp_parser_template_parameter_list (parser);
23614     }
23615
23616   /* Get the deferred access checks from the parameter list.  These
23617      will be checked once we know what is being declared, as for a
23618      member template the checks must be performed in the scope of the
23619      class containing the member.  */
23620   checks = get_deferred_access_checks ();
23621
23622   /* Look for the `>'.  */
23623   cp_parser_skip_to_end_of_template_parameter_list (parser);
23624   /* We just processed one more parameter list.  */
23625   ++parser->num_template_parameter_lists;
23626   /* If the next token is `template', there are more template
23627      parameters.  */
23628   if (cp_lexer_next_token_is_keyword (parser->lexer,
23629                                       RID_TEMPLATE))
23630     cp_parser_template_declaration_after_export (parser, member_p);
23631   else if (cxx_dialect >= cxx11
23632            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23633     decl = cp_parser_alias_declaration (parser);
23634   else
23635     {
23636       /* There are no access checks when parsing a template, as we do not
23637          know if a specialization will be a friend.  */
23638       push_deferring_access_checks (dk_no_check);
23639       token = cp_lexer_peek_token (parser->lexer);
23640       decl = cp_parser_single_declaration (parser,
23641                                            checks,
23642                                            member_p,
23643                                            /*explicit_specialization_p=*/false,
23644                                            &friend_p);
23645       pop_deferring_access_checks ();
23646
23647       /* If this is a member template declaration, let the front
23648          end know.  */
23649       if (member_p && !friend_p && decl)
23650         {
23651           if (TREE_CODE (decl) == TYPE_DECL)
23652             cp_parser_check_access_in_redeclaration (decl, token->location);
23653
23654           decl = finish_member_template_decl (decl);
23655         }
23656       else if (friend_p && decl
23657                && DECL_DECLARES_TYPE_P (decl))
23658         make_friend_class (current_class_type, TREE_TYPE (decl),
23659                            /*complain=*/true);
23660     }
23661   /* We are done with the current parameter list.  */
23662   --parser->num_template_parameter_lists;
23663
23664   pop_deferring_access_checks ();
23665
23666   /* Finish up.  */
23667   finish_template_decl (parameter_list);
23668
23669   /* Check the template arguments for a literal operator template.  */
23670   if (decl
23671       && DECL_DECLARES_FUNCTION_P (decl)
23672       && UDLIT_OPER_P (DECL_NAME (decl)))
23673     {
23674       bool ok = true;
23675       if (parameter_list == NULL_TREE)
23676         ok = false;
23677       else
23678         {
23679           int num_parms = TREE_VEC_LENGTH (parameter_list);
23680           if (num_parms == 1)
23681             {
23682               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23683               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23684               if (TREE_TYPE (parm) != char_type_node
23685                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23686                 ok = false;
23687             }
23688           else if (num_parms == 2 && cxx_dialect >= cxx14)
23689             {
23690               tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23691               tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23692               tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23693               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23694               if (TREE_TYPE (parm) != TREE_TYPE (type)
23695                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23696                 ok = false;
23697             }
23698           else
23699             ok = false;
23700         }
23701       if (!ok)
23702         {
23703           if (cxx_dialect >= cxx14)
23704             error ("literal operator template %qD has invalid parameter list."
23705                    "  Expected non-type template argument pack <char...>"
23706                    " or <typename CharT, CharT...>",
23707                    decl);
23708           else
23709             error ("literal operator template %qD has invalid parameter list."
23710                    "  Expected non-type template argument pack <char...>",
23711                    decl);
23712         }
23713     }
23714   /* Register member declarations.  */
23715   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23716     finish_member_declaration (decl);
23717   /* For the erroneous case of a template with C linkage, we pushed an
23718      implicit C++ linkage scope; exit that scope now.  */
23719   if (need_lang_pop)
23720     pop_lang_context ();
23721   /* If DECL is a function template, we must return to parse it later.
23722      (Even though there is no definition, there might be default
23723      arguments that need handling.)  */
23724   if (member_p && decl
23725       && DECL_DECLARES_FUNCTION_P (decl))
23726     vec_safe_push (unparsed_funs_with_definitions, decl);
23727 }
23728
23729 /* Perform the deferred access checks from a template-parameter-list.
23730    CHECKS is a TREE_LIST of access checks, as returned by
23731    get_deferred_access_checks.  */
23732
23733 static void
23734 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23735 {
23736   ++processing_template_parmlist;
23737   perform_access_checks (checks, tf_warning_or_error);
23738   --processing_template_parmlist;
23739 }
23740
23741 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23742    `function-definition' sequence that follows a template header.
23743    If MEMBER_P is true, this declaration appears in a class scope.
23744
23745    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
23746    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
23747
23748 static tree
23749 cp_parser_single_declaration (cp_parser* parser,
23750                               vec<deferred_access_check, va_gc> *checks,
23751                               bool member_p,
23752                               bool explicit_specialization_p,
23753                               bool* friend_p)
23754 {
23755   int declares_class_or_enum;
23756   tree decl = NULL_TREE;
23757   cp_decl_specifier_seq decl_specifiers;
23758   bool function_definition_p = false;
23759   cp_token *decl_spec_token_start;
23760
23761   /* This function is only used when processing a template
23762      declaration.  */
23763   gcc_assert (innermost_scope_kind () == sk_template_parms
23764               || innermost_scope_kind () == sk_template_spec);
23765
23766   /* Defer access checks until we know what is being declared.  */
23767   push_deferring_access_checks (dk_deferred);
23768
23769   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23770      alternative.  */
23771   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23772   cp_parser_decl_specifier_seq (parser,
23773                                 CP_PARSER_FLAGS_OPTIONAL,
23774                                 &decl_specifiers,
23775                                 &declares_class_or_enum);
23776   if (friend_p)
23777     *friend_p = cp_parser_friend_p (&decl_specifiers);
23778
23779   /* There are no template typedefs.  */
23780   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23781     {
23782       error_at (decl_spec_token_start->location,
23783                 "template declaration of %<typedef%>");
23784       decl = error_mark_node;
23785     }
23786
23787   /* Gather up the access checks that occurred the
23788      decl-specifier-seq.  */
23789   stop_deferring_access_checks ();
23790
23791   /* Check for the declaration of a template class.  */
23792   if (declares_class_or_enum)
23793     {
23794       if (cp_parser_declares_only_class_p (parser))
23795         {
23796           decl = shadow_tag (&decl_specifiers);
23797
23798           /* In this case:
23799
23800                struct C {
23801                  friend template <typename T> struct A<T>::B;
23802                };
23803
23804              A<T>::B will be represented by a TYPENAME_TYPE, and
23805              therefore not recognized by shadow_tag.  */
23806           if (friend_p && *friend_p
23807               && !decl
23808               && decl_specifiers.type
23809               && TYPE_P (decl_specifiers.type))
23810             decl = decl_specifiers.type;
23811
23812           if (decl && decl != error_mark_node)
23813             decl = TYPE_NAME (decl);
23814           else
23815             decl = error_mark_node;
23816
23817           /* Perform access checks for template parameters.  */
23818           cp_parser_perform_template_parameter_access_checks (checks);
23819         }
23820     }
23821
23822   /* Complain about missing 'typename' or other invalid type names.  */
23823   if (!decl_specifiers.any_type_specifiers_p
23824       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23825     {
23826       /* cp_parser_parse_and_diagnose_invalid_type_name calls
23827          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23828          the rest of this declaration.  */
23829       decl = error_mark_node;
23830       goto out;
23831     }
23832
23833   /* If it's not a template class, try for a template function.  If
23834      the next token is a `;', then this declaration does not declare
23835      anything.  But, if there were errors in the decl-specifiers, then
23836      the error might well have come from an attempted class-specifier.
23837      In that case, there's no need to warn about a missing declarator.  */
23838   if (!decl
23839       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23840           || decl_specifiers.type != error_mark_node))
23841     {
23842       decl = cp_parser_init_declarator (parser,
23843                                         &decl_specifiers,
23844                                         checks,
23845                                         /*function_definition_allowed_p=*/true,
23846                                         member_p,
23847                                         declares_class_or_enum,
23848                                         &function_definition_p,
23849                                         NULL, NULL);
23850
23851     /* 7.1.1-1 [dcl.stc]
23852
23853        A storage-class-specifier shall not be specified in an explicit
23854        specialization...  */
23855     if (decl
23856         && explicit_specialization_p
23857         && decl_specifiers.storage_class != sc_none)
23858       {
23859         error_at (decl_spec_token_start->location,
23860                   "explicit template specialization cannot have a storage class");
23861         decl = error_mark_node;
23862       }
23863
23864     if (decl && VAR_P (decl))
23865       check_template_variable (decl);
23866     }
23867
23868   /* Look for a trailing `;' after the declaration.  */
23869   if (!function_definition_p
23870       && (decl == error_mark_node
23871           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23872     cp_parser_skip_to_end_of_block_or_statement (parser);
23873
23874  out:
23875   pop_deferring_access_checks ();
23876
23877   /* Clear any current qualification; whatever comes next is the start
23878      of something new.  */
23879   parser->scope = NULL_TREE;
23880   parser->qualifying_scope = NULL_TREE;
23881   parser->object_scope = NULL_TREE;
23882
23883   return decl;
23884 }
23885
23886 /* Parse a cast-expression that is not the operand of a unary "&".  */
23887
23888 static tree
23889 cp_parser_simple_cast_expression (cp_parser *parser)
23890 {
23891   return cp_parser_cast_expression (parser, /*address_p=*/false,
23892                                     /*cast_p=*/false, /*decltype*/false, NULL);
23893 }
23894
23895 /* Parse a functional cast to TYPE.  Returns an expression
23896    representing the cast.  */
23897
23898 static tree
23899 cp_parser_functional_cast (cp_parser* parser, tree type)
23900 {
23901   vec<tree, va_gc> *vec;
23902   tree expression_list;
23903   tree cast;
23904   bool nonconst_p;
23905
23906   if (!type)
23907     type = error_mark_node;
23908
23909   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23910     {
23911       cp_lexer_set_source_position (parser->lexer);
23912       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23913       expression_list = cp_parser_braced_list (parser, &nonconst_p);
23914       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23915       if (TREE_CODE (type) == TYPE_DECL)
23916         type = TREE_TYPE (type);
23917       return finish_compound_literal (type, expression_list,
23918                                       tf_warning_or_error);
23919     }
23920
23921
23922   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23923                                                  /*cast_p=*/true,
23924                                                  /*allow_expansion_p=*/true,
23925                                                  /*non_constant_p=*/NULL);
23926   if (vec == NULL)
23927     expression_list = error_mark_node;
23928   else
23929     {
23930       expression_list = build_tree_list_vec (vec);
23931       release_tree_vector (vec);
23932     }
23933
23934   cast = build_functional_cast (type, expression_list,
23935                                 tf_warning_or_error);
23936   /* [expr.const]/1: In an integral constant expression "only type
23937      conversions to integral or enumeration type can be used".  */
23938   if (TREE_CODE (type) == TYPE_DECL)
23939     type = TREE_TYPE (type);
23940   if (cast != error_mark_node
23941       && !cast_valid_in_integral_constant_expression_p (type)
23942       && cp_parser_non_integral_constant_expression (parser,
23943                                                      NIC_CONSTRUCTOR))
23944     return error_mark_node;
23945   return cast;
23946 }
23947
23948 /* Save the tokens that make up the body of a member function defined
23949    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
23950    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
23951    specifiers applied to the declaration.  Returns the FUNCTION_DECL
23952    for the member function.  */
23953
23954 static tree
23955 cp_parser_save_member_function_body (cp_parser* parser,
23956                                      cp_decl_specifier_seq *decl_specifiers,
23957                                      cp_declarator *declarator,
23958                                      tree attributes)
23959 {
23960   cp_token *first;
23961   cp_token *last;
23962   tree fn;
23963
23964   /* Create the FUNCTION_DECL.  */
23965   fn = grokmethod (decl_specifiers, declarator, attributes);
23966   cp_finalize_omp_declare_simd (parser, fn);
23967   /* If something went badly wrong, bail out now.  */
23968   if (fn == error_mark_node)
23969     {
23970       /* If there's a function-body, skip it.  */
23971       if (cp_parser_token_starts_function_definition_p
23972           (cp_lexer_peek_token (parser->lexer)))
23973         cp_parser_skip_to_end_of_block_or_statement (parser);
23974       return error_mark_node;
23975     }
23976
23977   /* Remember it, if there default args to post process.  */
23978   cp_parser_save_default_args (parser, fn);
23979
23980   /* Save away the tokens that make up the body of the
23981      function.  */
23982   first = parser->lexer->next_token;
23983   /* Handle function try blocks.  */
23984   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23985     cp_lexer_consume_token (parser->lexer);
23986   /* We can have braced-init-list mem-initializers before the fn body.  */
23987   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23988     {
23989       cp_lexer_consume_token (parser->lexer);
23990       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23991         {
23992           /* cache_group will stop after an un-nested { } pair, too.  */
23993           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23994             break;
23995
23996           /* variadic mem-inits have ... after the ')'.  */
23997           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23998             cp_lexer_consume_token (parser->lexer);
23999         }
24000     }
24001   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24002   /* Handle function try blocks.  */
24003   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
24004     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24005   last = parser->lexer->next_token;
24006
24007   /* Save away the inline definition; we will process it when the
24008      class is complete.  */
24009   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
24010   DECL_PENDING_INLINE_P (fn) = 1;
24011
24012   /* We need to know that this was defined in the class, so that
24013      friend templates are handled correctly.  */
24014   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24015
24016   /* Add FN to the queue of functions to be parsed later.  */
24017   vec_safe_push (unparsed_funs_with_definitions, fn);
24018
24019   return fn;
24020 }
24021
24022 /* Save the tokens that make up the in-class initializer for a non-static
24023    data member.  Returns a DEFAULT_ARG.  */
24024
24025 static tree
24026 cp_parser_save_nsdmi (cp_parser* parser)
24027 {
24028   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24029 }
24030
24031 /* Parse a template-argument-list, as well as the trailing ">" (but
24032    not the opening "<").  See cp_parser_template_argument_list for the
24033    return value.  */
24034
24035 static tree
24036 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24037 {
24038   tree arguments;
24039   tree saved_scope;
24040   tree saved_qualifying_scope;
24041   tree saved_object_scope;
24042   bool saved_greater_than_is_operator_p;
24043   int saved_unevaluated_operand;
24044   int saved_inhibit_evaluation_warnings;
24045
24046   /* [temp.names]
24047
24048      When parsing a template-id, the first non-nested `>' is taken as
24049      the end of the template-argument-list rather than a greater-than
24050      operator.  */
24051   saved_greater_than_is_operator_p
24052     = parser->greater_than_is_operator_p;
24053   parser->greater_than_is_operator_p = false;
24054   /* Parsing the argument list may modify SCOPE, so we save it
24055      here.  */
24056   saved_scope = parser->scope;
24057   saved_qualifying_scope = parser->qualifying_scope;
24058   saved_object_scope = parser->object_scope;
24059   /* We need to evaluate the template arguments, even though this
24060      template-id may be nested within a "sizeof".  */
24061   saved_unevaluated_operand = cp_unevaluated_operand;
24062   cp_unevaluated_operand = 0;
24063   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24064   c_inhibit_evaluation_warnings = 0;
24065   /* Parse the template-argument-list itself.  */
24066   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24067       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24068     arguments = NULL_TREE;
24069   else
24070     arguments = cp_parser_template_argument_list (parser);
24071   /* Look for the `>' that ends the template-argument-list. If we find
24072      a '>>' instead, it's probably just a typo.  */
24073   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24074     {
24075       if (cxx_dialect != cxx98)
24076         {
24077           /* In C++0x, a `>>' in a template argument list or cast
24078              expression is considered to be two separate `>'
24079              tokens. So, change the current token to a `>', but don't
24080              consume it: it will be consumed later when the outer
24081              template argument list (or cast expression) is parsed.
24082              Note that this replacement of `>' for `>>' is necessary
24083              even if we are parsing tentatively: in the tentative
24084              case, after calling
24085              cp_parser_enclosed_template_argument_list we will always
24086              throw away all of the template arguments and the first
24087              closing `>', either because the template argument list
24088              was erroneous or because we are replacing those tokens
24089              with a CPP_TEMPLATE_ID token.  The second `>' (which will
24090              not have been thrown away) is needed either to close an
24091              outer template argument list or to complete a new-style
24092              cast.  */
24093           cp_token *token = cp_lexer_peek_token (parser->lexer);
24094           token->type = CPP_GREATER;
24095         }
24096       else if (!saved_greater_than_is_operator_p)
24097         {
24098           /* If we're in a nested template argument list, the '>>' has
24099             to be a typo for '> >'. We emit the error message, but we
24100             continue parsing and we push a '>' as next token, so that
24101             the argument list will be parsed correctly.  Note that the
24102             global source location is still on the token before the
24103             '>>', so we need to say explicitly where we want it.  */
24104           cp_token *token = cp_lexer_peek_token (parser->lexer);
24105           error_at (token->location, "%<>>%> should be %<> >%> "
24106                     "within a nested template argument list");
24107
24108           token->type = CPP_GREATER;
24109         }
24110       else
24111         {
24112           /* If this is not a nested template argument list, the '>>'
24113             is a typo for '>'. Emit an error message and continue.
24114             Same deal about the token location, but here we can get it
24115             right by consuming the '>>' before issuing the diagnostic.  */
24116           cp_token *token = cp_lexer_consume_token (parser->lexer);
24117           error_at (token->location,
24118                     "spurious %<>>%>, use %<>%> to terminate "
24119                     "a template argument list");
24120         }
24121     }
24122   else
24123     cp_parser_skip_to_end_of_template_parameter_list (parser);
24124   /* The `>' token might be a greater-than operator again now.  */
24125   parser->greater_than_is_operator_p
24126     = saved_greater_than_is_operator_p;
24127   /* Restore the SAVED_SCOPE.  */
24128   parser->scope = saved_scope;
24129   parser->qualifying_scope = saved_qualifying_scope;
24130   parser->object_scope = saved_object_scope;
24131   cp_unevaluated_operand = saved_unevaluated_operand;
24132   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24133
24134   return arguments;
24135 }
24136
24137 /* MEMBER_FUNCTION is a member function, or a friend.  If default
24138    arguments, or the body of the function have not yet been parsed,
24139    parse them now.  */
24140
24141 static void
24142 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24143 {
24144   timevar_push (TV_PARSE_INMETH);
24145   /* If this member is a template, get the underlying
24146      FUNCTION_DECL.  */
24147   if (DECL_FUNCTION_TEMPLATE_P (member_function))
24148     member_function = DECL_TEMPLATE_RESULT (member_function);
24149
24150   /* There should not be any class definitions in progress at this
24151      point; the bodies of members are only parsed outside of all class
24152      definitions.  */
24153   gcc_assert (parser->num_classes_being_defined == 0);
24154   /* While we're parsing the member functions we might encounter more
24155      classes.  We want to handle them right away, but we don't want
24156      them getting mixed up with functions that are currently in the
24157      queue.  */
24158   push_unparsed_function_queues (parser);
24159
24160   /* Make sure that any template parameters are in scope.  */
24161   maybe_begin_member_template_processing (member_function);
24162
24163   /* If the body of the function has not yet been parsed, parse it
24164      now.  */
24165   if (DECL_PENDING_INLINE_P (member_function))
24166     {
24167       tree function_scope;
24168       cp_token_cache *tokens;
24169
24170       /* The function is no longer pending; we are processing it.  */
24171       tokens = DECL_PENDING_INLINE_INFO (member_function);
24172       DECL_PENDING_INLINE_INFO (member_function) = NULL;
24173       DECL_PENDING_INLINE_P (member_function) = 0;
24174
24175       /* If this is a local class, enter the scope of the containing
24176          function.  */
24177       function_scope = current_function_decl;
24178       if (function_scope)
24179         push_function_context ();
24180
24181       /* Push the body of the function onto the lexer stack.  */
24182       cp_parser_push_lexer_for_tokens (parser, tokens);
24183
24184       /* Let the front end know that we going to be defining this
24185          function.  */
24186       start_preparsed_function (member_function, NULL_TREE,
24187                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
24188
24189       /* Don't do access checking if it is a templated function.  */
24190       if (processing_template_decl)
24191         push_deferring_access_checks (dk_no_check);
24192
24193       /* #pragma omp declare reduction needs special parsing.  */
24194       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24195         {
24196           parser->lexer->in_pragma = true;
24197           cp_parser_omp_declare_reduction_exprs (member_function, parser);
24198           finish_function (/*inline*/2);
24199           cp_check_omp_declare_reduction (member_function);
24200         }
24201       else
24202         /* Now, parse the body of the function.  */
24203         cp_parser_function_definition_after_declarator (parser,
24204                                                         /*inline_p=*/true);
24205
24206       if (processing_template_decl)
24207         pop_deferring_access_checks ();
24208
24209       /* Leave the scope of the containing function.  */
24210       if (function_scope)
24211         pop_function_context ();
24212       cp_parser_pop_lexer (parser);
24213     }
24214
24215   /* Remove any template parameters from the symbol table.  */
24216   maybe_end_member_template_processing ();
24217
24218   /* Restore the queue.  */
24219   pop_unparsed_function_queues (parser);
24220   timevar_pop (TV_PARSE_INMETH);
24221 }
24222
24223 /* If DECL contains any default args, remember it on the unparsed
24224    functions queue.  */
24225
24226 static void
24227 cp_parser_save_default_args (cp_parser* parser, tree decl)
24228 {
24229   tree probe;
24230
24231   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24232        probe;
24233        probe = TREE_CHAIN (probe))
24234     if (TREE_PURPOSE (probe))
24235       {
24236         cp_default_arg_entry entry = {current_class_type, decl};
24237         vec_safe_push (unparsed_funs_with_default_args, entry);
24238         break;
24239       }
24240 }
24241
24242 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24243    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
24244    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
24245    from the parameter-type-list.  */
24246
24247 static tree
24248 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24249                                       tree default_arg, tree parmtype)
24250 {
24251   cp_token_cache *tokens;
24252   tree parsed_arg;
24253   bool dummy;
24254
24255   if (default_arg == error_mark_node)
24256     return error_mark_node;
24257
24258   /* Push the saved tokens for the default argument onto the parser's
24259      lexer stack.  */
24260   tokens = DEFARG_TOKENS (default_arg);
24261   cp_parser_push_lexer_for_tokens (parser, tokens);
24262
24263   start_lambda_scope (decl);
24264
24265   /* Parse the default argument.  */
24266   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24267   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24268     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24269
24270   finish_lambda_scope ();
24271
24272   if (parsed_arg == error_mark_node)
24273     cp_parser_skip_to_end_of_statement (parser);
24274
24275   if (!processing_template_decl)
24276     {
24277       /* In a non-template class, check conversions now.  In a template,
24278          we'll wait and instantiate these as needed.  */
24279       if (TREE_CODE (decl) == PARM_DECL)
24280         parsed_arg = check_default_argument (parmtype, parsed_arg,
24281                                              tf_warning_or_error);
24282       else
24283         parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24284     }
24285
24286   /* If the token stream has not been completely used up, then
24287      there was extra junk after the end of the default
24288      argument.  */
24289   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24290     {
24291       if (TREE_CODE (decl) == PARM_DECL)
24292         cp_parser_error (parser, "expected %<,%>");
24293       else
24294         cp_parser_error (parser, "expected %<;%>");
24295     }
24296
24297   /* Revert to the main lexer.  */
24298   cp_parser_pop_lexer (parser);
24299
24300   return parsed_arg;
24301 }
24302
24303 /* FIELD is a non-static data member with an initializer which we saved for
24304    later; parse it now.  */
24305
24306 static void
24307 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24308 {
24309   tree def;
24310
24311   maybe_begin_member_template_processing (field);
24312
24313   push_unparsed_function_queues (parser);
24314   def = cp_parser_late_parse_one_default_arg (parser, field,
24315                                               DECL_INITIAL (field),
24316                                               NULL_TREE);
24317   pop_unparsed_function_queues (parser);
24318
24319   maybe_end_member_template_processing ();
24320
24321   DECL_INITIAL (field) = def;
24322 }
24323
24324 /* FN is a FUNCTION_DECL which may contains a parameter with an
24325    unparsed DEFAULT_ARG.  Parse the default args now.  This function
24326    assumes that the current scope is the scope in which the default
24327    argument should be processed.  */
24328
24329 static void
24330 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24331 {
24332   bool saved_local_variables_forbidden_p;
24333   tree parm, parmdecl;
24334
24335   /* While we're parsing the default args, we might (due to the
24336      statement expression extension) encounter more classes.  We want
24337      to handle them right away, but we don't want them getting mixed
24338      up with default args that are currently in the queue.  */
24339   push_unparsed_function_queues (parser);
24340
24341   /* Local variable names (and the `this' keyword) may not appear
24342      in a default argument.  */
24343   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24344   parser->local_variables_forbidden_p = true;
24345
24346   push_defarg_context (fn);
24347
24348   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24349          parmdecl = DECL_ARGUMENTS (fn);
24350        parm && parm != void_list_node;
24351        parm = TREE_CHAIN (parm),
24352          parmdecl = DECL_CHAIN (parmdecl))
24353     {
24354       tree default_arg = TREE_PURPOSE (parm);
24355       tree parsed_arg;
24356       vec<tree, va_gc> *insts;
24357       tree copy;
24358       unsigned ix;
24359
24360       if (!default_arg)
24361         continue;
24362
24363       if (TREE_CODE (default_arg) != DEFAULT_ARG)
24364         /* This can happen for a friend declaration for a function
24365            already declared with default arguments.  */
24366         continue;
24367
24368       parsed_arg
24369         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24370                                                 default_arg,
24371                                                 TREE_VALUE (parm));
24372       if (parsed_arg == error_mark_node)
24373         {
24374           continue;
24375         }
24376
24377       TREE_PURPOSE (parm) = parsed_arg;
24378
24379       /* Update any instantiations we've already created.  */
24380       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24381            vec_safe_iterate (insts, ix, &copy); ix++)
24382         TREE_PURPOSE (copy) = parsed_arg;
24383     }
24384
24385   pop_defarg_context ();
24386
24387   /* Make sure no default arg is missing.  */
24388   check_default_args (fn);
24389
24390   /* Restore the state of local_variables_forbidden_p.  */
24391   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24392
24393   /* Restore the queue.  */
24394   pop_unparsed_function_queues (parser);
24395 }
24396
24397 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24398
24399      sizeof ... ( identifier )
24400
24401    where the 'sizeof' token has already been consumed.  */
24402
24403 static tree
24404 cp_parser_sizeof_pack (cp_parser *parser)
24405 {
24406   /* Consume the `...'.  */
24407   cp_lexer_consume_token (parser->lexer);
24408   maybe_warn_variadic_templates ();
24409
24410   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24411   if (paren)
24412     cp_lexer_consume_token (parser->lexer);
24413   else
24414     permerror (cp_lexer_peek_token (parser->lexer)->location,
24415                "%<sizeof...%> argument must be surrounded by parentheses");
24416
24417   cp_token *token = cp_lexer_peek_token (parser->lexer);
24418   tree name = cp_parser_identifier (parser);
24419   if (name == error_mark_node)
24420     return error_mark_node;
24421   /* The name is not qualified.  */
24422   parser->scope = NULL_TREE;
24423   parser->qualifying_scope = NULL_TREE;
24424   parser->object_scope = NULL_TREE;
24425   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24426   if (expr == error_mark_node)
24427     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24428                                  token->location);
24429   if (TREE_CODE (expr) == TYPE_DECL)
24430     expr = TREE_TYPE (expr);
24431   else if (TREE_CODE (expr) == CONST_DECL)
24432     expr = DECL_INITIAL (expr);
24433   expr = make_pack_expansion (expr);
24434
24435   if (paren)
24436     cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24437
24438   return expr;
24439 }
24440
24441 /* Parse the operand of `sizeof' (or a similar operator).  Returns
24442    either a TYPE or an expression, depending on the form of the
24443    input.  The KEYWORD indicates which kind of expression we have
24444    encountered.  */
24445
24446 static tree
24447 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24448 {
24449   tree expr = NULL_TREE;
24450   const char *saved_message;
24451   char *tmp;
24452   bool saved_integral_constant_expression_p;
24453   bool saved_non_integral_constant_expression_p;
24454
24455   /* If it's a `...', then we are computing the length of a parameter
24456      pack.  */
24457   if (keyword == RID_SIZEOF
24458       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24459     return cp_parser_sizeof_pack (parser);
24460
24461   /* Types cannot be defined in a `sizeof' expression.  Save away the
24462      old message.  */
24463   saved_message = parser->type_definition_forbidden_message;
24464   /* And create the new one.  */
24465   tmp = concat ("types may not be defined in %<",
24466                 IDENTIFIER_POINTER (ridpointers[keyword]),
24467                 "%> expressions", NULL);
24468   parser->type_definition_forbidden_message = tmp;
24469
24470   /* The restrictions on constant-expressions do not apply inside
24471      sizeof expressions.  */
24472   saved_integral_constant_expression_p
24473     = parser->integral_constant_expression_p;
24474   saved_non_integral_constant_expression_p
24475     = parser->non_integral_constant_expression_p;
24476   parser->integral_constant_expression_p = false;
24477
24478   /* Do not actually evaluate the expression.  */
24479   ++cp_unevaluated_operand;
24480   ++c_inhibit_evaluation_warnings;
24481   /* If it's a `(', then we might be looking at the type-id
24482      construction.  */
24483   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24484     {
24485       tree type = NULL_TREE;
24486
24487       /* We can't be sure yet whether we're looking at a type-id or an
24488          expression.  */
24489       cp_parser_parse_tentatively (parser);
24490       /* Note: as a GNU Extension, compound literals are considered
24491          postfix-expressions as they are in C99, so they are valid
24492          arguments to sizeof.  See comment in cp_parser_cast_expression
24493          for details.  */
24494       if (cp_parser_compound_literal_p (parser))
24495         cp_parser_simulate_error (parser);
24496       else
24497         {
24498           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24499           parser->in_type_id_in_expr_p = true;
24500           /* Look for the type-id.  */
24501           type = cp_parser_type_id (parser);
24502           /* Look for the closing `)'.  */
24503           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24504           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24505         }
24506
24507       /* If all went well, then we're done.  */
24508       if (cp_parser_parse_definitely (parser))
24509         {
24510           cp_decl_specifier_seq decl_specs;
24511
24512           /* Build a trivial decl-specifier-seq.  */
24513           clear_decl_specs (&decl_specs);
24514           decl_specs.type = type;
24515
24516           /* Call grokdeclarator to figure out what type this is.  */
24517           expr = grokdeclarator (NULL,
24518                                  &decl_specs,
24519                                  TYPENAME,
24520                                  /*initialized=*/0,
24521                                  /*attrlist=*/NULL);
24522         }
24523     }
24524
24525   /* If the type-id production did not work out, then we must be
24526      looking at the unary-expression production.  */
24527   if (!expr)
24528     expr = cp_parser_unary_expression (parser);
24529
24530   /* Go back to evaluating expressions.  */
24531   --cp_unevaluated_operand;
24532   --c_inhibit_evaluation_warnings;
24533
24534   /* Free the message we created.  */
24535   free (tmp);
24536   /* And restore the old one.  */
24537   parser->type_definition_forbidden_message = saved_message;
24538   parser->integral_constant_expression_p
24539     = saved_integral_constant_expression_p;
24540   parser->non_integral_constant_expression_p
24541     = saved_non_integral_constant_expression_p;
24542
24543   return expr;
24544 }
24545
24546 /* If the current declaration has no declarator, return true.  */
24547
24548 static bool
24549 cp_parser_declares_only_class_p (cp_parser *parser)
24550 {
24551   /* If the next token is a `;' or a `,' then there is no
24552      declarator.  */
24553   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24554           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24555 }
24556
24557 /* Update the DECL_SPECS to reflect the storage class indicated by
24558    KEYWORD.  */
24559
24560 static void
24561 cp_parser_set_storage_class (cp_parser *parser,
24562                              cp_decl_specifier_seq *decl_specs,
24563                              enum rid keyword,
24564                              cp_token *token)
24565 {
24566   cp_storage_class storage_class;
24567
24568   if (parser->in_unbraced_linkage_specification_p)
24569     {
24570       error_at (token->location, "invalid use of %qD in linkage specification",
24571                 ridpointers[keyword]);
24572       return;
24573     }
24574   else if (decl_specs->storage_class != sc_none)
24575     {
24576       decl_specs->conflicting_specifiers_p = true;
24577       return;
24578     }
24579
24580   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24581       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24582       && decl_specs->gnu_thread_keyword_p)
24583     {
24584       pedwarn (decl_specs->locations[ds_thread], 0,
24585                 "%<__thread%> before %qD", ridpointers[keyword]);
24586     }
24587
24588   switch (keyword)
24589     {
24590     case RID_AUTO:
24591       storage_class = sc_auto;
24592       break;
24593     case RID_REGISTER:
24594       storage_class = sc_register;
24595       break;
24596     case RID_STATIC:
24597       storage_class = sc_static;
24598       break;
24599     case RID_EXTERN:
24600       storage_class = sc_extern;
24601       break;
24602     case RID_MUTABLE:
24603       storage_class = sc_mutable;
24604       break;
24605     default:
24606       gcc_unreachable ();
24607     }
24608   decl_specs->storage_class = storage_class;
24609   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24610
24611   /* A storage class specifier cannot be applied alongside a typedef 
24612      specifier. If there is a typedef specifier present then set 
24613      conflicting_specifiers_p which will trigger an error later
24614      on in grokdeclarator. */
24615   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24616     decl_specs->conflicting_specifiers_p = true;
24617 }
24618
24619 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
24620    is true, the type is a class or enum definition.  */
24621
24622 static void
24623 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24624                               tree type_spec,
24625                               cp_token *token,
24626                               bool type_definition_p)
24627 {
24628   decl_specs->any_specifiers_p = true;
24629
24630   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24631      (with, for example, in "typedef int wchar_t;") we remember that
24632      this is what happened.  In system headers, we ignore these
24633      declarations so that G++ can work with system headers that are not
24634      C++-safe.  */
24635   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24636       && !type_definition_p
24637       && (type_spec == boolean_type_node
24638           || type_spec == char16_type_node
24639           || type_spec == char32_type_node
24640           || type_spec == wchar_type_node)
24641       && (decl_specs->type
24642           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24643           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24644           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24645           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24646     {
24647       decl_specs->redefined_builtin_type = type_spec;
24648       set_and_check_decl_spec_loc (decl_specs,
24649                                    ds_redefined_builtin_type_spec,
24650                                    token);
24651       if (!decl_specs->type)
24652         {
24653           decl_specs->type = type_spec;
24654           decl_specs->type_definition_p = false;
24655           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24656         }
24657     }
24658   else if (decl_specs->type)
24659     decl_specs->multiple_types_p = true;
24660   else
24661     {
24662       decl_specs->type = type_spec;
24663       decl_specs->type_definition_p = type_definition_p;
24664       decl_specs->redefined_builtin_type = NULL_TREE;
24665       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24666     }
24667 }
24668
24669 /* True iff TOKEN is the GNU keyword __thread.  */
24670
24671 static bool
24672 token_is__thread (cp_token *token)
24673 {
24674   gcc_assert (token->keyword == RID_THREAD);
24675   return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24676 }
24677
24678 /* Set the location for a declarator specifier and check if it is
24679    duplicated.
24680
24681    DECL_SPECS is the sequence of declarator specifiers onto which to
24682    set the location.
24683
24684    DS is the single declarator specifier to set which location  is to
24685    be set onto the existing sequence of declarators.
24686
24687    LOCATION is the location for the declarator specifier to
24688    consider.  */
24689
24690 static void
24691 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24692                              cp_decl_spec ds, cp_token *token)
24693 {
24694   gcc_assert (ds < ds_last);
24695
24696   if (decl_specs == NULL)
24697     return;
24698
24699   source_location location = token->location;
24700
24701   if (decl_specs->locations[ds] == 0)
24702     {
24703       decl_specs->locations[ds] = location;
24704       if (ds == ds_thread)
24705         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24706     }
24707   else
24708     {
24709       if (ds == ds_long)
24710         {
24711           if (decl_specs->locations[ds_long_long] != 0)
24712             error_at (location,
24713                       "%<long long long%> is too long for GCC");
24714           else
24715             {
24716               decl_specs->locations[ds_long_long] = location;
24717               pedwarn_cxx98 (location,
24718                              OPT_Wlong_long, 
24719                              "ISO C++ 1998 does not support %<long long%>");
24720             }
24721         }
24722       else if (ds == ds_thread)
24723         {
24724           bool gnu = token_is__thread (token);
24725           if (gnu != decl_specs->gnu_thread_keyword_p)
24726             error_at (location,
24727                       "both %<__thread%> and %<thread_local%> specified");
24728           else
24729             error_at (location, "duplicate %qD", token->u.value);
24730         }
24731       else
24732         {
24733           static const char *const decl_spec_names[] = {
24734             "signed",
24735             "unsigned",
24736             "short",
24737             "long",
24738             "const",
24739             "volatile",
24740             "restrict",
24741             "inline",
24742             "virtual",
24743             "explicit",
24744             "friend",
24745             "typedef",
24746             "using",
24747             "constexpr",
24748             "__complex"
24749           };
24750           error_at (location,
24751                     "duplicate %qs", decl_spec_names[ds]);
24752         }
24753     }
24754 }
24755
24756 /* Return true iff the declarator specifier DS is present in the
24757    sequence of declarator specifiers DECL_SPECS.  */
24758
24759 bool
24760 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24761                           cp_decl_spec ds)
24762 {
24763   gcc_assert (ds < ds_last);
24764
24765   if (decl_specs == NULL)
24766     return false;
24767
24768   return decl_specs->locations[ds] != 0;
24769 }
24770
24771 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24772    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
24773
24774 static bool
24775 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24776 {
24777   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24778 }
24779
24780 /* Issue an error message indicating that TOKEN_DESC was expected.
24781    If KEYWORD is true, it indicated this function is called by
24782    cp_parser_require_keword and the required token can only be
24783    a indicated keyword. */
24784
24785 static void
24786 cp_parser_required_error (cp_parser *parser,
24787                           required_token token_desc,
24788                           bool keyword)
24789 {
24790   switch (token_desc)
24791     {
24792       case RT_NEW:
24793         cp_parser_error (parser, "expected %<new%>");
24794         return;
24795       case RT_DELETE:
24796         cp_parser_error (parser, "expected %<delete%>");
24797         return;
24798       case RT_RETURN:
24799         cp_parser_error (parser, "expected %<return%>");
24800         return;
24801       case RT_WHILE:
24802         cp_parser_error (parser, "expected %<while%>");
24803         return;
24804       case RT_EXTERN:
24805         cp_parser_error (parser, "expected %<extern%>");
24806         return;
24807       case RT_STATIC_ASSERT:
24808         cp_parser_error (parser, "expected %<static_assert%>");
24809         return;
24810       case RT_DECLTYPE:
24811         cp_parser_error (parser, "expected %<decltype%>");
24812         return;
24813       case RT_OPERATOR:
24814         cp_parser_error (parser, "expected %<operator%>");
24815         return;
24816       case RT_CLASS:
24817         cp_parser_error (parser, "expected %<class%>");
24818         return;
24819       case RT_TEMPLATE:
24820         cp_parser_error (parser, "expected %<template%>");
24821         return;
24822       case RT_NAMESPACE:
24823         cp_parser_error (parser, "expected %<namespace%>");
24824         return;
24825       case RT_USING:
24826         cp_parser_error (parser, "expected %<using%>");
24827         return;
24828       case RT_ASM:
24829         cp_parser_error (parser, "expected %<asm%>");
24830         return;
24831       case RT_TRY:
24832         cp_parser_error (parser, "expected %<try%>");
24833         return;
24834       case RT_CATCH:
24835         cp_parser_error (parser, "expected %<catch%>");
24836         return;
24837       case RT_THROW:
24838         cp_parser_error (parser, "expected %<throw%>");
24839         return;
24840       case RT_LABEL:
24841         cp_parser_error (parser, "expected %<__label__%>");
24842         return;
24843       case RT_AT_TRY:
24844         cp_parser_error (parser, "expected %<@try%>");
24845         return;
24846       case RT_AT_SYNCHRONIZED:
24847         cp_parser_error (parser, "expected %<@synchronized%>");
24848         return;
24849       case RT_AT_THROW:
24850         cp_parser_error (parser, "expected %<@throw%>");
24851         return;
24852       case RT_TRANSACTION_ATOMIC:
24853         cp_parser_error (parser, "expected %<__transaction_atomic%>");
24854         return;
24855       case RT_TRANSACTION_RELAXED:
24856         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24857         return;
24858       default:
24859         break;
24860     }
24861   if (!keyword)
24862     {
24863       switch (token_desc)
24864         {
24865           case RT_SEMICOLON:
24866             cp_parser_error (parser, "expected %<;%>");
24867             return;
24868           case RT_OPEN_PAREN:
24869             cp_parser_error (parser, "expected %<(%>");
24870             return;
24871           case RT_CLOSE_BRACE:
24872             cp_parser_error (parser, "expected %<}%>");
24873             return;
24874           case RT_OPEN_BRACE:
24875             cp_parser_error (parser, "expected %<{%>");
24876             return;
24877           case RT_CLOSE_SQUARE:
24878             cp_parser_error (parser, "expected %<]%>");
24879             return;
24880           case RT_OPEN_SQUARE:
24881             cp_parser_error (parser, "expected %<[%>");
24882             return;
24883           case RT_COMMA:
24884             cp_parser_error (parser, "expected %<,%>");
24885             return;
24886           case RT_SCOPE:
24887             cp_parser_error (parser, "expected %<::%>");
24888             return;
24889           case RT_LESS:
24890             cp_parser_error (parser, "expected %<<%>");
24891             return;
24892           case RT_GREATER:
24893             cp_parser_error (parser, "expected %<>%>");
24894             return;
24895           case RT_EQ:
24896             cp_parser_error (parser, "expected %<=%>");
24897             return;
24898           case RT_ELLIPSIS:
24899             cp_parser_error (parser, "expected %<...%>");
24900             return;
24901           case RT_MULT:
24902             cp_parser_error (parser, "expected %<*%>");
24903             return;
24904           case RT_COMPL:
24905             cp_parser_error (parser, "expected %<~%>");
24906             return;
24907           case RT_COLON:
24908             cp_parser_error (parser, "expected %<:%>");
24909             return;
24910           case RT_COLON_SCOPE:
24911             cp_parser_error (parser, "expected %<:%> or %<::%>");
24912             return;
24913           case RT_CLOSE_PAREN:
24914             cp_parser_error (parser, "expected %<)%>");
24915             return;
24916           case RT_COMMA_CLOSE_PAREN:
24917             cp_parser_error (parser, "expected %<,%> or %<)%>");
24918             return;
24919           case RT_PRAGMA_EOL:
24920             cp_parser_error (parser, "expected end of line");
24921             return;
24922           case RT_NAME:
24923             cp_parser_error (parser, "expected identifier");
24924             return;
24925           case RT_SELECT:
24926             cp_parser_error (parser, "expected selection-statement");
24927             return;
24928           case RT_INTERATION:
24929             cp_parser_error (parser, "expected iteration-statement");
24930             return;
24931           case RT_JUMP:
24932             cp_parser_error (parser, "expected jump-statement");
24933             return;
24934           case RT_CLASS_KEY:
24935             cp_parser_error (parser, "expected class-key");
24936             return;
24937           case RT_CLASS_TYPENAME_TEMPLATE:
24938             cp_parser_error (parser,
24939                  "expected %<class%>, %<typename%>, or %<template%>");
24940             return;
24941           default:
24942             gcc_unreachable ();
24943         }
24944     }
24945   else
24946     gcc_unreachable ();
24947 }
24948
24949
24950
24951 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
24952    issue an error message indicating that TOKEN_DESC was expected.
24953
24954    Returns the token consumed, if the token had the appropriate type.
24955    Otherwise, returns NULL.  */
24956
24957 static cp_token *
24958 cp_parser_require (cp_parser* parser,
24959                    enum cpp_ttype type,
24960                    required_token token_desc)
24961 {
24962   if (cp_lexer_next_token_is (parser->lexer, type))
24963     return cp_lexer_consume_token (parser->lexer);
24964   else
24965     {
24966       /* Output the MESSAGE -- unless we're parsing tentatively.  */
24967       if (!cp_parser_simulate_error (parser))
24968         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24969       return NULL;
24970     }
24971 }
24972
24973 /* An error message is produced if the next token is not '>'.
24974    All further tokens are skipped until the desired token is
24975    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
24976
24977 static void
24978 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24979 {
24980   /* Current level of '< ... >'.  */
24981   unsigned level = 0;
24982   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
24983   unsigned nesting_depth = 0;
24984
24985   /* Are we ready, yet?  If not, issue error message.  */
24986   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24987     return;
24988
24989   /* Skip tokens until the desired token is found.  */
24990   while (true)
24991     {
24992       /* Peek at the next token.  */
24993       switch (cp_lexer_peek_token (parser->lexer)->type)
24994         {
24995         case CPP_LESS:
24996           if (!nesting_depth)
24997             ++level;
24998           break;
24999
25000         case CPP_RSHIFT:
25001           if (cxx_dialect == cxx98)
25002             /* C++0x views the `>>' operator as two `>' tokens, but
25003                C++98 does not. */
25004             break;
25005           else if (!nesting_depth && level-- == 0)
25006             {
25007               /* We've hit a `>>' where the first `>' closes the
25008                  template argument list, and the second `>' is
25009                  spurious.  Just consume the `>>' and stop; we've
25010                  already produced at least one error.  */
25011               cp_lexer_consume_token (parser->lexer);
25012               return;
25013             }
25014           /* Fall through for C++0x, so we handle the second `>' in
25015              the `>>'.  */
25016
25017         case CPP_GREATER:
25018           if (!nesting_depth && level-- == 0)
25019             {
25020               /* We've reached the token we want, consume it and stop.  */
25021               cp_lexer_consume_token (parser->lexer);
25022               return;
25023             }
25024           break;
25025
25026         case CPP_OPEN_PAREN:
25027         case CPP_OPEN_SQUARE:
25028           ++nesting_depth;
25029           break;
25030
25031         case CPP_CLOSE_PAREN:
25032         case CPP_CLOSE_SQUARE:
25033           if (nesting_depth-- == 0)
25034             return;
25035           break;
25036
25037         case CPP_EOF:
25038         case CPP_PRAGMA_EOL:
25039         case CPP_SEMICOLON:
25040         case CPP_OPEN_BRACE:
25041         case CPP_CLOSE_BRACE:
25042           /* The '>' was probably forgotten, don't look further.  */
25043           return;
25044
25045         default:
25046           break;
25047         }
25048
25049       /* Consume this token.  */
25050       cp_lexer_consume_token (parser->lexer);
25051     }
25052 }
25053
25054 /* If the next token is the indicated keyword, consume it.  Otherwise,
25055    issue an error message indicating that TOKEN_DESC was expected.
25056
25057    Returns the token consumed, if the token had the appropriate type.
25058    Otherwise, returns NULL.  */
25059
25060 static cp_token *
25061 cp_parser_require_keyword (cp_parser* parser,
25062                            enum rid keyword,
25063                            required_token token_desc)
25064 {
25065   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25066
25067   if (token && token->keyword != keyword)
25068     {
25069       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
25070       return NULL;
25071     }
25072
25073   return token;
25074 }
25075
25076 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25077    function-definition.  */
25078
25079 static bool
25080 cp_parser_token_starts_function_definition_p (cp_token* token)
25081 {
25082   return (/* An ordinary function-body begins with an `{'.  */
25083           token->type == CPP_OPEN_BRACE
25084           /* A ctor-initializer begins with a `:'.  */
25085           || token->type == CPP_COLON
25086           /* A function-try-block begins with `try'.  */
25087           || token->keyword == RID_TRY
25088           /* A function-transaction-block begins with `__transaction_atomic'
25089              or `__transaction_relaxed'.  */
25090           || token->keyword == RID_TRANSACTION_ATOMIC
25091           || token->keyword == RID_TRANSACTION_RELAXED
25092           /* The named return value extension begins with `return'.  */
25093           || token->keyword == RID_RETURN);
25094 }
25095
25096 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25097    definition.  */
25098
25099 static bool
25100 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25101 {
25102   cp_token *token;
25103
25104   token = cp_lexer_peek_token (parser->lexer);
25105   return (token->type == CPP_OPEN_BRACE
25106           || (token->type == CPP_COLON
25107               && !parser->colon_doesnt_start_class_def_p));
25108 }
25109
25110 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25111    C++0x) ending a template-argument.  */
25112
25113 static bool
25114 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25115 {
25116   cp_token *token;
25117
25118   token = cp_lexer_peek_token (parser->lexer);
25119   return (token->type == CPP_COMMA 
25120           || token->type == CPP_GREATER
25121           || token->type == CPP_ELLIPSIS
25122           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25123 }
25124
25125 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25126    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
25127
25128 static bool
25129 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25130                                                      size_t n)
25131 {
25132   cp_token *token;
25133
25134   token = cp_lexer_peek_nth_token (parser->lexer, n);
25135   if (token->type == CPP_LESS)
25136     return true;
25137   /* Check for the sequence `<::' in the original code. It would be lexed as
25138      `[:', where `[' is a digraph, and there is no whitespace before
25139      `:'.  */
25140   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25141     {
25142       cp_token *token2;
25143       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25144       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25145         return true;
25146     }
25147   return false;
25148 }
25149
25150 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25151    or none_type otherwise.  */
25152
25153 static enum tag_types
25154 cp_parser_token_is_class_key (cp_token* token)
25155 {
25156   switch (token->keyword)
25157     {
25158     case RID_CLASS:
25159       return class_type;
25160     case RID_STRUCT:
25161       return record_type;
25162     case RID_UNION:
25163       return union_type;
25164
25165     default:
25166       return none_type;
25167     }
25168 }
25169
25170 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25171    or none_type otherwise or if the token is null.  */
25172
25173 static enum tag_types
25174 cp_parser_token_is_type_parameter_key (cp_token* token)
25175 {
25176   if (!token)
25177     return none_type;
25178
25179   switch (token->keyword)
25180     {
25181     case RID_CLASS:
25182       return class_type;
25183     case RID_TYPENAME:
25184       return typename_type;
25185
25186     default:
25187       return none_type;
25188     }
25189 }
25190
25191 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
25192
25193 static void
25194 cp_parser_check_class_key (enum tag_types class_key, tree type)
25195 {
25196   if (type == error_mark_node)
25197     return;
25198   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25199     {
25200       if (permerror (input_location, "%qs tag used in naming %q#T",
25201                      class_key == union_type ? "union"
25202                      : class_key == record_type ? "struct" : "class",
25203                      type))
25204         inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25205                 "%q#T was previously declared here", type);
25206     }
25207 }
25208
25209 /* Issue an error message if DECL is redeclared with different
25210    access than its original declaration [class.access.spec/3].
25211    This applies to nested classes and nested class templates.
25212    [class.mem/1].  */
25213
25214 static void
25215 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25216 {
25217   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25218     return;
25219
25220   if ((TREE_PRIVATE (decl)
25221        != (current_access_specifier == access_private_node))
25222       || (TREE_PROTECTED (decl)
25223           != (current_access_specifier == access_protected_node)))
25224     error_at (location, "%qD redeclared with different access", decl);
25225 }
25226
25227 /* Look for the `template' keyword, as a syntactic disambiguator.
25228    Return TRUE iff it is present, in which case it will be
25229    consumed.  */
25230
25231 static bool
25232 cp_parser_optional_template_keyword (cp_parser *parser)
25233 {
25234   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25235     {
25236       /* In C++98 the `template' keyword can only be used within templates;
25237          outside templates the parser can always figure out what is a
25238          template and what is not.  In C++11,  per the resolution of DR 468,
25239          `template' is allowed in cases where it is not strictly necessary.  */
25240       if (!processing_template_decl
25241           && pedantic && cxx_dialect == cxx98)
25242         {
25243           cp_token *token = cp_lexer_peek_token (parser->lexer);
25244           pedwarn (token->location, OPT_Wpedantic,
25245                    "in C++98 %<template%> (as a disambiguator) is only "
25246                    "allowed within templates");
25247           /* If this part of the token stream is rescanned, the same
25248              error message would be generated.  So, we purge the token
25249              from the stream.  */
25250           cp_lexer_purge_token (parser->lexer);
25251           return false;
25252         }
25253       else
25254         {
25255           /* Consume the `template' keyword.  */
25256           cp_lexer_consume_token (parser->lexer);
25257           return true;
25258         }
25259     }
25260   return false;
25261 }
25262
25263 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
25264    set PARSER->SCOPE, and perform other related actions.  */
25265
25266 static void
25267 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25268 {
25269   int i;
25270   struct tree_check *check_value;
25271   deferred_access_check *chk;
25272   vec<deferred_access_check, va_gc> *checks;
25273
25274   /* Get the stored value.  */
25275   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25276   /* Perform any access checks that were deferred.  */
25277   checks = check_value->checks;
25278   if (checks)
25279     {
25280       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25281         perform_or_defer_access_check (chk->binfo,
25282                                        chk->decl,
25283                                        chk->diag_decl, tf_warning_or_error);
25284     }
25285   /* Set the scope from the stored value.  */
25286   parser->scope = check_value->value;
25287   parser->qualifying_scope = check_value->qualifying_scope;
25288   parser->object_scope = NULL_TREE;
25289 }
25290
25291 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
25292    encounter the end of a block before what we were looking for.  */
25293
25294 static bool
25295 cp_parser_cache_group (cp_parser *parser,
25296                        enum cpp_ttype end,
25297                        unsigned depth)
25298 {
25299   while (true)
25300     {
25301       cp_token *token = cp_lexer_peek_token (parser->lexer);
25302
25303       /* Abort a parenthesized expression if we encounter a semicolon.  */
25304       if ((end == CPP_CLOSE_PAREN || depth == 0)
25305           && token->type == CPP_SEMICOLON)
25306         return true;
25307       /* If we've reached the end of the file, stop.  */
25308       if (token->type == CPP_EOF
25309           || (end != CPP_PRAGMA_EOL
25310               && token->type == CPP_PRAGMA_EOL))
25311         return true;
25312       if (token->type == CPP_CLOSE_BRACE && depth == 0)
25313         /* We've hit the end of an enclosing block, so there's been some
25314            kind of syntax error.  */
25315         return true;
25316
25317       /* Consume the token.  */
25318       cp_lexer_consume_token (parser->lexer);
25319       /* See if it starts a new group.  */
25320       if (token->type == CPP_OPEN_BRACE)
25321         {
25322           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25323           /* In theory this should probably check end == '}', but
25324              cp_parser_save_member_function_body needs it to exit
25325              after either '}' or ')' when called with ')'.  */
25326           if (depth == 0)
25327             return false;
25328         }
25329       else if (token->type == CPP_OPEN_PAREN)
25330         {
25331           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25332           if (depth == 0 && end == CPP_CLOSE_PAREN)
25333             return false;
25334         }
25335       else if (token->type == CPP_PRAGMA)
25336         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25337       else if (token->type == end)
25338         return false;
25339     }
25340 }
25341
25342 /* Like above, for caching a default argument or NSDMI.  Both of these are
25343    terminated by a non-nested comma, but it can be unclear whether or not a
25344    comma is nested in a template argument list unless we do more parsing.
25345    In order to handle this ambiguity, when we encounter a ',' after a '<'
25346    we try to parse what follows as a parameter-declaration-list (in the
25347    case of a default argument) or a member-declarator (in the case of an
25348    NSDMI).  If that succeeds, then we stop caching.  */
25349
25350 static tree
25351 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25352 {
25353   unsigned depth = 0;
25354   int maybe_template_id = 0;
25355   cp_token *first_token;
25356   cp_token *token;
25357   tree default_argument;
25358
25359   /* Add tokens until we have processed the entire default
25360      argument.  We add the range [first_token, token).  */
25361   first_token = cp_lexer_peek_token (parser->lexer);
25362   if (first_token->type == CPP_OPEN_BRACE)
25363     {
25364       /* For list-initialization, this is straightforward.  */
25365       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25366       token = cp_lexer_peek_token (parser->lexer);
25367     }
25368   else while (true)
25369     {
25370       bool done = false;
25371
25372       /* Peek at the next token.  */
25373       token = cp_lexer_peek_token (parser->lexer);
25374       /* What we do depends on what token we have.  */
25375       switch (token->type)
25376         {
25377           /* In valid code, a default argument must be
25378              immediately followed by a `,' `)', or `...'.  */
25379         case CPP_COMMA:
25380           if (depth == 0 && maybe_template_id)
25381             {
25382               /* If we've seen a '<', we might be in a
25383                  template-argument-list.  Until Core issue 325 is
25384                  resolved, we don't know how this situation ought
25385                  to be handled, so try to DTRT.  We check whether
25386                  what comes after the comma is a valid parameter
25387                  declaration list.  If it is, then the comma ends
25388                  the default argument; otherwise the default
25389                  argument continues.  */
25390               bool error = false;
25391
25392               /* Set ITALP so cp_parser_parameter_declaration_list
25393                  doesn't decide to commit to this parse.  */
25394               bool saved_italp = parser->in_template_argument_list_p;
25395               parser->in_template_argument_list_p = true;
25396
25397               cp_parser_parse_tentatively (parser);
25398               cp_lexer_consume_token (parser->lexer);
25399
25400               if (nsdmi)
25401                 {
25402                   int ctor_dtor_or_conv_p;
25403                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25404                                         &ctor_dtor_or_conv_p,
25405                                         /*parenthesized_p=*/NULL,
25406                                         /*member_p=*/true,
25407                                         /*friend_p=*/false);
25408                 }
25409               else
25410                 {
25411                   begin_scope (sk_function_parms, NULL_TREE);
25412                   cp_parser_parameter_declaration_list (parser, &error);
25413                   pop_bindings_and_leave_scope ();
25414                 }
25415               if (!cp_parser_error_occurred (parser) && !error)
25416                 done = true;
25417               cp_parser_abort_tentative_parse (parser);
25418
25419               parser->in_template_argument_list_p = saved_italp;
25420               break;
25421             }
25422         case CPP_CLOSE_PAREN:
25423         case CPP_ELLIPSIS:
25424           /* If we run into a non-nested `;', `}', or `]',
25425              then the code is invalid -- but the default
25426              argument is certainly over.  */
25427         case CPP_SEMICOLON:
25428         case CPP_CLOSE_BRACE:
25429         case CPP_CLOSE_SQUARE:
25430           if (depth == 0
25431               /* Handle correctly int n = sizeof ... ( p );  */
25432               && token->type != CPP_ELLIPSIS)
25433             done = true;
25434           /* Update DEPTH, if necessary.  */
25435           else if (token->type == CPP_CLOSE_PAREN
25436                    || token->type == CPP_CLOSE_BRACE
25437                    || token->type == CPP_CLOSE_SQUARE)
25438             --depth;
25439           break;
25440
25441         case CPP_OPEN_PAREN:
25442         case CPP_OPEN_SQUARE:
25443         case CPP_OPEN_BRACE:
25444           ++depth;
25445           break;
25446
25447         case CPP_LESS:
25448           if (depth == 0)
25449             /* This might be the comparison operator, or it might
25450                start a template argument list.  */
25451             ++maybe_template_id;
25452           break;
25453
25454         case CPP_RSHIFT:
25455           if (cxx_dialect == cxx98)
25456             break;
25457           /* Fall through for C++0x, which treats the `>>'
25458              operator like two `>' tokens in certain
25459              cases.  */
25460
25461         case CPP_GREATER:
25462           if (depth == 0)
25463             {
25464               /* This might be an operator, or it might close a
25465                  template argument list.  But if a previous '<'
25466                  started a template argument list, this will have
25467                  closed it, so we can't be in one anymore.  */
25468               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25469               if (maybe_template_id < 0)
25470                 maybe_template_id = 0;
25471             }
25472           break;
25473
25474           /* If we run out of tokens, issue an error message.  */
25475         case CPP_EOF:
25476         case CPP_PRAGMA_EOL:
25477           error_at (token->location, "file ends in default argument");
25478           done = true;
25479           break;
25480
25481         case CPP_NAME:
25482         case CPP_SCOPE:
25483           /* In these cases, we should look for template-ids.
25484              For example, if the default argument is
25485              `X<int, double>()', we need to do name lookup to
25486              figure out whether or not `X' is a template; if
25487              so, the `,' does not end the default argument.
25488
25489              That is not yet done.  */
25490           break;
25491
25492         default:
25493           break;
25494         }
25495
25496       /* If we've reached the end, stop.  */
25497       if (done)
25498         break;
25499
25500       /* Add the token to the token block.  */
25501       token = cp_lexer_consume_token (parser->lexer);
25502     }
25503
25504   /* Create a DEFAULT_ARG to represent the unparsed default
25505      argument.  */
25506   default_argument = make_node (DEFAULT_ARG);
25507   DEFARG_TOKENS (default_argument)
25508     = cp_token_cache_new (first_token, token);
25509   DEFARG_INSTANTIATIONS (default_argument) = NULL;
25510
25511   return default_argument;
25512 }
25513
25514 /* Begin parsing tentatively.  We always save tokens while parsing
25515    tentatively so that if the tentative parsing fails we can restore the
25516    tokens.  */
25517
25518 static void
25519 cp_parser_parse_tentatively (cp_parser* parser)
25520 {
25521   /* Enter a new parsing context.  */
25522   parser->context = cp_parser_context_new (parser->context);
25523   /* Begin saving tokens.  */
25524   cp_lexer_save_tokens (parser->lexer);
25525   /* In order to avoid repetitive access control error messages,
25526      access checks are queued up until we are no longer parsing
25527      tentatively.  */
25528   push_deferring_access_checks (dk_deferred);
25529 }
25530
25531 /* Commit to the currently active tentative parse.  */
25532
25533 static void
25534 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25535 {
25536   cp_parser_context *context;
25537   cp_lexer *lexer;
25538
25539   /* Mark all of the levels as committed.  */
25540   lexer = parser->lexer;
25541   for (context = parser->context; context->next; context = context->next)
25542     {
25543       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25544         break;
25545       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25546       while (!cp_lexer_saving_tokens (lexer))
25547         lexer = lexer->next;
25548       cp_lexer_commit_tokens (lexer);
25549     }
25550 }
25551
25552 /* Commit to the topmost currently active tentative parse.
25553
25554    Note that this function shouldn't be called when there are
25555    irreversible side-effects while in a tentative state.  For
25556    example, we shouldn't create a permanent entry in the symbol
25557    table, or issue an error message that might not apply if the
25558    tentative parse is aborted.  */
25559
25560 static void
25561 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25562 {
25563   cp_parser_context *context = parser->context;
25564   cp_lexer *lexer = parser->lexer;
25565
25566   if (context)
25567     {
25568       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25569         return;
25570       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25571
25572       while (!cp_lexer_saving_tokens (lexer))
25573         lexer = lexer->next;
25574       cp_lexer_commit_tokens (lexer);
25575     }
25576 }
25577
25578 /* Abort the currently active tentative parse.  All consumed tokens
25579    will be rolled back, and no diagnostics will be issued.  */
25580
25581 static void
25582 cp_parser_abort_tentative_parse (cp_parser* parser)
25583 {
25584   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25585               || errorcount > 0);
25586   cp_parser_simulate_error (parser);
25587   /* Now, pretend that we want to see if the construct was
25588      successfully parsed.  */
25589   cp_parser_parse_definitely (parser);
25590 }
25591
25592 /* Stop parsing tentatively.  If a parse error has occurred, restore the
25593    token stream.  Otherwise, commit to the tokens we have consumed.
25594    Returns true if no error occurred; false otherwise.  */
25595
25596 static bool
25597 cp_parser_parse_definitely (cp_parser* parser)
25598 {
25599   bool error_occurred;
25600   cp_parser_context *context;
25601
25602   /* Remember whether or not an error occurred, since we are about to
25603      destroy that information.  */
25604   error_occurred = cp_parser_error_occurred (parser);
25605   /* Remove the topmost context from the stack.  */
25606   context = parser->context;
25607   parser->context = context->next;
25608   /* If no parse errors occurred, commit to the tentative parse.  */
25609   if (!error_occurred)
25610     {
25611       /* Commit to the tokens read tentatively, unless that was
25612          already done.  */
25613       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25614         cp_lexer_commit_tokens (parser->lexer);
25615
25616       pop_to_parent_deferring_access_checks ();
25617     }
25618   /* Otherwise, if errors occurred, roll back our state so that things
25619      are just as they were before we began the tentative parse.  */
25620   else
25621     {
25622       cp_lexer_rollback_tokens (parser->lexer);
25623       pop_deferring_access_checks ();
25624     }
25625   /* Add the context to the front of the free list.  */
25626   context->next = cp_parser_context_free_list;
25627   cp_parser_context_free_list = context;
25628
25629   return !error_occurred;
25630 }
25631
25632 /* Returns true if we are parsing tentatively and are not committed to
25633    this tentative parse.  */
25634
25635 static bool
25636 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25637 {
25638   return (cp_parser_parsing_tentatively (parser)
25639           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25640 }
25641
25642 /* Returns nonzero iff an error has occurred during the most recent
25643    tentative parse.  */
25644
25645 static bool
25646 cp_parser_error_occurred (cp_parser* parser)
25647 {
25648   return (cp_parser_parsing_tentatively (parser)
25649           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25650 }
25651
25652 /* Returns nonzero if GNU extensions are allowed.  */
25653
25654 static bool
25655 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25656 {
25657   return parser->allow_gnu_extensions_p;
25658 }
25659 \f
25660 /* Objective-C++ Productions */
25661
25662
25663 /* Parse an Objective-C expression, which feeds into a primary-expression
25664    above.
25665
25666    objc-expression:
25667      objc-message-expression
25668      objc-string-literal
25669      objc-encode-expression
25670      objc-protocol-expression
25671      objc-selector-expression
25672
25673   Returns a tree representation of the expression.  */
25674
25675 static tree
25676 cp_parser_objc_expression (cp_parser* parser)
25677 {
25678   /* Try to figure out what kind of declaration is present.  */
25679   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25680
25681   switch (kwd->type)
25682     {
25683     case CPP_OPEN_SQUARE:
25684       return cp_parser_objc_message_expression (parser);
25685
25686     case CPP_OBJC_STRING:
25687       kwd = cp_lexer_consume_token (parser->lexer);
25688       return objc_build_string_object (kwd->u.value);
25689
25690     case CPP_KEYWORD:
25691       switch (kwd->keyword)
25692         {
25693         case RID_AT_ENCODE:
25694           return cp_parser_objc_encode_expression (parser);
25695
25696         case RID_AT_PROTOCOL:
25697           return cp_parser_objc_protocol_expression (parser);
25698
25699         case RID_AT_SELECTOR:
25700           return cp_parser_objc_selector_expression (parser);
25701
25702         default:
25703           break;
25704         }
25705     default:
25706       error_at (kwd->location,
25707                 "misplaced %<@%D%> Objective-C++ construct",
25708                 kwd->u.value);
25709       cp_parser_skip_to_end_of_block_or_statement (parser);
25710     }
25711
25712   return error_mark_node;
25713 }
25714
25715 /* Parse an Objective-C message expression.
25716
25717    objc-message-expression:
25718      [ objc-message-receiver objc-message-args ]
25719
25720    Returns a representation of an Objective-C message.  */
25721
25722 static tree
25723 cp_parser_objc_message_expression (cp_parser* parser)
25724 {
25725   tree receiver, messageargs;
25726
25727   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
25728   receiver = cp_parser_objc_message_receiver (parser);
25729   messageargs = cp_parser_objc_message_args (parser);
25730   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25731
25732   return objc_build_message_expr (receiver, messageargs);
25733 }
25734
25735 /* Parse an objc-message-receiver.
25736
25737    objc-message-receiver:
25738      expression
25739      simple-type-specifier
25740
25741   Returns a representation of the type or expression.  */
25742
25743 static tree
25744 cp_parser_objc_message_receiver (cp_parser* parser)
25745 {
25746   tree rcv;
25747
25748   /* An Objective-C message receiver may be either (1) a type
25749      or (2) an expression.  */
25750   cp_parser_parse_tentatively (parser);
25751   rcv = cp_parser_expression (parser);
25752
25753   /* If that worked out, fine.  */
25754   if (cp_parser_parse_definitely (parser))
25755     return rcv;
25756
25757   cp_parser_parse_tentatively (parser);
25758   rcv = cp_parser_simple_type_specifier (parser,
25759                                          /*decl_specs=*/NULL,
25760                                          CP_PARSER_FLAGS_NONE);
25761
25762   if (cp_parser_parse_definitely (parser))
25763     return objc_get_class_reference (rcv);
25764   
25765   cp_parser_error (parser, "objective-c++ message receiver expected");
25766   return error_mark_node;
25767 }
25768
25769 /* Parse the arguments and selectors comprising an Objective-C message.
25770
25771    objc-message-args:
25772      objc-selector
25773      objc-selector-args
25774      objc-selector-args , objc-comma-args
25775
25776    objc-selector-args:
25777      objc-selector [opt] : assignment-expression
25778      objc-selector-args objc-selector [opt] : assignment-expression
25779
25780    objc-comma-args:
25781      assignment-expression
25782      objc-comma-args , assignment-expression
25783
25784    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25785    selector arguments and TREE_VALUE containing a list of comma
25786    arguments.  */
25787
25788 static tree
25789 cp_parser_objc_message_args (cp_parser* parser)
25790 {
25791   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25792   bool maybe_unary_selector_p = true;
25793   cp_token *token = cp_lexer_peek_token (parser->lexer);
25794
25795   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25796     {
25797       tree selector = NULL_TREE, arg;
25798
25799       if (token->type != CPP_COLON)
25800         selector = cp_parser_objc_selector (parser);
25801
25802       /* Detect if we have a unary selector.  */
25803       if (maybe_unary_selector_p
25804           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25805         return build_tree_list (selector, NULL_TREE);
25806
25807       maybe_unary_selector_p = false;
25808       cp_parser_require (parser, CPP_COLON, RT_COLON);
25809       arg = cp_parser_assignment_expression (parser);
25810
25811       sel_args
25812         = chainon (sel_args,
25813                    build_tree_list (selector, arg));
25814
25815       token = cp_lexer_peek_token (parser->lexer);
25816     }
25817
25818   /* Handle non-selector arguments, if any. */
25819   while (token->type == CPP_COMMA)
25820     {
25821       tree arg;
25822
25823       cp_lexer_consume_token (parser->lexer);
25824       arg = cp_parser_assignment_expression (parser);
25825
25826       addl_args
25827         = chainon (addl_args,
25828                    build_tree_list (NULL_TREE, arg));
25829
25830       token = cp_lexer_peek_token (parser->lexer);
25831     }
25832
25833   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25834     {
25835       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25836       return build_tree_list (error_mark_node, error_mark_node);
25837     }
25838
25839   return build_tree_list (sel_args, addl_args);
25840 }
25841
25842 /* Parse an Objective-C encode expression.
25843
25844    objc-encode-expression:
25845      @encode objc-typename
25846
25847    Returns an encoded representation of the type argument.  */
25848
25849 static tree
25850 cp_parser_objc_encode_expression (cp_parser* parser)
25851 {
25852   tree type;
25853   cp_token *token;
25854
25855   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
25856   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25857   token = cp_lexer_peek_token (parser->lexer);
25858   type = complete_type (cp_parser_type_id (parser));
25859   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25860
25861   if (!type)
25862     {
25863       error_at (token->location, 
25864                 "%<@encode%> must specify a type as an argument");
25865       return error_mark_node;
25866     }
25867
25868   /* This happens if we find @encode(T) (where T is a template
25869      typename or something dependent on a template typename) when
25870      parsing a template.  In that case, we can't compile it
25871      immediately, but we rather create an AT_ENCODE_EXPR which will
25872      need to be instantiated when the template is used.
25873   */
25874   if (dependent_type_p (type))
25875     {
25876       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25877       TREE_READONLY (value) = 1;
25878       return value;
25879     }
25880
25881   return objc_build_encode_expr (type);
25882 }
25883
25884 /* Parse an Objective-C @defs expression.  */
25885
25886 static tree
25887 cp_parser_objc_defs_expression (cp_parser *parser)
25888 {
25889   tree name;
25890
25891   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
25892   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25893   name = cp_parser_identifier (parser);
25894   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25895
25896   return objc_get_class_ivars (name);
25897 }
25898
25899 /* Parse an Objective-C protocol expression.
25900
25901   objc-protocol-expression:
25902     @protocol ( identifier )
25903
25904   Returns a representation of the protocol expression.  */
25905
25906 static tree
25907 cp_parser_objc_protocol_expression (cp_parser* parser)
25908 {
25909   tree proto;
25910
25911   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
25912   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25913   proto = cp_parser_identifier (parser);
25914   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25915
25916   return objc_build_protocol_expr (proto);
25917 }
25918
25919 /* Parse an Objective-C selector expression.
25920
25921    objc-selector-expression:
25922      @selector ( objc-method-signature )
25923
25924    objc-method-signature:
25925      objc-selector
25926      objc-selector-seq
25927
25928    objc-selector-seq:
25929      objc-selector :
25930      objc-selector-seq objc-selector :
25931
25932   Returns a representation of the method selector.  */
25933
25934 static tree
25935 cp_parser_objc_selector_expression (cp_parser* parser)
25936 {
25937   tree sel_seq = NULL_TREE;
25938   bool maybe_unary_selector_p = true;
25939   cp_token *token;
25940   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25941
25942   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
25943   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25944   token = cp_lexer_peek_token (parser->lexer);
25945
25946   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25947          || token->type == CPP_SCOPE)
25948     {
25949       tree selector = NULL_TREE;
25950
25951       if (token->type != CPP_COLON
25952           || token->type == CPP_SCOPE)
25953         selector = cp_parser_objc_selector (parser);
25954
25955       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25956           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25957         {
25958           /* Detect if we have a unary selector.  */
25959           if (maybe_unary_selector_p)
25960             {
25961               sel_seq = selector;
25962               goto finish_selector;
25963             }
25964           else
25965             {
25966               cp_parser_error (parser, "expected %<:%>");
25967             }
25968         }
25969       maybe_unary_selector_p = false;
25970       token = cp_lexer_consume_token (parser->lexer);
25971
25972       if (token->type == CPP_SCOPE)
25973         {
25974           sel_seq
25975             = chainon (sel_seq,
25976                        build_tree_list (selector, NULL_TREE));
25977           sel_seq
25978             = chainon (sel_seq,
25979                        build_tree_list (NULL_TREE, NULL_TREE));
25980         }
25981       else
25982         sel_seq
25983           = chainon (sel_seq,
25984                      build_tree_list (selector, NULL_TREE));
25985
25986       token = cp_lexer_peek_token (parser->lexer);
25987     }
25988
25989  finish_selector:
25990   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25991
25992   return objc_build_selector_expr (loc, sel_seq);
25993 }
25994
25995 /* Parse a list of identifiers.
25996
25997    objc-identifier-list:
25998      identifier
25999      objc-identifier-list , identifier
26000
26001    Returns a TREE_LIST of identifier nodes.  */
26002
26003 static tree
26004 cp_parser_objc_identifier_list (cp_parser* parser)
26005 {
26006   tree identifier;
26007   tree list;
26008   cp_token *sep;
26009
26010   identifier = cp_parser_identifier (parser);
26011   if (identifier == error_mark_node)
26012     return error_mark_node;      
26013
26014   list = build_tree_list (NULL_TREE, identifier);
26015   sep = cp_lexer_peek_token (parser->lexer);
26016
26017   while (sep->type == CPP_COMMA)
26018     {
26019       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26020       identifier = cp_parser_identifier (parser);
26021       if (identifier == error_mark_node)
26022         return list;
26023
26024       list = chainon (list, build_tree_list (NULL_TREE,
26025                                              identifier));
26026       sep = cp_lexer_peek_token (parser->lexer);
26027     }
26028   
26029   return list;
26030 }
26031
26032 /* Parse an Objective-C alias declaration.
26033
26034    objc-alias-declaration:
26035      @compatibility_alias identifier identifier ;
26036
26037    This function registers the alias mapping with the Objective-C front end.
26038    It returns nothing.  */
26039
26040 static void
26041 cp_parser_objc_alias_declaration (cp_parser* parser)
26042 {
26043   tree alias, orig;
26044
26045   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
26046   alias = cp_parser_identifier (parser);
26047   orig = cp_parser_identifier (parser);
26048   objc_declare_alias (alias, orig);
26049   cp_parser_consume_semicolon_at_end_of_statement (parser);
26050 }
26051
26052 /* Parse an Objective-C class forward-declaration.
26053
26054    objc-class-declaration:
26055      @class objc-identifier-list ;
26056
26057    The function registers the forward declarations with the Objective-C
26058    front end.  It returns nothing.  */
26059
26060 static void
26061 cp_parser_objc_class_declaration (cp_parser* parser)
26062 {
26063   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
26064   while (true)
26065     {
26066       tree id;
26067       
26068       id = cp_parser_identifier (parser);
26069       if (id == error_mark_node)
26070         break;
26071       
26072       objc_declare_class (id);
26073
26074       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26075         cp_lexer_consume_token (parser->lexer);
26076       else
26077         break;
26078     }
26079   cp_parser_consume_semicolon_at_end_of_statement (parser);
26080 }
26081
26082 /* Parse a list of Objective-C protocol references.
26083
26084    objc-protocol-refs-opt:
26085      objc-protocol-refs [opt]
26086
26087    objc-protocol-refs:
26088      < objc-identifier-list >
26089
26090    Returns a TREE_LIST of identifiers, if any.  */
26091
26092 static tree
26093 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26094 {
26095   tree protorefs = NULL_TREE;
26096
26097   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26098     {
26099       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
26100       protorefs = cp_parser_objc_identifier_list (parser);
26101       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26102     }
26103
26104   return protorefs;
26105 }
26106
26107 /* Parse a Objective-C visibility specification.  */
26108
26109 static void
26110 cp_parser_objc_visibility_spec (cp_parser* parser)
26111 {
26112   cp_token *vis = cp_lexer_peek_token (parser->lexer);
26113
26114   switch (vis->keyword)
26115     {
26116     case RID_AT_PRIVATE:
26117       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26118       break;
26119     case RID_AT_PROTECTED:
26120       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26121       break;
26122     case RID_AT_PUBLIC:
26123       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26124       break;
26125     case RID_AT_PACKAGE:
26126       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26127       break;
26128     default:
26129       return;
26130     }
26131
26132   /* Eat '@private'/'@protected'/'@public'.  */
26133   cp_lexer_consume_token (parser->lexer);
26134 }
26135
26136 /* Parse an Objective-C method type.  Return 'true' if it is a class
26137    (+) method, and 'false' if it is an instance (-) method.  */
26138
26139 static inline bool
26140 cp_parser_objc_method_type (cp_parser* parser)
26141 {
26142   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26143     return true;
26144   else
26145     return false;
26146 }
26147
26148 /* Parse an Objective-C protocol qualifier.  */
26149
26150 static tree
26151 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26152 {
26153   tree quals = NULL_TREE, node;
26154   cp_token *token = cp_lexer_peek_token (parser->lexer);
26155
26156   node = token->u.value;
26157
26158   while (node && identifier_p (node)
26159          && (node == ridpointers [(int) RID_IN]
26160              || node == ridpointers [(int) RID_OUT]
26161              || node == ridpointers [(int) RID_INOUT]
26162              || node == ridpointers [(int) RID_BYCOPY]
26163              || node == ridpointers [(int) RID_BYREF]
26164              || node == ridpointers [(int) RID_ONEWAY]))
26165     {
26166       quals = tree_cons (NULL_TREE, node, quals);
26167       cp_lexer_consume_token (parser->lexer);
26168       token = cp_lexer_peek_token (parser->lexer);
26169       node = token->u.value;
26170     }
26171
26172   return quals;
26173 }
26174
26175 /* Parse an Objective-C typename.  */
26176
26177 static tree
26178 cp_parser_objc_typename (cp_parser* parser)
26179 {
26180   tree type_name = NULL_TREE;
26181
26182   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26183     {
26184       tree proto_quals, cp_type = NULL_TREE;
26185
26186       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
26187       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26188
26189       /* An ObjC type name may consist of just protocol qualifiers, in which
26190          case the type shall default to 'id'.  */
26191       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26192         {
26193           cp_type = cp_parser_type_id (parser);
26194           
26195           /* If the type could not be parsed, an error has already
26196              been produced.  For error recovery, behave as if it had
26197              not been specified, which will use the default type
26198              'id'.  */
26199           if (cp_type == error_mark_node)
26200             {
26201               cp_type = NULL_TREE;
26202               /* We need to skip to the closing parenthesis as
26203                  cp_parser_type_id() does not seem to do it for
26204                  us.  */
26205               cp_parser_skip_to_closing_parenthesis (parser,
26206                                                      /*recovering=*/true,
26207                                                      /*or_comma=*/false,
26208                                                      /*consume_paren=*/false);
26209             }
26210         }
26211
26212       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26213       type_name = build_tree_list (proto_quals, cp_type);
26214     }
26215
26216   return type_name;
26217 }
26218
26219 /* Check to see if TYPE refers to an Objective-C selector name.  */
26220
26221 static bool
26222 cp_parser_objc_selector_p (enum cpp_ttype type)
26223 {
26224   return (type == CPP_NAME || type == CPP_KEYWORD
26225           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26226           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26227           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26228           || type == CPP_XOR || type == CPP_XOR_EQ);
26229 }
26230
26231 /* Parse an Objective-C selector.  */
26232
26233 static tree
26234 cp_parser_objc_selector (cp_parser* parser)
26235 {
26236   cp_token *token = cp_lexer_consume_token (parser->lexer);
26237
26238   if (!cp_parser_objc_selector_p (token->type))
26239     {
26240       error_at (token->location, "invalid Objective-C++ selector name");
26241       return error_mark_node;
26242     }
26243
26244   /* C++ operator names are allowed to appear in ObjC selectors.  */
26245   switch (token->type)
26246     {
26247     case CPP_AND_AND: return get_identifier ("and");
26248     case CPP_AND_EQ: return get_identifier ("and_eq");
26249     case CPP_AND: return get_identifier ("bitand");
26250     case CPP_OR: return get_identifier ("bitor");
26251     case CPP_COMPL: return get_identifier ("compl");
26252     case CPP_NOT: return get_identifier ("not");
26253     case CPP_NOT_EQ: return get_identifier ("not_eq");
26254     case CPP_OR_OR: return get_identifier ("or");
26255     case CPP_OR_EQ: return get_identifier ("or_eq");
26256     case CPP_XOR: return get_identifier ("xor");
26257     case CPP_XOR_EQ: return get_identifier ("xor_eq");
26258     default: return token->u.value;
26259     }
26260 }
26261
26262 /* Parse an Objective-C params list.  */
26263
26264 static tree
26265 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26266 {
26267   tree params = NULL_TREE;
26268   bool maybe_unary_selector_p = true;
26269   cp_token *token = cp_lexer_peek_token (parser->lexer);
26270
26271   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26272     {
26273       tree selector = NULL_TREE, type_name, identifier;
26274       tree parm_attr = NULL_TREE;
26275
26276       if (token->keyword == RID_ATTRIBUTE)
26277         break;
26278
26279       if (token->type != CPP_COLON)
26280         selector = cp_parser_objc_selector (parser);
26281
26282       /* Detect if we have a unary selector.  */
26283       if (maybe_unary_selector_p
26284           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26285         {
26286           params = selector; /* Might be followed by attributes.  */
26287           break;
26288         }
26289
26290       maybe_unary_selector_p = false;
26291       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26292         {
26293           /* Something went quite wrong.  There should be a colon
26294              here, but there is not.  Stop parsing parameters.  */
26295           break;
26296         }
26297       type_name = cp_parser_objc_typename (parser);
26298       /* New ObjC allows attributes on parameters too.  */
26299       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26300         parm_attr = cp_parser_attributes_opt (parser);
26301       identifier = cp_parser_identifier (parser);
26302
26303       params
26304         = chainon (params,
26305                    objc_build_keyword_decl (selector,
26306                                             type_name,
26307                                             identifier,
26308                                             parm_attr));
26309
26310       token = cp_lexer_peek_token (parser->lexer);
26311     }
26312
26313   if (params == NULL_TREE)
26314     {
26315       cp_parser_error (parser, "objective-c++ method declaration is expected");
26316       return error_mark_node;
26317     }
26318
26319   /* We allow tail attributes for the method.  */
26320   if (token->keyword == RID_ATTRIBUTE)
26321     {
26322       *attributes = cp_parser_attributes_opt (parser);
26323       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26324           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26325         return params;
26326       cp_parser_error (parser, 
26327                        "method attributes must be specified at the end");
26328       return error_mark_node;
26329     }
26330
26331   if (params == NULL_TREE)
26332     {
26333       cp_parser_error (parser, "objective-c++ method declaration is expected");
26334       return error_mark_node;
26335     }
26336   return params;
26337 }
26338
26339 /* Parse the non-keyword Objective-C params.  */
26340
26341 static tree
26342 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
26343                                        tree* attributes)
26344 {
26345   tree params = make_node (TREE_LIST);
26346   cp_token *token = cp_lexer_peek_token (parser->lexer);
26347   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
26348
26349   while (token->type == CPP_COMMA)
26350     {
26351       cp_parameter_declarator *parmdecl;
26352       tree parm;
26353
26354       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26355       token = cp_lexer_peek_token (parser->lexer);
26356
26357       if (token->type == CPP_ELLIPSIS)
26358         {
26359           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
26360           *ellipsisp = true;
26361           token = cp_lexer_peek_token (parser->lexer);
26362           break;
26363         }
26364
26365       /* TODO: parse attributes for tail parameters.  */
26366       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26367       parm = grokdeclarator (parmdecl->declarator,
26368                              &parmdecl->decl_specifiers,
26369                              PARM, /*initialized=*/0,
26370                              /*attrlist=*/NULL);
26371
26372       chainon (params, build_tree_list (NULL_TREE, parm));
26373       token = cp_lexer_peek_token (parser->lexer);
26374     }
26375
26376   /* We allow tail attributes for the method.  */
26377   if (token->keyword == RID_ATTRIBUTE)
26378     {
26379       if (*attributes == NULL_TREE)
26380         {
26381           *attributes = cp_parser_attributes_opt (parser);
26382           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26383               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26384             return params;
26385         }
26386       else        
26387         /* We have an error, but parse the attributes, so that we can 
26388            carry on.  */
26389         *attributes = cp_parser_attributes_opt (parser);
26390
26391       cp_parser_error (parser, 
26392                        "method attributes must be specified at the end");
26393       return error_mark_node;
26394     }
26395
26396   return params;
26397 }
26398
26399 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
26400
26401 static void
26402 cp_parser_objc_interstitial_code (cp_parser* parser)
26403 {
26404   cp_token *token = cp_lexer_peek_token (parser->lexer);
26405
26406   /* If the next token is `extern' and the following token is a string
26407      literal, then we have a linkage specification.  */
26408   if (token->keyword == RID_EXTERN
26409       && cp_parser_is_pure_string_literal
26410          (cp_lexer_peek_nth_token (parser->lexer, 2)))
26411     cp_parser_linkage_specification (parser);
26412   /* Handle #pragma, if any.  */
26413   else if (token->type == CPP_PRAGMA)
26414     cp_parser_pragma (parser, pragma_objc_icode);
26415   /* Allow stray semicolons.  */
26416   else if (token->type == CPP_SEMICOLON)
26417     cp_lexer_consume_token (parser->lexer);
26418   /* Mark methods as optional or required, when building protocols.  */
26419   else if (token->keyword == RID_AT_OPTIONAL)
26420     {
26421       cp_lexer_consume_token (parser->lexer);
26422       objc_set_method_opt (true);
26423     }
26424   else if (token->keyword == RID_AT_REQUIRED)
26425     {
26426       cp_lexer_consume_token (parser->lexer);
26427       objc_set_method_opt (false);
26428     }
26429   else if (token->keyword == RID_NAMESPACE)
26430     cp_parser_namespace_definition (parser);
26431   /* Other stray characters must generate errors.  */
26432   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26433     {
26434       cp_lexer_consume_token (parser->lexer);
26435       error ("stray %qs between Objective-C++ methods",
26436              token->type == CPP_OPEN_BRACE ? "{" : "}");
26437     }
26438   /* Finally, try to parse a block-declaration, or a function-definition.  */
26439   else
26440     cp_parser_block_declaration (parser, /*statement_p=*/false);
26441 }
26442
26443 /* Parse a method signature.  */
26444
26445 static tree
26446 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26447 {
26448   tree rettype, kwdparms, optparms;
26449   bool ellipsis = false;
26450   bool is_class_method;
26451
26452   is_class_method = cp_parser_objc_method_type (parser);
26453   rettype = cp_parser_objc_typename (parser);
26454   *attributes = NULL_TREE;
26455   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26456   if (kwdparms == error_mark_node)
26457     return error_mark_node;
26458   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26459   if (optparms == error_mark_node)
26460     return error_mark_node;
26461
26462   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26463 }
26464
26465 static bool
26466 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26467 {
26468   tree tattr;  
26469   cp_lexer_save_tokens (parser->lexer);
26470   tattr = cp_parser_attributes_opt (parser);
26471   gcc_assert (tattr) ;
26472   
26473   /* If the attributes are followed by a method introducer, this is not allowed.
26474      Dump the attributes and flag the situation.  */
26475   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26476       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26477     return true;
26478
26479   /* Otherwise, the attributes introduce some interstitial code, possibly so
26480      rewind to allow that check.  */
26481   cp_lexer_rollback_tokens (parser->lexer);
26482   return false;  
26483 }
26484
26485 /* Parse an Objective-C method prototype list.  */
26486
26487 static void
26488 cp_parser_objc_method_prototype_list (cp_parser* parser)
26489 {
26490   cp_token *token = cp_lexer_peek_token (parser->lexer);
26491
26492   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26493     {
26494       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26495         {
26496           tree attributes, sig;
26497           bool is_class_method;
26498           if (token->type == CPP_PLUS)
26499             is_class_method = true;
26500           else
26501             is_class_method = false;
26502           sig = cp_parser_objc_method_signature (parser, &attributes);
26503           if (sig == error_mark_node)
26504             {
26505               cp_parser_skip_to_end_of_block_or_statement (parser);
26506               token = cp_lexer_peek_token (parser->lexer);
26507               continue;
26508             }
26509           objc_add_method_declaration (is_class_method, sig, attributes);
26510           cp_parser_consume_semicolon_at_end_of_statement (parser);
26511         }
26512       else if (token->keyword == RID_AT_PROPERTY)
26513         cp_parser_objc_at_property_declaration (parser);
26514       else if (token->keyword == RID_ATTRIBUTE 
26515                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26516         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
26517                     OPT_Wattributes, 
26518                     "prefix attributes are ignored for methods");
26519       else
26520         /* Allow for interspersed non-ObjC++ code.  */
26521         cp_parser_objc_interstitial_code (parser);
26522
26523       token = cp_lexer_peek_token (parser->lexer);
26524     }
26525
26526   if (token->type != CPP_EOF)
26527     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26528   else
26529     cp_parser_error (parser, "expected %<@end%>");
26530
26531   objc_finish_interface ();
26532 }
26533
26534 /* Parse an Objective-C method definition list.  */
26535
26536 static void
26537 cp_parser_objc_method_definition_list (cp_parser* parser)
26538 {
26539   cp_token *token = cp_lexer_peek_token (parser->lexer);
26540
26541   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26542     {
26543       tree meth;
26544
26545       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26546         {
26547           cp_token *ptk;
26548           tree sig, attribute;
26549           bool is_class_method;
26550           if (token->type == CPP_PLUS)
26551             is_class_method = true;
26552           else
26553             is_class_method = false;
26554           push_deferring_access_checks (dk_deferred);
26555           sig = cp_parser_objc_method_signature (parser, &attribute);
26556           if (sig == error_mark_node)
26557             {
26558               cp_parser_skip_to_end_of_block_or_statement (parser);
26559               token = cp_lexer_peek_token (parser->lexer);
26560               continue;
26561             }
26562           objc_start_method_definition (is_class_method, sig, attribute,
26563                                         NULL_TREE);
26564
26565           /* For historical reasons, we accept an optional semicolon.  */
26566           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26567             cp_lexer_consume_token (parser->lexer);
26568
26569           ptk = cp_lexer_peek_token (parser->lexer);
26570           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
26571                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26572             {
26573               perform_deferred_access_checks (tf_warning_or_error);
26574               stop_deferring_access_checks ();
26575               meth = cp_parser_function_definition_after_declarator (parser,
26576                                                                      false);
26577               pop_deferring_access_checks ();
26578               objc_finish_method_definition (meth);
26579             }
26580         }
26581       /* The following case will be removed once @synthesize is
26582          completely implemented.  */
26583       else if (token->keyword == RID_AT_PROPERTY)
26584         cp_parser_objc_at_property_declaration (parser);
26585       else if (token->keyword == RID_AT_SYNTHESIZE)
26586         cp_parser_objc_at_synthesize_declaration (parser);
26587       else if (token->keyword == RID_AT_DYNAMIC)
26588         cp_parser_objc_at_dynamic_declaration (parser);
26589       else if (token->keyword == RID_ATTRIBUTE 
26590                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26591         warning_at (token->location, OPT_Wattributes,
26592                     "prefix attributes are ignored for methods");
26593       else
26594         /* Allow for interspersed non-ObjC++ code.  */
26595         cp_parser_objc_interstitial_code (parser);
26596
26597       token = cp_lexer_peek_token (parser->lexer);
26598     }
26599
26600   if (token->type != CPP_EOF)
26601     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26602   else
26603     cp_parser_error (parser, "expected %<@end%>");
26604
26605   objc_finish_implementation ();
26606 }
26607
26608 /* Parse Objective-C ivars.  */
26609
26610 static void
26611 cp_parser_objc_class_ivars (cp_parser* parser)
26612 {
26613   cp_token *token = cp_lexer_peek_token (parser->lexer);
26614
26615   if (token->type != CPP_OPEN_BRACE)
26616     return;     /* No ivars specified.  */
26617
26618   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
26619   token = cp_lexer_peek_token (parser->lexer);
26620
26621   while (token->type != CPP_CLOSE_BRACE 
26622         && token->keyword != RID_AT_END && token->type != CPP_EOF)
26623     {
26624       cp_decl_specifier_seq declspecs;
26625       int decl_class_or_enum_p;
26626       tree prefix_attributes;
26627
26628       cp_parser_objc_visibility_spec (parser);
26629
26630       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26631         break;
26632
26633       cp_parser_decl_specifier_seq (parser,
26634                                     CP_PARSER_FLAGS_OPTIONAL,
26635                                     &declspecs,
26636                                     &decl_class_or_enum_p);
26637
26638       /* auto, register, static, extern, mutable.  */
26639       if (declspecs.storage_class != sc_none)
26640         {
26641           cp_parser_error (parser, "invalid type for instance variable");         
26642           declspecs.storage_class = sc_none;
26643         }
26644
26645       /* thread_local.  */
26646       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26647         {
26648           cp_parser_error (parser, "invalid type for instance variable");
26649           declspecs.locations[ds_thread] = 0;
26650         }
26651       
26652       /* typedef.  */
26653       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26654         {
26655           cp_parser_error (parser, "invalid type for instance variable");
26656           declspecs.locations[ds_typedef] = 0;
26657         }
26658
26659       prefix_attributes = declspecs.attributes;
26660       declspecs.attributes = NULL_TREE;
26661
26662       /* Keep going until we hit the `;' at the end of the
26663          declaration.  */
26664       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26665         {
26666           tree width = NULL_TREE, attributes, first_attribute, decl;
26667           cp_declarator *declarator = NULL;
26668           int ctor_dtor_or_conv_p;
26669
26670           /* Check for a (possibly unnamed) bitfield declaration.  */
26671           token = cp_lexer_peek_token (parser->lexer);
26672           if (token->type == CPP_COLON)
26673             goto eat_colon;
26674
26675           if (token->type == CPP_NAME
26676               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26677                   == CPP_COLON))
26678             {
26679               /* Get the name of the bitfield.  */
26680               declarator = make_id_declarator (NULL_TREE,
26681                                                cp_parser_identifier (parser),
26682                                                sfk_none);
26683
26684              eat_colon:
26685               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26686               /* Get the width of the bitfield.  */
26687               width
26688                 = cp_parser_constant_expression (parser);
26689             }
26690           else
26691             {
26692               /* Parse the declarator.  */
26693               declarator
26694                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26695                                         &ctor_dtor_or_conv_p,
26696                                         /*parenthesized_p=*/NULL,
26697                                         /*member_p=*/false,
26698                                         /*friend_p=*/false);
26699             }
26700
26701           /* Look for attributes that apply to the ivar.  */
26702           attributes = cp_parser_attributes_opt (parser);
26703           /* Remember which attributes are prefix attributes and
26704              which are not.  */
26705           first_attribute = attributes;
26706           /* Combine the attributes.  */
26707           attributes = chainon (prefix_attributes, attributes);
26708
26709           if (width)
26710               /* Create the bitfield declaration.  */
26711               decl = grokbitfield (declarator, &declspecs,
26712                                    width,
26713                                    attributes);
26714           else
26715             decl = grokfield (declarator, &declspecs,
26716                               NULL_TREE, /*init_const_expr_p=*/false,
26717                               NULL_TREE, attributes);
26718
26719           /* Add the instance variable.  */
26720           if (decl != error_mark_node && decl != NULL_TREE)
26721             objc_add_instance_variable (decl);
26722
26723           /* Reset PREFIX_ATTRIBUTES.  */
26724           while (attributes && TREE_CHAIN (attributes) != first_attribute)
26725             attributes = TREE_CHAIN (attributes);
26726           if (attributes)
26727             TREE_CHAIN (attributes) = NULL_TREE;
26728
26729           token = cp_lexer_peek_token (parser->lexer);
26730
26731           if (token->type == CPP_COMMA)
26732             {
26733               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26734               continue;
26735             }
26736           break;
26737         }
26738
26739       cp_parser_consume_semicolon_at_end_of_statement (parser);
26740       token = cp_lexer_peek_token (parser->lexer);
26741     }
26742
26743   if (token->keyword == RID_AT_END)
26744     cp_parser_error (parser, "expected %<}%>");
26745
26746   /* Do not consume the RID_AT_END, so it will be read again as terminating
26747      the @interface of @implementation.  */ 
26748   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26749     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
26750     
26751   /* For historical reasons, we accept an optional semicolon.  */
26752   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26753     cp_lexer_consume_token (parser->lexer);
26754 }
26755
26756 /* Parse an Objective-C protocol declaration.  */
26757
26758 static void
26759 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26760 {
26761   tree proto, protorefs;
26762   cp_token *tok;
26763
26764   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
26765   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26766     {
26767       tok = cp_lexer_peek_token (parser->lexer);
26768       error_at (tok->location, "identifier expected after %<@protocol%>");
26769       cp_parser_consume_semicolon_at_end_of_statement (parser);
26770       return;
26771     }
26772
26773   /* See if we have a forward declaration or a definition.  */
26774   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26775
26776   /* Try a forward declaration first.  */
26777   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26778     {
26779       while (true)
26780         {
26781           tree id;
26782           
26783           id = cp_parser_identifier (parser);
26784           if (id == error_mark_node)
26785             break;
26786           
26787           objc_declare_protocol (id, attributes);
26788           
26789           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26790             cp_lexer_consume_token (parser->lexer);
26791           else
26792             break;
26793         }
26794       cp_parser_consume_semicolon_at_end_of_statement (parser);
26795     }
26796
26797   /* Ok, we got a full-fledged definition (or at least should).  */
26798   else
26799     {
26800       proto = cp_parser_identifier (parser);
26801       protorefs = cp_parser_objc_protocol_refs_opt (parser);
26802       objc_start_protocol (proto, protorefs, attributes);
26803       cp_parser_objc_method_prototype_list (parser);
26804     }
26805 }
26806
26807 /* Parse an Objective-C superclass or category.  */
26808
26809 static void
26810 cp_parser_objc_superclass_or_category (cp_parser *parser, 
26811                                        bool iface_p,
26812                                        tree *super,
26813                                        tree *categ, bool *is_class_extension)
26814 {
26815   cp_token *next = cp_lexer_peek_token (parser->lexer);
26816
26817   *super = *categ = NULL_TREE;
26818   *is_class_extension = false;
26819   if (next->type == CPP_COLON)
26820     {
26821       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26822       *super = cp_parser_identifier (parser);
26823     }
26824   else if (next->type == CPP_OPEN_PAREN)
26825     {
26826       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
26827
26828       /* If there is no category name, and this is an @interface, we
26829          have a class extension.  */
26830       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26831         {
26832           *categ = NULL_TREE;
26833           *is_class_extension = true;
26834         }
26835       else
26836         *categ = cp_parser_identifier (parser);
26837
26838       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26839     }
26840 }
26841
26842 /* Parse an Objective-C class interface.  */
26843
26844 static void
26845 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26846 {
26847   tree name, super, categ, protos;
26848   bool is_class_extension;
26849
26850   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
26851   name = cp_parser_identifier (parser);
26852   if (name == error_mark_node)
26853     {
26854       /* It's hard to recover because even if valid @interface stuff
26855          is to follow, we can't compile it (or validate it) if we
26856          don't even know which class it refers to.  Let's assume this
26857          was a stray '@interface' token in the stream and skip it.
26858       */
26859       return;
26860     }
26861   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26862                                          &is_class_extension);
26863   protos = cp_parser_objc_protocol_refs_opt (parser);
26864
26865   /* We have either a class or a category on our hands.  */
26866   if (categ || is_class_extension)
26867     objc_start_category_interface (name, categ, protos, attributes);
26868   else
26869     {
26870       objc_start_class_interface (name, super, protos, attributes);
26871       /* Handle instance variable declarations, if any.  */
26872       cp_parser_objc_class_ivars (parser);
26873       objc_continue_interface ();
26874     }
26875
26876   cp_parser_objc_method_prototype_list (parser);
26877 }
26878
26879 /* Parse an Objective-C class implementation.  */
26880
26881 static void
26882 cp_parser_objc_class_implementation (cp_parser* parser)
26883 {
26884   tree name, super, categ;
26885   bool is_class_extension;
26886
26887   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
26888   name = cp_parser_identifier (parser);
26889   if (name == error_mark_node)
26890     {
26891       /* It's hard to recover because even if valid @implementation
26892          stuff is to follow, we can't compile it (or validate it) if
26893          we don't even know which class it refers to.  Let's assume
26894          this was a stray '@implementation' token in the stream and
26895          skip it.
26896       */
26897       return;
26898     }
26899   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26900                                          &is_class_extension);
26901
26902   /* We have either a class or a category on our hands.  */
26903   if (categ)
26904     objc_start_category_implementation (name, categ);
26905   else
26906     {
26907       objc_start_class_implementation (name, super);
26908       /* Handle instance variable declarations, if any.  */
26909       cp_parser_objc_class_ivars (parser);
26910       objc_continue_implementation ();
26911     }
26912
26913   cp_parser_objc_method_definition_list (parser);
26914 }
26915
26916 /* Consume the @end token and finish off the implementation.  */
26917
26918 static void
26919 cp_parser_objc_end_implementation (cp_parser* parser)
26920 {
26921   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26922   objc_finish_implementation ();
26923 }
26924
26925 /* Parse an Objective-C declaration.  */
26926
26927 static void
26928 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26929 {
26930   /* Try to figure out what kind of declaration is present.  */
26931   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26932
26933   if (attributes)
26934     switch (kwd->keyword)
26935       {
26936         case RID_AT_ALIAS:
26937         case RID_AT_CLASS:
26938         case RID_AT_END:
26939           error_at (kwd->location, "attributes may not be specified before"
26940                     " the %<@%D%> Objective-C++ keyword",
26941                     kwd->u.value);
26942           attributes = NULL;
26943           break;
26944         case RID_AT_IMPLEMENTATION:
26945           warning_at (kwd->location, OPT_Wattributes,
26946                       "prefix attributes are ignored before %<@%D%>",
26947                       kwd->u.value);
26948           attributes = NULL;
26949         default:
26950           break;
26951       }
26952
26953   switch (kwd->keyword)
26954     {
26955     case RID_AT_ALIAS:
26956       cp_parser_objc_alias_declaration (parser);
26957       break;
26958     case RID_AT_CLASS:
26959       cp_parser_objc_class_declaration (parser);
26960       break;
26961     case RID_AT_PROTOCOL:
26962       cp_parser_objc_protocol_declaration (parser, attributes);
26963       break;
26964     case RID_AT_INTERFACE:
26965       cp_parser_objc_class_interface (parser, attributes);
26966       break;
26967     case RID_AT_IMPLEMENTATION:
26968       cp_parser_objc_class_implementation (parser);
26969       break;
26970     case RID_AT_END:
26971       cp_parser_objc_end_implementation (parser);
26972       break;
26973     default:
26974       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26975                 kwd->u.value);
26976       cp_parser_skip_to_end_of_block_or_statement (parser);
26977     }
26978 }
26979
26980 /* Parse an Objective-C try-catch-finally statement.
26981
26982    objc-try-catch-finally-stmt:
26983      @try compound-statement objc-catch-clause-seq [opt]
26984        objc-finally-clause [opt]
26985
26986    objc-catch-clause-seq:
26987      objc-catch-clause objc-catch-clause-seq [opt]
26988
26989    objc-catch-clause:
26990      @catch ( objc-exception-declaration ) compound-statement
26991
26992    objc-finally-clause:
26993      @finally compound-statement
26994
26995    objc-exception-declaration:
26996      parameter-declaration
26997      '...'
26998
26999    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27000
27001    Returns NULL_TREE.
27002
27003    PS: This function is identical to c_parser_objc_try_catch_finally_statement
27004    for C.  Keep them in sync.  */   
27005
27006 static tree
27007 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
27008 {
27009   location_t location;
27010   tree stmt;
27011
27012   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27013   location = cp_lexer_peek_token (parser->lexer)->location;
27014   objc_maybe_warn_exceptions (location);
27015   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27016      node, lest it get absorbed into the surrounding block.  */
27017   stmt = push_stmt_list ();
27018   cp_parser_compound_statement (parser, NULL, false, false);
27019   objc_begin_try_stmt (location, pop_stmt_list (stmt));
27020
27021   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27022     {
27023       cp_parameter_declarator *parm;
27024       tree parameter_declaration = error_mark_node;
27025       bool seen_open_paren = false;
27026
27027       cp_lexer_consume_token (parser->lexer);
27028       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27029         seen_open_paren = true;
27030       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27031         {
27032           /* We have "@catch (...)" (where the '...' are literally
27033              what is in the code).  Skip the '...'.
27034              parameter_declaration is set to NULL_TREE, and
27035              objc_being_catch_clauses() knows that that means
27036              '...'.  */
27037           cp_lexer_consume_token (parser->lexer);
27038           parameter_declaration = NULL_TREE;
27039         }
27040       else
27041         {
27042           /* We have "@catch (NSException *exception)" or something
27043              like that.  Parse the parameter declaration.  */
27044           parm = cp_parser_parameter_declaration (parser, false, NULL);
27045           if (parm == NULL)
27046             parameter_declaration = error_mark_node;
27047           else
27048             parameter_declaration = grokdeclarator (parm->declarator,
27049                                                     &parm->decl_specifiers,
27050                                                     PARM, /*initialized=*/0,
27051                                                     /*attrlist=*/NULL);
27052         }
27053       if (seen_open_paren)
27054         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27055       else
27056         {
27057           /* If there was no open parenthesis, we are recovering from
27058              an error, and we are trying to figure out what mistake
27059              the user has made.  */
27060
27061           /* If there is an immediate closing parenthesis, the user
27062              probably forgot the opening one (ie, they typed "@catch
27063              NSException *e)".  Parse the closing parenthesis and keep
27064              going.  */
27065           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27066             cp_lexer_consume_token (parser->lexer);
27067           
27068           /* If these is no immediate closing parenthesis, the user
27069              probably doesn't know that parenthesis are required at
27070              all (ie, they typed "@catch NSException *e").  So, just
27071              forget about the closing parenthesis and keep going.  */
27072         }
27073       objc_begin_catch_clause (parameter_declaration);
27074       cp_parser_compound_statement (parser, NULL, false, false);
27075       objc_finish_catch_clause ();
27076     }
27077   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27078     {
27079       cp_lexer_consume_token (parser->lexer);
27080       location = cp_lexer_peek_token (parser->lexer)->location;
27081       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27082          node, lest it get absorbed into the surrounding block.  */
27083       stmt = push_stmt_list ();
27084       cp_parser_compound_statement (parser, NULL, false, false);
27085       objc_build_finally_clause (location, pop_stmt_list (stmt));
27086     }
27087
27088   return objc_finish_try_stmt ();
27089 }
27090
27091 /* Parse an Objective-C synchronized statement.
27092
27093    objc-synchronized-stmt:
27094      @synchronized ( expression ) compound-statement
27095
27096    Returns NULL_TREE.  */
27097
27098 static tree
27099 cp_parser_objc_synchronized_statement (cp_parser *parser)
27100 {
27101   location_t location;
27102   tree lock, stmt;
27103
27104   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27105
27106   location = cp_lexer_peek_token (parser->lexer)->location;
27107   objc_maybe_warn_exceptions (location);
27108   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27109   lock = cp_parser_expression (parser);
27110   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27111
27112   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27113      node, lest it get absorbed into the surrounding block.  */
27114   stmt = push_stmt_list ();
27115   cp_parser_compound_statement (parser, NULL, false, false);
27116
27117   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27118 }
27119
27120 /* Parse an Objective-C throw statement.
27121
27122    objc-throw-stmt:
27123      @throw assignment-expression [opt] ;
27124
27125    Returns a constructed '@throw' statement.  */
27126
27127 static tree
27128 cp_parser_objc_throw_statement (cp_parser *parser)
27129 {
27130   tree expr = NULL_TREE;
27131   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27132
27133   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27134
27135   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27136     expr = cp_parser_expression (parser);
27137
27138   cp_parser_consume_semicolon_at_end_of_statement (parser);
27139
27140   return objc_build_throw_stmt (loc, expr);
27141 }
27142
27143 /* Parse an Objective-C statement.  */
27144
27145 static tree
27146 cp_parser_objc_statement (cp_parser * parser)
27147 {
27148   /* Try to figure out what kind of declaration is present.  */
27149   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27150
27151   switch (kwd->keyword)
27152     {
27153     case RID_AT_TRY:
27154       return cp_parser_objc_try_catch_finally_statement (parser);
27155     case RID_AT_SYNCHRONIZED:
27156       return cp_parser_objc_synchronized_statement (parser);
27157     case RID_AT_THROW:
27158       return cp_parser_objc_throw_statement (parser);
27159     default:
27160       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27161                kwd->u.value);
27162       cp_parser_skip_to_end_of_block_or_statement (parser);
27163     }
27164
27165   return error_mark_node;
27166 }
27167
27168 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
27169    look ahead to see if an objc keyword follows the attributes.  This
27170    is to detect the use of prefix attributes on ObjC @interface and 
27171    @protocol.  */
27172
27173 static bool
27174 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27175 {
27176   cp_lexer_save_tokens (parser->lexer);
27177   *attrib = cp_parser_attributes_opt (parser);
27178   gcc_assert (*attrib);
27179   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27180     {
27181       cp_lexer_commit_tokens (parser->lexer);
27182       return true;
27183     }
27184   cp_lexer_rollback_tokens (parser->lexer);
27185   return false;  
27186 }
27187
27188 /* This routine is a minimal replacement for
27189    c_parser_struct_declaration () used when parsing the list of
27190    types/names or ObjC++ properties.  For example, when parsing the
27191    code
27192
27193    @property (readonly) int a, b, c;
27194
27195    this function is responsible for parsing "int a, int b, int c" and
27196    returning the declarations as CHAIN of DECLs.
27197
27198    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
27199    similar parsing.  */
27200 static tree
27201 cp_parser_objc_struct_declaration (cp_parser *parser)
27202 {
27203   tree decls = NULL_TREE;
27204   cp_decl_specifier_seq declspecs;
27205   int decl_class_or_enum_p;
27206   tree prefix_attributes;
27207
27208   cp_parser_decl_specifier_seq (parser,
27209                                 CP_PARSER_FLAGS_NONE,
27210                                 &declspecs,
27211                                 &decl_class_or_enum_p);
27212
27213   if (declspecs.type == error_mark_node)
27214     return error_mark_node;
27215
27216   /* auto, register, static, extern, mutable.  */
27217   if (declspecs.storage_class != sc_none)
27218     {
27219       cp_parser_error (parser, "invalid type for property");
27220       declspecs.storage_class = sc_none;
27221     }
27222   
27223   /* thread_local.  */
27224   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27225     {
27226       cp_parser_error (parser, "invalid type for property");
27227       declspecs.locations[ds_thread] = 0;
27228     }
27229   
27230   /* typedef.  */
27231   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27232     {
27233       cp_parser_error (parser, "invalid type for property");
27234       declspecs.locations[ds_typedef] = 0;
27235     }
27236
27237   prefix_attributes = declspecs.attributes;
27238   declspecs.attributes = NULL_TREE;
27239
27240   /* Keep going until we hit the `;' at the end of the declaration. */
27241   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27242     {
27243       tree attributes, first_attribute, decl;
27244       cp_declarator *declarator;
27245       cp_token *token;
27246
27247       /* Parse the declarator.  */
27248       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27249                                          NULL, NULL, false, false);
27250
27251       /* Look for attributes that apply to the ivar.  */
27252       attributes = cp_parser_attributes_opt (parser);
27253       /* Remember which attributes are prefix attributes and
27254          which are not.  */
27255       first_attribute = attributes;
27256       /* Combine the attributes.  */
27257       attributes = chainon (prefix_attributes, attributes);
27258       
27259       decl = grokfield (declarator, &declspecs,
27260                         NULL_TREE, /*init_const_expr_p=*/false,
27261                         NULL_TREE, attributes);
27262
27263       if (decl == error_mark_node || decl == NULL_TREE)
27264         return error_mark_node;
27265       
27266       /* Reset PREFIX_ATTRIBUTES.  */
27267       while (attributes && TREE_CHAIN (attributes) != first_attribute)
27268         attributes = TREE_CHAIN (attributes);
27269       if (attributes)
27270         TREE_CHAIN (attributes) = NULL_TREE;
27271
27272       DECL_CHAIN (decl) = decls;
27273       decls = decl;
27274
27275       token = cp_lexer_peek_token (parser->lexer);
27276       if (token->type == CPP_COMMA)
27277         {
27278           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
27279           continue;
27280         }
27281       else
27282         break;
27283     }
27284   return decls;
27285 }
27286
27287 /* Parse an Objective-C @property declaration.  The syntax is:
27288
27289    objc-property-declaration:
27290      '@property' objc-property-attributes[opt] struct-declaration ;
27291
27292    objc-property-attributes:
27293     '(' objc-property-attribute-list ')'
27294
27295    objc-property-attribute-list:
27296      objc-property-attribute
27297      objc-property-attribute-list, objc-property-attribute
27298
27299    objc-property-attribute
27300      'getter' = identifier
27301      'setter' = identifier
27302      'readonly'
27303      'readwrite'
27304      'assign'
27305      'retain'
27306      'copy'
27307      'nonatomic'
27308
27309   For example:
27310     @property NSString *name;
27311     @property (readonly) id object;
27312     @property (retain, nonatomic, getter=getTheName) id name;
27313     @property int a, b, c;
27314
27315    PS: This function is identical to
27316    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
27317 static void 
27318 cp_parser_objc_at_property_declaration (cp_parser *parser)
27319 {
27320   /* The following variables hold the attributes of the properties as
27321      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
27322      seen.  When we see an attribute, we set them to 'true' (if they
27323      are boolean properties) or to the identifier (if they have an
27324      argument, ie, for getter and setter).  Note that here we only
27325      parse the list of attributes, check the syntax and accumulate the
27326      attributes that we find.  objc_add_property_declaration() will
27327      then process the information.  */
27328   bool property_assign = false;
27329   bool property_copy = false;
27330   tree property_getter_ident = NULL_TREE;
27331   bool property_nonatomic = false;
27332   bool property_readonly = false;
27333   bool property_readwrite = false;
27334   bool property_retain = false;
27335   tree property_setter_ident = NULL_TREE;
27336
27337   /* 'properties' is the list of properties that we read.  Usually a
27338      single one, but maybe more (eg, in "@property int a, b, c;" there
27339      are three).  */
27340   tree properties;
27341   location_t loc;
27342
27343   loc = cp_lexer_peek_token (parser->lexer)->location;
27344
27345   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
27346
27347   /* Parse the optional attribute list...  */
27348   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27349     {
27350       /* Eat the '('.  */
27351       cp_lexer_consume_token (parser->lexer);
27352
27353       while (true)
27354         {
27355           bool syntax_error = false;
27356           cp_token *token = cp_lexer_peek_token (parser->lexer);
27357           enum rid keyword;
27358
27359           if (token->type != CPP_NAME)
27360             {
27361               cp_parser_error (parser, "expected identifier");
27362               break;
27363             }
27364           keyword = C_RID_CODE (token->u.value);
27365           cp_lexer_consume_token (parser->lexer);
27366           switch (keyword)
27367             {
27368             case RID_ASSIGN:    property_assign = true;    break;
27369             case RID_COPY:      property_copy = true;      break;
27370             case RID_NONATOMIC: property_nonatomic = true; break;
27371             case RID_READONLY:  property_readonly = true;  break;
27372             case RID_READWRITE: property_readwrite = true; break;
27373             case RID_RETAIN:    property_retain = true;    break;
27374
27375             case RID_GETTER:
27376             case RID_SETTER:
27377               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27378                 {
27379                   if (keyword == RID_GETTER)
27380                     cp_parser_error (parser,
27381                                      "missing %<=%> (after %<getter%> attribute)");
27382                   else
27383                     cp_parser_error (parser,
27384                                      "missing %<=%> (after %<setter%> attribute)");
27385                   syntax_error = true;
27386                   break;
27387                 }
27388               cp_lexer_consume_token (parser->lexer); /* eat the = */
27389               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27390                 {
27391                   cp_parser_error (parser, "expected identifier");
27392                   syntax_error = true;
27393                   break;
27394                 }
27395               if (keyword == RID_SETTER)
27396                 {
27397                   if (property_setter_ident != NULL_TREE)
27398                     {
27399                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27400                       cp_lexer_consume_token (parser->lexer);
27401                     }
27402                   else
27403                     property_setter_ident = cp_parser_objc_selector (parser);
27404                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27405                     cp_parser_error (parser, "setter name must terminate with %<:%>");
27406                   else
27407                     cp_lexer_consume_token (parser->lexer);
27408                 }
27409               else
27410                 {
27411                   if (property_getter_ident != NULL_TREE)
27412                     {
27413                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27414                       cp_lexer_consume_token (parser->lexer);
27415                     }
27416                   else
27417                     property_getter_ident = cp_parser_objc_selector (parser);
27418                 }
27419               break;
27420             default:
27421               cp_parser_error (parser, "unknown property attribute");
27422               syntax_error = true;
27423               break;
27424             }
27425
27426           if (syntax_error)
27427             break;
27428
27429           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27430             cp_lexer_consume_token (parser->lexer);
27431           else
27432             break;
27433         }
27434
27435       /* FIXME: "@property (setter, assign);" will generate a spurious
27436          "error: expected â€˜)’ before â€˜,’ token".  This is because
27437          cp_parser_require, unlike the C counterpart, will produce an
27438          error even if we are in error recovery.  */
27439       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27440         {
27441           cp_parser_skip_to_closing_parenthesis (parser,
27442                                                  /*recovering=*/true,
27443                                                  /*or_comma=*/false,
27444                                                  /*consume_paren=*/true);
27445         }
27446     }
27447
27448   /* ... and the property declaration(s).  */
27449   properties = cp_parser_objc_struct_declaration (parser);
27450
27451   if (properties == error_mark_node)
27452     {
27453       cp_parser_skip_to_end_of_statement (parser);
27454       /* If the next token is now a `;', consume it.  */
27455       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27456         cp_lexer_consume_token (parser->lexer);
27457       return;
27458     }
27459
27460   if (properties == NULL_TREE)
27461     cp_parser_error (parser, "expected identifier");
27462   else
27463     {
27464       /* Comma-separated properties are chained together in
27465          reverse order; add them one by one.  */
27466       properties = nreverse (properties);
27467       
27468       for (; properties; properties = TREE_CHAIN (properties))
27469         objc_add_property_declaration (loc, copy_node (properties),
27470                                        property_readonly, property_readwrite,
27471                                        property_assign, property_retain,
27472                                        property_copy, property_nonatomic,
27473                                        property_getter_ident, property_setter_ident);
27474     }
27475   
27476   cp_parser_consume_semicolon_at_end_of_statement (parser);
27477 }
27478
27479 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
27480
27481    objc-synthesize-declaration:
27482      @synthesize objc-synthesize-identifier-list ;
27483
27484    objc-synthesize-identifier-list:
27485      objc-synthesize-identifier
27486      objc-synthesize-identifier-list, objc-synthesize-identifier
27487
27488    objc-synthesize-identifier
27489      identifier
27490      identifier = identifier
27491
27492   For example:
27493     @synthesize MyProperty;
27494     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27495
27496   PS: This function is identical to c_parser_objc_at_synthesize_declaration
27497   for C.  Keep them in sync.
27498 */
27499 static void 
27500 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27501 {
27502   tree list = NULL_TREE;
27503   location_t loc;
27504   loc = cp_lexer_peek_token (parser->lexer)->location;
27505
27506   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
27507   while (true)
27508     {
27509       tree property, ivar;
27510       property = cp_parser_identifier (parser);
27511       if (property == error_mark_node)
27512         {
27513           cp_parser_consume_semicolon_at_end_of_statement (parser);
27514           return;
27515         }
27516       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27517         {
27518           cp_lexer_consume_token (parser->lexer);
27519           ivar = cp_parser_identifier (parser);
27520           if (ivar == error_mark_node)
27521             {
27522               cp_parser_consume_semicolon_at_end_of_statement (parser);
27523               return;
27524             }
27525         }
27526       else
27527         ivar = NULL_TREE;
27528       list = chainon (list, build_tree_list (ivar, property));
27529       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27530         cp_lexer_consume_token (parser->lexer);
27531       else
27532         break;
27533     }
27534   cp_parser_consume_semicolon_at_end_of_statement (parser);
27535   objc_add_synthesize_declaration (loc, list);
27536 }
27537
27538 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
27539
27540    objc-dynamic-declaration:
27541      @dynamic identifier-list ;
27542
27543    For example:
27544      @dynamic MyProperty;
27545      @dynamic MyProperty, AnotherProperty;
27546
27547   PS: This function is identical to c_parser_objc_at_dynamic_declaration
27548   for C.  Keep them in sync.
27549 */
27550 static void 
27551 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27552 {
27553   tree list = NULL_TREE;
27554   location_t loc;
27555   loc = cp_lexer_peek_token (parser->lexer)->location;
27556
27557   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
27558   while (true)
27559     {
27560       tree property;
27561       property = cp_parser_identifier (parser);
27562       if (property == error_mark_node)
27563         {
27564           cp_parser_consume_semicolon_at_end_of_statement (parser);
27565           return;
27566         }
27567       list = chainon (list, build_tree_list (NULL, property));
27568       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27569         cp_lexer_consume_token (parser->lexer);
27570       else
27571         break;
27572     }
27573   cp_parser_consume_semicolon_at_end_of_statement (parser);
27574   objc_add_dynamic_declaration (loc, list);
27575 }
27576
27577 \f
27578 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
27579
27580 /* Returns name of the next clause.
27581    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27582    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
27583    returned and the token is consumed.  */
27584
27585 static pragma_omp_clause
27586 cp_parser_omp_clause_name (cp_parser *parser)
27587 {
27588   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27589
27590   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27591     result = PRAGMA_OMP_CLAUSE_IF;
27592   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27593     result = PRAGMA_OMP_CLAUSE_DEFAULT;
27594   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27595     result = PRAGMA_OACC_CLAUSE_DELETE;
27596   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27597     result = PRAGMA_OMP_CLAUSE_PRIVATE;
27598   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27599     result = PRAGMA_OMP_CLAUSE_FOR;
27600   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27601     {
27602       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27603       const char *p = IDENTIFIER_POINTER (id);
27604
27605       switch (p[0])
27606         {
27607         case 'a':
27608           if (!strcmp ("aligned", p))
27609             result = PRAGMA_OMP_CLAUSE_ALIGNED;
27610           else if (!strcmp ("async", p))
27611             result = PRAGMA_OACC_CLAUSE_ASYNC;
27612           break;
27613         case 'c':
27614           if (!strcmp ("collapse", p))
27615             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27616           else if (!strcmp ("copy", p))
27617             result = PRAGMA_OACC_CLAUSE_COPY;
27618           else if (!strcmp ("copyin", p))
27619             result = PRAGMA_OMP_CLAUSE_COPYIN;
27620           else if (!strcmp ("copyout", p))
27621             result = PRAGMA_OACC_CLAUSE_COPYOUT;
27622           else if (!strcmp ("copyprivate", p))
27623             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27624           else if (!strcmp ("create", p))
27625             result = PRAGMA_OACC_CLAUSE_CREATE;
27626           break;
27627         case 'd':
27628           if (!strcmp ("depend", p))
27629             result = PRAGMA_OMP_CLAUSE_DEPEND;
27630           else if (!strcmp ("device", p))
27631             result = PRAGMA_OMP_CLAUSE_DEVICE;
27632           else if (!strcmp ("deviceptr", p))
27633             result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27634           else if (!strcmp ("dist_schedule", p))
27635             result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27636           break;
27637         case 'f':
27638           if (!strcmp ("final", p))
27639             result = PRAGMA_OMP_CLAUSE_FINAL;
27640           else if (!strcmp ("firstprivate", p))
27641             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27642           else if (!strcmp ("from", p))
27643             result = PRAGMA_OMP_CLAUSE_FROM;
27644           break;
27645         case 'h':
27646           if (!strcmp ("host", p))
27647             result = PRAGMA_OACC_CLAUSE_HOST;
27648           break;
27649         case 'i':
27650           if (!strcmp ("inbranch", p))
27651             result = PRAGMA_OMP_CLAUSE_INBRANCH;
27652           break;
27653         case 'l':
27654           if (!strcmp ("lastprivate", p))
27655             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27656           else if (!strcmp ("linear", p))
27657             result = PRAGMA_OMP_CLAUSE_LINEAR;
27658           break;
27659         case 'm':
27660           if (!strcmp ("map", p))
27661             result = PRAGMA_OMP_CLAUSE_MAP;
27662           else if (!strcmp ("mergeable", p))
27663             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27664           else if (flag_cilkplus && !strcmp ("mask", p))
27665             result = PRAGMA_CILK_CLAUSE_MASK;
27666           break;
27667         case 'n':
27668           if (!strcmp ("notinbranch", p))
27669             result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27670           else if (!strcmp ("nowait", p))
27671             result = PRAGMA_OMP_CLAUSE_NOWAIT;
27672           else if (flag_cilkplus && !strcmp ("nomask", p))
27673             result = PRAGMA_CILK_CLAUSE_NOMASK;
27674           else if (!strcmp ("num_gangs", p))
27675             result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27676           else if (!strcmp ("num_teams", p))
27677             result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27678           else if (!strcmp ("num_threads", p))
27679             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27680           else if (!strcmp ("num_workers", p))
27681             result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27682           break;
27683         case 'o':
27684           if (!strcmp ("ordered", p))
27685             result = PRAGMA_OMP_CLAUSE_ORDERED;
27686           break;
27687         case 'p':
27688           if (!strcmp ("parallel", p))
27689             result = PRAGMA_OMP_CLAUSE_PARALLEL;
27690           else if (!strcmp ("present", p))
27691             result = PRAGMA_OACC_CLAUSE_PRESENT;
27692           else if (!strcmp ("present_or_copy", p)
27693                    || !strcmp ("pcopy", p))
27694             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27695           else if (!strcmp ("present_or_copyin", p)
27696                    || !strcmp ("pcopyin", p))
27697             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27698           else if (!strcmp ("present_or_copyout", p)
27699                    || !strcmp ("pcopyout", p))
27700             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27701           else if (!strcmp ("present_or_create", p)
27702                    || !strcmp ("pcreate", p))
27703             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27704           else if (!strcmp ("proc_bind", p))
27705             result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27706           break;
27707         case 'r':
27708           if (!strcmp ("reduction", p))
27709             result = PRAGMA_OMP_CLAUSE_REDUCTION;
27710           break;
27711         case 's':
27712           if (!strcmp ("safelen", p))
27713             result = PRAGMA_OMP_CLAUSE_SAFELEN;
27714           else if (!strcmp ("schedule", p))
27715             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27716           else if (!strcmp ("sections", p))
27717             result = PRAGMA_OMP_CLAUSE_SECTIONS;
27718           else if (!strcmp ("self", p))
27719             result = PRAGMA_OACC_CLAUSE_SELF;
27720           else if (!strcmp ("shared", p))
27721             result = PRAGMA_OMP_CLAUSE_SHARED;
27722           else if (!strcmp ("simdlen", p))
27723             result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27724           break;
27725         case 't':
27726           if (!strcmp ("taskgroup", p))
27727             result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27728           else if (!strcmp ("thread_limit", p))
27729             result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27730           else if (!strcmp ("to", p))
27731             result = PRAGMA_OMP_CLAUSE_TO;
27732           break;
27733         case 'u':
27734           if (!strcmp ("uniform", p))
27735             result = PRAGMA_OMP_CLAUSE_UNIFORM;
27736           else if (!strcmp ("untied", p))
27737             result = PRAGMA_OMP_CLAUSE_UNTIED;
27738           break;
27739         case 'v':
27740           if (!strcmp ("vector_length", p))
27741             result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27742           else if (flag_cilkplus && !strcmp ("vectorlength", p))
27743             result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27744           break;
27745         case 'w':
27746           if (!strcmp ("wait", p))
27747             result = PRAGMA_OACC_CLAUSE_WAIT;
27748           break;
27749         }
27750     }
27751
27752   if (result != PRAGMA_OMP_CLAUSE_NONE)
27753     cp_lexer_consume_token (parser->lexer);
27754
27755   return result;
27756 }
27757
27758 /* Validate that a clause of the given type does not already exist.  */
27759
27760 static void
27761 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27762                            const char *name, location_t location)
27763 {
27764   tree c;
27765
27766   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27767     if (OMP_CLAUSE_CODE (c) == code)
27768       {
27769         error_at (location, "too many %qs clauses", name);
27770         break;
27771       }
27772 }
27773
27774 /* OpenMP 2.5:
27775    variable-list:
27776      identifier
27777      variable-list , identifier
27778
27779    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27780    colon).  An opening parenthesis will have been consumed by the caller.
27781
27782    If KIND is nonzero, create the appropriate node and install the decl
27783    in OMP_CLAUSE_DECL and add the node to the head of the list.
27784
27785    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27786    return the list created.
27787
27788    COLON can be NULL if only closing parenthesis should end the list,
27789    or pointer to bool which will receive false if the list is terminated
27790    by closing parenthesis or true if the list is terminated by colon.  */
27791
27792 static tree
27793 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27794                                 tree list, bool *colon)
27795 {
27796   cp_token *token;
27797   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27798   if (colon)
27799     {
27800       parser->colon_corrects_to_scope_p = false;
27801       *colon = false;
27802     }
27803   while (1)
27804     {
27805       tree name, decl;
27806
27807       token = cp_lexer_peek_token (parser->lexer);
27808       name = cp_parser_id_expression (parser, /*template_p=*/false,
27809                                       /*check_dependency_p=*/true,
27810                                       /*template_p=*/NULL,
27811                                       /*declarator_p=*/false,
27812                                       /*optional_p=*/false);
27813       if (name == error_mark_node)
27814         goto skip_comma;
27815
27816       decl = cp_parser_lookup_name_simple (parser, name, token->location);
27817       if (decl == error_mark_node)
27818         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27819                                      token->location);
27820       else if (kind != 0)
27821         {
27822           switch (kind)
27823             {
27824             case OMP_CLAUSE__CACHE_:
27825               if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27826                 {
27827                   error_at (token->location, "expected %<[%>");
27828                   decl = error_mark_node;
27829                   break;
27830                 }
27831               /* FALL THROUGH.  */
27832             case OMP_CLAUSE_MAP:
27833             case OMP_CLAUSE_FROM:
27834             case OMP_CLAUSE_TO:
27835             case OMP_CLAUSE_DEPEND:
27836               while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27837                 {
27838                   tree low_bound = NULL_TREE, length = NULL_TREE;
27839
27840                   parser->colon_corrects_to_scope_p = false;
27841                   cp_lexer_consume_token (parser->lexer);
27842                   if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27843                     low_bound = cp_parser_expression (parser);
27844                   if (!colon)
27845                     parser->colon_corrects_to_scope_p
27846                       = saved_colon_corrects_to_scope_p;
27847                   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27848                     length = integer_one_node;
27849                   else
27850                     {
27851                       /* Look for `:'.  */
27852                       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27853                         goto skip_comma;
27854                       if (!cp_lexer_next_token_is (parser->lexer,
27855                                                    CPP_CLOSE_SQUARE))
27856                         length = cp_parser_expression (parser);
27857                     }
27858                   /* Look for the closing `]'.  */
27859                   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27860                                           RT_CLOSE_SQUARE))
27861                     goto skip_comma;
27862
27863                   if (kind == OMP_CLAUSE__CACHE_)
27864                     {
27865                       if (TREE_CODE (low_bound) != INTEGER_CST
27866                           && !TREE_READONLY (low_bound))
27867                         {
27868                           error_at (token->location,
27869                                         "%qD is not a constant", low_bound);
27870                           decl = error_mark_node;
27871                         }
27872
27873                       if (TREE_CODE (length) != INTEGER_CST
27874                           && !TREE_READONLY (length))
27875                         {
27876                           error_at (token->location,
27877                                         "%qD is not a constant", length);
27878                           decl = error_mark_node;
27879                         }
27880                     }
27881
27882                   decl = tree_cons (low_bound, length, decl);
27883                 }
27884               break;
27885             default:
27886               break;
27887             }
27888
27889           tree u = build_omp_clause (token->location, kind);
27890           OMP_CLAUSE_DECL (u) = decl;
27891           OMP_CLAUSE_CHAIN (u) = list;
27892           list = u;
27893         }
27894       else
27895         list = tree_cons (decl, NULL_TREE, list);
27896
27897     get_comma:
27898       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27899         break;
27900       cp_lexer_consume_token (parser->lexer);
27901     }
27902
27903   if (colon)
27904     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27905
27906   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27907     {
27908       *colon = true;
27909       cp_parser_require (parser, CPP_COLON, RT_COLON);
27910       return list;
27911     }
27912
27913   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27914     {
27915       int ending;
27916
27917       /* Try to resync to an unnested comma.  Copied from
27918          cp_parser_parenthesized_expression_list.  */
27919     skip_comma:
27920       if (colon)
27921         parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27922       ending = cp_parser_skip_to_closing_parenthesis (parser,
27923                                                       /*recovering=*/true,
27924                                                       /*or_comma=*/true,
27925                                                       /*consume_paren=*/true);
27926       if (ending < 0)
27927         goto get_comma;
27928     }
27929
27930   return list;
27931 }
27932
27933 /* Similarly, but expect leading and trailing parenthesis.  This is a very
27934    common case for omp clauses.  */
27935
27936 static tree
27937 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27938 {
27939   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27940     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27941   return list;
27942 }
27943
27944 /* OpenACC 2.0:
27945    copy ( variable-list )
27946    copyin ( variable-list )
27947    copyout ( variable-list )
27948    create ( variable-list )
27949    delete ( variable-list )
27950    present ( variable-list )
27951    present_or_copy ( variable-list )
27952      pcopy ( variable-list )
27953    present_or_copyin ( variable-list )
27954      pcopyin ( variable-list )
27955    present_or_copyout ( variable-list )
27956      pcopyout ( variable-list )
27957    present_or_create ( variable-list )
27958      pcreate ( variable-list ) */
27959
27960 static tree
27961 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27962                             tree list)
27963 {
27964   enum gomp_map_kind kind;
27965   switch (c_kind)
27966     {
27967     case PRAGMA_OACC_CLAUSE_COPY:
27968       kind = GOMP_MAP_FORCE_TOFROM;
27969       break;
27970     case PRAGMA_OACC_CLAUSE_COPYIN:
27971       kind = GOMP_MAP_FORCE_TO;
27972       break;
27973     case PRAGMA_OACC_CLAUSE_COPYOUT:
27974       kind = GOMP_MAP_FORCE_FROM;
27975       break;
27976     case PRAGMA_OACC_CLAUSE_CREATE:
27977       kind = GOMP_MAP_FORCE_ALLOC;
27978       break;
27979     case PRAGMA_OACC_CLAUSE_DELETE:
27980       kind = GOMP_MAP_FORCE_DEALLOC;
27981       break;
27982     case PRAGMA_OACC_CLAUSE_DEVICE:
27983       kind = GOMP_MAP_FORCE_TO;
27984       break;
27985     case PRAGMA_OACC_CLAUSE_HOST:
27986     case PRAGMA_OACC_CLAUSE_SELF:
27987       kind = GOMP_MAP_FORCE_FROM;
27988       break;
27989     case PRAGMA_OACC_CLAUSE_PRESENT:
27990       kind = GOMP_MAP_FORCE_PRESENT;
27991       break;
27992     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
27993       kind = GOMP_MAP_TOFROM;
27994       break;
27995     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
27996       kind = GOMP_MAP_TO;
27997       break;
27998     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
27999       kind = GOMP_MAP_FROM;
28000       break;
28001     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
28002       kind = GOMP_MAP_ALLOC;
28003       break;
28004     default:
28005       gcc_unreachable ();
28006     }
28007   tree nl, c;
28008   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
28009
28010   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
28011     OMP_CLAUSE_SET_MAP_KIND (c, kind);
28012
28013   return nl;
28014 }
28015
28016 /* OpenACC 2.0:
28017    deviceptr ( variable-list ) */
28018
28019 static tree
28020 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28021 {
28022   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28023   tree vars, t;
28024
28025   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28026      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28027      variable-list must only allow for pointer variables.  */
28028   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28029   for (t = vars; t; t = TREE_CHAIN (t))
28030     {
28031       tree v = TREE_PURPOSE (t);
28032
28033       /* FIXME diagnostics: Ideally we should keep individual
28034          locations for all the variables in the var list to make the
28035          following errors more precise.  Perhaps
28036          c_parser_omp_var_list_parens should construct a list of
28037          locations to go along with the var list.  */
28038
28039       if (TREE_CODE (v) != VAR_DECL)
28040         error_at (loc, "%qD is not a variable", v);
28041       else if (TREE_TYPE (v) == error_mark_node)
28042         ;
28043       else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28044         error_at (loc, "%qD is not a pointer variable", v);
28045
28046       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28047       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28048       OMP_CLAUSE_DECL (u) = v;
28049       OMP_CLAUSE_CHAIN (u) = list;
28050       list = u;
28051     }
28052
28053   return list;
28054 }
28055
28056 /* OpenACC:
28057    vector_length ( expression ) */
28058
28059 static tree
28060 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28061 {
28062   tree t, c;
28063   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28064   bool error = false;
28065
28066   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28067     return list;
28068
28069   t = cp_parser_condition (parser);
28070   if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28071     {
28072       error_at (location, "expected positive integer expression");
28073       error = true;
28074     }
28075
28076   if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28077     {
28078       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28079                                            /*or_comma=*/false,
28080                                            /*consume_paren=*/true);
28081       return list;
28082     }
28083
28084   check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28085                              location);
28086
28087   c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28088   OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28089   OMP_CLAUSE_CHAIN (c) = list;
28090   list = c;
28091
28092   return list;
28093 }
28094
28095 /* OpenACC 2.0
28096    Parse wait clause or directive parameters.  */
28097
28098 static tree
28099 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28100 {
28101   vec<tree, va_gc> *args;
28102   tree t, args_tree;
28103
28104   args = cp_parser_parenthesized_expression_list (parser, non_attr,
28105                                                   /*cast_p=*/false,
28106                                                   /*allow_expansion_p=*/true,
28107                                                   /*non_constant_p=*/NULL);
28108
28109   if (args == NULL || args->length () == 0)
28110     {
28111       cp_parser_error (parser, "expected integer expression before ')'");
28112       if (args != NULL)
28113         release_tree_vector (args);
28114       return list;
28115     }
28116
28117   args_tree = build_tree_list_vec (args);
28118
28119   release_tree_vector (args);
28120
28121   for (t = args_tree; t; t = TREE_CHAIN (t))
28122     {
28123       tree targ = TREE_VALUE (t);
28124
28125       if (targ != error_mark_node)
28126         {
28127           if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28128             error ("%<wait%> expression must be integral");
28129           else
28130             {
28131               tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28132
28133               mark_rvalue_use (targ);
28134               OMP_CLAUSE_DECL (c) = targ;
28135               OMP_CLAUSE_CHAIN (c) = list;
28136               list = c;
28137             }
28138         }
28139     }
28140
28141   return list;
28142 }
28143
28144 /* OpenACC:
28145    wait ( int-expr-list ) */
28146
28147 static tree
28148 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28149 {
28150   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28151
28152   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28153     return list;
28154
28155   list = cp_parser_oacc_wait_list (parser, location, list);
28156
28157   return list;
28158 }
28159
28160 /* OpenMP 3.0:
28161    collapse ( constant-expression ) */
28162
28163 static tree
28164 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28165 {
28166   tree c, num;
28167   location_t loc;
28168   HOST_WIDE_INT n;
28169
28170   loc = cp_lexer_peek_token (parser->lexer)->location;
28171   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28172     return list;
28173
28174   num = cp_parser_constant_expression (parser);
28175
28176   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28177     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28178                                            /*or_comma=*/false,
28179                                            /*consume_paren=*/true);
28180
28181   if (num == error_mark_node)
28182     return list;
28183   num = fold_non_dependent_expr (num);
28184   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28185       || !tree_fits_shwi_p (num)
28186       || (n = tree_to_shwi (num)) <= 0
28187       || (int) n != n)
28188     {
28189       error_at (loc, "collapse argument needs positive constant integer expression");
28190       return list;
28191     }
28192
28193   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28194   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28195   OMP_CLAUSE_CHAIN (c) = list;
28196   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28197
28198   return c;
28199 }
28200
28201 /* OpenMP 2.5:
28202    default ( shared | none ) */
28203
28204 static tree
28205 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28206 {
28207   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28208   tree c;
28209
28210   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28211     return list;
28212   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28213     {
28214       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28215       const char *p = IDENTIFIER_POINTER (id);
28216
28217       switch (p[0])
28218         {
28219         case 'n':
28220           if (strcmp ("none", p) != 0)
28221             goto invalid_kind;
28222           kind = OMP_CLAUSE_DEFAULT_NONE;
28223           break;
28224
28225         case 's':
28226           if (strcmp ("shared", p) != 0)
28227             goto invalid_kind;
28228           kind = OMP_CLAUSE_DEFAULT_SHARED;
28229           break;
28230
28231         default:
28232           goto invalid_kind;
28233         }
28234
28235       cp_lexer_consume_token (parser->lexer);
28236     }
28237   else
28238     {
28239     invalid_kind:
28240       cp_parser_error (parser, "expected %<none%> or %<shared%>");
28241     }
28242
28243   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28244     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28245                                            /*or_comma=*/false,
28246                                            /*consume_paren=*/true);
28247
28248   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28249     return list;
28250
28251   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28252   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28253   OMP_CLAUSE_CHAIN (c) = list;
28254   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28255
28256   return c;
28257 }
28258
28259 /* OpenMP 3.1:
28260    final ( expression ) */
28261
28262 static tree
28263 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28264 {
28265   tree t, c;
28266
28267   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28268     return list;
28269
28270   t = cp_parser_condition (parser);
28271
28272   if (t == error_mark_node
28273       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28274     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28275                                            /*or_comma=*/false,
28276                                            /*consume_paren=*/true);
28277
28278   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28279
28280   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28281   OMP_CLAUSE_FINAL_EXPR (c) = t;
28282   OMP_CLAUSE_CHAIN (c) = list;
28283
28284   return c;
28285 }
28286
28287 /* OpenMP 2.5:
28288    if ( expression ) */
28289
28290 static tree
28291 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28292 {
28293   tree t, c;
28294
28295   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28296     return list;
28297
28298   t = cp_parser_condition (parser);
28299
28300   if (t == error_mark_node
28301       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28302     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28303                                            /*or_comma=*/false,
28304                                            /*consume_paren=*/true);
28305
28306   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28307
28308   c = build_omp_clause (location, OMP_CLAUSE_IF);
28309   OMP_CLAUSE_IF_EXPR (c) = t;
28310   OMP_CLAUSE_CHAIN (c) = list;
28311
28312   return c;
28313 }
28314
28315 /* OpenMP 3.1:
28316    mergeable */
28317
28318 static tree
28319 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28320                                 tree list, location_t location)
28321 {
28322   tree c;
28323
28324   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28325                              location);
28326
28327   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28328   OMP_CLAUSE_CHAIN (c) = list;
28329   return c;
28330 }
28331
28332 /* OpenMP 2.5:
28333    nowait */
28334
28335 static tree
28336 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28337                              tree list, location_t location)
28338 {
28339   tree c;
28340
28341   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28342
28343   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28344   OMP_CLAUSE_CHAIN (c) = list;
28345   return c;
28346 }
28347
28348 /* OpenACC:
28349    num_gangs ( expression ) */
28350
28351 static tree
28352 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28353 {
28354   tree t, c;
28355   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28356
28357   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28358     return list;
28359
28360   t = cp_parser_condition (parser);
28361
28362   if (t == error_mark_node
28363       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28364     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28365                                            /*or_comma=*/false,
28366                                            /*consume_paren=*/true);
28367
28368   if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28369     {
28370       error_at (location, "expected positive integer expression");
28371       return list;
28372     }
28373
28374   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28375
28376   c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28377   OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28378   OMP_CLAUSE_CHAIN (c) = list;
28379   list = c;
28380
28381   return list;
28382 }
28383
28384 /* OpenMP 2.5:
28385    num_threads ( expression ) */
28386
28387 static tree
28388 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28389                                   location_t location)
28390 {
28391   tree t, c;
28392
28393   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28394     return list;
28395
28396   t = cp_parser_expression (parser);
28397
28398   if (t == error_mark_node
28399       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28400     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28401                                            /*or_comma=*/false,
28402                                            /*consume_paren=*/true);
28403
28404   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28405                              "num_threads", location);
28406
28407   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28408   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28409   OMP_CLAUSE_CHAIN (c) = list;
28410
28411   return c;
28412 }
28413
28414 /* OpenACC:
28415    num_workers ( expression ) */
28416
28417 static tree
28418 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28419 {
28420   tree t, c;
28421   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28422
28423   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28424     return list;
28425
28426   t = cp_parser_condition (parser);
28427
28428   if (t == error_mark_node
28429       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28430     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28431                                            /*or_comma=*/false,
28432                                            /*consume_paren=*/true);
28433
28434   if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28435     {
28436       error_at (location, "expected positive integer expression");
28437       return list;
28438     }
28439
28440   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28441                                                                 location);
28442
28443   c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28444   OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28445   OMP_CLAUSE_CHAIN (c) = list;
28446   list = c;
28447
28448   return list;
28449 }
28450
28451 /* OpenMP 2.5:
28452    ordered */
28453
28454 static tree
28455 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28456                               tree list, location_t location)
28457 {
28458   tree c;
28459
28460   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28461                              "ordered", location);
28462
28463   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28464   OMP_CLAUSE_CHAIN (c) = list;
28465   return c;
28466 }
28467
28468 /* OpenMP 2.5:
28469    reduction ( reduction-operator : variable-list )
28470
28471    reduction-operator:
28472      One of: + * - & ^ | && ||
28473
28474    OpenMP 3.1:
28475
28476    reduction-operator:
28477      One of: + * - & ^ | && || min max
28478
28479    OpenMP 4.0:
28480
28481    reduction-operator:
28482      One of: + * - & ^ | && ||
28483      id-expression  */
28484
28485 static tree
28486 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28487 {
28488   enum tree_code code = ERROR_MARK;
28489   tree nlist, c, id = NULL_TREE;
28490
28491   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28492     return list;
28493
28494   switch (cp_lexer_peek_token (parser->lexer)->type)
28495     {
28496     case CPP_PLUS: code = PLUS_EXPR; break;
28497     case CPP_MULT: code = MULT_EXPR; break;
28498     case CPP_MINUS: code = MINUS_EXPR; break;
28499     case CPP_AND: code = BIT_AND_EXPR; break;
28500     case CPP_XOR: code = BIT_XOR_EXPR; break;
28501     case CPP_OR: code = BIT_IOR_EXPR; break;
28502     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28503     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28504     default: break;
28505     }
28506
28507   if (code != ERROR_MARK)
28508     cp_lexer_consume_token (parser->lexer);
28509   else
28510     {
28511       bool saved_colon_corrects_to_scope_p;
28512       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28513       parser->colon_corrects_to_scope_p = false;
28514       id = cp_parser_id_expression (parser, /*template_p=*/false,
28515                                     /*check_dependency_p=*/true,
28516                                     /*template_p=*/NULL,
28517                                     /*declarator_p=*/false,
28518                                     /*optional_p=*/false);
28519       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28520       if (identifier_p (id))
28521         {
28522           const char *p = IDENTIFIER_POINTER (id);
28523
28524           if (strcmp (p, "min") == 0)
28525             code = MIN_EXPR;
28526           else if (strcmp (p, "max") == 0)
28527             code = MAX_EXPR;
28528           else if (id == ansi_opname (PLUS_EXPR))
28529             code = PLUS_EXPR;
28530           else if (id == ansi_opname (MULT_EXPR))
28531             code = MULT_EXPR;
28532           else if (id == ansi_opname (MINUS_EXPR))
28533             code = MINUS_EXPR;
28534           else if (id == ansi_opname (BIT_AND_EXPR))
28535             code = BIT_AND_EXPR;
28536           else if (id == ansi_opname (BIT_IOR_EXPR))
28537             code = BIT_IOR_EXPR;
28538           else if (id == ansi_opname (BIT_XOR_EXPR))
28539             code = BIT_XOR_EXPR;
28540           else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28541             code = TRUTH_ANDIF_EXPR;
28542           else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28543             code = TRUTH_ORIF_EXPR;
28544           id = omp_reduction_id (code, id, NULL_TREE);
28545           tree scope = parser->scope;
28546           if (scope)
28547             id = build_qualified_name (NULL_TREE, scope, id, false);
28548           parser->scope = NULL_TREE;
28549           parser->qualifying_scope = NULL_TREE;
28550           parser->object_scope = NULL_TREE;
28551         }
28552       else
28553         {
28554           error ("invalid reduction-identifier");
28555          resync_fail:
28556           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28557                                                  /*or_comma=*/false,
28558                                                  /*consume_paren=*/true);
28559           return list;
28560         }
28561     }
28562
28563   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28564     goto resync_fail;
28565
28566   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28567                                           NULL);
28568   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28569     {
28570       OMP_CLAUSE_REDUCTION_CODE (c) = code;
28571       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28572     }
28573
28574   return nlist;
28575 }
28576
28577 /* OpenMP 2.5:
28578    schedule ( schedule-kind )
28579    schedule ( schedule-kind , expression )
28580
28581    schedule-kind:
28582      static | dynamic | guided | runtime | auto  */
28583
28584 static tree
28585 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28586 {
28587   tree c, t;
28588
28589   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28590     return list;
28591
28592   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28593
28594   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28595     {
28596       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28597       const char *p = IDENTIFIER_POINTER (id);
28598
28599       switch (p[0])
28600         {
28601         case 'd':
28602           if (strcmp ("dynamic", p) != 0)
28603             goto invalid_kind;
28604           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28605           break;
28606
28607         case 'g':
28608           if (strcmp ("guided", p) != 0)
28609             goto invalid_kind;
28610           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28611           break;
28612
28613         case 'r':
28614           if (strcmp ("runtime", p) != 0)
28615             goto invalid_kind;
28616           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28617           break;
28618
28619         default:
28620           goto invalid_kind;
28621         }
28622     }
28623   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28624     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28625   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28626     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28627   else
28628     goto invalid_kind;
28629   cp_lexer_consume_token (parser->lexer);
28630
28631   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28632     {
28633       cp_token *token;
28634       cp_lexer_consume_token (parser->lexer);
28635
28636       token = cp_lexer_peek_token (parser->lexer);
28637       t = cp_parser_assignment_expression (parser);
28638
28639       if (t == error_mark_node)
28640         goto resync_fail;
28641       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28642         error_at (token->location, "schedule %<runtime%> does not take "
28643                   "a %<chunk_size%> parameter");
28644       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28645         error_at (token->location, "schedule %<auto%> does not take "
28646                   "a %<chunk_size%> parameter");
28647       else
28648         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28649
28650       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28651         goto resync_fail;
28652     }
28653   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28654     goto resync_fail;
28655
28656   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28657   OMP_CLAUSE_CHAIN (c) = list;
28658   return c;
28659
28660  invalid_kind:
28661   cp_parser_error (parser, "invalid schedule kind");
28662  resync_fail:
28663   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28664                                          /*or_comma=*/false,
28665                                          /*consume_paren=*/true);
28666   return list;
28667 }
28668
28669 /* OpenMP 3.0:
28670    untied */
28671
28672 static tree
28673 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28674                              tree list, location_t location)
28675 {
28676   tree c;
28677
28678   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28679
28680   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28681   OMP_CLAUSE_CHAIN (c) = list;
28682   return c;
28683 }
28684
28685 /* OpenMP 4.0:
28686    inbranch
28687    notinbranch */
28688
28689 static tree
28690 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28691                              tree list, location_t location)
28692 {
28693   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28694   tree c = build_omp_clause (location, code);
28695   OMP_CLAUSE_CHAIN (c) = list;
28696   return c;
28697 }
28698
28699 /* OpenMP 4.0:
28700    parallel
28701    for
28702    sections
28703    taskgroup */
28704
28705 static tree
28706 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28707                                  enum omp_clause_code code,
28708                                  tree list, location_t location)
28709 {
28710   tree c = build_omp_clause (location, code);
28711   OMP_CLAUSE_CHAIN (c) = list;
28712   return c;
28713 }
28714
28715 /* OpenMP 4.0:
28716    num_teams ( expression ) */
28717
28718 static tree
28719 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28720                                 location_t location)
28721 {
28722   tree t, c;
28723
28724   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28725     return list;
28726
28727   t = cp_parser_expression (parser);
28728
28729   if (t == error_mark_node
28730       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28731     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28732                                            /*or_comma=*/false,
28733                                            /*consume_paren=*/true);
28734
28735   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28736                              "num_teams", location);
28737
28738   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28739   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28740   OMP_CLAUSE_CHAIN (c) = list;
28741
28742   return c;
28743 }
28744
28745 /* OpenMP 4.0:
28746    thread_limit ( expression ) */
28747
28748 static tree
28749 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28750                                    location_t location)
28751 {
28752   tree t, c;
28753
28754   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28755     return list;
28756
28757   t = cp_parser_expression (parser);
28758
28759   if (t == error_mark_node
28760       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28761     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28762                                            /*or_comma=*/false,
28763                                            /*consume_paren=*/true);
28764
28765   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28766                              "thread_limit", location);
28767
28768   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28769   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28770   OMP_CLAUSE_CHAIN (c) = list;
28771
28772   return c;
28773 }
28774
28775 /* OpenMP 4.0:
28776    aligned ( variable-list )
28777    aligned ( variable-list : constant-expression )  */
28778
28779 static tree
28780 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28781 {
28782   tree nlist, c, alignment = NULL_TREE;
28783   bool colon;
28784
28785   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28786     return list;
28787
28788   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28789                                           &colon);
28790
28791   if (colon)
28792     {
28793       alignment = cp_parser_constant_expression (parser);
28794
28795       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28796         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28797                                                /*or_comma=*/false,
28798                                                /*consume_paren=*/true);
28799
28800       if (alignment == error_mark_node)
28801         alignment = NULL_TREE;
28802     }
28803
28804   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28805     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28806
28807   return nlist;
28808 }
28809
28810 /* OpenMP 4.0:
28811    linear ( variable-list )
28812    linear ( variable-list : expression )  */
28813
28814 static tree
28815 cp_parser_omp_clause_linear (cp_parser *parser, tree list, 
28816                              bool is_cilk_simd_fn)
28817 {
28818   tree nlist, c, step = integer_one_node;
28819   bool colon;
28820
28821   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28822     return list;
28823
28824   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28825                                           &colon);
28826
28827   if (colon)
28828     {
28829       step = cp_parser_expression (parser);
28830
28831       if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28832         {
28833           sorry ("using parameters for %<linear%> step is not supported yet");
28834           step = integer_one_node;
28835         }
28836       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28837         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28838                                                /*or_comma=*/false,
28839                                                /*consume_paren=*/true);
28840
28841       if (step == error_mark_node)
28842         return list;
28843     }
28844
28845   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28846     OMP_CLAUSE_LINEAR_STEP (c) = step;
28847
28848   return nlist;
28849 }
28850
28851 /* OpenMP 4.0:
28852    safelen ( constant-expression )  */
28853
28854 static tree
28855 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28856                               location_t location)
28857 {
28858   tree t, c;
28859
28860   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28861     return list;
28862
28863   t = cp_parser_constant_expression (parser);
28864
28865   if (t == error_mark_node
28866       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28867     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28868                                            /*or_comma=*/false,
28869                                            /*consume_paren=*/true);
28870
28871   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28872
28873   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28874   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28875   OMP_CLAUSE_CHAIN (c) = list;
28876
28877   return c;
28878 }
28879
28880 /* OpenMP 4.0:
28881    simdlen ( constant-expression )  */
28882
28883 static tree
28884 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28885                               location_t location)
28886 {
28887   tree t, c;
28888
28889   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28890     return list;
28891
28892   t = cp_parser_constant_expression (parser);
28893
28894   if (t == error_mark_node
28895       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28896     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28897                                            /*or_comma=*/false,
28898                                            /*consume_paren=*/true);
28899
28900   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28901
28902   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28903   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28904   OMP_CLAUSE_CHAIN (c) = list;
28905
28906   return c;
28907 }
28908
28909 /* OpenMP 4.0:
28910    depend ( depend-kind : variable-list )
28911
28912    depend-kind:
28913      in | out | inout  */
28914
28915 static tree
28916 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28917 {
28918   tree nlist, c;
28919   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28920
28921   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28922     return list;
28923
28924   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28925     {
28926       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28927       const char *p = IDENTIFIER_POINTER (id);
28928
28929       if (strcmp ("in", p) == 0)
28930         kind = OMP_CLAUSE_DEPEND_IN;
28931       else if (strcmp ("inout", p) == 0)
28932         kind = OMP_CLAUSE_DEPEND_INOUT;
28933       else if (strcmp ("out", p) == 0)
28934         kind = OMP_CLAUSE_DEPEND_OUT;
28935       else
28936         goto invalid_kind;
28937     }
28938   else
28939     goto invalid_kind;
28940
28941   cp_lexer_consume_token (parser->lexer);
28942   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28943     goto resync_fail;
28944
28945   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28946                                           NULL);
28947
28948   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28949     OMP_CLAUSE_DEPEND_KIND (c) = kind;
28950
28951   return nlist;
28952
28953  invalid_kind:
28954   cp_parser_error (parser, "invalid depend kind");
28955  resync_fail:
28956   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28957                                          /*or_comma=*/false,
28958                                          /*consume_paren=*/true);
28959   return list;
28960 }
28961
28962 /* OpenMP 4.0:
28963    map ( map-kind : variable-list )
28964    map ( variable-list )
28965
28966    map-kind:
28967      alloc | to | from | tofrom  */
28968
28969 static tree
28970 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28971 {
28972   tree nlist, c;
28973   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
28974
28975   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28976     return list;
28977
28978   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28979       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28980     {
28981       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28982       const char *p = IDENTIFIER_POINTER (id);
28983
28984       if (strcmp ("alloc", p) == 0)
28985         kind = GOMP_MAP_ALLOC;
28986       else if (strcmp ("to", p) == 0)
28987         kind = GOMP_MAP_TO;
28988       else if (strcmp ("from", p) == 0)
28989         kind = GOMP_MAP_FROM;
28990       else if (strcmp ("tofrom", p) == 0)
28991         kind = GOMP_MAP_TOFROM;
28992       else
28993         {
28994           cp_parser_error (parser, "invalid map kind");
28995           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28996                                                  /*or_comma=*/false,
28997                                                  /*consume_paren=*/true);
28998           return list;
28999         }
29000       cp_lexer_consume_token (parser->lexer);
29001       cp_lexer_consume_token (parser->lexer);
29002     }
29003
29004   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
29005                                           NULL);
29006
29007   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29008     OMP_CLAUSE_SET_MAP_KIND (c, kind);
29009
29010   return nlist;
29011 }
29012
29013 /* OpenMP 4.0:
29014    device ( expression ) */
29015
29016 static tree
29017 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29018                              location_t location)
29019 {
29020   tree t, c;
29021
29022   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29023     return list;
29024
29025   t = cp_parser_expression (parser);
29026
29027   if (t == error_mark_node
29028       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29029     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29030                                            /*or_comma=*/false,
29031                                            /*consume_paren=*/true);
29032
29033   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29034                              "device", location);
29035
29036   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29037   OMP_CLAUSE_DEVICE_ID (c) = t;
29038   OMP_CLAUSE_CHAIN (c) = list;
29039
29040   return c;
29041 }
29042
29043 /* OpenMP 4.0:
29044    dist_schedule ( static )
29045    dist_schedule ( static , expression )  */
29046
29047 static tree
29048 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29049                                     location_t location)
29050 {
29051   tree c, t;
29052
29053   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29054     return list;
29055
29056   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29057
29058   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29059     goto invalid_kind;
29060   cp_lexer_consume_token (parser->lexer);
29061
29062   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29063     {
29064       cp_lexer_consume_token (parser->lexer);
29065
29066       t = cp_parser_assignment_expression (parser);
29067
29068       if (t == error_mark_node)
29069         goto resync_fail;
29070       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29071
29072       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29073         goto resync_fail;
29074     }
29075   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29076     goto resync_fail;
29077
29078   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29079                              location);
29080   OMP_CLAUSE_CHAIN (c) = list;
29081   return c;
29082
29083  invalid_kind:
29084   cp_parser_error (parser, "invalid dist_schedule kind");
29085  resync_fail:
29086   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29087                                          /*or_comma=*/false,
29088                                          /*consume_paren=*/true);
29089   return list;
29090 }
29091
29092 /* OpenMP 4.0:
29093    proc_bind ( proc-bind-kind )
29094
29095    proc-bind-kind:
29096      master | close | spread  */
29097
29098 static tree
29099 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29100                                 location_t location)
29101 {
29102   tree c;
29103   enum omp_clause_proc_bind_kind kind;
29104
29105   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29106     return list;
29107
29108   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29109     {
29110       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29111       const char *p = IDENTIFIER_POINTER (id);
29112
29113       if (strcmp ("master", p) == 0)
29114         kind = OMP_CLAUSE_PROC_BIND_MASTER;
29115       else if (strcmp ("close", p) == 0)
29116         kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29117       else if (strcmp ("spread", p) == 0)
29118         kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29119       else
29120         goto invalid_kind;
29121     }
29122   else
29123     goto invalid_kind;
29124
29125   cp_lexer_consume_token (parser->lexer);
29126   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29127     goto resync_fail;
29128
29129   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29130   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29131                              location);
29132   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29133   OMP_CLAUSE_CHAIN (c) = list;
29134   return c;
29135
29136  invalid_kind:
29137   cp_parser_error (parser, "invalid depend kind");
29138  resync_fail:
29139   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29140                                          /*or_comma=*/false,
29141                                          /*consume_paren=*/true);
29142   return list;
29143 }
29144
29145 /* OpenACC:
29146    async [( int-expr )] */
29147
29148 static tree
29149 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29150 {
29151   tree c, t;
29152   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29153
29154   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29155
29156   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29157     {
29158       cp_lexer_consume_token (parser->lexer);
29159
29160       t = cp_parser_expression (parser);
29161       if (t == error_mark_node
29162           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29163         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29164                                                 /*or_comma=*/false,
29165                                                 /*consume_paren=*/true);
29166     }
29167
29168   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29169
29170   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29171   OMP_CLAUSE_ASYNC_EXPR (c) = t;
29172   OMP_CLAUSE_CHAIN (c) = list;
29173   list = c;
29174
29175   return list;
29176 }
29177
29178 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
29179    is a bitmask in MASK.  Return the list of clauses found.  */
29180
29181 static tree
29182 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29183                            const char *where, cp_token *pragma_tok,
29184                            bool finish_p = true)
29185 {
29186   tree clauses = NULL;
29187   bool first = true;
29188
29189   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29190     {
29191       location_t here;
29192       pragma_omp_clause c_kind;
29193       const char *c_name;
29194       tree prev = clauses;
29195
29196       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29197         cp_lexer_consume_token (parser->lexer);
29198
29199       here = cp_lexer_peek_token (parser->lexer)->location;
29200       c_kind = cp_parser_omp_clause_name (parser);
29201
29202       switch (c_kind)
29203         {
29204         case PRAGMA_OACC_CLAUSE_ASYNC:
29205           clauses = cp_parser_oacc_clause_async (parser, clauses);
29206           c_name = "async";
29207           break;
29208         case PRAGMA_OACC_CLAUSE_COLLAPSE:
29209           clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29210           c_name = "collapse";
29211           break;
29212         case PRAGMA_OACC_CLAUSE_COPY:
29213           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29214           c_name = "copy";
29215           break;
29216         case PRAGMA_OACC_CLAUSE_COPYIN:
29217           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29218           c_name = "copyin";
29219           break;
29220         case PRAGMA_OACC_CLAUSE_COPYOUT:
29221           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29222           c_name = "copyout";
29223           break;
29224         case PRAGMA_OACC_CLAUSE_CREATE:
29225           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29226           c_name = "create";
29227           break;
29228         case PRAGMA_OACC_CLAUSE_DELETE:
29229           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29230           c_name = "delete";
29231           break;
29232         case PRAGMA_OACC_CLAUSE_DEVICE:
29233           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29234           c_name = "device";
29235           break;
29236         case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29237           clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29238           c_name = "deviceptr";
29239           break;
29240         case PRAGMA_OACC_CLAUSE_HOST:
29241           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29242           c_name = "host";
29243           break;
29244         case PRAGMA_OACC_CLAUSE_IF:
29245           clauses = cp_parser_omp_clause_if (parser, clauses, here);
29246           c_name = "if";
29247           break;
29248         case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29249           clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29250           c_name = "num_gangs";
29251           break;
29252         case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29253           clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29254           c_name = "num_workers";
29255           break;
29256         case PRAGMA_OACC_CLAUSE_PRESENT:
29257           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29258           c_name = "present";
29259           break;
29260         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29261           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29262           c_name = "present_or_copy";
29263           break;
29264         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29265           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29266           c_name = "present_or_copyin";
29267           break;
29268         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29269           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29270           c_name = "present_or_copyout";
29271           break;
29272         case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29273           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29274           c_name = "present_or_create";
29275           break;
29276         case PRAGMA_OACC_CLAUSE_REDUCTION:
29277           clauses = cp_parser_omp_clause_reduction (parser, clauses);
29278           c_name = "reduction";
29279           break;
29280         case PRAGMA_OACC_CLAUSE_SELF:
29281           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29282           c_name = "self";
29283           break;
29284         case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29285           clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29286           c_name = "vector_length";
29287           break;
29288         case PRAGMA_OACC_CLAUSE_WAIT:
29289           clauses = cp_parser_oacc_clause_wait (parser, clauses);
29290           c_name = "wait";
29291           break;
29292         default:
29293           cp_parser_error (parser, "expected %<#pragma acc%> clause");
29294           goto saw_error;
29295         }
29296
29297       first = false;
29298
29299       if (((mask >> c_kind) & 1) == 0)
29300         {
29301           /* Remove the invalid clause(s) from the list to avoid
29302              confusing the rest of the compiler.  */
29303           clauses = prev;
29304           error_at (here, "%qs is not valid for %qs", c_name, where);
29305         }
29306     }
29307
29308  saw_error:
29309   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29310
29311   if (finish_p)
29312     return finish_omp_clauses (clauses);
29313
29314   return clauses;
29315 }
29316
29317 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
29318    is a bitmask in MASK.  Return the list of clauses found; the result
29319    of clause default goes in *pdefault.  */
29320
29321 static tree
29322 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29323                            const char *where, cp_token *pragma_tok,
29324                            bool finish_p = true)
29325 {
29326   tree clauses = NULL;
29327   bool first = true;
29328   cp_token *token = NULL;
29329   bool cilk_simd_fn = false;
29330
29331   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29332     {
29333       pragma_omp_clause c_kind;
29334       const char *c_name;
29335       tree prev = clauses;
29336
29337       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29338         cp_lexer_consume_token (parser->lexer);
29339
29340       token = cp_lexer_peek_token (parser->lexer);
29341       c_kind = cp_parser_omp_clause_name (parser);
29342
29343       switch (c_kind)
29344         {
29345         case PRAGMA_OMP_CLAUSE_COLLAPSE:
29346           clauses = cp_parser_omp_clause_collapse (parser, clauses,
29347                                                    token->location);
29348           c_name = "collapse";
29349           break;
29350         case PRAGMA_OMP_CLAUSE_COPYIN:
29351           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29352           c_name = "copyin";
29353           break;
29354         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29355           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29356                                             clauses);
29357           c_name = "copyprivate";
29358           break;
29359         case PRAGMA_OMP_CLAUSE_DEFAULT:
29360           clauses = cp_parser_omp_clause_default (parser, clauses,
29361                                                   token->location);
29362           c_name = "default";
29363           break;
29364         case PRAGMA_OMP_CLAUSE_FINAL:
29365           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29366           c_name = "final";
29367           break;
29368         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29369           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29370                                             clauses);
29371           c_name = "firstprivate";
29372           break;
29373         case PRAGMA_OMP_CLAUSE_IF:
29374           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29375           c_name = "if";
29376           break;
29377         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29378           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29379                                             clauses);
29380           c_name = "lastprivate";
29381           break;
29382         case PRAGMA_OMP_CLAUSE_MERGEABLE:
29383           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29384                                                     token->location);
29385           c_name = "mergeable";
29386           break;
29387         case PRAGMA_OMP_CLAUSE_NOWAIT:
29388           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29389           c_name = "nowait";
29390           break;
29391         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29392           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29393                                                       token->location);
29394           c_name = "num_threads";
29395           break;
29396         case PRAGMA_OMP_CLAUSE_ORDERED:
29397           clauses = cp_parser_omp_clause_ordered (parser, clauses,
29398                                                   token->location);
29399           c_name = "ordered";
29400           break;
29401         case PRAGMA_OMP_CLAUSE_PRIVATE:
29402           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29403                                             clauses);
29404           c_name = "private";
29405           break;
29406         case PRAGMA_OMP_CLAUSE_REDUCTION:
29407           clauses = cp_parser_omp_clause_reduction (parser, clauses);
29408           c_name = "reduction";
29409           break;
29410         case PRAGMA_OMP_CLAUSE_SCHEDULE:
29411           clauses = cp_parser_omp_clause_schedule (parser, clauses,
29412                                                    token->location);
29413           c_name = "schedule";
29414           break;
29415         case PRAGMA_OMP_CLAUSE_SHARED:
29416           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29417                                             clauses);
29418           c_name = "shared";
29419           break;
29420         case PRAGMA_OMP_CLAUSE_UNTIED:
29421           clauses = cp_parser_omp_clause_untied (parser, clauses,
29422                                                  token->location);
29423           c_name = "untied";
29424           break;
29425         case PRAGMA_OMP_CLAUSE_INBRANCH:
29426         case PRAGMA_CILK_CLAUSE_MASK:
29427           clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29428                                                  clauses, token->location);
29429           c_name = "inbranch";
29430           break;
29431         case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29432         case PRAGMA_CILK_CLAUSE_NOMASK:
29433           clauses = cp_parser_omp_clause_branch (parser,
29434                                                  OMP_CLAUSE_NOTINBRANCH,
29435                                                  clauses, token->location);
29436           c_name = "notinbranch";
29437           break;
29438         case PRAGMA_OMP_CLAUSE_PARALLEL:
29439           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29440                                                      clauses, token->location);
29441           c_name = "parallel";
29442           if (!first)
29443             {
29444              clause_not_first:
29445               error_at (token->location, "%qs must be the first clause of %qs",
29446                         c_name, where);
29447               clauses = prev;
29448             }
29449           break;
29450         case PRAGMA_OMP_CLAUSE_FOR:
29451           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29452                                                      clauses, token->location);
29453           c_name = "for";
29454           if (!first)
29455             goto clause_not_first;
29456           break;
29457         case PRAGMA_OMP_CLAUSE_SECTIONS:
29458           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29459                                                      clauses, token->location);
29460           c_name = "sections";
29461           if (!first)
29462             goto clause_not_first;
29463           break;
29464         case PRAGMA_OMP_CLAUSE_TASKGROUP:
29465           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29466                                                      clauses, token->location);
29467           c_name = "taskgroup";
29468           if (!first)
29469             goto clause_not_first;
29470           break;
29471         case PRAGMA_OMP_CLAUSE_TO:
29472           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29473                                             clauses);
29474           c_name = "to";
29475           break;
29476         case PRAGMA_OMP_CLAUSE_FROM:
29477           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29478                                             clauses);
29479           c_name = "from";
29480           break;
29481         case PRAGMA_OMP_CLAUSE_UNIFORM:
29482           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29483                                             clauses);
29484           c_name = "uniform";
29485           break;
29486         case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29487           clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29488                                                     token->location);
29489           c_name = "num_teams";
29490           break;
29491         case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29492           clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29493                                                        token->location);
29494           c_name = "thread_limit";
29495           break;
29496         case PRAGMA_OMP_CLAUSE_ALIGNED:
29497           clauses = cp_parser_omp_clause_aligned (parser, clauses);
29498           c_name = "aligned";
29499           break;
29500         case PRAGMA_OMP_CLAUSE_LINEAR:
29501           if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29502             cilk_simd_fn = true;
29503           clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29504           c_name = "linear";
29505           break;
29506         case PRAGMA_OMP_CLAUSE_DEPEND:
29507           clauses = cp_parser_omp_clause_depend (parser, clauses);
29508           c_name = "depend";
29509           break;
29510         case PRAGMA_OMP_CLAUSE_MAP:
29511           clauses = cp_parser_omp_clause_map (parser, clauses);
29512           c_name = "map";
29513           break;
29514         case PRAGMA_OMP_CLAUSE_DEVICE:
29515           clauses = cp_parser_omp_clause_device (parser, clauses,
29516                                                  token->location);
29517           c_name = "device";
29518           break;
29519         case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29520           clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29521                                                         token->location);
29522           c_name = "dist_schedule";
29523           break;
29524         case PRAGMA_OMP_CLAUSE_PROC_BIND:
29525           clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29526                                                     token->location);
29527           c_name = "proc_bind";
29528           break;
29529         case PRAGMA_OMP_CLAUSE_SAFELEN:
29530           clauses = cp_parser_omp_clause_safelen (parser, clauses,
29531                                                   token->location);
29532           c_name = "safelen";
29533           break;
29534         case PRAGMA_OMP_CLAUSE_SIMDLEN:
29535           clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29536                                                   token->location);
29537           c_name = "simdlen";
29538           break;
29539         case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29540           clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29541           c_name = "simdlen";
29542           break;
29543         default:
29544           cp_parser_error (parser, "expected %<#pragma omp%> clause");
29545           goto saw_error;
29546         }
29547
29548       first = false;
29549
29550       if (((mask >> c_kind) & 1) == 0)
29551         {
29552           /* Remove the invalid clause(s) from the list to avoid
29553              confusing the rest of the compiler.  */
29554           clauses = prev;
29555           error_at (token->location, "%qs is not valid for %qs", c_name, where);
29556         }
29557     }
29558  saw_error:
29559   /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29560      no reason to skip to the end.  */
29561   if (!(flag_cilkplus && pragma_tok == NULL))
29562     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29563   if (finish_p)
29564     return finish_omp_clauses (clauses);
29565   return clauses;
29566 }
29567
29568 /* OpenMP 2.5:
29569    structured-block:
29570      statement
29571
29572    In practice, we're also interested in adding the statement to an
29573    outer node.  So it is convenient if we work around the fact that
29574    cp_parser_statement calls add_stmt.  */
29575
29576 static unsigned
29577 cp_parser_begin_omp_structured_block (cp_parser *parser)
29578 {
29579   unsigned save = parser->in_statement;
29580
29581   /* Only move the values to IN_OMP_BLOCK if they weren't false.
29582      This preserves the "not within loop or switch" style error messages
29583      for nonsense cases like
29584         void foo() {
29585         #pragma omp single
29586           break;
29587         }
29588   */
29589   if (parser->in_statement)
29590     parser->in_statement = IN_OMP_BLOCK;
29591
29592   return save;
29593 }
29594
29595 static void
29596 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29597 {
29598   parser->in_statement = save;
29599 }
29600
29601 static tree
29602 cp_parser_omp_structured_block (cp_parser *parser)
29603 {
29604   tree stmt = begin_omp_structured_block ();
29605   unsigned int save = cp_parser_begin_omp_structured_block (parser);
29606
29607   cp_parser_statement (parser, NULL_TREE, false, NULL);
29608
29609   cp_parser_end_omp_structured_block (parser, save);
29610   return finish_omp_structured_block (stmt);
29611 }
29612
29613 /* OpenMP 2.5:
29614    # pragma omp atomic new-line
29615      expression-stmt
29616
29617    expression-stmt:
29618      x binop= expr | x++ | ++x | x-- | --x
29619    binop:
29620      +, *, -, /, &, ^, |, <<, >>
29621
29622   where x is an lvalue expression with scalar type.
29623
29624    OpenMP 3.1:
29625    # pragma omp atomic new-line
29626      update-stmt
29627
29628    # pragma omp atomic read new-line
29629      read-stmt
29630
29631    # pragma omp atomic write new-line
29632      write-stmt
29633
29634    # pragma omp atomic update new-line
29635      update-stmt
29636
29637    # pragma omp atomic capture new-line
29638      capture-stmt
29639
29640    # pragma omp atomic capture new-line
29641      capture-block
29642
29643    read-stmt:
29644      v = x
29645    write-stmt:
29646      x = expr
29647    update-stmt:
29648      expression-stmt | x = x binop expr
29649    capture-stmt:
29650      v = expression-stmt
29651    capture-block:
29652      { v = x; update-stmt; } | { update-stmt; v = x; }
29653
29654    OpenMP 4.0:
29655    update-stmt:
29656      expression-stmt | x = x binop expr | x = expr binop x
29657    capture-stmt:
29658      v = update-stmt
29659    capture-block:
29660      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29661
29662   where x and v are lvalue expressions with scalar type.  */
29663
29664 static void
29665 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29666 {
29667   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29668   tree rhs1 = NULL_TREE, orig_lhs;
29669   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29670   bool structured_block = false;
29671   bool seq_cst = false;
29672
29673   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29674     {
29675       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29676       const char *p = IDENTIFIER_POINTER (id);
29677
29678       if (!strcmp (p, "seq_cst"))
29679         {
29680           seq_cst = true;
29681           cp_lexer_consume_token (parser->lexer);
29682           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29683               && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29684             cp_lexer_consume_token (parser->lexer);
29685         }
29686     }
29687   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29688     {
29689       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29690       const char *p = IDENTIFIER_POINTER (id);
29691
29692       if (!strcmp (p, "read"))
29693         code = OMP_ATOMIC_READ;
29694       else if (!strcmp (p, "write"))
29695         code = NOP_EXPR;
29696       else if (!strcmp (p, "update"))
29697         code = OMP_ATOMIC;
29698       else if (!strcmp (p, "capture"))
29699         code = OMP_ATOMIC_CAPTURE_NEW;
29700       else
29701         p = NULL;
29702       if (p)
29703         cp_lexer_consume_token (parser->lexer);
29704     }
29705   if (!seq_cst)
29706     {
29707       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29708           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29709         cp_lexer_consume_token (parser->lexer);
29710
29711       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29712         {
29713           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29714           const char *p = IDENTIFIER_POINTER (id);
29715
29716           if (!strcmp (p, "seq_cst"))
29717             {
29718               seq_cst = true;
29719               cp_lexer_consume_token (parser->lexer);
29720             }
29721         }
29722     }
29723   cp_parser_require_pragma_eol (parser, pragma_tok);
29724
29725   switch (code)
29726     {
29727     case OMP_ATOMIC_READ:
29728     case NOP_EXPR: /* atomic write */
29729       v = cp_parser_unary_expression (parser);
29730       if (v == error_mark_node)
29731         goto saw_error;
29732       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29733         goto saw_error;
29734       if (code == NOP_EXPR)
29735         lhs = cp_parser_expression (parser);
29736       else
29737         lhs = cp_parser_unary_expression (parser);
29738       if (lhs == error_mark_node)
29739         goto saw_error;
29740       if (code == NOP_EXPR)
29741         {
29742           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29743              opcode.  */
29744           code = OMP_ATOMIC;
29745           rhs = lhs;
29746           lhs = v;
29747           v = NULL_TREE;
29748         }
29749       goto done;
29750     case OMP_ATOMIC_CAPTURE_NEW:
29751       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29752         {
29753           cp_lexer_consume_token (parser->lexer);
29754           structured_block = true;
29755         }
29756       else
29757         {
29758           v = cp_parser_unary_expression (parser);
29759           if (v == error_mark_node)
29760             goto saw_error;
29761           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29762             goto saw_error;
29763         }
29764     default:
29765       break;
29766     }
29767
29768 restart:
29769   lhs = cp_parser_unary_expression (parser);
29770   orig_lhs = lhs;
29771   switch (TREE_CODE (lhs))
29772     {
29773     case ERROR_MARK:
29774       goto saw_error;
29775
29776     case POSTINCREMENT_EXPR:
29777       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29778         code = OMP_ATOMIC_CAPTURE_OLD;
29779       /* FALLTHROUGH */
29780     case PREINCREMENT_EXPR:
29781       lhs = TREE_OPERAND (lhs, 0);
29782       opcode = PLUS_EXPR;
29783       rhs = integer_one_node;
29784       break;
29785
29786     case POSTDECREMENT_EXPR:
29787       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29788         code = OMP_ATOMIC_CAPTURE_OLD;
29789       /* FALLTHROUGH */
29790     case PREDECREMENT_EXPR:
29791       lhs = TREE_OPERAND (lhs, 0);
29792       opcode = MINUS_EXPR;
29793       rhs = integer_one_node;
29794       break;
29795
29796     case COMPOUND_EXPR:
29797       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29798          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29799          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29800          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29801          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29802                                              (TREE_OPERAND (lhs, 1), 0), 0)))
29803             == BOOLEAN_TYPE)
29804        /* Undo effects of boolean_increment for post {in,de}crement.  */
29805        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29806       /* FALLTHRU */
29807     case MODIFY_EXPR:
29808       if (TREE_CODE (lhs) == MODIFY_EXPR
29809          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29810         {
29811           /* Undo effects of boolean_increment.  */
29812           if (integer_onep (TREE_OPERAND (lhs, 1)))
29813             {
29814               /* This is pre or post increment.  */
29815               rhs = TREE_OPERAND (lhs, 1);
29816               lhs = TREE_OPERAND (lhs, 0);
29817               opcode = NOP_EXPR;
29818               if (code == OMP_ATOMIC_CAPTURE_NEW
29819                   && !structured_block
29820                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29821                 code = OMP_ATOMIC_CAPTURE_OLD;
29822               break;
29823             }
29824         }
29825       /* FALLTHRU */
29826     default:
29827       switch (cp_lexer_peek_token (parser->lexer)->type)
29828         {
29829         case CPP_MULT_EQ:
29830           opcode = MULT_EXPR;
29831           break;
29832         case CPP_DIV_EQ:
29833           opcode = TRUNC_DIV_EXPR;
29834           break;
29835         case CPP_PLUS_EQ:
29836           opcode = PLUS_EXPR;
29837           break;
29838         case CPP_MINUS_EQ:
29839           opcode = MINUS_EXPR;
29840           break;
29841         case CPP_LSHIFT_EQ:
29842           opcode = LSHIFT_EXPR;
29843           break;
29844         case CPP_RSHIFT_EQ:
29845           opcode = RSHIFT_EXPR;
29846           break;
29847         case CPP_AND_EQ:
29848           opcode = BIT_AND_EXPR;
29849           break;
29850         case CPP_OR_EQ:
29851           opcode = BIT_IOR_EXPR;
29852           break;
29853         case CPP_XOR_EQ:
29854           opcode = BIT_XOR_EXPR;
29855           break;
29856         case CPP_EQ:
29857           enum cp_parser_prec oprec;
29858           cp_token *token;
29859           cp_lexer_consume_token (parser->lexer);
29860           cp_parser_parse_tentatively (parser);
29861           rhs1 = cp_parser_simple_cast_expression (parser);
29862           if (rhs1 == error_mark_node)
29863             {
29864               cp_parser_abort_tentative_parse (parser);
29865               cp_parser_simple_cast_expression (parser);
29866               goto saw_error;
29867             }
29868           token = cp_lexer_peek_token (parser->lexer);
29869           if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29870             {
29871               cp_parser_abort_tentative_parse (parser);
29872               cp_parser_parse_tentatively (parser);
29873               rhs = cp_parser_binary_expression (parser, false, true,
29874                                                  PREC_NOT_OPERATOR, NULL);
29875               if (rhs == error_mark_node)
29876                 {
29877                   cp_parser_abort_tentative_parse (parser);
29878                   cp_parser_binary_expression (parser, false, true,
29879                                                PREC_NOT_OPERATOR, NULL);
29880                   goto saw_error;
29881                 }
29882               switch (TREE_CODE (rhs))
29883                 {
29884                 case MULT_EXPR:
29885                 case TRUNC_DIV_EXPR:
29886                 case RDIV_EXPR:
29887                 case PLUS_EXPR:
29888                 case MINUS_EXPR:
29889                 case LSHIFT_EXPR:
29890                 case RSHIFT_EXPR:
29891                 case BIT_AND_EXPR:
29892                 case BIT_IOR_EXPR:
29893                 case BIT_XOR_EXPR:
29894                   if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29895                     {
29896                       if (cp_parser_parse_definitely (parser))
29897                         {
29898                           opcode = TREE_CODE (rhs);
29899                           rhs1 = TREE_OPERAND (rhs, 0);
29900                           rhs = TREE_OPERAND (rhs, 1);
29901                           goto stmt_done;
29902                         }
29903                       else
29904                         goto saw_error;
29905                     }
29906                   break;
29907                 default:
29908                   break;
29909                 }
29910               cp_parser_abort_tentative_parse (parser);
29911               if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29912                 {
29913                   rhs = cp_parser_expression (parser);
29914                   if (rhs == error_mark_node)
29915                     goto saw_error;
29916                   opcode = NOP_EXPR;
29917                   rhs1 = NULL_TREE;
29918                   goto stmt_done;
29919                 }
29920               cp_parser_error (parser,
29921                                "invalid form of %<#pragma omp atomic%>");
29922               goto saw_error;
29923             }
29924           if (!cp_parser_parse_definitely (parser))
29925             goto saw_error;
29926           switch (token->type)
29927             {
29928             case CPP_SEMICOLON:
29929               if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29930                 {
29931                   code = OMP_ATOMIC_CAPTURE_OLD;
29932                   v = lhs;
29933                   lhs = NULL_TREE;
29934                   lhs1 = rhs1;
29935                   rhs1 = NULL_TREE;
29936                   cp_lexer_consume_token (parser->lexer);
29937                   goto restart;
29938                 }
29939               else if (structured_block)
29940                 {
29941                   opcode = NOP_EXPR;
29942                   rhs = rhs1;
29943                   rhs1 = NULL_TREE;
29944                   goto stmt_done;
29945                 }
29946               cp_parser_error (parser,
29947                                "invalid form of %<#pragma omp atomic%>");
29948               goto saw_error;
29949             case CPP_MULT:
29950               opcode = MULT_EXPR;
29951               break;
29952             case CPP_DIV:
29953               opcode = TRUNC_DIV_EXPR;
29954               break;
29955             case CPP_PLUS:
29956               opcode = PLUS_EXPR;
29957               break;
29958             case CPP_MINUS:
29959               opcode = MINUS_EXPR;
29960               break;
29961             case CPP_LSHIFT:
29962               opcode = LSHIFT_EXPR;
29963               break;
29964             case CPP_RSHIFT:
29965               opcode = RSHIFT_EXPR;
29966               break;
29967             case CPP_AND:
29968               opcode = BIT_AND_EXPR;
29969               break;
29970             case CPP_OR:
29971               opcode = BIT_IOR_EXPR;
29972               break;
29973             case CPP_XOR:
29974               opcode = BIT_XOR_EXPR;
29975               break;
29976             default:
29977               cp_parser_error (parser,
29978                                "invalid operator for %<#pragma omp atomic%>");
29979               goto saw_error;
29980             }
29981           oprec = TOKEN_PRECEDENCE (token);
29982           gcc_assert (oprec != PREC_NOT_OPERATOR);
29983           if (commutative_tree_code (opcode))
29984             oprec = (enum cp_parser_prec) (oprec - 1);
29985           cp_lexer_consume_token (parser->lexer);
29986           rhs = cp_parser_binary_expression (parser, false, false,
29987                                              oprec, NULL);
29988           if (rhs == error_mark_node)
29989             goto saw_error;
29990           goto stmt_done;
29991           /* FALLTHROUGH */
29992         default:
29993           cp_parser_error (parser,
29994                            "invalid operator for %<#pragma omp atomic%>");
29995           goto saw_error;
29996         }
29997       cp_lexer_consume_token (parser->lexer);
29998
29999       rhs = cp_parser_expression (parser);
30000       if (rhs == error_mark_node)
30001         goto saw_error;
30002       break;
30003     }
30004 stmt_done:
30005   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30006     {
30007       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
30008         goto saw_error;
30009       v = cp_parser_unary_expression (parser);
30010       if (v == error_mark_node)
30011         goto saw_error;
30012       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30013         goto saw_error;
30014       lhs1 = cp_parser_unary_expression (parser);
30015       if (lhs1 == error_mark_node)
30016         goto saw_error;
30017     }
30018   if (structured_block)
30019     {
30020       cp_parser_consume_semicolon_at_end_of_statement (parser);
30021       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30022     }
30023 done:
30024   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30025   if (!structured_block)
30026     cp_parser_consume_semicolon_at_end_of_statement (parser);
30027   return;
30028
30029  saw_error:
30030   cp_parser_skip_to_end_of_block_or_statement (parser);
30031   if (structured_block)
30032     {
30033       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30034         cp_lexer_consume_token (parser->lexer);
30035       else if (code == OMP_ATOMIC_CAPTURE_NEW)
30036         {
30037           cp_parser_skip_to_end_of_block_or_statement (parser);
30038           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30039             cp_lexer_consume_token (parser->lexer);
30040         }
30041     }
30042 }
30043
30044
30045 /* OpenMP 2.5:
30046    # pragma omp barrier new-line  */
30047
30048 static void
30049 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30050 {
30051   cp_parser_require_pragma_eol (parser, pragma_tok);
30052   finish_omp_barrier ();
30053 }
30054
30055 /* OpenMP 2.5:
30056    # pragma omp critical [(name)] new-line
30057      structured-block  */
30058
30059 static tree
30060 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30061 {
30062   tree stmt, name = NULL;
30063
30064   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30065     {
30066       cp_lexer_consume_token (parser->lexer);
30067
30068       name = cp_parser_identifier (parser);
30069
30070       if (name == error_mark_node
30071           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30072         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30073                                                /*or_comma=*/false,
30074                                                /*consume_paren=*/true);
30075       if (name == error_mark_node)
30076         name = NULL;
30077     }
30078   cp_parser_require_pragma_eol (parser, pragma_tok);
30079
30080   stmt = cp_parser_omp_structured_block (parser);
30081   return c_finish_omp_critical (input_location, stmt, name);
30082 }
30083
30084 /* OpenMP 2.5:
30085    # pragma omp flush flush-vars[opt] new-line
30086
30087    flush-vars:
30088      ( variable-list ) */
30089
30090 static void
30091 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30092 {
30093   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30094     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30095   cp_parser_require_pragma_eol (parser, pragma_tok);
30096
30097   finish_omp_flush ();
30098 }
30099
30100 /* Helper function, to parse omp for increment expression.  */
30101
30102 static tree
30103 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30104 {
30105   tree cond = cp_parser_binary_expression (parser, false, true,
30106                                            PREC_NOT_OPERATOR, NULL);
30107   if (cond == error_mark_node
30108       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30109     {
30110       cp_parser_skip_to_end_of_statement (parser);
30111       return error_mark_node;
30112     }
30113
30114   switch (TREE_CODE (cond))
30115     {
30116     case GT_EXPR:
30117     case GE_EXPR:
30118     case LT_EXPR:
30119     case LE_EXPR:
30120       break;
30121     case NE_EXPR:
30122       if (code == CILK_SIMD || code == CILK_FOR)
30123         break;
30124       /* Fall through: OpenMP disallows NE_EXPR.  */
30125     default:
30126       return error_mark_node;
30127     }
30128
30129   /* If decl is an iterator, preserve LHS and RHS of the relational
30130      expr until finish_omp_for.  */
30131   if (decl
30132       && (type_dependent_expression_p (decl)
30133           || CLASS_TYPE_P (TREE_TYPE (decl))))
30134     return cond;
30135
30136   return build_x_binary_op (input_location, TREE_CODE (cond),
30137                             TREE_OPERAND (cond, 0), ERROR_MARK,
30138                             TREE_OPERAND (cond, 1), ERROR_MARK,
30139                             /*overload=*/NULL, tf_warning_or_error);
30140 }
30141
30142 /* Helper function, to parse omp for increment expression.  */
30143
30144 static tree
30145 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30146 {
30147   cp_token *token = cp_lexer_peek_token (parser->lexer);
30148   enum tree_code op;
30149   tree lhs, rhs;
30150   cp_id_kind idk;
30151   bool decl_first;
30152
30153   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30154     {
30155       op = (token->type == CPP_PLUS_PLUS
30156             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30157       cp_lexer_consume_token (parser->lexer);
30158       lhs = cp_parser_simple_cast_expression (parser);
30159       if (lhs != decl)
30160         return error_mark_node;
30161       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30162     }
30163
30164   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30165   if (lhs != decl)
30166     return error_mark_node;
30167
30168   token = cp_lexer_peek_token (parser->lexer);
30169   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30170     {
30171       op = (token->type == CPP_PLUS_PLUS
30172             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30173       cp_lexer_consume_token (parser->lexer);
30174       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30175     }
30176
30177   op = cp_parser_assignment_operator_opt (parser);
30178   if (op == ERROR_MARK)
30179     return error_mark_node;
30180
30181   if (op != NOP_EXPR)
30182     {
30183       rhs = cp_parser_assignment_expression (parser);
30184       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30185       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30186     }
30187
30188   lhs = cp_parser_binary_expression (parser, false, false,
30189                                      PREC_ADDITIVE_EXPRESSION, NULL);
30190   token = cp_lexer_peek_token (parser->lexer);
30191   decl_first = lhs == decl;
30192   if (decl_first)
30193     lhs = NULL_TREE;
30194   if (token->type != CPP_PLUS
30195       && token->type != CPP_MINUS)
30196     return error_mark_node;
30197
30198   do
30199     {
30200       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30201       cp_lexer_consume_token (parser->lexer);
30202       rhs = cp_parser_binary_expression (parser, false, false,
30203                                          PREC_ADDITIVE_EXPRESSION, NULL);
30204       token = cp_lexer_peek_token (parser->lexer);
30205       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30206         {
30207           if (lhs == NULL_TREE)
30208             {
30209               if (op == PLUS_EXPR)
30210                 lhs = rhs;
30211               else
30212                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30213                                         tf_warning_or_error);
30214             }
30215           else
30216             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30217                                      ERROR_MARK, NULL, tf_warning_or_error);
30218         }
30219     }
30220   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30221
30222   if (!decl_first)
30223     {
30224       if (rhs != decl || op == MINUS_EXPR)
30225         return error_mark_node;
30226       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30227     }
30228   else
30229     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30230
30231   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30232 }
30233
30234 /* Parse the initialization statement of either an OpenMP for loop or
30235    a Cilk Plus for loop.
30236
30237    Return true if the resulting construct should have an
30238    OMP_CLAUSE_PRIVATE added to it.  */
30239
30240 static bool
30241 cp_parser_omp_for_loop_init (cp_parser *parser,
30242                              enum tree_code code,
30243                              tree &this_pre_body,
30244                              vec<tree, va_gc> *for_block,
30245                              tree &init,
30246                              tree &decl,
30247                              tree &real_decl)
30248 {
30249   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30250     return false;
30251
30252   bool add_private_clause = false;
30253
30254   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30255
30256      init-expr:
30257      var = lb
30258      integer-type var = lb
30259      random-access-iterator-type var = lb
30260      pointer-type var = lb
30261   */
30262   cp_decl_specifier_seq type_specifiers;
30263
30264   /* First, try to parse as an initialized declaration.  See
30265      cp_parser_condition, from whence the bulk of this is copied.  */
30266
30267   cp_parser_parse_tentatively (parser);
30268   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30269                                 /*is_trailing_return=*/false,
30270                                 &type_specifiers);
30271   if (cp_parser_parse_definitely (parser))
30272     {
30273       /* If parsing a type specifier seq succeeded, then this
30274          MUST be a initialized declaration.  */
30275       tree asm_specification, attributes;
30276       cp_declarator *declarator;
30277
30278       declarator = cp_parser_declarator (parser,
30279                                          CP_PARSER_DECLARATOR_NAMED,
30280                                          /*ctor_dtor_or_conv_p=*/NULL,
30281                                          /*parenthesized_p=*/NULL,
30282                                          /*member_p=*/false,
30283                                          /*friend_p=*/false);
30284       attributes = cp_parser_attributes_opt (parser);
30285       asm_specification = cp_parser_asm_specification_opt (parser);
30286
30287       if (declarator == cp_error_declarator) 
30288         cp_parser_skip_to_end_of_statement (parser);
30289
30290       else 
30291         {
30292           tree pushed_scope, auto_node;
30293
30294           decl = start_decl (declarator, &type_specifiers,
30295                              SD_INITIALIZED, attributes,
30296                              /*prefix_attributes=*/NULL_TREE,
30297                              &pushed_scope);
30298
30299           auto_node = type_uses_auto (TREE_TYPE (decl));
30300           if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30301             {
30302               if (cp_lexer_next_token_is (parser->lexer, 
30303                                           CPP_OPEN_PAREN))
30304                 {
30305                   if (code != CILK_SIMD && code != CILK_FOR)
30306                     error ("parenthesized initialization is not allowed in "
30307                            "OpenMP %<for%> loop");
30308                   else
30309                     error ("parenthesized initialization is "
30310                            "not allowed in for-loop");
30311                 }
30312               else
30313                 /* Trigger an error.  */
30314                 cp_parser_require (parser, CPP_EQ, RT_EQ);
30315
30316               init = error_mark_node;
30317               cp_parser_skip_to_end_of_statement (parser);
30318             }
30319           else if (CLASS_TYPE_P (TREE_TYPE (decl))
30320                    || type_dependent_expression_p (decl)
30321                    || auto_node)
30322             {
30323               bool is_direct_init, is_non_constant_init;
30324
30325               init = cp_parser_initializer (parser,
30326                                             &is_direct_init,
30327                                             &is_non_constant_init);
30328
30329               if (auto_node)
30330                 {
30331                   TREE_TYPE (decl)
30332                     = do_auto_deduction (TREE_TYPE (decl), init,
30333                                          auto_node);
30334
30335                   if (!CLASS_TYPE_P (TREE_TYPE (decl))
30336                       && !type_dependent_expression_p (decl))
30337                     goto non_class;
30338                 }
30339                       
30340               cp_finish_decl (decl, init, !is_non_constant_init,
30341                               asm_specification,
30342                               LOOKUP_ONLYCONVERTING);
30343               if (CLASS_TYPE_P (TREE_TYPE (decl)))
30344                 {
30345                   vec_safe_push (for_block, this_pre_body);
30346                   init = NULL_TREE;
30347                 }
30348               else
30349                 init = pop_stmt_list (this_pre_body);
30350               this_pre_body = NULL_TREE;
30351             }
30352           else
30353             {
30354               /* Consume '='.  */
30355               cp_lexer_consume_token (parser->lexer);
30356               init = cp_parser_assignment_expression (parser);
30357
30358             non_class:
30359               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30360                 init = error_mark_node;
30361               else
30362                 cp_finish_decl (decl, NULL_TREE,
30363                                 /*init_const_expr_p=*/false,
30364                                 asm_specification,
30365                                 LOOKUP_ONLYCONVERTING);
30366             }
30367
30368           if (pushed_scope)
30369             pop_scope (pushed_scope);
30370         }
30371     }
30372   else 
30373     {
30374       cp_id_kind idk;
30375       /* If parsing a type specifier sequence failed, then
30376          this MUST be a simple expression.  */
30377       if (code == CILK_FOR)
30378         error ("%<_Cilk_for%> allows expression instead of declaration only "
30379                "in C, not in C++");
30380       cp_parser_parse_tentatively (parser);
30381       decl = cp_parser_primary_expression (parser, false, false,
30382                                            false, &idk);
30383       if (!cp_parser_error_occurred (parser)
30384           && decl
30385           && DECL_P (decl)
30386           && CLASS_TYPE_P (TREE_TYPE (decl)))
30387         {
30388           tree rhs;
30389
30390           cp_parser_parse_definitely (parser);
30391           cp_parser_require (parser, CPP_EQ, RT_EQ);
30392           rhs = cp_parser_assignment_expression (parser);
30393           finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30394                                                  decl, NOP_EXPR,
30395                                                  rhs,
30396                                                  tf_warning_or_error));
30397           add_private_clause = true;
30398         }
30399       else
30400         {
30401           decl = NULL;
30402           cp_parser_abort_tentative_parse (parser);
30403           init = cp_parser_expression (parser);
30404           if (init)
30405             {
30406               if (TREE_CODE (init) == MODIFY_EXPR
30407                   || TREE_CODE (init) == MODOP_EXPR)
30408                 real_decl = TREE_OPERAND (init, 0);
30409             }
30410         }
30411     }
30412   return add_private_clause;
30413 }
30414
30415 /* Parse the restricted form of the for statement allowed by OpenMP.  */
30416
30417 static tree
30418 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30419                         tree *cclauses)
30420 {
30421   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30422   tree real_decl, initv, condv, incrv, declv;
30423   tree this_pre_body, cl;
30424   location_t loc_first;
30425   bool collapse_err = false;
30426   int i, collapse = 1, nbraces = 0;
30427   vec<tree, va_gc> *for_block = make_tree_vector ();
30428
30429   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30430     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30431       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30432
30433   gcc_assert (collapse >= 1);
30434
30435   declv = make_tree_vec (collapse);
30436   initv = make_tree_vec (collapse);
30437   condv = make_tree_vec (collapse);
30438   incrv = make_tree_vec (collapse);
30439
30440   loc_first = cp_lexer_peek_token (parser->lexer)->location;
30441
30442   for (i = 0; i < collapse; i++)
30443     {
30444       int bracecount = 0;
30445       bool add_private_clause = false;
30446       location_t loc;
30447
30448       if (code != CILK_FOR
30449           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30450         {
30451           cp_parser_error (parser, "for statement expected");
30452           return NULL;
30453         }
30454       if (code == CILK_FOR
30455           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30456         {
30457           cp_parser_error (parser, "_Cilk_for statement expected");
30458           return NULL;
30459         }
30460       loc = cp_lexer_consume_token (parser->lexer)->location;
30461
30462       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30463         return NULL;
30464
30465       init = decl = real_decl = NULL;
30466       this_pre_body = push_stmt_list ();
30467
30468       add_private_clause
30469         |= cp_parser_omp_for_loop_init (parser, code,
30470                                         this_pre_body, for_block,
30471                                         init, decl, real_decl);
30472
30473       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30474       if (this_pre_body)
30475         {
30476           this_pre_body = pop_stmt_list (this_pre_body);
30477           if (pre_body)
30478             {
30479               tree t = pre_body;
30480               pre_body = push_stmt_list ();
30481               add_stmt (t);
30482               add_stmt (this_pre_body);
30483               pre_body = pop_stmt_list (pre_body);
30484             }
30485           else
30486             pre_body = this_pre_body;
30487         }
30488
30489       if (decl)
30490         real_decl = decl;
30491       if (cclauses != NULL
30492           && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30493           && real_decl != NULL_TREE)
30494         {
30495           tree *c;
30496           for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30497             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30498                 && OMP_CLAUSE_DECL (*c) == real_decl)
30499               {
30500                 error_at (loc, "iteration variable %qD"
30501                           " should not be firstprivate", real_decl);
30502                 *c = OMP_CLAUSE_CHAIN (*c);
30503               }
30504             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30505                      && OMP_CLAUSE_DECL (*c) == real_decl)
30506               {
30507                 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
30508                 tree l = *c;
30509                 *c = OMP_CLAUSE_CHAIN (*c);
30510                 if (code == OMP_SIMD)
30511                   {
30512                     OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30513                     cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30514                   }
30515                 else
30516                   {
30517                     OMP_CLAUSE_CHAIN (l) = clauses;
30518                     clauses = l;
30519                   }
30520                 add_private_clause = false;
30521               }
30522             else
30523               {
30524                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30525                     && OMP_CLAUSE_DECL (*c) == real_decl)
30526                   add_private_clause = false;
30527                 c = &OMP_CLAUSE_CHAIN (*c);
30528               }
30529         }
30530
30531       if (add_private_clause)
30532         {
30533           tree c;
30534           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30535             {
30536               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30537                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30538                   && OMP_CLAUSE_DECL (c) == decl)
30539                 break;
30540               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30541                        && OMP_CLAUSE_DECL (c) == decl)
30542                 error_at (loc, "iteration variable %qD "
30543                           "should not be firstprivate",
30544                           decl);
30545               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30546                        && OMP_CLAUSE_DECL (c) == decl)
30547                 error_at (loc, "iteration variable %qD should not be reduction",
30548                           decl);
30549             }
30550           if (c == NULL)
30551             {
30552               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30553               OMP_CLAUSE_DECL (c) = decl;
30554               c = finish_omp_clauses (c);
30555               if (c)
30556                 {
30557                   OMP_CLAUSE_CHAIN (c) = clauses;
30558                   clauses = c;
30559                 }
30560             }
30561         }
30562
30563       cond = NULL;
30564       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30565         cond = cp_parser_omp_for_cond (parser, decl, code);
30566       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30567
30568       incr = NULL;
30569       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30570         {
30571           /* If decl is an iterator, preserve the operator on decl
30572              until finish_omp_for.  */
30573           if (real_decl
30574               && ((processing_template_decl
30575                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30576                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30577             incr = cp_parser_omp_for_incr (parser, real_decl);
30578           else
30579             incr = cp_parser_expression (parser);
30580           if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30581             SET_EXPR_LOCATION (incr, input_location);
30582         }
30583
30584       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30585         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30586                                                /*or_comma=*/false,
30587                                                /*consume_paren=*/true);
30588
30589       TREE_VEC_ELT (declv, i) = decl;
30590       TREE_VEC_ELT (initv, i) = init;
30591       TREE_VEC_ELT (condv, i) = cond;
30592       TREE_VEC_ELT (incrv, i) = incr;
30593
30594       if (i == collapse - 1)
30595         break;
30596
30597       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30598          in between the collapsed for loops to be still considered perfectly
30599          nested.  Hopefully the final version clarifies this.
30600          For now handle (multiple) {'s and empty statements.  */
30601       cp_parser_parse_tentatively (parser);
30602       do
30603         {
30604           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30605             break;
30606           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30607             {
30608               cp_lexer_consume_token (parser->lexer);
30609               bracecount++;
30610             }
30611           else if (bracecount
30612                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30613             cp_lexer_consume_token (parser->lexer);
30614           else
30615             {
30616               loc = cp_lexer_peek_token (parser->lexer)->location;
30617               error_at (loc, "not enough collapsed for loops");
30618               collapse_err = true;
30619               cp_parser_abort_tentative_parse (parser);
30620               declv = NULL_TREE;
30621               break;
30622             }
30623         }
30624       while (1);
30625
30626       if (declv)
30627         {
30628           cp_parser_parse_definitely (parser);
30629           nbraces += bracecount;
30630         }
30631     }
30632
30633   /* Note that we saved the original contents of this flag when we entered
30634      the structured block, and so we don't need to re-save it here.  */
30635   if (code == CILK_SIMD || code == CILK_FOR)
30636     parser->in_statement = IN_CILK_SIMD_FOR;
30637   else
30638     parser->in_statement = IN_OMP_FOR;
30639
30640   /* Note that the grammar doesn't call for a structured block here,
30641      though the loop as a whole is a structured block.  */
30642   body = push_stmt_list ();
30643   cp_parser_statement (parser, NULL_TREE, false, NULL);
30644   body = pop_stmt_list (body);
30645
30646   if (declv == NULL_TREE)
30647     ret = NULL_TREE;
30648   else
30649     ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30650                           pre_body, clauses);
30651
30652   while (nbraces)
30653     {
30654       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30655         {
30656           cp_lexer_consume_token (parser->lexer);
30657           nbraces--;
30658         }
30659       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30660         cp_lexer_consume_token (parser->lexer);
30661       else
30662         {
30663           if (!collapse_err)
30664             {
30665               error_at (cp_lexer_peek_token (parser->lexer)->location,
30666                         "collapsed loops not perfectly nested");
30667             }
30668           collapse_err = true;
30669           cp_parser_statement_seq_opt (parser, NULL);
30670           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30671             break;
30672         }
30673     }
30674
30675   while (!for_block->is_empty ())
30676     add_stmt (pop_stmt_list (for_block->pop ()));
30677   release_tree_vector (for_block);
30678
30679   return ret;
30680 }
30681
30682 /* Helper function for OpenMP parsing, split clauses and call
30683    finish_omp_clauses on each of the set of clauses afterwards.  */
30684
30685 static void
30686 cp_omp_split_clauses (location_t loc, enum tree_code code,
30687                       omp_clause_mask mask, tree clauses, tree *cclauses)
30688 {
30689   int i;
30690   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30691   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30692     if (cclauses[i])
30693       cclauses[i] = finish_omp_clauses (cclauses[i]);
30694 }
30695
30696 /* OpenMP 4.0:
30697    #pragma omp simd simd-clause[optseq] new-line
30698      for-loop  */
30699
30700 #define OMP_SIMD_CLAUSE_MASK                                    \
30701         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)      \
30702         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
30703         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
30704         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30705         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30706         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30707         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30708
30709 static tree
30710 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30711                     char *p_name, omp_clause_mask mask, tree *cclauses)
30712 {
30713   tree clauses, sb, ret;
30714   unsigned int save;
30715   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30716
30717   strcat (p_name, " simd");
30718   mask |= OMP_SIMD_CLAUSE_MASK;
30719   mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30720
30721   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30722                                        cclauses == NULL);
30723   if (cclauses)
30724     {
30725       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30726       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30727     }
30728
30729   sb = begin_omp_structured_block ();
30730   save = cp_parser_begin_omp_structured_block (parser);
30731
30732   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30733
30734   cp_parser_end_omp_structured_block (parser, save);
30735   add_stmt (finish_omp_structured_block (sb));
30736
30737   return ret;
30738 }
30739
30740 /* OpenMP 2.5:
30741    #pragma omp for for-clause[optseq] new-line
30742      for-loop
30743
30744    OpenMP 4.0:
30745    #pragma omp for simd for-simd-clause[optseq] new-line
30746      for-loop  */
30747
30748 #define OMP_FOR_CLAUSE_MASK                                     \
30749         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30750         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30751         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30752         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30753         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)      \
30754         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)     \
30755         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
30756         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30757
30758 static tree
30759 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30760                    char *p_name, omp_clause_mask mask, tree *cclauses)
30761 {
30762   tree clauses, sb, ret;
30763   unsigned int save;
30764   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30765
30766   strcat (p_name, " for");
30767   mask |= OMP_FOR_CLAUSE_MASK;
30768   if (cclauses)
30769     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30770
30771   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30772     {
30773       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30774       const char *p = IDENTIFIER_POINTER (id);
30775
30776       if (strcmp (p, "simd") == 0)
30777         {
30778           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30779           if (cclauses == NULL)
30780             cclauses = cclauses_buf;
30781
30782           cp_lexer_consume_token (parser->lexer);
30783           if (!flag_openmp)  /* flag_openmp_simd  */
30784             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30785                                        cclauses);
30786           sb = begin_omp_structured_block ();
30787           save = cp_parser_begin_omp_structured_block (parser);
30788           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30789                                     cclauses);
30790           cp_parser_end_omp_structured_block (parser, save);
30791           tree body = finish_omp_structured_block (sb);
30792           if (ret == NULL)
30793             return ret;
30794           ret = make_node (OMP_FOR);
30795           TREE_TYPE (ret) = void_type_node;
30796           OMP_FOR_BODY (ret) = body;
30797           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30798           SET_EXPR_LOCATION (ret, loc);
30799           add_stmt (ret);
30800           return ret;
30801         }
30802     }
30803   if (!flag_openmp)  /* flag_openmp_simd  */
30804     {
30805       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30806       return NULL_TREE;
30807     }
30808
30809   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30810                                        cclauses == NULL);
30811   if (cclauses)
30812     {
30813       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30814       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30815     }
30816
30817   sb = begin_omp_structured_block ();
30818   save = cp_parser_begin_omp_structured_block (parser);
30819
30820   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30821
30822   cp_parser_end_omp_structured_block (parser, save);
30823   add_stmt (finish_omp_structured_block (sb));
30824
30825   return ret;
30826 }
30827
30828 /* OpenMP 2.5:
30829    # pragma omp master new-line
30830      structured-block  */
30831
30832 static tree
30833 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30834 {
30835   cp_parser_require_pragma_eol (parser, pragma_tok);
30836   return c_finish_omp_master (input_location,
30837                               cp_parser_omp_structured_block (parser));
30838 }
30839
30840 /* OpenMP 2.5:
30841    # pragma omp ordered new-line
30842      structured-block  */
30843
30844 static tree
30845 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30846 {
30847   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30848   cp_parser_require_pragma_eol (parser, pragma_tok);
30849   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30850 }
30851
30852 /* OpenMP 2.5:
30853
30854    section-scope:
30855      { section-sequence }
30856
30857    section-sequence:
30858      section-directive[opt] structured-block
30859      section-sequence section-directive structured-block  */
30860
30861 static tree
30862 cp_parser_omp_sections_scope (cp_parser *parser)
30863 {
30864   tree stmt, substmt;
30865   bool error_suppress = false;
30866   cp_token *tok;
30867
30868   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30869     return NULL_TREE;
30870
30871   stmt = push_stmt_list ();
30872
30873   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30874     {
30875       substmt = cp_parser_omp_structured_block (parser);
30876       substmt = build1 (OMP_SECTION, void_type_node, substmt);
30877       add_stmt (substmt);
30878     }
30879
30880   while (1)
30881     {
30882       tok = cp_lexer_peek_token (parser->lexer);
30883       if (tok->type == CPP_CLOSE_BRACE)
30884         break;
30885       if (tok->type == CPP_EOF)
30886         break;
30887
30888       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30889         {
30890           cp_lexer_consume_token (parser->lexer);
30891           cp_parser_require_pragma_eol (parser, tok);
30892           error_suppress = false;
30893         }
30894       else if (!error_suppress)
30895         {
30896           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30897           error_suppress = true;
30898         }
30899
30900       substmt = cp_parser_omp_structured_block (parser);
30901       substmt = build1 (OMP_SECTION, void_type_node, substmt);
30902       add_stmt (substmt);
30903     }
30904   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30905
30906   substmt = pop_stmt_list (stmt);
30907
30908   stmt = make_node (OMP_SECTIONS);
30909   TREE_TYPE (stmt) = void_type_node;
30910   OMP_SECTIONS_BODY (stmt) = substmt;
30911
30912   add_stmt (stmt);
30913   return stmt;
30914 }
30915
30916 /* OpenMP 2.5:
30917    # pragma omp sections sections-clause[optseq] newline
30918      sections-scope  */
30919
30920 #define OMP_SECTIONS_CLAUSE_MASK                                \
30921         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30922         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30923         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30924         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30925         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30926
30927 static tree
30928 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30929                         char *p_name, omp_clause_mask mask, tree *cclauses)
30930 {
30931   tree clauses, ret;
30932   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30933
30934   strcat (p_name, " sections");
30935   mask |= OMP_SECTIONS_CLAUSE_MASK;
30936   if (cclauses)
30937     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30938
30939   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30940                                        cclauses == NULL);
30941   if (cclauses)
30942     {
30943       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30944       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30945     }
30946
30947   ret = cp_parser_omp_sections_scope (parser);
30948   if (ret)
30949     OMP_SECTIONS_CLAUSES (ret) = clauses;
30950
30951   return ret;
30952 }
30953
30954 /* OpenMP 2.5:
30955    # pragma omp parallel parallel-clause[optseq] new-line
30956      structured-block
30957    # pragma omp parallel for parallel-for-clause[optseq] new-line
30958      structured-block
30959    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30960      structured-block
30961
30962    OpenMP 4.0:
30963    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30964      structured-block */
30965
30966 #define OMP_PARALLEL_CLAUSE_MASK                                \
30967         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
30968         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30969         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30970         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
30971         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
30972         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)       \
30973         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30974         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)  \
30975         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30976
30977 static tree
30978 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30979                         char *p_name, omp_clause_mask mask, tree *cclauses)
30980 {
30981   tree stmt, clauses, block;
30982   unsigned int save;
30983   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30984
30985   strcat (p_name, " parallel");
30986   mask |= OMP_PARALLEL_CLAUSE_MASK;
30987
30988   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30989     {
30990       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30991       if (cclauses == NULL)
30992         cclauses = cclauses_buf;
30993
30994       cp_lexer_consume_token (parser->lexer);
30995       if (!flag_openmp)  /* flag_openmp_simd  */
30996         return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30997       block = begin_omp_parallel ();
30998       save = cp_parser_begin_omp_structured_block (parser);
30999       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31000       cp_parser_end_omp_structured_block (parser, save);
31001       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31002                                   block);
31003       if (ret == NULL_TREE)
31004         return ret;
31005       OMP_PARALLEL_COMBINED (stmt) = 1;
31006       return stmt;
31007     }
31008   else if (cclauses)
31009     {
31010       error_at (loc, "expected %<for%> after %qs", p_name);
31011       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31012       return NULL_TREE;
31013     }
31014   else if (!flag_openmp)  /* flag_openmp_simd  */
31015     {
31016       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31017       return NULL_TREE;
31018     }
31019   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31020     {
31021       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31022       const char *p = IDENTIFIER_POINTER (id);
31023       if (strcmp (p, "sections") == 0)
31024         {
31025           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31026           cclauses = cclauses_buf;
31027
31028           cp_lexer_consume_token (parser->lexer);
31029           block = begin_omp_parallel ();
31030           save = cp_parser_begin_omp_structured_block (parser);
31031           cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31032           cp_parser_end_omp_structured_block (parser, save);
31033           stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31034                                       block);
31035           OMP_PARALLEL_COMBINED (stmt) = 1;
31036           return stmt;
31037         }
31038     }
31039
31040   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31041
31042   block = begin_omp_parallel ();
31043   save = cp_parser_begin_omp_structured_block (parser);
31044   cp_parser_statement (parser, NULL_TREE, false, NULL);
31045   cp_parser_end_omp_structured_block (parser, save);
31046   stmt = finish_omp_parallel (clauses, block);
31047   return stmt;
31048 }
31049
31050 /* OpenMP 2.5:
31051    # pragma omp single single-clause[optseq] new-line
31052      structured-block  */
31053
31054 #define OMP_SINGLE_CLAUSE_MASK                                  \
31055         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31056         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31057         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)  \
31058         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31059
31060 static tree
31061 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31062 {
31063   tree stmt = make_node (OMP_SINGLE);
31064   TREE_TYPE (stmt) = void_type_node;
31065
31066   OMP_SINGLE_CLAUSES (stmt)
31067     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31068                                  "#pragma omp single", pragma_tok);
31069   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31070
31071   return add_stmt (stmt);
31072 }
31073
31074 /* OpenMP 3.0:
31075    # pragma omp task task-clause[optseq] new-line
31076      structured-block  */
31077
31078 #define OMP_TASK_CLAUSE_MASK                                    \
31079         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
31080         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
31081         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
31082         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31083         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31084         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
31085         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
31086         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
31087         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31088
31089 static tree
31090 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31091 {
31092   tree clauses, block;
31093   unsigned int save;
31094
31095   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31096                                        "#pragma omp task", pragma_tok);
31097   block = begin_omp_task ();
31098   save = cp_parser_begin_omp_structured_block (parser);
31099   cp_parser_statement (parser, NULL_TREE, false, NULL);
31100   cp_parser_end_omp_structured_block (parser, save);
31101   return finish_omp_task (clauses, block);
31102 }
31103
31104 /* OpenMP 3.0:
31105    # pragma omp taskwait new-line  */
31106
31107 static void
31108 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31109 {
31110   cp_parser_require_pragma_eol (parser, pragma_tok);
31111   finish_omp_taskwait ();
31112 }
31113
31114 /* OpenMP 3.1:
31115    # pragma omp taskyield new-line  */
31116
31117 static void
31118 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31119 {
31120   cp_parser_require_pragma_eol (parser, pragma_tok);
31121   finish_omp_taskyield ();
31122 }
31123
31124 /* OpenMP 4.0:
31125    # pragma omp taskgroup new-line
31126      structured-block  */
31127
31128 static tree
31129 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31130 {
31131   cp_parser_require_pragma_eol (parser, pragma_tok);
31132   return c_finish_omp_taskgroup (input_location,
31133                                  cp_parser_omp_structured_block (parser));
31134 }
31135
31136
31137 /* OpenMP 2.5:
31138    # pragma omp threadprivate (variable-list) */
31139
31140 static void
31141 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31142 {
31143   tree vars;
31144
31145   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31146   cp_parser_require_pragma_eol (parser, pragma_tok);
31147
31148   finish_omp_threadprivate (vars);
31149 }
31150
31151 /* OpenMP 4.0:
31152    # pragma omp cancel cancel-clause[optseq] new-line  */
31153
31154 #define OMP_CANCEL_CLAUSE_MASK                                  \
31155         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
31156         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
31157         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
31158         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)    \
31159         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31160
31161 static void
31162 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31163 {
31164   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31165                                             "#pragma omp cancel", pragma_tok);
31166   finish_omp_cancel (clauses);
31167 }
31168
31169 /* OpenMP 4.0:
31170    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
31171
31172 #define OMP_CANCELLATION_POINT_CLAUSE_MASK                      \
31173         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
31174         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
31175         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
31176         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31177
31178 static void
31179 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31180 {
31181   tree clauses;
31182   bool point_seen = false;
31183
31184   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31185     {
31186       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31187       const char *p = IDENTIFIER_POINTER (id);
31188
31189       if (strcmp (p, "point") == 0)
31190         {
31191           cp_lexer_consume_token (parser->lexer);
31192           point_seen = true;
31193         }
31194     }
31195   if (!point_seen)
31196     {
31197       cp_parser_error (parser, "expected %<point%>");
31198       cp_parser_require_pragma_eol (parser, pragma_tok);
31199       return;
31200     }
31201
31202   clauses = cp_parser_omp_all_clauses (parser,
31203                                        OMP_CANCELLATION_POINT_CLAUSE_MASK,
31204                                        "#pragma omp cancellation point",
31205                                        pragma_tok);
31206   finish_omp_cancellation_point (clauses);
31207 }
31208
31209 /* OpenMP 4.0:
31210    #pragma omp distribute distribute-clause[optseq] new-line
31211      for-loop  */
31212
31213 #define OMP_DISTRIBUTE_CLAUSE_MASK                              \
31214         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31215         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31216         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31217         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31218
31219 static tree
31220 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31221                           char *p_name, omp_clause_mask mask, tree *cclauses)
31222 {
31223   tree clauses, sb, ret;
31224   unsigned int save;
31225   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31226
31227   strcat (p_name, " distribute");
31228   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31229
31230   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31231     {
31232       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31233       const char *p = IDENTIFIER_POINTER (id);
31234       bool simd = false;
31235       bool parallel = false;
31236
31237       if (strcmp (p, "simd") == 0)
31238         simd = true;
31239       else
31240         parallel = strcmp (p, "parallel") == 0;
31241       if (parallel || simd)
31242         {
31243           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31244           if (cclauses == NULL)
31245             cclauses = cclauses_buf;
31246           cp_lexer_consume_token (parser->lexer);
31247           if (!flag_openmp)  /* flag_openmp_simd  */
31248             {
31249               if (simd)
31250                 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31251                                            cclauses);
31252               else
31253                 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31254                                                cclauses);
31255             }
31256           sb = begin_omp_structured_block ();
31257           save = cp_parser_begin_omp_structured_block (parser);
31258           if (simd)
31259             ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31260                                       cclauses);
31261           else
31262             ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31263                                           cclauses);
31264           cp_parser_end_omp_structured_block (parser, save);
31265           tree body = finish_omp_structured_block (sb);
31266           if (ret == NULL)
31267             return ret;
31268           ret = make_node (OMP_DISTRIBUTE);
31269           TREE_TYPE (ret) = void_type_node;
31270           OMP_FOR_BODY (ret) = body;
31271           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31272           SET_EXPR_LOCATION (ret, loc);
31273           add_stmt (ret);
31274           return ret;
31275         }
31276     }
31277   if (!flag_openmp)  /* flag_openmp_simd  */
31278     {
31279       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31280       return NULL_TREE;
31281     }
31282
31283   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31284                                        cclauses == NULL);
31285   if (cclauses)
31286     {
31287       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31288       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31289     }
31290
31291   sb = begin_omp_structured_block ();
31292   save = cp_parser_begin_omp_structured_block (parser);
31293
31294   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31295
31296   cp_parser_end_omp_structured_block (parser, save);
31297   add_stmt (finish_omp_structured_block (sb));
31298
31299   return ret;
31300 }
31301
31302 /* OpenMP 4.0:
31303    # pragma omp teams teams-clause[optseq] new-line
31304      structured-block  */
31305
31306 #define OMP_TEAMS_CLAUSE_MASK                                   \
31307         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31308         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31309         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
31310         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
31311         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)    \
31312         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31313         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31314
31315 static tree
31316 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31317                      char *p_name, omp_clause_mask mask, tree *cclauses)
31318 {
31319   tree clauses, sb, ret;
31320   unsigned int save;
31321   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31322
31323   strcat (p_name, " teams");
31324   mask |= OMP_TEAMS_CLAUSE_MASK;
31325
31326   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31327     {
31328       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31329       const char *p = IDENTIFIER_POINTER (id);
31330       if (strcmp (p, "distribute") == 0)
31331         {
31332           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31333           if (cclauses == NULL)
31334             cclauses = cclauses_buf;
31335
31336           cp_lexer_consume_token (parser->lexer);
31337           if (!flag_openmp)  /* flag_openmp_simd  */
31338             return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31339                                              cclauses);
31340           sb = begin_omp_structured_block ();
31341           save = cp_parser_begin_omp_structured_block (parser);
31342           ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31343                                           cclauses);
31344           cp_parser_end_omp_structured_block (parser, save);
31345           tree body = finish_omp_structured_block (sb);
31346           if (ret == NULL)
31347             return ret;
31348           clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31349           ret = make_node (OMP_TEAMS);
31350           TREE_TYPE (ret) = void_type_node;
31351           OMP_TEAMS_CLAUSES (ret) = clauses;
31352           OMP_TEAMS_BODY (ret) = body;
31353           OMP_TEAMS_COMBINED (ret) = 1;
31354           return add_stmt (ret);
31355         }
31356     }
31357   if (!flag_openmp)  /* flag_openmp_simd  */
31358     {
31359       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31360       return NULL_TREE;
31361     }
31362
31363   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31364                                        cclauses == NULL);
31365   if (cclauses)
31366     {
31367       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31368       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31369     }
31370
31371   tree stmt = make_node (OMP_TEAMS);
31372   TREE_TYPE (stmt) = void_type_node;
31373   OMP_TEAMS_CLAUSES (stmt) = clauses;
31374   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31375
31376   return add_stmt (stmt);
31377 }
31378
31379 /* OpenMP 4.0:
31380    # pragma omp target data target-data-clause[optseq] new-line
31381      structured-block  */
31382
31383 #define OMP_TARGET_DATA_CLAUSE_MASK                             \
31384         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31385         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
31386         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31387
31388 static tree
31389 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31390 {
31391   tree stmt = make_node (OMP_TARGET_DATA);
31392   TREE_TYPE (stmt) = void_type_node;
31393
31394   OMP_TARGET_DATA_CLAUSES (stmt)
31395     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31396                                  "#pragma omp target data", pragma_tok);
31397   keep_next_level (true);
31398   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31399
31400   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31401   return add_stmt (stmt);
31402 }
31403
31404 /* OpenMP 4.0:
31405    # pragma omp target update target-update-clause[optseq] new-line */
31406
31407 #define OMP_TARGET_UPDATE_CLAUSE_MASK                           \
31408         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)         \
31409         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
31410         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31411         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31412
31413 static bool
31414 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31415                              enum pragma_context context)
31416 {
31417   if (context == pragma_stmt)
31418     {
31419       error_at (pragma_tok->location,
31420                 "%<#pragma omp target update%> may only be "
31421                 "used in compound statements");
31422       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31423       return false;
31424     }
31425
31426   tree clauses
31427     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31428                                  "#pragma omp target update", pragma_tok);
31429   if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31430       && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31431     {
31432       error_at (pragma_tok->location,
31433                 "%<#pragma omp target update%> must contain at least one "
31434                 "%<from%> or %<to%> clauses");
31435       return false;
31436     }
31437
31438   tree stmt = make_node (OMP_TARGET_UPDATE);
31439   TREE_TYPE (stmt) = void_type_node;
31440   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31441   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31442   add_stmt (stmt);
31443   return false;
31444 }
31445
31446 /* OpenMP 4.0:
31447    # pragma omp target target-clause[optseq] new-line
31448      structured-block  */
31449
31450 #define OMP_TARGET_CLAUSE_MASK                                  \
31451         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31452         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
31453         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31454
31455 static bool
31456 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31457                       enum pragma_context context)
31458 {
31459   if (context != pragma_stmt && context != pragma_compound)
31460     {
31461       cp_parser_error (parser, "expected declaration specifiers");
31462       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31463       return false;
31464     }
31465
31466   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31467     {
31468       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31469       const char *p = IDENTIFIER_POINTER (id);
31470
31471       if (strcmp (p, "teams") == 0)
31472         {
31473           tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31474           char p_name[sizeof ("#pragma omp target teams distribute "
31475                               "parallel for simd")];
31476
31477           cp_lexer_consume_token (parser->lexer);
31478           strcpy (p_name, "#pragma omp target");
31479           if (!flag_openmp)  /* flag_openmp_simd  */
31480             {
31481               tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31482                                                OMP_TARGET_CLAUSE_MASK,
31483                                                cclauses);
31484               return stmt != NULL_TREE;
31485             }
31486           keep_next_level (true);
31487           tree sb = begin_omp_structured_block ();
31488           unsigned save = cp_parser_begin_omp_structured_block (parser);
31489           tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31490                                           OMP_TARGET_CLAUSE_MASK, cclauses);
31491           cp_parser_end_omp_structured_block (parser, save);
31492           tree body = finish_omp_structured_block (sb);
31493           if (ret == NULL_TREE)
31494             return false;
31495           tree stmt = make_node (OMP_TARGET);
31496           TREE_TYPE (stmt) = void_type_node;
31497           OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31498           OMP_TARGET_BODY (stmt) = body;
31499           add_stmt (stmt);
31500           return true;
31501         }
31502       else if (!flag_openmp)  /* flag_openmp_simd  */
31503         {
31504           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31505           return false;
31506         }
31507       else if (strcmp (p, "data") == 0)
31508         {
31509           cp_lexer_consume_token (parser->lexer);
31510           cp_parser_omp_target_data (parser, pragma_tok);
31511           return true;
31512         }
31513       else if (strcmp (p, "update") == 0)
31514         {
31515           cp_lexer_consume_token (parser->lexer);
31516           return cp_parser_omp_target_update (parser, pragma_tok, context);
31517         }
31518     }
31519
31520   tree stmt = make_node (OMP_TARGET);
31521   TREE_TYPE (stmt) = void_type_node;
31522
31523   OMP_TARGET_CLAUSES (stmt)
31524     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31525                                  "#pragma omp target", pragma_tok);
31526   keep_next_level (true);
31527   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31528
31529   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31530   add_stmt (stmt);
31531   return true;
31532 }
31533
31534 /* OpenACC 2.0:
31535    # pragma acc cache (variable-list) new-line
31536 */
31537
31538 static tree
31539 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31540 {
31541   tree stmt, clauses;
31542
31543   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31544   clauses = finish_omp_clauses (clauses);
31545
31546   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31547
31548   stmt = make_node (OACC_CACHE);
31549   TREE_TYPE (stmt) = void_type_node;
31550   OACC_CACHE_CLAUSES (stmt) = clauses;
31551   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31552   add_stmt (stmt);
31553
31554   return stmt;
31555 }
31556
31557 /* OpenACC 2.0:
31558    # pragma acc data oacc-data-clause[optseq] new-line
31559      structured-block  */
31560
31561 #define OACC_DATA_CLAUSE_MASK                                           \
31562         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31563         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31564         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31565         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31566         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31567         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31568         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31569         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31570         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31571         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31572         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31573
31574 static tree
31575 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31576 {
31577   tree stmt, clauses, block;
31578   unsigned int save;
31579
31580   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31581                                         "#pragma acc data", pragma_tok);
31582
31583   block = begin_omp_parallel ();
31584   save = cp_parser_begin_omp_structured_block (parser);
31585   cp_parser_statement (parser, NULL_TREE, false, NULL);
31586   cp_parser_end_omp_structured_block (parser, save);
31587   stmt = finish_oacc_data (clauses, block);
31588   return stmt;
31589 }
31590
31591 /* OpenACC 2.0:
31592    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31593
31594    or
31595
31596    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31597
31598    LOC is the location of the #pragma token.
31599 */
31600
31601 #define OACC_ENTER_DATA_CLAUSE_MASK                                     \
31602         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31603         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31604         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31605         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31606         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31607         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31608         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31609
31610 #define OACC_EXIT_DATA_CLAUSE_MASK                                      \
31611         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31612         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31613         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31614         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE)              \
31615         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31616
31617 static tree
31618 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31619                                 bool enter)
31620 {
31621   tree stmt, clauses;
31622
31623   if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31624      || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31625     {
31626       cp_parser_error (parser, enter
31627                        ? "expected %<data%> in %<#pragma acc enter data%>"
31628                        : "expected %<data%> in %<#pragma acc exit data%>");
31629       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31630       return NULL_TREE;
31631     }
31632
31633   const char *p =
31634     IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31635   if (strcmp (p, "data") != 0)
31636     {
31637       cp_parser_error (parser, "invalid pragma");
31638       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31639       return NULL_TREE;
31640     }
31641
31642   cp_lexer_consume_token (parser->lexer);
31643
31644   if (enter)
31645     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31646                                          "#pragma acc enter data", pragma_tok);
31647   else
31648     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31649                                          "#pragma acc exit data", pragma_tok);
31650
31651   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31652     {
31653       error_at (pragma_tok->location,
31654                 "%<#pragma acc enter data%> has no data movement clause");
31655       return NULL_TREE;
31656     }
31657
31658   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31659   TREE_TYPE (stmt) = void_type_node;
31660   if (enter)
31661     OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
31662   else
31663     OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
31664   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31665   add_stmt (stmt);
31666   return stmt;
31667 }
31668
31669 /* OpenACC 2.0:
31670    # pragma acc kernels oacc-kernels-clause[optseq] new-line
31671      structured-block  */
31672
31673 #define OACC_KERNELS_CLAUSE_MASK                                        \
31674         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31675         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31676         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31677         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31678         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31679         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31680         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31681         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31682         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31683         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31684         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31685         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31686         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31687
31688 static tree
31689 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31690 {
31691   tree stmt, clauses, block;
31692   unsigned int save;
31693
31694   clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31695                                         "#pragma acc kernels", pragma_tok);
31696
31697   block = begin_omp_parallel ();
31698   save = cp_parser_begin_omp_structured_block (parser);
31699   cp_parser_statement (parser, NULL_TREE, false, NULL);
31700   cp_parser_end_omp_structured_block (parser, save);
31701   stmt = finish_oacc_kernels (clauses, block);
31702   return stmt;
31703 }
31704
31705 /* OpenACC 2.0:
31706    # pragma acc loop oacc-loop-clause[optseq] new-line
31707      structured-block  */
31708
31709 #define OACC_LOOP_CLAUSE_MASK                                           \
31710         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)            \
31711         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31712
31713 static tree
31714 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31715 {
31716   tree stmt, clauses, block;
31717   int save;
31718
31719   clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31720                                         "#pragma acc loop", pragma_tok);
31721
31722   block = begin_omp_structured_block ();
31723   save = cp_parser_begin_omp_structured_block (parser);
31724   stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31725   cp_parser_end_omp_structured_block (parser, save);
31726   add_stmt (finish_omp_structured_block (block));
31727   return stmt;
31728 }
31729
31730 /* OpenACC 2.0:
31731    # pragma acc parallel oacc-parallel-clause[optseq] new-line
31732      structured-block  */
31733
31734 #define OACC_PARALLEL_CLAUSE_MASK                                       \
31735         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31736         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31737         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31738         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31739         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31740         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31741         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31742         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
31743         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
31744         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31745         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31746         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31747         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31748         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31749         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
31750         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
31751         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31752
31753 static tree
31754 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31755 {
31756   tree stmt, clauses, block;
31757   unsigned int save;
31758
31759   clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31760                                          "#pragma acc parallel", pragma_tok);
31761
31762   block = begin_omp_parallel ();
31763   save = cp_parser_begin_omp_structured_block (parser);
31764   cp_parser_statement (parser, NULL_TREE, false, NULL);
31765   cp_parser_end_omp_structured_block (parser, save);
31766   stmt = finish_oacc_parallel (clauses, block);
31767   return stmt;
31768 }
31769
31770 /* OpenACC 2.0:
31771    # pragma acc update oacc-update-clause[optseq] new-line
31772 */
31773
31774 #define OACC_UPDATE_CLAUSE_MASK                                         \
31775         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31776         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)              \
31777         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)                \
31778         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31779         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)                \
31780         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31781
31782 static tree
31783 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31784 {
31785   tree stmt, clauses;
31786
31787   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31788                                          "#pragma acc update", pragma_tok);
31789
31790   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31791     {
31792       error_at (pragma_tok->location,
31793                 "%<#pragma acc update%> must contain at least one "
31794                 "%<device%> or %<host/self%> clause");
31795       return NULL_TREE;
31796     }
31797
31798   stmt = make_node (OACC_UPDATE);
31799   TREE_TYPE (stmt) = void_type_node;
31800   OACC_UPDATE_CLAUSES (stmt) = clauses;
31801   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31802   add_stmt (stmt);
31803   return stmt;
31804 }
31805
31806 /* OpenACC 2.0:
31807    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31808
31809    LOC is the location of the #pragma token.
31810 */
31811
31812 #define OACC_WAIT_CLAUSE_MASK                                   \
31813         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31814
31815 static tree
31816 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31817 {
31818   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31819   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31820
31821   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31822     list = cp_parser_oacc_wait_list (parser, loc, list);
31823
31824   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31825                                         "#pragma acc wait", pragma_tok);
31826
31827   stmt = c_finish_oacc_wait (loc, list, clauses);
31828
31829   return stmt;
31830 }
31831
31832 /* OpenMP 4.0:
31833    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
31834
31835 #define OMP_DECLARE_SIMD_CLAUSE_MASK                            \
31836         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
31837         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
31838         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
31839         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)      \
31840         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)     \
31841         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31842
31843 static void
31844 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31845                             enum pragma_context context)
31846 {
31847   bool first_p = parser->omp_declare_simd == NULL;
31848   cp_omp_declare_simd_data data;
31849   if (first_p)
31850     {
31851       data.error_seen = false;
31852       data.fndecl_seen = false;
31853       data.tokens = vNULL;
31854       parser->omp_declare_simd = &data;
31855     }
31856   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31857          && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31858     cp_lexer_consume_token (parser->lexer);
31859   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31860     parser->omp_declare_simd->error_seen = true;
31861   cp_parser_require_pragma_eol (parser, pragma_tok);
31862   struct cp_token_cache *cp
31863     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31864   parser->omp_declare_simd->tokens.safe_push (cp);
31865   if (first_p)
31866     {
31867       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31868         cp_parser_pragma (parser, context);
31869       switch (context)
31870         {
31871         case pragma_external:
31872           cp_parser_declaration (parser);
31873           break;
31874         case pragma_member:
31875           cp_parser_member_declaration (parser);
31876           break;
31877         case pragma_objc_icode:
31878           cp_parser_block_declaration (parser, /*statement_p=*/false);
31879           break;
31880         default:
31881           cp_parser_declaration_statement (parser);
31882           break;
31883         }
31884       if (parser->omp_declare_simd
31885           && !parser->omp_declare_simd->error_seen
31886           && !parser->omp_declare_simd->fndecl_seen)
31887         error_at (pragma_tok->location,
31888                   "%<#pragma omp declare simd%> not immediately followed by "
31889                   "function declaration or definition");
31890       data.tokens.release ();
31891       parser->omp_declare_simd = NULL;
31892     }
31893 }
31894
31895 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.  
31896    This function is modelled similar to the late parsing of omp declare 
31897    simd.  */
31898
31899 static tree
31900 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31901 {
31902   struct cp_token_cache *ce;
31903   cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31904   int ii = 0;
31905
31906   if (parser->omp_declare_simd != NULL)
31907     {
31908       error ("%<#pragma omp declare simd%> cannot be used in the same function"
31909              " marked as a Cilk Plus SIMD-enabled function");
31910       XDELETE (parser->cilk_simd_fn_info);
31911       parser->cilk_simd_fn_info = NULL;
31912       return attrs;
31913     }
31914   if (!info->error_seen && info->fndecl_seen)
31915     {
31916       error ("vector attribute not immediately followed by a single function"
31917              " declaration or definition");
31918       info->error_seen = true;
31919     }
31920   if (info->error_seen)
31921     return attrs;
31922
31923   FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31924     {
31925       tree c, cl;
31926
31927       cp_parser_push_lexer_for_tokens (parser, ce);
31928       parser->lexer->in_pragma = true;
31929       cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31930                                       "SIMD-enabled functions attribute", 
31931                                       NULL);
31932       cp_parser_pop_lexer (parser);
31933       if (cl)
31934         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31935
31936       c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31937       TREE_CHAIN (c) = attrs;
31938       attrs = c;
31939
31940       c = build_tree_list (get_identifier ("omp declare simd"), cl);
31941       TREE_CHAIN (c) = attrs;
31942       if (processing_template_decl)
31943         ATTR_IS_DEPENDENT (c) = 1;
31944       attrs = c;
31945     }
31946   info->fndecl_seen = true;
31947   XDELETE (parser->cilk_simd_fn_info);
31948   parser->cilk_simd_fn_info = NULL;
31949   return attrs;
31950 }
31951
31952 /* Finalize #pragma omp declare simd clauses after direct declarator has
31953    been parsed, and put that into "omp declare simd" attribute.  */
31954
31955 static tree
31956 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31957 {
31958   struct cp_token_cache *ce;
31959   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31960   int i;
31961
31962   if (!data->error_seen && data->fndecl_seen)
31963     {
31964       error ("%<#pragma omp declare simd%> not immediately followed by "
31965              "a single function declaration or definition");
31966       data->error_seen = true;
31967       return attrs;
31968     }
31969   if (data->error_seen)
31970     return attrs;
31971
31972   FOR_EACH_VEC_ELT (data->tokens, i, ce)
31973     {
31974       tree c, cl;
31975
31976       cp_parser_push_lexer_for_tokens (parser, ce);
31977       parser->lexer->in_pragma = true;
31978       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31979       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31980       cp_lexer_consume_token (parser->lexer);
31981       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31982                                       "#pragma omp declare simd", pragma_tok);
31983       cp_parser_pop_lexer (parser);
31984       if (cl)
31985         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31986       c = build_tree_list (get_identifier ("omp declare simd"), cl);
31987       TREE_CHAIN (c) = attrs;
31988       if (processing_template_decl)
31989         ATTR_IS_DEPENDENT (c) = 1;
31990       attrs = c;
31991     }
31992
31993   data->fndecl_seen = true;
31994   return attrs;
31995 }
31996
31997
31998 /* OpenMP 4.0:
31999    # pragma omp declare target new-line
32000    declarations and definitions
32001    # pragma omp end declare target new-line  */
32002
32003 static void
32004 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
32005 {
32006   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32007   scope_chain->omp_declare_target_attribute++;
32008 }
32009
32010 static void
32011 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
32012 {
32013   const char *p = "";
32014   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32015     {
32016       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32017       p = IDENTIFIER_POINTER (id);
32018     }
32019   if (strcmp (p, "declare") == 0)
32020     {
32021       cp_lexer_consume_token (parser->lexer);
32022       p = "";
32023       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32024         {
32025           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32026           p = IDENTIFIER_POINTER (id);
32027         }
32028       if (strcmp (p, "target") == 0)
32029         cp_lexer_consume_token (parser->lexer);
32030       else
32031         {
32032           cp_parser_error (parser, "expected %<target%>");
32033           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32034           return;
32035         }
32036     }
32037   else
32038     {
32039       cp_parser_error (parser, "expected %<declare%>");
32040       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32041       return;
32042     }
32043   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32044   if (!scope_chain->omp_declare_target_attribute)
32045     error_at (pragma_tok->location,
32046               "%<#pragma omp end declare target%> without corresponding "
32047               "%<#pragma omp declare target%>");
32048   else
32049     scope_chain->omp_declare_target_attribute--;
32050 }
32051
32052 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
32053    expression and optional initializer clause of
32054    #pragma omp declare reduction.  We store the expression(s) as
32055    either 3, 6 or 7 special statements inside of the artificial function's
32056    body.  The first two statements are DECL_EXPRs for the artificial
32057    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32058    expression that uses those variables.
32059    If there was any INITIALIZER clause, this is followed by further statements,
32060    the fourth and fifth statements are DECL_EXPRs for the artificial
32061    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
32062    constructor variant (first token after open paren is not omp_priv),
32063    then the sixth statement is a statement with the function call expression
32064    that uses the OMP_PRIV and optionally OMP_ORIG variable.
32065    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32066    to initialize the OMP_PRIV artificial variable and there is seventh
32067    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
32068
32069 static bool
32070 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32071 {
32072   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32073   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32074   type = TREE_TYPE (type);
32075   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32076   DECL_ARTIFICIAL (omp_out) = 1;
32077   pushdecl (omp_out);
32078   add_decl_expr (omp_out);
32079   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32080   DECL_ARTIFICIAL (omp_in) = 1;
32081   pushdecl (omp_in);
32082   add_decl_expr (omp_in);
32083   tree combiner;
32084   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32085
32086   keep_next_level (true);
32087   tree block = begin_omp_structured_block ();
32088   combiner = cp_parser_expression (parser);
32089   finish_expr_stmt (combiner);
32090   block = finish_omp_structured_block (block);
32091   add_stmt (block);
32092
32093   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32094     return false;
32095
32096   const char *p = "";
32097   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32098     {
32099       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32100       p = IDENTIFIER_POINTER (id);
32101     }
32102
32103   if (strcmp (p, "initializer") == 0)
32104     {
32105       cp_lexer_consume_token (parser->lexer);
32106       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32107         return false;
32108
32109       p = "";
32110       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32111         {
32112           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32113           p = IDENTIFIER_POINTER (id);
32114         }
32115
32116       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32117       DECL_ARTIFICIAL (omp_priv) = 1;
32118       pushdecl (omp_priv);
32119       add_decl_expr (omp_priv);
32120       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32121       DECL_ARTIFICIAL (omp_orig) = 1;
32122       pushdecl (omp_orig);
32123       add_decl_expr (omp_orig);
32124
32125       keep_next_level (true);
32126       block = begin_omp_structured_block ();
32127
32128       bool ctor = false;
32129       if (strcmp (p, "omp_priv") == 0)
32130         {
32131           bool is_direct_init, is_non_constant_init;
32132           ctor = true;
32133           cp_lexer_consume_token (parser->lexer);
32134           /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
32135           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32136               || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32137                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32138                      == CPP_CLOSE_PAREN
32139                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32140                      == CPP_CLOSE_PAREN))
32141             {
32142               finish_omp_structured_block (block);
32143               error ("invalid initializer clause");
32144               return false;
32145             }
32146           initializer = cp_parser_initializer (parser, &is_direct_init,
32147                                                &is_non_constant_init);
32148           cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32149                           NULL_TREE, LOOKUP_ONLYCONVERTING);
32150         }
32151       else
32152         {
32153           cp_parser_parse_tentatively (parser);
32154           tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32155                                                   /*check_dependency_p=*/true,
32156                                                   /*template_p=*/NULL,
32157                                                   /*declarator_p=*/false,
32158                                                   /*optional_p=*/false);
32159           vec<tree, va_gc> *args;
32160           if (fn_name == error_mark_node
32161               || cp_parser_error_occurred (parser)
32162               || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32163               || ((args = cp_parser_parenthesized_expression_list
32164                                 (parser, non_attr, /*cast_p=*/false,
32165                                  /*allow_expansion_p=*/true,
32166                                  /*non_constant_p=*/NULL)),
32167                   cp_parser_error_occurred (parser)))
32168             {
32169               finish_omp_structured_block (block);
32170               cp_parser_abort_tentative_parse (parser);
32171               cp_parser_error (parser, "expected id-expression (arguments)");
32172               return false;
32173             }
32174           unsigned int i;
32175           tree arg;
32176           FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32177             if (arg == omp_priv
32178                 || (TREE_CODE (arg) == ADDR_EXPR
32179                     && TREE_OPERAND (arg, 0) == omp_priv))
32180               break;
32181           cp_parser_abort_tentative_parse (parser);
32182           if (arg == NULL_TREE)
32183             error ("one of the initializer call arguments should be %<omp_priv%>"
32184                    " or %<&omp_priv%>");
32185           initializer = cp_parser_postfix_expression (parser, false, false, false,
32186                                                       false, NULL);
32187           finish_expr_stmt (initializer);
32188         }
32189
32190       block = finish_omp_structured_block (block);
32191       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32192       add_stmt (block);
32193
32194       if (ctor)
32195         add_decl_expr (omp_orig);
32196
32197       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32198         return false;
32199     }
32200
32201   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32202     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32203
32204   return true;
32205 }
32206
32207 /* OpenMP 4.0
32208    #pragma omp declare reduction (reduction-id : typename-list : expression) \
32209       initializer-clause[opt] new-line
32210
32211    initializer-clause:
32212       initializer (omp_priv initializer)
32213       initializer (function-name (argument-list))  */
32214
32215 static void
32216 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32217                                  enum pragma_context)
32218 {
32219   auto_vec<tree> types;
32220   enum tree_code reduc_code = ERROR_MARK;
32221   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32222   unsigned int i;
32223   cp_token *first_token;
32224   cp_token_cache *cp;
32225   int errs;
32226   void *p;
32227     
32228   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
32229   p = obstack_alloc (&declarator_obstack, 0);
32230
32231   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32232     goto fail;
32233
32234   switch (cp_lexer_peek_token (parser->lexer)->type)
32235     {
32236     case CPP_PLUS:
32237       reduc_code = PLUS_EXPR;
32238       break;
32239     case CPP_MULT:
32240       reduc_code = MULT_EXPR;
32241       break;
32242     case CPP_MINUS:
32243       reduc_code = MINUS_EXPR;
32244       break;
32245     case CPP_AND:
32246       reduc_code = BIT_AND_EXPR;
32247       break;
32248     case CPP_XOR:
32249       reduc_code = BIT_XOR_EXPR;
32250       break;
32251     case CPP_OR:
32252       reduc_code = BIT_IOR_EXPR;
32253       break;
32254     case CPP_AND_AND:
32255       reduc_code = TRUTH_ANDIF_EXPR;
32256       break;
32257     case CPP_OR_OR:
32258       reduc_code = TRUTH_ORIF_EXPR;
32259       break;
32260     case CPP_NAME:
32261       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32262       break;
32263     default:
32264       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32265                                "%<|%>, %<&&%>, %<||%> or identifier");
32266       goto fail;
32267     }
32268
32269   if (reduc_code != ERROR_MARK)
32270     cp_lexer_consume_token (parser->lexer);
32271
32272   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32273   if (reduc_id == error_mark_node)
32274     goto fail;
32275
32276   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32277     goto fail;
32278
32279   /* Types may not be defined in declare reduction type list.  */
32280   const char *saved_message;
32281   saved_message = parser->type_definition_forbidden_message;
32282   parser->type_definition_forbidden_message
32283     = G_("types may not be defined in declare reduction type list");
32284   bool saved_colon_corrects_to_scope_p;
32285   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32286   parser->colon_corrects_to_scope_p = false;
32287   bool saved_colon_doesnt_start_class_def_p;
32288   saved_colon_doesnt_start_class_def_p
32289     = parser->colon_doesnt_start_class_def_p;
32290   parser->colon_doesnt_start_class_def_p = true;
32291
32292   while (true)
32293     {
32294       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32295       type = cp_parser_type_id (parser);
32296       if (type == error_mark_node)
32297         ;
32298       else if (ARITHMETIC_TYPE_P (type)
32299                && (orig_reduc_id == NULL_TREE
32300                    || (TREE_CODE (type) != COMPLEX_TYPE
32301                        && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32302                                    "min") == 0
32303                            || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32304                                       "max") == 0))))
32305         error_at (loc, "predeclared arithmetic type %qT in "
32306                        "%<#pragma omp declare reduction%>", type);
32307       else if (TREE_CODE (type) == FUNCTION_TYPE
32308                || TREE_CODE (type) == METHOD_TYPE
32309                || TREE_CODE (type) == ARRAY_TYPE)
32310         error_at (loc, "function or array type %qT in "
32311                        "%<#pragma omp declare reduction%>", type);
32312       else if (TREE_CODE (type) == REFERENCE_TYPE)
32313         error_at (loc, "reference type %qT in "
32314                        "%<#pragma omp declare reduction%>", type);
32315       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32316         error_at (loc, "const, volatile or __restrict qualified type %qT in "
32317                        "%<#pragma omp declare reduction%>", type);
32318       else
32319         types.safe_push (type);
32320
32321       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32322         cp_lexer_consume_token (parser->lexer);
32323       else
32324         break;
32325     }
32326
32327   /* Restore the saved message.  */
32328   parser->type_definition_forbidden_message = saved_message;
32329   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32330   parser->colon_doesnt_start_class_def_p
32331     = saved_colon_doesnt_start_class_def_p;
32332
32333   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32334       || types.is_empty ())
32335     {
32336      fail:
32337       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32338       goto done;
32339     }
32340
32341   first_token = cp_lexer_peek_token (parser->lexer);
32342   cp = NULL;
32343   errs = errorcount;
32344   FOR_EACH_VEC_ELT (types, i, type)
32345     {
32346       tree fntype
32347         = build_function_type_list (void_type_node,
32348                                     cp_build_reference_type (type, false),
32349                                     NULL_TREE);
32350       tree this_reduc_id = reduc_id;
32351       if (!dependent_type_p (type))
32352         this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32353       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32354       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32355       DECL_ARTIFICIAL (fndecl) = 1;
32356       DECL_EXTERNAL (fndecl) = 1;
32357       DECL_DECLARED_INLINE_P (fndecl) = 1;
32358       DECL_IGNORED_P (fndecl) = 1;
32359       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32360       DECL_ATTRIBUTES (fndecl)
32361         = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32362                      DECL_ATTRIBUTES (fndecl));
32363       if (processing_template_decl)
32364         fndecl = push_template_decl (fndecl);
32365       bool block_scope = false;
32366       tree block = NULL_TREE;
32367       if (current_function_decl)
32368         {
32369           block_scope = true;
32370           DECL_CONTEXT (fndecl) = global_namespace;
32371           if (!processing_template_decl)
32372             pushdecl (fndecl);
32373         }
32374       else if (current_class_type)
32375         {
32376           if (cp == NULL)
32377             {
32378               while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32379                      && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32380                 cp_lexer_consume_token (parser->lexer);
32381               if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32382                 goto fail;
32383               cp = cp_token_cache_new (first_token,
32384                                        cp_lexer_peek_nth_token (parser->lexer,
32385                                                                 2));
32386             }
32387           DECL_STATIC_FUNCTION_P (fndecl) = 1;
32388           finish_member_declaration (fndecl);
32389           DECL_PENDING_INLINE_INFO (fndecl) = cp;
32390           DECL_PENDING_INLINE_P (fndecl) = 1;
32391           vec_safe_push (unparsed_funs_with_definitions, fndecl);
32392           continue;
32393         }
32394       else
32395         {
32396           DECL_CONTEXT (fndecl) = current_namespace;
32397           pushdecl (fndecl);
32398         }
32399       if (!block_scope)
32400         start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32401       else
32402         block = begin_omp_structured_block ();
32403       if (cp)
32404         {
32405           cp_parser_push_lexer_for_tokens (parser, cp);
32406           parser->lexer->in_pragma = true;
32407         }
32408       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32409         {
32410           if (!block_scope)
32411             finish_function (0);
32412           else
32413             DECL_CONTEXT (fndecl) = current_function_decl;
32414           if (cp)
32415             cp_parser_pop_lexer (parser);
32416           goto fail;
32417         }
32418       if (cp)
32419         cp_parser_pop_lexer (parser);
32420       if (!block_scope)
32421         finish_function (0);
32422       else
32423         {
32424           DECL_CONTEXT (fndecl) = current_function_decl;
32425           block = finish_omp_structured_block (block);
32426           if (TREE_CODE (block) == BIND_EXPR)
32427             DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32428           else if (TREE_CODE (block) == STATEMENT_LIST)
32429             DECL_SAVED_TREE (fndecl) = block;
32430           if (processing_template_decl)
32431             add_decl_expr (fndecl);
32432         }
32433       cp_check_omp_declare_reduction (fndecl);
32434       if (cp == NULL && types.length () > 1)
32435         cp = cp_token_cache_new (first_token,
32436                                  cp_lexer_peek_nth_token (parser->lexer, 2));
32437       if (errs != errorcount)
32438         break;
32439     }
32440
32441   cp_parser_require_pragma_eol (parser, pragma_tok);
32442
32443  done:
32444   /* Free any declarators allocated.  */
32445   obstack_free (&declarator_obstack, p);
32446 }
32447
32448 /* OpenMP 4.0
32449    #pragma omp declare simd declare-simd-clauses[optseq] new-line
32450    #pragma omp declare reduction (reduction-id : typename-list : expression) \
32451       initializer-clause[opt] new-line
32452    #pragma omp declare target new-line  */
32453
32454 static void
32455 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32456                        enum pragma_context context)
32457 {
32458   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32459     {
32460       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32461       const char *p = IDENTIFIER_POINTER (id);
32462
32463       if (strcmp (p, "simd") == 0)
32464         {
32465           cp_lexer_consume_token (parser->lexer);
32466           cp_parser_omp_declare_simd (parser, pragma_tok,
32467                                       context);
32468           return;
32469         }
32470       cp_ensure_no_omp_declare_simd (parser);
32471       if (strcmp (p, "reduction") == 0)
32472         {
32473           cp_lexer_consume_token (parser->lexer);
32474           cp_parser_omp_declare_reduction (parser, pragma_tok,
32475                                            context);
32476           return;
32477         }
32478       if (!flag_openmp)  /* flag_openmp_simd  */
32479         {
32480           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32481           return;
32482         }
32483       if (strcmp (p, "target") == 0)
32484         {
32485           cp_lexer_consume_token (parser->lexer);
32486           cp_parser_omp_declare_target (parser, pragma_tok);
32487           return;
32488         }
32489     }
32490   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32491                            "or %<target%>");
32492   cp_parser_require_pragma_eol (parser, pragma_tok);
32493 }
32494
32495 /* Main entry point to OpenMP statement pragmas.  */
32496
32497 static void
32498 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32499 {
32500   tree stmt;
32501   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32502   omp_clause_mask mask (0);
32503
32504   switch (pragma_tok->pragma_kind)
32505     {
32506     case PRAGMA_OACC_CACHE:
32507       stmt = cp_parser_oacc_cache (parser, pragma_tok);
32508       break;
32509     case PRAGMA_OACC_DATA:
32510       stmt = cp_parser_oacc_data (parser, pragma_tok);
32511       break;
32512     case PRAGMA_OACC_ENTER_DATA:
32513       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32514       break;
32515     case PRAGMA_OACC_EXIT_DATA:
32516       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32517       break;
32518     case PRAGMA_OACC_KERNELS:
32519       stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32520       break;
32521     case PRAGMA_OACC_LOOP:
32522       stmt = cp_parser_oacc_loop (parser, pragma_tok);
32523       break;
32524     case PRAGMA_OACC_PARALLEL:
32525       stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32526       break;
32527     case PRAGMA_OACC_UPDATE:
32528       stmt = cp_parser_oacc_update (parser, pragma_tok);
32529       break;
32530     case PRAGMA_OACC_WAIT:
32531       stmt = cp_parser_oacc_wait (parser, pragma_tok);
32532       break;
32533     case PRAGMA_OMP_ATOMIC:
32534       cp_parser_omp_atomic (parser, pragma_tok);
32535       return;
32536     case PRAGMA_OMP_CRITICAL:
32537       stmt = cp_parser_omp_critical (parser, pragma_tok);
32538       break;
32539     case PRAGMA_OMP_DISTRIBUTE:
32540       strcpy (p_name, "#pragma omp");
32541       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32542       break;
32543     case PRAGMA_OMP_FOR:
32544       strcpy (p_name, "#pragma omp");
32545       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32546       break;
32547     case PRAGMA_OMP_MASTER:
32548       stmt = cp_parser_omp_master (parser, pragma_tok);
32549       break;
32550     case PRAGMA_OMP_ORDERED:
32551       stmt = cp_parser_omp_ordered (parser, pragma_tok);
32552       break;
32553     case PRAGMA_OMP_PARALLEL:
32554       strcpy (p_name, "#pragma omp");
32555       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32556       break;
32557     case PRAGMA_OMP_SECTIONS:
32558       strcpy (p_name, "#pragma omp");
32559       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32560       break;
32561     case PRAGMA_OMP_SIMD:
32562       strcpy (p_name, "#pragma omp");
32563       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32564       break;
32565     case PRAGMA_OMP_SINGLE:
32566       stmt = cp_parser_omp_single (parser, pragma_tok);
32567       break;
32568     case PRAGMA_OMP_TASK:
32569       stmt = cp_parser_omp_task (parser, pragma_tok);
32570       break;
32571     case PRAGMA_OMP_TASKGROUP:
32572       stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32573       break;
32574     case PRAGMA_OMP_TEAMS:
32575       strcpy (p_name, "#pragma omp");
32576       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32577       break;
32578     default:
32579       gcc_unreachable ();
32580     }
32581
32582   if (stmt)
32583     SET_EXPR_LOCATION (stmt, pragma_tok->location);
32584 }
32585 \f
32586 /* Transactional Memory parsing routines.  */
32587
32588 /* Parse a transaction attribute.
32589
32590    txn-attribute:
32591         attribute
32592         [ [ identifier ] ]
32593
32594    ??? Simplify this when C++0x bracket attributes are
32595    implemented properly.  */
32596
32597 static tree
32598 cp_parser_txn_attribute_opt (cp_parser *parser)
32599 {
32600   cp_token *token;
32601   tree attr_name, attr = NULL;
32602
32603   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32604     return cp_parser_attributes_opt (parser);
32605
32606   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32607     return NULL_TREE;
32608   cp_lexer_consume_token (parser->lexer);
32609   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32610     goto error1;
32611
32612   token = cp_lexer_peek_token (parser->lexer);
32613   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32614     {
32615       token = cp_lexer_consume_token (parser->lexer);
32616
32617       attr_name = (token->type == CPP_KEYWORD
32618                    /* For keywords, use the canonical spelling,
32619                       not the parsed identifier.  */
32620                    ? ridpointers[(int) token->keyword]
32621                    : token->u.value);
32622       attr = build_tree_list (attr_name, NULL_TREE);
32623     }
32624   else
32625     cp_parser_error (parser, "expected identifier");
32626
32627   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32628  error1:
32629   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32630   return attr;
32631 }
32632
32633 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32634
32635    transaction-statement:
32636      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32637        compound-statement
32638      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32639 */
32640
32641 static tree
32642 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32643 {
32644   unsigned char old_in = parser->in_transaction;
32645   unsigned char this_in = 1, new_in;
32646   cp_token *token;
32647   tree stmt, attrs, noex;
32648
32649   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32650       || keyword == RID_TRANSACTION_RELAXED);
32651   token = cp_parser_require_keyword (parser, keyword,
32652       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32653           : RT_TRANSACTION_RELAXED));
32654   gcc_assert (token != NULL);
32655
32656   if (keyword == RID_TRANSACTION_RELAXED)
32657     this_in |= TM_STMT_ATTR_RELAXED;
32658   else
32659     {
32660       attrs = cp_parser_txn_attribute_opt (parser);
32661       if (attrs)
32662         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32663     }
32664
32665   /* Parse a noexcept specification.  */
32666   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32667
32668   /* Keep track if we're in the lexical scope of an outer transaction.  */
32669   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32670
32671   stmt = begin_transaction_stmt (token->location, NULL, this_in);
32672
32673   parser->in_transaction = new_in;
32674   cp_parser_compound_statement (parser, NULL, false, false);
32675   parser->in_transaction = old_in;
32676
32677   finish_transaction_stmt (stmt, NULL, this_in, noex);
32678
32679   return stmt;
32680 }
32681
32682 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32683
32684    transaction-expression:
32685      __transaction_atomic txn-noexcept-spec[opt] ( expression )
32686      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32687 */
32688
32689 static tree
32690 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32691 {
32692   unsigned char old_in = parser->in_transaction;
32693   unsigned char this_in = 1;
32694   cp_token *token;
32695   tree expr, noex;
32696   bool noex_expr;
32697
32698   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32699       || keyword == RID_TRANSACTION_RELAXED);
32700
32701   if (!flag_tm)
32702     error (keyword == RID_TRANSACTION_RELAXED
32703            ? G_("%<__transaction_relaxed%> without transactional memory "
32704                 "support enabled")
32705            : G_("%<__transaction_atomic%> without transactional memory "
32706                 "support enabled"));
32707
32708   token = cp_parser_require_keyword (parser, keyword,
32709       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32710           : RT_TRANSACTION_RELAXED));
32711   gcc_assert (token != NULL);
32712
32713   if (keyword == RID_TRANSACTION_RELAXED)
32714     this_in |= TM_STMT_ATTR_RELAXED;
32715
32716   /* Set this early.  This might mean that we allow transaction_cancel in
32717      an expression that we find out later actually has to be a constexpr.
32718      However, we expect that cxx_constant_value will be able to deal with
32719      this; also, if the noexcept has no constexpr, then what we parse next
32720      really is a transaction's body.  */
32721   parser->in_transaction = this_in;
32722
32723   /* Parse a noexcept specification.  */
32724   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32725                                                true);
32726
32727   if (!noex || !noex_expr
32728       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32729     {
32730       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32731
32732       expr = cp_parser_expression (parser);
32733       expr = finish_parenthesized_expr (expr);
32734
32735       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32736     }
32737   else
32738     {
32739       /* The only expression that is available got parsed for the noexcept
32740          already.  noexcept is true then.  */
32741       expr = noex;
32742       noex = boolean_true_node;
32743     }
32744
32745   expr = build_transaction_expr (token->location, expr, this_in, noex);
32746   parser->in_transaction = old_in;
32747
32748   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32749     return error_mark_node;
32750
32751   return (flag_tm ? expr : error_mark_node);
32752 }
32753
32754 /* Parse a function-transaction-block.
32755
32756    function-transaction-block:
32757      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32758          function-body
32759      __transaction_atomic txn-attribute[opt] function-try-block
32760      __transaction_relaxed ctor-initializer[opt] function-body
32761      __transaction_relaxed function-try-block
32762 */
32763
32764 static bool
32765 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32766 {
32767   unsigned char old_in = parser->in_transaction;
32768   unsigned char new_in = 1;
32769   tree compound_stmt, stmt, attrs;
32770   bool ctor_initializer_p;
32771   cp_token *token;
32772
32773   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32774       || keyword == RID_TRANSACTION_RELAXED);
32775   token = cp_parser_require_keyword (parser, keyword,
32776       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32777           : RT_TRANSACTION_RELAXED));
32778   gcc_assert (token != NULL);
32779
32780   if (keyword == RID_TRANSACTION_RELAXED)
32781     new_in |= TM_STMT_ATTR_RELAXED;
32782   else
32783     {
32784       attrs = cp_parser_txn_attribute_opt (parser);
32785       if (attrs)
32786         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32787     }
32788
32789   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32790
32791   parser->in_transaction = new_in;
32792
32793   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32794     ctor_initializer_p = cp_parser_function_try_block (parser);
32795   else
32796     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32797       (parser, /*in_function_try_block=*/false);
32798
32799   parser->in_transaction = old_in;
32800
32801   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32802
32803   return ctor_initializer_p;
32804 }
32805
32806 /* Parse a __transaction_cancel statement.
32807
32808    cancel-statement:
32809      __transaction_cancel txn-attribute[opt] ;
32810      __transaction_cancel txn-attribute[opt] throw-expression ;
32811
32812    ??? Cancel and throw is not yet implemented.  */
32813
32814 static tree
32815 cp_parser_transaction_cancel (cp_parser *parser)
32816 {
32817   cp_token *token;
32818   bool is_outer = false;
32819   tree stmt, attrs;
32820
32821   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32822                                      RT_TRANSACTION_CANCEL);
32823   gcc_assert (token != NULL);
32824
32825   attrs = cp_parser_txn_attribute_opt (parser);
32826   if (attrs)
32827     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32828
32829   /* ??? Parse cancel-and-throw here.  */
32830
32831   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32832
32833   if (!flag_tm)
32834     {
32835       error_at (token->location, "%<__transaction_cancel%> without "
32836                 "transactional memory support enabled");
32837       return error_mark_node;
32838     }
32839   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32840     {
32841       error_at (token->location, "%<__transaction_cancel%> within a "
32842                 "%<__transaction_relaxed%>");
32843       return error_mark_node;
32844     }
32845   else if (is_outer)
32846     {
32847       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32848           && !is_tm_may_cancel_outer (current_function_decl))
32849         {
32850           error_at (token->location, "outer %<__transaction_cancel%> not "
32851                     "within outer %<__transaction_atomic%>");
32852           error_at (token->location,
32853                     "  or a %<transaction_may_cancel_outer%> function");
32854           return error_mark_node;
32855         }
32856     }
32857   else if (parser->in_transaction == 0)
32858     {
32859       error_at (token->location, "%<__transaction_cancel%> not within "
32860                 "%<__transaction_atomic%>");
32861       return error_mark_node;
32862     }
32863
32864   stmt = build_tm_abort_call (token->location, is_outer);
32865   add_stmt (stmt);
32866
32867   return stmt;
32868 }
32869 \f
32870 /* The parser.  */
32871
32872 static GTY (()) cp_parser *the_parser;
32873
32874 \f
32875 /* Special handling for the first token or line in the file.  The first
32876    thing in the file might be #pragma GCC pch_preprocess, which loads a
32877    PCH file, which is a GC collection point.  So we need to handle this
32878    first pragma without benefit of an existing lexer structure.
32879
32880    Always returns one token to the caller in *FIRST_TOKEN.  This is
32881    either the true first token of the file, or the first token after
32882    the initial pragma.  */
32883
32884 static void
32885 cp_parser_initial_pragma (cp_token *first_token)
32886 {
32887   tree name = NULL;
32888
32889   cp_lexer_get_preprocessor_token (NULL, first_token);
32890   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32891     return;
32892
32893   cp_lexer_get_preprocessor_token (NULL, first_token);
32894   if (first_token->type == CPP_STRING)
32895     {
32896       name = first_token->u.value;
32897
32898       cp_lexer_get_preprocessor_token (NULL, first_token);
32899       if (first_token->type != CPP_PRAGMA_EOL)
32900         error_at (first_token->location,
32901                   "junk at end of %<#pragma GCC pch_preprocess%>");
32902     }
32903   else
32904     error_at (first_token->location, "expected string literal");
32905
32906   /* Skip to the end of the pragma.  */
32907   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32908     cp_lexer_get_preprocessor_token (NULL, first_token);
32909
32910   /* Now actually load the PCH file.  */
32911   if (name)
32912     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32913
32914   /* Read one more token to return to our caller.  We have to do this
32915      after reading the PCH file in, since its pointers have to be
32916      live.  */
32917   cp_lexer_get_preprocessor_token (NULL, first_token);
32918 }
32919
32920 /* Parses the grainsize pragma for the _Cilk_for statement.
32921    Syntax:
32922    #pragma cilk grainsize = <VALUE>.  */
32923
32924 static void
32925 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32926 {
32927   if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32928     {
32929       tree exp = cp_parser_binary_expression (parser, false, false,
32930                                               PREC_NOT_OPERATOR, NULL);
32931       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32932       if (!exp || exp == error_mark_node)
32933         {
32934           error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32935           return;
32936         }
32937
32938       /* Make sure the next token is _Cilk_for, it is invalid otherwise.  */
32939       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32940         cp_parser_cilk_for (parser, exp);
32941       else
32942         warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32943                     "%<#pragma cilk grainsize%> is not followed by "
32944                     "%<_Cilk_for%>");
32945       return;
32946     }
32947   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32948 }
32949
32950 /* Normal parsing of a pragma token.  Here we can (and must) use the
32951    regular lexer.  */
32952
32953 static bool
32954 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32955 {
32956   cp_token *pragma_tok;
32957   unsigned int id;
32958
32959   pragma_tok = cp_lexer_consume_token (parser->lexer);
32960   gcc_assert (pragma_tok->type == CPP_PRAGMA);
32961   parser->lexer->in_pragma = true;
32962
32963   id = pragma_tok->pragma_kind;
32964   if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32965     cp_ensure_no_omp_declare_simd (parser);
32966   switch (id)
32967     {
32968     case PRAGMA_GCC_PCH_PREPROCESS:
32969       error_at (pragma_tok->location,
32970                 "%<#pragma GCC pch_preprocess%> must be first");
32971       break;
32972
32973     case PRAGMA_OMP_BARRIER:
32974       switch (context)
32975         {
32976         case pragma_compound:
32977           cp_parser_omp_barrier (parser, pragma_tok);
32978           return false;
32979         case pragma_stmt:
32980           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32981                     "used in compound statements");
32982           break;
32983         default:
32984           goto bad_stmt;
32985         }
32986       break;
32987
32988     case PRAGMA_OMP_FLUSH:
32989       switch (context)
32990         {
32991         case pragma_compound:
32992           cp_parser_omp_flush (parser, pragma_tok);
32993           return false;
32994         case pragma_stmt:
32995           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32996                     "used in compound statements");
32997           break;
32998         default:
32999           goto bad_stmt;
33000         }
33001       break;
33002
33003     case PRAGMA_OMP_TASKWAIT:
33004       switch (context)
33005         {
33006         case pragma_compound:
33007           cp_parser_omp_taskwait (parser, pragma_tok);
33008           return false;
33009         case pragma_stmt:
33010           error_at (pragma_tok->location,
33011                     "%<#pragma omp taskwait%> may only be "
33012                     "used in compound statements");
33013           break;
33014         default:
33015           goto bad_stmt;
33016         }
33017       break;
33018
33019     case PRAGMA_OMP_TASKYIELD:
33020       switch (context)
33021         {
33022         case pragma_compound:
33023           cp_parser_omp_taskyield (parser, pragma_tok);
33024           return false;
33025         case pragma_stmt:
33026           error_at (pragma_tok->location,
33027                     "%<#pragma omp taskyield%> may only be "
33028                     "used in compound statements");
33029           break;
33030         default:
33031           goto bad_stmt;
33032         }
33033       break;
33034
33035     case PRAGMA_OMP_CANCEL:
33036       switch (context)
33037         {
33038         case pragma_compound:
33039           cp_parser_omp_cancel (parser, pragma_tok);
33040           return false;
33041         case pragma_stmt:
33042           error_at (pragma_tok->location,
33043                     "%<#pragma omp cancel%> may only be "
33044                     "used in compound statements");
33045           break;
33046         default:
33047           goto bad_stmt;
33048         }
33049       break;
33050
33051     case PRAGMA_OMP_CANCELLATION_POINT:
33052       switch (context)
33053         {
33054         case pragma_compound:
33055           cp_parser_omp_cancellation_point (parser, pragma_tok);
33056           return false;
33057         case pragma_stmt:
33058           error_at (pragma_tok->location,
33059                     "%<#pragma omp cancellation point%> may only be "
33060                     "used in compound statements");
33061           break;
33062         default:
33063           goto bad_stmt;
33064         }
33065       break;
33066
33067     case PRAGMA_OMP_THREADPRIVATE:
33068       cp_parser_omp_threadprivate (parser, pragma_tok);
33069       return false;
33070
33071     case PRAGMA_OMP_DECLARE_REDUCTION:
33072       cp_parser_omp_declare (parser, pragma_tok, context);
33073       return false;
33074
33075     case PRAGMA_OACC_CACHE:
33076     case PRAGMA_OACC_DATA:
33077     case PRAGMA_OACC_ENTER_DATA:
33078     case PRAGMA_OACC_EXIT_DATA:
33079     case PRAGMA_OACC_KERNELS:
33080     case PRAGMA_OACC_PARALLEL:
33081     case PRAGMA_OACC_LOOP:
33082     case PRAGMA_OACC_UPDATE:
33083     case PRAGMA_OACC_WAIT:
33084     case PRAGMA_OMP_ATOMIC:
33085     case PRAGMA_OMP_CRITICAL:
33086     case PRAGMA_OMP_DISTRIBUTE:
33087     case PRAGMA_OMP_FOR:
33088     case PRAGMA_OMP_MASTER:
33089     case PRAGMA_OMP_ORDERED:
33090     case PRAGMA_OMP_PARALLEL:
33091     case PRAGMA_OMP_SECTIONS:
33092     case PRAGMA_OMP_SIMD:
33093     case PRAGMA_OMP_SINGLE:
33094     case PRAGMA_OMP_TASK:
33095     case PRAGMA_OMP_TASKGROUP:
33096     case PRAGMA_OMP_TEAMS:
33097       if (context != pragma_stmt && context != pragma_compound)
33098         goto bad_stmt;
33099       cp_parser_omp_construct (parser, pragma_tok);
33100       return true;
33101
33102     case PRAGMA_OMP_TARGET:
33103       return cp_parser_omp_target (parser, pragma_tok, context);
33104
33105     case PRAGMA_OMP_END_DECLARE_TARGET:
33106       cp_parser_omp_end_declare_target (parser, pragma_tok);
33107       return false;
33108
33109     case PRAGMA_OMP_SECTION:
33110       error_at (pragma_tok->location, 
33111                 "%<#pragma omp section%> may only be used in "
33112                 "%<#pragma omp sections%> construct");
33113       break;
33114
33115     case PRAGMA_IVDEP:
33116       {
33117         if (context == pragma_external)
33118           {
33119             error_at (pragma_tok->location,
33120                       "%<#pragma GCC ivdep%> must be inside a function");
33121             break;
33122           }
33123         cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33124         cp_token *tok;
33125         tok = cp_lexer_peek_token (the_parser->lexer);
33126         if (tok->type != CPP_KEYWORD
33127             || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33128                 && tok->keyword != RID_DO))
33129           {
33130             cp_parser_error (parser, "for, while or do statement expected");
33131             return false;
33132           }
33133         cp_parser_iteration_statement (parser, true);
33134         return true;
33135       }
33136
33137     case PRAGMA_CILK_SIMD:
33138       if (context == pragma_external)
33139         {
33140           error_at (pragma_tok->location,
33141                     "%<#pragma simd%> must be inside a function");
33142           break;
33143         }
33144       cp_parser_cilk_simd (parser, pragma_tok);
33145       return true;
33146
33147     case PRAGMA_CILK_GRAINSIZE:
33148       if (context == pragma_external)
33149         {
33150           error_at (pragma_tok->location,
33151                     "%<#pragma cilk grainsize%> must be inside a function");
33152           break;
33153         }
33154
33155       /* Ignore the pragma if Cilk Plus is not enabled.  */
33156       if (flag_cilkplus)
33157         {
33158           cp_parser_cilk_grainsize (parser, pragma_tok);
33159           return true;
33160         }
33161       else
33162         {
33163           error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33164                     "%<#pragma cilk grainsize%>");
33165           break;
33166         }
33167
33168     default:
33169       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33170       c_invoke_pragma_handler (id);
33171       break;
33172
33173     bad_stmt:
33174       cp_parser_error (parser, "expected declaration specifiers");
33175       break;
33176     }
33177
33178   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33179   return false;
33180 }
33181
33182 /* The interface the pragma parsers have to the lexer.  */
33183
33184 enum cpp_ttype
33185 pragma_lex (tree *value)
33186 {
33187   cp_token *tok;
33188   enum cpp_ttype ret;
33189
33190   tok = cp_lexer_peek_token (the_parser->lexer);
33191
33192   ret = tok->type;
33193   *value = tok->u.value;
33194
33195   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33196     ret = CPP_EOF;
33197   else if (ret == CPP_STRING)
33198     *value = cp_parser_string_literal (the_parser, false, false);
33199   else
33200     {
33201       cp_lexer_consume_token (the_parser->lexer);
33202       if (ret == CPP_KEYWORD)
33203         ret = CPP_NAME;
33204     }
33205
33206   return ret;
33207 }
33208
33209 \f
33210 /* External interface.  */
33211
33212 /* Parse one entire translation unit.  */
33213
33214 void
33215 c_parse_file (void)
33216 {
33217   static bool already_called = false;
33218
33219   if (already_called)
33220     fatal_error (input_location,
33221                  "inter-module optimizations not implemented for C++");
33222   already_called = true;
33223
33224   the_parser = cp_parser_new ();
33225   push_deferring_access_checks (flag_access_control
33226                                 ? dk_no_deferred : dk_no_check);
33227   cp_parser_translation_unit (the_parser);
33228   the_parser = NULL;
33229 }
33230
33231 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's 
33232    vectorlength clause:
33233    Syntax:
33234    vectorlength ( constant-expression )  */
33235
33236 static tree
33237 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33238                                   bool is_simd_fn)
33239 {
33240   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33241   tree expr;
33242   /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33243      safelen clause.  Thus, vectorlength is represented as OMP 4.0
33244      safelen.  For SIMD-enabled function it is represented by OMP 4.0
33245      simdlen.  */
33246   if (!is_simd_fn)
33247     check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", 
33248                                loc);
33249   else
33250     check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33251                                loc);
33252
33253   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33254     return error_mark_node;
33255
33256   expr = cp_parser_constant_expression (parser);
33257   expr = maybe_constant_value (expr);
33258
33259   /* If expr == error_mark_node, then don't emit any errors nor
33260      create a clause.  if any of the above functions returns
33261      error mark node then they would have emitted an error message.  */
33262   if (expr == error_mark_node)
33263     ;
33264   else if (!TREE_TYPE (expr)
33265            || !TREE_CONSTANT (expr)
33266            || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33267     error_at (loc, "vectorlength must be an integer constant");
33268   else if (TREE_CONSTANT (expr)
33269            && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33270     error_at (loc, "vectorlength must be a power of 2");
33271   else 
33272     {
33273       tree c;
33274       if (!is_simd_fn)
33275         { 
33276           c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN); 
33277           OMP_CLAUSE_SAFELEN_EXPR (c) = expr; 
33278           OMP_CLAUSE_CHAIN (c) = clauses; 
33279           clauses = c;
33280         }
33281       else
33282         {
33283           c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33284           OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33285           OMP_CLAUSE_CHAIN (c) = clauses;
33286           clauses = c;
33287         }
33288     }
33289
33290   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33291     return error_mark_node;
33292   return clauses;
33293 }
33294
33295 /* Handles the Cilk Plus #pragma simd linear clause.
33296    Syntax:
33297    linear ( simd-linear-variable-list )
33298
33299    simd-linear-variable-list:
33300      simd-linear-variable
33301      simd-linear-variable-list , simd-linear-variable
33302
33303    simd-linear-variable:
33304      id-expression
33305      id-expression : simd-linear-step
33306
33307    simd-linear-step:
33308    conditional-expression */
33309
33310 static tree
33311 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33312 {
33313   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33314
33315   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33316     return clauses;
33317   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33318     {
33319       cp_parser_error (parser, "expected identifier");
33320       cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33321       return error_mark_node;
33322     }
33323
33324   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33325   parser->colon_corrects_to_scope_p = false;
33326   while (1)
33327     {
33328       cp_token *token = cp_lexer_peek_token (parser->lexer);
33329       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33330         {
33331           cp_parser_error (parser, "expected variable-name");
33332           clauses = error_mark_node;
33333           break;
33334         }
33335
33336       tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33337                                                false, false);
33338       tree decl = cp_parser_lookup_name_simple (parser, var_name,
33339                                                 token->location);
33340       if (decl == error_mark_node)
33341         {
33342           cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33343                                        token->location);
33344           clauses = error_mark_node;
33345         }
33346       else
33347         {
33348           tree e = NULL_TREE;
33349           tree step_size = integer_one_node;
33350
33351           /* If present, parse the linear step.  Otherwise, assume the default
33352              value of 1.  */
33353           if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33354             {
33355               cp_lexer_consume_token (parser->lexer);
33356
33357               e = cp_parser_assignment_expression (parser);
33358               e = maybe_constant_value (e);
33359
33360               if (e == error_mark_node)
33361                 {
33362                   /* If an error has occurred,  then the whole pragma is
33363                      considered ill-formed.  Thus, no reason to keep
33364                      parsing.  */
33365                   clauses = error_mark_node;
33366                   break;
33367                 }
33368               else if (type_dependent_expression_p (e)
33369                        || value_dependent_expression_p (e)
33370                        || (TREE_TYPE (e)
33371                            && INTEGRAL_TYPE_P (TREE_TYPE (e))
33372                            && (TREE_CONSTANT (e)
33373                                || DECL_P (e))))
33374                 step_size = e;
33375               else
33376                 cp_parser_error (parser,
33377                                  "step size must be an integer constant "
33378                                  "expression or an integer variable");
33379             }
33380
33381           /* Use the OMP_CLAUSE_LINEAR,  which has the same semantics.  */
33382           tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33383           OMP_CLAUSE_DECL (l) = decl;
33384           OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33385           OMP_CLAUSE_CHAIN (l) = clauses;
33386           clauses = l;
33387         }
33388       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33389         cp_lexer_consume_token (parser->lexer);
33390       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33391         break;
33392       else
33393         {
33394           error_at (cp_lexer_peek_token (parser->lexer)->location,
33395                     "expected %<,%> or %<)%> after %qE", decl);
33396           clauses = error_mark_node;
33397           break;
33398         }
33399     }
33400   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33401   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33402   return clauses;
33403 }
33404
33405 /* Returns the name of the next clause.  If the clause is not
33406    recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33407    token is not consumed.  Otherwise, the appropriate enum from the
33408    pragma_simd_clause is returned and the token is consumed.  */
33409
33410 static pragma_omp_clause
33411 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33412 {
33413   pragma_omp_clause clause_type;
33414   cp_token *token = cp_lexer_peek_token (parser->lexer);
33415
33416   if (token->keyword == RID_PRIVATE)
33417     clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33418   else if (!token->u.value || token->type != CPP_NAME)
33419     return PRAGMA_CILK_CLAUSE_NONE;
33420   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33421     clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33422   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33423     clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33424   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33425     clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33426   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33427     clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33428   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33429     clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33430   else
33431     return PRAGMA_CILK_CLAUSE_NONE;
33432
33433   cp_lexer_consume_token (parser->lexer);
33434   return clause_type;
33435 }
33436
33437 /* Parses all the #pragma simd clauses.  Returns a list of clauses found.  */
33438
33439 static tree
33440 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33441 {
33442   tree clauses = NULL_TREE;
33443
33444   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33445          && clauses != error_mark_node)
33446     {
33447       pragma_omp_clause c_kind;
33448       c_kind = cp_parser_cilk_simd_clause_name (parser);
33449       if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33450         clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33451       else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33452         clauses = cp_parser_cilk_simd_linear (parser, clauses);
33453       else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33454         /* Use the OpenMP 4.0 equivalent function.  */
33455         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33456       else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33457         /* Use the OpenMP 4.0 equivalent function.  */
33458         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33459                                           clauses);
33460       else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33461         /* Use the OMP 4.0 equivalent function.  */
33462         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33463                                           clauses);
33464       else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33465         /* Use the OMP 4.0 equivalent function.  */
33466         clauses = cp_parser_omp_clause_reduction (parser, clauses);
33467       else
33468         {
33469           clauses = error_mark_node;
33470           cp_parser_error (parser, "expected %<#pragma simd%> clause");
33471           break;
33472         }
33473     }
33474
33475   cp_parser_skip_to_pragma_eol (parser, pragma_token);
33476
33477   if (clauses == error_mark_node)
33478     return error_mark_node;
33479   else
33480     return c_finish_cilk_clauses (clauses);
33481 }
33482
33483 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops.  */
33484
33485 static void
33486 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33487 {
33488   tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33489
33490   if (clauses == error_mark_node)
33491     return;
33492
33493   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33494     {
33495       error_at (cp_lexer_peek_token (parser->lexer)->location,
33496                 "for statement expected");
33497       return;
33498     }
33499
33500   tree sb = begin_omp_structured_block ();
33501   int save = cp_parser_begin_omp_structured_block (parser);
33502   tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33503   if (ret)
33504     cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33505   cp_parser_end_omp_structured_block (parser, save);
33506   add_stmt (finish_omp_structured_block (sb));
33507 }
33508
33509 /* Main entry-point for parsing Cilk Plus _Cilk_for
33510    loops.  The return value is error_mark_node
33511    when errors happen and CILK_FOR tree on success.  */
33512
33513 static tree
33514 cp_parser_cilk_for (cp_parser *parser, tree grain)
33515 {
33516   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33517     gcc_unreachable ();
33518
33519   tree sb = begin_omp_structured_block ();
33520   int save = cp_parser_begin_omp_structured_block (parser);
33521
33522   tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33523   OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33524   OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33525   clauses = finish_omp_clauses (clauses);
33526
33527   tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33528   if (ret)
33529     cpp_validate_cilk_plus_loop (ret);
33530   else
33531     ret = error_mark_node;
33532
33533   cp_parser_end_omp_structured_block (parser, save);
33534   add_stmt (finish_omp_structured_block (sb));
33535   return ret;
33536 }
33537
33538 /* Create an identifier for a generic parameter type (a synthesized
33539    template parameter implied by `auto' or a concept identifier). */
33540
33541 static GTY(()) int generic_parm_count;
33542 static tree
33543 make_generic_type_name ()
33544 {
33545   char buf[32];
33546   sprintf (buf, "auto:%d", ++generic_parm_count);
33547   return get_identifier (buf);
33548 }
33549
33550 /* Predicate that behaves as is_auto_or_concept but matches the parent
33551    node of the generic type rather than the generic type itself.  This
33552    allows for type transformation in add_implicit_template_parms.  */
33553
33554 static inline bool
33555 tree_type_is_auto_or_concept (const_tree t)
33556 {
33557   return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33558 }
33559
33560 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33561    (creating a new template parameter list if necessary).  Returns the newly
33562    created template type parm.  */
33563
33564 tree
33565 synthesize_implicit_template_parm  (cp_parser *parser)
33566 {
33567   gcc_assert (current_binding_level->kind == sk_function_parms);
33568
33569   /* We are either continuing a function template that already contains implicit
33570      template parameters, creating a new fully-implicit function template, or
33571      extending an existing explicit function template with implicit template
33572      parameters.  */
33573
33574   cp_binding_level *const entry_scope = current_binding_level;
33575
33576   bool become_template = false;
33577   cp_binding_level *parent_scope = 0;
33578
33579   if (parser->implicit_template_scope)
33580     {
33581       gcc_assert (parser->implicit_template_parms);
33582
33583       current_binding_level = parser->implicit_template_scope;
33584     }
33585   else
33586     {
33587       /* Roll back to the existing template parameter scope (in the case of
33588          extending an explicit function template) or introduce a new template
33589          parameter scope ahead of the function parameter scope (or class scope
33590          in the case of out-of-line member definitions).  The function scope is
33591          added back after template parameter synthesis below.  */
33592
33593       cp_binding_level *scope = entry_scope;
33594
33595       while (scope->kind == sk_function_parms)
33596         {
33597           parent_scope = scope;
33598           scope = scope->level_chain;
33599         }
33600       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33601         {
33602           /* If not defining a class, then any class scope is a scope level in
33603              an out-of-line member definition.  In this case simply wind back
33604              beyond the first such scope to inject the template parameter list.
33605              Otherwise wind back to the class being defined.  The latter can
33606              occur in class member friend declarations such as:
33607
33608                class A {
33609                  void foo (auto);
33610                };
33611                class B {
33612                  friend void A::foo (auto);
33613                };
33614
33615             The template parameter list synthesized for the friend declaration
33616             must be injected in the scope of 'B'.  This can also occur in
33617             erroneous cases such as:
33618
33619                struct A {
33620                  struct B {
33621                    void foo (auto);
33622                  };
33623                  void B::foo (auto) {}
33624                };
33625
33626             Here the attempted definition of 'B::foo' within 'A' is ill-formed
33627             but, nevertheless, the template parameter list synthesized for the
33628             declarator should be injected into the scope of 'A' as if the
33629             ill-formed template was specified explicitly.  */
33630
33631           while (scope->kind == sk_class && !scope->defining_class_p)
33632             {
33633               parent_scope = scope;
33634               scope = scope->level_chain;
33635             }
33636         }
33637
33638       current_binding_level = scope;
33639
33640       if (scope->kind != sk_template_parms
33641           || !function_being_declared_is_template_p (parser))
33642         {
33643           /* Introduce a new template parameter list for implicit template
33644              parameters.  */
33645
33646           become_template = true;
33647
33648           parser->implicit_template_scope
33649               = begin_scope (sk_template_parms, NULL);
33650
33651           ++processing_template_decl;
33652
33653           parser->fully_implicit_function_template_p = true;
33654           ++parser->num_template_parameter_lists;
33655         }
33656       else
33657         {
33658           /* Synthesize implicit template parameters at the end of the explicit
33659              template parameter list.  */
33660
33661           gcc_assert (current_template_parms);
33662
33663           parser->implicit_template_scope = scope;
33664
33665           tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33666           parser->implicit_template_parms
33667             = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33668         }
33669     }
33670
33671   /* Synthesize a new template parameter and track the current template
33672      parameter chain with implicit_template_parms.  */
33673
33674   tree synth_id = make_generic_type_name ();
33675   tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33676                                                     synth_id);
33677   tree new_parm
33678     = process_template_parm (parser->implicit_template_parms,
33679                              input_location,
33680                              build_tree_list (NULL_TREE, synth_tmpl_parm),
33681                              /*non_type=*/false,
33682                              /*param_pack=*/false);
33683
33684
33685   if (parser->implicit_template_parms)
33686     parser->implicit_template_parms
33687       = TREE_CHAIN (parser->implicit_template_parms);
33688   else
33689     parser->implicit_template_parms = new_parm;
33690
33691   tree new_type = TREE_TYPE (getdecls ());
33692
33693   /* If creating a fully implicit function template, start the new implicit
33694      template parameter list with this synthesized type, otherwise grow the
33695      current template parameter list.  */
33696
33697   if (become_template)
33698     {
33699       parent_scope->level_chain = current_binding_level;
33700
33701       tree new_parms = make_tree_vec (1);
33702       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33703       current_template_parms = tree_cons (size_int (processing_template_decl),
33704                                           new_parms, current_template_parms);
33705     }
33706   else
33707     {
33708       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33709       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33710       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33711       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33712     }
33713
33714   current_binding_level = entry_scope;
33715
33716   return new_type;
33717 }
33718
33719 /* Finish the declaration of a fully implicit function template.  Such a
33720    template has no explicit template parameter list so has not been through the
33721    normal template head and tail processing.  synthesize_implicit_template_parm
33722    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
33723    provided if the declaration is a class member such that its template
33724    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
33725    form is returned.  Otherwise NULL_TREE is returned. */
33726
33727 tree
33728 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33729 {
33730   gcc_assert (parser->fully_implicit_function_template_p);
33731
33732   if (member_decl_opt && member_decl_opt != error_mark_node
33733       && DECL_VIRTUAL_P (member_decl_opt))
33734     {
33735       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33736                 "implicit templates may not be %<virtual%>");
33737       DECL_VIRTUAL_P (member_decl_opt) = false;
33738     }
33739
33740   if (member_decl_opt)
33741     member_decl_opt = finish_member_template_decl (member_decl_opt);
33742   end_template_decl ();
33743
33744   parser->fully_implicit_function_template_p = false;
33745   --parser->num_template_parameter_lists;
33746
33747   return member_decl_opt;
33748 }
33749
33750 #include "gt-cp-parser.h"