7168aba358cfc62ec9e172305e2154bcf0d6691e
[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 fn;
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   if (result != error_mark_node)
3866     return result;
3867
3868   error ("unable to find character literal operator %qD with %qT argument",
3869          name, TREE_TYPE (value));
3870   return error_mark_node;
3871 }
3872
3873 /* A subroutine of cp_parser_userdef_numeric_literal to
3874    create a char... template parameter pack from a string node.  */
3875
3876 static tree
3877 make_char_string_pack (tree value)
3878 {
3879   tree charvec;
3880   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3881   const char *str = TREE_STRING_POINTER (value);
3882   int i, len = TREE_STRING_LENGTH (value) - 1;
3883   tree argvec = make_tree_vec (1);
3884
3885   /* Fill in CHARVEC with all of the parameters.  */
3886   charvec = make_tree_vec (len);
3887   for (i = 0; i < len; ++i)
3888     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3889
3890   /* Build the argument packs.  */
3891   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3892   TREE_TYPE (argpack) = char_type_node;
3893
3894   TREE_VEC_ELT (argvec, 0) = argpack;
3895
3896   return argvec;
3897 }
3898
3899 /* A subroutine of cp_parser_userdef_numeric_literal to
3900    create a char... template parameter pack from a string node.  */
3901
3902 static tree
3903 make_string_pack (tree value)
3904 {
3905   tree charvec;
3906   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3907   const unsigned char *str
3908     = (const unsigned char *) TREE_STRING_POINTER (value);
3909   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3910   int len = TREE_STRING_LENGTH (value) / sz - 1;
3911   tree argvec = make_tree_vec (2);
3912
3913   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3914   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3915
3916   /* First template parm is character type.  */
3917   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3918
3919   /* Fill in CHARVEC with all of the parameters.  */
3920   charvec = make_tree_vec (len);
3921   for (int i = 0; i < len; ++i)
3922     TREE_VEC_ELT (charvec, i)
3923       = double_int_to_tree (str_char_type_node,
3924                             double_int::from_buffer (str + i * sz, sz));
3925
3926   /* Build the argument packs.  */
3927   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3928   TREE_TYPE (argpack) = str_char_type_node;
3929
3930   TREE_VEC_ELT (argvec, 1) = argpack;
3931
3932   return argvec;
3933 }
3934
3935 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3936    literal operator.  */
3937
3938 static tree
3939 cp_parser_userdef_numeric_literal (cp_parser *parser)
3940 {
3941   cp_token *token = cp_lexer_consume_token (parser->lexer);
3942   tree literal = token->u.value;
3943   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3944   tree value = USERDEF_LITERAL_VALUE (literal);
3945   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3946   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3947   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3948   tree decl, result;
3949   vec<tree, va_gc> *args;
3950
3951   /* Look for a literal operator taking the exact type of numeric argument
3952      as the literal value.  */
3953   args = make_tree_vector ();
3954   vec_safe_push (args, value);
3955   decl = lookup_literal_operator (name, args);
3956   if (decl && decl != error_mark_node)
3957     {
3958       result = finish_call_expr (decl, &args, false, true, tf_none);
3959       if (result != error_mark_node)
3960         {
3961           if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3962             warning_at (token->location, OPT_Woverflow,
3963                         "integer literal exceeds range of %qT type",
3964                         long_long_unsigned_type_node);
3965           else
3966             {
3967               if (overflow > 0)
3968                 warning_at (token->location, OPT_Woverflow,
3969                             "floating literal exceeds range of %qT type",
3970                             long_double_type_node);
3971               else if (overflow < 0)
3972                 warning_at (token->location, OPT_Woverflow,
3973                             "floating literal truncated to zero");
3974             }
3975           release_tree_vector (args);
3976           return result;
3977         }
3978     }
3979   release_tree_vector (args);
3980
3981   /* If the numeric argument didn't work, look for a raw literal
3982      operator taking a const char* argument consisting of the number
3983      in string format.  */
3984   args = make_tree_vector ();
3985   vec_safe_push (args, num_string);
3986   decl = lookup_literal_operator (name, args);
3987   if (decl && decl != error_mark_node)
3988     {
3989       result = finish_call_expr (decl, &args, false, true, tf_none);
3990       if (result != error_mark_node)
3991         {
3992           release_tree_vector (args);
3993           return result;
3994         }
3995     }
3996   release_tree_vector (args);
3997
3998   /* If the raw literal didn't work, look for a non-type template
3999      function with parameter pack char....  Call the function with
4000      template parameter characters representing the number.  */
4001   args = make_tree_vector ();
4002   decl = lookup_literal_operator (name, args);
4003   if (decl && decl != error_mark_node)
4004     {
4005       tree tmpl_args = make_char_string_pack (num_string);
4006       decl = lookup_template_function (decl, tmpl_args);
4007       result = finish_call_expr (decl, &args, false, true, tf_none);
4008       if (result != error_mark_node)
4009         {
4010           release_tree_vector (args);
4011           return result;
4012         }
4013     }
4014   release_tree_vector (args);
4015
4016   error ("unable to find numeric literal operator %qD", name);
4017   if (!cpp_get_options (parse_in)->ext_numeric_literals)
4018     inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4019             "to enable more built-in suffixes");
4020   return error_mark_node;
4021 }
4022
4023 /* Parse a user-defined string constant.  Returns a call to a user-defined
4024    literal operator taking a character pointer and the length of the string
4025    as arguments.  */
4026
4027 static tree
4028 cp_parser_userdef_string_literal (tree literal)
4029 {
4030   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4031   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4032   tree value = USERDEF_LITERAL_VALUE (literal);
4033   int len = TREE_STRING_LENGTH (value)
4034         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4035   tree decl, result;
4036   vec<tree, va_gc> *args;
4037
4038   /* Look for a template function with typename parameter CharT
4039      and parameter pack CharT...  Call the function with
4040      template parameter characters representing the string.  */
4041   args = make_tree_vector ();
4042   decl = lookup_literal_operator (name, args);
4043   if (decl && decl != error_mark_node)
4044     {
4045       tree tmpl_args = make_string_pack (value);
4046       decl = lookup_template_function (decl, tmpl_args);
4047       result = finish_call_expr (decl, &args, false, true, tf_none);
4048       if (result != error_mark_node)
4049         {
4050           release_tree_vector (args);
4051           return result;
4052         }
4053     }
4054   release_tree_vector (args);
4055
4056   /* Build up a call to the user-defined operator  */
4057   /* Lookup the name we got back from the id-expression.  */
4058   args = make_tree_vector ();
4059   vec_safe_push (args, value);
4060   vec_safe_push (args, build_int_cst (size_type_node, len));
4061   decl = lookup_name (name);
4062   if (!decl || decl == error_mark_node)
4063     {
4064       error ("unable to find string literal operator %qD", name);
4065       release_tree_vector (args);
4066       return error_mark_node;
4067     }
4068   result = finish_call_expr (decl, &args, false, true, tf_none);
4069   release_tree_vector (args);
4070   if (result != error_mark_node)
4071     return result;
4072
4073   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4074          name, TREE_TYPE (value), size_type_node);
4075   return error_mark_node;
4076 }
4077
4078
4079 /* Basic concepts [gram.basic]  */
4080
4081 /* Parse a translation-unit.
4082
4083    translation-unit:
4084      declaration-seq [opt]
4085
4086    Returns TRUE if all went well.  */
4087
4088 static bool
4089 cp_parser_translation_unit (cp_parser* parser)
4090 {
4091   /* The address of the first non-permanent object on the declarator
4092      obstack.  */
4093   static void *declarator_obstack_base;
4094
4095   bool success;
4096
4097   /* Create the declarator obstack, if necessary.  */
4098   if (!cp_error_declarator)
4099     {
4100       gcc_obstack_init (&declarator_obstack);
4101       /* Create the error declarator.  */
4102       cp_error_declarator = make_declarator (cdk_error);
4103       /* Create the empty parameter list.  */
4104       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4105       /* Remember where the base of the declarator obstack lies.  */
4106       declarator_obstack_base = obstack_next_free (&declarator_obstack);
4107     }
4108
4109   cp_parser_declaration_seq_opt (parser);
4110
4111   /* If there are no tokens left then all went well.  */
4112   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4113     {
4114       /* Get rid of the token array; we don't need it any more.  */
4115       cp_lexer_destroy (parser->lexer);
4116       parser->lexer = NULL;
4117
4118       /* This file might have been a context that's implicitly extern
4119          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
4120       if (parser->implicit_extern_c)
4121         {
4122           pop_lang_context ();
4123           parser->implicit_extern_c = false;
4124         }
4125
4126       /* Finish up.  */
4127       finish_translation_unit ();
4128
4129       success = true;
4130     }
4131   else
4132     {
4133       cp_parser_error (parser, "expected declaration");
4134       success = false;
4135     }
4136
4137   /* Make sure the declarator obstack was fully cleaned up.  */
4138   gcc_assert (obstack_next_free (&declarator_obstack)
4139               == declarator_obstack_base);
4140
4141   /* All went well.  */
4142   return success;
4143 }
4144
4145 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4146    decltype context.  */
4147
4148 static inline tsubst_flags_t
4149 complain_flags (bool decltype_p)
4150 {
4151   tsubst_flags_t complain = tf_warning_or_error;
4152   if (decltype_p)
4153     complain |= tf_decltype;
4154   return complain;
4155 }
4156
4157 /* We're about to parse a collection of statements.  If we're currently
4158    parsing tentatively, set up a firewall so that any nested
4159    cp_parser_commit_to_tentative_parse won't affect the current context.  */
4160
4161 static cp_token_position
4162 cp_parser_start_tentative_firewall (cp_parser *parser)
4163 {
4164   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4165     return 0;
4166
4167   cp_parser_parse_tentatively (parser);
4168   cp_parser_commit_to_topmost_tentative_parse (parser);
4169   return cp_lexer_token_position (parser->lexer, false);
4170 }
4171
4172 /* We've finished parsing the collection of statements.  Wrap up the
4173    firewall and replace the relevant tokens with the parsed form.  */
4174
4175 static void
4176 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4177                                   tree expr)
4178 {
4179   if (!start)
4180     return;
4181
4182   /* Finish the firewall level.  */
4183   cp_parser_parse_definitely (parser);
4184   /* And remember the result of the parse for when we try again.  */
4185   cp_token *token = cp_lexer_token_at (parser->lexer, start);
4186   token->type = CPP_PREPARSED_EXPR;
4187   token->u.value = expr;
4188   token->keyword = RID_MAX;
4189   cp_lexer_purge_tokens_after (parser->lexer, start);
4190 }
4191
4192 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4193    enclosing parentheses.  */
4194
4195 static tree
4196 cp_parser_statement_expr (cp_parser *parser)
4197 {
4198   cp_token_position start = cp_parser_start_tentative_firewall (parser);
4199
4200   /* Consume the '('.  */
4201   cp_lexer_consume_token (parser->lexer);
4202   /* Start the statement-expression.  */
4203   tree expr = begin_stmt_expr ();
4204   /* Parse the compound-statement.  */
4205   cp_parser_compound_statement (parser, expr, false, false);
4206   /* Finish up.  */
4207   expr = finish_stmt_expr (expr, false);
4208   /* Consume the ')'.  */
4209   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4210     cp_parser_skip_to_end_of_statement (parser);
4211
4212   cp_parser_end_tentative_firewall (parser, start, expr);
4213   return expr;
4214 }
4215
4216 /* Expressions [gram.expr] */
4217
4218 /* Parse a primary-expression.
4219
4220    primary-expression:
4221      literal
4222      this
4223      ( expression )
4224      id-expression
4225      lambda-expression (C++11)
4226
4227    GNU Extensions:
4228
4229    primary-expression:
4230      ( compound-statement )
4231      __builtin_va_arg ( assignment-expression , type-id )
4232      __builtin_offsetof ( type-id , offsetof-expression )
4233
4234    C++ Extensions:
4235      __has_nothrow_assign ( type-id )   
4236      __has_nothrow_constructor ( type-id )
4237      __has_nothrow_copy ( type-id )
4238      __has_trivial_assign ( type-id )   
4239      __has_trivial_constructor ( type-id )
4240      __has_trivial_copy ( type-id )
4241      __has_trivial_destructor ( type-id )
4242      __has_virtual_destructor ( type-id )     
4243      __is_abstract ( type-id )
4244      __is_base_of ( type-id , type-id )
4245      __is_class ( type-id )
4246      __is_empty ( type-id )
4247      __is_enum ( type-id )
4248      __is_final ( type-id )
4249      __is_literal_type ( type-id )
4250      __is_pod ( type-id )
4251      __is_polymorphic ( type-id )
4252      __is_std_layout ( type-id )
4253      __is_trivial ( type-id )
4254      __is_union ( type-id )
4255
4256    Objective-C++ Extension:
4257
4258    primary-expression:
4259      objc-expression
4260
4261    literal:
4262      __null
4263
4264    ADDRESS_P is true iff this expression was immediately preceded by
4265    "&" and therefore might denote a pointer-to-member.  CAST_P is true
4266    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
4267    true iff this expression is a template argument.
4268
4269    Returns a representation of the expression.  Upon return, *IDK
4270    indicates what kind of id-expression (if any) was present.  */
4271
4272 static tree
4273 cp_parser_primary_expression (cp_parser *parser,
4274                               bool address_p,
4275                               bool cast_p,
4276                               bool template_arg_p,
4277                               bool decltype_p,
4278                               cp_id_kind *idk)
4279 {
4280   cp_token *token = NULL;
4281
4282   /* Assume the primary expression is not an id-expression.  */
4283   *idk = CP_ID_KIND_NONE;
4284
4285   /* Peek at the next token.  */
4286   token = cp_lexer_peek_token (parser->lexer);
4287   switch ((int) token->type)
4288     {
4289       /* literal:
4290            integer-literal
4291            character-literal
4292            floating-literal
4293            string-literal
4294            boolean-literal
4295            pointer-literal
4296            user-defined-literal  */
4297     case CPP_CHAR:
4298     case CPP_CHAR16:
4299     case CPP_CHAR32:
4300     case CPP_WCHAR:
4301     case CPP_NUMBER:
4302     case CPP_PREPARSED_EXPR:
4303       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4304         return cp_parser_userdef_numeric_literal (parser);
4305       token = cp_lexer_consume_token (parser->lexer);
4306       if (TREE_CODE (token->u.value) == FIXED_CST)
4307         {
4308           error_at (token->location,
4309                     "fixed-point types not supported in C++");
4310           return error_mark_node;
4311         }
4312       /* Floating-point literals are only allowed in an integral
4313          constant expression if they are cast to an integral or
4314          enumeration type.  */
4315       if (TREE_CODE (token->u.value) == REAL_CST
4316           && parser->integral_constant_expression_p
4317           && pedantic)
4318         {
4319           /* CAST_P will be set even in invalid code like "int(2.7 +
4320              ...)".   Therefore, we have to check that the next token
4321              is sure to end the cast.  */
4322           if (cast_p)
4323             {
4324               cp_token *next_token;
4325
4326               next_token = cp_lexer_peek_token (parser->lexer);
4327               if (/* The comma at the end of an
4328                      enumerator-definition.  */
4329                   next_token->type != CPP_COMMA
4330                   /* The curly brace at the end of an enum-specifier.  */
4331                   && next_token->type != CPP_CLOSE_BRACE
4332                   /* The end of a statement.  */
4333                   && next_token->type != CPP_SEMICOLON
4334                   /* The end of the cast-expression.  */
4335                   && next_token->type != CPP_CLOSE_PAREN
4336                   /* The end of an array bound.  */
4337                   && next_token->type != CPP_CLOSE_SQUARE
4338                   /* The closing ">" in a template-argument-list.  */
4339                   && (next_token->type != CPP_GREATER
4340                       || parser->greater_than_is_operator_p)
4341                   /* C++0x only: A ">>" treated like two ">" tokens,
4342                      in a template-argument-list.  */
4343                   && (next_token->type != CPP_RSHIFT
4344                       || (cxx_dialect == cxx98)
4345                       || parser->greater_than_is_operator_p))
4346                 cast_p = false;
4347             }
4348
4349           /* If we are within a cast, then the constraint that the
4350              cast is to an integral or enumeration type will be
4351              checked at that point.  If we are not within a cast, then
4352              this code is invalid.  */
4353           if (!cast_p)
4354             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4355         }
4356       return token->u.value;
4357
4358     case CPP_CHAR_USERDEF:
4359     case CPP_CHAR16_USERDEF:
4360     case CPP_CHAR32_USERDEF:
4361     case CPP_WCHAR_USERDEF:
4362       return cp_parser_userdef_char_literal (parser);
4363
4364     case CPP_STRING:
4365     case CPP_STRING16:
4366     case CPP_STRING32:
4367     case CPP_WSTRING:
4368     case CPP_UTF8STRING:
4369     case CPP_STRING_USERDEF:
4370     case CPP_STRING16_USERDEF:
4371     case CPP_STRING32_USERDEF:
4372     case CPP_WSTRING_USERDEF:
4373     case CPP_UTF8STRING_USERDEF:
4374       /* ??? Should wide strings be allowed when parser->translate_strings_p
4375          is false (i.e. in attributes)?  If not, we can kill the third
4376          argument to cp_parser_string_literal.  */
4377       return cp_parser_string_literal (parser,
4378                                        parser->translate_strings_p,
4379                                        true);
4380
4381     case CPP_OPEN_PAREN:
4382       /* If we see `( { ' then we are looking at the beginning of
4383          a GNU statement-expression.  */
4384       if (cp_parser_allow_gnu_extensions_p (parser)
4385           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4386         {
4387           /* Statement-expressions are not allowed by the standard.  */
4388           pedwarn (token->location, OPT_Wpedantic,
4389                    "ISO C++ forbids braced-groups within expressions");
4390
4391           /* And they're not allowed outside of a function-body; you
4392              cannot, for example, write:
4393
4394              int i = ({ int j = 3; j + 1; });
4395
4396              at class or namespace scope.  */
4397           if (!parser->in_function_body
4398               || parser->in_template_argument_list_p)
4399             {
4400               error_at (token->location,
4401                         "statement-expressions are not allowed outside "
4402                         "functions nor in template-argument lists");
4403               cp_parser_skip_to_end_of_block_or_statement (parser);
4404               if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4405                 cp_lexer_consume_token (parser->lexer);
4406               return error_mark_node;
4407             }
4408           else
4409             return cp_parser_statement_expr (parser);
4410         }
4411       /* Otherwise it's a normal parenthesized expression.  */
4412       {
4413         tree expr;
4414         bool saved_greater_than_is_operator_p;
4415
4416         /* Consume the `('.  */
4417         cp_lexer_consume_token (parser->lexer);
4418         /* Within a parenthesized expression, a `>' token is always
4419            the greater-than operator.  */
4420         saved_greater_than_is_operator_p
4421           = parser->greater_than_is_operator_p;
4422         parser->greater_than_is_operator_p = true;
4423
4424         /* Parse the parenthesized expression.  */
4425         expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4426         /* Let the front end know that this expression was
4427            enclosed in parentheses. This matters in case, for
4428            example, the expression is of the form `A::B', since
4429            `&A::B' might be a pointer-to-member, but `&(A::B)' is
4430            not.  */
4431         expr = finish_parenthesized_expr (expr);
4432         /* DR 705: Wrapping an unqualified name in parentheses
4433            suppresses arg-dependent lookup.  We want to pass back
4434            CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4435            (c++/37862), but none of the others.  */
4436         if (*idk != CP_ID_KIND_QUALIFIED)
4437           *idk = CP_ID_KIND_NONE;
4438
4439         /* The `>' token might be the end of a template-id or
4440            template-parameter-list now.  */
4441         parser->greater_than_is_operator_p
4442           = saved_greater_than_is_operator_p;
4443         /* Consume the `)'.  */
4444         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4445           cp_parser_skip_to_end_of_statement (parser);
4446
4447         return expr;
4448       }
4449
4450     case CPP_OPEN_SQUARE:
4451       {
4452         if (c_dialect_objc ())
4453           {
4454             /* We might have an Objective-C++ message. */
4455             cp_parser_parse_tentatively (parser);
4456             tree msg = cp_parser_objc_message_expression (parser);
4457             /* If that works out, we're done ... */
4458             if (cp_parser_parse_definitely (parser))
4459               return msg;
4460             /* ... else, fall though to see if it's a lambda.  */
4461           }
4462         tree lam = cp_parser_lambda_expression (parser);
4463         /* Don't warn about a failed tentative parse.  */
4464         if (cp_parser_error_occurred (parser))
4465           return error_mark_node;
4466         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4467         return lam;
4468       }
4469
4470     case CPP_OBJC_STRING:
4471       if (c_dialect_objc ())
4472         /* We have an Objective-C++ string literal. */
4473         return cp_parser_objc_expression (parser);
4474       cp_parser_error (parser, "expected primary-expression");
4475       return error_mark_node;
4476
4477     case CPP_KEYWORD:
4478       switch (token->keyword)
4479         {
4480           /* These two are the boolean literals.  */
4481         case RID_TRUE:
4482           cp_lexer_consume_token (parser->lexer);
4483           return boolean_true_node;
4484         case RID_FALSE:
4485           cp_lexer_consume_token (parser->lexer);
4486           return boolean_false_node;
4487
4488           /* The `__null' literal.  */
4489         case RID_NULL:
4490           cp_lexer_consume_token (parser->lexer);
4491           return null_node;
4492
4493           /* The `nullptr' literal.  */
4494         case RID_NULLPTR:
4495           cp_lexer_consume_token (parser->lexer);
4496           return nullptr_node;
4497
4498           /* Recognize the `this' keyword.  */
4499         case RID_THIS:
4500           cp_lexer_consume_token (parser->lexer);
4501           if (parser->local_variables_forbidden_p)
4502             {
4503               error_at (token->location,
4504                         "%<this%> may not be used in this context");
4505               return error_mark_node;
4506             }
4507           /* Pointers cannot appear in constant-expressions.  */
4508           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4509             return error_mark_node;
4510           return finish_this_expr ();
4511
4512           /* The `operator' keyword can be the beginning of an
4513              id-expression.  */
4514         case RID_OPERATOR:
4515           goto id_expression;
4516
4517         case RID_FUNCTION_NAME:
4518         case RID_PRETTY_FUNCTION_NAME:
4519         case RID_C99_FUNCTION_NAME:
4520           {
4521             non_integral_constant name;
4522
4523             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4524                __func__ are the names of variables -- but they are
4525                treated specially.  Therefore, they are handled here,
4526                rather than relying on the generic id-expression logic
4527                below.  Grammatically, these names are id-expressions.
4528
4529                Consume the token.  */
4530             token = cp_lexer_consume_token (parser->lexer);
4531
4532             switch (token->keyword)
4533               {
4534               case RID_FUNCTION_NAME:
4535                 name = NIC_FUNC_NAME;
4536                 break;
4537               case RID_PRETTY_FUNCTION_NAME:
4538                 name = NIC_PRETTY_FUNC;
4539                 break;
4540               case RID_C99_FUNCTION_NAME:
4541                 name = NIC_C99_FUNC;
4542                 break;
4543               default:
4544                 gcc_unreachable ();
4545               }
4546
4547             if (cp_parser_non_integral_constant_expression (parser, name))
4548               return error_mark_node;
4549
4550             /* Look up the name.  */
4551             return finish_fname (token->u.value);
4552           }
4553
4554         case RID_VA_ARG:
4555           {
4556             tree expression;
4557             tree type;
4558             source_location type_location;
4559
4560             /* The `__builtin_va_arg' construct is used to handle
4561                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4562             cp_lexer_consume_token (parser->lexer);
4563             /* Look for the opening `('.  */
4564             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4565             /* Now, parse the assignment-expression.  */
4566             expression = cp_parser_assignment_expression (parser);
4567             /* Look for the `,'.  */
4568             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4569             type_location = cp_lexer_peek_token (parser->lexer)->location;
4570             /* Parse the type-id.  */
4571             type = cp_parser_type_id (parser);
4572             /* Look for the closing `)'.  */
4573             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4574             /* Using `va_arg' in a constant-expression is not
4575                allowed.  */
4576             if (cp_parser_non_integral_constant_expression (parser,
4577                                                             NIC_VA_ARG))
4578               return error_mark_node;
4579             return build_x_va_arg (type_location, expression, type);
4580           }
4581
4582         case RID_OFFSETOF:
4583           return cp_parser_builtin_offsetof (parser);
4584
4585         case RID_HAS_NOTHROW_ASSIGN:
4586         case RID_HAS_NOTHROW_CONSTRUCTOR:
4587         case RID_HAS_NOTHROW_COPY:        
4588         case RID_HAS_TRIVIAL_ASSIGN:
4589         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4590         case RID_HAS_TRIVIAL_COPY:        
4591         case RID_HAS_TRIVIAL_DESTRUCTOR:
4592         case RID_HAS_VIRTUAL_DESTRUCTOR:
4593         case RID_IS_ABSTRACT:
4594         case RID_IS_BASE_OF:
4595         case RID_IS_CLASS:
4596         case RID_IS_EMPTY:
4597         case RID_IS_ENUM:
4598         case RID_IS_FINAL:
4599         case RID_IS_LITERAL_TYPE:
4600         case RID_IS_POD:
4601         case RID_IS_POLYMORPHIC:
4602         case RID_IS_STD_LAYOUT:
4603         case RID_IS_TRIVIAL:
4604         case RID_IS_TRIVIALLY_ASSIGNABLE:
4605         case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4606         case RID_IS_TRIVIALLY_COPYABLE:
4607         case RID_IS_UNION:
4608           return cp_parser_trait_expr (parser, token->keyword);
4609
4610         /* Objective-C++ expressions.  */
4611         case RID_AT_ENCODE:
4612         case RID_AT_PROTOCOL:
4613         case RID_AT_SELECTOR:
4614           return cp_parser_objc_expression (parser);
4615
4616         case RID_TEMPLATE:
4617           if (parser->in_function_body
4618               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4619                   == CPP_LESS))
4620             {
4621               error_at (token->location,
4622                         "a template declaration cannot appear at block scope");
4623               cp_parser_skip_to_end_of_block_or_statement (parser);
4624               return error_mark_node;
4625             }
4626         default:
4627           cp_parser_error (parser, "expected primary-expression");
4628           return error_mark_node;
4629         }
4630
4631       /* An id-expression can start with either an identifier, a
4632          `::' as the beginning of a qualified-id, or the "operator"
4633          keyword.  */
4634     case CPP_NAME:
4635     case CPP_SCOPE:
4636     case CPP_TEMPLATE_ID:
4637     case CPP_NESTED_NAME_SPECIFIER:
4638       {
4639         tree id_expression;
4640         tree decl;
4641         const char *error_msg;
4642         bool template_p;
4643         bool done;
4644         cp_token *id_expr_token;
4645
4646       id_expression:
4647         /* Parse the id-expression.  */
4648         id_expression
4649           = cp_parser_id_expression (parser,
4650                                      /*template_keyword_p=*/false,
4651                                      /*check_dependency_p=*/true,
4652                                      &template_p,
4653                                      /*declarator_p=*/false,
4654                                      /*optional_p=*/false);
4655         if (id_expression == error_mark_node)
4656           return error_mark_node;
4657         id_expr_token = token;
4658         token = cp_lexer_peek_token (parser->lexer);
4659         done = (token->type != CPP_OPEN_SQUARE
4660                 && token->type != CPP_OPEN_PAREN
4661                 && token->type != CPP_DOT
4662                 && token->type != CPP_DEREF
4663                 && token->type != CPP_PLUS_PLUS
4664                 && token->type != CPP_MINUS_MINUS);
4665         /* If we have a template-id, then no further lookup is
4666            required.  If the template-id was for a template-class, we
4667            will sometimes have a TYPE_DECL at this point.  */
4668         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4669                  || TREE_CODE (id_expression) == TYPE_DECL)
4670           decl = id_expression;
4671         /* Look up the name.  */
4672         else
4673           {
4674             tree ambiguous_decls;
4675
4676             /* If we already know that this lookup is ambiguous, then
4677                we've already issued an error message; there's no reason
4678                to check again.  */
4679             if (id_expr_token->type == CPP_NAME
4680                 && id_expr_token->error_reported)
4681               {
4682                 cp_parser_simulate_error (parser);
4683                 return error_mark_node;
4684               }
4685
4686             decl = cp_parser_lookup_name (parser, id_expression,
4687                                           none_type,
4688                                           template_p,
4689                                           /*is_namespace=*/false,
4690                                           /*check_dependency=*/true,
4691                                           &ambiguous_decls,
4692                                           id_expr_token->location);
4693             /* If the lookup was ambiguous, an error will already have
4694                been issued.  */
4695             if (ambiguous_decls)
4696               return error_mark_node;
4697
4698             /* In Objective-C++, we may have an Objective-C 2.0
4699                dot-syntax for classes here.  */
4700             if (c_dialect_objc ()
4701                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4702                 && TREE_CODE (decl) == TYPE_DECL
4703                 && objc_is_class_name (decl))
4704               {
4705                 tree component;
4706                 cp_lexer_consume_token (parser->lexer);
4707                 component = cp_parser_identifier (parser);
4708                 if (component == error_mark_node)
4709                   return error_mark_node;
4710
4711                 return objc_build_class_component_ref (id_expression, component);
4712               }
4713
4714             /* In Objective-C++, an instance variable (ivar) may be preferred
4715                to whatever cp_parser_lookup_name() found.  */
4716             decl = objc_lookup_ivar (decl, id_expression);
4717
4718             /* If name lookup gives us a SCOPE_REF, then the
4719                qualifying scope was dependent.  */
4720             if (TREE_CODE (decl) == SCOPE_REF)
4721               {
4722                 /* At this point, we do not know if DECL is a valid
4723                    integral constant expression.  We assume that it is
4724                    in fact such an expression, so that code like:
4725
4726                       template <int N> struct A {
4727                         int a[B<N>::i];
4728                       };
4729                      
4730                    is accepted.  At template-instantiation time, we
4731                    will check that B<N>::i is actually a constant.  */
4732                 return decl;
4733               }
4734             /* Check to see if DECL is a local variable in a context
4735                where that is forbidden.  */
4736             if (parser->local_variables_forbidden_p
4737                 && local_variable_p (decl))
4738               {
4739                 /* It might be that we only found DECL because we are
4740                    trying to be generous with pre-ISO scoping rules.
4741                    For example, consider:
4742
4743                      int i;
4744                      void g() {
4745                        for (int i = 0; i < 10; ++i) {}
4746                        extern void f(int j = i);
4747                      }
4748
4749                    Here, name look up will originally find the out
4750                    of scope `i'.  We need to issue a warning message,
4751                    but then use the global `i'.  */
4752                 decl = check_for_out_of_scope_variable (decl);
4753                 if (local_variable_p (decl))
4754                   {
4755                     error_at (id_expr_token->location,
4756                               "local variable %qD may not appear in this context",
4757                               decl);
4758                     return error_mark_node;
4759                   }
4760               }
4761           }
4762
4763         decl = (finish_id_expression
4764                 (id_expression, decl, parser->scope,
4765                  idk,
4766                  parser->integral_constant_expression_p,
4767                  parser->allow_non_integral_constant_expression_p,
4768                  &parser->non_integral_constant_expression_p,
4769                  template_p, done, address_p,
4770                  template_arg_p,
4771                  &error_msg,
4772                  id_expr_token->location));
4773         if (error_msg)
4774           cp_parser_error (parser, error_msg);
4775         return decl;
4776       }
4777
4778       /* Anything else is an error.  */
4779     default:
4780       cp_parser_error (parser, "expected primary-expression");
4781       return error_mark_node;
4782     }
4783 }
4784
4785 static inline tree
4786 cp_parser_primary_expression (cp_parser *parser,
4787                               bool address_p,
4788                               bool cast_p,
4789                               bool template_arg_p,
4790                               cp_id_kind *idk)
4791 {
4792   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4793                                        /*decltype*/false, idk);
4794 }
4795
4796 /* Parse an id-expression.
4797
4798    id-expression:
4799      unqualified-id
4800      qualified-id
4801
4802    qualified-id:
4803      :: [opt] nested-name-specifier template [opt] unqualified-id
4804      :: identifier
4805      :: operator-function-id
4806      :: template-id
4807
4808    Return a representation of the unqualified portion of the
4809    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4810    a `::' or nested-name-specifier.
4811
4812    Often, if the id-expression was a qualified-id, the caller will
4813    want to make a SCOPE_REF to represent the qualified-id.  This
4814    function does not do this in order to avoid wastefully creating
4815    SCOPE_REFs when they are not required.
4816
4817    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4818    `template' keyword.
4819
4820    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4821    uninstantiated templates.
4822
4823    If *TEMPLATE_P is non-NULL, it is set to true iff the
4824    `template' keyword is used to explicitly indicate that the entity
4825    named is a template.
4826
4827    If DECLARATOR_P is true, the id-expression is appearing as part of
4828    a declarator, rather than as part of an expression.  */
4829
4830 static tree
4831 cp_parser_id_expression (cp_parser *parser,
4832                          bool template_keyword_p,
4833                          bool check_dependency_p,
4834                          bool *template_p,
4835                          bool declarator_p,
4836                          bool optional_p)
4837 {
4838   bool global_scope_p;
4839   bool nested_name_specifier_p;
4840
4841   /* Assume the `template' keyword was not used.  */
4842   if (template_p)
4843     *template_p = template_keyword_p;
4844
4845   /* Look for the optional `::' operator.  */
4846   global_scope_p
4847     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4848        != NULL_TREE);
4849   /* Look for the optional nested-name-specifier.  */
4850   nested_name_specifier_p
4851     = (cp_parser_nested_name_specifier_opt (parser,
4852                                             /*typename_keyword_p=*/false,
4853                                             check_dependency_p,
4854                                             /*type_p=*/false,
4855                                             declarator_p)
4856        != NULL_TREE);
4857   /* If there is a nested-name-specifier, then we are looking at
4858      the first qualified-id production.  */
4859   if (nested_name_specifier_p)
4860     {
4861       tree saved_scope;
4862       tree saved_object_scope;
4863       tree saved_qualifying_scope;
4864       tree unqualified_id;
4865       bool is_template;
4866
4867       /* See if the next token is the `template' keyword.  */
4868       if (!template_p)
4869         template_p = &is_template;
4870       *template_p = cp_parser_optional_template_keyword (parser);
4871       /* Name lookup we do during the processing of the
4872          unqualified-id might obliterate SCOPE.  */
4873       saved_scope = parser->scope;
4874       saved_object_scope = parser->object_scope;
4875       saved_qualifying_scope = parser->qualifying_scope;
4876       /* Process the final unqualified-id.  */
4877       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4878                                                  check_dependency_p,
4879                                                  declarator_p,
4880                                                  /*optional_p=*/false);
4881       /* Restore the SAVED_SCOPE for our caller.  */
4882       parser->scope = saved_scope;
4883       parser->object_scope = saved_object_scope;
4884       parser->qualifying_scope = saved_qualifying_scope;
4885
4886       return unqualified_id;
4887     }
4888   /* Otherwise, if we are in global scope, then we are looking at one
4889      of the other qualified-id productions.  */
4890   else if (global_scope_p)
4891     {
4892       cp_token *token;
4893       tree id;
4894
4895       /* Peek at the next token.  */
4896       token = cp_lexer_peek_token (parser->lexer);
4897
4898       /* If it's an identifier, and the next token is not a "<", then
4899          we can avoid the template-id case.  This is an optimization
4900          for this common case.  */
4901       if (token->type == CPP_NAME
4902           && !cp_parser_nth_token_starts_template_argument_list_p
4903                (parser, 2))
4904         return cp_parser_identifier (parser);
4905
4906       cp_parser_parse_tentatively (parser);
4907       /* Try a template-id.  */
4908       id = cp_parser_template_id (parser,
4909                                   /*template_keyword_p=*/false,
4910                                   /*check_dependency_p=*/true,
4911                                   none_type,
4912                                   declarator_p);
4913       /* If that worked, we're done.  */
4914       if (cp_parser_parse_definitely (parser))
4915         return id;
4916
4917       /* Peek at the next token.  (Changes in the token buffer may
4918          have invalidated the pointer obtained above.)  */
4919       token = cp_lexer_peek_token (parser->lexer);
4920
4921       switch (token->type)
4922         {
4923         case CPP_NAME:
4924           return cp_parser_identifier (parser);
4925
4926         case CPP_KEYWORD:
4927           if (token->keyword == RID_OPERATOR)
4928             return cp_parser_operator_function_id (parser);
4929           /* Fall through.  */
4930
4931         default:
4932           cp_parser_error (parser, "expected id-expression");
4933           return error_mark_node;
4934         }
4935     }
4936   else
4937     return cp_parser_unqualified_id (parser, template_keyword_p,
4938                                      /*check_dependency_p=*/true,
4939                                      declarator_p,
4940                                      optional_p);
4941 }
4942
4943 /* Parse an unqualified-id.
4944
4945    unqualified-id:
4946      identifier
4947      operator-function-id
4948      conversion-function-id
4949      ~ class-name
4950      template-id
4951
4952    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4953    keyword, in a construct like `A::template ...'.
4954
4955    Returns a representation of unqualified-id.  For the `identifier'
4956    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4957    production a BIT_NOT_EXPR is returned; the operand of the
4958    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4959    other productions, see the documentation accompanying the
4960    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4961    names are looked up in uninstantiated templates.  If DECLARATOR_P
4962    is true, the unqualified-id is appearing as part of a declarator,
4963    rather than as part of an expression.  */
4964
4965 static tree
4966 cp_parser_unqualified_id (cp_parser* parser,
4967                           bool template_keyword_p,
4968                           bool check_dependency_p,
4969                           bool declarator_p,
4970                           bool optional_p)
4971 {
4972   cp_token *token;
4973
4974   /* Peek at the next token.  */
4975   token = cp_lexer_peek_token (parser->lexer);
4976
4977   switch ((int) token->type)
4978     {
4979     case CPP_NAME:
4980       {
4981         tree id;
4982
4983         /* We don't know yet whether or not this will be a
4984            template-id.  */
4985         cp_parser_parse_tentatively (parser);
4986         /* Try a template-id.  */
4987         id = cp_parser_template_id (parser, template_keyword_p,
4988                                     check_dependency_p,
4989                                     none_type,
4990                                     declarator_p);
4991         /* If it worked, we're done.  */
4992         if (cp_parser_parse_definitely (parser))
4993           return id;
4994         /* Otherwise, it's an ordinary identifier.  */
4995         return cp_parser_identifier (parser);
4996       }
4997
4998     case CPP_TEMPLATE_ID:
4999       return cp_parser_template_id (parser, template_keyword_p,
5000                                     check_dependency_p,
5001                                     none_type,
5002                                     declarator_p);
5003
5004     case CPP_COMPL:
5005       {
5006         tree type_decl;
5007         tree qualifying_scope;
5008         tree object_scope;
5009         tree scope;
5010         bool done;
5011
5012         /* Consume the `~' token.  */
5013         cp_lexer_consume_token (parser->lexer);
5014         /* Parse the class-name.  The standard, as written, seems to
5015            say that:
5016
5017              template <typename T> struct S { ~S (); };
5018              template <typename T> S<T>::~S() {}
5019
5020            is invalid, since `~' must be followed by a class-name, but
5021            `S<T>' is dependent, and so not known to be a class.
5022            That's not right; we need to look in uninstantiated
5023            templates.  A further complication arises from:
5024
5025              template <typename T> void f(T t) {
5026                t.T::~T();
5027              }
5028
5029            Here, it is not possible to look up `T' in the scope of `T'
5030            itself.  We must look in both the current scope, and the
5031            scope of the containing complete expression.
5032
5033            Yet another issue is:
5034
5035              struct S {
5036                int S;
5037                ~S();
5038              };
5039
5040              S::~S() {}
5041
5042            The standard does not seem to say that the `S' in `~S'
5043            should refer to the type `S' and not the data member
5044            `S::S'.  */
5045
5046         /* DR 244 says that we look up the name after the "~" in the
5047            same scope as we looked up the qualifying name.  That idea
5048            isn't fully worked out; it's more complicated than that.  */
5049         scope = parser->scope;
5050         object_scope = parser->object_scope;
5051         qualifying_scope = parser->qualifying_scope;
5052
5053         /* Check for invalid scopes.  */
5054         if (scope == error_mark_node)
5055           {
5056             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5057               cp_lexer_consume_token (parser->lexer);
5058             return error_mark_node;
5059           }
5060         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5061           {
5062             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5063               error_at (token->location,
5064                         "scope %qT before %<~%> is not a class-name",
5065                         scope);
5066             cp_parser_simulate_error (parser);
5067             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5068               cp_lexer_consume_token (parser->lexer);
5069             return error_mark_node;
5070           }
5071         gcc_assert (!scope || TYPE_P (scope));
5072
5073         /* If the name is of the form "X::~X" it's OK even if X is a
5074            typedef.  */
5075         token = cp_lexer_peek_token (parser->lexer);
5076         if (scope
5077             && token->type == CPP_NAME
5078             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5079                 != CPP_LESS)
5080             && (token->u.value == TYPE_IDENTIFIER (scope)
5081                 || (CLASS_TYPE_P (scope)
5082                     && constructor_name_p (token->u.value, scope))))
5083           {
5084             cp_lexer_consume_token (parser->lexer);
5085             return build_nt (BIT_NOT_EXPR, scope);
5086           }
5087
5088         /* ~auto means the destructor of whatever the object is.  */
5089         if (cp_parser_is_keyword (token, RID_AUTO))
5090           {
5091             if (cxx_dialect < cxx14)
5092               pedwarn (input_location, 0,
5093                        "%<~auto%> only available with "
5094                        "-std=c++14 or -std=gnu++14");
5095             cp_lexer_consume_token (parser->lexer);
5096             return build_nt (BIT_NOT_EXPR, make_auto ());
5097           }
5098
5099         /* If there was an explicit qualification (S::~T), first look
5100            in the scope given by the qualification (i.e., S).
5101
5102            Note: in the calls to cp_parser_class_name below we pass
5103            typename_type so that lookup finds the injected-class-name
5104            rather than the constructor.  */
5105         done = false;
5106         type_decl = NULL_TREE;
5107         if (scope)
5108           {
5109             cp_parser_parse_tentatively (parser);
5110             type_decl = cp_parser_class_name (parser,
5111                                               /*typename_keyword_p=*/false,
5112                                               /*template_keyword_p=*/false,
5113                                               typename_type,
5114                                               /*check_dependency=*/false,
5115                                               /*class_head_p=*/false,
5116                                               declarator_p);
5117             if (cp_parser_parse_definitely (parser))
5118               done = true;
5119           }
5120         /* In "N::S::~S", look in "N" as well.  */
5121         if (!done && scope && qualifying_scope)
5122           {
5123             cp_parser_parse_tentatively (parser);
5124             parser->scope = qualifying_scope;
5125             parser->object_scope = NULL_TREE;
5126             parser->qualifying_scope = NULL_TREE;
5127             type_decl
5128               = cp_parser_class_name (parser,
5129                                       /*typename_keyword_p=*/false,
5130                                       /*template_keyword_p=*/false,
5131                                       typename_type,
5132                                       /*check_dependency=*/false,
5133                                       /*class_head_p=*/false,
5134                                       declarator_p);
5135             if (cp_parser_parse_definitely (parser))
5136               done = true;
5137           }
5138         /* In "p->S::~T", look in the scope given by "*p" as well.  */
5139         else if (!done && object_scope)
5140           {
5141             cp_parser_parse_tentatively (parser);
5142             parser->scope = object_scope;
5143             parser->object_scope = NULL_TREE;
5144             parser->qualifying_scope = NULL_TREE;
5145             type_decl
5146               = cp_parser_class_name (parser,
5147                                       /*typename_keyword_p=*/false,
5148                                       /*template_keyword_p=*/false,
5149                                       typename_type,
5150                                       /*check_dependency=*/false,
5151                                       /*class_head_p=*/false,
5152                                       declarator_p);
5153             if (cp_parser_parse_definitely (parser))
5154               done = true;
5155           }
5156         /* Look in the surrounding context.  */
5157         if (!done)
5158           {
5159             parser->scope = NULL_TREE;
5160             parser->object_scope = NULL_TREE;
5161             parser->qualifying_scope = NULL_TREE;
5162             if (processing_template_decl)
5163               cp_parser_parse_tentatively (parser);
5164             type_decl
5165               = cp_parser_class_name (parser,
5166                                       /*typename_keyword_p=*/false,
5167                                       /*template_keyword_p=*/false,
5168                                       typename_type,
5169                                       /*check_dependency=*/false,
5170                                       /*class_head_p=*/false,
5171                                       declarator_p);
5172             if (processing_template_decl
5173                 && ! cp_parser_parse_definitely (parser))
5174               {
5175                 /* We couldn't find a type with this name, so just accept
5176                    it and check for a match at instantiation time.  */
5177                 type_decl = cp_parser_identifier (parser);
5178                 if (type_decl != error_mark_node)
5179                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5180                 return type_decl;
5181               }
5182           }
5183         /* If an error occurred, assume that the name of the
5184            destructor is the same as the name of the qualifying
5185            class.  That allows us to keep parsing after running
5186            into ill-formed destructor names.  */
5187         if (type_decl == error_mark_node && scope)
5188           return build_nt (BIT_NOT_EXPR, scope);
5189         else if (type_decl == error_mark_node)
5190           return error_mark_node;
5191
5192         /* Check that destructor name and scope match.  */
5193         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5194           {
5195             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5196               error_at (token->location,
5197                         "declaration of %<~%T%> as member of %qT",
5198                         type_decl, scope);
5199             cp_parser_simulate_error (parser);
5200             return error_mark_node;
5201           }
5202
5203         /* [class.dtor]
5204
5205            A typedef-name that names a class shall not be used as the
5206            identifier in the declarator for a destructor declaration.  */
5207         if (declarator_p
5208             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5209             && !DECL_SELF_REFERENCE_P (type_decl)
5210             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5211           error_at (token->location,
5212                     "typedef-name %qD used as destructor declarator",
5213                     type_decl);
5214
5215         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5216       }
5217
5218     case CPP_KEYWORD:
5219       if (token->keyword == RID_OPERATOR)
5220         {
5221           tree id;
5222
5223           /* This could be a template-id, so we try that first.  */
5224           cp_parser_parse_tentatively (parser);
5225           /* Try a template-id.  */
5226           id = cp_parser_template_id (parser, template_keyword_p,
5227                                       /*check_dependency_p=*/true,
5228                                       none_type,
5229                                       declarator_p);
5230           /* If that worked, we're done.  */
5231           if (cp_parser_parse_definitely (parser))
5232             return id;
5233           /* We still don't know whether we're looking at an
5234              operator-function-id or a conversion-function-id.  */
5235           cp_parser_parse_tentatively (parser);
5236           /* Try an operator-function-id.  */
5237           id = cp_parser_operator_function_id (parser);
5238           /* If that didn't work, try a conversion-function-id.  */
5239           if (!cp_parser_parse_definitely (parser))
5240             id = cp_parser_conversion_function_id (parser);
5241           else if (UDLIT_OPER_P (id))
5242             {
5243               /* 17.6.3.3.5  */
5244               const char *name = UDLIT_OP_SUFFIX (id);
5245               if (name[0] != '_' && !in_system_header_at (input_location)
5246                   && declarator_p)
5247                 warning (0, "literal operator suffixes not preceded by %<_%>"
5248                             " are reserved for future standardization");
5249             }
5250
5251           return id;
5252         }
5253       /* Fall through.  */
5254
5255     default:
5256       if (optional_p)
5257         return NULL_TREE;
5258       cp_parser_error (parser, "expected unqualified-id");
5259       return error_mark_node;
5260     }
5261 }
5262
5263 /* Parse an (optional) nested-name-specifier.
5264
5265    nested-name-specifier: [C++98]
5266      class-or-namespace-name :: nested-name-specifier [opt]
5267      class-or-namespace-name :: template nested-name-specifier [opt]
5268
5269    nested-name-specifier: [C++0x]
5270      type-name ::
5271      namespace-name ::
5272      nested-name-specifier identifier ::
5273      nested-name-specifier template [opt] simple-template-id ::
5274
5275    PARSER->SCOPE should be set appropriately before this function is
5276    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5277    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
5278    in name lookups.
5279
5280    Sets PARSER->SCOPE to the class (TYPE) or namespace
5281    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5282    it unchanged if there is no nested-name-specifier.  Returns the new
5283    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5284
5285    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5286    part of a declaration and/or decl-specifier.  */
5287
5288 static tree
5289 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5290                                      bool typename_keyword_p,
5291                                      bool check_dependency_p,
5292                                      bool type_p,
5293                                      bool is_declaration)
5294 {
5295   bool success = false;
5296   cp_token_position start = 0;
5297   cp_token *token;
5298
5299   /* Remember where the nested-name-specifier starts.  */
5300   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5301     {
5302       start = cp_lexer_token_position (parser->lexer, false);
5303       push_deferring_access_checks (dk_deferred);
5304     }
5305
5306   while (true)
5307     {
5308       tree new_scope;
5309       tree old_scope;
5310       tree saved_qualifying_scope;
5311       bool template_keyword_p;
5312
5313       /* Spot cases that cannot be the beginning of a
5314          nested-name-specifier.  */
5315       token = cp_lexer_peek_token (parser->lexer);
5316
5317       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5318          the already parsed nested-name-specifier.  */
5319       if (token->type == CPP_NESTED_NAME_SPECIFIER)
5320         {
5321           /* Grab the nested-name-specifier and continue the loop.  */
5322           cp_parser_pre_parsed_nested_name_specifier (parser);
5323           /* If we originally encountered this nested-name-specifier
5324              with IS_DECLARATION set to false, we will not have
5325              resolved TYPENAME_TYPEs, so we must do so here.  */
5326           if (is_declaration
5327               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5328             {
5329               new_scope = resolve_typename_type (parser->scope,
5330                                                  /*only_current_p=*/false);
5331               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5332                 parser->scope = new_scope;
5333             }
5334           success = true;
5335           continue;
5336         }
5337
5338       /* Spot cases that cannot be the beginning of a
5339          nested-name-specifier.  On the second and subsequent times
5340          through the loop, we look for the `template' keyword.  */
5341       if (success && token->keyword == RID_TEMPLATE)
5342         ;
5343       /* A template-id can start a nested-name-specifier.  */
5344       else if (token->type == CPP_TEMPLATE_ID)
5345         ;
5346       /* DR 743: decltype can be used in a nested-name-specifier.  */
5347       else if (token_is_decltype (token))
5348         ;
5349       else
5350         {
5351           /* If the next token is not an identifier, then it is
5352              definitely not a type-name or namespace-name.  */
5353           if (token->type != CPP_NAME)
5354             break;
5355           /* If the following token is neither a `<' (to begin a
5356              template-id), nor a `::', then we are not looking at a
5357              nested-name-specifier.  */
5358           token = cp_lexer_peek_nth_token (parser->lexer, 2);
5359
5360           if (token->type == CPP_COLON
5361               && parser->colon_corrects_to_scope_p
5362               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5363             {
5364               error_at (token->location,
5365                         "found %<:%> in nested-name-specifier, expected %<::%>");
5366               token->type = CPP_SCOPE;
5367             }
5368
5369           if (token->type != CPP_SCOPE
5370               && !cp_parser_nth_token_starts_template_argument_list_p
5371                   (parser, 2))
5372             break;
5373         }
5374
5375       /* The nested-name-specifier is optional, so we parse
5376          tentatively.  */
5377       cp_parser_parse_tentatively (parser);
5378
5379       /* Look for the optional `template' keyword, if this isn't the
5380          first time through the loop.  */
5381       if (success)
5382         template_keyword_p = cp_parser_optional_template_keyword (parser);
5383       else
5384         template_keyword_p = false;
5385
5386       /* Save the old scope since the name lookup we are about to do
5387          might destroy it.  */
5388       old_scope = parser->scope;
5389       saved_qualifying_scope = parser->qualifying_scope;
5390       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5391          look up names in "X<T>::I" in order to determine that "Y" is
5392          a template.  So, if we have a typename at this point, we make
5393          an effort to look through it.  */
5394       if (is_declaration
5395           && !typename_keyword_p
5396           && parser->scope
5397           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5398         parser->scope = resolve_typename_type (parser->scope,
5399                                                /*only_current_p=*/false);
5400       /* Parse the qualifying entity.  */
5401       new_scope
5402         = cp_parser_qualifying_entity (parser,
5403                                        typename_keyword_p,
5404                                        template_keyword_p,
5405                                        check_dependency_p,
5406                                        type_p,
5407                                        is_declaration);
5408       /* Look for the `::' token.  */
5409       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5410
5411       /* If we found what we wanted, we keep going; otherwise, we're
5412          done.  */
5413       if (!cp_parser_parse_definitely (parser))
5414         {
5415           bool error_p = false;
5416
5417           /* Restore the OLD_SCOPE since it was valid before the
5418              failed attempt at finding the last
5419              class-or-namespace-name.  */
5420           parser->scope = old_scope;
5421           parser->qualifying_scope = saved_qualifying_scope;
5422
5423           /* If the next token is a decltype, and the one after that is a
5424              `::', then the decltype has failed to resolve to a class or
5425              enumeration type.  Give this error even when parsing
5426              tentatively since it can't possibly be valid--and we're going
5427              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5428              won't get another chance.*/
5429           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5430               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5431                   == CPP_SCOPE))
5432             {
5433               token = cp_lexer_consume_token (parser->lexer);
5434               error_at (token->location, "decltype evaluates to %qT, "
5435                         "which is not a class or enumeration type",
5436                         token->u.value);
5437               parser->scope = error_mark_node;
5438               error_p = true;
5439               /* As below.  */
5440               success = true;
5441               cp_lexer_consume_token (parser->lexer);
5442             }
5443
5444           if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5445               && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5446             {
5447               /* If we have a non-type template-id followed by ::, it can't
5448                  possibly be valid.  */
5449               token = cp_lexer_peek_token (parser->lexer);
5450               tree tid = token->u.tree_check_value->value;
5451               if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5452                   && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5453                 {
5454                   tree tmpl = NULL_TREE;
5455                   if (is_overloaded_fn (tid))
5456                     {
5457                       tree fns = get_fns (tid);
5458                       if (!OVL_CHAIN (fns))
5459                         tmpl = OVL_CURRENT (fns);
5460                       error_at (token->location, "function template-id %qD "
5461                                 "in nested-name-specifier", tid);
5462                     }
5463                   else
5464                     {
5465                       /* Variable template.  */
5466                       tmpl = TREE_OPERAND (tid, 0);
5467                       gcc_assert (variable_template_p (tmpl));
5468                       error_at (token->location, "variable template-id %qD "
5469                                 "in nested-name-specifier", tid);
5470                     }
5471                   if (tmpl)
5472                     inform (DECL_SOURCE_LOCATION (tmpl),
5473                             "%qD declared here", tmpl);
5474
5475                   parser->scope = error_mark_node;
5476                   error_p = true;
5477                   /* As below.  */
5478                   success = true;
5479                   cp_lexer_consume_token (parser->lexer);
5480                   cp_lexer_consume_token (parser->lexer);
5481                 }
5482             }
5483
5484           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5485             break;
5486           /* If the next token is an identifier, and the one after
5487              that is a `::', then any valid interpretation would have
5488              found a class-or-namespace-name.  */
5489           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5490                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5491                      == CPP_SCOPE)
5492                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5493                      != CPP_COMPL))
5494             {
5495               token = cp_lexer_consume_token (parser->lexer);
5496               if (!error_p)
5497                 {
5498                   if (!token->error_reported)
5499                     {
5500                       tree decl;
5501                       tree ambiguous_decls;
5502
5503                       decl = cp_parser_lookup_name (parser, token->u.value,
5504                                                     none_type,
5505                                                     /*is_template=*/false,
5506                                                     /*is_namespace=*/false,
5507                                                     /*check_dependency=*/true,
5508                                                     &ambiguous_decls,
5509                                                     token->location);
5510                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5511                         error_at (token->location,
5512                                   "%qD used without template parameters",
5513                                   decl);
5514                       else if (ambiguous_decls)
5515                         {
5516                           // cp_parser_lookup_name has the same diagnostic,
5517                           // thus make sure to emit it at most once.
5518                           if (cp_parser_uncommitted_to_tentative_parse_p
5519                               (parser))
5520                             {
5521                               error_at (token->location,
5522                                         "reference to %qD is ambiguous",
5523                                         token->u.value);
5524                               print_candidates (ambiguous_decls);
5525                             }
5526                           decl = error_mark_node;
5527                         }
5528                       else
5529                         {
5530                           if (cxx_dialect != cxx98)
5531                             cp_parser_name_lookup_error
5532                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5533                              token->location);
5534                           else
5535                             cp_parser_name_lookup_error
5536                             (parser, token->u.value, decl, NLE_CXX98,
5537                              token->location);
5538                         }
5539                     }
5540                   parser->scope = error_mark_node;
5541                   error_p = true;
5542                   /* Treat this as a successful nested-name-specifier
5543                      due to:
5544
5545                      [basic.lookup.qual]
5546
5547                      If the name found is not a class-name (clause
5548                      _class_) or namespace-name (_namespace.def_), the
5549                      program is ill-formed.  */
5550                   success = true;
5551                 }
5552               cp_lexer_consume_token (parser->lexer);
5553             }
5554           break;
5555         }
5556       /* We've found one valid nested-name-specifier.  */
5557       success = true;
5558       /* Name lookup always gives us a DECL.  */
5559       if (TREE_CODE (new_scope) == TYPE_DECL)
5560         new_scope = TREE_TYPE (new_scope);
5561       /* Uses of "template" must be followed by actual templates.  */
5562       if (template_keyword_p
5563           && !(CLASS_TYPE_P (new_scope)
5564                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5565                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5566                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5567           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5568                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5569                    == TEMPLATE_ID_EXPR)))
5570         permerror (input_location, TYPE_P (new_scope)
5571                    ? G_("%qT is not a template")
5572                    : G_("%qD is not a template"),
5573                    new_scope);
5574       /* If it is a class scope, try to complete it; we are about to
5575          be looking up names inside the class.  */
5576       if (TYPE_P (new_scope)
5577           /* Since checking types for dependency can be expensive,
5578              avoid doing it if the type is already complete.  */
5579           && !COMPLETE_TYPE_P (new_scope)
5580           /* Do not try to complete dependent types.  */
5581           && !dependent_type_p (new_scope))
5582         {
5583           new_scope = complete_type (new_scope);
5584           /* If it is a typedef to current class, use the current
5585              class instead, as the typedef won't have any names inside
5586              it yet.  */
5587           if (!COMPLETE_TYPE_P (new_scope)
5588               && currently_open_class (new_scope))
5589             new_scope = TYPE_MAIN_VARIANT (new_scope);
5590         }
5591       /* Make sure we look in the right scope the next time through
5592          the loop.  */
5593       parser->scope = new_scope;
5594     }
5595
5596   /* If parsing tentatively, replace the sequence of tokens that makes
5597      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5598      token.  That way, should we re-parse the token stream, we will
5599      not have to repeat the effort required to do the parse, nor will
5600      we issue duplicate error messages.  */
5601   if (success && start)
5602     {
5603       cp_token *token;
5604
5605       token = cp_lexer_token_at (parser->lexer, start);
5606       /* Reset the contents of the START token.  */
5607       token->type = CPP_NESTED_NAME_SPECIFIER;
5608       /* Retrieve any deferred checks.  Do not pop this access checks yet
5609          so the memory will not be reclaimed during token replacing below.  */
5610       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5611       token->u.tree_check_value->value = parser->scope;
5612       token->u.tree_check_value->checks = get_deferred_access_checks ();
5613       token->u.tree_check_value->qualifying_scope =
5614         parser->qualifying_scope;
5615       token->keyword = RID_MAX;
5616
5617       /* Purge all subsequent tokens.  */
5618       cp_lexer_purge_tokens_after (parser->lexer, start);
5619     }
5620
5621   if (start)
5622     pop_to_parent_deferring_access_checks ();
5623
5624   return success ? parser->scope : NULL_TREE;
5625 }
5626
5627 /* Parse a nested-name-specifier.  See
5628    cp_parser_nested_name_specifier_opt for details.  This function
5629    behaves identically, except that it will an issue an error if no
5630    nested-name-specifier is present.  */
5631
5632 static tree
5633 cp_parser_nested_name_specifier (cp_parser *parser,
5634                                  bool typename_keyword_p,
5635                                  bool check_dependency_p,
5636                                  bool type_p,
5637                                  bool is_declaration)
5638 {
5639   tree scope;
5640
5641   /* Look for the nested-name-specifier.  */
5642   scope = cp_parser_nested_name_specifier_opt (parser,
5643                                                typename_keyword_p,
5644                                                check_dependency_p,
5645                                                type_p,
5646                                                is_declaration);
5647   /* If it was not present, issue an error message.  */
5648   if (!scope)
5649     {
5650       cp_parser_error (parser, "expected nested-name-specifier");
5651       parser->scope = NULL_TREE;
5652     }
5653
5654   return scope;
5655 }
5656
5657 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5658    this is either a class-name or a namespace-name (which corresponds
5659    to the class-or-namespace-name production in the grammar). For
5660    C++0x, it can also be a type-name that refers to an enumeration
5661    type or a simple-template-id.
5662
5663    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5664    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5665    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5666    TYPE_P is TRUE iff the next name should be taken as a class-name,
5667    even the same name is declared to be another entity in the same
5668    scope.
5669
5670    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5671    specified by the class-or-namespace-name.  If neither is found the
5672    ERROR_MARK_NODE is returned.  */
5673
5674 static tree
5675 cp_parser_qualifying_entity (cp_parser *parser,
5676                              bool typename_keyword_p,
5677                              bool template_keyword_p,
5678                              bool check_dependency_p,
5679                              bool type_p,
5680                              bool is_declaration)
5681 {
5682   tree saved_scope;
5683   tree saved_qualifying_scope;
5684   tree saved_object_scope;
5685   tree scope;
5686   bool only_class_p;
5687   bool successful_parse_p;
5688
5689   /* DR 743: decltype can appear in a nested-name-specifier.  */
5690   if (cp_lexer_next_token_is_decltype (parser->lexer))
5691     {
5692       scope = cp_parser_decltype (parser);
5693       if (TREE_CODE (scope) != ENUMERAL_TYPE
5694           && !MAYBE_CLASS_TYPE_P (scope))
5695         {
5696           cp_parser_simulate_error (parser);
5697           return error_mark_node;
5698         }
5699       if (TYPE_NAME (scope))
5700         scope = TYPE_NAME (scope);
5701       return scope;
5702     }
5703
5704   /* Before we try to parse the class-name, we must save away the
5705      current PARSER->SCOPE since cp_parser_class_name will destroy
5706      it.  */
5707   saved_scope = parser->scope;
5708   saved_qualifying_scope = parser->qualifying_scope;
5709   saved_object_scope = parser->object_scope;
5710   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5711      there is no need to look for a namespace-name.  */
5712   only_class_p = template_keyword_p 
5713     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5714   if (!only_class_p)
5715     cp_parser_parse_tentatively (parser);
5716   scope = cp_parser_class_name (parser,
5717                                 typename_keyword_p,
5718                                 template_keyword_p,
5719                                 type_p ? class_type : none_type,
5720                                 check_dependency_p,
5721                                 /*class_head_p=*/false,
5722                                 is_declaration);
5723   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5724   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5725   if (!only_class_p 
5726       && cxx_dialect != cxx98
5727       && !successful_parse_p)
5728     {
5729       /* Restore the saved scope.  */
5730       parser->scope = saved_scope;
5731       parser->qualifying_scope = saved_qualifying_scope;
5732       parser->object_scope = saved_object_scope;
5733
5734       /* Parse tentatively.  */
5735       cp_parser_parse_tentatively (parser);
5736      
5737       /* Parse a type-name  */
5738       scope = cp_parser_type_name (parser);
5739
5740       /* "If the name found does not designate a namespace or a class,
5741          enumeration, or dependent type, the program is ill-formed."
5742
5743          We cover classes and dependent types above and namespaces below,
5744          so this code is only looking for enums.  */
5745       if (!scope || TREE_CODE (scope) != TYPE_DECL
5746           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5747         cp_parser_simulate_error (parser);
5748
5749       successful_parse_p = cp_parser_parse_definitely (parser);
5750     }
5751   /* If that didn't work, try for a namespace-name.  */
5752   if (!only_class_p && !successful_parse_p)
5753     {
5754       /* Restore the saved scope.  */
5755       parser->scope = saved_scope;
5756       parser->qualifying_scope = saved_qualifying_scope;
5757       parser->object_scope = saved_object_scope;
5758       /* If we are not looking at an identifier followed by the scope
5759          resolution operator, then this is not part of a
5760          nested-name-specifier.  (Note that this function is only used
5761          to parse the components of a nested-name-specifier.)  */
5762       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5763           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5764         return error_mark_node;
5765       scope = cp_parser_namespace_name (parser);
5766     }
5767
5768   return scope;
5769 }
5770
5771 /* Return true if we are looking at a compound-literal, false otherwise.  */
5772
5773 static bool
5774 cp_parser_compound_literal_p (cp_parser *parser)
5775 {
5776   /* Consume the `('.  */
5777   cp_lexer_consume_token (parser->lexer);
5778
5779   cp_lexer_save_tokens (parser->lexer);
5780
5781   /* Skip tokens until the next token is a closing parenthesis.
5782      If we find the closing `)', and the next token is a `{', then
5783      we are looking at a compound-literal.  */
5784   bool compound_literal_p
5785     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5786                                               /*consume_paren=*/true)
5787        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5788   
5789   /* Roll back the tokens we skipped.  */
5790   cp_lexer_rollback_tokens (parser->lexer);
5791
5792   return compound_literal_p;
5793 }
5794
5795 /* Parse a postfix-expression.
5796
5797    postfix-expression:
5798      primary-expression
5799      postfix-expression [ expression ]
5800      postfix-expression ( expression-list [opt] )
5801      simple-type-specifier ( expression-list [opt] )
5802      typename :: [opt] nested-name-specifier identifier
5803        ( expression-list [opt] )
5804      typename :: [opt] nested-name-specifier template [opt] template-id
5805        ( expression-list [opt] )
5806      postfix-expression . template [opt] id-expression
5807      postfix-expression -> template [opt] id-expression
5808      postfix-expression . pseudo-destructor-name
5809      postfix-expression -> pseudo-destructor-name
5810      postfix-expression ++
5811      postfix-expression --
5812      dynamic_cast < type-id > ( expression )
5813      static_cast < type-id > ( expression )
5814      reinterpret_cast < type-id > ( expression )
5815      const_cast < type-id > ( expression )
5816      typeid ( expression )
5817      typeid ( type-id )
5818
5819    GNU Extension:
5820
5821    postfix-expression:
5822      ( type-id ) { initializer-list , [opt] }
5823
5824    This extension is a GNU version of the C99 compound-literal
5825    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5826    but they are essentially the same concept.)
5827
5828    If ADDRESS_P is true, the postfix expression is the operand of the
5829    `&' operator.  CAST_P is true if this expression is the target of a
5830    cast.
5831
5832    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5833    class member access expressions [expr.ref].
5834
5835    Returns a representation of the expression.  */
5836
5837 static tree
5838 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5839                               bool member_access_only_p, bool decltype_p,
5840                               cp_id_kind * pidk_return)
5841 {
5842   cp_token *token;
5843   location_t loc;
5844   enum rid keyword;
5845   cp_id_kind idk = CP_ID_KIND_NONE;
5846   tree postfix_expression = NULL_TREE;
5847   bool is_member_access = false;
5848   int saved_in_statement = -1;
5849
5850   /* Peek at the next token.  */
5851   token = cp_lexer_peek_token (parser->lexer);
5852   loc = token->location;
5853   /* Some of the productions are determined by keywords.  */
5854   keyword = token->keyword;
5855   switch (keyword)
5856     {
5857     case RID_DYNCAST:
5858     case RID_STATCAST:
5859     case RID_REINTCAST:
5860     case RID_CONSTCAST:
5861       {
5862         tree type;
5863         tree expression;
5864         const char *saved_message;
5865         bool saved_in_type_id_in_expr_p;
5866
5867         /* All of these can be handled in the same way from the point
5868            of view of parsing.  Begin by consuming the token
5869            identifying the cast.  */
5870         cp_lexer_consume_token (parser->lexer);
5871
5872         /* New types cannot be defined in the cast.  */
5873         saved_message = parser->type_definition_forbidden_message;
5874         parser->type_definition_forbidden_message
5875           = G_("types may not be defined in casts");
5876
5877         /* Look for the opening `<'.  */
5878         cp_parser_require (parser, CPP_LESS, RT_LESS);
5879         /* Parse the type to which we are casting.  */
5880         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5881         parser->in_type_id_in_expr_p = true;
5882         type = cp_parser_type_id (parser);
5883         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5884         /* Look for the closing `>'.  */
5885         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5886         /* Restore the old message.  */
5887         parser->type_definition_forbidden_message = saved_message;
5888
5889         bool saved_greater_than_is_operator_p
5890           = parser->greater_than_is_operator_p;
5891         parser->greater_than_is_operator_p = true;
5892
5893         /* And the expression which is being cast.  */
5894         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5895         expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5896         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5897
5898         parser->greater_than_is_operator_p
5899           = saved_greater_than_is_operator_p;
5900
5901         /* Only type conversions to integral or enumeration types
5902            can be used in constant-expressions.  */
5903         if (!cast_valid_in_integral_constant_expression_p (type)
5904             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5905           return error_mark_node;
5906
5907         switch (keyword)
5908           {
5909           case RID_DYNCAST:
5910             postfix_expression
5911               = build_dynamic_cast (type, expression, tf_warning_or_error);
5912             break;
5913           case RID_STATCAST:
5914             postfix_expression
5915               = build_static_cast (type, expression, tf_warning_or_error);
5916             break;
5917           case RID_REINTCAST:
5918             postfix_expression
5919               = build_reinterpret_cast (type, expression, 
5920                                         tf_warning_or_error);
5921             break;
5922           case RID_CONSTCAST:
5923             postfix_expression
5924               = build_const_cast (type, expression, tf_warning_or_error);
5925             break;
5926           default:
5927             gcc_unreachable ();
5928           }
5929       }
5930       break;
5931
5932     case RID_TYPEID:
5933       {
5934         tree type;
5935         const char *saved_message;
5936         bool saved_in_type_id_in_expr_p;
5937
5938         /* Consume the `typeid' token.  */
5939         cp_lexer_consume_token (parser->lexer);
5940         /* Look for the `(' token.  */
5941         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5942         /* Types cannot be defined in a `typeid' expression.  */
5943         saved_message = parser->type_definition_forbidden_message;
5944         parser->type_definition_forbidden_message
5945           = G_("types may not be defined in a %<typeid%> expression");
5946         /* We can't be sure yet whether we're looking at a type-id or an
5947            expression.  */
5948         cp_parser_parse_tentatively (parser);
5949         /* Try a type-id first.  */
5950         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5951         parser->in_type_id_in_expr_p = true;
5952         type = cp_parser_type_id (parser);
5953         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5954         /* Look for the `)' token.  Otherwise, we can't be sure that
5955            we're not looking at an expression: consider `typeid (int
5956            (3))', for example.  */
5957         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5958         /* If all went well, simply lookup the type-id.  */
5959         if (cp_parser_parse_definitely (parser))
5960           postfix_expression = get_typeid (type, tf_warning_or_error);
5961         /* Otherwise, fall back to the expression variant.  */
5962         else
5963           {
5964             tree expression;
5965
5966             /* Look for an expression.  */
5967             expression = cp_parser_expression (parser, & idk);
5968             /* Compute its typeid.  */
5969             postfix_expression = build_typeid (expression, tf_warning_or_error);
5970             /* Look for the `)' token.  */
5971             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5972           }
5973         /* Restore the saved message.  */
5974         parser->type_definition_forbidden_message = saved_message;
5975         /* `typeid' may not appear in an integral constant expression.  */
5976         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5977           return error_mark_node;
5978       }
5979       break;
5980
5981     case RID_TYPENAME:
5982       {
5983         tree type;
5984         /* The syntax permitted here is the same permitted for an
5985            elaborated-type-specifier.  */
5986         type = cp_parser_elaborated_type_specifier (parser,
5987                                                     /*is_friend=*/false,
5988                                                     /*is_declaration=*/false);
5989         postfix_expression = cp_parser_functional_cast (parser, type);
5990       }
5991       break;
5992
5993     case RID_CILK_SPAWN:
5994       {
5995         cp_lexer_consume_token (parser->lexer);
5996         token = cp_lexer_peek_token (parser->lexer);
5997         if (token->type == CPP_SEMICOLON)
5998           {
5999             error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6000                       "an expression");
6001             postfix_expression = error_mark_node;
6002             break;
6003           }
6004         else if (!current_function_decl)
6005           {
6006             error_at (token->location, "%<_Cilk_spawn%> may only be used "
6007                       "inside a function");
6008             postfix_expression = error_mark_node;
6009             break;
6010           }
6011         else
6012           {
6013             /* Consecutive _Cilk_spawns are not allowed in a statement.  */
6014             saved_in_statement = parser->in_statement;
6015             parser->in_statement |= IN_CILK_SPAWN;
6016           }
6017         cfun->calls_cilk_spawn = 1;
6018         postfix_expression = 
6019           cp_parser_postfix_expression (parser, false, false, 
6020                                         false, false, &idk);
6021         if (!flag_cilkplus)
6022           {
6023             error_at (token->location, "-fcilkplus must be enabled to use"
6024                       " %<_Cilk_spawn%>");
6025             cfun->calls_cilk_spawn = 0;
6026           }
6027         else if (saved_in_statement & IN_CILK_SPAWN)
6028           {
6029             error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6030                       "are not permitted");
6031             postfix_expression = error_mark_node;
6032             cfun->calls_cilk_spawn = 0; 
6033           }
6034         else
6035           {
6036             postfix_expression = build_cilk_spawn (token->location, 
6037                                                    postfix_expression);
6038             if (postfix_expression != error_mark_node) 
6039               SET_EXPR_LOCATION (postfix_expression, input_location);
6040             parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6041           }
6042         break;
6043       }
6044
6045     case RID_BUILTIN_SHUFFLE:
6046       {
6047         vec<tree, va_gc> *vec;
6048         unsigned int i;
6049         tree p;
6050
6051         cp_lexer_consume_token (parser->lexer);
6052         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6053                     /*cast_p=*/false, /*allow_expansion_p=*/true,
6054                     /*non_constant_p=*/NULL);
6055         if (vec == NULL)
6056           return error_mark_node;
6057
6058         FOR_EACH_VEC_ELT (*vec, i, p)
6059           mark_exp_read (p);
6060
6061         if (vec->length () == 2)
6062           return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6063                                          tf_warning_or_error);
6064         else if (vec->length () == 3)
6065           return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6066                                          tf_warning_or_error);
6067         else
6068         {
6069           error_at (loc, "wrong number of arguments to "
6070               "%<__builtin_shuffle%>");
6071           return error_mark_node;
6072         }
6073         break;
6074       }
6075
6076     default:
6077       {
6078         tree type;
6079
6080         /* If the next thing is a simple-type-specifier, we may be
6081            looking at a functional cast.  We could also be looking at
6082            an id-expression.  So, we try the functional cast, and if
6083            that doesn't work we fall back to the primary-expression.  */
6084         cp_parser_parse_tentatively (parser);
6085         /* Look for the simple-type-specifier.  */
6086         type = cp_parser_simple_type_specifier (parser,
6087                                                 /*decl_specs=*/NULL,
6088                                                 CP_PARSER_FLAGS_NONE);
6089         /* Parse the cast itself.  */
6090         if (!cp_parser_error_occurred (parser))
6091           postfix_expression
6092             = cp_parser_functional_cast (parser, type);
6093         /* If that worked, we're done.  */
6094         if (cp_parser_parse_definitely (parser))
6095           break;
6096
6097         /* If the functional-cast didn't work out, try a
6098            compound-literal.  */
6099         if (cp_parser_allow_gnu_extensions_p (parser)
6100             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6101           {
6102             tree initializer = NULL_TREE;
6103
6104             cp_parser_parse_tentatively (parser);
6105
6106             /* Avoid calling cp_parser_type_id pointlessly, see comment
6107                in cp_parser_cast_expression about c++/29234.  */
6108             if (!cp_parser_compound_literal_p (parser))
6109               cp_parser_simulate_error (parser);
6110             else
6111               {
6112                 /* Parse the type.  */
6113                 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6114                 parser->in_type_id_in_expr_p = true;
6115                 type = cp_parser_type_id (parser);
6116                 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6117                 /* Look for the `)'.  */
6118                 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6119               }
6120
6121             /* If things aren't going well, there's no need to
6122                keep going.  */
6123             if (!cp_parser_error_occurred (parser))
6124               {
6125                 bool non_constant_p;
6126                 /* Parse the brace-enclosed initializer list.  */
6127                 initializer = cp_parser_braced_list (parser,
6128                                                      &non_constant_p);
6129               }
6130             /* If that worked, we're definitely looking at a
6131                compound-literal expression.  */
6132             if (cp_parser_parse_definitely (parser))
6133               {
6134                 /* Warn the user that a compound literal is not
6135                    allowed in standard C++.  */
6136                 pedwarn (input_location, OPT_Wpedantic,
6137                          "ISO C++ forbids compound-literals");
6138                 /* For simplicity, we disallow compound literals in
6139                    constant-expressions.  We could
6140                    allow compound literals of integer type, whose
6141                    initializer was a constant, in constant
6142                    expressions.  Permitting that usage, as a further
6143                    extension, would not change the meaning of any
6144                    currently accepted programs.  (Of course, as
6145                    compound literals are not part of ISO C++, the
6146                    standard has nothing to say.)  */
6147                 if (cp_parser_non_integral_constant_expression (parser,
6148                                                                 NIC_NCC))
6149                   {
6150                     postfix_expression = error_mark_node;
6151                     break;
6152                   }
6153                 /* Form the representation of the compound-literal.  */
6154                 postfix_expression
6155                   = finish_compound_literal (type, initializer,
6156                                              tf_warning_or_error);
6157                 break;
6158               }
6159           }
6160
6161         /* It must be a primary-expression.  */
6162         postfix_expression
6163           = cp_parser_primary_expression (parser, address_p, cast_p,
6164                                           /*template_arg_p=*/false,
6165                                           decltype_p,
6166                                           &idk);
6167       }
6168       break;
6169     }
6170
6171   /* Note that we don't need to worry about calling build_cplus_new on a
6172      class-valued CALL_EXPR in decltype when it isn't the end of the
6173      postfix-expression; unary_complex_lvalue will take care of that for
6174      all these cases.  */
6175
6176   /* Keep looping until the postfix-expression is complete.  */
6177   while (true)
6178     {
6179       if (idk == CP_ID_KIND_UNQUALIFIED
6180           && identifier_p (postfix_expression)
6181           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6182         /* It is not a Koenig lookup function call.  */
6183         postfix_expression
6184           = unqualified_name_lookup_error (postfix_expression);
6185
6186       /* Peek at the next token.  */
6187       token = cp_lexer_peek_token (parser->lexer);
6188
6189       switch (token->type)
6190         {
6191         case CPP_OPEN_SQUARE:
6192           if (cp_next_tokens_can_be_std_attribute_p (parser))
6193             {
6194               cp_parser_error (parser,
6195                                "two consecutive %<[%> shall "
6196                                "only introduce an attribute");
6197               return error_mark_node;
6198             }
6199           postfix_expression
6200             = cp_parser_postfix_open_square_expression (parser,
6201                                                         postfix_expression,
6202                                                         false,
6203                                                         decltype_p);
6204           idk = CP_ID_KIND_NONE;
6205           is_member_access = false;
6206           break;
6207
6208         case CPP_OPEN_PAREN:
6209           /* postfix-expression ( expression-list [opt] ) */
6210           {
6211             bool koenig_p;
6212             bool is_builtin_constant_p;
6213             bool saved_integral_constant_expression_p = false;
6214             bool saved_non_integral_constant_expression_p = false;
6215             tsubst_flags_t complain = complain_flags (decltype_p);
6216             vec<tree, va_gc> *args;
6217
6218             is_member_access = false;
6219
6220             is_builtin_constant_p
6221               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6222             if (is_builtin_constant_p)
6223               {
6224                 /* The whole point of __builtin_constant_p is to allow
6225                    non-constant expressions to appear as arguments.  */
6226                 saved_integral_constant_expression_p
6227                   = parser->integral_constant_expression_p;
6228                 saved_non_integral_constant_expression_p
6229                   = parser->non_integral_constant_expression_p;
6230                 parser->integral_constant_expression_p = false;
6231               }
6232             args = (cp_parser_parenthesized_expression_list
6233                     (parser, non_attr,
6234                      /*cast_p=*/false, /*allow_expansion_p=*/true,
6235                      /*non_constant_p=*/NULL,
6236                      /*want_literal_zero_p=*/warn_memset_transposed_args));
6237             if (is_builtin_constant_p)
6238               {
6239                 parser->integral_constant_expression_p
6240                   = saved_integral_constant_expression_p;
6241                 parser->non_integral_constant_expression_p
6242                   = saved_non_integral_constant_expression_p;
6243               }
6244
6245             if (args == NULL)
6246               {
6247                 postfix_expression = error_mark_node;
6248                 break;
6249               }
6250
6251             /* Function calls are not permitted in
6252                constant-expressions.  */
6253             if (! builtin_valid_in_constant_expr_p (postfix_expression)
6254                 && cp_parser_non_integral_constant_expression (parser,
6255                                                                NIC_FUNC_CALL))
6256               {
6257                 postfix_expression = error_mark_node;
6258                 release_tree_vector (args);
6259                 break;
6260               }
6261
6262             koenig_p = false;
6263             if (idk == CP_ID_KIND_UNQUALIFIED
6264                 || idk == CP_ID_KIND_TEMPLATE_ID)
6265               {
6266                 if (identifier_p (postfix_expression))
6267                   {
6268                     if (!args->is_empty ())
6269                       {
6270                         koenig_p = true;
6271                         if (!any_type_dependent_arguments_p (args))
6272                           postfix_expression
6273                             = perform_koenig_lookup (postfix_expression, args,
6274                                                      complain);
6275                       }
6276                     else
6277                       postfix_expression
6278                         = unqualified_fn_lookup_error (postfix_expression);
6279                   }
6280                 /* We do not perform argument-dependent lookup if
6281                    normal lookup finds a non-function, in accordance
6282                    with the expected resolution of DR 218.  */
6283                 else if (!args->is_empty ()
6284                          && is_overloaded_fn (postfix_expression))
6285                   {
6286                     tree fn = get_first_fn (postfix_expression);
6287                     fn = STRIP_TEMPLATE (fn);
6288
6289                     /* Do not do argument dependent lookup if regular
6290                        lookup finds a member function or a block-scope
6291                        function declaration.  [basic.lookup.argdep]/3  */
6292                     if (!DECL_FUNCTION_MEMBER_P (fn)
6293                         && !DECL_LOCAL_FUNCTION_P (fn))
6294                       {
6295                         koenig_p = true;
6296                         if (!any_type_dependent_arguments_p (args))
6297                           postfix_expression
6298                             = perform_koenig_lookup (postfix_expression, args,
6299                                                      complain);
6300                       }
6301                   }
6302               }
6303
6304             if (warn_memset_transposed_args)
6305               {
6306                 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6307                     && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6308                     && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6309                     && vec_safe_length (args) == 3
6310                     && integer_zerop ((*args)[2])
6311                     && LITERAL_ZERO_P ((*args)[2])
6312                     && !(integer_zerop ((*args)[1])
6313                          && LITERAL_ZERO_P ((*args)[1])))
6314                   warning (OPT_Wmemset_transposed_args,
6315                            "%<memset%> used with constant zero length "
6316                            "parameter; this could be due to transposed "
6317                            "parameters");
6318
6319                 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6320                    to avoid leaking those into folder and middle-end.  */
6321                 unsigned int i;
6322                 tree arg;
6323                 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6324                   if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6325                     (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6326               }
6327
6328             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6329               {
6330                 tree instance = TREE_OPERAND (postfix_expression, 0);
6331                 tree fn = TREE_OPERAND (postfix_expression, 1);
6332
6333                 if (processing_template_decl
6334                     && (type_dependent_expression_p (instance)
6335                         || (!BASELINK_P (fn)
6336                             && TREE_CODE (fn) != FIELD_DECL)
6337                         || type_dependent_expression_p (fn)
6338                         || any_type_dependent_arguments_p (args)))
6339                   {
6340                     postfix_expression
6341                       = build_nt_call_vec (postfix_expression, args);
6342                     release_tree_vector (args);
6343                     break;
6344                   }
6345
6346                 if (BASELINK_P (fn))
6347                   {
6348                   postfix_expression
6349                     = (build_new_method_call
6350                        (instance, fn, &args, NULL_TREE,
6351                         (idk == CP_ID_KIND_QUALIFIED
6352                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6353                          : LOOKUP_NORMAL),
6354                         /*fn_p=*/NULL,
6355                         complain));
6356                   }
6357                 else
6358                   postfix_expression
6359                     = finish_call_expr (postfix_expression, &args,
6360                                         /*disallow_virtual=*/false,
6361                                         /*koenig_p=*/false,
6362                                         complain);
6363               }
6364             else if (TREE_CODE (postfix_expression) == OFFSET_REF
6365                      || TREE_CODE (postfix_expression) == MEMBER_REF
6366                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6367               postfix_expression = (build_offset_ref_call_from_tree
6368                                     (postfix_expression, &args,
6369                                      complain));
6370             else if (idk == CP_ID_KIND_QUALIFIED)
6371               /* A call to a static class member, or a namespace-scope
6372                  function.  */
6373               postfix_expression
6374                 = finish_call_expr (postfix_expression, &args,
6375                                     /*disallow_virtual=*/true,
6376                                     koenig_p,
6377                                     complain);
6378             else
6379               /* All other function calls.  */
6380               postfix_expression
6381                 = finish_call_expr (postfix_expression, &args,
6382                                     /*disallow_virtual=*/false,
6383                                     koenig_p,
6384                                     complain);
6385
6386             protected_set_expr_location (postfix_expression, token->location);
6387
6388             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
6389             idk = CP_ID_KIND_NONE;
6390
6391             release_tree_vector (args);
6392           }
6393           break;
6394
6395         case CPP_DOT:
6396         case CPP_DEREF:
6397           /* postfix-expression . template [opt] id-expression
6398              postfix-expression . pseudo-destructor-name
6399              postfix-expression -> template [opt] id-expression
6400              postfix-expression -> pseudo-destructor-name */
6401
6402           /* Consume the `.' or `->' operator.  */
6403           cp_lexer_consume_token (parser->lexer);
6404
6405           postfix_expression
6406             = cp_parser_postfix_dot_deref_expression (parser, token->type,
6407                                                       postfix_expression,
6408                                                       false, &idk, loc);
6409
6410           is_member_access = true;
6411           break;
6412
6413         case CPP_PLUS_PLUS:
6414           /* postfix-expression ++  */
6415           /* Consume the `++' token.  */
6416           cp_lexer_consume_token (parser->lexer);
6417           /* Generate a representation for the complete expression.  */
6418           postfix_expression
6419             = finish_increment_expr (postfix_expression,
6420                                      POSTINCREMENT_EXPR);
6421           /* Increments may not appear in constant-expressions.  */
6422           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6423             postfix_expression = error_mark_node;
6424           idk = CP_ID_KIND_NONE;
6425           is_member_access = false;
6426           break;
6427
6428         case CPP_MINUS_MINUS:
6429           /* postfix-expression -- */
6430           /* Consume the `--' token.  */
6431           cp_lexer_consume_token (parser->lexer);
6432           /* Generate a representation for the complete expression.  */
6433           postfix_expression
6434             = finish_increment_expr (postfix_expression,
6435                                      POSTDECREMENT_EXPR);
6436           /* Decrements may not appear in constant-expressions.  */
6437           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6438             postfix_expression = error_mark_node;
6439           idk = CP_ID_KIND_NONE;
6440           is_member_access = false;
6441           break;
6442
6443         default:
6444           if (pidk_return != NULL)
6445             * pidk_return = idk;
6446           if (member_access_only_p)
6447             return is_member_access? postfix_expression : error_mark_node;
6448           else
6449             return postfix_expression;
6450         }
6451     }
6452
6453   /* We should never get here.  */
6454   gcc_unreachable ();
6455   return error_mark_node;
6456 }
6457
6458 /* This function parses Cilk Plus array notations.  If a normal array expr. is
6459    parsed then the array index is passed back to the caller through *INIT_INDEX 
6460    and the function returns a NULL_TREE.  If array notation expr. is parsed, 
6461    then *INIT_INDEX is ignored by the caller and the function returns 
6462    a tree of type ARRAY_NOTATION_REF.  If some error occurred it returns 
6463    error_mark_node.  */
6464
6465 static tree
6466 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6467                           tree array_value)
6468 {
6469   cp_token *token = NULL;
6470   tree length_index, stride = NULL_TREE, value_tree, array_type;
6471   if (!array_value || array_value == error_mark_node)
6472     {
6473       cp_parser_skip_to_end_of_statement (parser);
6474       return error_mark_node;
6475     }
6476
6477   array_type = TREE_TYPE (array_value);
6478   
6479   bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6480   parser->colon_corrects_to_scope_p = false;
6481   token = cp_lexer_peek_token (parser->lexer);
6482   
6483   if (!token)
6484     {
6485       cp_parser_error (parser, "expected %<:%> or numeral");
6486       return error_mark_node;
6487     }
6488   else if (token->type == CPP_COLON)
6489     {
6490       /* Consume the ':'.  */
6491       cp_lexer_consume_token (parser->lexer);
6492       
6493       /* If we are here, then we have a case like this A[:].  */
6494       if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6495         {
6496           cp_parser_error (parser, "expected %<]%>");
6497           cp_parser_skip_to_end_of_statement (parser);
6498           return error_mark_node;
6499         }
6500       *init_index = NULL_TREE;
6501       stride = NULL_TREE;
6502       length_index = NULL_TREE;
6503     }
6504   else
6505     {
6506       /* If we are here, then there are three valid possibilities:
6507          1. ARRAY [ EXP ]
6508          2. ARRAY [ EXP : EXP ]
6509          3. ARRAY [ EXP : EXP : EXP ]  */
6510
6511       *init_index = cp_parser_expression (parser);      
6512       if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6513         {  
6514           /* This indicates that we have a normal array expression.  */
6515           parser->colon_corrects_to_scope_p = saved_colon_corrects;
6516           return NULL_TREE;
6517         }
6518       
6519       /* Consume the ':'.  */
6520       cp_lexer_consume_token (parser->lexer);
6521       length_index = cp_parser_expression (parser);
6522       if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6523         {
6524           cp_lexer_consume_token (parser->lexer);
6525           stride = cp_parser_expression (parser);
6526         }
6527     }
6528   parser->colon_corrects_to_scope_p = saved_colon_corrects;
6529
6530   if (*init_index == error_mark_node || length_index == error_mark_node
6531       || stride == error_mark_node || array_type == error_mark_node)
6532     {
6533       if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6534         cp_lexer_consume_token (parser->lexer);
6535       return error_mark_node;
6536     }
6537   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6538
6539   value_tree = build_array_notation_ref (loc, array_value, *init_index, 
6540                                          length_index, stride, array_type);
6541   return value_tree;
6542 }
6543
6544 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6545    by cp_parser_builtin_offsetof.  We're looking for
6546
6547      postfix-expression [ expression ]
6548      postfix-expression [ braced-init-list ] (C++11)
6549
6550    FOR_OFFSETOF is set if we're being called in that context, which
6551    changes how we deal with integer constant expressions.  */
6552
6553 static tree
6554 cp_parser_postfix_open_square_expression (cp_parser *parser,
6555                                           tree postfix_expression,
6556                                           bool for_offsetof,
6557                                           bool decltype_p)
6558 {
6559   tree index = NULL_TREE;
6560   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6561   bool saved_greater_than_is_operator_p;
6562
6563   /* Consume the `[' token.  */
6564   cp_lexer_consume_token (parser->lexer);
6565
6566   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6567   parser->greater_than_is_operator_p = true;
6568
6569   /* Parse the index expression.  */
6570   /* ??? For offsetof, there is a question of what to allow here.  If
6571      offsetof is not being used in an integral constant expression context,
6572      then we *could* get the right answer by computing the value at runtime.
6573      If we are in an integral constant expression context, then we might
6574      could accept any constant expression; hard to say without analysis.
6575      Rather than open the barn door too wide right away, allow only integer
6576      constant expressions here.  */
6577   if (for_offsetof)
6578     index = cp_parser_constant_expression (parser);
6579   else
6580     {
6581       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6582         {
6583           bool expr_nonconst_p;
6584           cp_lexer_set_source_position (parser->lexer);
6585           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6586           index = cp_parser_braced_list (parser, &expr_nonconst_p);
6587           if (flag_cilkplus
6588               && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6589             {
6590               error_at (cp_lexer_peek_token (parser->lexer)->location,
6591                         "braced list index is not allowed with array "
6592                         "notation");
6593               cp_parser_skip_to_end_of_statement (parser);
6594               return error_mark_node;
6595             }
6596         }
6597       else if (flag_cilkplus)
6598         {
6599           /* Here are have these two options:
6600              ARRAY[EXP : EXP]        - Array notation expr with default
6601              stride of 1.
6602              ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6603              stride.  */
6604           tree an_exp = cp_parser_array_notation (loc, parser, &index, 
6605                                                   postfix_expression);
6606           if (an_exp)
6607             return an_exp;
6608         }
6609       else
6610         index = cp_parser_expression (parser);
6611     }
6612
6613   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6614
6615   /* Look for the closing `]'.  */
6616   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6617
6618   /* Build the ARRAY_REF.  */
6619   postfix_expression = grok_array_decl (loc, postfix_expression,
6620                                         index, decltype_p);
6621
6622   /* When not doing offsetof, array references are not permitted in
6623      constant-expressions.  */
6624   if (!for_offsetof
6625       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6626     postfix_expression = error_mark_node;
6627
6628   return postfix_expression;
6629 }
6630
6631 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6632    by cp_parser_builtin_offsetof.  We're looking for
6633
6634      postfix-expression . template [opt] id-expression
6635      postfix-expression . pseudo-destructor-name
6636      postfix-expression -> template [opt] id-expression
6637      postfix-expression -> pseudo-destructor-name
6638
6639    FOR_OFFSETOF is set if we're being called in that context.  That sorta
6640    limits what of the above we'll actually accept, but nevermind.
6641    TOKEN_TYPE is the "." or "->" token, which will already have been
6642    removed from the stream.  */
6643
6644 static tree
6645 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6646                                         enum cpp_ttype token_type,
6647                                         tree postfix_expression,
6648                                         bool for_offsetof, cp_id_kind *idk,
6649                                         location_t location)
6650 {
6651   tree name;
6652   bool dependent_p;
6653   bool pseudo_destructor_p;
6654   tree scope = NULL_TREE;
6655
6656   /* If this is a `->' operator, dereference the pointer.  */
6657   if (token_type == CPP_DEREF)
6658     postfix_expression = build_x_arrow (location, postfix_expression,
6659                                         tf_warning_or_error);
6660   /* Check to see whether or not the expression is type-dependent.  */
6661   dependent_p = type_dependent_expression_p (postfix_expression);
6662   /* The identifier following the `->' or `.' is not qualified.  */
6663   parser->scope = NULL_TREE;
6664   parser->qualifying_scope = NULL_TREE;
6665   parser->object_scope = NULL_TREE;
6666   *idk = CP_ID_KIND_NONE;
6667
6668   /* Enter the scope corresponding to the type of the object
6669      given by the POSTFIX_EXPRESSION.  */
6670   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6671     {
6672       scope = TREE_TYPE (postfix_expression);
6673       /* According to the standard, no expression should ever have
6674          reference type.  Unfortunately, we do not currently match
6675          the standard in this respect in that our internal representation
6676          of an expression may have reference type even when the standard
6677          says it does not.  Therefore, we have to manually obtain the
6678          underlying type here.  */
6679       scope = non_reference (scope);
6680       /* The type of the POSTFIX_EXPRESSION must be complete.  */
6681       if (scope == unknown_type_node)
6682         {
6683           error_at (location, "%qE does not have class type",
6684                     postfix_expression);
6685           scope = NULL_TREE;
6686         }
6687       /* Unlike the object expression in other contexts, *this is not
6688          required to be of complete type for purposes of class member
6689          access (5.2.5) outside the member function body.  */
6690       else if (postfix_expression != current_class_ref
6691                && !(processing_template_decl && scope == current_class_type))
6692         scope = complete_type_or_else (scope, NULL_TREE);
6693       /* Let the name lookup machinery know that we are processing a
6694          class member access expression.  */
6695       parser->context->object_type = scope;
6696       /* If something went wrong, we want to be able to discern that case,
6697          as opposed to the case where there was no SCOPE due to the type
6698          of expression being dependent.  */
6699       if (!scope)
6700         scope = error_mark_node;
6701       /* If the SCOPE was erroneous, make the various semantic analysis
6702          functions exit quickly -- and without issuing additional error
6703          messages.  */
6704       if (scope == error_mark_node)
6705         postfix_expression = error_mark_node;
6706     }
6707
6708   /* Assume this expression is not a pseudo-destructor access.  */
6709   pseudo_destructor_p = false;
6710
6711   /* If the SCOPE is a scalar type, then, if this is a valid program,
6712      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
6713      is type dependent, it can be pseudo-destructor-name or something else.
6714      Try to parse it as pseudo-destructor-name first.  */
6715   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6716     {
6717       tree s;
6718       tree type;
6719
6720       cp_parser_parse_tentatively (parser);
6721       /* Parse the pseudo-destructor-name.  */
6722       s = NULL_TREE;
6723       cp_parser_pseudo_destructor_name (parser, postfix_expression,
6724                                         &s, &type);
6725       if (dependent_p
6726           && (cp_parser_error_occurred (parser)
6727               || !SCALAR_TYPE_P (type)))
6728         cp_parser_abort_tentative_parse (parser);
6729       else if (cp_parser_parse_definitely (parser))
6730         {
6731           pseudo_destructor_p = true;
6732           postfix_expression
6733             = finish_pseudo_destructor_expr (postfix_expression,
6734                                              s, type, location);
6735         }
6736     }
6737
6738   if (!pseudo_destructor_p)
6739     {
6740       /* If the SCOPE is not a scalar type, we are looking at an
6741          ordinary class member access expression, rather than a
6742          pseudo-destructor-name.  */
6743       bool template_p;
6744       cp_token *token = cp_lexer_peek_token (parser->lexer);
6745       /* Parse the id-expression.  */
6746       name = (cp_parser_id_expression
6747               (parser,
6748                cp_parser_optional_template_keyword (parser),
6749                /*check_dependency_p=*/true,
6750                &template_p,
6751                /*declarator_p=*/false,
6752                /*optional_p=*/false));
6753       /* In general, build a SCOPE_REF if the member name is qualified.
6754          However, if the name was not dependent and has already been
6755          resolved; there is no need to build the SCOPE_REF.  For example;
6756
6757              struct X { void f(); };
6758              template <typename T> void f(T* t) { t->X::f(); }
6759
6760          Even though "t" is dependent, "X::f" is not and has been resolved
6761          to a BASELINK; there is no need to include scope information.  */
6762
6763       /* But we do need to remember that there was an explicit scope for
6764          virtual function calls.  */
6765       if (parser->scope)
6766         *idk = CP_ID_KIND_QUALIFIED;
6767
6768       /* If the name is a template-id that names a type, we will get a
6769          TYPE_DECL here.  That is invalid code.  */
6770       if (TREE_CODE (name) == TYPE_DECL)
6771         {
6772           error_at (token->location, "invalid use of %qD", name);
6773           postfix_expression = error_mark_node;
6774         }
6775       else
6776         {
6777           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6778             {
6779               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6780                 {
6781                   error_at (token->location, "%<%D::%D%> is not a class member",
6782                             parser->scope, name);
6783                   postfix_expression = error_mark_node;
6784                 }
6785               else
6786                 name = build_qualified_name (/*type=*/NULL_TREE,
6787                                              parser->scope,
6788                                              name,
6789                                              template_p);
6790               parser->scope = NULL_TREE;
6791               parser->qualifying_scope = NULL_TREE;
6792               parser->object_scope = NULL_TREE;
6793             }
6794           if (parser->scope && name && BASELINK_P (name))
6795             adjust_result_of_qualified_name_lookup
6796               (name, parser->scope, scope);
6797           postfix_expression
6798             = finish_class_member_access_expr (postfix_expression, name,
6799                                                template_p, 
6800                                                tf_warning_or_error);
6801         }
6802     }
6803
6804   /* We no longer need to look up names in the scope of the object on
6805      the left-hand side of the `.' or `->' operator.  */
6806   parser->context->object_type = NULL_TREE;
6807
6808   /* Outside of offsetof, these operators may not appear in
6809      constant-expressions.  */
6810   if (!for_offsetof
6811       && (cp_parser_non_integral_constant_expression
6812           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6813     postfix_expression = error_mark_node;
6814
6815   return postfix_expression;
6816 }
6817
6818 /* Cache of LITERAL_ZERO_P constants.  */
6819
6820 static GTY(()) tree literal_zeros[itk_none];
6821
6822 /* Parse a parenthesized expression-list.
6823
6824    expression-list:
6825      assignment-expression
6826      expression-list, assignment-expression
6827
6828    attribute-list:
6829      expression-list
6830      identifier
6831      identifier, expression-list
6832
6833    CAST_P is true if this expression is the target of a cast.
6834
6835    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6836    argument pack.
6837
6838    Returns a vector of trees.  Each element is a representation of an
6839    assignment-expression.  NULL is returned if the ( and or ) are
6840    missing.  An empty, but allocated, vector is returned on no
6841    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6842    if we are parsing an attribute list for an attribute that wants a
6843    plain identifier argument, normal_attr for an attribute that wants
6844    an expression, or non_attr if we aren't parsing an attribute list.  If
6845    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6846    not all of the expressions in the list were constant.
6847    WANT_LITERAL_ZERO_P is true if the caller is interested in
6848    LITERAL_ZERO_P INTEGER_CSTs.  FIXME: once we don't fold everything
6849    immediately, this can be removed.  */
6850
6851 static vec<tree, va_gc> *
6852 cp_parser_parenthesized_expression_list (cp_parser* parser,
6853                                          int is_attribute_list,
6854                                          bool cast_p,
6855                                          bool allow_expansion_p,
6856                                          bool *non_constant_p,
6857                                          bool want_literal_zero_p)
6858 {
6859   vec<tree, va_gc> *expression_list;
6860   bool fold_expr_p = is_attribute_list != non_attr;
6861   tree identifier = NULL_TREE;
6862   bool saved_greater_than_is_operator_p;
6863
6864   /* Assume all the expressions will be constant.  */
6865   if (non_constant_p)
6866     *non_constant_p = false;
6867
6868   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6869     return NULL;
6870
6871   expression_list = make_tree_vector ();
6872
6873   /* Within a parenthesized expression, a `>' token is always
6874      the greater-than operator.  */
6875   saved_greater_than_is_operator_p
6876     = parser->greater_than_is_operator_p;
6877   parser->greater_than_is_operator_p = true;
6878
6879   /* Consume expressions until there are no more.  */
6880   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6881     while (true)
6882       {
6883         tree expr;
6884
6885         /* At the beginning of attribute lists, check to see if the
6886            next token is an identifier.  */
6887         if (is_attribute_list == id_attr
6888             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6889           {
6890             cp_token *token;
6891
6892             /* Consume the identifier.  */
6893             token = cp_lexer_consume_token (parser->lexer);
6894             /* Save the identifier.  */
6895             identifier = token->u.value;
6896           }
6897         else
6898           {
6899             bool expr_non_constant_p;
6900
6901             /* Parse the next assignment-expression.  */
6902             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6903               {
6904                 /* A braced-init-list.  */
6905                 cp_lexer_set_source_position (parser->lexer);
6906                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6907                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6908                 if (non_constant_p && expr_non_constant_p)
6909                   *non_constant_p = true;
6910               }
6911             else if (non_constant_p)
6912               {
6913                 expr = (cp_parser_constant_expression
6914                         (parser, /*allow_non_constant_p=*/true,
6915                          &expr_non_constant_p));
6916                 if (expr_non_constant_p)
6917                   *non_constant_p = true;
6918               }
6919             else
6920               {
6921                 expr = NULL_TREE;
6922                 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6923                 switch (tok->type)
6924                   {
6925                   case CPP_NUMBER:
6926                   case CPP_CHAR:
6927                   case CPP_WCHAR:
6928                   case CPP_CHAR16:
6929                   case CPP_CHAR32:
6930                     /* If a parameter is literal zero alone, remember it
6931                        for -Wmemset-transposed-args warning.  */
6932                     if (integer_zerop (tok->u.value)
6933                         && !TREE_OVERFLOW (tok->u.value)
6934                         && want_literal_zero_p
6935                         && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6936                             == CPP_COMMA
6937                             || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6938                                == CPP_CLOSE_PAREN))
6939                       {
6940                         unsigned int i;
6941                         for (i = 0; i < itk_none; ++i)
6942                           if (TREE_TYPE (tok->u.value) == integer_types[i])
6943                             break;
6944                         if (i < itk_none && literal_zeros[i])
6945                           expr = literal_zeros[i];
6946                         else
6947                           {
6948                             expr = copy_node (tok->u.value);
6949                             LITERAL_ZERO_P (expr) = 1;
6950                             if (i < itk_none)
6951                               literal_zeros[i] = expr;
6952                           }
6953                         /* Consume the 0 token (or '\0', 0LL etc.).  */
6954                         cp_lexer_consume_token (parser->lexer);
6955                       }
6956                     break;
6957                   default:
6958                     break;
6959                   }
6960                 if (expr == NULL_TREE)
6961                   expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6962                                                           cast_p);
6963               }
6964
6965             if (fold_expr_p)
6966               expr = instantiate_non_dependent_expr (expr);
6967
6968             /* If we have an ellipsis, then this is an expression
6969                expansion.  */
6970             if (allow_expansion_p
6971                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6972               {
6973                 /* Consume the `...'.  */
6974                 cp_lexer_consume_token (parser->lexer);
6975
6976                 /* Build the argument pack.  */
6977                 expr = make_pack_expansion (expr);
6978               }
6979
6980              /* Add it to the list.  We add error_mark_node
6981                 expressions to the list, so that we can still tell if
6982                 the correct form for a parenthesized expression-list
6983                 is found. That gives better errors.  */
6984             vec_safe_push (expression_list, expr);
6985
6986             if (expr == error_mark_node)
6987               goto skip_comma;
6988           }
6989
6990         /* After the first item, attribute lists look the same as
6991            expression lists.  */
6992         is_attribute_list = non_attr;
6993
6994       get_comma:;
6995         /* If the next token isn't a `,', then we are done.  */
6996         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6997           break;
6998
6999         /* Otherwise, consume the `,' and keep going.  */
7000         cp_lexer_consume_token (parser->lexer);
7001       }
7002
7003   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7004     {
7005       int ending;
7006
7007     skip_comma:;
7008       /* We try and resync to an unnested comma, as that will give the
7009          user better diagnostics.  */
7010       ending = cp_parser_skip_to_closing_parenthesis (parser,
7011                                                       /*recovering=*/true,
7012                                                       /*or_comma=*/true,
7013                                                       /*consume_paren=*/true);
7014       if (ending < 0)
7015         goto get_comma;
7016       if (!ending)
7017         {
7018           parser->greater_than_is_operator_p
7019             = saved_greater_than_is_operator_p;
7020           return NULL;
7021         }
7022     }
7023
7024   parser->greater_than_is_operator_p
7025     = saved_greater_than_is_operator_p;
7026
7027   if (identifier)
7028     vec_safe_insert (expression_list, 0, identifier);
7029
7030   return expression_list;
7031 }
7032
7033 /* Parse a pseudo-destructor-name.
7034
7035    pseudo-destructor-name:
7036      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7037      :: [opt] nested-name-specifier template template-id :: ~ type-name
7038      :: [opt] nested-name-specifier [opt] ~ type-name
7039
7040    If either of the first two productions is used, sets *SCOPE to the
7041    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7042    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7043    or ERROR_MARK_NODE if the parse fails.  */
7044
7045 static void
7046 cp_parser_pseudo_destructor_name (cp_parser* parser,
7047                                   tree object,
7048                                   tree* scope,
7049                                   tree* type)
7050 {
7051   bool nested_name_specifier_p;
7052
7053   /* Handle ~auto.  */
7054   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7055       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7056       && !type_dependent_expression_p (object))
7057     {
7058       if (cxx_dialect < cxx14)
7059         pedwarn (input_location, 0,
7060                  "%<~auto%> only available with "
7061                  "-std=c++14 or -std=gnu++14");
7062       cp_lexer_consume_token (parser->lexer);
7063       cp_lexer_consume_token (parser->lexer);
7064       *scope = NULL_TREE;
7065       *type = TREE_TYPE (object);
7066       return;
7067     }
7068
7069   /* Assume that things will not work out.  */
7070   *type = error_mark_node;
7071
7072   /* Look for the optional `::' operator.  */
7073   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7074   /* Look for the optional nested-name-specifier.  */
7075   nested_name_specifier_p
7076     = (cp_parser_nested_name_specifier_opt (parser,
7077                                             /*typename_keyword_p=*/false,
7078                                             /*check_dependency_p=*/true,
7079                                             /*type_p=*/false,
7080                                             /*is_declaration=*/false)
7081        != NULL_TREE);
7082   /* Now, if we saw a nested-name-specifier, we might be doing the
7083      second production.  */
7084   if (nested_name_specifier_p
7085       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7086     {
7087       /* Consume the `template' keyword.  */
7088       cp_lexer_consume_token (parser->lexer);
7089       /* Parse the template-id.  */
7090       cp_parser_template_id (parser,
7091                              /*template_keyword_p=*/true,
7092                              /*check_dependency_p=*/false,
7093                              class_type,
7094                              /*is_declaration=*/true);
7095       /* Look for the `::' token.  */
7096       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7097     }
7098   /* If the next token is not a `~', then there might be some
7099      additional qualification.  */
7100   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7101     {
7102       /* At this point, we're looking for "type-name :: ~".  The type-name
7103          must not be a class-name, since this is a pseudo-destructor.  So,
7104          it must be either an enum-name, or a typedef-name -- both of which
7105          are just identifiers.  So, we peek ahead to check that the "::"
7106          and "~" tokens are present; if they are not, then we can avoid
7107          calling type_name.  */
7108       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7109           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7110           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7111         {
7112           cp_parser_error (parser, "non-scalar type");
7113           return;
7114         }
7115
7116       /* Look for the type-name.  */
7117       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7118       if (*scope == error_mark_node)
7119         return;
7120
7121       /* Look for the `::' token.  */
7122       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7123     }
7124   else
7125     *scope = NULL_TREE;
7126
7127   /* Look for the `~'.  */
7128   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7129
7130   /* Once we see the ~, this has to be a pseudo-destructor.  */
7131   if (!processing_template_decl && !cp_parser_error_occurred (parser))
7132     cp_parser_commit_to_topmost_tentative_parse (parser);
7133
7134   /* Look for the type-name again.  We are not responsible for
7135      checking that it matches the first type-name.  */
7136   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7137 }
7138
7139 /* Parse a unary-expression.
7140
7141    unary-expression:
7142      postfix-expression
7143      ++ cast-expression
7144      -- cast-expression
7145      unary-operator cast-expression
7146      sizeof unary-expression
7147      sizeof ( type-id )
7148      alignof ( type-id )  [C++0x]
7149      new-expression
7150      delete-expression
7151
7152    GNU Extensions:
7153
7154    unary-expression:
7155      __extension__ cast-expression
7156      __alignof__ unary-expression
7157      __alignof__ ( type-id )
7158      alignof unary-expression  [C++0x]
7159      __real__ cast-expression
7160      __imag__ cast-expression
7161      && identifier
7162      sizeof ( type-id ) { initializer-list , [opt] }
7163      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7164      __alignof__ ( type-id ) { initializer-list , [opt] }
7165
7166    ADDRESS_P is true iff the unary-expression is appearing as the
7167    operand of the `&' operator.   CAST_P is true if this expression is
7168    the target of a cast.
7169
7170    Returns a representation of the expression.  */
7171
7172 static tree
7173 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7174                             bool address_p, bool cast_p, bool decltype_p)
7175 {
7176   cp_token *token;
7177   enum tree_code unary_operator;
7178
7179   /* Peek at the next token.  */
7180   token = cp_lexer_peek_token (parser->lexer);
7181   /* Some keywords give away the kind of expression.  */
7182   if (token->type == CPP_KEYWORD)
7183     {
7184       enum rid keyword = token->keyword;
7185
7186       switch (keyword)
7187         {
7188         case RID_ALIGNOF:
7189         case RID_SIZEOF:
7190           {
7191             tree operand, ret;
7192             enum tree_code op;
7193             location_t first_loc;
7194
7195             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7196             /* Consume the token.  */
7197             cp_lexer_consume_token (parser->lexer);
7198             first_loc = cp_lexer_peek_token (parser->lexer)->location;
7199             /* Parse the operand.  */
7200             operand = cp_parser_sizeof_operand (parser, keyword);
7201
7202             if (TYPE_P (operand))
7203               ret = cxx_sizeof_or_alignof_type (operand, op, true);
7204             else
7205               {
7206                 /* ISO C++ defines alignof only with types, not with
7207                    expressions. So pedwarn if alignof is used with a non-
7208                    type expression. However, __alignof__ is ok.  */
7209                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7210                   pedwarn (token->location, OPT_Wpedantic,
7211                            "ISO C++ does not allow %<alignof%> "
7212                            "with a non-type");
7213
7214                 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7215               }
7216             /* For SIZEOF_EXPR, just issue diagnostics, but keep
7217                SIZEOF_EXPR with the original operand.  */
7218             if (op == SIZEOF_EXPR && ret != error_mark_node)
7219               {
7220                 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7221                   {
7222                     if (!processing_template_decl && TYPE_P (operand))
7223                       {
7224                         ret = build_min (SIZEOF_EXPR, size_type_node,
7225                                          build1 (NOP_EXPR, operand,
7226                                                  error_mark_node));
7227                         SIZEOF_EXPR_TYPE_P (ret) = 1;
7228                       }
7229                     else
7230                       ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7231                     TREE_SIDE_EFFECTS (ret) = 0;
7232                     TREE_READONLY (ret) = 1;
7233                   }
7234                 SET_EXPR_LOCATION (ret, first_loc);
7235               }
7236             return ret;
7237           }
7238
7239         case RID_NEW:
7240           return cp_parser_new_expression (parser);
7241
7242         case RID_DELETE:
7243           return cp_parser_delete_expression (parser);
7244
7245         case RID_EXTENSION:
7246           {
7247             /* The saved value of the PEDANTIC flag.  */
7248             int saved_pedantic;
7249             tree expr;
7250
7251             /* Save away the PEDANTIC flag.  */
7252             cp_parser_extension_opt (parser, &saved_pedantic);
7253             /* Parse the cast-expression.  */
7254             expr = cp_parser_simple_cast_expression (parser);
7255             /* Restore the PEDANTIC flag.  */
7256             pedantic = saved_pedantic;
7257
7258             return expr;
7259           }
7260
7261         case RID_REALPART:
7262         case RID_IMAGPART:
7263           {
7264             tree expression;
7265
7266             /* Consume the `__real__' or `__imag__' token.  */
7267             cp_lexer_consume_token (parser->lexer);
7268             /* Parse the cast-expression.  */
7269             expression = cp_parser_simple_cast_expression (parser);
7270             /* Create the complete representation.  */
7271             return build_x_unary_op (token->location,
7272                                      (keyword == RID_REALPART
7273                                       ? REALPART_EXPR : IMAGPART_EXPR),
7274                                      expression,
7275                                      tf_warning_or_error);
7276           }
7277           break;
7278
7279         case RID_TRANSACTION_ATOMIC:
7280         case RID_TRANSACTION_RELAXED:
7281           return cp_parser_transaction_expression (parser, keyword);
7282
7283         case RID_NOEXCEPT:
7284           {
7285             tree expr;
7286             const char *saved_message;
7287             bool saved_integral_constant_expression_p;
7288             bool saved_non_integral_constant_expression_p;
7289             bool saved_greater_than_is_operator_p;
7290
7291             cp_lexer_consume_token (parser->lexer);
7292             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7293
7294             saved_message = parser->type_definition_forbidden_message;
7295             parser->type_definition_forbidden_message
7296               = G_("types may not be defined in %<noexcept%> expressions");
7297
7298             saved_integral_constant_expression_p
7299               = parser->integral_constant_expression_p;
7300             saved_non_integral_constant_expression_p
7301               = parser->non_integral_constant_expression_p;
7302             parser->integral_constant_expression_p = false;
7303
7304             saved_greater_than_is_operator_p
7305               = parser->greater_than_is_operator_p;
7306             parser->greater_than_is_operator_p = true;
7307
7308             ++cp_unevaluated_operand;
7309             ++c_inhibit_evaluation_warnings;
7310             ++cp_noexcept_operand;
7311             expr = cp_parser_expression (parser);
7312             --cp_noexcept_operand;
7313             --c_inhibit_evaluation_warnings;
7314             --cp_unevaluated_operand;
7315
7316             parser->greater_than_is_operator_p
7317               = saved_greater_than_is_operator_p;
7318
7319             parser->integral_constant_expression_p
7320               = saved_integral_constant_expression_p;
7321             parser->non_integral_constant_expression_p
7322               = saved_non_integral_constant_expression_p;
7323
7324             parser->type_definition_forbidden_message = saved_message;
7325
7326             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7327             return finish_noexcept_expr (expr, tf_warning_or_error);
7328           }
7329
7330         default:
7331           break;
7332         }
7333     }
7334
7335   /* Look for the `:: new' and `:: delete', which also signal the
7336      beginning of a new-expression, or delete-expression,
7337      respectively.  If the next token is `::', then it might be one of
7338      these.  */
7339   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7340     {
7341       enum rid keyword;
7342
7343       /* See if the token after the `::' is one of the keywords in
7344          which we're interested.  */
7345       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7346       /* If it's `new', we have a new-expression.  */
7347       if (keyword == RID_NEW)
7348         return cp_parser_new_expression (parser);
7349       /* Similarly, for `delete'.  */
7350       else if (keyword == RID_DELETE)
7351         return cp_parser_delete_expression (parser);
7352     }
7353
7354   /* Look for a unary operator.  */
7355   unary_operator = cp_parser_unary_operator (token);
7356   /* The `++' and `--' operators can be handled similarly, even though
7357      they are not technically unary-operators in the grammar.  */
7358   if (unary_operator == ERROR_MARK)
7359     {
7360       if (token->type == CPP_PLUS_PLUS)
7361         unary_operator = PREINCREMENT_EXPR;
7362       else if (token->type == CPP_MINUS_MINUS)
7363         unary_operator = PREDECREMENT_EXPR;
7364       /* Handle the GNU address-of-label extension.  */
7365       else if (cp_parser_allow_gnu_extensions_p (parser)
7366                && token->type == CPP_AND_AND)
7367         {
7368           tree identifier;
7369           tree expression;
7370           location_t loc = token->location;
7371
7372           /* Consume the '&&' token.  */
7373           cp_lexer_consume_token (parser->lexer);
7374           /* Look for the identifier.  */
7375           identifier = cp_parser_identifier (parser);
7376           /* Create an expression representing the address.  */
7377           expression = finish_label_address_expr (identifier, loc);
7378           if (cp_parser_non_integral_constant_expression (parser,
7379                                                           NIC_ADDR_LABEL))
7380             expression = error_mark_node;
7381           return expression;
7382         }
7383     }
7384   if (unary_operator != ERROR_MARK)
7385     {
7386       tree cast_expression;
7387       tree expression = error_mark_node;
7388       non_integral_constant non_constant_p = NIC_NONE;
7389       location_t loc = token->location;
7390       tsubst_flags_t complain = complain_flags (decltype_p);
7391
7392       /* Consume the operator token.  */
7393       token = cp_lexer_consume_token (parser->lexer);
7394       /* Parse the cast-expression.  */
7395       cast_expression
7396         = cp_parser_cast_expression (parser,
7397                                      unary_operator == ADDR_EXPR,
7398                                      /*cast_p=*/false,
7399                                      /*decltype*/false,
7400                                      pidk);
7401       /* Now, build an appropriate representation.  */
7402       switch (unary_operator)
7403         {
7404         case INDIRECT_REF:
7405           non_constant_p = NIC_STAR;
7406           expression = build_x_indirect_ref (loc, cast_expression,
7407                                              RO_UNARY_STAR,
7408                                              complain);
7409           break;
7410
7411         case ADDR_EXPR:
7412            non_constant_p = NIC_ADDR;
7413           /* Fall through.  */
7414         case BIT_NOT_EXPR:
7415           expression = build_x_unary_op (loc, unary_operator,
7416                                          cast_expression,
7417                                          complain);
7418           break;
7419
7420         case PREINCREMENT_EXPR:
7421         case PREDECREMENT_EXPR:
7422           non_constant_p = unary_operator == PREINCREMENT_EXPR
7423                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7424           /* Fall through.  */
7425         case UNARY_PLUS_EXPR:
7426         case NEGATE_EXPR:
7427         case TRUTH_NOT_EXPR:
7428           expression = finish_unary_op_expr (loc, unary_operator,
7429                                              cast_expression, complain);
7430           break;
7431
7432         default:
7433           gcc_unreachable ();
7434         }
7435
7436       if (non_constant_p != NIC_NONE
7437           && cp_parser_non_integral_constant_expression (parser,
7438                                                          non_constant_p))
7439         expression = error_mark_node;
7440
7441       return expression;
7442     }
7443
7444   return cp_parser_postfix_expression (parser, address_p, cast_p,
7445                                        /*member_access_only_p=*/false,
7446                                        decltype_p,
7447                                        pidk);
7448 }
7449
7450 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
7451    unary-operator, the corresponding tree code is returned.  */
7452
7453 static enum tree_code
7454 cp_parser_unary_operator (cp_token* token)
7455 {
7456   switch (token->type)
7457     {
7458     case CPP_MULT:
7459       return INDIRECT_REF;
7460
7461     case CPP_AND:
7462       return ADDR_EXPR;
7463
7464     case CPP_PLUS:
7465       return UNARY_PLUS_EXPR;
7466
7467     case CPP_MINUS:
7468       return NEGATE_EXPR;
7469
7470     case CPP_NOT:
7471       return TRUTH_NOT_EXPR;
7472
7473     case CPP_COMPL:
7474       return BIT_NOT_EXPR;
7475
7476     default:
7477       return ERROR_MARK;
7478     }
7479 }
7480
7481 /* Parse a new-expression.
7482
7483    new-expression:
7484      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7485      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7486
7487    Returns a representation of the expression.  */
7488
7489 static tree
7490 cp_parser_new_expression (cp_parser* parser)
7491 {
7492   bool global_scope_p;
7493   vec<tree, va_gc> *placement;
7494   tree type;
7495   vec<tree, va_gc> *initializer;
7496   tree nelts = NULL_TREE;
7497   tree ret;
7498
7499   /* Look for the optional `::' operator.  */
7500   global_scope_p
7501     = (cp_parser_global_scope_opt (parser,
7502                                    /*current_scope_valid_p=*/false)
7503        != NULL_TREE);
7504   /* Look for the `new' operator.  */
7505   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7506   /* There's no easy way to tell a new-placement from the
7507      `( type-id )' construct.  */
7508   cp_parser_parse_tentatively (parser);
7509   /* Look for a new-placement.  */
7510   placement = cp_parser_new_placement (parser);
7511   /* If that didn't work out, there's no new-placement.  */
7512   if (!cp_parser_parse_definitely (parser))
7513     {
7514       if (placement != NULL)
7515         release_tree_vector (placement);
7516       placement = NULL;
7517     }
7518
7519   /* If the next token is a `(', then we have a parenthesized
7520      type-id.  */
7521   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7522     {
7523       cp_token *token;
7524       const char *saved_message = parser->type_definition_forbidden_message;
7525
7526       /* Consume the `('.  */
7527       cp_lexer_consume_token (parser->lexer);
7528
7529       /* Parse the type-id.  */
7530       parser->type_definition_forbidden_message
7531         = G_("types may not be defined in a new-expression");
7532       type = cp_parser_type_id (parser);
7533       parser->type_definition_forbidden_message = saved_message;
7534
7535       /* Look for the closing `)'.  */
7536       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7537       token = cp_lexer_peek_token (parser->lexer);
7538       /* There should not be a direct-new-declarator in this production,
7539          but GCC used to allowed this, so we check and emit a sensible error
7540          message for this case.  */
7541       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7542         {
7543           error_at (token->location,
7544                     "array bound forbidden after parenthesized type-id");
7545           inform (token->location, 
7546                   "try removing the parentheses around the type-id");
7547           cp_parser_direct_new_declarator (parser);
7548         }
7549     }
7550   /* Otherwise, there must be a new-type-id.  */
7551   else
7552     type = cp_parser_new_type_id (parser, &nelts);
7553
7554   /* If the next token is a `(' or '{', then we have a new-initializer.  */
7555   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7556       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7557     initializer = cp_parser_new_initializer (parser);
7558   else
7559     initializer = NULL;
7560
7561   /* A new-expression may not appear in an integral constant
7562      expression.  */
7563   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7564     ret = error_mark_node;
7565   else
7566     {
7567       /* Create a representation of the new-expression.  */
7568       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7569                        tf_warning_or_error);
7570     }
7571
7572   if (placement != NULL)
7573     release_tree_vector (placement);
7574   if (initializer != NULL)
7575     release_tree_vector (initializer);
7576
7577   return ret;
7578 }
7579
7580 /* Parse a new-placement.
7581
7582    new-placement:
7583      ( expression-list )
7584
7585    Returns the same representation as for an expression-list.  */
7586
7587 static vec<tree, va_gc> *
7588 cp_parser_new_placement (cp_parser* parser)
7589 {
7590   vec<tree, va_gc> *expression_list;
7591
7592   /* Parse the expression-list.  */
7593   expression_list = (cp_parser_parenthesized_expression_list
7594                      (parser, non_attr, /*cast_p=*/false,
7595                       /*allow_expansion_p=*/true,
7596                       /*non_constant_p=*/NULL));
7597
7598   return expression_list;
7599 }
7600
7601 /* Parse a new-type-id.
7602
7603    new-type-id:
7604      type-specifier-seq new-declarator [opt]
7605
7606    Returns the TYPE allocated.  If the new-type-id indicates an array
7607    type, *NELTS is set to the number of elements in the last array
7608    bound; the TYPE will not include the last array bound.  */
7609
7610 static tree
7611 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7612 {
7613   cp_decl_specifier_seq type_specifier_seq;
7614   cp_declarator *new_declarator;
7615   cp_declarator *declarator;
7616   cp_declarator *outer_declarator;
7617   const char *saved_message;
7618
7619   /* The type-specifier sequence must not contain type definitions.
7620      (It cannot contain declarations of new types either, but if they
7621      are not definitions we will catch that because they are not
7622      complete.)  */
7623   saved_message = parser->type_definition_forbidden_message;
7624   parser->type_definition_forbidden_message
7625     = G_("types may not be defined in a new-type-id");
7626   /* Parse the type-specifier-seq.  */
7627   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7628                                 /*is_trailing_return=*/false,
7629                                 &type_specifier_seq);
7630   /* Restore the old message.  */
7631   parser->type_definition_forbidden_message = saved_message;
7632
7633   if (type_specifier_seq.type == error_mark_node)
7634     return error_mark_node;
7635
7636   /* Parse the new-declarator.  */
7637   new_declarator = cp_parser_new_declarator_opt (parser);
7638
7639   /* Determine the number of elements in the last array dimension, if
7640      any.  */
7641   *nelts = NULL_TREE;
7642   /* Skip down to the last array dimension.  */
7643   declarator = new_declarator;
7644   outer_declarator = NULL;
7645   while (declarator && (declarator->kind == cdk_pointer
7646                         || declarator->kind == cdk_ptrmem))
7647     {
7648       outer_declarator = declarator;
7649       declarator = declarator->declarator;
7650     }
7651   while (declarator
7652          && declarator->kind == cdk_array
7653          && declarator->declarator
7654          && declarator->declarator->kind == cdk_array)
7655     {
7656       outer_declarator = declarator;
7657       declarator = declarator->declarator;
7658     }
7659
7660   if (declarator && declarator->kind == cdk_array)
7661     {
7662       *nelts = declarator->u.array.bounds;
7663       if (*nelts == error_mark_node)
7664         *nelts = integer_one_node;
7665
7666       if (outer_declarator)
7667         outer_declarator->declarator = declarator->declarator;
7668       else
7669         new_declarator = NULL;
7670     }
7671
7672   return groktypename (&type_specifier_seq, new_declarator, false);
7673 }
7674
7675 /* Parse an (optional) new-declarator.
7676
7677    new-declarator:
7678      ptr-operator new-declarator [opt]
7679      direct-new-declarator
7680
7681    Returns the declarator.  */
7682
7683 static cp_declarator *
7684 cp_parser_new_declarator_opt (cp_parser* parser)
7685 {
7686   enum tree_code code;
7687   tree type, std_attributes = NULL_TREE;
7688   cp_cv_quals cv_quals;  
7689
7690   /* We don't know if there's a ptr-operator next, or not.  */
7691   cp_parser_parse_tentatively (parser);
7692   /* Look for a ptr-operator.  */
7693   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7694   /* If that worked, look for more new-declarators.  */
7695   if (cp_parser_parse_definitely (parser))
7696     {
7697       cp_declarator *declarator;
7698
7699       /* Parse another optional declarator.  */
7700       declarator = cp_parser_new_declarator_opt (parser);
7701
7702       declarator = cp_parser_make_indirect_declarator
7703         (code, type, cv_quals, declarator, std_attributes);
7704
7705       return declarator;
7706     }
7707
7708   /* If the next token is a `[', there is a direct-new-declarator.  */
7709   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7710     return cp_parser_direct_new_declarator (parser);
7711
7712   return NULL;
7713 }
7714
7715 /* Parse a direct-new-declarator.
7716
7717    direct-new-declarator:
7718      [ expression ]
7719      direct-new-declarator [constant-expression]
7720
7721    */
7722
7723 static cp_declarator *
7724 cp_parser_direct_new_declarator (cp_parser* parser)
7725 {
7726   cp_declarator *declarator = NULL;
7727
7728   while (true)
7729     {
7730       tree expression;
7731       cp_token *token;
7732
7733       /* Look for the opening `['.  */
7734       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7735
7736       token = cp_lexer_peek_token (parser->lexer);
7737       expression = cp_parser_expression (parser);
7738       /* The standard requires that the expression have integral
7739          type.  DR 74 adds enumeration types.  We believe that the
7740          real intent is that these expressions be handled like the
7741          expression in a `switch' condition, which also allows
7742          classes with a single conversion to integral or
7743          enumeration type.  */
7744       if (!processing_template_decl)
7745         {
7746           expression
7747             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7748                                           expression,
7749                                           /*complain=*/true);
7750           if (!expression)
7751             {
7752               error_at (token->location,
7753                         "expression in new-declarator must have integral "
7754                         "or enumeration type");
7755               expression = error_mark_node;
7756             }
7757         }
7758
7759       /* Look for the closing `]'.  */
7760       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7761
7762       /* Add this bound to the declarator.  */
7763       declarator = make_array_declarator (declarator, expression);
7764
7765       /* If the next token is not a `[', then there are no more
7766          bounds.  */
7767       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7768         break;
7769     }
7770
7771   return declarator;
7772 }
7773
7774 /* Parse a new-initializer.
7775
7776    new-initializer:
7777      ( expression-list [opt] )
7778      braced-init-list
7779
7780    Returns a representation of the expression-list.  */
7781
7782 static vec<tree, va_gc> *
7783 cp_parser_new_initializer (cp_parser* parser)
7784 {
7785   vec<tree, va_gc> *expression_list;
7786
7787   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7788     {
7789       tree t;
7790       bool expr_non_constant_p;
7791       cp_lexer_set_source_position (parser->lexer);
7792       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7793       t = cp_parser_braced_list (parser, &expr_non_constant_p);
7794       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7795       expression_list = make_tree_vector_single (t);
7796     }
7797   else
7798     expression_list = (cp_parser_parenthesized_expression_list
7799                        (parser, non_attr, /*cast_p=*/false,
7800                         /*allow_expansion_p=*/true,
7801                         /*non_constant_p=*/NULL));
7802
7803   return expression_list;
7804 }
7805
7806 /* Parse a delete-expression.
7807
7808    delete-expression:
7809      :: [opt] delete cast-expression
7810      :: [opt] delete [ ] cast-expression
7811
7812    Returns a representation of the expression.  */
7813
7814 static tree
7815 cp_parser_delete_expression (cp_parser* parser)
7816 {
7817   bool global_scope_p;
7818   bool array_p;
7819   tree expression;
7820
7821   /* Look for the optional `::' operator.  */
7822   global_scope_p
7823     = (cp_parser_global_scope_opt (parser,
7824                                    /*current_scope_valid_p=*/false)
7825        != NULL_TREE);
7826   /* Look for the `delete' keyword.  */
7827   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7828   /* See if the array syntax is in use.  */
7829   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7830     {
7831       /* Consume the `[' token.  */
7832       cp_lexer_consume_token (parser->lexer);
7833       /* Look for the `]' token.  */
7834       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7835       /* Remember that this is the `[]' construct.  */
7836       array_p = true;
7837     }
7838   else
7839     array_p = false;
7840
7841   /* Parse the cast-expression.  */
7842   expression = cp_parser_simple_cast_expression (parser);
7843
7844   /* A delete-expression may not appear in an integral constant
7845      expression.  */
7846   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7847     return error_mark_node;
7848
7849   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7850                         tf_warning_or_error);
7851 }
7852
7853 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7854    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7855    0 otherwise.  */
7856
7857 static int
7858 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7859 {
7860   cp_token *token = cp_lexer_peek_token (parser->lexer);
7861   switch (token->type)
7862     {
7863     case CPP_COMMA:
7864     case CPP_SEMICOLON:
7865     case CPP_QUERY:
7866     case CPP_COLON:
7867     case CPP_CLOSE_SQUARE:
7868     case CPP_CLOSE_PAREN:
7869     case CPP_CLOSE_BRACE:
7870     case CPP_OPEN_BRACE:
7871     case CPP_DOT:
7872     case CPP_DOT_STAR:
7873     case CPP_DEREF:
7874     case CPP_DEREF_STAR:
7875     case CPP_DIV:
7876     case CPP_MOD:
7877     case CPP_LSHIFT:
7878     case CPP_RSHIFT:
7879     case CPP_LESS:
7880     case CPP_GREATER:
7881     case CPP_LESS_EQ:
7882     case CPP_GREATER_EQ:
7883     case CPP_EQ_EQ:
7884     case CPP_NOT_EQ:
7885     case CPP_EQ:
7886     case CPP_MULT_EQ:
7887     case CPP_DIV_EQ:
7888     case CPP_MOD_EQ:
7889     case CPP_PLUS_EQ:
7890     case CPP_MINUS_EQ:
7891     case CPP_RSHIFT_EQ:
7892     case CPP_LSHIFT_EQ:
7893     case CPP_AND_EQ:
7894     case CPP_XOR_EQ:
7895     case CPP_OR_EQ:
7896     case CPP_XOR:
7897     case CPP_OR:
7898     case CPP_OR_OR:
7899     case CPP_EOF:
7900     case CPP_ELLIPSIS:
7901       return 0;
7902
7903     case CPP_OPEN_PAREN:
7904       /* In ((type ()) () the last () isn't a valid cast-expression,
7905          so the whole must be parsed as postfix-expression.  */
7906       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7907              != CPP_CLOSE_PAREN;
7908
7909     case CPP_OPEN_SQUARE:
7910       /* '[' may start a primary-expression in obj-c++ and in C++11,
7911          as a lambda-expression, eg, '(void)[]{}'.  */
7912       if (cxx_dialect >= cxx11)
7913         return -1;
7914       return c_dialect_objc ();
7915
7916     case CPP_PLUS_PLUS:
7917     case CPP_MINUS_MINUS:
7918       /* '++' and '--' may or may not start a cast-expression:
7919
7920          struct T { void operator++(int); };
7921          void f() { (T())++; }
7922
7923          vs
7924
7925          int a;
7926          (int)++a;  */
7927       return -1;
7928
7929     default:
7930       return 1;
7931     }
7932 }
7933
7934 /* Parse a cast-expression.
7935
7936    cast-expression:
7937      unary-expression
7938      ( type-id ) cast-expression
7939
7940    ADDRESS_P is true iff the unary-expression is appearing as the
7941    operand of the `&' operator.   CAST_P is true if this expression is
7942    the target of a cast.
7943
7944    Returns a representation of the expression.  */
7945
7946 static tree
7947 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7948                            bool decltype_p, cp_id_kind * pidk)
7949 {
7950   /* If it's a `(', then we might be looking at a cast.  */
7951   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7952     {
7953       tree type = NULL_TREE;
7954       tree expr = NULL_TREE;
7955       int cast_expression = 0;
7956       const char *saved_message;
7957
7958       /* There's no way to know yet whether or not this is a cast.
7959          For example, `(int (3))' is a unary-expression, while `(int)
7960          3' is a cast.  So, we resort to parsing tentatively.  */
7961       cp_parser_parse_tentatively (parser);
7962       /* Types may not be defined in a cast.  */
7963       saved_message = parser->type_definition_forbidden_message;
7964       parser->type_definition_forbidden_message
7965         = G_("types may not be defined in casts");
7966       /* Consume the `('.  */
7967       cp_lexer_consume_token (parser->lexer);
7968       /* A very tricky bit is that `(struct S) { 3 }' is a
7969          compound-literal (which we permit in C++ as an extension).
7970          But, that construct is not a cast-expression -- it is a
7971          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7972          is legal; if the compound-literal were a cast-expression,
7973          you'd need an extra set of parentheses.)  But, if we parse
7974          the type-id, and it happens to be a class-specifier, then we
7975          will commit to the parse at that point, because we cannot
7976          undo the action that is done when creating a new class.  So,
7977          then we cannot back up and do a postfix-expression.
7978
7979          Another tricky case is the following (c++/29234):
7980
7981          struct S { void operator () (); };
7982
7983          void foo ()
7984          {
7985            ( S()() );
7986          }
7987
7988          As a type-id we parse the parenthesized S()() as a function
7989          returning a function, groktypename complains and we cannot
7990          back up in this case either.
7991
7992          Therefore, we scan ahead to the closing `)', and check to see
7993          if the tokens after the `)' can start a cast-expression.  Otherwise
7994          we are dealing with an unary-expression, a postfix-expression
7995          or something else.
7996
7997          Yet another tricky case, in C++11, is the following (c++/54891):
7998
7999          (void)[]{};
8000
8001          The issue is that usually, besides the case of lambda-expressions,
8002          the parenthesized type-id cannot be followed by '[', and, eg, we
8003          want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8004          Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8005          we don't commit, we try a cast-expression, then an unary-expression.
8006
8007          Save tokens so that we can put them back.  */
8008       cp_lexer_save_tokens (parser->lexer);
8009
8010       /* We may be looking at a cast-expression.  */
8011       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8012                                                  /*consume_paren=*/true))
8013         cast_expression
8014           = cp_parser_tokens_start_cast_expression (parser);
8015
8016       /* Roll back the tokens we skipped.  */
8017       cp_lexer_rollback_tokens (parser->lexer);
8018       /* If we aren't looking at a cast-expression, simulate an error so
8019          that the call to cp_parser_error_occurred below returns true.  */
8020       if (!cast_expression)
8021         cp_parser_simulate_error (parser);
8022       else
8023         {
8024           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8025           parser->in_type_id_in_expr_p = true;
8026           /* Look for the type-id.  */
8027           type = cp_parser_type_id (parser);
8028           /* Look for the closing `)'.  */
8029           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8030           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8031         }
8032
8033       /* Restore the saved message.  */
8034       parser->type_definition_forbidden_message = saved_message;
8035
8036       /* At this point this can only be either a cast or a
8037          parenthesized ctor such as `(T ())' that looks like a cast to
8038          function returning T.  */
8039       if (!cp_parser_error_occurred (parser))
8040         {
8041           /* Only commit if the cast-expression doesn't start with
8042              '++', '--', or '[' in C++11.  */
8043           if (cast_expression > 0)
8044             cp_parser_commit_to_topmost_tentative_parse (parser);
8045
8046           expr = cp_parser_cast_expression (parser,
8047                                             /*address_p=*/false,
8048                                             /*cast_p=*/true,
8049                                             /*decltype_p=*/false,
8050                                             pidk);
8051
8052           if (cp_parser_parse_definitely (parser))
8053             {
8054               /* Warn about old-style casts, if so requested.  */
8055               if (warn_old_style_cast
8056                   && !in_system_header_at (input_location)
8057                   && !VOID_TYPE_P (type)
8058                   && current_lang_name != lang_name_c)
8059                 warning (OPT_Wold_style_cast, "use of old-style cast");
8060
8061               /* Only type conversions to integral or enumeration types
8062                  can be used in constant-expressions.  */
8063               if (!cast_valid_in_integral_constant_expression_p (type)
8064                   && cp_parser_non_integral_constant_expression (parser,
8065                                                                  NIC_CAST))
8066                 return error_mark_node;
8067
8068               /* Perform the cast.  */
8069               expr = build_c_cast (input_location, type, expr);
8070               return expr;
8071             }
8072         }
8073       else 
8074         cp_parser_abort_tentative_parse (parser);
8075     }
8076
8077   /* If we get here, then it's not a cast, so it must be a
8078      unary-expression.  */
8079   return cp_parser_unary_expression (parser, pidk, address_p,
8080                                      cast_p, decltype_p);
8081 }
8082
8083 /* Parse a binary expression of the general form:
8084
8085    pm-expression:
8086      cast-expression
8087      pm-expression .* cast-expression
8088      pm-expression ->* cast-expression
8089
8090    multiplicative-expression:
8091      pm-expression
8092      multiplicative-expression * pm-expression
8093      multiplicative-expression / pm-expression
8094      multiplicative-expression % pm-expression
8095
8096    additive-expression:
8097      multiplicative-expression
8098      additive-expression + multiplicative-expression
8099      additive-expression - multiplicative-expression
8100
8101    shift-expression:
8102      additive-expression
8103      shift-expression << additive-expression
8104      shift-expression >> additive-expression
8105
8106    relational-expression:
8107      shift-expression
8108      relational-expression < shift-expression
8109      relational-expression > shift-expression
8110      relational-expression <= shift-expression
8111      relational-expression >= shift-expression
8112
8113   GNU Extension:
8114
8115    relational-expression:
8116      relational-expression <? shift-expression
8117      relational-expression >? shift-expression
8118
8119    equality-expression:
8120      relational-expression
8121      equality-expression == relational-expression
8122      equality-expression != relational-expression
8123
8124    and-expression:
8125      equality-expression
8126      and-expression & equality-expression
8127
8128    exclusive-or-expression:
8129      and-expression
8130      exclusive-or-expression ^ and-expression
8131
8132    inclusive-or-expression:
8133      exclusive-or-expression
8134      inclusive-or-expression | exclusive-or-expression
8135
8136    logical-and-expression:
8137      inclusive-or-expression
8138      logical-and-expression && inclusive-or-expression
8139
8140    logical-or-expression:
8141      logical-and-expression
8142      logical-or-expression || logical-and-expression
8143
8144    All these are implemented with a single function like:
8145
8146    binary-expression:
8147      simple-cast-expression
8148      binary-expression <token> binary-expression
8149
8150    CAST_P is true if this expression is the target of a cast.
8151
8152    The binops_by_token map is used to get the tree codes for each <token> type.
8153    binary-expressions are associated according to a precedence table.  */
8154
8155 #define TOKEN_PRECEDENCE(token)                              \
8156 (((token->type == CPP_GREATER                                \
8157    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8158   && !parser->greater_than_is_operator_p)                    \
8159  ? PREC_NOT_OPERATOR                                         \
8160  : binops_by_token[token->type].prec)
8161
8162 static tree
8163 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8164                              bool no_toplevel_fold_p,
8165                              bool decltype_p,
8166                              enum cp_parser_prec prec,
8167                              cp_id_kind * pidk)
8168 {
8169   cp_parser_expression_stack stack;
8170   cp_parser_expression_stack_entry *sp = &stack[0];
8171   cp_parser_expression_stack_entry current;
8172   tree rhs;
8173   cp_token *token;
8174   enum tree_code rhs_type;
8175   enum cp_parser_prec new_prec, lookahead_prec;
8176   tree overload;
8177
8178   /* Parse the first expression.  */
8179   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8180                       ? TRUTH_NOT_EXPR : ERROR_MARK);
8181   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8182                                            cast_p, decltype_p, pidk);
8183   current.prec = prec;
8184
8185   if (cp_parser_error_occurred (parser))
8186     return error_mark_node;
8187
8188   for (;;)
8189     {
8190       /* Get an operator token.  */
8191       token = cp_lexer_peek_token (parser->lexer);
8192
8193       if (warn_cxx0x_compat
8194           && token->type == CPP_RSHIFT
8195           && !parser->greater_than_is_operator_p)
8196         {
8197           if (warning_at (token->location, OPT_Wc__0x_compat,
8198                           "%<>>%> operator is treated"
8199                           " as two right angle brackets in C++11"))
8200             inform (token->location,
8201                     "suggest parentheses around %<>>%> expression");
8202         }
8203
8204       new_prec = TOKEN_PRECEDENCE (token);
8205
8206       /* Popping an entry off the stack means we completed a subexpression:
8207          - either we found a token which is not an operator (`>' where it is not
8208            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8209            will happen repeatedly;
8210          - or, we found an operator which has lower priority.  This is the case
8211            where the recursive descent *ascends*, as in `3 * 4 + 5' after
8212            parsing `3 * 4'.  */
8213       if (new_prec <= current.prec)
8214         {
8215           if (sp == stack)
8216             break;
8217           else
8218             goto pop;
8219         }
8220
8221      get_rhs:
8222       current.tree_type = binops_by_token[token->type].tree_type;
8223       current.loc = token->location;
8224
8225       /* We used the operator token.  */
8226       cp_lexer_consume_token (parser->lexer);
8227
8228       /* For "false && x" or "true || x", x will never be executed;
8229          disable warnings while evaluating it.  */
8230       if (current.tree_type == TRUTH_ANDIF_EXPR)
8231         c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8232       else if (current.tree_type == TRUTH_ORIF_EXPR)
8233         c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8234
8235       /* Extract another operand.  It may be the RHS of this expression
8236          or the LHS of a new, higher priority expression.  */
8237       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8238                   ? TRUTH_NOT_EXPR : ERROR_MARK);
8239       rhs = cp_parser_simple_cast_expression (parser);
8240
8241       /* Get another operator token.  Look up its precedence to avoid
8242          building a useless (immediately popped) stack entry for common
8243          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
8244       token = cp_lexer_peek_token (parser->lexer);
8245       lookahead_prec = TOKEN_PRECEDENCE (token);
8246       if (lookahead_prec > new_prec)
8247         {
8248           /* ... and prepare to parse the RHS of the new, higher priority
8249              expression.  Since precedence levels on the stack are
8250              monotonically increasing, we do not have to care about
8251              stack overflows.  */
8252           *sp = current;
8253           ++sp;
8254           current.lhs = rhs;
8255           current.lhs_type = rhs_type;
8256           current.prec = new_prec;
8257           new_prec = lookahead_prec;
8258           goto get_rhs;
8259
8260          pop:
8261           lookahead_prec = new_prec;
8262           /* If the stack is not empty, we have parsed into LHS the right side
8263              (`4' in the example above) of an expression we had suspended.
8264              We can use the information on the stack to recover the LHS (`3')
8265              from the stack together with the tree code (`MULT_EXPR'), and
8266              the precedence of the higher level subexpression
8267              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
8268              which will be used to actually build the additive expression.  */
8269           rhs = current.lhs;
8270           rhs_type = current.lhs_type;
8271           --sp;
8272           current = *sp;
8273         }
8274
8275       /* Undo the disabling of warnings done above.  */
8276       if (current.tree_type == TRUTH_ANDIF_EXPR)
8277         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8278       else if (current.tree_type == TRUTH_ORIF_EXPR)
8279         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8280
8281       if (warn_logical_not_paren
8282           && current.lhs_type == TRUTH_NOT_EXPR)
8283         warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8284
8285       overload = NULL;
8286       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8287          ERROR_MARK for everything that is not a binary expression.
8288          This makes warn_about_parentheses miss some warnings that
8289          involve unary operators.  For unary expressions we should
8290          pass the correct tree_code unless the unary expression was
8291          surrounded by parentheses.
8292       */
8293       if (no_toplevel_fold_p
8294           && lookahead_prec <= current.prec
8295           && sp == stack)
8296         current.lhs = build2 (current.tree_type,
8297                               TREE_CODE_CLASS (current.tree_type)
8298                               == tcc_comparison
8299                               ? boolean_type_node : TREE_TYPE (current.lhs),
8300                               current.lhs, rhs);
8301       else
8302         current.lhs = build_x_binary_op (current.loc, current.tree_type,
8303                                          current.lhs, current.lhs_type,
8304                                          rhs, rhs_type, &overload,
8305                                          complain_flags (decltype_p));
8306       current.lhs_type = current.tree_type;
8307       if (EXPR_P (current.lhs))
8308         SET_EXPR_LOCATION (current.lhs, current.loc);
8309
8310       /* If the binary operator required the use of an overloaded operator,
8311          then this expression cannot be an integral constant-expression.
8312          An overloaded operator can be used even if both operands are
8313          otherwise permissible in an integral constant-expression if at
8314          least one of the operands is of enumeration type.  */
8315
8316       if (overload
8317           && cp_parser_non_integral_constant_expression (parser,
8318                                                          NIC_OVERLOADED))
8319         return error_mark_node;
8320     }
8321
8322   return current.lhs;
8323 }
8324
8325 static tree
8326 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8327                              bool no_toplevel_fold_p,
8328                              enum cp_parser_prec prec,
8329                              cp_id_kind * pidk)
8330 {
8331   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8332                                       /*decltype*/false, prec, pidk);
8333 }
8334
8335 /* Parse the `? expression : assignment-expression' part of a
8336    conditional-expression.  The LOGICAL_OR_EXPR is the
8337    logical-or-expression that started the conditional-expression.
8338    Returns a representation of the entire conditional-expression.
8339
8340    This routine is used by cp_parser_assignment_expression.
8341
8342      ? expression : assignment-expression
8343
8344    GNU Extensions:
8345
8346      ? : assignment-expression */
8347
8348 static tree
8349 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8350 {
8351   tree expr;
8352   tree assignment_expr;
8353   struct cp_token *token;
8354   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8355
8356   /* Consume the `?' token.  */
8357   cp_lexer_consume_token (parser->lexer);
8358   token = cp_lexer_peek_token (parser->lexer);
8359   if (cp_parser_allow_gnu_extensions_p (parser)
8360       && token->type == CPP_COLON)
8361     {
8362       pedwarn (token->location, OPT_Wpedantic, 
8363                "ISO C++ does not allow ?: with omitted middle operand");
8364       /* Implicit true clause.  */
8365       expr = NULL_TREE;
8366       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8367       warn_for_omitted_condop (token->location, logical_or_expr);
8368     }
8369   else
8370     {
8371       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8372       parser->colon_corrects_to_scope_p = false;
8373       /* Parse the expression.  */
8374       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8375       expr = cp_parser_expression (parser);
8376       c_inhibit_evaluation_warnings +=
8377         ((logical_or_expr == truthvalue_true_node)
8378          - (logical_or_expr == truthvalue_false_node));
8379       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8380     }
8381
8382   /* The next token should be a `:'.  */
8383   cp_parser_require (parser, CPP_COLON, RT_COLON);
8384   /* Parse the assignment-expression.  */
8385   assignment_expr = cp_parser_assignment_expression (parser);
8386   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8387
8388   /* Build the conditional-expression.  */
8389   return build_x_conditional_expr (loc, logical_or_expr,
8390                                    expr,
8391                                    assignment_expr,
8392                                    tf_warning_or_error);
8393 }
8394
8395 /* Parse an assignment-expression.
8396
8397    assignment-expression:
8398      conditional-expression
8399      logical-or-expression assignment-operator assignment_expression
8400      throw-expression
8401
8402    CAST_P is true if this expression is the target of a cast.
8403    DECLTYPE_P is true if this expression is the operand of decltype.
8404
8405    Returns a representation for the expression.  */
8406
8407 static tree
8408 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8409                                  bool cast_p, bool decltype_p)
8410 {
8411   tree expr;
8412
8413   /* If the next token is the `throw' keyword, then we're looking at
8414      a throw-expression.  */
8415   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8416     expr = cp_parser_throw_expression (parser);
8417   /* Otherwise, it must be that we are looking at a
8418      logical-or-expression.  */
8419   else
8420     {
8421       /* Parse the binary expressions (logical-or-expression).  */
8422       expr = cp_parser_binary_expression (parser, cast_p, false,
8423                                           decltype_p,
8424                                           PREC_NOT_OPERATOR, pidk);
8425       /* If the next token is a `?' then we're actually looking at a
8426          conditional-expression.  */
8427       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8428         return cp_parser_question_colon_clause (parser, expr);
8429       else
8430         {
8431           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8432
8433           /* If it's an assignment-operator, we're using the second
8434              production.  */
8435           enum tree_code assignment_operator
8436             = cp_parser_assignment_operator_opt (parser);
8437           if (assignment_operator != ERROR_MARK)
8438             {
8439               bool non_constant_p;
8440               location_t saved_input_location;
8441
8442               /* Parse the right-hand side of the assignment.  */
8443               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8444
8445               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8446                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8447
8448               /* An assignment may not appear in a
8449                  constant-expression.  */
8450               if (cp_parser_non_integral_constant_expression (parser,
8451                                                               NIC_ASSIGNMENT))
8452                 return error_mark_node;
8453               /* Build the assignment expression.  Its default
8454                  location is the location of the '=' token.  */
8455               saved_input_location = input_location;
8456               input_location = loc;
8457               expr = build_x_modify_expr (loc, expr,
8458                                           assignment_operator,
8459                                           rhs,
8460                                           complain_flags (decltype_p));
8461               input_location = saved_input_location;
8462             }
8463         }
8464     }
8465
8466   return expr;
8467 }
8468
8469 /* Parse an (optional) assignment-operator.
8470
8471    assignment-operator: one of
8472      = *= /= %= += -= >>= <<= &= ^= |=
8473
8474    GNU Extension:
8475
8476    assignment-operator: one of
8477      <?= >?=
8478
8479    If the next token is an assignment operator, the corresponding tree
8480    code is returned, and the token is consumed.  For example, for
8481    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
8482    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
8483    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
8484    operator, ERROR_MARK is returned.  */
8485
8486 static enum tree_code
8487 cp_parser_assignment_operator_opt (cp_parser* parser)
8488 {
8489   enum tree_code op;
8490   cp_token *token;
8491
8492   /* Peek at the next token.  */
8493   token = cp_lexer_peek_token (parser->lexer);
8494
8495   switch (token->type)
8496     {
8497     case CPP_EQ:
8498       op = NOP_EXPR;
8499       break;
8500
8501     case CPP_MULT_EQ:
8502       op = MULT_EXPR;
8503       break;
8504
8505     case CPP_DIV_EQ:
8506       op = TRUNC_DIV_EXPR;
8507       break;
8508
8509     case CPP_MOD_EQ:
8510       op = TRUNC_MOD_EXPR;
8511       break;
8512
8513     case CPP_PLUS_EQ:
8514       op = PLUS_EXPR;
8515       break;
8516
8517     case CPP_MINUS_EQ:
8518       op = MINUS_EXPR;
8519       break;
8520
8521     case CPP_RSHIFT_EQ:
8522       op = RSHIFT_EXPR;
8523       break;
8524
8525     case CPP_LSHIFT_EQ:
8526       op = LSHIFT_EXPR;
8527       break;
8528
8529     case CPP_AND_EQ:
8530       op = BIT_AND_EXPR;
8531       break;
8532
8533     case CPP_XOR_EQ:
8534       op = BIT_XOR_EXPR;
8535       break;
8536
8537     case CPP_OR_EQ:
8538       op = BIT_IOR_EXPR;
8539       break;
8540
8541     default:
8542       /* Nothing else is an assignment operator.  */
8543       op = ERROR_MARK;
8544     }
8545
8546   /* If it was an assignment operator, consume it.  */
8547   if (op != ERROR_MARK)
8548     cp_lexer_consume_token (parser->lexer);
8549
8550   return op;
8551 }
8552
8553 /* Parse an expression.
8554
8555    expression:
8556      assignment-expression
8557      expression , assignment-expression
8558
8559    CAST_P is true if this expression is the target of a cast.
8560    DECLTYPE_P is true if this expression is the immediate operand of decltype,
8561      except possibly parenthesized or on the RHS of a comma (N3276).
8562
8563    Returns a representation of the expression.  */
8564
8565 static tree
8566 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8567                       bool cast_p, bool decltype_p)
8568 {
8569   tree expression = NULL_TREE;
8570   location_t loc = UNKNOWN_LOCATION;
8571
8572   while (true)
8573     {
8574       tree assignment_expression;
8575
8576       /* Parse the next assignment-expression.  */
8577       assignment_expression
8578         = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8579
8580       /* We don't create a temporary for a call that is the immediate operand
8581          of decltype or on the RHS of a comma.  But when we see a comma, we
8582          need to create a temporary for a call on the LHS.  */
8583       if (decltype_p && !processing_template_decl
8584           && TREE_CODE (assignment_expression) == CALL_EXPR
8585           && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8586           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8587         assignment_expression
8588           = build_cplus_new (TREE_TYPE (assignment_expression),
8589                              assignment_expression, tf_warning_or_error);
8590
8591       /* If this is the first assignment-expression, we can just
8592          save it away.  */
8593       if (!expression)
8594         expression = assignment_expression;
8595       else
8596         expression = build_x_compound_expr (loc, expression,
8597                                             assignment_expression,
8598                                             complain_flags (decltype_p));
8599       /* If the next token is not a comma, then we are done with the
8600          expression.  */
8601       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8602         break;
8603       /* Consume the `,'.  */
8604       loc = cp_lexer_peek_token (parser->lexer)->location;
8605       cp_lexer_consume_token (parser->lexer);
8606       /* A comma operator cannot appear in a constant-expression.  */
8607       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8608         expression = error_mark_node;
8609     }
8610
8611   return expression;
8612 }
8613
8614 /* Parse a constant-expression.
8615
8616    constant-expression:
8617      conditional-expression
8618
8619   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8620   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
8621   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
8622   is false, NON_CONSTANT_P should be NULL.  */
8623
8624 static tree
8625 cp_parser_constant_expression (cp_parser* parser,
8626                                bool allow_non_constant_p,
8627                                bool *non_constant_p)
8628 {
8629   bool saved_integral_constant_expression_p;
8630   bool saved_allow_non_integral_constant_expression_p;
8631   bool saved_non_integral_constant_expression_p;
8632   tree expression;
8633
8634   /* It might seem that we could simply parse the
8635      conditional-expression, and then check to see if it were
8636      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
8637      one that the compiler can figure out is constant, possibly after
8638      doing some simplifications or optimizations.  The standard has a
8639      precise definition of constant-expression, and we must honor
8640      that, even though it is somewhat more restrictive.
8641
8642      For example:
8643
8644        int i[(2, 3)];
8645
8646      is not a legal declaration, because `(2, 3)' is not a
8647      constant-expression.  The `,' operator is forbidden in a
8648      constant-expression.  However, GCC's constant-folding machinery
8649      will fold this operation to an INTEGER_CST for `3'.  */
8650
8651   /* Save the old settings.  */
8652   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8653   saved_allow_non_integral_constant_expression_p
8654     = parser->allow_non_integral_constant_expression_p;
8655   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8656   /* We are now parsing a constant-expression.  */
8657   parser->integral_constant_expression_p = true;
8658   parser->allow_non_integral_constant_expression_p
8659     = (allow_non_constant_p || cxx_dialect >= cxx11);
8660   parser->non_integral_constant_expression_p = false;
8661   /* Although the grammar says "conditional-expression", we parse an
8662      "assignment-expression", which also permits "throw-expression"
8663      and the use of assignment operators.  In the case that
8664      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8665      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
8666      actually essential that we look for an assignment-expression.
8667      For example, cp_parser_initializer_clauses uses this function to
8668      determine whether a particular assignment-expression is in fact
8669      constant.  */
8670   expression = cp_parser_assignment_expression (parser);
8671   /* Restore the old settings.  */
8672   parser->integral_constant_expression_p
8673     = saved_integral_constant_expression_p;
8674   parser->allow_non_integral_constant_expression_p
8675     = saved_allow_non_integral_constant_expression_p;
8676   if (cxx_dialect >= cxx11)
8677     {
8678       /* Require an rvalue constant expression here; that's what our
8679          callers expect.  Reference constant expressions are handled
8680          separately in e.g. cp_parser_template_argument.  */
8681       bool is_const = potential_rvalue_constant_expression (expression);
8682       parser->non_integral_constant_expression_p = !is_const;
8683       if (!is_const && !allow_non_constant_p)
8684         require_potential_rvalue_constant_expression (expression);
8685     }
8686   if (allow_non_constant_p)
8687     *non_constant_p = parser->non_integral_constant_expression_p;
8688   parser->non_integral_constant_expression_p
8689     = saved_non_integral_constant_expression_p;
8690
8691   return expression;
8692 }
8693
8694 /* Parse __builtin_offsetof.
8695
8696    offsetof-expression:
8697      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8698
8699    offsetof-member-designator:
8700      id-expression
8701      | offsetof-member-designator "." id-expression
8702      | offsetof-member-designator "[" expression "]"
8703      | offsetof-member-designator "->" id-expression  */
8704
8705 static tree
8706 cp_parser_builtin_offsetof (cp_parser *parser)
8707 {
8708   int save_ice_p, save_non_ice_p;
8709   tree type, expr;
8710   cp_id_kind dummy;
8711   cp_token *token;
8712
8713   /* We're about to accept non-integral-constant things, but will
8714      definitely yield an integral constant expression.  Save and
8715      restore these values around our local parsing.  */
8716   save_ice_p = parser->integral_constant_expression_p;
8717   save_non_ice_p = parser->non_integral_constant_expression_p;
8718
8719   /* Consume the "__builtin_offsetof" token.  */
8720   cp_lexer_consume_token (parser->lexer);
8721   /* Consume the opening `('.  */
8722   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8723   /* Parse the type-id.  */
8724   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8725   type = cp_parser_type_id (parser);
8726   /* Look for the `,'.  */
8727   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8728   token = cp_lexer_peek_token (parser->lexer);
8729
8730   /* Build the (type *)null that begins the traditional offsetof macro.  */
8731   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8732                             tf_warning_or_error);
8733
8734   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
8735   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8736                                                  true, &dummy, token->location);
8737   while (true)
8738     {
8739       token = cp_lexer_peek_token (parser->lexer);
8740       switch (token->type)
8741         {
8742         case CPP_OPEN_SQUARE:
8743           /* offsetof-member-designator "[" expression "]" */
8744           expr = cp_parser_postfix_open_square_expression (parser, expr,
8745                                                            true, false);
8746           break;
8747
8748         case CPP_DEREF:
8749           /* offsetof-member-designator "->" identifier */
8750           expr = grok_array_decl (token->location, expr,
8751                                   integer_zero_node, false);
8752           /* FALLTHRU */
8753
8754         case CPP_DOT:
8755           /* offsetof-member-designator "." identifier */
8756           cp_lexer_consume_token (parser->lexer);
8757           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8758                                                          expr, true, &dummy,
8759                                                          token->location);
8760           break;
8761
8762         case CPP_CLOSE_PAREN:
8763           /* Consume the ")" token.  */
8764           cp_lexer_consume_token (parser->lexer);
8765           goto success;
8766
8767         default:
8768           /* Error.  We know the following require will fail, but
8769              that gives the proper error message.  */
8770           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8771           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8772           expr = error_mark_node;
8773           goto failure;
8774         }
8775     }
8776
8777  success:
8778   expr = finish_offsetof (expr, loc);
8779
8780  failure:
8781   parser->integral_constant_expression_p = save_ice_p;
8782   parser->non_integral_constant_expression_p = save_non_ice_p;
8783
8784   return expr;
8785 }
8786
8787 /* Parse a trait expression.
8788
8789    Returns a representation of the expression, the underlying type
8790    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
8791
8792 static tree
8793 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8794 {
8795   cp_trait_kind kind;
8796   tree type1, type2 = NULL_TREE;
8797   bool binary = false;
8798   bool variadic = false;
8799
8800   switch (keyword)
8801     {
8802     case RID_HAS_NOTHROW_ASSIGN:
8803       kind = CPTK_HAS_NOTHROW_ASSIGN;
8804       break;
8805     case RID_HAS_NOTHROW_CONSTRUCTOR:
8806       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8807       break;
8808     case RID_HAS_NOTHROW_COPY:
8809       kind = CPTK_HAS_NOTHROW_COPY;
8810       break;
8811     case RID_HAS_TRIVIAL_ASSIGN:
8812       kind = CPTK_HAS_TRIVIAL_ASSIGN;
8813       break;
8814     case RID_HAS_TRIVIAL_CONSTRUCTOR:
8815       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8816       break;
8817     case RID_HAS_TRIVIAL_COPY:
8818       kind = CPTK_HAS_TRIVIAL_COPY;
8819       break;
8820     case RID_HAS_TRIVIAL_DESTRUCTOR:
8821       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8822       break;
8823     case RID_HAS_VIRTUAL_DESTRUCTOR:
8824       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8825       break;
8826     case RID_IS_ABSTRACT:
8827       kind = CPTK_IS_ABSTRACT;
8828       break;
8829     case RID_IS_BASE_OF:
8830       kind = CPTK_IS_BASE_OF;
8831       binary = true;
8832       break;
8833     case RID_IS_CLASS:
8834       kind = CPTK_IS_CLASS;
8835       break;
8836     case RID_IS_EMPTY:
8837       kind = CPTK_IS_EMPTY;
8838       break;
8839     case RID_IS_ENUM:
8840       kind = CPTK_IS_ENUM;
8841       break;
8842     case RID_IS_FINAL:
8843       kind = CPTK_IS_FINAL;
8844       break;
8845     case RID_IS_LITERAL_TYPE:
8846       kind = CPTK_IS_LITERAL_TYPE;
8847       break;
8848     case RID_IS_POD:
8849       kind = CPTK_IS_POD;
8850       break;
8851     case RID_IS_POLYMORPHIC:
8852       kind = CPTK_IS_POLYMORPHIC;
8853       break;
8854     case RID_IS_STD_LAYOUT:
8855       kind = CPTK_IS_STD_LAYOUT;
8856       break;
8857     case RID_IS_TRIVIAL:
8858       kind = CPTK_IS_TRIVIAL;
8859       break;
8860     case RID_IS_TRIVIALLY_ASSIGNABLE:
8861       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8862       binary = true;
8863       break;
8864     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8865       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8866       variadic = true;
8867       break;
8868     case RID_IS_TRIVIALLY_COPYABLE:
8869       kind = CPTK_IS_TRIVIALLY_COPYABLE;
8870       break;
8871     case RID_IS_UNION:
8872       kind = CPTK_IS_UNION;
8873       break;
8874     case RID_UNDERLYING_TYPE:
8875       kind = CPTK_UNDERLYING_TYPE;
8876       break;
8877     case RID_BASES:
8878       kind = CPTK_BASES;
8879       break;
8880     case RID_DIRECT_BASES:
8881       kind = CPTK_DIRECT_BASES;
8882       break;
8883     default:
8884       gcc_unreachable ();
8885     }
8886
8887   /* Consume the token.  */
8888   cp_lexer_consume_token (parser->lexer);
8889
8890   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8891
8892   type1 = cp_parser_type_id (parser);
8893
8894   if (type1 == error_mark_node)
8895     return error_mark_node;
8896
8897   if (binary)
8898     {
8899       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8900  
8901       type2 = cp_parser_type_id (parser);
8902
8903       if (type2 == error_mark_node)
8904         return error_mark_node;
8905     }
8906   else if (variadic)
8907     {
8908       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8909         {
8910           cp_lexer_consume_token (parser->lexer);
8911           tree elt = cp_parser_type_id (parser);
8912           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8913             {
8914               cp_lexer_consume_token (parser->lexer);
8915               elt = make_pack_expansion (elt);
8916             }
8917           if (elt == error_mark_node)
8918             return error_mark_node;
8919           type2 = tree_cons (NULL_TREE, elt, type2);
8920         }
8921     }
8922
8923   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8924
8925   /* Complete the trait expression, which may mean either processing
8926      the trait expr now or saving it for template instantiation.  */
8927   switch(kind)
8928     {
8929     case CPTK_UNDERLYING_TYPE:
8930       return finish_underlying_type (type1);
8931     case CPTK_BASES:
8932       return finish_bases (type1, false);
8933     case CPTK_DIRECT_BASES:
8934       return finish_bases (type1, true);
8935     default:
8936       return finish_trait_expr (kind, type1, type2);
8937     }
8938 }
8939
8940 /* Lambdas that appear in variable initializer or default argument scope
8941    get that in their mangling, so we need to record it.  We might as well
8942    use the count for function and namespace scopes as well.  */
8943 static GTY(()) tree lambda_scope;
8944 static GTY(()) int lambda_count;
8945 typedef struct GTY(()) tree_int
8946 {
8947   tree t;
8948   int i;
8949 } tree_int;
8950 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8951
8952 static void
8953 start_lambda_scope (tree decl)
8954 {
8955   tree_int ti;
8956   gcc_assert (decl);
8957   /* Once we're inside a function, we ignore other scopes and just push
8958      the function again so that popping works properly.  */
8959   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8960     decl = current_function_decl;
8961   ti.t = lambda_scope;
8962   ti.i = lambda_count;
8963   vec_safe_push (lambda_scope_stack, ti);
8964   if (lambda_scope != decl)
8965     {
8966       /* Don't reset the count if we're still in the same function.  */
8967       lambda_scope = decl;
8968       lambda_count = 0;
8969     }
8970 }
8971
8972 static void
8973 record_lambda_scope (tree lambda)
8974 {
8975   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8976   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8977 }
8978
8979 static void
8980 finish_lambda_scope (void)
8981 {
8982   tree_int *p = &lambda_scope_stack->last ();
8983   if (lambda_scope != p->t)
8984     {
8985       lambda_scope = p->t;
8986       lambda_count = p->i;
8987     }
8988   lambda_scope_stack->pop ();
8989 }
8990
8991 /* Parse a lambda expression.
8992
8993    lambda-expression:
8994      lambda-introducer lambda-declarator [opt] compound-statement
8995
8996    Returns a representation of the expression.  */
8997
8998 static tree
8999 cp_parser_lambda_expression (cp_parser* parser)
9000 {
9001   tree lambda_expr = build_lambda_expr ();
9002   tree type;
9003   bool ok = true;
9004   cp_token *token = cp_lexer_peek_token (parser->lexer);
9005   cp_token_position start = 0;
9006
9007   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9008
9009   if (cp_unevaluated_operand)
9010     {
9011       if (!token->error_reported)
9012         {
9013           error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9014                     "lambda-expression in unevaluated context");
9015           token->error_reported = true;
9016         }
9017       ok = false;
9018     }
9019   else if (parser->in_template_argument_list_p)
9020     {
9021       if (!token->error_reported)
9022         {
9023           error_at (token->location, "lambda-expression in template-argument");
9024           token->error_reported = true;
9025         }
9026       ok = false;
9027     }
9028
9029   /* We may be in the middle of deferred access check.  Disable
9030      it now.  */
9031   push_deferring_access_checks (dk_no_deferred);
9032
9033   cp_parser_lambda_introducer (parser, lambda_expr);
9034
9035   type = begin_lambda_type (lambda_expr);
9036   if (type == error_mark_node)
9037     return error_mark_node;
9038
9039   record_lambda_scope (lambda_expr);
9040
9041   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
9042   determine_visibility (TYPE_NAME (type));
9043
9044   /* Now that we've started the type, add the capture fields for any
9045      explicit captures.  */
9046   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9047
9048   {
9049     /* Inside the class, surrounding template-parameter-lists do not apply.  */
9050     unsigned int saved_num_template_parameter_lists
9051         = parser->num_template_parameter_lists;
9052     unsigned char in_statement = parser->in_statement;
9053     bool in_switch_statement_p = parser->in_switch_statement_p;
9054     bool fully_implicit_function_template_p
9055         = parser->fully_implicit_function_template_p;
9056     tree implicit_template_parms = parser->implicit_template_parms;
9057     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9058     bool auto_is_implicit_function_template_parm_p
9059         = parser->auto_is_implicit_function_template_parm_p;
9060
9061     parser->num_template_parameter_lists = 0;
9062     parser->in_statement = 0;
9063     parser->in_switch_statement_p = false;
9064     parser->fully_implicit_function_template_p = false;
9065     parser->implicit_template_parms = 0;
9066     parser->implicit_template_scope = 0;
9067     parser->auto_is_implicit_function_template_parm_p = false;
9068
9069     /* By virtue of defining a local class, a lambda expression has access to
9070        the private variables of enclosing classes.  */
9071
9072     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9073
9074     if (ok)
9075       {
9076         if (!cp_parser_error_occurred (parser)
9077             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9078             && cp_parser_start_tentative_firewall (parser))
9079           start = token;
9080         cp_parser_lambda_body (parser, lambda_expr);
9081       }
9082     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9083       {
9084         if (cp_parser_skip_to_closing_brace (parser))
9085           cp_lexer_consume_token (parser->lexer);
9086       }
9087
9088     /* The capture list was built up in reverse order; fix that now.  */
9089     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9090       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9091
9092     if (ok)
9093       maybe_add_lambda_conv_op (type);
9094
9095     type = finish_struct (type, /*attributes=*/NULL_TREE);
9096
9097     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9098     parser->in_statement = in_statement;
9099     parser->in_switch_statement_p = in_switch_statement_p;
9100     parser->fully_implicit_function_template_p
9101         = fully_implicit_function_template_p;
9102     parser->implicit_template_parms = implicit_template_parms;
9103     parser->implicit_template_scope = implicit_template_scope;
9104     parser->auto_is_implicit_function_template_parm_p
9105         = auto_is_implicit_function_template_parm_p;
9106   }
9107
9108   pop_deferring_access_checks ();
9109
9110   /* This field is only used during parsing of the lambda.  */
9111   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9112
9113   /* This lambda shouldn't have any proxies left at this point.  */
9114   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9115   /* And now that we're done, push proxies for an enclosing lambda.  */
9116   insert_pending_capture_proxies ();
9117
9118   if (ok)
9119     lambda_expr = build_lambda_object (lambda_expr);
9120   else
9121     lambda_expr = error_mark_node;
9122
9123   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9124
9125   return lambda_expr;
9126 }
9127
9128 /* Parse the beginning of a lambda expression.
9129
9130    lambda-introducer:
9131      [ lambda-capture [opt] ]
9132
9133    LAMBDA_EXPR is the current representation of the lambda expression.  */
9134
9135 static void
9136 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9137 {
9138   /* Need commas after the first capture.  */
9139   bool first = true;
9140
9141   /* Eat the leading `['.  */
9142   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9143
9144   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
9145   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9146       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9147     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9148   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9149     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9150
9151   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9152     {
9153       cp_lexer_consume_token (parser->lexer);
9154       first = false;
9155     }
9156
9157   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9158     {
9159       cp_token* capture_token;
9160       tree capture_id;
9161       tree capture_init_expr;
9162       cp_id_kind idk = CP_ID_KIND_NONE;
9163       bool explicit_init_p = false;
9164
9165       enum capture_kind_type
9166       {
9167         BY_COPY,
9168         BY_REFERENCE
9169       };
9170       enum capture_kind_type capture_kind = BY_COPY;
9171
9172       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9173         {
9174           error ("expected end of capture-list");
9175           return;
9176         }
9177
9178       if (first)
9179         first = false;
9180       else
9181         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9182
9183       /* Possibly capture `this'.  */
9184       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9185         {
9186           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9187           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9188             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9189                      "with by-copy capture default");
9190           cp_lexer_consume_token (parser->lexer);
9191           add_capture (lambda_expr,
9192                        /*id=*/this_identifier,
9193                        /*initializer=*/finish_this_expr(),
9194                        /*by_reference_p=*/false,
9195                        explicit_init_p);
9196           continue;
9197         }
9198
9199       /* Remember whether we want to capture as a reference or not.  */
9200       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9201         {
9202           capture_kind = BY_REFERENCE;
9203           cp_lexer_consume_token (parser->lexer);
9204         }
9205
9206       /* Get the identifier.  */
9207       capture_token = cp_lexer_peek_token (parser->lexer);
9208       capture_id = cp_parser_identifier (parser);
9209
9210       if (capture_id == error_mark_node)
9211         /* Would be nice to have a cp_parser_skip_to_closing_x for general
9212            delimiters, but I modified this to stop on unnested ']' as well.  It
9213            was already changed to stop on unnested '}', so the
9214            "closing_parenthesis" name is no more misleading with my change.  */
9215         {
9216           cp_parser_skip_to_closing_parenthesis (parser,
9217                                                  /*recovering=*/true,
9218                                                  /*or_comma=*/true,
9219                                                  /*consume_paren=*/true);
9220           break;
9221         }
9222
9223       /* Find the initializer for this capture.  */
9224       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9225           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9226           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9227         {
9228           bool direct, non_constant;
9229           /* An explicit initializer exists.  */
9230           if (cxx_dialect < cxx14)
9231             pedwarn (input_location, 0,
9232                      "lambda capture initializers "
9233                      "only available with -std=c++14 or -std=gnu++14");
9234           capture_init_expr = cp_parser_initializer (parser, &direct,
9235                                                      &non_constant);
9236           explicit_init_p = true;
9237           if (capture_init_expr == NULL_TREE)
9238             {
9239               error ("empty initializer for lambda init-capture");
9240               capture_init_expr = error_mark_node;
9241             }
9242         }
9243       else
9244         {
9245           const char* error_msg;
9246
9247           /* Turn the identifier into an id-expression.  */
9248           capture_init_expr
9249             = cp_parser_lookup_name_simple (parser, capture_id,
9250                                             capture_token->location);
9251
9252           if (capture_init_expr == error_mark_node)
9253             {
9254               unqualified_name_lookup_error (capture_id);
9255               continue;
9256             }
9257           else if (DECL_P (capture_init_expr)
9258                    && (!VAR_P (capture_init_expr)
9259                        && TREE_CODE (capture_init_expr) != PARM_DECL))
9260             {
9261               error_at (capture_token->location,
9262                         "capture of non-variable %qD ",
9263                         capture_init_expr);
9264               inform (0, "%q+#D declared here", capture_init_expr);
9265               continue;
9266             }
9267           if (VAR_P (capture_init_expr)
9268               && decl_storage_duration (capture_init_expr) != dk_auto)
9269             {
9270               if (pedwarn (capture_token->location, 0, "capture of variable "
9271                            "%qD with non-automatic storage duration",
9272                            capture_init_expr))
9273                 inform (0, "%q+#D declared here", capture_init_expr);
9274               continue;
9275             }
9276
9277           capture_init_expr
9278             = finish_id_expression
9279                 (capture_id,
9280                  capture_init_expr,
9281                  parser->scope,
9282                  &idk,
9283                  /*integral_constant_expression_p=*/false,
9284                  /*allow_non_integral_constant_expression_p=*/false,
9285                  /*non_integral_constant_expression_p=*/NULL,
9286                  /*template_p=*/false,
9287                  /*done=*/true,
9288                  /*address_p=*/false,
9289                  /*template_arg_p=*/false,
9290                  &error_msg,
9291                  capture_token->location);
9292
9293           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9294             {
9295               cp_lexer_consume_token (parser->lexer);
9296               capture_init_expr = make_pack_expansion (capture_init_expr);
9297             }
9298           else
9299             check_for_bare_parameter_packs (capture_init_expr);
9300         }
9301
9302       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9303           && !explicit_init_p)
9304         {
9305           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9306               && capture_kind == BY_COPY)
9307             pedwarn (capture_token->location, 0, "explicit by-copy capture "
9308                      "of %qD redundant with by-copy capture default",
9309                      capture_id);
9310           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9311               && capture_kind == BY_REFERENCE)
9312             pedwarn (capture_token->location, 0, "explicit by-reference "
9313                      "capture of %qD redundant with by-reference capture "
9314                      "default", capture_id);
9315         }
9316
9317       add_capture (lambda_expr,
9318                    capture_id,
9319                    capture_init_expr,
9320                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
9321                    explicit_init_p);
9322     }
9323
9324   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9325 }
9326
9327 /* Parse the (optional) middle of a lambda expression.
9328
9329    lambda-declarator:
9330      < template-parameter-list [opt] >
9331      ( parameter-declaration-clause [opt] )
9332        attribute-specifier [opt]
9333        mutable [opt]
9334        exception-specification [opt]
9335        lambda-return-type-clause [opt]
9336
9337    LAMBDA_EXPR is the current representation of the lambda expression.  */
9338
9339 static bool
9340 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9341 {
9342   /* 5.1.1.4 of the standard says:
9343        If a lambda-expression does not include a lambda-declarator, it is as if
9344        the lambda-declarator were ().
9345      This means an empty parameter list, no attributes, and no exception
9346      specification.  */
9347   tree param_list = void_list_node;
9348   tree attributes = NULL_TREE;
9349   tree exception_spec = NULL_TREE;
9350   tree template_param_list = NULL_TREE;
9351
9352   /* The template-parameter-list is optional, but must begin with
9353      an opening angle if present.  */
9354   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9355     {
9356       if (cxx_dialect < cxx14)
9357         pedwarn (parser->lexer->next_token->location, 0,
9358                  "lambda templates are only available with "
9359                  "-std=c++14 or -std=gnu++14");
9360
9361       cp_lexer_consume_token (parser->lexer);
9362
9363       template_param_list = cp_parser_template_parameter_list (parser);
9364
9365       cp_parser_skip_to_end_of_template_parameter_list (parser);
9366
9367       /* We just processed one more parameter list.  */
9368       ++parser->num_template_parameter_lists;
9369     }
9370
9371   /* The parameter-declaration-clause is optional (unless
9372      template-parameter-list was given), but must begin with an
9373      opening parenthesis if present.  */
9374   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9375     {
9376       cp_lexer_consume_token (parser->lexer);
9377
9378       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9379
9380       /* Parse parameters.  */
9381       param_list = cp_parser_parameter_declaration_clause (parser);
9382
9383       /* Default arguments shall not be specified in the
9384          parameter-declaration-clause of a lambda-declarator.  */
9385       for (tree t = param_list; t; t = TREE_CHAIN (t))
9386         if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9387           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9388                    "default argument specified for lambda parameter");
9389
9390       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9391
9392       attributes = cp_parser_attributes_opt (parser);
9393
9394       /* Parse optional `mutable' keyword.  */
9395       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9396         {
9397           cp_lexer_consume_token (parser->lexer);
9398           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9399         }
9400
9401       /* Parse optional exception specification.  */
9402       exception_spec = cp_parser_exception_specification_opt (parser);
9403
9404       /* Parse optional trailing return type.  */
9405       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9406         {
9407           cp_lexer_consume_token (parser->lexer);
9408           LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9409             = cp_parser_trailing_type_id (parser);
9410         }
9411
9412       /* The function parameters must be in scope all the way until after the
9413          trailing-return-type in case of decltype.  */
9414       pop_bindings_and_leave_scope ();
9415     }
9416   else if (template_param_list != NULL_TREE) // generate diagnostic
9417     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9418
9419   /* Create the function call operator.
9420
9421      Messing with declarators like this is no uglier than building up the
9422      FUNCTION_DECL by hand, and this is less likely to get out of sync with
9423      other code.  */
9424   {
9425     cp_decl_specifier_seq return_type_specs;
9426     cp_declarator* declarator;
9427     tree fco;
9428     int quals;
9429     void *p;
9430
9431     clear_decl_specs (&return_type_specs);
9432     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9433       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9434     else
9435       /* Maybe we will deduce the return type later.  */
9436       return_type_specs.type = make_auto ();
9437
9438     p = obstack_alloc (&declarator_obstack, 0);
9439
9440     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9441                                      sfk_none);
9442
9443     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9444              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9445     declarator = make_call_declarator (declarator, param_list, quals,
9446                                        VIRT_SPEC_UNSPECIFIED,
9447                                        REF_QUAL_NONE,
9448                                        exception_spec,
9449                                        /*late_return_type=*/NULL_TREE);
9450     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9451
9452     fco = grokmethod (&return_type_specs,
9453                       declarator,
9454                       attributes);
9455     if (fco != error_mark_node)
9456       {
9457         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9458         DECL_ARTIFICIAL (fco) = 1;
9459         /* Give the object parameter a different name.  */
9460         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9461         if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9462           TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9463       }
9464     if (template_param_list)
9465       {
9466         fco = finish_member_template_decl (fco);
9467         finish_template_decl (template_param_list);
9468         --parser->num_template_parameter_lists;
9469       }
9470     else if (parser->fully_implicit_function_template_p)
9471       fco = finish_fully_implicit_template (parser, fco);
9472
9473     finish_member_declaration (fco);
9474
9475     obstack_free (&declarator_obstack, p);
9476
9477     return (fco != error_mark_node);
9478   }
9479 }
9480
9481 /* Parse the body of a lambda expression, which is simply
9482
9483    compound-statement
9484
9485    but which requires special handling.
9486    LAMBDA_EXPR is the current representation of the lambda expression.  */
9487
9488 static void
9489 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9490 {
9491   bool nested = (current_function_decl != NULL_TREE);
9492   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9493   if (nested)
9494     push_function_context ();
9495   else
9496     /* Still increment function_depth so that we don't GC in the
9497        middle of an expression.  */
9498     ++function_depth;
9499   /* Clear this in case we're in the middle of a default argument.  */
9500   parser->local_variables_forbidden_p = false;
9501
9502   /* Finish the function call operator
9503      - class_specifier
9504      + late_parsing_for_member
9505      + function_definition_after_declarator
9506      + ctor_initializer_opt_and_function_body  */
9507   {
9508     tree fco = lambda_function (lambda_expr);
9509     tree body;
9510     bool done = false;
9511     tree compound_stmt;
9512     tree cap;
9513
9514     /* Let the front end know that we are going to be defining this
9515        function.  */
9516     start_preparsed_function (fco,
9517                               NULL_TREE,
9518                               SF_PRE_PARSED | SF_INCLASS_INLINE);
9519
9520     start_lambda_scope (fco);
9521     body = begin_function_body ();
9522
9523     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9524       goto out;
9525
9526     /* Push the proxies for any explicit captures.  */
9527     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9528          cap = TREE_CHAIN (cap))
9529       build_capture_proxy (TREE_PURPOSE (cap));
9530
9531     compound_stmt = begin_compound_stmt (0);
9532
9533     /* 5.1.1.4 of the standard says:
9534          If a lambda-expression does not include a trailing-return-type, it
9535          is as if the trailing-return-type denotes the following type:
9536           * if the compound-statement is of the form
9537                { return attribute-specifier [opt] expression ; }
9538              the type of the returned expression after lvalue-to-rvalue
9539              conversion (_conv.lval_ 4.1), array-to-pointer conversion
9540              (_conv.array_ 4.2), and function-to-pointer conversion
9541              (_conv.func_ 4.3);
9542           * otherwise, void.  */
9543
9544     /* In a lambda that has neither a lambda-return-type-clause
9545        nor a deducible form, errors should be reported for return statements
9546        in the body.  Since we used void as the placeholder return type, parsing
9547        the body as usual will give such desired behavior.  */
9548     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9549         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9550         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9551       {
9552         tree expr = NULL_TREE;
9553         cp_id_kind idk = CP_ID_KIND_NONE;
9554
9555         /* Parse tentatively in case there's more after the initial return
9556            statement.  */
9557         cp_parser_parse_tentatively (parser);
9558
9559         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9560
9561         expr = cp_parser_expression (parser, &idk);
9562
9563         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9564         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9565
9566         if (cp_parser_parse_definitely (parser))
9567           {
9568             if (!processing_template_decl)
9569               apply_deduced_return_type (fco, lambda_return_type (expr));
9570
9571             /* Will get error here if type not deduced yet.  */
9572             finish_return_stmt (expr);
9573
9574             done = true;
9575           }
9576       }
9577
9578     if (!done)
9579       {
9580         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9581           cp_parser_label_declaration (parser);
9582         cp_parser_statement_seq_opt (parser, NULL_TREE);
9583         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9584       }
9585
9586     finish_compound_stmt (compound_stmt);
9587
9588   out:
9589     finish_function_body (body);
9590     finish_lambda_scope ();
9591
9592     /* Finish the function and generate code for it if necessary.  */
9593     tree fn = finish_function (/*inline*/2);
9594
9595     /* Only expand if the call op is not a template.  */
9596     if (!DECL_TEMPLATE_INFO (fco))
9597       expand_or_defer_fn (fn);
9598   }
9599
9600   parser->local_variables_forbidden_p = local_variables_forbidden_p;
9601   if (nested)
9602     pop_function_context();
9603   else
9604     --function_depth;
9605 }
9606
9607 /* Statements [gram.stmt.stmt]  */
9608
9609 /* Parse a statement.
9610
9611    statement:
9612      labeled-statement
9613      expression-statement
9614      compound-statement
9615      selection-statement
9616      iteration-statement
9617      jump-statement
9618      declaration-statement
9619      try-block
9620
9621   C++11:
9622
9623   statement:
9624     labeled-statement
9625     attribute-specifier-seq (opt) expression-statement
9626     attribute-specifier-seq (opt) compound-statement
9627     attribute-specifier-seq (opt) selection-statement
9628     attribute-specifier-seq (opt) iteration-statement
9629     attribute-specifier-seq (opt) jump-statement
9630     declaration-statement
9631     attribute-specifier-seq (opt) try-block
9632
9633   TM Extension:
9634
9635    statement:
9636      atomic-statement
9637
9638   IN_COMPOUND is true when the statement is nested inside a
9639   cp_parser_compound_statement; this matters for certain pragmas.
9640
9641   If IF_P is not NULL, *IF_P is set to indicate whether the statement
9642   is a (possibly labeled) if statement which is not enclosed in braces
9643   and has an else clause.  This is used to implement -Wparentheses.  */
9644
9645 static void
9646 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9647                      bool in_compound, bool *if_p)
9648 {
9649   tree statement, std_attrs = NULL_TREE;
9650   cp_token *token;
9651   location_t statement_location, attrs_location;
9652
9653  restart:
9654   if (if_p != NULL)
9655     *if_p = false;
9656   /* There is no statement yet.  */
9657   statement = NULL_TREE;
9658
9659   saved_token_sentinel saved_tokens (parser->lexer);
9660   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9661   if (c_dialect_objc ())
9662     /* In obj-c++, seeing '[[' might be the either the beginning of
9663        c++11 attributes, or a nested objc-message-expression.  So
9664        let's parse the c++11 attributes tentatively.  */
9665     cp_parser_parse_tentatively (parser);
9666   std_attrs = cp_parser_std_attribute_spec_seq (parser);
9667   if (c_dialect_objc ())
9668     {
9669       if (!cp_parser_parse_definitely (parser))
9670         std_attrs = NULL_TREE;
9671     }
9672
9673   /* Peek at the next token.  */
9674   token = cp_lexer_peek_token (parser->lexer);
9675   /* Remember the location of the first token in the statement.  */
9676   statement_location = token->location;
9677   /* If this is a keyword, then that will often determine what kind of
9678      statement we have.  */
9679   if (token->type == CPP_KEYWORD)
9680     {
9681       enum rid keyword = token->keyword;
9682
9683       switch (keyword)
9684         {
9685         case RID_CASE:
9686         case RID_DEFAULT:
9687           /* Looks like a labeled-statement with a case label.
9688              Parse the label, and then use tail recursion to parse
9689              the statement.  */
9690           cp_parser_label_for_labeled_statement (parser, std_attrs);
9691           goto restart;
9692
9693         case RID_IF:
9694         case RID_SWITCH:
9695           statement = cp_parser_selection_statement (parser, if_p);
9696           break;
9697
9698         case RID_WHILE:
9699         case RID_DO:
9700         case RID_FOR:
9701           statement = cp_parser_iteration_statement (parser, false);
9702           break;
9703
9704         case RID_CILK_FOR:
9705           if (!flag_cilkplus)
9706             {
9707               error_at (cp_lexer_peek_token (parser->lexer)->location,
9708                         "-fcilkplus must be enabled to use %<_Cilk_for%>");
9709               cp_lexer_consume_token (parser->lexer);
9710               statement = error_mark_node;
9711             }
9712           else
9713             statement = cp_parser_cilk_for (parser, integer_zero_node);
9714           break;
9715
9716         case RID_BREAK:
9717         case RID_CONTINUE:
9718         case RID_RETURN:
9719         case RID_GOTO:
9720           statement = cp_parser_jump_statement (parser);
9721           break;
9722
9723         case RID_CILK_SYNC:
9724           cp_lexer_consume_token (parser->lexer);
9725           if (flag_cilkplus)
9726             {
9727               tree sync_expr = build_cilk_sync ();
9728               SET_EXPR_LOCATION (sync_expr,
9729                                  token->location);
9730               statement = finish_expr_stmt (sync_expr);
9731             }
9732           else
9733             {
9734               error_at (token->location, "-fcilkplus must be enabled to use"
9735                         " %<_Cilk_sync%>");
9736               statement = error_mark_node;
9737             }
9738           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9739           break;
9740
9741           /* Objective-C++ exception-handling constructs.  */
9742         case RID_AT_TRY:
9743         case RID_AT_CATCH:
9744         case RID_AT_FINALLY:
9745         case RID_AT_SYNCHRONIZED:
9746         case RID_AT_THROW:
9747           statement = cp_parser_objc_statement (parser);
9748           break;
9749
9750         case RID_TRY:
9751           statement = cp_parser_try_block (parser);
9752           break;
9753
9754         case RID_NAMESPACE:
9755           /* This must be a namespace alias definition.  */
9756           cp_parser_declaration_statement (parser);
9757           return;
9758           
9759         case RID_TRANSACTION_ATOMIC:
9760         case RID_TRANSACTION_RELAXED:
9761           statement = cp_parser_transaction (parser, keyword);
9762           break;
9763         case RID_TRANSACTION_CANCEL:
9764           statement = cp_parser_transaction_cancel (parser);
9765           break;
9766
9767         default:
9768           /* It might be a keyword like `int' that can start a
9769              declaration-statement.  */
9770           break;
9771         }
9772     }
9773   else if (token->type == CPP_NAME)
9774     {
9775       /* If the next token is a `:', then we are looking at a
9776          labeled-statement.  */
9777       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9778       if (token->type == CPP_COLON)
9779         {
9780           /* Looks like a labeled-statement with an ordinary label.
9781              Parse the label, and then use tail recursion to parse
9782              the statement.  */
9783
9784           cp_parser_label_for_labeled_statement (parser, std_attrs);
9785           goto restart;
9786         }
9787     }
9788   /* Anything that starts with a `{' must be a compound-statement.  */
9789   else if (token->type == CPP_OPEN_BRACE)
9790     statement = cp_parser_compound_statement (parser, NULL, false, false);
9791   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9792      a statement all its own.  */
9793   else if (token->type == CPP_PRAGMA)
9794     {
9795       /* Only certain OpenMP pragmas are attached to statements, and thus
9796          are considered statements themselves.  All others are not.  In
9797          the context of a compound, accept the pragma as a "statement" and
9798          return so that we can check for a close brace.  Otherwise we
9799          require a real statement and must go back and read one.  */
9800       if (in_compound)
9801         cp_parser_pragma (parser, pragma_compound);
9802       else if (!cp_parser_pragma (parser, pragma_stmt))
9803         goto restart;
9804       return;
9805     }
9806   else if (token->type == CPP_EOF)
9807     {
9808       cp_parser_error (parser, "expected statement");
9809       return;
9810     }
9811
9812   /* Everything else must be a declaration-statement or an
9813      expression-statement.  Try for the declaration-statement
9814      first, unless we are looking at a `;', in which case we know that
9815      we have an expression-statement.  */
9816   if (!statement)
9817     {
9818       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9819         {
9820           if (std_attrs != NULL_TREE)
9821             {
9822               /*  Attributes should be parsed as part of the the
9823                   declaration, so let's un-parse them.  */
9824               saved_tokens.rollback();
9825               std_attrs = NULL_TREE;
9826             }
9827
9828           cp_parser_parse_tentatively (parser);
9829           /* Try to parse the declaration-statement.  */
9830           cp_parser_declaration_statement (parser);
9831           /* If that worked, we're done.  */
9832           if (cp_parser_parse_definitely (parser))
9833             return;
9834         }
9835       /* Look for an expression-statement instead.  */
9836       statement = cp_parser_expression_statement (parser, in_statement_expr);
9837     }
9838
9839   /* Set the line number for the statement.  */
9840   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9841     SET_EXPR_LOCATION (statement, statement_location);
9842
9843   /* Note that for now, we don't do anything with c++11 statements
9844      parsed at this level.  */
9845   if (std_attrs != NULL_TREE)
9846     warning_at (attrs_location,
9847                 OPT_Wattributes,
9848                 "attributes at the beginning of statement are ignored");
9849 }
9850
9851 /* Parse the label for a labeled-statement, i.e.
9852
9853    identifier :
9854    case constant-expression :
9855    default :
9856
9857    GNU Extension:
9858    case constant-expression ... constant-expression : statement
9859
9860    When a label is parsed without errors, the label is added to the
9861    parse tree by the finish_* functions, so this function doesn't
9862    have to return the label.  */
9863
9864 static void
9865 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9866 {
9867   cp_token *token;
9868   tree label = NULL_TREE;
9869   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9870
9871   /* The next token should be an identifier.  */
9872   token = cp_lexer_peek_token (parser->lexer);
9873   if (token->type != CPP_NAME
9874       && token->type != CPP_KEYWORD)
9875     {
9876       cp_parser_error (parser, "expected labeled-statement");
9877       return;
9878     }
9879
9880   parser->colon_corrects_to_scope_p = false;
9881   switch (token->keyword)
9882     {
9883     case RID_CASE:
9884       {
9885         tree expr, expr_hi;
9886         cp_token *ellipsis;
9887
9888         /* Consume the `case' token.  */
9889         cp_lexer_consume_token (parser->lexer);
9890         /* Parse the constant-expression.  */
9891         expr = cp_parser_constant_expression (parser);
9892         if (check_for_bare_parameter_packs (expr))
9893           expr = error_mark_node;
9894
9895         ellipsis = cp_lexer_peek_token (parser->lexer);
9896         if (ellipsis->type == CPP_ELLIPSIS)
9897           {
9898             /* Consume the `...' token.  */
9899             cp_lexer_consume_token (parser->lexer);
9900             expr_hi = cp_parser_constant_expression (parser);
9901             if (check_for_bare_parameter_packs (expr_hi))
9902               expr_hi = error_mark_node;
9903
9904             /* We don't need to emit warnings here, as the common code
9905                will do this for us.  */
9906           }
9907         else
9908           expr_hi = NULL_TREE;
9909
9910         if (parser->in_switch_statement_p)
9911           finish_case_label (token->location, expr, expr_hi);
9912         else
9913           error_at (token->location,
9914                     "case label %qE not within a switch statement",
9915                     expr);
9916       }
9917       break;
9918
9919     case RID_DEFAULT:
9920       /* Consume the `default' token.  */
9921       cp_lexer_consume_token (parser->lexer);
9922
9923       if (parser->in_switch_statement_p)
9924         finish_case_label (token->location, NULL_TREE, NULL_TREE);
9925       else
9926         error_at (token->location, "case label not within a switch statement");
9927       break;
9928
9929     default:
9930       /* Anything else must be an ordinary label.  */
9931       label = finish_label_stmt (cp_parser_identifier (parser));
9932       break;
9933     }
9934
9935   /* Require the `:' token.  */
9936   cp_parser_require (parser, CPP_COLON, RT_COLON);
9937
9938   /* An ordinary label may optionally be followed by attributes.
9939      However, this is only permitted if the attributes are then
9940      followed by a semicolon.  This is because, for backward
9941      compatibility, when parsing
9942        lab: __attribute__ ((unused)) int i;
9943      we want the attribute to attach to "i", not "lab".  */
9944   if (label != NULL_TREE
9945       && cp_next_tokens_can_be_gnu_attribute_p (parser))
9946     {
9947       tree attrs;
9948       cp_parser_parse_tentatively (parser);
9949       attrs = cp_parser_gnu_attributes_opt (parser);
9950       if (attrs == NULL_TREE
9951           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9952         cp_parser_abort_tentative_parse (parser);
9953       else if (!cp_parser_parse_definitely (parser))
9954         ;
9955       else
9956         attributes = chainon (attributes, attrs);
9957     }
9958
9959   if (attributes != NULL_TREE)
9960     cplus_decl_attributes (&label, attributes, 0);
9961
9962   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9963 }
9964
9965 /* Parse an expression-statement.
9966
9967    expression-statement:
9968      expression [opt] ;
9969
9970    Returns the new EXPR_STMT -- or NULL_TREE if the expression
9971    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9972    indicates whether this expression-statement is part of an
9973    expression statement.  */
9974
9975 static tree
9976 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9977 {
9978   tree statement = NULL_TREE;
9979   cp_token *token = cp_lexer_peek_token (parser->lexer);
9980
9981   /* If the next token is a ';', then there is no expression
9982      statement.  */
9983   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9984     {
9985       statement = cp_parser_expression (parser);
9986       if (statement == error_mark_node
9987           && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9988         {
9989           cp_parser_skip_to_end_of_block_or_statement (parser);
9990           return error_mark_node;
9991         }
9992     }
9993
9994   /* Give a helpful message for "A<T>::type t;" and the like.  */
9995   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9996       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9997     {
9998       if (TREE_CODE (statement) == SCOPE_REF)
9999         error_at (token->location, "need %<typename%> before %qE because "
10000                   "%qT is a dependent scope",
10001                   statement, TREE_OPERAND (statement, 0));
10002       else if (is_overloaded_fn (statement)
10003                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10004         {
10005           /* A::A a; */
10006           tree fn = get_first_fn (statement);
10007           error_at (token->location,
10008                     "%<%T::%D%> names the constructor, not the type",
10009                     DECL_CONTEXT (fn), DECL_NAME (fn));
10010         }
10011     }
10012
10013   /* Consume the final `;'.  */
10014   cp_parser_consume_semicolon_at_end_of_statement (parser);
10015
10016   if (in_statement_expr
10017       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10018     /* This is the final expression statement of a statement
10019        expression.  */
10020     statement = finish_stmt_expr_expr (statement, in_statement_expr);
10021   else if (statement)
10022     statement = finish_expr_stmt (statement);
10023
10024   return statement;
10025 }
10026
10027 /* Parse a compound-statement.
10028
10029    compound-statement:
10030      { statement-seq [opt] }
10031
10032    GNU extension:
10033
10034    compound-statement:
10035      { label-declaration-seq [opt] statement-seq [opt] }
10036
10037    label-declaration-seq:
10038      label-declaration
10039      label-declaration-seq label-declaration
10040
10041    Returns a tree representing the statement.  */
10042
10043 static tree
10044 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10045                               bool in_try, bool function_body)
10046 {
10047   tree compound_stmt;
10048
10049   /* Consume the `{'.  */
10050   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10051     return error_mark_node;
10052   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10053       && !function_body && cxx_dialect < cxx14)
10054     pedwarn (input_location, OPT_Wpedantic,
10055              "compound-statement in constexpr function");
10056   /* Begin the compound-statement.  */
10057   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10058   /* If the next keyword is `__label__' we have a label declaration.  */
10059   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10060     cp_parser_label_declaration (parser);
10061   /* Parse an (optional) statement-seq.  */
10062   cp_parser_statement_seq_opt (parser, in_statement_expr);
10063   /* Finish the compound-statement.  */
10064   finish_compound_stmt (compound_stmt);
10065   /* Consume the `}'.  */
10066   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10067
10068   return compound_stmt;
10069 }
10070
10071 /* Parse an (optional) statement-seq.
10072
10073    statement-seq:
10074      statement
10075      statement-seq [opt] statement  */
10076
10077 static void
10078 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10079 {
10080   /* Scan statements until there aren't any more.  */
10081   while (true)
10082     {
10083       cp_token *token = cp_lexer_peek_token (parser->lexer);
10084
10085       /* If we are looking at a `}', then we have run out of
10086          statements; the same is true if we have reached the end
10087          of file, or have stumbled upon a stray '@end'.  */
10088       if (token->type == CPP_CLOSE_BRACE
10089           || token->type == CPP_EOF
10090           || token->type == CPP_PRAGMA_EOL
10091           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10092         break;
10093       
10094       /* If we are in a compound statement and find 'else' then
10095          something went wrong.  */
10096       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10097         {
10098           if (parser->in_statement & IN_IF_STMT) 
10099             break;
10100           else
10101             {
10102               token = cp_lexer_consume_token (parser->lexer);
10103               error_at (token->location, "%<else%> without a previous %<if%>");
10104             }
10105         }
10106
10107       /* Parse the statement.  */
10108       cp_parser_statement (parser, in_statement_expr, true, NULL);
10109     }
10110 }
10111
10112 /* Parse a selection-statement.
10113
10114    selection-statement:
10115      if ( condition ) statement
10116      if ( condition ) statement else statement
10117      switch ( condition ) statement
10118
10119    Returns the new IF_STMT or SWITCH_STMT.
10120
10121    If IF_P is not NULL, *IF_P is set to indicate whether the statement
10122    is a (possibly labeled) if statement which is not enclosed in
10123    braces and has an else clause.  This is used to implement
10124    -Wparentheses.  */
10125
10126 static tree
10127 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10128 {
10129   cp_token *token;
10130   enum rid keyword;
10131
10132   if (if_p != NULL)
10133     *if_p = false;
10134
10135   /* Peek at the next token.  */
10136   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10137
10138   /* See what kind of keyword it is.  */
10139   keyword = token->keyword;
10140   switch (keyword)
10141     {
10142     case RID_IF:
10143     case RID_SWITCH:
10144       {
10145         tree statement;
10146         tree condition;
10147
10148         /* Look for the `('.  */
10149         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10150           {
10151             cp_parser_skip_to_end_of_statement (parser);
10152             return error_mark_node;
10153           }
10154
10155         /* Begin the selection-statement.  */
10156         if (keyword == RID_IF)
10157           statement = begin_if_stmt ();
10158         else
10159           statement = begin_switch_stmt ();
10160
10161         /* Parse the condition.  */
10162         condition = cp_parser_condition (parser);
10163         /* Look for the `)'.  */
10164         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10165           cp_parser_skip_to_closing_parenthesis (parser, true, false,
10166                                                  /*consume_paren=*/true);
10167
10168         if (keyword == RID_IF)
10169           {
10170             bool nested_if;
10171             unsigned char in_statement;
10172
10173             /* Add the condition.  */
10174             finish_if_stmt_cond (condition, statement);
10175
10176             /* Parse the then-clause.  */
10177             in_statement = parser->in_statement;
10178             parser->in_statement |= IN_IF_STMT;
10179             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10180               {
10181                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10182                 add_stmt (build_empty_stmt (loc));
10183                 cp_lexer_consume_token (parser->lexer);
10184                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10185                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
10186                               "empty body in an %<if%> statement");
10187                 nested_if = false;
10188               }
10189             else
10190               cp_parser_implicitly_scoped_statement (parser, &nested_if);
10191             parser->in_statement = in_statement;
10192
10193             finish_then_clause (statement);
10194
10195             /* If the next token is `else', parse the else-clause.  */
10196             if (cp_lexer_next_token_is_keyword (parser->lexer,
10197                                                 RID_ELSE))
10198               {
10199                 /* Consume the `else' keyword.  */
10200                 cp_lexer_consume_token (parser->lexer);
10201                 begin_else_clause (statement);
10202                 /* Parse the else-clause.  */
10203                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10204                   {
10205                     location_t loc;
10206                     loc = cp_lexer_peek_token (parser->lexer)->location;
10207                     warning_at (loc,
10208                                 OPT_Wempty_body, "suggest braces around "
10209                                 "empty body in an %<else%> statement");
10210                     add_stmt (build_empty_stmt (loc));
10211                     cp_lexer_consume_token (parser->lexer);
10212                   }
10213                 else
10214                   cp_parser_implicitly_scoped_statement (parser, NULL);
10215
10216                 finish_else_clause (statement);
10217
10218                 /* If we are currently parsing a then-clause, then
10219                    IF_P will not be NULL.  We set it to true to
10220                    indicate that this if statement has an else clause.
10221                    This may trigger the Wparentheses warning below
10222                    when we get back up to the parent if statement.  */
10223                 if (if_p != NULL)
10224                   *if_p = true;
10225               }
10226             else
10227               {
10228                 /* This if statement does not have an else clause.  If
10229                    NESTED_IF is true, then the then-clause is an if
10230                    statement which does have an else clause.  We warn
10231                    about the potential ambiguity.  */
10232                 if (nested_if)
10233                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10234                               "suggest explicit braces to avoid ambiguous"
10235                               " %<else%>");
10236               }
10237
10238             /* Now we're all done with the if-statement.  */
10239             finish_if_stmt (statement);
10240           }
10241         else
10242           {
10243             bool in_switch_statement_p;
10244             unsigned char in_statement;
10245
10246             /* Add the condition.  */
10247             finish_switch_cond (condition, statement);
10248
10249             /* Parse the body of the switch-statement.  */
10250             in_switch_statement_p = parser->in_switch_statement_p;
10251             in_statement = parser->in_statement;
10252             parser->in_switch_statement_p = true;
10253             parser->in_statement |= IN_SWITCH_STMT;
10254             cp_parser_implicitly_scoped_statement (parser, NULL);
10255             parser->in_switch_statement_p = in_switch_statement_p;
10256             parser->in_statement = in_statement;
10257
10258             /* Now we're all done with the switch-statement.  */
10259             finish_switch_stmt (statement);
10260           }
10261
10262         return statement;
10263       }
10264       break;
10265
10266     default:
10267       cp_parser_error (parser, "expected selection-statement");
10268       return error_mark_node;
10269     }
10270 }
10271
10272 /* Parse a condition.
10273
10274    condition:
10275      expression
10276      type-specifier-seq declarator = initializer-clause
10277      type-specifier-seq declarator braced-init-list
10278
10279    GNU Extension:
10280
10281    condition:
10282      type-specifier-seq declarator asm-specification [opt]
10283        attributes [opt] = assignment-expression
10284
10285    Returns the expression that should be tested.  */
10286
10287 static tree
10288 cp_parser_condition (cp_parser* parser)
10289 {
10290   cp_decl_specifier_seq type_specifiers;
10291   const char *saved_message;
10292   int declares_class_or_enum;
10293
10294   /* Try the declaration first.  */
10295   cp_parser_parse_tentatively (parser);
10296   /* New types are not allowed in the type-specifier-seq for a
10297      condition.  */
10298   saved_message = parser->type_definition_forbidden_message;
10299   parser->type_definition_forbidden_message
10300     = G_("types may not be defined in conditions");
10301   /* Parse the type-specifier-seq.  */
10302   cp_parser_decl_specifier_seq (parser,
10303                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10304                                 &type_specifiers,
10305                                 &declares_class_or_enum);
10306   /* Restore the saved message.  */
10307   parser->type_definition_forbidden_message = saved_message;
10308   /* If all is well, we might be looking at a declaration.  */
10309   if (!cp_parser_error_occurred (parser))
10310     {
10311       tree decl;
10312       tree asm_specification;
10313       tree attributes;
10314       cp_declarator *declarator;
10315       tree initializer = NULL_TREE;
10316
10317       /* Parse the declarator.  */
10318       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10319                                          /*ctor_dtor_or_conv_p=*/NULL,
10320                                          /*parenthesized_p=*/NULL,
10321                                          /*member_p=*/false,
10322                                          /*friend_p=*/false);
10323       /* Parse the attributes.  */
10324       attributes = cp_parser_attributes_opt (parser);
10325       /* Parse the asm-specification.  */
10326       asm_specification = cp_parser_asm_specification_opt (parser);
10327       /* If the next token is not an `=' or '{', then we might still be
10328          looking at an expression.  For example:
10329
10330            if (A(a).x)
10331
10332          looks like a decl-specifier-seq and a declarator -- but then
10333          there is no `=', so this is an expression.  */
10334       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10335           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10336         cp_parser_simulate_error (parser);
10337         
10338       /* If we did see an `=' or '{', then we are looking at a declaration
10339          for sure.  */
10340       if (cp_parser_parse_definitely (parser))
10341         {
10342           tree pushed_scope;
10343           bool non_constant_p;
10344           bool flags = LOOKUP_ONLYCONVERTING;
10345
10346           /* Create the declaration.  */
10347           decl = start_decl (declarator, &type_specifiers,
10348                              /*initialized_p=*/true,
10349                              attributes, /*prefix_attributes=*/NULL_TREE,
10350                              &pushed_scope);
10351
10352           /* Parse the initializer.  */
10353           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10354             {
10355               initializer = cp_parser_braced_list (parser, &non_constant_p);
10356               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10357               flags = 0;
10358             }
10359           else
10360             {
10361               /* Consume the `='.  */
10362               cp_parser_require (parser, CPP_EQ, RT_EQ);
10363               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10364             }
10365           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10366             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10367
10368           /* Process the initializer.  */
10369           cp_finish_decl (decl,
10370                           initializer, !non_constant_p,
10371                           asm_specification,
10372                           flags);
10373
10374           if (pushed_scope)
10375             pop_scope (pushed_scope);
10376
10377           return convert_from_reference (decl);
10378         }
10379     }
10380   /* If we didn't even get past the declarator successfully, we are
10381      definitely not looking at a declaration.  */
10382   else
10383     cp_parser_abort_tentative_parse (parser);
10384
10385   /* Otherwise, we are looking at an expression.  */
10386   return cp_parser_expression (parser);
10387 }
10388
10389 /* Parses a for-statement or range-for-statement until the closing ')',
10390    not included. */
10391
10392 static tree
10393 cp_parser_for (cp_parser *parser, bool ivdep)
10394 {
10395   tree init, scope, decl;
10396   bool is_range_for;
10397
10398   /* Begin the for-statement.  */
10399   scope = begin_for_scope (&init);
10400
10401   /* Parse the initialization.  */
10402   is_range_for = cp_parser_for_init_statement (parser, &decl);
10403
10404   if (is_range_for)
10405     return cp_parser_range_for (parser, scope, init, decl, ivdep);
10406   else
10407     return cp_parser_c_for (parser, scope, init, ivdep);
10408 }
10409
10410 static tree
10411 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10412 {
10413   /* Normal for loop */
10414   tree condition = NULL_TREE;
10415   tree expression = NULL_TREE;
10416   tree stmt;
10417
10418   stmt = begin_for_stmt (scope, init);
10419   /* The for-init-statement has already been parsed in
10420      cp_parser_for_init_statement, so no work is needed here.  */
10421   finish_for_init_stmt (stmt);
10422
10423   /* If there's a condition, process it.  */
10424   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10425     condition = cp_parser_condition (parser);
10426   else if (ivdep)
10427     {
10428       cp_parser_error (parser, "missing loop condition in loop with "
10429                        "%<GCC ivdep%> pragma");
10430       condition = error_mark_node;
10431     }
10432   finish_for_cond (condition, stmt, ivdep);
10433   /* Look for the `;'.  */
10434   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10435
10436   /* If there's an expression, process it.  */
10437   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10438     expression = cp_parser_expression (parser);
10439   finish_for_expr (expression, stmt);
10440
10441   return stmt;
10442 }
10443
10444 /* Tries to parse a range-based for-statement:
10445
10446   range-based-for:
10447     decl-specifier-seq declarator : expression
10448
10449   The decl-specifier-seq declarator and the `:' are already parsed by
10450   cp_parser_for_init_statement. If processing_template_decl it returns a
10451   newly created RANGE_FOR_STMT; if not, it is converted to a
10452   regular FOR_STMT.  */
10453
10454 static tree
10455 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10456                      bool ivdep)
10457 {
10458   tree stmt, range_expr;
10459
10460   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10461     {
10462       bool expr_non_constant_p;
10463       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10464     }
10465   else
10466     range_expr = cp_parser_expression (parser);
10467
10468   /* If in template, STMT is converted to a normal for-statement
10469      at instantiation. If not, it is done just ahead. */
10470   if (processing_template_decl)
10471     {
10472       if (check_for_bare_parameter_packs (range_expr))
10473         range_expr = error_mark_node;
10474       stmt = begin_range_for_stmt (scope, init);
10475       if (ivdep)
10476         RANGE_FOR_IVDEP (stmt) = 1;
10477       finish_range_for_decl (stmt, range_decl, range_expr);
10478       if (!type_dependent_expression_p (range_expr)
10479           /* do_auto_deduction doesn't mess with template init-lists.  */
10480           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10481         do_range_for_auto_deduction (range_decl, range_expr);
10482     }
10483   else
10484     {
10485       stmt = begin_for_stmt (scope, init);
10486       stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10487     }
10488   return stmt;
10489 }
10490
10491 /* Subroutine of cp_convert_range_for: given the initializer expression,
10492    builds up the range temporary.  */
10493
10494 static tree
10495 build_range_temp (tree range_expr)
10496 {
10497   tree range_type, range_temp;
10498
10499   /* Find out the type deduced by the declaration
10500      `auto &&__range = range_expr'.  */
10501   range_type = cp_build_reference_type (make_auto (), true);
10502   range_type = do_auto_deduction (range_type, range_expr,
10503                                   type_uses_auto (range_type));
10504
10505   /* Create the __range variable.  */
10506   range_temp = build_decl (input_location, VAR_DECL,
10507                            get_identifier ("__for_range"), range_type);
10508   TREE_USED (range_temp) = 1;
10509   DECL_ARTIFICIAL (range_temp) = 1;
10510
10511   return range_temp;
10512 }
10513
10514 /* Used by cp_parser_range_for in template context: we aren't going to
10515    do a full conversion yet, but we still need to resolve auto in the
10516    type of the for-range-declaration if present.  This is basically
10517    a shortcut version of cp_convert_range_for.  */
10518
10519 static void
10520 do_range_for_auto_deduction (tree decl, tree range_expr)
10521 {
10522   tree auto_node = type_uses_auto (TREE_TYPE (decl));
10523   if (auto_node)
10524     {
10525       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10526       range_temp = convert_from_reference (build_range_temp (range_expr));
10527       iter_type = (cp_parser_perform_range_for_lookup
10528                    (range_temp, &begin_dummy, &end_dummy));
10529       if (iter_type)
10530         {
10531           iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10532                                   iter_type);
10533           iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10534                                             tf_warning_or_error);
10535           TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10536                                                 iter_decl, auto_node);
10537         }
10538     }
10539 }
10540
10541 /* Converts a range-based for-statement into a normal
10542    for-statement, as per the definition.
10543
10544       for (RANGE_DECL : RANGE_EXPR)
10545         BLOCK
10546
10547    should be equivalent to:
10548
10549       {
10550         auto &&__range = RANGE_EXPR;
10551         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10552               __begin != __end;
10553               ++__begin)
10554           {
10555               RANGE_DECL = *__begin;
10556               BLOCK
10557           }
10558       }
10559
10560    If RANGE_EXPR is an array:
10561         BEGIN_EXPR = __range
10562         END_EXPR = __range + ARRAY_SIZE(__range)
10563    Else if RANGE_EXPR has a member 'begin' or 'end':
10564         BEGIN_EXPR = __range.begin()
10565         END_EXPR = __range.end()
10566    Else:
10567         BEGIN_EXPR = begin(__range)
10568         END_EXPR = end(__range);
10569
10570    If __range has a member 'begin' but not 'end', or vice versa, we must
10571    still use the second alternative (it will surely fail, however).
10572    When calling begin()/end() in the third alternative we must use
10573    argument dependent lookup, but always considering 'std' as an associated
10574    namespace.  */
10575
10576 tree
10577 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10578                       bool ivdep)
10579 {
10580   tree begin, end;
10581   tree iter_type, begin_expr, end_expr;
10582   tree condition, expression;
10583
10584   if (range_decl == error_mark_node || range_expr == error_mark_node)
10585     /* If an error happened previously do nothing or else a lot of
10586        unhelpful errors would be issued.  */
10587     begin_expr = end_expr = iter_type = error_mark_node;
10588   else
10589     {
10590       tree range_temp;
10591
10592       if (TREE_CODE (range_expr) == VAR_DECL
10593           && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10594         /* Can't bind a reference to an array of runtime bound.  */
10595         range_temp = range_expr;
10596       else
10597         {
10598           range_temp = build_range_temp (range_expr);
10599           pushdecl (range_temp);
10600           cp_finish_decl (range_temp, range_expr,
10601                           /*is_constant_init*/false, NULL_TREE,
10602                           LOOKUP_ONLYCONVERTING);
10603           range_temp = convert_from_reference (range_temp);
10604         }
10605       iter_type = cp_parser_perform_range_for_lookup (range_temp,
10606                                                       &begin_expr, &end_expr);
10607     }
10608
10609   /* The new for initialization statement.  */
10610   begin = build_decl (input_location, VAR_DECL,
10611                       get_identifier ("__for_begin"), iter_type);
10612   TREE_USED (begin) = 1;
10613   DECL_ARTIFICIAL (begin) = 1;
10614   pushdecl (begin);
10615   cp_finish_decl (begin, begin_expr,
10616                   /*is_constant_init*/false, NULL_TREE,
10617                   LOOKUP_ONLYCONVERTING);
10618
10619   end = build_decl (input_location, VAR_DECL,
10620                     get_identifier ("__for_end"), iter_type);
10621   TREE_USED (end) = 1;
10622   DECL_ARTIFICIAL (end) = 1;
10623   pushdecl (end);
10624   cp_finish_decl (end, end_expr,
10625                   /*is_constant_init*/false, NULL_TREE,
10626                   LOOKUP_ONLYCONVERTING);
10627
10628   finish_for_init_stmt (statement);
10629
10630   /* The new for condition.  */
10631   condition = build_x_binary_op (input_location, NE_EXPR,
10632                                  begin, ERROR_MARK,
10633                                  end, ERROR_MARK,
10634                                  NULL, tf_warning_or_error);
10635   finish_for_cond (condition, statement, ivdep);
10636
10637   /* The new increment expression.  */
10638   expression = finish_unary_op_expr (input_location,
10639                                      PREINCREMENT_EXPR, begin,
10640                                      tf_warning_or_error);
10641   finish_for_expr (expression, statement);
10642
10643   /* The declaration is initialized with *__begin inside the loop body.  */
10644   cp_finish_decl (range_decl,
10645                   build_x_indirect_ref (input_location, begin, RO_NULL,
10646                                         tf_warning_or_error),
10647                   /*is_constant_init*/false, NULL_TREE,
10648                   LOOKUP_ONLYCONVERTING);
10649
10650   return statement;
10651 }
10652
10653 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10654    We need to solve both at the same time because the method used
10655    depends on the existence of members begin or end.
10656    Returns the type deduced for the iterator expression.  */
10657
10658 static tree
10659 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10660 {
10661   if (error_operand_p (range))
10662     {
10663       *begin = *end = error_mark_node;
10664       return error_mark_node;
10665     }
10666
10667   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10668     {
10669       error ("range-based %<for%> expression of type %qT "
10670              "has incomplete type", TREE_TYPE (range));
10671       *begin = *end = error_mark_node;
10672       return error_mark_node;
10673     }
10674   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10675     {
10676       /* If RANGE is an array, we will use pointer arithmetic.  */
10677       *begin = range;
10678       *end = build_binary_op (input_location, PLUS_EXPR,
10679                               range,
10680                               array_type_nelts_top (TREE_TYPE (range)),
10681                               0);
10682       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10683     }
10684   else
10685     {
10686       /* If it is not an array, we must do a bit of magic.  */
10687       tree id_begin, id_end;
10688       tree member_begin, member_end;
10689
10690       *begin = *end = error_mark_node;
10691
10692       id_begin = get_identifier ("begin");
10693       id_end = get_identifier ("end");
10694       member_begin = lookup_member (TREE_TYPE (range), id_begin,
10695                                     /*protect=*/2, /*want_type=*/false,
10696                                     tf_warning_or_error);
10697       member_end = lookup_member (TREE_TYPE (range), id_end,
10698                                   /*protect=*/2, /*want_type=*/false,
10699                                   tf_warning_or_error);
10700
10701       if (member_begin != NULL_TREE || member_end != NULL_TREE)
10702         {
10703           /* Use the member functions.  */
10704           if (member_begin != NULL_TREE)
10705             *begin = cp_parser_range_for_member_function (range, id_begin);
10706           else
10707             error ("range-based %<for%> expression of type %qT has an "
10708                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10709
10710           if (member_end != NULL_TREE)
10711             *end = cp_parser_range_for_member_function (range, id_end);
10712           else
10713             error ("range-based %<for%> expression of type %qT has a "
10714                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10715         }
10716       else
10717         {
10718           /* Use global functions with ADL.  */
10719           vec<tree, va_gc> *vec;
10720           vec = make_tree_vector ();
10721
10722           vec_safe_push (vec, range);
10723
10724           member_begin = perform_koenig_lookup (id_begin, vec,
10725                                                 tf_warning_or_error);
10726           *begin = finish_call_expr (member_begin, &vec, false, true,
10727                                      tf_warning_or_error);
10728           member_end = perform_koenig_lookup (id_end, vec,
10729                                               tf_warning_or_error);
10730           *end = finish_call_expr (member_end, &vec, false, true,
10731                                    tf_warning_or_error);
10732
10733           release_tree_vector (vec);
10734         }
10735
10736       /* Last common checks.  */
10737       if (*begin == error_mark_node || *end == error_mark_node)
10738         {
10739           /* If one of the expressions is an error do no more checks.  */
10740           *begin = *end = error_mark_node;
10741           return error_mark_node;
10742         }
10743       else if (type_dependent_expression_p (*begin)
10744                || type_dependent_expression_p (*end))
10745         /* Can happen, when, eg, in a template context, Koenig lookup
10746            can't resolve begin/end (c++/58503).  */
10747         return NULL_TREE;
10748       else
10749         {
10750           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10751           /* The unqualified type of the __begin and __end temporaries should
10752              be the same, as required by the multiple auto declaration.  */
10753           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10754             error ("inconsistent begin/end types in range-based %<for%> "
10755                    "statement: %qT and %qT",
10756                    TREE_TYPE (*begin), TREE_TYPE (*end));
10757           return iter_type;
10758         }
10759     }
10760 }
10761
10762 /* Helper function for cp_parser_perform_range_for_lookup.
10763    Builds a tree for RANGE.IDENTIFIER().  */
10764
10765 static tree
10766 cp_parser_range_for_member_function (tree range, tree identifier)
10767 {
10768   tree member, res;
10769   vec<tree, va_gc> *vec;
10770
10771   member = finish_class_member_access_expr (range, identifier,
10772                                             false, tf_warning_or_error);
10773   if (member == error_mark_node)
10774     return error_mark_node;
10775
10776   vec = make_tree_vector ();
10777   res = finish_call_expr (member, &vec,
10778                           /*disallow_virtual=*/false,
10779                           /*koenig_p=*/false,
10780                           tf_warning_or_error);
10781   release_tree_vector (vec);
10782   return res;
10783 }
10784
10785 /* Parse an iteration-statement.
10786
10787    iteration-statement:
10788      while ( condition ) statement
10789      do statement while ( expression ) ;
10790      for ( for-init-statement condition [opt] ; expression [opt] )
10791        statement
10792
10793    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
10794
10795 static tree
10796 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10797 {
10798   cp_token *token;
10799   enum rid keyword;
10800   tree statement;
10801   unsigned char in_statement;
10802
10803   /* Peek at the next token.  */
10804   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10805   if (!token)
10806     return error_mark_node;
10807
10808   /* Remember whether or not we are already within an iteration
10809      statement.  */
10810   in_statement = parser->in_statement;
10811
10812   /* See what kind of keyword it is.  */
10813   keyword = token->keyword;
10814   switch (keyword)
10815     {
10816     case RID_WHILE:
10817       {
10818         tree condition;
10819
10820         /* Begin the while-statement.  */
10821         statement = begin_while_stmt ();
10822         /* Look for the `('.  */
10823         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10824         /* Parse the condition.  */
10825         condition = cp_parser_condition (parser);
10826         finish_while_stmt_cond (condition, statement, ivdep);
10827         /* Look for the `)'.  */
10828         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10829         /* Parse the dependent statement.  */
10830         parser->in_statement = IN_ITERATION_STMT;
10831         cp_parser_already_scoped_statement (parser);
10832         parser->in_statement = in_statement;
10833         /* We're done with the while-statement.  */
10834         finish_while_stmt (statement);
10835       }
10836       break;
10837
10838     case RID_DO:
10839       {
10840         tree expression;
10841
10842         /* Begin the do-statement.  */
10843         statement = begin_do_stmt ();
10844         /* Parse the body of the do-statement.  */
10845         parser->in_statement = IN_ITERATION_STMT;
10846         cp_parser_implicitly_scoped_statement (parser, NULL);
10847         parser->in_statement = in_statement;
10848         finish_do_body (statement);
10849         /* Look for the `while' keyword.  */
10850         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10851         /* Look for the `('.  */
10852         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10853         /* Parse the expression.  */
10854         expression = cp_parser_expression (parser);
10855         /* We're done with the do-statement.  */
10856         finish_do_stmt (expression, statement, ivdep);
10857         /* Look for the `)'.  */
10858         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10859         /* Look for the `;'.  */
10860         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10861       }
10862       break;
10863
10864     case RID_FOR:
10865       {
10866         /* Look for the `('.  */
10867         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10868
10869         statement = cp_parser_for (parser, ivdep);
10870
10871         /* Look for the `)'.  */
10872         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10873
10874         /* Parse the body of the for-statement.  */
10875         parser->in_statement = IN_ITERATION_STMT;
10876         cp_parser_already_scoped_statement (parser);
10877         parser->in_statement = in_statement;
10878
10879         /* We're done with the for-statement.  */
10880         finish_for_stmt (statement);
10881       }
10882       break;
10883
10884     default:
10885       cp_parser_error (parser, "expected iteration-statement");
10886       statement = error_mark_node;
10887       break;
10888     }
10889
10890   return statement;
10891 }
10892
10893 /* Parse a for-init-statement or the declarator of a range-based-for.
10894    Returns true if a range-based-for declaration is seen.
10895
10896    for-init-statement:
10897      expression-statement
10898      simple-declaration  */
10899
10900 static bool
10901 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10902 {
10903   /* If the next token is a `;', then we have an empty
10904      expression-statement.  Grammatically, this is also a
10905      simple-declaration, but an invalid one, because it does not
10906      declare anything.  Therefore, if we did not handle this case
10907      specially, we would issue an error message about an invalid
10908      declaration.  */
10909   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10910     {
10911       bool is_range_for = false;
10912       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10913
10914       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10915           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10916         {
10917           /* N3994 -- for (id : init) ... */
10918           if (cxx_dialect < cxx1z)
10919             pedwarn (input_location, 0, "range-based for loop without a "
10920                      "type-specifier only available with "
10921                      "-std=c++1z or -std=gnu++1z");
10922           tree name = cp_parser_identifier (parser);
10923           tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10924           *decl = build_decl (input_location, VAR_DECL, name, type);
10925           pushdecl (*decl);
10926           cp_lexer_consume_token (parser->lexer);
10927           return true;
10928         }
10929
10930       /* A colon is used in range-based for.  */
10931       parser->colon_corrects_to_scope_p = false;
10932
10933       /* We're going to speculatively look for a declaration, falling back
10934          to an expression, if necessary.  */
10935       cp_parser_parse_tentatively (parser);
10936       /* Parse the declaration.  */
10937       cp_parser_simple_declaration (parser,
10938                                     /*function_definition_allowed_p=*/false,
10939                                     decl);
10940       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10941       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10942         {
10943           /* It is a range-for, consume the ':' */
10944           cp_lexer_consume_token (parser->lexer);
10945           is_range_for = true;
10946           if (cxx_dialect < cxx11)
10947             {
10948               pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10949                        "range-based %<for%> loops only available with "
10950                        "-std=c++11 or -std=gnu++11");
10951               *decl = error_mark_node;
10952             }
10953         }
10954       else
10955           /* The ';' is not consumed yet because we told
10956              cp_parser_simple_declaration not to.  */
10957           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10958
10959       if (cp_parser_parse_definitely (parser))
10960         return is_range_for;
10961       /* If the tentative parse failed, then we shall need to look for an
10962          expression-statement.  */
10963     }
10964   /* If we are here, it is an expression-statement.  */
10965   cp_parser_expression_statement (parser, NULL_TREE);
10966   return false;
10967 }
10968
10969 /* Parse a jump-statement.
10970
10971    jump-statement:
10972      break ;
10973      continue ;
10974      return expression [opt] ;
10975      return braced-init-list ;
10976      goto identifier ;
10977
10978    GNU extension:
10979
10980    jump-statement:
10981      goto * expression ;
10982
10983    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
10984
10985 static tree
10986 cp_parser_jump_statement (cp_parser* parser)
10987 {
10988   tree statement = error_mark_node;
10989   cp_token *token;
10990   enum rid keyword;
10991   unsigned char in_statement;
10992
10993   /* Peek at the next token.  */
10994   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10995   if (!token)
10996     return error_mark_node;
10997
10998   /* See what kind of keyword it is.  */
10999   keyword = token->keyword;
11000   switch (keyword)
11001     {
11002     case RID_BREAK:
11003       in_statement = parser->in_statement & ~IN_IF_STMT;      
11004       switch (in_statement)
11005         {
11006         case 0:
11007           error_at (token->location, "break statement not within loop or switch");
11008           break;
11009         default:
11010           gcc_assert ((in_statement & IN_SWITCH_STMT)
11011                       || in_statement == IN_ITERATION_STMT);
11012           statement = finish_break_stmt ();
11013           if (in_statement == IN_ITERATION_STMT)
11014             break_maybe_infinite_loop ();
11015           break;
11016         case IN_OMP_BLOCK:
11017           error_at (token->location, "invalid exit from OpenMP structured block");
11018           break;
11019         case IN_OMP_FOR:
11020           error_at (token->location, "break statement used with OpenMP for loop");
11021           break;
11022         case IN_CILK_SIMD_FOR:
11023           error_at (token->location, "break statement used with Cilk Plus for loop");
11024           break;
11025         }
11026       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11027       break;
11028
11029     case RID_CONTINUE:
11030       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11031         {
11032         case 0:
11033           error_at (token->location, "continue statement not within a loop");
11034           break;
11035         case IN_CILK_SIMD_FOR:
11036           error_at (token->location,
11037                     "continue statement within %<#pragma simd%> loop body");
11038           /* Fall through.  */
11039         case IN_ITERATION_STMT:
11040         case IN_OMP_FOR:
11041           statement = finish_continue_stmt ();
11042           break;
11043         case IN_OMP_BLOCK:
11044           error_at (token->location, "invalid exit from OpenMP structured block");
11045           break;
11046         default:
11047           gcc_unreachable ();
11048         }
11049       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11050       break;
11051
11052     case RID_RETURN:
11053       {
11054         tree expr;
11055         bool expr_non_constant_p;
11056
11057         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11058           {
11059             cp_lexer_set_source_position (parser->lexer);
11060             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11061             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11062           }
11063         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11064           expr = cp_parser_expression (parser);
11065         else
11066           /* If the next token is a `;', then there is no
11067              expression.  */
11068           expr = NULL_TREE;
11069         /* Build the return-statement.  */
11070         statement = finish_return_stmt (expr);
11071         /* Look for the final `;'.  */
11072         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11073       }
11074       break;
11075
11076     case RID_GOTO:
11077       if (parser->in_function_body
11078           && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11079         {
11080           error ("%<goto%> in %<constexpr%> function");
11081           cp_function_chain->invalid_constexpr = true;
11082         }
11083
11084       /* Create the goto-statement.  */
11085       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11086         {
11087           /* Issue a warning about this use of a GNU extension.  */
11088           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11089           /* Consume the '*' token.  */
11090           cp_lexer_consume_token (parser->lexer);
11091           /* Parse the dependent expression.  */
11092           finish_goto_stmt (cp_parser_expression (parser));
11093         }
11094       else
11095         finish_goto_stmt (cp_parser_identifier (parser));
11096       /* Look for the final `;'.  */
11097       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11098       break;
11099
11100     default:
11101       cp_parser_error (parser, "expected jump-statement");
11102       break;
11103     }
11104
11105   return statement;
11106 }
11107
11108 /* Parse a declaration-statement.
11109
11110    declaration-statement:
11111      block-declaration  */
11112
11113 static void
11114 cp_parser_declaration_statement (cp_parser* parser)
11115 {
11116   void *p;
11117
11118   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11119   p = obstack_alloc (&declarator_obstack, 0);
11120
11121  /* Parse the block-declaration.  */
11122   cp_parser_block_declaration (parser, /*statement_p=*/true);
11123
11124   /* Free any declarators allocated.  */
11125   obstack_free (&declarator_obstack, p);
11126 }
11127
11128 /* Some dependent statements (like `if (cond) statement'), are
11129    implicitly in their own scope.  In other words, if the statement is
11130    a single statement (as opposed to a compound-statement), it is
11131    none-the-less treated as if it were enclosed in braces.  Any
11132    declarations appearing in the dependent statement are out of scope
11133    after control passes that point.  This function parses a statement,
11134    but ensures that is in its own scope, even if it is not a
11135    compound-statement.
11136
11137    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11138    is a (possibly labeled) if statement which is not enclosed in
11139    braces and has an else clause.  This is used to implement
11140    -Wparentheses.
11141
11142    Returns the new statement.  */
11143
11144 static tree
11145 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11146 {
11147   tree statement;
11148
11149   if (if_p != NULL)
11150     *if_p = false;
11151
11152   /* Mark if () ; with a special NOP_EXPR.  */
11153   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11154     {
11155       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11156       cp_lexer_consume_token (parser->lexer);
11157       statement = add_stmt (build_empty_stmt (loc));
11158     }
11159   /* if a compound is opened, we simply parse the statement directly.  */
11160   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11161     statement = cp_parser_compound_statement (parser, NULL, false, false);
11162   /* If the token is not a `{', then we must take special action.  */
11163   else
11164     {
11165       /* Create a compound-statement.  */
11166       statement = begin_compound_stmt (0);
11167       /* Parse the dependent-statement.  */
11168       cp_parser_statement (parser, NULL_TREE, false, if_p);
11169       /* Finish the dummy compound-statement.  */
11170       finish_compound_stmt (statement);
11171     }
11172
11173   /* Return the statement.  */
11174   return statement;
11175 }
11176
11177 /* For some dependent statements (like `while (cond) statement'), we
11178    have already created a scope.  Therefore, even if the dependent
11179    statement is a compound-statement, we do not want to create another
11180    scope.  */
11181
11182 static void
11183 cp_parser_already_scoped_statement (cp_parser* parser)
11184 {
11185   /* If the token is a `{', then we must take special action.  */
11186   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11187     cp_parser_statement (parser, NULL_TREE, false, NULL);
11188   else
11189     {
11190       /* Avoid calling cp_parser_compound_statement, so that we
11191          don't create a new scope.  Do everything else by hand.  */
11192       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11193       /* If the next keyword is `__label__' we have a label declaration.  */
11194       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11195         cp_parser_label_declaration (parser);
11196       /* Parse an (optional) statement-seq.  */
11197       cp_parser_statement_seq_opt (parser, NULL_TREE);
11198       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11199     }
11200 }
11201
11202 /* Declarations [gram.dcl.dcl] */
11203
11204 /* Parse an optional declaration-sequence.
11205
11206    declaration-seq:
11207      declaration
11208      declaration-seq declaration  */
11209
11210 static void
11211 cp_parser_declaration_seq_opt (cp_parser* parser)
11212 {
11213   while (true)
11214     {
11215       cp_token *token;
11216
11217       token = cp_lexer_peek_token (parser->lexer);
11218
11219       if (token->type == CPP_CLOSE_BRACE
11220           || token->type == CPP_EOF
11221           || token->type == CPP_PRAGMA_EOL)
11222         break;
11223
11224       if (token->type == CPP_SEMICOLON)
11225         {
11226           /* A declaration consisting of a single semicolon is
11227              invalid.  Allow it unless we're being pedantic.  */
11228           cp_lexer_consume_token (parser->lexer);
11229           if (!in_system_header_at (input_location))
11230             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11231           continue;
11232         }
11233
11234       /* If we're entering or exiting a region that's implicitly
11235          extern "C", modify the lang context appropriately.  */
11236       if (!parser->implicit_extern_c && token->implicit_extern_c)
11237         {
11238           push_lang_context (lang_name_c);
11239           parser->implicit_extern_c = true;
11240         }
11241       else if (parser->implicit_extern_c && !token->implicit_extern_c)
11242         {
11243           pop_lang_context ();
11244           parser->implicit_extern_c = false;
11245         }
11246
11247       if (token->type == CPP_PRAGMA)
11248         {
11249           /* A top-level declaration can consist solely of a #pragma.
11250              A nested declaration cannot, so this is done here and not
11251              in cp_parser_declaration.  (A #pragma at block scope is
11252              handled in cp_parser_statement.)  */
11253           cp_parser_pragma (parser, pragma_external);
11254           continue;
11255         }
11256
11257       /* Parse the declaration itself.  */
11258       cp_parser_declaration (parser);
11259     }
11260 }
11261
11262 /* Parse a declaration.
11263
11264    declaration:
11265      block-declaration
11266      function-definition
11267      template-declaration
11268      explicit-instantiation
11269      explicit-specialization
11270      linkage-specification
11271      namespace-definition
11272
11273    GNU extension:
11274
11275    declaration:
11276       __extension__ declaration */
11277
11278 static void
11279 cp_parser_declaration (cp_parser* parser)
11280 {
11281   cp_token token1;
11282   cp_token token2;
11283   int saved_pedantic;
11284   void *p;
11285   tree attributes = NULL_TREE;
11286
11287   /* Check for the `__extension__' keyword.  */
11288   if (cp_parser_extension_opt (parser, &saved_pedantic))
11289     {
11290       /* Parse the qualified declaration.  */
11291       cp_parser_declaration (parser);
11292       /* Restore the PEDANTIC flag.  */
11293       pedantic = saved_pedantic;
11294
11295       return;
11296     }
11297
11298   /* Try to figure out what kind of declaration is present.  */
11299   token1 = *cp_lexer_peek_token (parser->lexer);
11300
11301   if (token1.type != CPP_EOF)
11302     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11303   else
11304     {
11305       token2.type = CPP_EOF;
11306       token2.keyword = RID_MAX;
11307     }
11308
11309   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11310   p = obstack_alloc (&declarator_obstack, 0);
11311
11312   /* If the next token is `extern' and the following token is a string
11313      literal, then we have a linkage specification.  */
11314   if (token1.keyword == RID_EXTERN
11315       && cp_parser_is_pure_string_literal (&token2))
11316     cp_parser_linkage_specification (parser);
11317   /* If the next token is `template', then we have either a template
11318      declaration, an explicit instantiation, or an explicit
11319      specialization.  */
11320   else if (token1.keyword == RID_TEMPLATE)
11321     {
11322       /* `template <>' indicates a template specialization.  */
11323       if (token2.type == CPP_LESS
11324           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11325         cp_parser_explicit_specialization (parser);
11326       /* `template <' indicates a template declaration.  */
11327       else if (token2.type == CPP_LESS)
11328         cp_parser_template_declaration (parser, /*member_p=*/false);
11329       /* Anything else must be an explicit instantiation.  */
11330       else
11331         cp_parser_explicit_instantiation (parser);
11332     }
11333   /* If the next token is `export', then we have a template
11334      declaration.  */
11335   else if (token1.keyword == RID_EXPORT)
11336     cp_parser_template_declaration (parser, /*member_p=*/false);
11337   /* If the next token is `extern', 'static' or 'inline' and the one
11338      after that is `template', we have a GNU extended explicit
11339      instantiation directive.  */
11340   else if (cp_parser_allow_gnu_extensions_p (parser)
11341            && (token1.keyword == RID_EXTERN
11342                || token1.keyword == RID_STATIC
11343                || token1.keyword == RID_INLINE)
11344            && token2.keyword == RID_TEMPLATE)
11345     cp_parser_explicit_instantiation (parser);
11346   /* If the next token is `namespace', check for a named or unnamed
11347      namespace definition.  */
11348   else if (token1.keyword == RID_NAMESPACE
11349            && (/* A named namespace definition.  */
11350                (token2.type == CPP_NAME
11351                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11352                     != CPP_EQ))
11353                /* An unnamed namespace definition.  */
11354                || token2.type == CPP_OPEN_BRACE
11355                || token2.keyword == RID_ATTRIBUTE))
11356     cp_parser_namespace_definition (parser);
11357   /* An inline (associated) namespace definition.  */
11358   else if (token1.keyword == RID_INLINE
11359            && token2.keyword == RID_NAMESPACE)
11360     cp_parser_namespace_definition (parser);
11361   /* Objective-C++ declaration/definition.  */
11362   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11363     cp_parser_objc_declaration (parser, NULL_TREE);
11364   else if (c_dialect_objc ()
11365            && token1.keyword == RID_ATTRIBUTE
11366            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11367     cp_parser_objc_declaration (parser, attributes);
11368   /* We must have either a block declaration or a function
11369      definition.  */
11370   else
11371     /* Try to parse a block-declaration, or a function-definition.  */
11372     cp_parser_block_declaration (parser, /*statement_p=*/false);
11373
11374   /* Free any declarators allocated.  */
11375   obstack_free (&declarator_obstack, p);
11376 }
11377
11378 /* Parse a block-declaration.
11379
11380    block-declaration:
11381      simple-declaration
11382      asm-definition
11383      namespace-alias-definition
11384      using-declaration
11385      using-directive
11386
11387    GNU Extension:
11388
11389    block-declaration:
11390      __extension__ block-declaration
11391
11392    C++0x Extension:
11393
11394    block-declaration:
11395      static_assert-declaration
11396
11397    If STATEMENT_P is TRUE, then this block-declaration is occurring as
11398    part of a declaration-statement.  */
11399
11400 static void
11401 cp_parser_block_declaration (cp_parser *parser,
11402                              bool      statement_p)
11403 {
11404   cp_token *token1;
11405   int saved_pedantic;
11406
11407   /* Check for the `__extension__' keyword.  */
11408   if (cp_parser_extension_opt (parser, &saved_pedantic))
11409     {
11410       /* Parse the qualified declaration.  */
11411       cp_parser_block_declaration (parser, statement_p);
11412       /* Restore the PEDANTIC flag.  */
11413       pedantic = saved_pedantic;
11414
11415       return;
11416     }
11417
11418   /* Peek at the next token to figure out which kind of declaration is
11419      present.  */
11420   token1 = cp_lexer_peek_token (parser->lexer);
11421
11422   /* If the next keyword is `asm', we have an asm-definition.  */
11423   if (token1->keyword == RID_ASM)
11424     {
11425       if (statement_p)
11426         cp_parser_commit_to_tentative_parse (parser);
11427       cp_parser_asm_definition (parser);
11428     }
11429   /* If the next keyword is `namespace', we have a
11430      namespace-alias-definition.  */
11431   else if (token1->keyword == RID_NAMESPACE)
11432     cp_parser_namespace_alias_definition (parser);
11433   /* If the next keyword is `using', we have a
11434      using-declaration, a using-directive, or an alias-declaration.  */
11435   else if (token1->keyword == RID_USING)
11436     {
11437       cp_token *token2;
11438
11439       if (statement_p)
11440         cp_parser_commit_to_tentative_parse (parser);
11441       /* If the token after `using' is `namespace', then we have a
11442          using-directive.  */
11443       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11444       if (token2->keyword == RID_NAMESPACE)
11445         cp_parser_using_directive (parser);
11446       /* If the second token after 'using' is '=', then we have an
11447          alias-declaration.  */
11448       else if (cxx_dialect >= cxx11
11449                && token2->type == CPP_NAME
11450                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11451                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11452         cp_parser_alias_declaration (parser);
11453       /* Otherwise, it's a using-declaration.  */
11454       else
11455         cp_parser_using_declaration (parser,
11456                                      /*access_declaration_p=*/false);
11457     }
11458   /* If the next keyword is `__label__' we have a misplaced label
11459      declaration.  */
11460   else if (token1->keyword == RID_LABEL)
11461     {
11462       cp_lexer_consume_token (parser->lexer);
11463       error_at (token1->location, "%<__label__%> not at the beginning of a block");
11464       cp_parser_skip_to_end_of_statement (parser);
11465       /* If the next token is now a `;', consume it.  */
11466       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11467         cp_lexer_consume_token (parser->lexer);
11468     }
11469   /* If the next token is `static_assert' we have a static assertion.  */
11470   else if (token1->keyword == RID_STATIC_ASSERT)
11471     cp_parser_static_assert (parser, /*member_p=*/false);
11472   /* Anything else must be a simple-declaration.  */
11473   else
11474     cp_parser_simple_declaration (parser, !statement_p,
11475                                   /*maybe_range_for_decl*/NULL);
11476 }
11477
11478 /* Parse a simple-declaration.
11479
11480    simple-declaration:
11481      decl-specifier-seq [opt] init-declarator-list [opt] ;
11482
11483    init-declarator-list:
11484      init-declarator
11485      init-declarator-list , init-declarator
11486
11487    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11488    function-definition as a simple-declaration.
11489
11490    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11491    parsed declaration if it is an uninitialized single declarator not followed
11492    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11493    if present, will not be consumed.  */
11494
11495 static void
11496 cp_parser_simple_declaration (cp_parser* parser,
11497                               bool function_definition_allowed_p,
11498                               tree *maybe_range_for_decl)
11499 {
11500   cp_decl_specifier_seq decl_specifiers;
11501   int declares_class_or_enum;
11502   bool saw_declarator;
11503   location_t comma_loc = UNKNOWN_LOCATION;
11504   location_t init_loc = UNKNOWN_LOCATION;
11505
11506   if (maybe_range_for_decl)
11507     *maybe_range_for_decl = NULL_TREE;
11508
11509   /* Defer access checks until we know what is being declared; the
11510      checks for names appearing in the decl-specifier-seq should be
11511      done as if we were in the scope of the thing being declared.  */
11512   push_deferring_access_checks (dk_deferred);
11513
11514   /* Parse the decl-specifier-seq.  We have to keep track of whether
11515      or not the decl-specifier-seq declares a named class or
11516      enumeration type, since that is the only case in which the
11517      init-declarator-list is allowed to be empty.
11518
11519      [dcl.dcl]
11520
11521      In a simple-declaration, the optional init-declarator-list can be
11522      omitted only when declaring a class or enumeration, that is when
11523      the decl-specifier-seq contains either a class-specifier, an
11524      elaborated-type-specifier, or an enum-specifier.  */
11525   cp_parser_decl_specifier_seq (parser,
11526                                 CP_PARSER_FLAGS_OPTIONAL,
11527                                 &decl_specifiers,
11528                                 &declares_class_or_enum);
11529   /* We no longer need to defer access checks.  */
11530   stop_deferring_access_checks ();
11531
11532   /* In a block scope, a valid declaration must always have a
11533      decl-specifier-seq.  By not trying to parse declarators, we can
11534      resolve the declaration/expression ambiguity more quickly.  */
11535   if (!function_definition_allowed_p
11536       && !decl_specifiers.any_specifiers_p)
11537     {
11538       cp_parser_error (parser, "expected declaration");
11539       goto done;
11540     }
11541
11542   /* If the next two tokens are both identifiers, the code is
11543      erroneous. The usual cause of this situation is code like:
11544
11545        T t;
11546
11547      where "T" should name a type -- but does not.  */
11548   if (!decl_specifiers.any_type_specifiers_p
11549       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11550     {
11551       /* If parsing tentatively, we should commit; we really are
11552          looking at a declaration.  */
11553       cp_parser_commit_to_tentative_parse (parser);
11554       /* Give up.  */
11555       goto done;
11556     }
11557
11558   /* If we have seen at least one decl-specifier, and the next token
11559      is not a parenthesis, then we must be looking at a declaration.
11560      (After "int (" we might be looking at a functional cast.)  */
11561   if (decl_specifiers.any_specifiers_p
11562       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11563       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11564       && !cp_parser_error_occurred (parser))
11565     cp_parser_commit_to_tentative_parse (parser);
11566
11567   /* Keep going until we hit the `;' at the end of the simple
11568      declaration.  */
11569   saw_declarator = false;
11570   while (cp_lexer_next_token_is_not (parser->lexer,
11571                                      CPP_SEMICOLON))
11572     {
11573       cp_token *token;
11574       bool function_definition_p;
11575       tree decl;
11576
11577       if (saw_declarator)
11578         {
11579           /* If we are processing next declarator, comma is expected */
11580           token = cp_lexer_peek_token (parser->lexer);
11581           gcc_assert (token->type == CPP_COMMA);
11582           cp_lexer_consume_token (parser->lexer);
11583           if (maybe_range_for_decl)
11584             {
11585               *maybe_range_for_decl = error_mark_node;
11586               if (comma_loc == UNKNOWN_LOCATION)
11587                 comma_loc = token->location;
11588             }
11589         }
11590       else
11591         saw_declarator = true;
11592
11593       /* Parse the init-declarator.  */
11594       decl = cp_parser_init_declarator (parser, &decl_specifiers,
11595                                         /*checks=*/NULL,
11596                                         function_definition_allowed_p,
11597                                         /*member_p=*/false,
11598                                         declares_class_or_enum,
11599                                         &function_definition_p,
11600                                         maybe_range_for_decl,
11601                                         &init_loc);
11602       /* If an error occurred while parsing tentatively, exit quickly.
11603          (That usually happens when in the body of a function; each
11604          statement is treated as a declaration-statement until proven
11605          otherwise.)  */
11606       if (cp_parser_error_occurred (parser))
11607         goto done;
11608       /* Handle function definitions specially.  */
11609       if (function_definition_p)
11610         {
11611           /* If the next token is a `,', then we are probably
11612              processing something like:
11613
11614                void f() {}, *p;
11615
11616              which is erroneous.  */
11617           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11618             {
11619               cp_token *token = cp_lexer_peek_token (parser->lexer);
11620               error_at (token->location,
11621                         "mixing"
11622                         " declarations and function-definitions is forbidden");
11623             }
11624           /* Otherwise, we're done with the list of declarators.  */
11625           else
11626             {
11627               pop_deferring_access_checks ();
11628               return;
11629             }
11630         }
11631       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11632         *maybe_range_for_decl = decl;
11633       /* The next token should be either a `,' or a `;'.  */
11634       token = cp_lexer_peek_token (parser->lexer);
11635       /* If it's a `,', there are more declarators to come.  */
11636       if (token->type == CPP_COMMA)
11637         /* will be consumed next time around */;
11638       /* If it's a `;', we are done.  */
11639       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11640         break;
11641       /* Anything else is an error.  */
11642       else
11643         {
11644           /* If we have already issued an error message we don't need
11645              to issue another one.  */
11646           if (decl != error_mark_node
11647               || cp_parser_uncommitted_to_tentative_parse_p (parser))
11648             cp_parser_error (parser, "expected %<,%> or %<;%>");
11649           /* Skip tokens until we reach the end of the statement.  */
11650           cp_parser_skip_to_end_of_statement (parser);
11651           /* If the next token is now a `;', consume it.  */
11652           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11653             cp_lexer_consume_token (parser->lexer);
11654           goto done;
11655         }
11656       /* After the first time around, a function-definition is not
11657          allowed -- even if it was OK at first.  For example:
11658
11659            int i, f() {}
11660
11661          is not valid.  */
11662       function_definition_allowed_p = false;
11663     }
11664
11665   /* Issue an error message if no declarators are present, and the
11666      decl-specifier-seq does not itself declare a class or
11667      enumeration: [dcl.dcl]/3.  */
11668   if (!saw_declarator)
11669     {
11670       if (cp_parser_declares_only_class_p (parser))
11671         {
11672           if (!declares_class_or_enum
11673               && decl_specifiers.type
11674               && OVERLOAD_TYPE_P (decl_specifiers.type))
11675             /* Ensure an error is issued anyway when finish_decltype_type,
11676                called via cp_parser_decl_specifier_seq, returns a class or
11677                an enumeration (c++/51786).  */
11678             decl_specifiers.type = NULL_TREE;
11679           shadow_tag (&decl_specifiers);
11680         }
11681       /* Perform any deferred access checks.  */
11682       perform_deferred_access_checks (tf_warning_or_error);
11683     }
11684
11685   /* Consume the `;'.  */
11686   if (!maybe_range_for_decl)
11687     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11688   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11689     {
11690       if (init_loc != UNKNOWN_LOCATION)
11691         error_at (init_loc, "initializer in range-based %<for%> loop");
11692       if (comma_loc != UNKNOWN_LOCATION)
11693         error_at (comma_loc,
11694                   "multiple declarations in range-based %<for%> loop");
11695     }
11696
11697  done:
11698   pop_deferring_access_checks ();
11699 }
11700
11701 /* Parse a decl-specifier-seq.
11702
11703    decl-specifier-seq:
11704      decl-specifier-seq [opt] decl-specifier
11705      decl-specifier attribute-specifier-seq [opt] (C++11)
11706
11707    decl-specifier:
11708      storage-class-specifier
11709      type-specifier
11710      function-specifier
11711      friend
11712      typedef
11713
11714    GNU Extension:
11715
11716    decl-specifier:
11717      attributes
11718
11719    Set *DECL_SPECS to a representation of the decl-specifier-seq.
11720
11721    The parser flags FLAGS is used to control type-specifier parsing.
11722
11723    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11724    flags:
11725
11726      1: one of the decl-specifiers is an elaborated-type-specifier
11727         (i.e., a type declaration)
11728      2: one of the decl-specifiers is an enum-specifier or a
11729         class-specifier (i.e., a type definition)
11730
11731    */
11732
11733 static void
11734 cp_parser_decl_specifier_seq (cp_parser* parser,
11735                               cp_parser_flags flags,
11736                               cp_decl_specifier_seq *decl_specs,
11737                               int* declares_class_or_enum)
11738 {
11739   bool constructor_possible_p = !parser->in_declarator_p;
11740   bool found_decl_spec = false;
11741   cp_token *start_token = NULL;
11742   cp_decl_spec ds;
11743
11744   /* Clear DECL_SPECS.  */
11745   clear_decl_specs (decl_specs);
11746
11747   /* Assume no class or enumeration type is declared.  */
11748   *declares_class_or_enum = 0;
11749
11750   /* Keep reading specifiers until there are no more to read.  */
11751   while (true)
11752     {
11753       bool constructor_p;
11754       cp_token *token;
11755       ds = ds_last;
11756
11757       /* Peek at the next token.  */
11758       token = cp_lexer_peek_token (parser->lexer);
11759
11760       /* Save the first token of the decl spec list for error
11761          reporting.  */
11762       if (!start_token)
11763         start_token = token;
11764       /* Handle attributes.  */
11765       if (cp_next_tokens_can_be_attribute_p (parser))
11766         {
11767           /* Parse the attributes.  */
11768           tree attrs = cp_parser_attributes_opt (parser);
11769
11770           /* In a sequence of declaration specifiers, c++11 attributes
11771              appertain to the type that precede them. In that case
11772              [dcl.spec]/1 says:
11773
11774                  The attribute-specifier-seq affects the type only for
11775                  the declaration it appears in, not other declarations
11776                  involving the same type.
11777
11778              But for now let's force the user to position the
11779              attribute either at the beginning of the declaration or
11780              after the declarator-id, which would clearly mean that it
11781              applies to the declarator.  */
11782           if (cxx11_attribute_p (attrs))
11783             {
11784               if (!found_decl_spec)
11785                 /* The c++11 attribute is at the beginning of the
11786                    declaration.  It appertains to the entity being
11787                    declared.  */;
11788               else
11789                 {
11790                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11791                     {
11792                       /*  This is an attribute following a
11793                           class-specifier.  */
11794                       if (decl_specs->type_definition_p)
11795                         warn_misplaced_attr_for_class_type (token->location,
11796                                                             decl_specs->type);
11797                       attrs = NULL_TREE;
11798                     }
11799                   else
11800                     {
11801                       decl_specs->std_attributes
11802                         = chainon (decl_specs->std_attributes,
11803                                    attrs);
11804                       if (decl_specs->locations[ds_std_attribute] == 0)
11805                         decl_specs->locations[ds_std_attribute] = token->location;
11806                     }
11807                   continue;
11808                 }
11809             }
11810
11811             decl_specs->attributes
11812               = chainon (decl_specs->attributes,
11813                          attrs);
11814           if (decl_specs->locations[ds_attribute] == 0)
11815             decl_specs->locations[ds_attribute] = token->location;
11816           continue;
11817         }
11818       /* Assume we will find a decl-specifier keyword.  */
11819       found_decl_spec = true;
11820       /* If the next token is an appropriate keyword, we can simply
11821          add it to the list.  */
11822       switch (token->keyword)
11823         {
11824           /* decl-specifier:
11825                friend
11826                constexpr */
11827         case RID_FRIEND:
11828           if (!at_class_scope_p ())
11829             {
11830               error_at (token->location, "%<friend%> used outside of class");
11831               cp_lexer_purge_token (parser->lexer);
11832             }
11833           else
11834             {
11835               ds = ds_friend;
11836               /* Consume the token.  */
11837               cp_lexer_consume_token (parser->lexer);
11838             }
11839           break;
11840
11841         case RID_CONSTEXPR:
11842           ds = ds_constexpr;
11843           cp_lexer_consume_token (parser->lexer);
11844           break;
11845
11846           /* function-specifier:
11847                inline
11848                virtual
11849                explicit  */
11850         case RID_INLINE:
11851         case RID_VIRTUAL:
11852         case RID_EXPLICIT:
11853           cp_parser_function_specifier_opt (parser, decl_specs);
11854           break;
11855
11856           /* decl-specifier:
11857                typedef  */
11858         case RID_TYPEDEF:
11859           ds = ds_typedef;
11860           /* Consume the token.  */
11861           cp_lexer_consume_token (parser->lexer);
11862           /* A constructor declarator cannot appear in a typedef.  */
11863           constructor_possible_p = false;
11864           /* The "typedef" keyword can only occur in a declaration; we
11865              may as well commit at this point.  */
11866           cp_parser_commit_to_tentative_parse (parser);
11867
11868           if (decl_specs->storage_class != sc_none)
11869             decl_specs->conflicting_specifiers_p = true;
11870           break;
11871
11872           /* storage-class-specifier:
11873                auto
11874                register
11875                static
11876                extern
11877                mutable
11878
11879              GNU Extension:
11880                thread  */
11881         case RID_AUTO:
11882           if (cxx_dialect == cxx98) 
11883             {
11884               /* Consume the token.  */
11885               cp_lexer_consume_token (parser->lexer);
11886
11887               /* Complain about `auto' as a storage specifier, if
11888                  we're complaining about C++0x compatibility.  */
11889               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11890                           " changes meaning in C++11; please remove it");
11891
11892               /* Set the storage class anyway.  */
11893               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11894                                            token);
11895             }
11896           else
11897             /* C++0x auto type-specifier.  */
11898             found_decl_spec = false;
11899           break;
11900
11901         case RID_REGISTER:
11902         case RID_STATIC:
11903         case RID_EXTERN:
11904         case RID_MUTABLE:
11905           /* Consume the token.  */
11906           cp_lexer_consume_token (parser->lexer);
11907           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11908                                        token);
11909           break;
11910         case RID_THREAD:
11911           /* Consume the token.  */
11912           ds = ds_thread;
11913           cp_lexer_consume_token (parser->lexer);
11914           break;
11915
11916         default:
11917           /* We did not yet find a decl-specifier yet.  */
11918           found_decl_spec = false;
11919           break;
11920         }
11921
11922       if (found_decl_spec
11923           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11924           && token->keyword != RID_CONSTEXPR)
11925         error ("decl-specifier invalid in condition");
11926
11927       if (ds != ds_last)
11928         set_and_check_decl_spec_loc (decl_specs, ds, token);
11929
11930       /* Constructors are a special case.  The `S' in `S()' is not a
11931          decl-specifier; it is the beginning of the declarator.  */
11932       constructor_p
11933         = (!found_decl_spec
11934            && constructor_possible_p
11935            && (cp_parser_constructor_declarator_p
11936                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11937
11938       /* If we don't have a DECL_SPEC yet, then we must be looking at
11939          a type-specifier.  */
11940       if (!found_decl_spec && !constructor_p)
11941         {
11942           int decl_spec_declares_class_or_enum;
11943           bool is_cv_qualifier;
11944           tree type_spec;
11945
11946           type_spec
11947             = cp_parser_type_specifier (parser, flags,
11948                                         decl_specs,
11949                                         /*is_declaration=*/true,
11950                                         &decl_spec_declares_class_or_enum,
11951                                         &is_cv_qualifier);
11952           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11953
11954           /* If this type-specifier referenced a user-defined type
11955              (a typedef, class-name, etc.), then we can't allow any
11956              more such type-specifiers henceforth.
11957
11958              [dcl.spec]
11959
11960              The longest sequence of decl-specifiers that could
11961              possibly be a type name is taken as the
11962              decl-specifier-seq of a declaration.  The sequence shall
11963              be self-consistent as described below.
11964
11965              [dcl.type]
11966
11967              As a general rule, at most one type-specifier is allowed
11968              in the complete decl-specifier-seq of a declaration.  The
11969              only exceptions are the following:
11970
11971              -- const or volatile can be combined with any other
11972                 type-specifier.
11973
11974              -- signed or unsigned can be combined with char, long,
11975                 short, or int.
11976
11977              -- ..
11978
11979              Example:
11980
11981                typedef char* Pc;
11982                void g (const int Pc);
11983
11984              Here, Pc is *not* part of the decl-specifier seq; it's
11985              the declarator.  Therefore, once we see a type-specifier
11986              (other than a cv-qualifier), we forbid any additional
11987              user-defined types.  We *do* still allow things like `int
11988              int' to be considered a decl-specifier-seq, and issue the
11989              error message later.  */
11990           if (type_spec && !is_cv_qualifier)
11991             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11992           /* A constructor declarator cannot follow a type-specifier.  */
11993           if (type_spec)
11994             {
11995               constructor_possible_p = false;
11996               found_decl_spec = true;
11997               if (!is_cv_qualifier)
11998                 decl_specs->any_type_specifiers_p = true;
11999             }
12000         }
12001
12002       /* If we still do not have a DECL_SPEC, then there are no more
12003          decl-specifiers.  */
12004       if (!found_decl_spec)
12005         break;
12006
12007       decl_specs->any_specifiers_p = true;
12008       /* After we see one decl-specifier, further decl-specifiers are
12009          always optional.  */
12010       flags |= CP_PARSER_FLAGS_OPTIONAL;
12011     }
12012
12013   /* Don't allow a friend specifier with a class definition.  */
12014   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12015       && (*declares_class_or_enum & 2))
12016     error_at (decl_specs->locations[ds_friend],
12017               "class definition may not be declared a friend");
12018 }
12019
12020 /* Parse an (optional) storage-class-specifier.
12021
12022    storage-class-specifier:
12023      auto
12024      register
12025      static
12026      extern
12027      mutable
12028
12029    GNU Extension:
12030
12031    storage-class-specifier:
12032      thread
12033
12034    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
12035
12036 static tree
12037 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12038 {
12039   switch (cp_lexer_peek_token (parser->lexer)->keyword)
12040     {
12041     case RID_AUTO:
12042       if (cxx_dialect != cxx98)
12043         return NULL_TREE;
12044       /* Fall through for C++98.  */
12045
12046     case RID_REGISTER:
12047     case RID_STATIC:
12048     case RID_EXTERN:
12049     case RID_MUTABLE:
12050     case RID_THREAD:
12051       /* Consume the token.  */
12052       return cp_lexer_consume_token (parser->lexer)->u.value;
12053
12054     default:
12055       return NULL_TREE;
12056     }
12057 }
12058
12059 /* Parse an (optional) function-specifier.
12060
12061    function-specifier:
12062      inline
12063      virtual
12064      explicit
12065
12066    Returns an IDENTIFIER_NODE corresponding to the keyword used.
12067    Updates DECL_SPECS, if it is non-NULL.  */
12068
12069 static tree
12070 cp_parser_function_specifier_opt (cp_parser* parser,
12071                                   cp_decl_specifier_seq *decl_specs)
12072 {
12073   cp_token *token = cp_lexer_peek_token (parser->lexer);
12074   switch (token->keyword)
12075     {
12076     case RID_INLINE:
12077       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12078       break;
12079
12080     case RID_VIRTUAL:
12081       /* 14.5.2.3 [temp.mem]
12082
12083          A member function template shall not be virtual.  */
12084       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12085         error_at (token->location, "templates may not be %<virtual%>");
12086       else
12087         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12088       break;
12089
12090     case RID_EXPLICIT:
12091       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12092       break;
12093
12094     default:
12095       return NULL_TREE;
12096     }
12097
12098   /* Consume the token.  */
12099   return cp_lexer_consume_token (parser->lexer)->u.value;
12100 }
12101
12102 /* Parse a linkage-specification.
12103
12104    linkage-specification:
12105      extern string-literal { declaration-seq [opt] }
12106      extern string-literal declaration  */
12107
12108 static void
12109 cp_parser_linkage_specification (cp_parser* parser)
12110 {
12111   tree linkage;
12112
12113   /* Look for the `extern' keyword.  */
12114   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12115
12116   /* Look for the string-literal.  */
12117   linkage = cp_parser_string_literal (parser, false, false);
12118
12119   /* Transform the literal into an identifier.  If the literal is a
12120      wide-character string, or contains embedded NULs, then we can't
12121      handle it as the user wants.  */
12122   if (strlen (TREE_STRING_POINTER (linkage))
12123       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12124     {
12125       cp_parser_error (parser, "invalid linkage-specification");
12126       /* Assume C++ linkage.  */
12127       linkage = lang_name_cplusplus;
12128     }
12129   else
12130     linkage = get_identifier (TREE_STRING_POINTER (linkage));
12131
12132   /* We're now using the new linkage.  */
12133   push_lang_context (linkage);
12134
12135   /* If the next token is a `{', then we're using the first
12136      production.  */
12137   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12138     {
12139       cp_ensure_no_omp_declare_simd (parser);
12140
12141       /* Consume the `{' token.  */
12142       cp_lexer_consume_token (parser->lexer);
12143       /* Parse the declarations.  */
12144       cp_parser_declaration_seq_opt (parser);
12145       /* Look for the closing `}'.  */
12146       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12147     }
12148   /* Otherwise, there's just one declaration.  */
12149   else
12150     {
12151       bool saved_in_unbraced_linkage_specification_p;
12152
12153       saved_in_unbraced_linkage_specification_p
12154         = parser->in_unbraced_linkage_specification_p;
12155       parser->in_unbraced_linkage_specification_p = true;
12156       cp_parser_declaration (parser);
12157       parser->in_unbraced_linkage_specification_p
12158         = saved_in_unbraced_linkage_specification_p;
12159     }
12160
12161   /* We're done with the linkage-specification.  */
12162   pop_lang_context ();
12163 }
12164
12165 /* Parse a static_assert-declaration.
12166
12167    static_assert-declaration:
12168      static_assert ( constant-expression , string-literal ) ; 
12169
12170    If MEMBER_P, this static_assert is a class member.  */
12171
12172 static void 
12173 cp_parser_static_assert(cp_parser *parser, bool member_p)
12174 {
12175   tree condition;
12176   tree message;
12177   cp_token *token;
12178   location_t saved_loc;
12179   bool dummy;
12180
12181   /* Peek at the `static_assert' token so we can keep track of exactly
12182      where the static assertion started.  */
12183   token = cp_lexer_peek_token (parser->lexer);
12184   saved_loc = token->location;
12185
12186   /* Look for the `static_assert' keyword.  */
12187   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
12188                                   RT_STATIC_ASSERT))
12189     return;
12190
12191   /*  We know we are in a static assertion; commit to any tentative
12192       parse.  */
12193   if (cp_parser_parsing_tentatively (parser))
12194     cp_parser_commit_to_tentative_parse (parser);
12195
12196   /* Parse the `(' starting the static assertion condition.  */
12197   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12198
12199   /* Parse the constant-expression.  Allow a non-constant expression
12200      here in order to give better diagnostics in finish_static_assert.  */
12201   condition = 
12202     cp_parser_constant_expression (parser,
12203                                    /*allow_non_constant_p=*/true,
12204                                    /*non_constant_p=*/&dummy);
12205
12206   /* Parse the separating `,'.  */
12207   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12208
12209   /* Parse the string-literal message.  */
12210   message = cp_parser_string_literal (parser, 
12211                                       /*translate=*/false,
12212                                       /*wide_ok=*/true);
12213
12214   /* A `)' completes the static assertion.  */
12215   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12216     cp_parser_skip_to_closing_parenthesis (parser, 
12217                                            /*recovering=*/true, 
12218                                            /*or_comma=*/false,
12219                                            /*consume_paren=*/true);
12220
12221   /* A semicolon terminates the declaration.  */
12222   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12223
12224   /* Complete the static assertion, which may mean either processing 
12225      the static assert now or saving it for template instantiation.  */
12226   finish_static_assert (condition, message, saved_loc, member_p);
12227 }
12228
12229 /* Parse the expression in decltype ( expression ).  */
12230
12231 static tree
12232 cp_parser_decltype_expr (cp_parser *parser,
12233                          bool &id_expression_or_member_access_p)
12234 {
12235   cp_token *id_expr_start_token;
12236   tree expr;
12237
12238   /* First, try parsing an id-expression.  */
12239   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12240   cp_parser_parse_tentatively (parser);
12241   expr = cp_parser_id_expression (parser,
12242                                   /*template_keyword_p=*/false,
12243                                   /*check_dependency_p=*/true,
12244                                   /*template_p=*/NULL,
12245                                   /*declarator_p=*/false,
12246                                   /*optional_p=*/false);
12247
12248   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12249     {
12250       bool non_integral_constant_expression_p = false;
12251       tree id_expression = expr;
12252       cp_id_kind idk;
12253       const char *error_msg;
12254
12255       if (identifier_p (expr))
12256         /* Lookup the name we got back from the id-expression.  */
12257         expr = cp_parser_lookup_name_simple (parser, expr,
12258                                              id_expr_start_token->location);
12259
12260       if (expr
12261           && expr != error_mark_node
12262           && TREE_CODE (expr) != TYPE_DECL
12263           && (TREE_CODE (expr) != BIT_NOT_EXPR
12264               || !TYPE_P (TREE_OPERAND (expr, 0)))
12265           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12266         {
12267           /* Complete lookup of the id-expression.  */
12268           expr = (finish_id_expression
12269                   (id_expression, expr, parser->scope, &idk,
12270                    /*integral_constant_expression_p=*/false,
12271                    /*allow_non_integral_constant_expression_p=*/true,
12272                    &non_integral_constant_expression_p,
12273                    /*template_p=*/false,
12274                    /*done=*/true,
12275                    /*address_p=*/false,
12276                    /*template_arg_p=*/false,
12277                    &error_msg,
12278                    id_expr_start_token->location));
12279
12280           if (expr == error_mark_node)
12281             /* We found an id-expression, but it was something that we
12282                should not have found. This is an error, not something
12283                we can recover from, so note that we found an
12284                id-expression and we'll recover as gracefully as
12285                possible.  */
12286             id_expression_or_member_access_p = true;
12287         }
12288
12289       if (expr 
12290           && expr != error_mark_node
12291           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12292         /* We have an id-expression.  */
12293         id_expression_or_member_access_p = true;
12294     }
12295
12296   if (!id_expression_or_member_access_p)
12297     {
12298       /* Abort the id-expression parse.  */
12299       cp_parser_abort_tentative_parse (parser);
12300
12301       /* Parsing tentatively, again.  */
12302       cp_parser_parse_tentatively (parser);
12303
12304       /* Parse a class member access.  */
12305       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12306                                            /*cast_p=*/false, /*decltype*/true,
12307                                            /*member_access_only_p=*/true, NULL);
12308
12309       if (expr 
12310           && expr != error_mark_node
12311           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12312         /* We have an id-expression.  */
12313         id_expression_or_member_access_p = true;
12314     }
12315
12316   if (id_expression_or_member_access_p)
12317     /* We have parsed the complete id-expression or member access.  */
12318     cp_parser_parse_definitely (parser);
12319   else
12320     {
12321       /* Abort our attempt to parse an id-expression or member access
12322          expression.  */
12323       cp_parser_abort_tentative_parse (parser);
12324
12325       /* Parse a full expression.  */
12326       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12327                                    /*decltype_p=*/true);
12328     }
12329
12330   return expr;
12331 }
12332
12333 /* Parse a `decltype' type. Returns the type.
12334
12335    simple-type-specifier:
12336      decltype ( expression )
12337    C++14 proposal:
12338      decltype ( auto )  */
12339
12340 static tree
12341 cp_parser_decltype (cp_parser *parser)
12342 {
12343   tree expr;
12344   bool id_expression_or_member_access_p = false;
12345   const char *saved_message;
12346   bool saved_integral_constant_expression_p;
12347   bool saved_non_integral_constant_expression_p;
12348   bool saved_greater_than_is_operator_p;
12349   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12350
12351   if (start_token->type == CPP_DECLTYPE)
12352     {
12353       /* Already parsed.  */
12354       cp_lexer_consume_token (parser->lexer);
12355       return start_token->u.value;
12356     }
12357
12358   /* Look for the `decltype' token.  */
12359   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12360     return error_mark_node;
12361
12362   /* Parse the opening `('.  */
12363   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12364     return error_mark_node;
12365
12366   /* decltype (auto) */
12367   if (cxx_dialect >= cxx14
12368       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12369     {
12370       cp_lexer_consume_token (parser->lexer);
12371       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12372         return error_mark_node;
12373       expr = make_decltype_auto ();
12374       AUTO_IS_DECLTYPE (expr) = true;
12375       goto rewrite;
12376     }
12377
12378   /* Types cannot be defined in a `decltype' expression.  Save away the
12379      old message.  */
12380   saved_message = parser->type_definition_forbidden_message;
12381
12382   /* And create the new one.  */
12383   parser->type_definition_forbidden_message
12384     = G_("types may not be defined in %<decltype%> expressions");
12385
12386   /* The restrictions on constant-expressions do not apply inside
12387      decltype expressions.  */
12388   saved_integral_constant_expression_p
12389     = parser->integral_constant_expression_p;
12390   saved_non_integral_constant_expression_p
12391     = parser->non_integral_constant_expression_p;
12392   parser->integral_constant_expression_p = false;
12393
12394   /* Within a parenthesized expression, a `>' token is always
12395      the greater-than operator.  */
12396   saved_greater_than_is_operator_p
12397     = parser->greater_than_is_operator_p;
12398   parser->greater_than_is_operator_p = true;
12399
12400   /* Do not actually evaluate the expression.  */
12401   ++cp_unevaluated_operand;
12402
12403   /* Do not warn about problems with the expression.  */
12404   ++c_inhibit_evaluation_warnings;
12405
12406   expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12407
12408   /* Go back to evaluating expressions.  */
12409   --cp_unevaluated_operand;
12410   --c_inhibit_evaluation_warnings;
12411
12412   /* The `>' token might be the end of a template-id or
12413      template-parameter-list now.  */
12414   parser->greater_than_is_operator_p
12415     = saved_greater_than_is_operator_p;
12416
12417   /* Restore the old message and the integral constant expression
12418      flags.  */
12419   parser->type_definition_forbidden_message = saved_message;
12420   parser->integral_constant_expression_p
12421     = saved_integral_constant_expression_p;
12422   parser->non_integral_constant_expression_p
12423     = saved_non_integral_constant_expression_p;
12424
12425   /* Parse to the closing `)'.  */
12426   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12427     {
12428       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12429                                              /*consume_paren=*/true);
12430       return error_mark_node;
12431     }
12432
12433   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12434                                tf_warning_or_error);
12435
12436  rewrite:
12437   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12438      it again.  */
12439   start_token->type = CPP_DECLTYPE;
12440   start_token->u.value = expr;
12441   start_token->keyword = RID_MAX;
12442   cp_lexer_purge_tokens_after (parser->lexer, start_token);
12443
12444   return expr;
12445 }
12446
12447 /* Special member functions [gram.special] */
12448
12449 /* Parse a conversion-function-id.
12450
12451    conversion-function-id:
12452      operator conversion-type-id
12453
12454    Returns an IDENTIFIER_NODE representing the operator.  */
12455
12456 static tree
12457 cp_parser_conversion_function_id (cp_parser* parser)
12458 {
12459   tree type;
12460   tree saved_scope;
12461   tree saved_qualifying_scope;
12462   tree saved_object_scope;
12463   tree pushed_scope = NULL_TREE;
12464
12465   /* Look for the `operator' token.  */
12466   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12467     return error_mark_node;
12468   /* When we parse the conversion-type-id, the current scope will be
12469      reset.  However, we need that information in able to look up the
12470      conversion function later, so we save it here.  */
12471   saved_scope = parser->scope;
12472   saved_qualifying_scope = parser->qualifying_scope;
12473   saved_object_scope = parser->object_scope;
12474   /* We must enter the scope of the class so that the names of
12475      entities declared within the class are available in the
12476      conversion-type-id.  For example, consider:
12477
12478        struct S {
12479          typedef int I;
12480          operator I();
12481        };
12482
12483        S::operator I() { ... }
12484
12485      In order to see that `I' is a type-name in the definition, we
12486      must be in the scope of `S'.  */
12487   if (saved_scope)
12488     pushed_scope = push_scope (saved_scope);
12489   /* Parse the conversion-type-id.  */
12490   type = cp_parser_conversion_type_id (parser);
12491   /* Leave the scope of the class, if any.  */
12492   if (pushed_scope)
12493     pop_scope (pushed_scope);
12494   /* Restore the saved scope.  */
12495   parser->scope = saved_scope;
12496   parser->qualifying_scope = saved_qualifying_scope;
12497   parser->object_scope = saved_object_scope;
12498   /* If the TYPE is invalid, indicate failure.  */
12499   if (type == error_mark_node)
12500     return error_mark_node;
12501   return mangle_conv_op_name_for_type (type);
12502 }
12503
12504 /* Parse a conversion-type-id:
12505
12506    conversion-type-id:
12507      type-specifier-seq conversion-declarator [opt]
12508
12509    Returns the TYPE specified.  */
12510
12511 static tree
12512 cp_parser_conversion_type_id (cp_parser* parser)
12513 {
12514   tree attributes;
12515   cp_decl_specifier_seq type_specifiers;
12516   cp_declarator *declarator;
12517   tree type_specified;
12518   const char *saved_message;
12519
12520   /* Parse the attributes.  */
12521   attributes = cp_parser_attributes_opt (parser);
12522
12523   saved_message = parser->type_definition_forbidden_message;
12524   parser->type_definition_forbidden_message
12525     = G_("types may not be defined in a conversion-type-id");
12526
12527   /* Parse the type-specifiers.  */
12528   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12529                                 /*is_trailing_return=*/false,
12530                                 &type_specifiers);
12531
12532   parser->type_definition_forbidden_message = saved_message;
12533
12534   /* If that didn't work, stop.  */
12535   if (type_specifiers.type == error_mark_node)
12536     return error_mark_node;
12537   /* Parse the conversion-declarator.  */
12538   declarator = cp_parser_conversion_declarator_opt (parser);
12539
12540   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
12541                                     /*initialized=*/0, &attributes);
12542   if (attributes)
12543     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12544
12545   /* Don't give this error when parsing tentatively.  This happens to
12546      work because we always parse this definitively once.  */
12547   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12548       && type_uses_auto (type_specified))
12549     {
12550       if (cxx_dialect < cxx14)
12551         {
12552           error ("invalid use of %<auto%> in conversion operator");
12553           return error_mark_node;
12554         }
12555       else if (template_parm_scope_p ())
12556         warning (0, "use of %<auto%> in member template "
12557                  "conversion operator can never be deduced");
12558     }
12559
12560   return type_specified;
12561 }
12562
12563 /* Parse an (optional) conversion-declarator.
12564
12565    conversion-declarator:
12566      ptr-operator conversion-declarator [opt]
12567
12568    */
12569
12570 static cp_declarator *
12571 cp_parser_conversion_declarator_opt (cp_parser* parser)
12572 {
12573   enum tree_code code;
12574   tree class_type, std_attributes = NULL_TREE;
12575   cp_cv_quals cv_quals;
12576
12577   /* We don't know if there's a ptr-operator next, or not.  */
12578   cp_parser_parse_tentatively (parser);
12579   /* Try the ptr-operator.  */
12580   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12581                                  &std_attributes);
12582   /* If it worked, look for more conversion-declarators.  */
12583   if (cp_parser_parse_definitely (parser))
12584     {
12585       cp_declarator *declarator;
12586
12587       /* Parse another optional declarator.  */
12588       declarator = cp_parser_conversion_declarator_opt (parser);
12589
12590       declarator = cp_parser_make_indirect_declarator
12591         (code, class_type, cv_quals, declarator, std_attributes);
12592
12593       return declarator;
12594    }
12595
12596   return NULL;
12597 }
12598
12599 /* Parse an (optional) ctor-initializer.
12600
12601    ctor-initializer:
12602      : mem-initializer-list
12603
12604    Returns TRUE iff the ctor-initializer was actually present.  */
12605
12606 static bool
12607 cp_parser_ctor_initializer_opt (cp_parser* parser)
12608 {
12609   /* If the next token is not a `:', then there is no
12610      ctor-initializer.  */
12611   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12612     {
12613       /* Do default initialization of any bases and members.  */
12614       if (DECL_CONSTRUCTOR_P (current_function_decl))
12615         finish_mem_initializers (NULL_TREE);
12616
12617       return false;
12618     }
12619
12620   /* Consume the `:' token.  */
12621   cp_lexer_consume_token (parser->lexer);
12622   /* And the mem-initializer-list.  */
12623   cp_parser_mem_initializer_list (parser);
12624
12625   return true;
12626 }
12627
12628 /* Parse a mem-initializer-list.
12629
12630    mem-initializer-list:
12631      mem-initializer ... [opt]
12632      mem-initializer ... [opt] , mem-initializer-list  */
12633
12634 static void
12635 cp_parser_mem_initializer_list (cp_parser* parser)
12636 {
12637   tree mem_initializer_list = NULL_TREE;
12638   tree target_ctor = error_mark_node;
12639   cp_token *token = cp_lexer_peek_token (parser->lexer);
12640
12641   /* Let the semantic analysis code know that we are starting the
12642      mem-initializer-list.  */
12643   if (!DECL_CONSTRUCTOR_P (current_function_decl))
12644     error_at (token->location,
12645               "only constructors take member initializers");
12646
12647   /* Loop through the list.  */
12648   while (true)
12649     {
12650       tree mem_initializer;
12651
12652       token = cp_lexer_peek_token (parser->lexer);
12653       /* Parse the mem-initializer.  */
12654       mem_initializer = cp_parser_mem_initializer (parser);
12655       /* If the next token is a `...', we're expanding member initializers. */
12656       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12657         {
12658           /* Consume the `...'. */
12659           cp_lexer_consume_token (parser->lexer);
12660
12661           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12662              can be expanded but members cannot. */
12663           if (mem_initializer != error_mark_node
12664               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12665             {
12666               error_at (token->location,
12667                         "cannot expand initializer for member %<%D%>",
12668                         TREE_PURPOSE (mem_initializer));
12669               mem_initializer = error_mark_node;
12670             }
12671
12672           /* Construct the pack expansion type. */
12673           if (mem_initializer != error_mark_node)
12674             mem_initializer = make_pack_expansion (mem_initializer);
12675         }
12676       if (target_ctor != error_mark_node
12677           && mem_initializer != error_mark_node)
12678         {
12679           error ("mem-initializer for %qD follows constructor delegation",
12680                  TREE_PURPOSE (mem_initializer));
12681           mem_initializer = error_mark_node;
12682         }
12683       /* Look for a target constructor. */
12684       if (mem_initializer != error_mark_node
12685           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12686           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12687         {
12688           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12689           if (mem_initializer_list)
12690             {
12691               error ("constructor delegation follows mem-initializer for %qD",
12692                      TREE_PURPOSE (mem_initializer_list));
12693               mem_initializer = error_mark_node;
12694             }
12695           target_ctor = mem_initializer;
12696         }
12697       /* Add it to the list, unless it was erroneous.  */
12698       if (mem_initializer != error_mark_node)
12699         {
12700           TREE_CHAIN (mem_initializer) = mem_initializer_list;
12701           mem_initializer_list = mem_initializer;
12702         }
12703       /* If the next token is not a `,', we're done.  */
12704       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12705         break;
12706       /* Consume the `,' token.  */
12707       cp_lexer_consume_token (parser->lexer);
12708     }
12709
12710   /* Perform semantic analysis.  */
12711   if (DECL_CONSTRUCTOR_P (current_function_decl))
12712     finish_mem_initializers (mem_initializer_list);
12713 }
12714
12715 /* Parse a mem-initializer.
12716
12717    mem-initializer:
12718      mem-initializer-id ( expression-list [opt] )
12719      mem-initializer-id braced-init-list
12720
12721    GNU extension:
12722
12723    mem-initializer:
12724      ( expression-list [opt] )
12725
12726    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
12727    class) or FIELD_DECL (for a non-static data member) to initialize;
12728    the TREE_VALUE is the expression-list.  An empty initialization
12729    list is represented by void_list_node.  */
12730
12731 static tree
12732 cp_parser_mem_initializer (cp_parser* parser)
12733 {
12734   tree mem_initializer_id;
12735   tree expression_list;
12736   tree member;
12737   cp_token *token = cp_lexer_peek_token (parser->lexer);
12738
12739   /* Find out what is being initialized.  */
12740   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12741     {
12742       permerror (token->location,
12743                  "anachronistic old-style base class initializer");
12744       mem_initializer_id = NULL_TREE;
12745     }
12746   else
12747     {
12748       mem_initializer_id = cp_parser_mem_initializer_id (parser);
12749       if (mem_initializer_id == error_mark_node)
12750         return mem_initializer_id;
12751     }
12752   member = expand_member_init (mem_initializer_id);
12753   if (member && !DECL_P (member))
12754     in_base_initializer = 1;
12755
12756   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12757     {
12758       bool expr_non_constant_p;
12759       cp_lexer_set_source_position (parser->lexer);
12760       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12761       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12762       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12763       expression_list = build_tree_list (NULL_TREE, expression_list);
12764     }
12765   else
12766     {
12767       vec<tree, va_gc> *vec;
12768       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12769                                                      /*cast_p=*/false,
12770                                                      /*allow_expansion_p=*/true,
12771                                                      /*non_constant_p=*/NULL);
12772       if (vec == NULL)
12773         return error_mark_node;
12774       expression_list = build_tree_list_vec (vec);
12775       release_tree_vector (vec);
12776     }
12777
12778   if (expression_list == error_mark_node)
12779     return error_mark_node;
12780   if (!expression_list)
12781     expression_list = void_type_node;
12782
12783   in_base_initializer = 0;
12784
12785   return member ? build_tree_list (member, expression_list) : error_mark_node;
12786 }
12787
12788 /* Parse a mem-initializer-id.
12789
12790    mem-initializer-id:
12791      :: [opt] nested-name-specifier [opt] class-name
12792      identifier
12793
12794    Returns a TYPE indicating the class to be initializer for the first
12795    production.  Returns an IDENTIFIER_NODE indicating the data member
12796    to be initialized for the second production.  */
12797
12798 static tree
12799 cp_parser_mem_initializer_id (cp_parser* parser)
12800 {
12801   bool global_scope_p;
12802   bool nested_name_specifier_p;
12803   bool template_p = false;
12804   tree id;
12805
12806   cp_token *token = cp_lexer_peek_token (parser->lexer);
12807
12808   /* `typename' is not allowed in this context ([temp.res]).  */
12809   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12810     {
12811       error_at (token->location, 
12812                 "keyword %<typename%> not allowed in this context (a qualified "
12813                 "member initializer is implicitly a type)");
12814       cp_lexer_consume_token (parser->lexer);
12815     }
12816   /* Look for the optional `::' operator.  */
12817   global_scope_p
12818     = (cp_parser_global_scope_opt (parser,
12819                                    /*current_scope_valid_p=*/false)
12820        != NULL_TREE);
12821   /* Look for the optional nested-name-specifier.  The simplest way to
12822      implement:
12823
12824        [temp.res]
12825
12826        The keyword `typename' is not permitted in a base-specifier or
12827        mem-initializer; in these contexts a qualified name that
12828        depends on a template-parameter is implicitly assumed to be a
12829        type name.
12830
12831      is to assume that we have seen the `typename' keyword at this
12832      point.  */
12833   nested_name_specifier_p
12834     = (cp_parser_nested_name_specifier_opt (parser,
12835                                             /*typename_keyword_p=*/true,
12836                                             /*check_dependency_p=*/true,
12837                                             /*type_p=*/true,
12838                                             /*is_declaration=*/true)
12839        != NULL_TREE);
12840   if (nested_name_specifier_p)
12841     template_p = cp_parser_optional_template_keyword (parser);
12842   /* If there is a `::' operator or a nested-name-specifier, then we
12843      are definitely looking for a class-name.  */
12844   if (global_scope_p || nested_name_specifier_p)
12845     return cp_parser_class_name (parser,
12846                                  /*typename_keyword_p=*/true,
12847                                  /*template_keyword_p=*/template_p,
12848                                  typename_type,
12849                                  /*check_dependency_p=*/true,
12850                                  /*class_head_p=*/false,
12851                                  /*is_declaration=*/true);
12852   /* Otherwise, we could also be looking for an ordinary identifier.  */
12853   cp_parser_parse_tentatively (parser);
12854   /* Try a class-name.  */
12855   id = cp_parser_class_name (parser,
12856                              /*typename_keyword_p=*/true,
12857                              /*template_keyword_p=*/false,
12858                              none_type,
12859                              /*check_dependency_p=*/true,
12860                              /*class_head_p=*/false,
12861                              /*is_declaration=*/true);
12862   /* If we found one, we're done.  */
12863   if (cp_parser_parse_definitely (parser))
12864     return id;
12865   /* Otherwise, look for an ordinary identifier.  */
12866   return cp_parser_identifier (parser);
12867 }
12868
12869 /* Overloading [gram.over] */
12870
12871 /* Parse an operator-function-id.
12872
12873    operator-function-id:
12874      operator operator
12875
12876    Returns an IDENTIFIER_NODE for the operator which is a
12877    human-readable spelling of the identifier, e.g., `operator +'.  */
12878
12879 static tree
12880 cp_parser_operator_function_id (cp_parser* parser)
12881 {
12882   /* Look for the `operator' keyword.  */
12883   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12884     return error_mark_node;
12885   /* And then the name of the operator itself.  */
12886   return cp_parser_operator (parser);
12887 }
12888
12889 /* Return an identifier node for a user-defined literal operator.
12890    The suffix identifier is chained to the operator name identifier.  */
12891
12892 static tree
12893 cp_literal_operator_id (const char* name)
12894 {
12895   tree identifier;
12896   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12897                               + strlen (name) + 10);
12898   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12899   identifier = get_identifier (buffer);
12900
12901   return identifier;
12902 }
12903
12904 /* Parse an operator.
12905
12906    operator:
12907      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12908      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12909      || ++ -- , ->* -> () []
12910
12911    GNU Extensions:
12912
12913    operator:
12914      <? >? <?= >?=
12915
12916    Returns an IDENTIFIER_NODE for the operator which is a
12917    human-readable spelling of the identifier, e.g., `operator +'.  */
12918
12919 static tree
12920 cp_parser_operator (cp_parser* parser)
12921 {
12922   tree id = NULL_TREE;
12923   cp_token *token;
12924   bool utf8 = false;
12925
12926   /* Peek at the next token.  */
12927   token = cp_lexer_peek_token (parser->lexer);
12928   /* Figure out which operator we have.  */
12929   switch (token->type)
12930     {
12931     case CPP_KEYWORD:
12932       {
12933         enum tree_code op;
12934
12935         /* The keyword should be either `new' or `delete'.  */
12936         if (token->keyword == RID_NEW)
12937           op = NEW_EXPR;
12938         else if (token->keyword == RID_DELETE)
12939           op = DELETE_EXPR;
12940         else
12941           break;
12942
12943         /* Consume the `new' or `delete' token.  */
12944         cp_lexer_consume_token (parser->lexer);
12945
12946         /* Peek at the next token.  */
12947         token = cp_lexer_peek_token (parser->lexer);
12948         /* If it's a `[' token then this is the array variant of the
12949            operator.  */
12950         if (token->type == CPP_OPEN_SQUARE)
12951           {
12952             /* Consume the `[' token.  */
12953             cp_lexer_consume_token (parser->lexer);
12954             /* Look for the `]' token.  */
12955             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12956             id = ansi_opname (op == NEW_EXPR
12957                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12958           }
12959         /* Otherwise, we have the non-array variant.  */
12960         else
12961           id = ansi_opname (op);
12962
12963         return id;
12964       }
12965
12966     case CPP_PLUS:
12967       id = ansi_opname (PLUS_EXPR);
12968       break;
12969
12970     case CPP_MINUS:
12971       id = ansi_opname (MINUS_EXPR);
12972       break;
12973
12974     case CPP_MULT:
12975       id = ansi_opname (MULT_EXPR);
12976       break;
12977
12978     case CPP_DIV:
12979       id = ansi_opname (TRUNC_DIV_EXPR);
12980       break;
12981
12982     case CPP_MOD:
12983       id = ansi_opname (TRUNC_MOD_EXPR);
12984       break;
12985
12986     case CPP_XOR:
12987       id = ansi_opname (BIT_XOR_EXPR);
12988       break;
12989
12990     case CPP_AND:
12991       id = ansi_opname (BIT_AND_EXPR);
12992       break;
12993
12994     case CPP_OR:
12995       id = ansi_opname (BIT_IOR_EXPR);
12996       break;
12997
12998     case CPP_COMPL:
12999       id = ansi_opname (BIT_NOT_EXPR);
13000       break;
13001
13002     case CPP_NOT:
13003       id = ansi_opname (TRUTH_NOT_EXPR);
13004       break;
13005
13006     case CPP_EQ:
13007       id = ansi_assopname (NOP_EXPR);
13008       break;
13009
13010     case CPP_LESS:
13011       id = ansi_opname (LT_EXPR);
13012       break;
13013
13014     case CPP_GREATER:
13015       id = ansi_opname (GT_EXPR);
13016       break;
13017
13018     case CPP_PLUS_EQ:
13019       id = ansi_assopname (PLUS_EXPR);
13020       break;
13021
13022     case CPP_MINUS_EQ:
13023       id = ansi_assopname (MINUS_EXPR);
13024       break;
13025
13026     case CPP_MULT_EQ:
13027       id = ansi_assopname (MULT_EXPR);
13028       break;
13029
13030     case CPP_DIV_EQ:
13031       id = ansi_assopname (TRUNC_DIV_EXPR);
13032       break;
13033
13034     case CPP_MOD_EQ:
13035       id = ansi_assopname (TRUNC_MOD_EXPR);
13036       break;
13037
13038     case CPP_XOR_EQ:
13039       id = ansi_assopname (BIT_XOR_EXPR);
13040       break;
13041
13042     case CPP_AND_EQ:
13043       id = ansi_assopname (BIT_AND_EXPR);
13044       break;
13045
13046     case CPP_OR_EQ:
13047       id = ansi_assopname (BIT_IOR_EXPR);
13048       break;
13049
13050     case CPP_LSHIFT:
13051       id = ansi_opname (LSHIFT_EXPR);
13052       break;
13053
13054     case CPP_RSHIFT:
13055       id = ansi_opname (RSHIFT_EXPR);
13056       break;
13057
13058     case CPP_LSHIFT_EQ:
13059       id = ansi_assopname (LSHIFT_EXPR);
13060       break;
13061
13062     case CPP_RSHIFT_EQ:
13063       id = ansi_assopname (RSHIFT_EXPR);
13064       break;
13065
13066     case CPP_EQ_EQ:
13067       id = ansi_opname (EQ_EXPR);
13068       break;
13069
13070     case CPP_NOT_EQ:
13071       id = ansi_opname (NE_EXPR);
13072       break;
13073
13074     case CPP_LESS_EQ:
13075       id = ansi_opname (LE_EXPR);
13076       break;
13077
13078     case CPP_GREATER_EQ:
13079       id = ansi_opname (GE_EXPR);
13080       break;
13081
13082     case CPP_AND_AND:
13083       id = ansi_opname (TRUTH_ANDIF_EXPR);
13084       break;
13085
13086     case CPP_OR_OR:
13087       id = ansi_opname (TRUTH_ORIF_EXPR);
13088       break;
13089
13090     case CPP_PLUS_PLUS:
13091       id = ansi_opname (POSTINCREMENT_EXPR);
13092       break;
13093
13094     case CPP_MINUS_MINUS:
13095       id = ansi_opname (PREDECREMENT_EXPR);
13096       break;
13097
13098     case CPP_COMMA:
13099       id = ansi_opname (COMPOUND_EXPR);
13100       break;
13101
13102     case CPP_DEREF_STAR:
13103       id = ansi_opname (MEMBER_REF);
13104       break;
13105
13106     case CPP_DEREF:
13107       id = ansi_opname (COMPONENT_REF);
13108       break;
13109
13110     case CPP_OPEN_PAREN:
13111       /* Consume the `('.  */
13112       cp_lexer_consume_token (parser->lexer);
13113       /* Look for the matching `)'.  */
13114       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13115       return ansi_opname (CALL_EXPR);
13116
13117     case CPP_OPEN_SQUARE:
13118       /* Consume the `['.  */
13119       cp_lexer_consume_token (parser->lexer);
13120       /* Look for the matching `]'.  */
13121       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13122       return ansi_opname (ARRAY_REF);
13123
13124     case CPP_UTF8STRING:
13125     case CPP_UTF8STRING_USERDEF:
13126       utf8 = true;
13127     case CPP_STRING:
13128     case CPP_WSTRING:
13129     case CPP_STRING16:
13130     case CPP_STRING32:
13131     case CPP_STRING_USERDEF:
13132     case CPP_WSTRING_USERDEF:
13133     case CPP_STRING16_USERDEF:
13134     case CPP_STRING32_USERDEF:
13135       {
13136         tree str, string_tree;
13137         int sz, len;
13138
13139         if (cxx_dialect == cxx98)
13140           maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13141
13142         /* Consume the string.  */
13143         str = cp_parser_string_literal (parser, /*translate=*/true,
13144                                       /*wide_ok=*/true, /*lookup_udlit=*/false);
13145         if (str == error_mark_node)
13146           return error_mark_node;
13147         else if (TREE_CODE (str) == USERDEF_LITERAL)
13148           {
13149             string_tree = USERDEF_LITERAL_VALUE (str);
13150             id = USERDEF_LITERAL_SUFFIX_ID (str);
13151           }
13152         else
13153           {
13154             string_tree = str;
13155             /* Look for the suffix identifier.  */
13156             token = cp_lexer_peek_token (parser->lexer);
13157             if (token->type == CPP_NAME)
13158               id = cp_parser_identifier (parser);
13159             else if (token->type == CPP_KEYWORD)
13160               {
13161                 error ("unexpected keyword;"
13162                        " remove space between quotes and suffix identifier");
13163                 return error_mark_node;
13164               }
13165             else
13166               {
13167                 error ("expected suffix identifier");
13168                 return error_mark_node;
13169               }
13170           }
13171         sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13172                                (TREE_TYPE (TREE_TYPE (string_tree))));
13173         len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13174         if (len != 0)
13175           {
13176             error ("expected empty string after %<operator%> keyword");
13177             return error_mark_node;
13178           }
13179         if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13180             != char_type_node)
13181           {
13182             error ("invalid encoding prefix in literal operator");
13183             return error_mark_node;
13184           }
13185         if (id != error_mark_node)
13186           {
13187             const char *name = IDENTIFIER_POINTER (id);
13188             id = cp_literal_operator_id (name);
13189           }
13190         return id;
13191       }
13192
13193     default:
13194       /* Anything else is an error.  */
13195       break;
13196     }
13197
13198   /* If we have selected an identifier, we need to consume the
13199      operator token.  */
13200   if (id)
13201     cp_lexer_consume_token (parser->lexer);
13202   /* Otherwise, no valid operator name was present.  */
13203   else
13204     {
13205       cp_parser_error (parser, "expected operator");
13206       id = error_mark_node;
13207     }
13208
13209   return id;
13210 }
13211
13212 /* Parse a template-declaration.
13213
13214    template-declaration:
13215      export [opt] template < template-parameter-list > declaration
13216
13217    If MEMBER_P is TRUE, this template-declaration occurs within a
13218    class-specifier.
13219
13220    The grammar rule given by the standard isn't correct.  What
13221    is really meant is:
13222
13223    template-declaration:
13224      export [opt] template-parameter-list-seq
13225        decl-specifier-seq [opt] init-declarator [opt] ;
13226      export [opt] template-parameter-list-seq
13227        function-definition
13228
13229    template-parameter-list-seq:
13230      template-parameter-list-seq [opt]
13231      template < template-parameter-list >  */
13232
13233 static void
13234 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13235 {
13236   /* Check for `export'.  */
13237   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13238     {
13239       /* Consume the `export' token.  */
13240       cp_lexer_consume_token (parser->lexer);
13241       /* Warn that we do not support `export'.  */
13242       warning (0, "keyword %<export%> not implemented, and will be ignored");
13243     }
13244
13245   cp_parser_template_declaration_after_export (parser, member_p);
13246 }
13247
13248 /* Parse a template-parameter-list.
13249
13250    template-parameter-list:
13251      template-parameter
13252      template-parameter-list , template-parameter
13253
13254    Returns a TREE_LIST.  Each node represents a template parameter.
13255    The nodes are connected via their TREE_CHAINs.  */
13256
13257 static tree
13258 cp_parser_template_parameter_list (cp_parser* parser)
13259 {
13260   tree parameter_list = NULL_TREE;
13261
13262   begin_template_parm_list ();
13263
13264   /* The loop below parses the template parms.  We first need to know
13265      the total number of template parms to be able to compute proper
13266      canonical types of each dependent type. So after the loop, when
13267      we know the total number of template parms,
13268      end_template_parm_list computes the proper canonical types and
13269      fixes up the dependent types accordingly.  */
13270   while (true)
13271     {
13272       tree parameter;
13273       bool is_non_type;
13274       bool is_parameter_pack;
13275       location_t parm_loc;
13276
13277       /* Parse the template-parameter.  */
13278       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13279       parameter = cp_parser_template_parameter (parser, 
13280                                                 &is_non_type,
13281                                                 &is_parameter_pack);
13282       /* Add it to the list.  */
13283       if (parameter != error_mark_node)
13284         parameter_list = process_template_parm (parameter_list,
13285                                                 parm_loc,
13286                                                 parameter,
13287                                                 is_non_type,
13288                                                 is_parameter_pack);
13289       else
13290        {
13291          tree err_parm = build_tree_list (parameter, parameter);
13292          parameter_list = chainon (parameter_list, err_parm);
13293        }
13294
13295       /* If the next token is not a `,', we're done.  */
13296       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13297         break;
13298       /* Otherwise, consume the `,' token.  */
13299       cp_lexer_consume_token (parser->lexer);
13300     }
13301
13302   return end_template_parm_list (parameter_list);
13303 }
13304
13305 /* Parse a template-parameter.
13306
13307    template-parameter:
13308      type-parameter
13309      parameter-declaration
13310
13311    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
13312    the parameter.  The TREE_PURPOSE is the default value, if any.
13313    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
13314    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
13315    set to true iff this parameter is a parameter pack. */
13316
13317 static tree
13318 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13319                               bool *is_parameter_pack)
13320 {
13321   cp_token *token;
13322   cp_parameter_declarator *parameter_declarator;
13323   cp_declarator *id_declarator;
13324   tree parm;
13325
13326   /* Assume it is a type parameter or a template parameter.  */
13327   *is_non_type = false;
13328   /* Assume it not a parameter pack. */
13329   *is_parameter_pack = false;
13330   /* Peek at the next token.  */
13331   token = cp_lexer_peek_token (parser->lexer);
13332   /* If it is `class' or `template', we have a type-parameter.  */
13333   if (token->keyword == RID_TEMPLATE)
13334     return cp_parser_type_parameter (parser, is_parameter_pack);
13335   /* If it is `class' or `typename' we do not know yet whether it is a
13336      type parameter or a non-type parameter.  Consider:
13337
13338        template <typename T, typename T::X X> ...
13339
13340      or:
13341
13342        template <class C, class D*> ...
13343
13344      Here, the first parameter is a type parameter, and the second is
13345      a non-type parameter.  We can tell by looking at the token after
13346      the identifier -- if it is a `,', `=', or `>' then we have a type
13347      parameter.  */
13348   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13349     {
13350       /* Peek at the token after `class' or `typename'.  */
13351       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13352       /* If it's an ellipsis, we have a template type parameter
13353          pack. */
13354       if (token->type == CPP_ELLIPSIS)
13355         return cp_parser_type_parameter (parser, is_parameter_pack);
13356       /* If it's an identifier, skip it.  */
13357       if (token->type == CPP_NAME)
13358         token = cp_lexer_peek_nth_token (parser->lexer, 3);
13359       /* Now, see if the token looks like the end of a template
13360          parameter.  */
13361       if (token->type == CPP_COMMA
13362           || token->type == CPP_EQ
13363           || token->type == CPP_GREATER)
13364         return cp_parser_type_parameter (parser, is_parameter_pack);
13365     }
13366
13367   /* Otherwise, it is a non-type parameter.
13368
13369      [temp.param]
13370
13371      When parsing a default template-argument for a non-type
13372      template-parameter, the first non-nested `>' is taken as the end
13373      of the template parameter-list rather than a greater-than
13374      operator.  */
13375   *is_non_type = true;
13376   parameter_declarator
13377      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13378                                         /*parenthesized_p=*/NULL);
13379
13380   if (!parameter_declarator)
13381     return error_mark_node;
13382
13383   /* If the parameter declaration is marked as a parameter pack, set
13384      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13385      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13386      grokdeclarator. */
13387   if (parameter_declarator->declarator
13388       && parameter_declarator->declarator->parameter_pack_p)
13389     {
13390       *is_parameter_pack = true;
13391       parameter_declarator->declarator->parameter_pack_p = false;
13392     }
13393
13394   if (parameter_declarator->default_argument)
13395     {
13396       /* Can happen in some cases of erroneous input (c++/34892).  */
13397       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13398         /* Consume the `...' for better error recovery.  */
13399         cp_lexer_consume_token (parser->lexer);
13400     }
13401   /* If the next token is an ellipsis, and we don't already have it
13402      marked as a parameter pack, then we have a parameter pack (that
13403      has no declarator).  */
13404   else if (!*is_parameter_pack
13405            && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13406            && (declarator_can_be_parameter_pack
13407                (parameter_declarator->declarator)))
13408     {
13409       /* Consume the `...'.  */
13410       cp_lexer_consume_token (parser->lexer);
13411       maybe_warn_variadic_templates ();
13412       
13413       *is_parameter_pack = true;
13414     }
13415   /* We might end up with a pack expansion as the type of the non-type
13416      template parameter, in which case this is a non-type template
13417      parameter pack.  */
13418   else if (parameter_declarator->decl_specifiers.type
13419            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13420     {
13421       *is_parameter_pack = true;
13422       parameter_declarator->decl_specifiers.type = 
13423         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13424     }
13425
13426   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13427     {
13428       /* Parameter packs cannot have default arguments.  However, a
13429          user may try to do so, so we'll parse them and give an
13430          appropriate diagnostic here.  */
13431
13432       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13433       
13434       /* Find the name of the parameter pack.  */     
13435       id_declarator = parameter_declarator->declarator;
13436       while (id_declarator && id_declarator->kind != cdk_id)
13437         id_declarator = id_declarator->declarator;
13438       
13439       if (id_declarator && id_declarator->kind == cdk_id)
13440         error_at (start_token->location,
13441                   "template parameter pack %qD cannot have a default argument",
13442                   id_declarator->u.id.unqualified_name);
13443       else
13444         error_at (start_token->location,
13445                   "template parameter pack cannot have a default argument");
13446       
13447       /* Parse the default argument, but throw away the result.  */
13448       cp_parser_default_argument (parser, /*template_parm_p=*/true);
13449     }
13450
13451   parm = grokdeclarator (parameter_declarator->declarator,
13452                          &parameter_declarator->decl_specifiers,
13453                          TPARM, /*initialized=*/0,
13454                          /*attrlist=*/NULL);
13455   if (parm == error_mark_node)
13456     return error_mark_node;
13457
13458   return build_tree_list (parameter_declarator->default_argument, parm);
13459 }
13460
13461 /* Parse a type-parameter.
13462
13463    type-parameter:
13464      class identifier [opt]
13465      class identifier [opt] = type-id
13466      typename identifier [opt]
13467      typename identifier [opt] = type-id
13468      template < template-parameter-list > class identifier [opt]
13469      template < template-parameter-list > class identifier [opt]
13470        = id-expression
13471
13472    GNU Extension (variadic templates):
13473
13474    type-parameter:
13475      class ... identifier [opt]
13476      typename ... identifier [opt]
13477
13478    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
13479    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
13480    the declaration of the parameter.
13481
13482    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13483
13484 static tree
13485 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13486 {
13487   cp_token *token;
13488   tree parameter;
13489
13490   /* Look for a keyword to tell us what kind of parameter this is.  */
13491   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13492   if (!token)
13493     return error_mark_node;
13494
13495   switch (token->keyword)
13496     {
13497     case RID_CLASS:
13498     case RID_TYPENAME:
13499       {
13500         tree identifier;
13501         tree default_argument;
13502
13503         /* If the next token is an ellipsis, we have a template
13504            argument pack. */
13505         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13506           {
13507             /* Consume the `...' token. */
13508             cp_lexer_consume_token (parser->lexer);
13509             maybe_warn_variadic_templates ();
13510
13511             *is_parameter_pack = true;
13512           }
13513
13514         /* If the next token is an identifier, then it names the
13515            parameter.  */
13516         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13517           identifier = cp_parser_identifier (parser);
13518         else
13519           identifier = NULL_TREE;
13520
13521         /* Create the parameter.  */
13522         parameter = finish_template_type_parm (class_type_node, identifier);
13523
13524         /* If the next token is an `=', we have a default argument.  */
13525         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13526           {
13527             /* Consume the `=' token.  */
13528             cp_lexer_consume_token (parser->lexer);
13529             /* Parse the default-argument.  */
13530             push_deferring_access_checks (dk_no_deferred);
13531             default_argument = cp_parser_type_id (parser);
13532
13533             /* Template parameter packs cannot have default
13534                arguments. */
13535             if (*is_parameter_pack)
13536               {
13537                 if (identifier)
13538                   error_at (token->location,
13539                             "template parameter pack %qD cannot have a "
13540                             "default argument", identifier);
13541                 else
13542                   error_at (token->location,
13543                             "template parameter packs cannot have "
13544                             "default arguments");
13545                 default_argument = NULL_TREE;
13546               }
13547             else if (check_for_bare_parameter_packs (default_argument))
13548               default_argument = error_mark_node;
13549             pop_deferring_access_checks ();
13550           }
13551         else
13552           default_argument = NULL_TREE;
13553
13554         /* Create the combined representation of the parameter and the
13555            default argument.  */
13556         parameter = build_tree_list (default_argument, parameter);
13557       }
13558       break;
13559
13560     case RID_TEMPLATE:
13561       {
13562         tree identifier;
13563         tree default_argument;
13564
13565         /* Look for the `<'.  */
13566         cp_parser_require (parser, CPP_LESS, RT_LESS);
13567         /* Parse the template-parameter-list.  */
13568         cp_parser_template_parameter_list (parser);
13569         /* Look for the `>'.  */
13570         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13571         /* Look for the `class' or 'typename' keywords.  */
13572         cp_parser_type_parameter_key (parser);
13573         /* If the next token is an ellipsis, we have a template
13574            argument pack. */
13575         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13576           {
13577             /* Consume the `...' token. */
13578             cp_lexer_consume_token (parser->lexer);
13579             maybe_warn_variadic_templates ();
13580
13581             *is_parameter_pack = true;
13582           }
13583         /* If the next token is an `=', then there is a
13584            default-argument.  If the next token is a `>', we are at
13585            the end of the parameter-list.  If the next token is a `,',
13586            then we are at the end of this parameter.  */
13587         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13588             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13589             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13590           {
13591             identifier = cp_parser_identifier (parser);
13592             /* Treat invalid names as if the parameter were nameless.  */
13593             if (identifier == error_mark_node)
13594               identifier = NULL_TREE;
13595           }
13596         else
13597           identifier = NULL_TREE;
13598
13599         /* Create the template parameter.  */
13600         parameter = finish_template_template_parm (class_type_node,
13601                                                    identifier);
13602
13603         /* If the next token is an `=', then there is a
13604            default-argument.  */
13605         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13606           {
13607             bool is_template;
13608
13609             /* Consume the `='.  */
13610             cp_lexer_consume_token (parser->lexer);
13611             /* Parse the id-expression.  */
13612             push_deferring_access_checks (dk_no_deferred);
13613             /* save token before parsing the id-expression, for error
13614                reporting */
13615             token = cp_lexer_peek_token (parser->lexer);
13616             default_argument
13617               = cp_parser_id_expression (parser,
13618                                          /*template_keyword_p=*/false,
13619                                          /*check_dependency_p=*/true,
13620                                          /*template_p=*/&is_template,
13621                                          /*declarator_p=*/false,
13622                                          /*optional_p=*/false);
13623             if (TREE_CODE (default_argument) == TYPE_DECL)
13624               /* If the id-expression was a template-id that refers to
13625                  a template-class, we already have the declaration here,
13626                  so no further lookup is needed.  */
13627                  ;
13628             else
13629               /* Look up the name.  */
13630               default_argument
13631                 = cp_parser_lookup_name (parser, default_argument,
13632                                          none_type,
13633                                          /*is_template=*/is_template,
13634                                          /*is_namespace=*/false,
13635                                          /*check_dependency=*/true,
13636                                          /*ambiguous_decls=*/NULL,
13637                                          token->location);
13638             /* See if the default argument is valid.  */
13639             default_argument
13640               = check_template_template_default_arg (default_argument);
13641
13642             /* Template parameter packs cannot have default
13643                arguments. */
13644             if (*is_parameter_pack)
13645               {
13646                 if (identifier)
13647                   error_at (token->location,
13648                             "template parameter pack %qD cannot "
13649                             "have a default argument",
13650                             identifier);
13651                 else
13652                   error_at (token->location, "template parameter packs cannot "
13653                             "have default arguments");
13654                 default_argument = NULL_TREE;
13655               }
13656             pop_deferring_access_checks ();
13657           }
13658         else
13659           default_argument = NULL_TREE;
13660
13661         /* Create the combined representation of the parameter and the
13662            default argument.  */
13663         parameter = build_tree_list (default_argument, parameter);
13664       }
13665       break;
13666
13667     default:
13668       gcc_unreachable ();
13669       break;
13670     }
13671
13672   return parameter;
13673 }
13674
13675 /* Parse a template-id.
13676
13677    template-id:
13678      template-name < template-argument-list [opt] >
13679
13680    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13681    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
13682    returned.  Otherwise, if the template-name names a function, or set
13683    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
13684    names a class, returns a TYPE_DECL for the specialization.
13685
13686    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13687    uninstantiated templates.  */
13688
13689 static tree
13690 cp_parser_template_id (cp_parser *parser,
13691                        bool template_keyword_p,
13692                        bool check_dependency_p,
13693                        enum tag_types tag_type,
13694                        bool is_declaration)
13695 {
13696   int i;
13697   tree templ;
13698   tree arguments;
13699   tree template_id;
13700   cp_token_position start_of_id = 0;
13701   deferred_access_check *chk;
13702   vec<deferred_access_check, va_gc> *access_check;
13703   cp_token *next_token = NULL, *next_token_2 = NULL;
13704   bool is_identifier;
13705
13706   /* If the next token corresponds to a template-id, there is no need
13707      to reparse it.  */
13708   next_token = cp_lexer_peek_token (parser->lexer);
13709   if (next_token->type == CPP_TEMPLATE_ID)
13710     {
13711       struct tree_check *check_value;
13712
13713       /* Get the stored value.  */
13714       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13715       /* Perform any access checks that were deferred.  */
13716       access_check = check_value->checks;
13717       if (access_check)
13718         {
13719           FOR_EACH_VEC_ELT (*access_check, i, chk)
13720             perform_or_defer_access_check (chk->binfo,
13721                                            chk->decl,
13722                                            chk->diag_decl,
13723                                            tf_warning_or_error);
13724         }
13725       /* Return the stored value.  */
13726       return check_value->value;
13727     }
13728
13729   /* Avoid performing name lookup if there is no possibility of
13730      finding a template-id.  */
13731   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13732       || (next_token->type == CPP_NAME
13733           && !cp_parser_nth_token_starts_template_argument_list_p
13734                (parser, 2)))
13735     {
13736       cp_parser_error (parser, "expected template-id");
13737       return error_mark_node;
13738     }
13739
13740   /* Remember where the template-id starts.  */
13741   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13742     start_of_id = cp_lexer_token_position (parser->lexer, false);
13743
13744   push_deferring_access_checks (dk_deferred);
13745
13746   /* Parse the template-name.  */
13747   is_identifier = false;
13748   templ = cp_parser_template_name (parser, template_keyword_p,
13749                                    check_dependency_p,
13750                                    is_declaration,
13751                                    tag_type,
13752                                    &is_identifier);
13753   if (templ == error_mark_node || is_identifier)
13754     {
13755       pop_deferring_access_checks ();
13756       return templ;
13757     }
13758
13759   /* If we find the sequence `[:' after a template-name, it's probably
13760      a digraph-typo for `< ::'. Substitute the tokens and check if we can
13761      parse correctly the argument list.  */
13762   next_token = cp_lexer_peek_token (parser->lexer);
13763   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13764   if (next_token->type == CPP_OPEN_SQUARE
13765       && next_token->flags & DIGRAPH
13766       && next_token_2->type == CPP_COLON
13767       && !(next_token_2->flags & PREV_WHITE))
13768     {
13769       cp_parser_parse_tentatively (parser);
13770       /* Change `:' into `::'.  */
13771       next_token_2->type = CPP_SCOPE;
13772       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13773          CPP_LESS.  */
13774       cp_lexer_consume_token (parser->lexer);
13775
13776       /* Parse the arguments.  */
13777       arguments = cp_parser_enclosed_template_argument_list (parser);
13778       if (!cp_parser_parse_definitely (parser))
13779         {
13780           /* If we couldn't parse an argument list, then we revert our changes
13781              and return simply an error. Maybe this is not a template-id
13782              after all.  */
13783           next_token_2->type = CPP_COLON;
13784           cp_parser_error (parser, "expected %<<%>");
13785           pop_deferring_access_checks ();
13786           return error_mark_node;
13787         }
13788       /* Otherwise, emit an error about the invalid digraph, but continue
13789          parsing because we got our argument list.  */
13790       if (permerror (next_token->location,
13791                      "%<<::%> cannot begin a template-argument list"))
13792         {
13793           static bool hint = false;
13794           inform (next_token->location,
13795                   "%<<:%> is an alternate spelling for %<[%>."
13796                   " Insert whitespace between %<<%> and %<::%>");
13797           if (!hint && !flag_permissive)
13798             {
13799               inform (next_token->location, "(if you use %<-fpermissive%> "
13800                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13801                       "accept your code)");
13802               hint = true;
13803             }
13804         }
13805     }
13806   else
13807     {
13808       /* Look for the `<' that starts the template-argument-list.  */
13809       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13810         {
13811           pop_deferring_access_checks ();
13812           return error_mark_node;
13813         }
13814       /* Parse the arguments.  */
13815       arguments = cp_parser_enclosed_template_argument_list (parser);
13816     }
13817
13818   /* Build a representation of the specialization.  */
13819   if (identifier_p (templ))
13820     template_id = build_min_nt_loc (next_token->location,
13821                                     TEMPLATE_ID_EXPR,
13822                                     templ, arguments);
13823   else if (DECL_TYPE_TEMPLATE_P (templ)
13824            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13825     {
13826       bool entering_scope;
13827       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13828          template (rather than some instantiation thereof) only if
13829          is not nested within some other construct.  For example, in
13830          "template <typename T> void f(T) { A<T>::", A<T> is just an
13831          instantiation of A.  */
13832       entering_scope = (template_parm_scope_p ()
13833                         && cp_lexer_next_token_is (parser->lexer,
13834                                                    CPP_SCOPE));
13835       template_id
13836         = finish_template_type (templ, arguments, entering_scope);
13837     }
13838   else if (variable_template_p (templ))
13839     {
13840       template_id = lookup_template_variable (templ, arguments);
13841     }
13842   else
13843     {
13844       /* If it's not a class-template or a template-template, it should be
13845          a function-template.  */
13846       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13847                    || TREE_CODE (templ) == OVERLOAD
13848                    || BASELINK_P (templ)));
13849
13850       template_id = lookup_template_function (templ, arguments);
13851     }
13852
13853   /* If parsing tentatively, replace the sequence of tokens that makes
13854      up the template-id with a CPP_TEMPLATE_ID token.  That way,
13855      should we re-parse the token stream, we will not have to repeat
13856      the effort required to do the parse, nor will we issue duplicate
13857      error messages about problems during instantiation of the
13858      template.  */
13859   if (start_of_id
13860       /* Don't do this if we had a parse error in a declarator; re-parsing
13861          might succeed if a name changes meaning (60361).  */
13862       && !(cp_parser_error_occurred (parser)
13863            && cp_parser_parsing_tentatively (parser)
13864            && parser->in_declarator_p))
13865     {
13866       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13867
13868       /* Reset the contents of the START_OF_ID token.  */
13869       token->type = CPP_TEMPLATE_ID;
13870       /* Retrieve any deferred checks.  Do not pop this access checks yet
13871          so the memory will not be reclaimed during token replacing below.  */
13872       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13873       token->u.tree_check_value->value = template_id;
13874       token->u.tree_check_value->checks = get_deferred_access_checks ();
13875       token->keyword = RID_MAX;
13876
13877       /* Purge all subsequent tokens.  */
13878       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13879
13880       /* ??? Can we actually assume that, if template_id ==
13881          error_mark_node, we will have issued a diagnostic to the
13882          user, as opposed to simply marking the tentative parse as
13883          failed?  */
13884       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13885         error_at (token->location, "parse error in template argument list");
13886     }
13887
13888   pop_to_parent_deferring_access_checks ();
13889   return template_id;
13890 }
13891
13892 /* Parse a template-name.
13893
13894    template-name:
13895      identifier
13896
13897    The standard should actually say:
13898
13899    template-name:
13900      identifier
13901      operator-function-id
13902
13903    A defect report has been filed about this issue.
13904
13905    A conversion-function-id cannot be a template name because they cannot
13906    be part of a template-id. In fact, looking at this code:
13907
13908    a.operator K<int>()
13909
13910    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13911    It is impossible to call a templated conversion-function-id with an
13912    explicit argument list, since the only allowed template parameter is
13913    the type to which it is converting.
13914
13915    If TEMPLATE_KEYWORD_P is true, then we have just seen the
13916    `template' keyword, in a construction like:
13917
13918      T::template f<3>()
13919
13920    In that case `f' is taken to be a template-name, even though there
13921    is no way of knowing for sure.
13922
13923    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13924    name refers to a set of overloaded functions, at least one of which
13925    is a template, or an IDENTIFIER_NODE with the name of the template,
13926    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
13927    names are looked up inside uninstantiated templates.  */
13928
13929 static tree
13930 cp_parser_template_name (cp_parser* parser,
13931                          bool template_keyword_p,
13932                          bool check_dependency_p,
13933                          bool is_declaration,
13934                          enum tag_types tag_type,
13935                          bool *is_identifier)
13936 {
13937   tree identifier;
13938   tree decl;
13939   tree fns;
13940   cp_token *token = cp_lexer_peek_token (parser->lexer);
13941
13942   /* If the next token is `operator', then we have either an
13943      operator-function-id or a conversion-function-id.  */
13944   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13945     {
13946       /* We don't know whether we're looking at an
13947          operator-function-id or a conversion-function-id.  */
13948       cp_parser_parse_tentatively (parser);
13949       /* Try an operator-function-id.  */
13950       identifier = cp_parser_operator_function_id (parser);
13951       /* If that didn't work, try a conversion-function-id.  */
13952       if (!cp_parser_parse_definitely (parser))
13953         {
13954           cp_parser_error (parser, "expected template-name");
13955           return error_mark_node;
13956         }
13957     }
13958   /* Look for the identifier.  */
13959   else
13960     identifier = cp_parser_identifier (parser);
13961
13962   /* If we didn't find an identifier, we don't have a template-id.  */
13963   if (identifier == error_mark_node)
13964     return error_mark_node;
13965
13966   /* If the name immediately followed the `template' keyword, then it
13967      is a template-name.  However, if the next token is not `<', then
13968      we do not treat it as a template-name, since it is not being used
13969      as part of a template-id.  This enables us to handle constructs
13970      like:
13971
13972        template <typename T> struct S { S(); };
13973        template <typename T> S<T>::S();
13974
13975      correctly.  We would treat `S' as a template -- if it were `S<T>'
13976      -- but we do not if there is no `<'.  */
13977
13978   if (processing_template_decl
13979       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13980     {
13981       /* In a declaration, in a dependent context, we pretend that the
13982          "template" keyword was present in order to improve error
13983          recovery.  For example, given:
13984
13985            template <typename T> void f(T::X<int>);
13986
13987          we want to treat "X<int>" as a template-id.  */
13988       if (is_declaration
13989           && !template_keyword_p
13990           && parser->scope && TYPE_P (parser->scope)
13991           && check_dependency_p
13992           && dependent_scope_p (parser->scope)
13993           /* Do not do this for dtors (or ctors), since they never
13994              need the template keyword before their name.  */
13995           && !constructor_name_p (identifier, parser->scope))
13996         {
13997           cp_token_position start = 0;
13998
13999           /* Explain what went wrong.  */
14000           error_at (token->location, "non-template %qD used as template",
14001                     identifier);
14002           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14003                   parser->scope, identifier);
14004           /* If parsing tentatively, find the location of the "<" token.  */
14005           if (cp_parser_simulate_error (parser))
14006             start = cp_lexer_token_position (parser->lexer, true);
14007           /* Parse the template arguments so that we can issue error
14008              messages about them.  */
14009           cp_lexer_consume_token (parser->lexer);
14010           cp_parser_enclosed_template_argument_list (parser);
14011           /* Skip tokens until we find a good place from which to
14012              continue parsing.  */
14013           cp_parser_skip_to_closing_parenthesis (parser,
14014                                                  /*recovering=*/true,
14015                                                  /*or_comma=*/true,
14016                                                  /*consume_paren=*/false);
14017           /* If parsing tentatively, permanently remove the
14018              template argument list.  That will prevent duplicate
14019              error messages from being issued about the missing
14020              "template" keyword.  */
14021           if (start)
14022             cp_lexer_purge_tokens_after (parser->lexer, start);
14023           if (is_identifier)
14024             *is_identifier = true;
14025           return identifier;
14026         }
14027
14028       /* If the "template" keyword is present, then there is generally
14029          no point in doing name-lookup, so we just return IDENTIFIER.
14030          But, if the qualifying scope is non-dependent then we can
14031          (and must) do name-lookup normally.  */
14032       if (template_keyword_p
14033           && (!parser->scope
14034               || (TYPE_P (parser->scope)
14035                   && dependent_type_p (parser->scope))))
14036         return identifier;
14037     }
14038
14039   /* Look up the name.  */
14040   decl = cp_parser_lookup_name (parser, identifier,
14041                                 tag_type,
14042                                 /*is_template=*/true,
14043                                 /*is_namespace=*/false,
14044                                 check_dependency_p,
14045                                 /*ambiguous_decls=*/NULL,
14046                                 token->location);
14047
14048   /* If DECL is a template, then the name was a template-name.  */
14049   if (TREE_CODE (decl) == TEMPLATE_DECL)
14050     {
14051       if (TREE_DEPRECATED (decl)
14052           && deprecated_state != DEPRECATED_SUPPRESS)
14053         warn_deprecated_use (decl, NULL_TREE);
14054     }
14055   else
14056     {
14057       tree fn = NULL_TREE;
14058
14059       /* The standard does not explicitly indicate whether a name that
14060          names a set of overloaded declarations, some of which are
14061          templates, is a template-name.  However, such a name should
14062          be a template-name; otherwise, there is no way to form a
14063          template-id for the overloaded templates.  */
14064       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14065       if (TREE_CODE (fns) == OVERLOAD)
14066         for (fn = fns; fn; fn = OVL_NEXT (fn))
14067           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14068             break;
14069
14070       if (!fn)
14071         {
14072           /* The name does not name a template.  */
14073           cp_parser_error (parser, "expected template-name");
14074           return error_mark_node;
14075         }
14076     }
14077
14078   /* If DECL is dependent, and refers to a function, then just return
14079      its name; we will look it up again during template instantiation.  */
14080   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14081     {
14082       tree scope = ovl_scope (decl);
14083       if (TYPE_P (scope) && dependent_type_p (scope))
14084         return identifier;
14085     }
14086
14087   return decl;
14088 }
14089
14090 /* Parse a template-argument-list.
14091
14092    template-argument-list:
14093      template-argument ... [opt]
14094      template-argument-list , template-argument ... [opt]
14095
14096    Returns a TREE_VEC containing the arguments.  */
14097
14098 static tree
14099 cp_parser_template_argument_list (cp_parser* parser)
14100 {
14101   tree fixed_args[10];
14102   unsigned n_args = 0;
14103   unsigned alloced = 10;
14104   tree *arg_ary = fixed_args;
14105   tree vec;
14106   bool saved_in_template_argument_list_p;
14107   bool saved_ice_p;
14108   bool saved_non_ice_p;
14109
14110   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14111   parser->in_template_argument_list_p = true;
14112   /* Even if the template-id appears in an integral
14113      constant-expression, the contents of the argument list do
14114      not.  */
14115   saved_ice_p = parser->integral_constant_expression_p;
14116   parser->integral_constant_expression_p = false;
14117   saved_non_ice_p = parser->non_integral_constant_expression_p;
14118   parser->non_integral_constant_expression_p = false;
14119
14120   /* Parse the arguments.  */
14121   do
14122     {
14123       tree argument;
14124
14125       if (n_args)
14126         /* Consume the comma.  */
14127         cp_lexer_consume_token (parser->lexer);
14128
14129       /* Parse the template-argument.  */
14130       argument = cp_parser_template_argument (parser);
14131
14132       /* If the next token is an ellipsis, we're expanding a template
14133          argument pack. */
14134       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14135         {
14136           if (argument == error_mark_node)
14137             {
14138               cp_token *token = cp_lexer_peek_token (parser->lexer);
14139               error_at (token->location,
14140                         "expected parameter pack before %<...%>");
14141             }
14142           /* Consume the `...' token. */
14143           cp_lexer_consume_token (parser->lexer);
14144
14145           /* Make the argument into a TYPE_PACK_EXPANSION or
14146              EXPR_PACK_EXPANSION. */
14147           argument = make_pack_expansion (argument);
14148         }
14149
14150       if (n_args == alloced)
14151         {
14152           alloced *= 2;
14153
14154           if (arg_ary == fixed_args)
14155             {
14156               arg_ary = XNEWVEC (tree, alloced);
14157               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14158             }
14159           else
14160             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14161         }
14162       arg_ary[n_args++] = argument;
14163     }
14164   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14165
14166   vec = make_tree_vec (n_args);
14167
14168   while (n_args--)
14169     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14170
14171   if (arg_ary != fixed_args)
14172     free (arg_ary);
14173   parser->non_integral_constant_expression_p = saved_non_ice_p;
14174   parser->integral_constant_expression_p = saved_ice_p;
14175   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14176 #ifdef ENABLE_CHECKING
14177   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14178 #endif
14179   return vec;
14180 }
14181
14182 /* Parse a template-argument.
14183
14184    template-argument:
14185      assignment-expression
14186      type-id
14187      id-expression
14188
14189    The representation is that of an assignment-expression, type-id, or
14190    id-expression -- except that the qualified id-expression is
14191    evaluated, so that the value returned is either a DECL or an
14192    OVERLOAD.
14193
14194    Although the standard says "assignment-expression", it forbids
14195    throw-expressions or assignments in the template argument.
14196    Therefore, we use "conditional-expression" instead.  */
14197
14198 static tree
14199 cp_parser_template_argument (cp_parser* parser)
14200 {
14201   tree argument;
14202   bool template_p;
14203   bool address_p;
14204   bool maybe_type_id = false;
14205   cp_token *token = NULL, *argument_start_token = NULL;
14206   location_t loc = 0;
14207   cp_id_kind idk;
14208
14209   /* There's really no way to know what we're looking at, so we just
14210      try each alternative in order.
14211
14212        [temp.arg]
14213
14214        In a template-argument, an ambiguity between a type-id and an
14215        expression is resolved to a type-id, regardless of the form of
14216        the corresponding template-parameter.
14217
14218      Therefore, we try a type-id first.  */
14219   cp_parser_parse_tentatively (parser);
14220   argument = cp_parser_template_type_arg (parser);
14221   /* If there was no error parsing the type-id but the next token is a
14222      '>>', our behavior depends on which dialect of C++ we're
14223      parsing. In C++98, we probably found a typo for '> >'. But there
14224      are type-id which are also valid expressions. For instance:
14225
14226      struct X { int operator >> (int); };
14227      template <int V> struct Foo {};
14228      Foo<X () >> 5> r;
14229
14230      Here 'X()' is a valid type-id of a function type, but the user just
14231      wanted to write the expression "X() >> 5". Thus, we remember that we
14232      found a valid type-id, but we still try to parse the argument as an
14233      expression to see what happens. 
14234
14235      In C++0x, the '>>' will be considered two separate '>'
14236      tokens.  */
14237   if (!cp_parser_error_occurred (parser)
14238       && cxx_dialect == cxx98
14239       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14240     {
14241       maybe_type_id = true;
14242       cp_parser_abort_tentative_parse (parser);
14243     }
14244   else
14245     {
14246       /* If the next token isn't a `,' or a `>', then this argument wasn't
14247       really finished. This means that the argument is not a valid
14248       type-id.  */
14249       if (!cp_parser_next_token_ends_template_argument_p (parser))
14250         cp_parser_error (parser, "expected template-argument");
14251       /* If that worked, we're done.  */
14252       if (cp_parser_parse_definitely (parser))
14253         return argument;
14254     }
14255   /* We're still not sure what the argument will be.  */
14256   cp_parser_parse_tentatively (parser);
14257   /* Try a template.  */
14258   argument_start_token = cp_lexer_peek_token (parser->lexer);
14259   argument = cp_parser_id_expression (parser,
14260                                       /*template_keyword_p=*/false,
14261                                       /*check_dependency_p=*/true,
14262                                       &template_p,
14263                                       /*declarator_p=*/false,
14264                                       /*optional_p=*/false);
14265   /* If the next token isn't a `,' or a `>', then this argument wasn't
14266      really finished.  */
14267   if (!cp_parser_next_token_ends_template_argument_p (parser))
14268     cp_parser_error (parser, "expected template-argument");
14269   if (!cp_parser_error_occurred (parser))
14270     {
14271       /* Figure out what is being referred to.  If the id-expression
14272          was for a class template specialization, then we will have a
14273          TYPE_DECL at this point.  There is no need to do name lookup
14274          at this point in that case.  */
14275       if (TREE_CODE (argument) != TYPE_DECL)
14276         argument = cp_parser_lookup_name (parser, argument,
14277                                           none_type,
14278                                           /*is_template=*/template_p,
14279                                           /*is_namespace=*/false,
14280                                           /*check_dependency=*/true,
14281                                           /*ambiguous_decls=*/NULL,
14282                                           argument_start_token->location);
14283       if (TREE_CODE (argument) != TEMPLATE_DECL
14284           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14285         cp_parser_error (parser, "expected template-name");
14286     }
14287   if (cp_parser_parse_definitely (parser))
14288     {
14289       if (TREE_DEPRECATED (argument))
14290         warn_deprecated_use (argument, NULL_TREE);
14291       return argument;
14292     }
14293   /* It must be a non-type argument.  There permitted cases are given
14294      in [temp.arg.nontype]:
14295
14296      -- an integral constant-expression of integral or enumeration
14297         type; or
14298
14299      -- the name of a non-type template-parameter; or
14300
14301      -- the name of an object or function with external linkage...
14302
14303      -- the address of an object or function with external linkage...
14304
14305      -- a pointer to member...  */
14306   /* Look for a non-type template parameter.  */
14307   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14308     {
14309       cp_parser_parse_tentatively (parser);
14310       argument = cp_parser_primary_expression (parser,
14311                                                /*address_p=*/false,
14312                                                /*cast_p=*/false,
14313                                                /*template_arg_p=*/true,
14314                                                &idk);
14315       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14316           || !cp_parser_next_token_ends_template_argument_p (parser))
14317         cp_parser_simulate_error (parser);
14318       if (cp_parser_parse_definitely (parser))
14319         return argument;
14320     }
14321
14322   /* If the next token is "&", the argument must be the address of an
14323      object or function with external linkage.  */
14324   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14325   if (address_p)
14326     {
14327       loc = cp_lexer_peek_token (parser->lexer)->location;
14328       cp_lexer_consume_token (parser->lexer);
14329     }
14330   /* See if we might have an id-expression.  */
14331   token = cp_lexer_peek_token (parser->lexer);
14332   if (token->type == CPP_NAME
14333       || token->keyword == RID_OPERATOR
14334       || token->type == CPP_SCOPE
14335       || token->type == CPP_TEMPLATE_ID
14336       || token->type == CPP_NESTED_NAME_SPECIFIER)
14337     {
14338       cp_parser_parse_tentatively (parser);
14339       argument = cp_parser_primary_expression (parser,
14340                                                address_p,
14341                                                /*cast_p=*/false,
14342                                                /*template_arg_p=*/true,
14343                                                &idk);
14344       if (cp_parser_error_occurred (parser)
14345           || !cp_parser_next_token_ends_template_argument_p (parser))
14346         cp_parser_abort_tentative_parse (parser);
14347       else
14348         {
14349           tree probe;
14350
14351           if (INDIRECT_REF_P (argument))
14352             {
14353               /* Strip the dereference temporarily.  */
14354               gcc_assert (REFERENCE_REF_P (argument));
14355               argument = TREE_OPERAND (argument, 0);
14356             }
14357
14358           /* If we're in a template, we represent a qualified-id referring
14359              to a static data member as a SCOPE_REF even if the scope isn't
14360              dependent so that we can check access control later.  */
14361           probe = argument;
14362           if (TREE_CODE (probe) == SCOPE_REF)
14363             probe = TREE_OPERAND (probe, 1);
14364           if (VAR_P (probe))
14365             {
14366               /* A variable without external linkage might still be a
14367                  valid constant-expression, so no error is issued here
14368                  if the external-linkage check fails.  */
14369               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14370                 cp_parser_simulate_error (parser);
14371             }
14372           else if (is_overloaded_fn (argument))
14373             /* All overloaded functions are allowed; if the external
14374                linkage test does not pass, an error will be issued
14375                later.  */
14376             ;
14377           else if (address_p
14378                    && (TREE_CODE (argument) == OFFSET_REF
14379                        || TREE_CODE (argument) == SCOPE_REF))
14380             /* A pointer-to-member.  */
14381             ;
14382           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14383             ;
14384           else
14385             cp_parser_simulate_error (parser);
14386
14387           if (cp_parser_parse_definitely (parser))
14388             {
14389               if (address_p)
14390                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14391                                              tf_warning_or_error);
14392               else
14393                 argument = convert_from_reference (argument);
14394               return argument;
14395             }
14396         }
14397     }
14398   /* If the argument started with "&", there are no other valid
14399      alternatives at this point.  */
14400   if (address_p)
14401     {
14402       cp_parser_error (parser, "invalid non-type template argument");
14403       return error_mark_node;
14404     }
14405
14406   /* If the argument wasn't successfully parsed as a type-id followed
14407      by '>>', the argument can only be a constant expression now.
14408      Otherwise, we try parsing the constant-expression tentatively,
14409      because the argument could really be a type-id.  */
14410   if (maybe_type_id)
14411     cp_parser_parse_tentatively (parser);
14412   argument = cp_parser_constant_expression (parser);
14413
14414   if (!maybe_type_id)
14415     return argument;
14416   if (!cp_parser_next_token_ends_template_argument_p (parser))
14417     cp_parser_error (parser, "expected template-argument");
14418   if (cp_parser_parse_definitely (parser))
14419     return argument;
14420   /* We did our best to parse the argument as a non type-id, but that
14421      was the only alternative that matched (albeit with a '>' after
14422      it). We can assume it's just a typo from the user, and a
14423      diagnostic will then be issued.  */
14424   return cp_parser_template_type_arg (parser);
14425 }
14426
14427 /* Parse an explicit-instantiation.
14428
14429    explicit-instantiation:
14430      template declaration
14431
14432    Although the standard says `declaration', what it really means is:
14433
14434    explicit-instantiation:
14435      template decl-specifier-seq [opt] declarator [opt] ;
14436
14437    Things like `template int S<int>::i = 5, int S<double>::j;' are not
14438    supposed to be allowed.  A defect report has been filed about this
14439    issue.
14440
14441    GNU Extension:
14442
14443    explicit-instantiation:
14444      storage-class-specifier template
14445        decl-specifier-seq [opt] declarator [opt] ;
14446      function-specifier template
14447        decl-specifier-seq [opt] declarator [opt] ;  */
14448
14449 static void
14450 cp_parser_explicit_instantiation (cp_parser* parser)
14451 {
14452   int declares_class_or_enum;
14453   cp_decl_specifier_seq decl_specifiers;
14454   tree extension_specifier = NULL_TREE;
14455
14456   timevar_push (TV_TEMPLATE_INST);
14457
14458   /* Look for an (optional) storage-class-specifier or
14459      function-specifier.  */
14460   if (cp_parser_allow_gnu_extensions_p (parser))
14461     {
14462       extension_specifier
14463         = cp_parser_storage_class_specifier_opt (parser);
14464       if (!extension_specifier)
14465         extension_specifier
14466           = cp_parser_function_specifier_opt (parser,
14467                                               /*decl_specs=*/NULL);
14468     }
14469
14470   /* Look for the `template' keyword.  */
14471   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14472   /* Let the front end know that we are processing an explicit
14473      instantiation.  */
14474   begin_explicit_instantiation ();
14475   /* [temp.explicit] says that we are supposed to ignore access
14476      control while processing explicit instantiation directives.  */
14477   push_deferring_access_checks (dk_no_check);
14478   /* Parse a decl-specifier-seq.  */
14479   cp_parser_decl_specifier_seq (parser,
14480                                 CP_PARSER_FLAGS_OPTIONAL,
14481                                 &decl_specifiers,
14482                                 &declares_class_or_enum);
14483   /* If there was exactly one decl-specifier, and it declared a class,
14484      and there's no declarator, then we have an explicit type
14485      instantiation.  */
14486   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14487     {
14488       tree type;
14489
14490       type = check_tag_decl (&decl_specifiers,
14491                              /*explicit_type_instantiation_p=*/true);
14492       /* Turn access control back on for names used during
14493          template instantiation.  */
14494       pop_deferring_access_checks ();
14495       if (type)
14496         do_type_instantiation (type, extension_specifier,
14497                                /*complain=*/tf_error);
14498     }
14499   else
14500     {
14501       cp_declarator *declarator;
14502       tree decl;
14503
14504       /* Parse the declarator.  */
14505       declarator
14506         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14507                                 /*ctor_dtor_or_conv_p=*/NULL,
14508                                 /*parenthesized_p=*/NULL,
14509                                 /*member_p=*/false,
14510                                 /*friend_p=*/false);
14511       if (declares_class_or_enum & 2)
14512         cp_parser_check_for_definition_in_return_type (declarator,
14513                                                        decl_specifiers.type,
14514                                                        decl_specifiers.locations[ds_type_spec]);
14515       if (declarator != cp_error_declarator)
14516         {
14517           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14518             permerror (decl_specifiers.locations[ds_inline],
14519                        "explicit instantiation shall not use"
14520                        " %<inline%> specifier");
14521           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14522             permerror (decl_specifiers.locations[ds_constexpr],
14523                        "explicit instantiation shall not use"
14524                        " %<constexpr%> specifier");
14525
14526           decl = grokdeclarator (declarator, &decl_specifiers,
14527                                  NORMAL, 0, &decl_specifiers.attributes);
14528           /* Turn access control back on for names used during
14529              template instantiation.  */
14530           pop_deferring_access_checks ();
14531           /* Do the explicit instantiation.  */
14532           do_decl_instantiation (decl, extension_specifier);
14533         }
14534       else
14535         {
14536           pop_deferring_access_checks ();
14537           /* Skip the body of the explicit instantiation.  */
14538           cp_parser_skip_to_end_of_statement (parser);
14539         }
14540     }
14541   /* We're done with the instantiation.  */
14542   end_explicit_instantiation ();
14543
14544   cp_parser_consume_semicolon_at_end_of_statement (parser);
14545
14546   timevar_pop (TV_TEMPLATE_INST);
14547 }
14548
14549 /* Parse an explicit-specialization.
14550
14551    explicit-specialization:
14552      template < > declaration
14553
14554    Although the standard says `declaration', what it really means is:
14555
14556    explicit-specialization:
14557      template <> decl-specifier [opt] init-declarator [opt] ;
14558      template <> function-definition
14559      template <> explicit-specialization
14560      template <> template-declaration  */
14561
14562 static void
14563 cp_parser_explicit_specialization (cp_parser* parser)
14564 {
14565   bool need_lang_pop;
14566   cp_token *token = cp_lexer_peek_token (parser->lexer);
14567
14568   /* Look for the `template' keyword.  */
14569   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14570   /* Look for the `<'.  */
14571   cp_parser_require (parser, CPP_LESS, RT_LESS);
14572   /* Look for the `>'.  */
14573   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14574   /* We have processed another parameter list.  */
14575   ++parser->num_template_parameter_lists;
14576   /* [temp]
14577
14578      A template ... explicit specialization ... shall not have C
14579      linkage.  */
14580   if (current_lang_name == lang_name_c)
14581     {
14582       error_at (token->location, "template specialization with C linkage");
14583       /* Give it C++ linkage to avoid confusing other parts of the
14584          front end.  */
14585       push_lang_context (lang_name_cplusplus);
14586       need_lang_pop = true;
14587     }
14588   else
14589     need_lang_pop = false;
14590   /* Let the front end know that we are beginning a specialization.  */
14591   if (!begin_specialization ())
14592     {
14593       end_specialization ();
14594       return;
14595     }
14596
14597   /* If the next keyword is `template', we need to figure out whether
14598      or not we're looking a template-declaration.  */
14599   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14600     {
14601       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14602           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14603         cp_parser_template_declaration_after_export (parser,
14604                                                      /*member_p=*/false);
14605       else
14606         cp_parser_explicit_specialization (parser);
14607     }
14608   else
14609     /* Parse the dependent declaration.  */
14610     cp_parser_single_declaration (parser,
14611                                   /*checks=*/NULL,
14612                                   /*member_p=*/false,
14613                                   /*explicit_specialization_p=*/true,
14614                                   /*friend_p=*/NULL);
14615   /* We're done with the specialization.  */
14616   end_specialization ();
14617   /* For the erroneous case of a template with C linkage, we pushed an
14618      implicit C++ linkage scope; exit that scope now.  */
14619   if (need_lang_pop)
14620     pop_lang_context ();
14621   /* We're done with this parameter list.  */
14622   --parser->num_template_parameter_lists;
14623 }
14624
14625 /* Parse a type-specifier.
14626
14627    type-specifier:
14628      simple-type-specifier
14629      class-specifier
14630      enum-specifier
14631      elaborated-type-specifier
14632      cv-qualifier
14633
14634    GNU Extension:
14635
14636    type-specifier:
14637      __complex__
14638
14639    Returns a representation of the type-specifier.  For a
14640    class-specifier, enum-specifier, or elaborated-type-specifier, a
14641    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14642
14643    The parser flags FLAGS is used to control type-specifier parsing.
14644
14645    If IS_DECLARATION is TRUE, then this type-specifier is appearing
14646    in a decl-specifier-seq.
14647
14648    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14649    class-specifier, enum-specifier, or elaborated-type-specifier, then
14650    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
14651    if a type is declared; 2 if it is defined.  Otherwise, it is set to
14652    zero.
14653
14654    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14655    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
14656    is set to FALSE.  */
14657
14658 static tree
14659 cp_parser_type_specifier (cp_parser* parser,
14660                           cp_parser_flags flags,
14661                           cp_decl_specifier_seq *decl_specs,
14662                           bool is_declaration,
14663                           int* declares_class_or_enum,
14664                           bool* is_cv_qualifier)
14665 {
14666   tree type_spec = NULL_TREE;
14667   cp_token *token;
14668   enum rid keyword;
14669   cp_decl_spec ds = ds_last;
14670
14671   /* Assume this type-specifier does not declare a new type.  */
14672   if (declares_class_or_enum)
14673     *declares_class_or_enum = 0;
14674   /* And that it does not specify a cv-qualifier.  */
14675   if (is_cv_qualifier)
14676     *is_cv_qualifier = false;
14677   /* Peek at the next token.  */
14678   token = cp_lexer_peek_token (parser->lexer);
14679
14680   /* If we're looking at a keyword, we can use that to guide the
14681      production we choose.  */
14682   keyword = token->keyword;
14683   switch (keyword)
14684     {
14685     case RID_ENUM:
14686       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14687         goto elaborated_type_specifier;
14688
14689       /* Look for the enum-specifier.  */
14690       type_spec = cp_parser_enum_specifier (parser);
14691       /* If that worked, we're done.  */
14692       if (type_spec)
14693         {
14694           if (declares_class_or_enum)
14695             *declares_class_or_enum = 2;
14696           if (decl_specs)
14697             cp_parser_set_decl_spec_type (decl_specs,
14698                                           type_spec,
14699                                           token,
14700                                           /*type_definition_p=*/true);
14701           return type_spec;
14702         }
14703       else
14704         goto elaborated_type_specifier;
14705
14706       /* Any of these indicate either a class-specifier, or an
14707          elaborated-type-specifier.  */
14708     case RID_CLASS:
14709     case RID_STRUCT:
14710     case RID_UNION:
14711       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14712         goto elaborated_type_specifier;
14713
14714       /* Parse tentatively so that we can back up if we don't find a
14715          class-specifier.  */
14716       cp_parser_parse_tentatively (parser);
14717       /* Look for the class-specifier.  */
14718       type_spec = cp_parser_class_specifier (parser);
14719       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14720       /* If that worked, we're done.  */
14721       if (cp_parser_parse_definitely (parser))
14722         {
14723           if (declares_class_or_enum)
14724             *declares_class_or_enum = 2;
14725           if (decl_specs)
14726             cp_parser_set_decl_spec_type (decl_specs,
14727                                           type_spec,
14728                                           token,
14729                                           /*type_definition_p=*/true);
14730           return type_spec;
14731         }
14732
14733       /* Fall through.  */
14734     elaborated_type_specifier:
14735       /* We're declaring (not defining) a class or enum.  */
14736       if (declares_class_or_enum)
14737         *declares_class_or_enum = 1;
14738
14739       /* Fall through.  */
14740     case RID_TYPENAME:
14741       /* Look for an elaborated-type-specifier.  */
14742       type_spec
14743         = (cp_parser_elaborated_type_specifier
14744            (parser,
14745             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14746             is_declaration));
14747       if (decl_specs)
14748         cp_parser_set_decl_spec_type (decl_specs,
14749                                       type_spec,
14750                                       token,
14751                                       /*type_definition_p=*/false);
14752       return type_spec;
14753
14754     case RID_CONST:
14755       ds = ds_const;
14756       if (is_cv_qualifier)
14757         *is_cv_qualifier = true;
14758       break;
14759
14760     case RID_VOLATILE:
14761       ds = ds_volatile;
14762       if (is_cv_qualifier)
14763         *is_cv_qualifier = true;
14764       break;
14765
14766     case RID_RESTRICT:
14767       ds = ds_restrict;
14768       if (is_cv_qualifier)
14769         *is_cv_qualifier = true;
14770       break;
14771
14772     case RID_COMPLEX:
14773       /* The `__complex__' keyword is a GNU extension.  */
14774       ds = ds_complex;
14775       break;
14776
14777     default:
14778       break;
14779     }
14780
14781   /* Handle simple keywords.  */
14782   if (ds != ds_last)
14783     {
14784       if (decl_specs)
14785         {
14786           set_and_check_decl_spec_loc (decl_specs, ds, token);
14787           decl_specs->any_specifiers_p = true;
14788         }
14789       return cp_lexer_consume_token (parser->lexer)->u.value;
14790     }
14791
14792   /* If we do not already have a type-specifier, assume we are looking
14793      at a simple-type-specifier.  */
14794   type_spec = cp_parser_simple_type_specifier (parser,
14795                                                decl_specs,
14796                                                flags);
14797
14798   /* If we didn't find a type-specifier, and a type-specifier was not
14799      optional in this context, issue an error message.  */
14800   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14801     {
14802       cp_parser_error (parser, "expected type specifier");
14803       return error_mark_node;
14804     }
14805
14806   return type_spec;
14807 }
14808
14809 /* Parse a simple-type-specifier.
14810
14811    simple-type-specifier:
14812      :: [opt] nested-name-specifier [opt] type-name
14813      :: [opt] nested-name-specifier template template-id
14814      char
14815      wchar_t
14816      bool
14817      short
14818      int
14819      long
14820      signed
14821      unsigned
14822      float
14823      double
14824      void
14825
14826    C++0x Extension:
14827
14828    simple-type-specifier:
14829      auto
14830      decltype ( expression )   
14831      char16_t
14832      char32_t
14833      __underlying_type ( type-id )
14834
14835    GNU Extension:
14836
14837    simple-type-specifier:
14838      __int128
14839      __typeof__ unary-expression
14840      __typeof__ ( type-id )
14841      __typeof__ ( type-id ) { initializer-list , [opt] }
14842
14843    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
14844    appropriately updated.  */
14845
14846 static tree
14847 cp_parser_simple_type_specifier (cp_parser* parser,
14848                                  cp_decl_specifier_seq *decl_specs,
14849                                  cp_parser_flags flags)
14850 {
14851   tree type = NULL_TREE;
14852   cp_token *token;
14853   int idx;
14854
14855   /* Peek at the next token.  */
14856   token = cp_lexer_peek_token (parser->lexer);
14857
14858   /* If we're looking at a keyword, things are easy.  */
14859   switch (token->keyword)
14860     {
14861     case RID_CHAR:
14862       if (decl_specs)
14863         decl_specs->explicit_char_p = true;
14864       type = char_type_node;
14865       break;
14866     case RID_CHAR16:
14867       type = char16_type_node;
14868       break;
14869     case RID_CHAR32:
14870       type = char32_type_node;
14871       break;
14872     case RID_WCHAR:
14873       type = wchar_type_node;
14874       break;
14875     case RID_BOOL:
14876       type = boolean_type_node;
14877       break;
14878     case RID_SHORT:
14879       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14880       type = short_integer_type_node;
14881       break;
14882     case RID_INT:
14883       if (decl_specs)
14884         decl_specs->explicit_int_p = true;
14885       type = integer_type_node;
14886       break;
14887     case RID_INT_N_0:
14888     case RID_INT_N_1:
14889     case RID_INT_N_2:
14890     case RID_INT_N_3:
14891       idx = token->keyword - RID_INT_N_0;
14892       if (! int_n_enabled_p [idx])
14893         break;
14894       if (decl_specs)
14895         {
14896           decl_specs->explicit_intN_p = true;
14897           decl_specs->int_n_idx = idx;
14898         }
14899       type = int_n_trees [idx].signed_type;
14900       break;
14901     case RID_LONG:
14902       if (decl_specs)
14903         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14904       type = long_integer_type_node;
14905       break;
14906     case RID_SIGNED:
14907       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14908       type = integer_type_node;
14909       break;
14910     case RID_UNSIGNED:
14911       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14912       type = unsigned_type_node;
14913       break;
14914     case RID_FLOAT:
14915       type = float_type_node;
14916       break;
14917     case RID_DOUBLE:
14918       type = double_type_node;
14919       break;
14920     case RID_VOID:
14921       type = void_type_node;
14922       break;
14923
14924     case RID_AUTO:
14925       maybe_warn_cpp0x (CPP0X_AUTO);
14926       if (parser->auto_is_implicit_function_template_parm_p)
14927         {
14928           if (cxx_dialect >= cxx14)
14929             type = synthesize_implicit_template_parm (parser);
14930           else
14931             type = error_mark_node;
14932
14933           if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14934             {
14935               if (cxx_dialect < cxx14)
14936                 error_at (token->location,
14937                          "use of %<auto%> in lambda parameter declaration "
14938                          "only available with "
14939                          "-std=c++14 or -std=gnu++14");
14940             }
14941           else if (cxx_dialect < cxx14)
14942             error_at (token->location,
14943                      "use of %<auto%> in parameter declaration "
14944                      "only available with "
14945                      "-std=c++14 or -std=gnu++14");
14946           else
14947             pedwarn (token->location, OPT_Wpedantic,
14948                      "ISO C++ forbids use of %<auto%> in parameter "
14949                      "declaration");
14950         }
14951       else
14952         type = make_auto ();
14953       break;
14954
14955     case RID_DECLTYPE:
14956       /* Since DR 743, decltype can either be a simple-type-specifier by
14957          itself or begin a nested-name-specifier.  Parsing it will replace
14958          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14959          handling below decide what to do.  */
14960       cp_parser_decltype (parser);
14961       cp_lexer_set_token_position (parser->lexer, token);
14962       break;
14963
14964     case RID_TYPEOF:
14965       /* Consume the `typeof' token.  */
14966       cp_lexer_consume_token (parser->lexer);
14967       /* Parse the operand to `typeof'.  */
14968       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14969       /* If it is not already a TYPE, take its type.  */
14970       if (!TYPE_P (type))
14971         type = finish_typeof (type);
14972
14973       if (decl_specs)
14974         cp_parser_set_decl_spec_type (decl_specs, type,
14975                                       token,
14976                                       /*type_definition_p=*/false);
14977
14978       return type;
14979
14980     case RID_UNDERLYING_TYPE:
14981       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14982       if (decl_specs)
14983         cp_parser_set_decl_spec_type (decl_specs, type,
14984                                       token,
14985                                       /*type_definition_p=*/false);
14986
14987       return type;
14988
14989     case RID_BASES:
14990     case RID_DIRECT_BASES:
14991       type = cp_parser_trait_expr (parser, token->keyword);
14992       if (decl_specs)
14993        cp_parser_set_decl_spec_type (decl_specs, type,
14994                                      token,
14995                                      /*type_definition_p=*/false);
14996       return type;
14997     default:
14998       break;
14999     }
15000
15001   /* If token is an already-parsed decltype not followed by ::,
15002      it's a simple-type-specifier.  */
15003   if (token->type == CPP_DECLTYPE
15004       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15005     {
15006       type = token->u.value;
15007       if (decl_specs)
15008         {
15009           cp_parser_set_decl_spec_type (decl_specs, type,
15010                                         token,
15011                                         /*type_definition_p=*/false);
15012           /* Remember that we are handling a decltype in order to
15013              implement the resolution of DR 1510 when the argument
15014              isn't instantiation dependent.  */
15015           decl_specs->decltype_p = true;
15016         }
15017       cp_lexer_consume_token (parser->lexer);
15018       return type;
15019     }
15020
15021   /* If the type-specifier was for a built-in type, we're done.  */
15022   if (type)
15023     {
15024       /* Record the type.  */
15025       if (decl_specs
15026           && (token->keyword != RID_SIGNED
15027               && token->keyword != RID_UNSIGNED
15028               && token->keyword != RID_SHORT
15029               && token->keyword != RID_LONG))
15030         cp_parser_set_decl_spec_type (decl_specs,
15031                                       type,
15032                                       token,
15033                                       /*type_definition_p=*/false);
15034       if (decl_specs)
15035         decl_specs->any_specifiers_p = true;
15036
15037       /* Consume the token.  */
15038       cp_lexer_consume_token (parser->lexer);
15039
15040       if (type == error_mark_node)
15041         return error_mark_node;
15042
15043       /* There is no valid C++ program where a non-template type is
15044          followed by a "<".  That usually indicates that the user thought
15045          that the type was a template.  */
15046       cp_parser_check_for_invalid_template_id (parser, type, none_type,
15047                                                token->location);
15048
15049       return TYPE_NAME (type);
15050     }
15051
15052   /* The type-specifier must be a user-defined type.  */
15053   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15054     {
15055       bool qualified_p;
15056       bool global_p;
15057
15058       /* Don't gobble tokens or issue error messages if this is an
15059          optional type-specifier.  */
15060       if (flags & CP_PARSER_FLAGS_OPTIONAL)
15061         cp_parser_parse_tentatively (parser);
15062
15063       /* Look for the optional `::' operator.  */
15064       global_p
15065         = (cp_parser_global_scope_opt (parser,
15066                                        /*current_scope_valid_p=*/false)
15067            != NULL_TREE);
15068       /* Look for the nested-name specifier.  */
15069       qualified_p
15070         = (cp_parser_nested_name_specifier_opt (parser,
15071                                                 /*typename_keyword_p=*/false,
15072                                                 /*check_dependency_p=*/true,
15073                                                 /*type_p=*/false,
15074                                                 /*is_declaration=*/false)
15075            != NULL_TREE);
15076       token = cp_lexer_peek_token (parser->lexer);
15077       /* If we have seen a nested-name-specifier, and the next token
15078          is `template', then we are using the template-id production.  */
15079       if (parser->scope
15080           && cp_parser_optional_template_keyword (parser))
15081         {
15082           /* Look for the template-id.  */
15083           type = cp_parser_template_id (parser,
15084                                         /*template_keyword_p=*/true,
15085                                         /*check_dependency_p=*/true,
15086                                         none_type,
15087                                         /*is_declaration=*/false);
15088           /* If the template-id did not name a type, we are out of
15089              luck.  */
15090           if (TREE_CODE (type) != TYPE_DECL)
15091             {
15092               cp_parser_error (parser, "expected template-id for type");
15093               type = NULL_TREE;
15094             }
15095         }
15096       /* Otherwise, look for a type-name.  */
15097       else
15098         type = cp_parser_type_name (parser);
15099       /* Keep track of all name-lookups performed in class scopes.  */
15100       if (type
15101           && !global_p
15102           && !qualified_p
15103           && TREE_CODE (type) == TYPE_DECL
15104           && identifier_p (DECL_NAME (type)))
15105         maybe_note_name_used_in_class (DECL_NAME (type), type);
15106       /* If it didn't work out, we don't have a TYPE.  */
15107       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15108           && !cp_parser_parse_definitely (parser))
15109         type = NULL_TREE;
15110       if (type && decl_specs)
15111         cp_parser_set_decl_spec_type (decl_specs, type,
15112                                       token,
15113                                       /*type_definition_p=*/false);
15114     }
15115
15116   /* If we didn't get a type-name, issue an error message.  */
15117   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15118     {
15119       cp_parser_error (parser, "expected type-name");
15120       return error_mark_node;
15121     }
15122
15123   if (type && type != error_mark_node)
15124     {
15125       /* See if TYPE is an Objective-C type, and if so, parse and
15126          accept any protocol references following it.  Do this before
15127          the cp_parser_check_for_invalid_template_id() call, because
15128          Objective-C types can be followed by '<...>' which would
15129          enclose protocol names rather than template arguments, and so
15130          everything is fine.  */
15131       if (c_dialect_objc () && !parser->scope
15132           && (objc_is_id (type) || objc_is_class_name (type)))
15133         {
15134           tree protos = cp_parser_objc_protocol_refs_opt (parser);
15135           tree qual_type = objc_get_protocol_qualified_type (type, protos);
15136
15137           /* Clobber the "unqualified" type previously entered into
15138              DECL_SPECS with the new, improved protocol-qualified version.  */
15139           if (decl_specs)
15140             decl_specs->type = qual_type;
15141
15142           return qual_type;
15143         }
15144
15145       /* There is no valid C++ program where a non-template type is
15146          followed by a "<".  That usually indicates that the user
15147          thought that the type was a template.  */
15148       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15149                                                none_type,
15150                                                token->location);
15151     }
15152
15153   return type;
15154 }
15155
15156 /* Parse a type-name.
15157
15158    type-name:
15159      class-name
15160      enum-name
15161      typedef-name
15162      simple-template-id [in c++0x]
15163
15164    enum-name:
15165      identifier
15166
15167    typedef-name:
15168      identifier
15169
15170    Returns a TYPE_DECL for the type.  */
15171
15172 static tree
15173 cp_parser_type_name (cp_parser* parser)
15174 {
15175   tree type_decl;
15176
15177   /* We can't know yet whether it is a class-name or not.  */
15178   cp_parser_parse_tentatively (parser);
15179   /* Try a class-name.  */
15180   type_decl = cp_parser_class_name (parser,
15181                                     /*typename_keyword_p=*/false,
15182                                     /*template_keyword_p=*/false,
15183                                     none_type,
15184                                     /*check_dependency_p=*/true,
15185                                     /*class_head_p=*/false,
15186                                     /*is_declaration=*/false);
15187   /* If it's not a class-name, keep looking.  */
15188   if (!cp_parser_parse_definitely (parser))
15189     {
15190       if (cxx_dialect < cxx11)
15191         /* It must be a typedef-name or an enum-name.  */
15192         return cp_parser_nonclass_name (parser);
15193
15194       cp_parser_parse_tentatively (parser);
15195       /* It is either a simple-template-id representing an
15196          instantiation of an alias template...  */
15197       type_decl = cp_parser_template_id (parser,
15198                                          /*template_keyword_p=*/false,
15199                                          /*check_dependency_p=*/true,
15200                                          none_type,
15201                                          /*is_declaration=*/false);
15202       /* Note that this must be an instantiation of an alias template
15203          because [temp.names]/6 says:
15204          
15205              A template-id that names an alias template specialization
15206              is a type-name.
15207
15208          Whereas [temp.names]/7 says:
15209          
15210              A simple-template-id that names a class template
15211              specialization is a class-name.  */
15212       if (type_decl != NULL_TREE
15213           && TREE_CODE (type_decl) == TYPE_DECL
15214           && TYPE_DECL_ALIAS_P (type_decl))
15215         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15216       else
15217         cp_parser_simulate_error (parser);
15218
15219       if (!cp_parser_parse_definitely (parser))
15220         /* ... Or a typedef-name or an enum-name.  */
15221         return cp_parser_nonclass_name (parser);
15222     }
15223
15224   return type_decl;
15225 }
15226
15227 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15228
15229    enum-name:
15230      identifier
15231
15232    typedef-name:
15233      identifier
15234
15235    Returns a TYPE_DECL for the type.  */
15236
15237 static tree
15238 cp_parser_nonclass_name (cp_parser* parser)
15239 {
15240   tree type_decl;
15241   tree identifier;
15242
15243   cp_token *token = cp_lexer_peek_token (parser->lexer);
15244   identifier = cp_parser_identifier (parser);
15245   if (identifier == error_mark_node)
15246     return error_mark_node;
15247
15248   /* Look up the type-name.  */
15249   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15250
15251   type_decl = strip_using_decl (type_decl);
15252   
15253   if (TREE_CODE (type_decl) != TYPE_DECL
15254       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15255     {
15256       /* See if this is an Objective-C type.  */
15257       tree protos = cp_parser_objc_protocol_refs_opt (parser);
15258       tree type = objc_get_protocol_qualified_type (identifier, protos);
15259       if (type)
15260         type_decl = TYPE_NAME (type);
15261     }
15262
15263   /* Issue an error if we did not find a type-name.  */
15264   if (TREE_CODE (type_decl) != TYPE_DECL
15265       /* In Objective-C, we have the complication that class names are
15266          normally type names and start declarations (eg, the
15267          "NSObject" in "NSObject *object;"), but can be used in an
15268          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15269          is an expression.  So, a classname followed by a dot is not a
15270          valid type-name.  */
15271       || (objc_is_class_name (TREE_TYPE (type_decl))
15272           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15273     {
15274       if (!cp_parser_simulate_error (parser))
15275         cp_parser_name_lookup_error (parser, identifier, type_decl,
15276                                      NLE_TYPE, token->location);
15277       return error_mark_node;
15278     }
15279   /* Remember that the name was used in the definition of the
15280      current class so that we can check later to see if the
15281      meaning would have been different after the class was
15282      entirely defined.  */
15283   else if (type_decl != error_mark_node
15284            && !parser->scope)
15285     maybe_note_name_used_in_class (identifier, type_decl);
15286   
15287   return type_decl;
15288 }
15289
15290 /* Parse an elaborated-type-specifier.  Note that the grammar given
15291    here incorporates the resolution to DR68.
15292
15293    elaborated-type-specifier:
15294      class-key :: [opt] nested-name-specifier [opt] identifier
15295      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15296      enum-key :: [opt] nested-name-specifier [opt] identifier
15297      typename :: [opt] nested-name-specifier identifier
15298      typename :: [opt] nested-name-specifier template [opt]
15299        template-id
15300
15301    GNU extension:
15302
15303    elaborated-type-specifier:
15304      class-key attributes :: [opt] nested-name-specifier [opt] identifier
15305      class-key attributes :: [opt] nested-name-specifier [opt]
15306                template [opt] template-id
15307      enum attributes :: [opt] nested-name-specifier [opt] identifier
15308
15309    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15310    declared `friend'.  If IS_DECLARATION is TRUE, then this
15311    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15312    something is being declared.
15313
15314    Returns the TYPE specified.  */
15315
15316 static tree
15317 cp_parser_elaborated_type_specifier (cp_parser* parser,
15318                                      bool is_friend,
15319                                      bool is_declaration)
15320 {
15321   enum tag_types tag_type;
15322   tree identifier;
15323   tree type = NULL_TREE;
15324   tree attributes = NULL_TREE;
15325   tree globalscope;
15326   cp_token *token = NULL;
15327
15328   /* See if we're looking at the `enum' keyword.  */
15329   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15330     {
15331       /* Consume the `enum' token.  */
15332       cp_lexer_consume_token (parser->lexer);
15333       /* Remember that it's an enumeration type.  */
15334       tag_type = enum_type;
15335       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15336          enums) is used here.  */
15337       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15338           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15339         {
15340             pedwarn (input_location, 0, "elaborated-type-specifier "
15341                       "for a scoped enum must not use the %<%D%> keyword",
15342                       cp_lexer_peek_token (parser->lexer)->u.value);
15343           /* Consume the `struct' or `class' and parse it anyway.  */
15344           cp_lexer_consume_token (parser->lexer);
15345         }
15346       /* Parse the attributes.  */
15347       attributes = cp_parser_attributes_opt (parser);
15348     }
15349   /* Or, it might be `typename'.  */
15350   else if (cp_lexer_next_token_is_keyword (parser->lexer,
15351                                            RID_TYPENAME))
15352     {
15353       /* Consume the `typename' token.  */
15354       cp_lexer_consume_token (parser->lexer);
15355       /* Remember that it's a `typename' type.  */
15356       tag_type = typename_type;
15357     }
15358   /* Otherwise it must be a class-key.  */
15359   else
15360     {
15361       tag_type = cp_parser_class_key (parser);
15362       if (tag_type == none_type)
15363         return error_mark_node;
15364       /* Parse the attributes.  */
15365       attributes = cp_parser_attributes_opt (parser);
15366     }
15367
15368   /* Look for the `::' operator.  */
15369   globalscope =  cp_parser_global_scope_opt (parser,
15370                                              /*current_scope_valid_p=*/false);
15371   /* Look for the nested-name-specifier.  */
15372   if (tag_type == typename_type && !globalscope)
15373     {
15374       if (!cp_parser_nested_name_specifier (parser,
15375                                            /*typename_keyword_p=*/true,
15376                                            /*check_dependency_p=*/true,
15377                                            /*type_p=*/true,
15378                                             is_declaration))
15379         return error_mark_node;
15380     }
15381   else
15382     /* Even though `typename' is not present, the proposed resolution
15383        to Core Issue 180 says that in `class A<T>::B', `B' should be
15384        considered a type-name, even if `A<T>' is dependent.  */
15385     cp_parser_nested_name_specifier_opt (parser,
15386                                          /*typename_keyword_p=*/true,
15387                                          /*check_dependency_p=*/true,
15388                                          /*type_p=*/true,
15389                                          is_declaration);
15390  /* For everything but enumeration types, consider a template-id.
15391     For an enumeration type, consider only a plain identifier.  */
15392   if (tag_type != enum_type)
15393     {
15394       bool template_p = false;
15395       tree decl;
15396
15397       /* Allow the `template' keyword.  */
15398       template_p = cp_parser_optional_template_keyword (parser);
15399       /* If we didn't see `template', we don't know if there's a
15400          template-id or not.  */
15401       if (!template_p)
15402         cp_parser_parse_tentatively (parser);
15403       /* Parse the template-id.  */
15404       token = cp_lexer_peek_token (parser->lexer);
15405       decl = cp_parser_template_id (parser, template_p,
15406                                     /*check_dependency_p=*/true,
15407                                     tag_type,
15408                                     is_declaration);
15409       /* If we didn't find a template-id, look for an ordinary
15410          identifier.  */
15411       if (!template_p && !cp_parser_parse_definitely (parser))
15412         ;
15413       /* We can get here when cp_parser_template_id, called by
15414          cp_parser_class_name with tag_type == none_type, succeeds
15415          and caches a BASELINK.  Then, when called again here,
15416          instead of failing and returning an error_mark_node
15417          returns it (see template/typename17.C in C++11).
15418          ??? Could we diagnose this earlier?  */
15419       else if (tag_type == typename_type && BASELINK_P (decl))
15420         {
15421           cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15422           type = error_mark_node;
15423         }
15424       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15425          in effect, then we must assume that, upon instantiation, the
15426          template will correspond to a class.  */
15427       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15428                && tag_type == typename_type)
15429         type = make_typename_type (parser->scope, decl,
15430                                    typename_type,
15431                                    /*complain=*/tf_error);
15432       /* If the `typename' keyword is in effect and DECL is not a type
15433          decl, then type is non existent.   */
15434       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15435         ; 
15436       else if (TREE_CODE (decl) == TYPE_DECL)
15437         type = check_elaborated_type_specifier (tag_type, decl,
15438                                                 /*allow_template_p=*/true);
15439       else if (decl == error_mark_node)
15440         type = error_mark_node; 
15441     }
15442
15443   if (!type)
15444     {
15445       token = cp_lexer_peek_token (parser->lexer);
15446       identifier = cp_parser_identifier (parser);
15447
15448       if (identifier == error_mark_node)
15449         {
15450           parser->scope = NULL_TREE;
15451           return error_mark_node;
15452         }
15453
15454       /* For a `typename', we needn't call xref_tag.  */
15455       if (tag_type == typename_type
15456           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15457         return cp_parser_make_typename_type (parser, identifier,
15458                                              token->location);
15459
15460       /* Template parameter lists apply only if we are not within a
15461          function parameter list.  */
15462       bool template_parm_lists_apply
15463           = parser->num_template_parameter_lists;
15464       if (template_parm_lists_apply)
15465         for (cp_binding_level *s = current_binding_level;
15466              s && s->kind != sk_template_parms;
15467              s = s->level_chain)
15468           if (s->kind == sk_function_parms)
15469             template_parm_lists_apply = false;
15470
15471       /* Look up a qualified name in the usual way.  */
15472       if (parser->scope)
15473         {
15474           tree decl;
15475           tree ambiguous_decls;
15476
15477           decl = cp_parser_lookup_name (parser, identifier,
15478                                         tag_type,
15479                                         /*is_template=*/false,
15480                                         /*is_namespace=*/false,
15481                                         /*check_dependency=*/true,
15482                                         &ambiguous_decls,
15483                                         token->location);
15484
15485           /* If the lookup was ambiguous, an error will already have been
15486              issued.  */
15487           if (ambiguous_decls)
15488             return error_mark_node;
15489
15490           /* If we are parsing friend declaration, DECL may be a
15491              TEMPLATE_DECL tree node here.  However, we need to check
15492              whether this TEMPLATE_DECL results in valid code.  Consider
15493              the following example:
15494
15495                namespace N {
15496                  template <class T> class C {};
15497                }
15498                class X {
15499                  template <class T> friend class N::C; // #1, valid code
15500                };
15501                template <class T> class Y {
15502                  friend class N::C;                    // #2, invalid code
15503                };
15504
15505              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15506              name lookup of `N::C'.  We see that friend declaration must
15507              be template for the code to be valid.  Note that
15508              processing_template_decl does not work here since it is
15509              always 1 for the above two cases.  */
15510
15511           decl = (cp_parser_maybe_treat_template_as_class
15512                   (decl, /*tag_name_p=*/is_friend
15513                          && template_parm_lists_apply));
15514
15515           if (TREE_CODE (decl) != TYPE_DECL)
15516             {
15517               cp_parser_diagnose_invalid_type_name (parser,
15518                                                     identifier,
15519                                                     token->location);
15520               return error_mark_node;
15521             }
15522
15523           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15524             {
15525               bool allow_template = (template_parm_lists_apply
15526                                      || DECL_SELF_REFERENCE_P (decl));
15527               type = check_elaborated_type_specifier (tag_type, decl,
15528                                                       allow_template);
15529
15530               if (type == error_mark_node)
15531                 return error_mark_node;
15532             }
15533
15534           /* Forward declarations of nested types, such as
15535
15536                class C1::C2;
15537                class C1::C2::C3;
15538
15539              are invalid unless all components preceding the final '::'
15540              are complete.  If all enclosing types are complete, these
15541              declarations become merely pointless.
15542
15543              Invalid forward declarations of nested types are errors
15544              caught elsewhere in parsing.  Those that are pointless arrive
15545              here.  */
15546
15547           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15548               && !is_friend && !processing_explicit_instantiation)
15549             warning (0, "declaration %qD does not declare anything", decl);
15550
15551           type = TREE_TYPE (decl);
15552         }
15553       else
15554         {
15555           /* An elaborated-type-specifier sometimes introduces a new type and
15556              sometimes names an existing type.  Normally, the rule is that it
15557              introduces a new type only if there is not an existing type of
15558              the same name already in scope.  For example, given:
15559
15560                struct S {};
15561                void f() { struct S s; }
15562
15563              the `struct S' in the body of `f' is the same `struct S' as in
15564              the global scope; the existing definition is used.  However, if
15565              there were no global declaration, this would introduce a new
15566              local class named `S'.
15567
15568              An exception to this rule applies to the following code:
15569
15570                namespace N { struct S; }
15571
15572              Here, the elaborated-type-specifier names a new type
15573              unconditionally; even if there is already an `S' in the
15574              containing scope this declaration names a new type.
15575              This exception only applies if the elaborated-type-specifier
15576              forms the complete declaration:
15577
15578                [class.name]
15579
15580                A declaration consisting solely of `class-key identifier ;' is
15581                either a redeclaration of the name in the current scope or a
15582                forward declaration of the identifier as a class name.  It
15583                introduces the name into the current scope.
15584
15585              We are in this situation precisely when the next token is a `;'.
15586
15587              An exception to the exception is that a `friend' declaration does
15588              *not* name a new type; i.e., given:
15589
15590                struct S { friend struct T; };
15591
15592              `T' is not a new type in the scope of `S'.
15593
15594              Also, `new struct S' or `sizeof (struct S)' never results in the
15595              definition of a new type; a new type can only be declared in a
15596              declaration context.  */
15597
15598           tag_scope ts;
15599           bool template_p;
15600
15601           if (is_friend)
15602             /* Friends have special name lookup rules.  */
15603             ts = ts_within_enclosing_non_class;
15604           else if (is_declaration
15605                    && cp_lexer_next_token_is (parser->lexer,
15606                                               CPP_SEMICOLON))
15607             /* This is a `class-key identifier ;' */
15608             ts = ts_current;
15609           else
15610             ts = ts_global;
15611
15612           template_p =
15613             (template_parm_lists_apply
15614              && (cp_parser_next_token_starts_class_definition_p (parser)
15615                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15616           /* An unqualified name was used to reference this type, so
15617              there were no qualifying templates.  */
15618           if (template_parm_lists_apply
15619               && !cp_parser_check_template_parameters (parser,
15620                                                        /*num_templates=*/0,
15621                                                        token->location,
15622                                                        /*declarator=*/NULL))
15623             return error_mark_node;
15624           type = xref_tag (tag_type, identifier, ts, template_p);
15625         }
15626     }
15627
15628   if (type == error_mark_node)
15629     return error_mark_node;
15630
15631   /* Allow attributes on forward declarations of classes.  */
15632   if (attributes)
15633     {
15634       if (TREE_CODE (type) == TYPENAME_TYPE)
15635         warning (OPT_Wattributes,
15636                  "attributes ignored on uninstantiated type");
15637       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15638                && ! processing_explicit_instantiation)
15639         warning (OPT_Wattributes,
15640                  "attributes ignored on template instantiation");
15641       else if (is_declaration && cp_parser_declares_only_class_p (parser))
15642         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15643       else
15644         warning (OPT_Wattributes,
15645                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15646     }
15647
15648   if (tag_type != enum_type)
15649     {
15650       /* Indicate whether this class was declared as a `class' or as a
15651          `struct'.  */
15652       if (TREE_CODE (type) == RECORD_TYPE)
15653         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15654       cp_parser_check_class_key (tag_type, type);
15655     }
15656
15657   /* A "<" cannot follow an elaborated type specifier.  If that
15658      happens, the user was probably trying to form a template-id.  */
15659   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15660                                            token->location);
15661
15662   return type;
15663 }
15664
15665 /* Parse an enum-specifier.
15666
15667    enum-specifier:
15668      enum-head { enumerator-list [opt] }
15669      enum-head { enumerator-list , } [C++0x]
15670
15671    enum-head:
15672      enum-key identifier [opt] enum-base [opt]
15673      enum-key nested-name-specifier identifier enum-base [opt]
15674
15675    enum-key:
15676      enum
15677      enum class   [C++0x]
15678      enum struct  [C++0x]
15679
15680    enum-base:   [C++0x]
15681      : type-specifier-seq
15682
15683    opaque-enum-specifier:
15684      enum-key identifier enum-base [opt] ;
15685
15686    GNU Extensions:
15687      enum-key attributes[opt] identifier [opt] enum-base [opt] 
15688        { enumerator-list [opt] }attributes[opt]
15689      enum-key attributes[opt] identifier [opt] enum-base [opt]
15690        { enumerator-list, }attributes[opt] [C++0x]
15691
15692    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15693    if the token stream isn't an enum-specifier after all.  */
15694
15695 static tree
15696 cp_parser_enum_specifier (cp_parser* parser)
15697 {
15698   tree identifier;
15699   tree type = NULL_TREE;
15700   tree prev_scope;
15701   tree nested_name_specifier = NULL_TREE;
15702   tree attributes;
15703   bool scoped_enum_p = false;
15704   bool has_underlying_type = false;
15705   bool nested_being_defined = false;
15706   bool new_value_list = false;
15707   bool is_new_type = false;
15708   bool is_anonymous = false;
15709   tree underlying_type = NULL_TREE;
15710   cp_token *type_start_token = NULL;
15711   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15712
15713   parser->colon_corrects_to_scope_p = false;
15714
15715   /* Parse tentatively so that we can back up if we don't find a
15716      enum-specifier.  */
15717   cp_parser_parse_tentatively (parser);
15718
15719   /* Caller guarantees that the current token is 'enum', an identifier
15720      possibly follows, and the token after that is an opening brace.
15721      If we don't have an identifier, fabricate an anonymous name for
15722      the enumeration being defined.  */
15723   cp_lexer_consume_token (parser->lexer);
15724
15725   /* Parse the "class" or "struct", which indicates a scoped
15726      enumeration type in C++0x.  */
15727   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15728       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15729     {
15730       if (cxx_dialect < cxx11)
15731         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15732
15733       /* Consume the `struct' or `class' token.  */
15734       cp_lexer_consume_token (parser->lexer);
15735
15736       scoped_enum_p = true;
15737     }
15738
15739   attributes = cp_parser_attributes_opt (parser);
15740
15741   /* Clear the qualification.  */
15742   parser->scope = NULL_TREE;
15743   parser->qualifying_scope = NULL_TREE;
15744   parser->object_scope = NULL_TREE;
15745
15746   /* Figure out in what scope the declaration is being placed.  */
15747   prev_scope = current_scope ();
15748
15749   type_start_token = cp_lexer_peek_token (parser->lexer);
15750
15751   push_deferring_access_checks (dk_no_check);
15752   nested_name_specifier
15753       = cp_parser_nested_name_specifier_opt (parser,
15754                                              /*typename_keyword_p=*/true,
15755                                              /*check_dependency_p=*/false,
15756                                              /*type_p=*/false,
15757                                              /*is_declaration=*/false);
15758
15759   if (nested_name_specifier)
15760     {
15761       tree name;
15762
15763       identifier = cp_parser_identifier (parser);
15764       name =  cp_parser_lookup_name (parser, identifier,
15765                                      enum_type,
15766                                      /*is_template=*/false,
15767                                      /*is_namespace=*/false,
15768                                      /*check_dependency=*/true,
15769                                      /*ambiguous_decls=*/NULL,
15770                                      input_location);
15771       if (name && name != error_mark_node)
15772         {
15773           type = TREE_TYPE (name);
15774           if (TREE_CODE (type) == TYPENAME_TYPE)
15775             {
15776               /* Are template enums allowed in ISO? */
15777               if (template_parm_scope_p ())
15778                 pedwarn (type_start_token->location, OPT_Wpedantic,
15779                          "%qD is an enumeration template", name);
15780               /* ignore a typename reference, for it will be solved by name
15781                  in start_enum.  */
15782               type = NULL_TREE;
15783             }
15784         }
15785       else if (nested_name_specifier == error_mark_node)
15786         /* We already issued an error.  */;
15787       else
15788         error_at (type_start_token->location,
15789                   "%qD is not an enumerator-name", identifier);
15790     }
15791   else
15792     {
15793       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15794         identifier = cp_parser_identifier (parser);
15795       else
15796         {
15797           identifier = make_anon_name ();
15798           is_anonymous = true;
15799           if (scoped_enum_p)
15800             error_at (type_start_token->location,
15801                       "anonymous scoped enum is not allowed");
15802         }
15803     }
15804   pop_deferring_access_checks ();
15805
15806   /* Check for the `:' that denotes a specified underlying type in C++0x.
15807      Note that a ':' could also indicate a bitfield width, however.  */
15808   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15809     {
15810       cp_decl_specifier_seq type_specifiers;
15811
15812       /* Consume the `:'.  */
15813       cp_lexer_consume_token (parser->lexer);
15814
15815       /* Parse the type-specifier-seq.  */
15816       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15817                                     /*is_trailing_return=*/false,
15818                                     &type_specifiers);
15819
15820       /* At this point this is surely not elaborated type specifier.  */
15821       if (!cp_parser_parse_definitely (parser))
15822         return NULL_TREE;
15823
15824       if (cxx_dialect < cxx11)
15825         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15826
15827       has_underlying_type = true;
15828
15829       /* If that didn't work, stop.  */
15830       if (type_specifiers.type != error_mark_node)
15831         {
15832           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15833                                             /*initialized=*/0, NULL);
15834           if (underlying_type == error_mark_node
15835               || check_for_bare_parameter_packs (underlying_type))
15836             underlying_type = NULL_TREE;
15837         }
15838     }
15839
15840   /* Look for the `{' but don't consume it yet.  */
15841   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15842     {
15843       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15844         {
15845           cp_parser_error (parser, "expected %<{%>");
15846           if (has_underlying_type)
15847             {
15848               type = NULL_TREE;
15849               goto out;
15850             }
15851         }
15852       /* An opaque-enum-specifier must have a ';' here.  */
15853       if ((scoped_enum_p || underlying_type)
15854           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15855         {
15856           cp_parser_error (parser, "expected %<;%> or %<{%>");
15857           if (has_underlying_type)
15858             {
15859               type = NULL_TREE;
15860               goto out;
15861             }
15862         }
15863     }
15864
15865   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15866     return NULL_TREE;
15867
15868   if (nested_name_specifier)
15869     {
15870       if (CLASS_TYPE_P (nested_name_specifier))
15871         {
15872           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15873           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15874           push_scope (nested_name_specifier);
15875         }
15876       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15877         {
15878           push_nested_namespace (nested_name_specifier);
15879         }
15880     }
15881
15882   /* Issue an error message if type-definitions are forbidden here.  */
15883   if (!cp_parser_check_type_definition (parser))
15884     type = error_mark_node;
15885   else
15886     /* Create the new type.  We do this before consuming the opening
15887        brace so the enum will be recorded as being on the line of its
15888        tag (or the 'enum' keyword, if there is no tag).  */
15889     type = start_enum (identifier, type, underlying_type,
15890                        scoped_enum_p, &is_new_type);
15891
15892   /* If the next token is not '{' it is an opaque-enum-specifier or an
15893      elaborated-type-specifier.  */
15894   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15895     {
15896       timevar_push (TV_PARSE_ENUM);
15897       if (nested_name_specifier
15898           && nested_name_specifier != error_mark_node)
15899         {
15900           /* The following catches invalid code such as:
15901              enum class S<int>::E { A, B, C }; */
15902           if (!processing_specialization
15903               && CLASS_TYPE_P (nested_name_specifier)
15904               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15905             error_at (type_start_token->location, "cannot add an enumerator "
15906                       "list to a template instantiation");
15907
15908           if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15909             {
15910               error_at (type_start_token->location,
15911                         "%<%T::%E%> has not been declared",
15912                         TYPE_CONTEXT (nested_name_specifier),
15913                         nested_name_specifier);
15914               type = error_mark_node;
15915             }
15916           /* If that scope does not contain the scope in which the
15917              class was originally declared, the program is invalid.  */
15918           else if (prev_scope && !is_ancestor (prev_scope,
15919                                                nested_name_specifier))
15920             {
15921               if (at_namespace_scope_p ())
15922                 error_at (type_start_token->location,
15923                           "declaration of %qD in namespace %qD which does not "
15924                           "enclose %qD",
15925                           type, prev_scope, nested_name_specifier);
15926               else
15927                 error_at (type_start_token->location,
15928                           "declaration of %qD in %qD which does not "
15929                           "enclose %qD",
15930                           type, prev_scope, nested_name_specifier);
15931               type = error_mark_node;
15932             }
15933         }
15934
15935       if (scoped_enum_p)
15936         begin_scope (sk_scoped_enum, type);
15937
15938       /* Consume the opening brace.  */
15939       cp_lexer_consume_token (parser->lexer);
15940
15941       if (type == error_mark_node)
15942         ; /* Nothing to add */
15943       else if (OPAQUE_ENUM_P (type)
15944                || (cxx_dialect > cxx98 && processing_specialization))
15945         {
15946           new_value_list = true;
15947           SET_OPAQUE_ENUM_P (type, false);
15948           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15949         }
15950       else
15951         {
15952           error_at (type_start_token->location,
15953                     "multiple definition of %q#T", type);
15954           inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15955                   "previous definition here");
15956           type = error_mark_node;
15957         }
15958
15959       if (type == error_mark_node)
15960         cp_parser_skip_to_end_of_block_or_statement (parser);
15961       /* If the next token is not '}', then there are some enumerators.  */
15962       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15963         {
15964           if (is_anonymous && !scoped_enum_p)
15965             pedwarn (type_start_token->location, OPT_Wpedantic,
15966                      "ISO C++ forbids empty anonymous enum");
15967         }
15968       else
15969         cp_parser_enumerator_list (parser, type);
15970
15971       /* Consume the final '}'.  */
15972       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15973
15974       if (scoped_enum_p)
15975         finish_scope ();
15976       timevar_pop (TV_PARSE_ENUM);
15977     }
15978   else
15979     {
15980       /* If a ';' follows, then it is an opaque-enum-specifier
15981         and additional restrictions apply.  */
15982       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15983         {
15984           if (is_anonymous)
15985             error_at (type_start_token->location,
15986                       "opaque-enum-specifier without name");
15987           else if (nested_name_specifier)
15988             error_at (type_start_token->location,
15989                       "opaque-enum-specifier must use a simple identifier");
15990         }
15991     }
15992
15993   /* Look for trailing attributes to apply to this enumeration, and
15994      apply them if appropriate.  */
15995   if (cp_parser_allow_gnu_extensions_p (parser))
15996     {
15997       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15998       trailing_attr = chainon (trailing_attr, attributes);
15999       cplus_decl_attributes (&type,
16000                              trailing_attr,
16001                              (int) ATTR_FLAG_TYPE_IN_PLACE);
16002     }
16003
16004   /* Finish up the enumeration.  */
16005   if (type != error_mark_node)
16006     {
16007       if (new_value_list)
16008         finish_enum_value_list (type);
16009       if (is_new_type)
16010         finish_enum (type);
16011     }
16012
16013   if (nested_name_specifier)
16014     {
16015       if (CLASS_TYPE_P (nested_name_specifier))
16016         {
16017           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16018           pop_scope (nested_name_specifier);
16019         }
16020       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16021         {
16022           pop_nested_namespace (nested_name_specifier);
16023         }
16024     }
16025  out:
16026   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16027   return type;
16028 }
16029
16030 /* Parse an enumerator-list.  The enumerators all have the indicated
16031    TYPE.
16032
16033    enumerator-list:
16034      enumerator-definition
16035      enumerator-list , enumerator-definition  */
16036
16037 static void
16038 cp_parser_enumerator_list (cp_parser* parser, tree type)
16039 {
16040   while (true)
16041     {
16042       /* Parse an enumerator-definition.  */
16043       cp_parser_enumerator_definition (parser, type);
16044
16045       /* If the next token is not a ',', we've reached the end of
16046          the list.  */
16047       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16048         break;
16049       /* Otherwise, consume the `,' and keep going.  */
16050       cp_lexer_consume_token (parser->lexer);
16051       /* If the next token is a `}', there is a trailing comma.  */
16052       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16053         {
16054           if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16055             pedwarn (input_location, OPT_Wpedantic,
16056                      "comma at end of enumerator list");
16057           break;
16058         }
16059     }
16060 }
16061
16062 /* Parse an enumerator-definition.  The enumerator has the indicated
16063    TYPE.
16064
16065    enumerator-definition:
16066      enumerator
16067      enumerator = constant-expression
16068
16069    enumerator:
16070      identifier  */
16071
16072 static void
16073 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16074 {
16075   tree identifier;
16076   tree value;
16077   location_t loc;
16078
16079   /* Save the input location because we are interested in the location
16080      of the identifier and not the location of the explicit value.  */
16081   loc = cp_lexer_peek_token (parser->lexer)->location;
16082
16083   /* Look for the identifier.  */
16084   identifier = cp_parser_identifier (parser);
16085   if (identifier == error_mark_node)
16086     return;
16087
16088   /* If the next token is an '=', then there is an explicit value.  */
16089   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16090     {
16091       /* Consume the `=' token.  */
16092       cp_lexer_consume_token (parser->lexer);
16093       /* Parse the value.  */
16094       value = cp_parser_constant_expression (parser);
16095     }
16096   else
16097     value = NULL_TREE;
16098
16099   /* If we are processing a template, make sure the initializer of the
16100      enumerator doesn't contain any bare template parameter pack.  */
16101   if (check_for_bare_parameter_packs (value))
16102     value = error_mark_node;
16103
16104   /* Create the enumerator.  */
16105   build_enumerator (identifier, value, type, loc);
16106 }
16107
16108 /* Parse a namespace-name.
16109
16110    namespace-name:
16111      original-namespace-name
16112      namespace-alias
16113
16114    Returns the NAMESPACE_DECL for the namespace.  */
16115
16116 static tree
16117 cp_parser_namespace_name (cp_parser* parser)
16118 {
16119   tree identifier;
16120   tree namespace_decl;
16121
16122   cp_token *token = cp_lexer_peek_token (parser->lexer);
16123
16124   /* Get the name of the namespace.  */
16125   identifier = cp_parser_identifier (parser);
16126   if (identifier == error_mark_node)
16127     return error_mark_node;
16128
16129   /* Look up the identifier in the currently active scope.  Look only
16130      for namespaces, due to:
16131
16132        [basic.lookup.udir]
16133
16134        When looking up a namespace-name in a using-directive or alias
16135        definition, only namespace names are considered.
16136
16137      And:
16138
16139        [basic.lookup.qual]
16140
16141        During the lookup of a name preceding the :: scope resolution
16142        operator, object, function, and enumerator names are ignored.
16143
16144      (Note that cp_parser_qualifying_entity only calls this
16145      function if the token after the name is the scope resolution
16146      operator.)  */
16147   namespace_decl = cp_parser_lookup_name (parser, identifier,
16148                                           none_type,
16149                                           /*is_template=*/false,
16150                                           /*is_namespace=*/true,
16151                                           /*check_dependency=*/true,
16152                                           /*ambiguous_decls=*/NULL,
16153                                           token->location);
16154   /* If it's not a namespace, issue an error.  */
16155   if (namespace_decl == error_mark_node
16156       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16157     {
16158       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16159         error_at (token->location, "%qD is not a namespace-name", identifier);
16160       cp_parser_error (parser, "expected namespace-name");
16161       namespace_decl = error_mark_node;
16162     }
16163
16164   return namespace_decl;
16165 }
16166
16167 /* Parse a namespace-definition.
16168
16169    namespace-definition:
16170      named-namespace-definition
16171      unnamed-namespace-definition
16172
16173    named-namespace-definition:
16174      original-namespace-definition
16175      extension-namespace-definition
16176
16177    original-namespace-definition:
16178      namespace identifier { namespace-body }
16179
16180    extension-namespace-definition:
16181      namespace original-namespace-name { namespace-body }
16182
16183    unnamed-namespace-definition:
16184      namespace { namespace-body } */
16185
16186 static void
16187 cp_parser_namespace_definition (cp_parser* parser)
16188 {
16189   tree identifier, attribs;
16190   bool has_visibility;
16191   bool is_inline;
16192
16193   cp_ensure_no_omp_declare_simd (parser);
16194   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16195     {
16196       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16197       is_inline = true;
16198       cp_lexer_consume_token (parser->lexer);
16199     }
16200   else
16201     is_inline = false;
16202
16203   /* Look for the `namespace' keyword.  */
16204   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16205
16206   /* Get the name of the namespace.  We do not attempt to distinguish
16207      between an original-namespace-definition and an
16208      extension-namespace-definition at this point.  The semantic
16209      analysis routines are responsible for that.  */
16210   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16211     identifier = cp_parser_identifier (parser);
16212   else
16213     identifier = NULL_TREE;
16214
16215   /* Parse any specified attributes.  */
16216   attribs = cp_parser_attributes_opt (parser);
16217
16218   /* Look for the `{' to start the namespace.  */
16219   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16220   /* Start the namespace.  */
16221   push_namespace (identifier);
16222
16223   /* "inline namespace" is equivalent to a stub namespace definition
16224      followed by a strong using directive.  */
16225   if (is_inline)
16226     {
16227       tree name_space = current_namespace;
16228       /* Set up namespace association.  */
16229       DECL_NAMESPACE_ASSOCIATIONS (name_space)
16230         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16231                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
16232       /* Import the contents of the inline namespace.  */
16233       pop_namespace ();
16234       do_using_directive (name_space);
16235       push_namespace (identifier);
16236     }
16237
16238   has_visibility = handle_namespace_attrs (current_namespace, attribs);
16239
16240   /* Parse the body of the namespace.  */
16241   cp_parser_namespace_body (parser);
16242
16243   if (has_visibility)
16244     pop_visibility (1);
16245
16246   /* Finish the namespace.  */
16247   pop_namespace ();
16248   /* Look for the final `}'.  */
16249   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16250 }
16251
16252 /* Parse a namespace-body.
16253
16254    namespace-body:
16255      declaration-seq [opt]  */
16256
16257 static void
16258 cp_parser_namespace_body (cp_parser* parser)
16259 {
16260   cp_parser_declaration_seq_opt (parser);
16261 }
16262
16263 /* Parse a namespace-alias-definition.
16264
16265    namespace-alias-definition:
16266      namespace identifier = qualified-namespace-specifier ;  */
16267
16268 static void
16269 cp_parser_namespace_alias_definition (cp_parser* parser)
16270 {
16271   tree identifier;
16272   tree namespace_specifier;
16273
16274   cp_token *token = cp_lexer_peek_token (parser->lexer);
16275
16276   /* Look for the `namespace' keyword.  */
16277   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16278   /* Look for the identifier.  */
16279   identifier = cp_parser_identifier (parser);
16280   if (identifier == error_mark_node)
16281     return;
16282   /* Look for the `=' token.  */
16283   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16284       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
16285     {
16286       error_at (token->location, "%<namespace%> definition is not allowed here");
16287       /* Skip the definition.  */
16288       cp_lexer_consume_token (parser->lexer);
16289       if (cp_parser_skip_to_closing_brace (parser))
16290         cp_lexer_consume_token (parser->lexer);
16291       return;
16292     }
16293   cp_parser_require (parser, CPP_EQ, RT_EQ);
16294   /* Look for the qualified-namespace-specifier.  */
16295   namespace_specifier
16296     = cp_parser_qualified_namespace_specifier (parser);
16297   /* Look for the `;' token.  */
16298   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16299
16300   /* Register the alias in the symbol table.  */
16301   do_namespace_alias (identifier, namespace_specifier);
16302 }
16303
16304 /* Parse a qualified-namespace-specifier.
16305
16306    qualified-namespace-specifier:
16307      :: [opt] nested-name-specifier [opt] namespace-name
16308
16309    Returns a NAMESPACE_DECL corresponding to the specified
16310    namespace.  */
16311
16312 static tree
16313 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16314 {
16315   /* Look for the optional `::'.  */
16316   cp_parser_global_scope_opt (parser,
16317                               /*current_scope_valid_p=*/false);
16318
16319   /* Look for the optional nested-name-specifier.  */
16320   cp_parser_nested_name_specifier_opt (parser,
16321                                        /*typename_keyword_p=*/false,
16322                                        /*check_dependency_p=*/true,
16323                                        /*type_p=*/false,
16324                                        /*is_declaration=*/true);
16325
16326   return cp_parser_namespace_name (parser);
16327 }
16328
16329 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16330    access declaration.
16331
16332    using-declaration:
16333      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16334      using :: unqualified-id ;  
16335
16336    access-declaration:
16337      qualified-id ;  
16338
16339    */
16340
16341 static bool
16342 cp_parser_using_declaration (cp_parser* parser, 
16343                              bool access_declaration_p)
16344 {
16345   cp_token *token;
16346   bool typename_p = false;
16347   bool global_scope_p;
16348   tree decl;
16349   tree identifier;
16350   tree qscope;
16351   int oldcount = errorcount;
16352   cp_token *diag_token = NULL;
16353
16354   if (access_declaration_p)
16355     {
16356       diag_token = cp_lexer_peek_token (parser->lexer);
16357       cp_parser_parse_tentatively (parser);
16358     }
16359   else
16360     {
16361       /* Look for the `using' keyword.  */
16362       cp_parser_require_keyword (parser, RID_USING, RT_USING);
16363       
16364       /* Peek at the next token.  */
16365       token = cp_lexer_peek_token (parser->lexer);
16366       /* See if it's `typename'.  */
16367       if (token->keyword == RID_TYPENAME)
16368         {
16369           /* Remember that we've seen it.  */
16370           typename_p = true;
16371           /* Consume the `typename' token.  */
16372           cp_lexer_consume_token (parser->lexer);
16373         }
16374     }
16375
16376   /* Look for the optional global scope qualification.  */
16377   global_scope_p
16378     = (cp_parser_global_scope_opt (parser,
16379                                    /*current_scope_valid_p=*/false)
16380        != NULL_TREE);
16381
16382   /* If we saw `typename', or didn't see `::', then there must be a
16383      nested-name-specifier present.  */
16384   if (typename_p || !global_scope_p)
16385     {
16386       qscope = cp_parser_nested_name_specifier (parser, typename_p,
16387                                                 /*check_dependency_p=*/true,
16388                                                 /*type_p=*/false,
16389                                                 /*is_declaration=*/true);
16390       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16391         {
16392           cp_parser_skip_to_end_of_block_or_statement (parser);
16393           return false;
16394         }
16395     }
16396   /* Otherwise, we could be in either of the two productions.  In that
16397      case, treat the nested-name-specifier as optional.  */
16398   else
16399     qscope = cp_parser_nested_name_specifier_opt (parser,
16400                                                   /*typename_keyword_p=*/false,
16401                                                   /*check_dependency_p=*/true,
16402                                                   /*type_p=*/false,
16403                                                   /*is_declaration=*/true);
16404   if (!qscope)
16405     qscope = global_namespace;
16406   else if (UNSCOPED_ENUM_P (qscope))
16407     qscope = CP_TYPE_CONTEXT (qscope);
16408
16409   if (access_declaration_p && cp_parser_error_occurred (parser))
16410     /* Something has already gone wrong; there's no need to parse
16411        further.  Since an error has occurred, the return value of
16412        cp_parser_parse_definitely will be false, as required.  */
16413     return cp_parser_parse_definitely (parser);
16414
16415   token = cp_lexer_peek_token (parser->lexer);
16416   /* Parse the unqualified-id.  */
16417   identifier = cp_parser_unqualified_id (parser,
16418                                          /*template_keyword_p=*/false,
16419                                          /*check_dependency_p=*/true,
16420                                          /*declarator_p=*/true,
16421                                          /*optional_p=*/false);
16422
16423   if (access_declaration_p)
16424     {
16425       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16426         cp_parser_simulate_error (parser);
16427       if (!cp_parser_parse_definitely (parser))
16428         return false;
16429     }
16430
16431   /* The function we call to handle a using-declaration is different
16432      depending on what scope we are in.  */
16433   if (qscope == error_mark_node || identifier == error_mark_node)
16434     ;
16435   else if (!identifier_p (identifier)
16436            && TREE_CODE (identifier) != BIT_NOT_EXPR)
16437     /* [namespace.udecl]
16438
16439        A using declaration shall not name a template-id.  */
16440     error_at (token->location,
16441               "a template-id may not appear in a using-declaration");
16442   else
16443     {
16444       if (at_class_scope_p ())
16445         {
16446           /* Create the USING_DECL.  */
16447           decl = do_class_using_decl (parser->scope, identifier);
16448
16449           if (decl && typename_p)
16450             USING_DECL_TYPENAME_P (decl) = 1;
16451
16452           if (check_for_bare_parameter_packs (decl))
16453             {
16454               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16455               return false;
16456             }
16457           else
16458             /* Add it to the list of members in this class.  */
16459             finish_member_declaration (decl);
16460         }
16461       else
16462         {
16463           decl = cp_parser_lookup_name_simple (parser,
16464                                                identifier,
16465                                                token->location);
16466           if (decl == error_mark_node)
16467             cp_parser_name_lookup_error (parser, identifier,
16468                                          decl, NLE_NULL,
16469                                          token->location);
16470           else if (check_for_bare_parameter_packs (decl))
16471             {
16472               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16473               return false;
16474             }
16475           else if (!at_namespace_scope_p ())
16476             do_local_using_decl (decl, qscope, identifier);
16477           else
16478             do_toplevel_using_decl (decl, qscope, identifier);
16479         }
16480     }
16481
16482   /* Look for the final `;'.  */
16483   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16484
16485   if (access_declaration_p && errorcount == oldcount)
16486     warning_at (diag_token->location, OPT_Wdeprecated,
16487                 "access declarations are deprecated "
16488                 "in favour of using-declarations; "
16489                 "suggestion: add the %<using%> keyword");
16490
16491   return true;
16492 }
16493
16494 /* Parse an alias-declaration.
16495
16496    alias-declaration:
16497      using identifier attribute-specifier-seq [opt] = type-id  */
16498
16499 static tree
16500 cp_parser_alias_declaration (cp_parser* parser)
16501 {
16502   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16503   location_t id_location;
16504   cp_declarator *declarator;
16505   cp_decl_specifier_seq decl_specs;
16506   bool member_p;
16507   const char *saved_message = NULL;
16508
16509   /* Look for the `using' keyword.  */
16510   cp_token *using_token
16511     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16512   if (using_token == NULL)
16513     return error_mark_node;
16514
16515   id_location = cp_lexer_peek_token (parser->lexer)->location;
16516   id = cp_parser_identifier (parser);
16517   if (id == error_mark_node)
16518     return error_mark_node;
16519
16520   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16521   attributes = cp_parser_attributes_opt (parser);
16522   if (attributes == error_mark_node)
16523     return error_mark_node;
16524
16525   cp_parser_require (parser, CPP_EQ, RT_EQ);
16526
16527   if (cp_parser_error_occurred (parser))
16528     return error_mark_node;
16529
16530   cp_parser_commit_to_tentative_parse (parser);
16531
16532   /* Now we are going to parse the type-id of the declaration.  */
16533
16534   /*
16535     [dcl.type]/3 says:
16536
16537         "A type-specifier-seq shall not define a class or enumeration
16538          unless it appears in the type-id of an alias-declaration (7.1.3) that
16539          is not the declaration of a template-declaration."
16540
16541     In other words, if we currently are in an alias template, the
16542     type-id should not define a type.
16543
16544     So let's set parser->type_definition_forbidden_message in that
16545     case; cp_parser_check_type_definition (called by
16546     cp_parser_class_specifier) will then emit an error if a type is
16547     defined in the type-id.  */
16548   if (parser->num_template_parameter_lists)
16549     {
16550       saved_message = parser->type_definition_forbidden_message;
16551       parser->type_definition_forbidden_message =
16552         G_("types may not be defined in alias template declarations");
16553     }
16554
16555   type = cp_parser_type_id (parser);
16556
16557   /* Restore the error message if need be.  */
16558   if (parser->num_template_parameter_lists)
16559     parser->type_definition_forbidden_message = saved_message;
16560
16561   if (type == error_mark_node
16562       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16563     {
16564       cp_parser_skip_to_end_of_block_or_statement (parser);
16565       return error_mark_node;
16566     }
16567
16568   /* A typedef-name can also be introduced by an alias-declaration. The
16569      identifier following the using keyword becomes a typedef-name. It has
16570      the same semantics as if it were introduced by the typedef
16571      specifier. In particular, it does not define a new type and it shall
16572      not appear in the type-id.  */
16573
16574   clear_decl_specs (&decl_specs);
16575   decl_specs.type = type;
16576   if (attributes != NULL_TREE)
16577     {
16578       decl_specs.attributes = attributes;
16579       set_and_check_decl_spec_loc (&decl_specs,
16580                                    ds_attribute,
16581                                    attrs_token);
16582     }
16583   set_and_check_decl_spec_loc (&decl_specs,
16584                                ds_typedef,
16585                                using_token);
16586   set_and_check_decl_spec_loc (&decl_specs,
16587                                ds_alias,
16588                                using_token);
16589
16590   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16591   declarator->id_loc = id_location;
16592
16593   member_p = at_class_scope_p ();
16594   if (member_p)
16595     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16596                       NULL_TREE, attributes);
16597   else
16598     decl = start_decl (declarator, &decl_specs, 0,
16599                        attributes, NULL_TREE, &pushed_scope);
16600   if (decl == error_mark_node)
16601     return decl;
16602
16603   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16604
16605   if (pushed_scope)
16606     pop_scope (pushed_scope);
16607
16608   /* If decl is a template, return its TEMPLATE_DECL so that it gets
16609      added into the symbol table; otherwise, return the TYPE_DECL.  */
16610   if (DECL_LANG_SPECIFIC (decl)
16611       && DECL_TEMPLATE_INFO (decl)
16612       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16613     {
16614       decl = DECL_TI_TEMPLATE (decl);
16615       if (member_p)
16616         check_member_template (decl);
16617     }
16618
16619   return decl;
16620 }
16621
16622 /* Parse a using-directive.
16623
16624    using-directive:
16625      using namespace :: [opt] nested-name-specifier [opt]
16626        namespace-name ;  */
16627
16628 static void
16629 cp_parser_using_directive (cp_parser* parser)
16630 {
16631   tree namespace_decl;
16632   tree attribs;
16633
16634   /* Look for the `using' keyword.  */
16635   cp_parser_require_keyword (parser, RID_USING, RT_USING);
16636   /* And the `namespace' keyword.  */
16637   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16638   /* Look for the optional `::' operator.  */
16639   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16640   /* And the optional nested-name-specifier.  */
16641   cp_parser_nested_name_specifier_opt (parser,
16642                                        /*typename_keyword_p=*/false,
16643                                        /*check_dependency_p=*/true,
16644                                        /*type_p=*/false,
16645                                        /*is_declaration=*/true);
16646   /* Get the namespace being used.  */
16647   namespace_decl = cp_parser_namespace_name (parser);
16648   /* And any specified attributes.  */
16649   attribs = cp_parser_attributes_opt (parser);
16650   /* Update the symbol table.  */
16651   parse_using_directive (namespace_decl, attribs);
16652   /* Look for the final `;'.  */
16653   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16654 }
16655
16656 /* Parse an asm-definition.
16657
16658    asm-definition:
16659      asm ( string-literal ) ;
16660
16661    GNU Extension:
16662
16663    asm-definition:
16664      asm volatile [opt] ( string-literal ) ;
16665      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16666      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16667                           : asm-operand-list [opt] ) ;
16668      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16669                           : asm-operand-list [opt]
16670                           : asm-clobber-list [opt] ) ;
16671      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16672                                : asm-clobber-list [opt]
16673                                : asm-goto-list ) ;  */
16674
16675 static void
16676 cp_parser_asm_definition (cp_parser* parser)
16677 {
16678   tree string;
16679   tree outputs = NULL_TREE;
16680   tree inputs = NULL_TREE;
16681   tree clobbers = NULL_TREE;
16682   tree labels = NULL_TREE;
16683   tree asm_stmt;
16684   bool volatile_p = false;
16685   bool extended_p = false;
16686   bool invalid_inputs_p = false;
16687   bool invalid_outputs_p = false;
16688   bool goto_p = false;
16689   required_token missing = RT_NONE;
16690
16691   /* Look for the `asm' keyword.  */
16692   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16693
16694   if (parser->in_function_body
16695       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16696     {
16697       error ("%<asm%> in %<constexpr%> function");
16698       cp_function_chain->invalid_constexpr = true;
16699     }
16700
16701   /* See if the next token is `volatile'.  */
16702   if (cp_parser_allow_gnu_extensions_p (parser)
16703       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16704     {
16705       /* Remember that we saw the `volatile' keyword.  */
16706       volatile_p = true;
16707       /* Consume the token.  */
16708       cp_lexer_consume_token (parser->lexer);
16709     }
16710   if (cp_parser_allow_gnu_extensions_p (parser)
16711       && parser->in_function_body
16712       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16713     {
16714       /* Remember that we saw the `goto' keyword.  */
16715       goto_p = true;
16716       /* Consume the token.  */
16717       cp_lexer_consume_token (parser->lexer);
16718     }
16719   /* Look for the opening `('.  */
16720   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16721     return;
16722   /* Look for the string.  */
16723   string = cp_parser_string_literal (parser, false, false);
16724   if (string == error_mark_node)
16725     {
16726       cp_parser_skip_to_closing_parenthesis (parser, true, false,
16727                                              /*consume_paren=*/true);
16728       return;
16729     }
16730
16731   /* If we're allowing GNU extensions, check for the extended assembly
16732      syntax.  Unfortunately, the `:' tokens need not be separated by
16733      a space in C, and so, for compatibility, we tolerate that here
16734      too.  Doing that means that we have to treat the `::' operator as
16735      two `:' tokens.  */
16736   if (cp_parser_allow_gnu_extensions_p (parser)
16737       && parser->in_function_body
16738       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16739           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16740     {
16741       bool inputs_p = false;
16742       bool clobbers_p = false;
16743       bool labels_p = false;
16744
16745       /* The extended syntax was used.  */
16746       extended_p = true;
16747
16748       /* Look for outputs.  */
16749       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16750         {
16751           /* Consume the `:'.  */
16752           cp_lexer_consume_token (parser->lexer);
16753           /* Parse the output-operands.  */
16754           if (cp_lexer_next_token_is_not (parser->lexer,
16755                                           CPP_COLON)
16756               && cp_lexer_next_token_is_not (parser->lexer,
16757                                              CPP_SCOPE)
16758               && cp_lexer_next_token_is_not (parser->lexer,
16759                                              CPP_CLOSE_PAREN)
16760               && !goto_p)
16761             outputs = cp_parser_asm_operand_list (parser);
16762
16763             if (outputs == error_mark_node)
16764               invalid_outputs_p = true;
16765         }
16766       /* If the next token is `::', there are no outputs, and the
16767          next token is the beginning of the inputs.  */
16768       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16769         /* The inputs are coming next.  */
16770         inputs_p = true;
16771
16772       /* Look for inputs.  */
16773       if (inputs_p
16774           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16775         {
16776           /* Consume the `:' or `::'.  */
16777           cp_lexer_consume_token (parser->lexer);
16778           /* Parse the output-operands.  */
16779           if (cp_lexer_next_token_is_not (parser->lexer,
16780                                           CPP_COLON)
16781               && cp_lexer_next_token_is_not (parser->lexer,
16782                                              CPP_SCOPE)
16783               && cp_lexer_next_token_is_not (parser->lexer,
16784                                              CPP_CLOSE_PAREN))
16785             inputs = cp_parser_asm_operand_list (parser);
16786
16787             if (inputs == error_mark_node)
16788               invalid_inputs_p = true;
16789         }
16790       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16791         /* The clobbers are coming next.  */
16792         clobbers_p = true;
16793
16794       /* Look for clobbers.  */
16795       if (clobbers_p
16796           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16797         {
16798           clobbers_p = true;
16799           /* Consume the `:' or `::'.  */
16800           cp_lexer_consume_token (parser->lexer);
16801           /* Parse the clobbers.  */
16802           if (cp_lexer_next_token_is_not (parser->lexer,
16803                                           CPP_COLON)
16804               && cp_lexer_next_token_is_not (parser->lexer,
16805                                              CPP_CLOSE_PAREN))
16806             clobbers = cp_parser_asm_clobber_list (parser);
16807         }
16808       else if (goto_p
16809                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16810         /* The labels are coming next.  */
16811         labels_p = true;
16812
16813       /* Look for labels.  */
16814       if (labels_p
16815           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16816         {
16817           labels_p = true;
16818           /* Consume the `:' or `::'.  */
16819           cp_lexer_consume_token (parser->lexer);
16820           /* Parse the labels.  */
16821           labels = cp_parser_asm_label_list (parser);
16822         }
16823
16824       if (goto_p && !labels_p)
16825         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16826     }
16827   else if (goto_p)
16828     missing = RT_COLON_SCOPE;
16829
16830   /* Look for the closing `)'.  */
16831   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16832                           missing ? missing : RT_CLOSE_PAREN))
16833     cp_parser_skip_to_closing_parenthesis (parser, true, false,
16834                                            /*consume_paren=*/true);
16835   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16836
16837   if (!invalid_inputs_p && !invalid_outputs_p)
16838     {
16839       /* Create the ASM_EXPR.  */
16840       if (parser->in_function_body)
16841         {
16842           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16843                                       inputs, clobbers, labels);
16844           /* If the extended syntax was not used, mark the ASM_EXPR.  */
16845           if (!extended_p)
16846             {
16847               tree temp = asm_stmt;
16848               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16849                 temp = TREE_OPERAND (temp, 0);
16850
16851               ASM_INPUT_P (temp) = 1;
16852             }
16853         }
16854       else
16855         symtab->finalize_toplevel_asm (string);
16856     }
16857 }
16858
16859 /* Declarators [gram.dcl.decl] */
16860
16861 /* Parse an init-declarator.
16862
16863    init-declarator:
16864      declarator initializer [opt]
16865
16866    GNU Extension:
16867
16868    init-declarator:
16869      declarator asm-specification [opt] attributes [opt] initializer [opt]
16870
16871    function-definition:
16872      decl-specifier-seq [opt] declarator ctor-initializer [opt]
16873        function-body
16874      decl-specifier-seq [opt] declarator function-try-block
16875
16876    GNU Extension:
16877
16878    function-definition:
16879      __extension__ function-definition
16880
16881    TM Extension:
16882
16883    function-definition:
16884      decl-specifier-seq [opt] declarator function-transaction-block
16885
16886    The DECL_SPECIFIERS apply to this declarator.  Returns a
16887    representation of the entity declared.  If MEMBER_P is TRUE, then
16888    this declarator appears in a class scope.  The new DECL created by
16889    this declarator is returned.
16890
16891    The CHECKS are access checks that should be performed once we know
16892    what entity is being declared (and, therefore, what classes have
16893    befriended it).
16894
16895    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16896    for a function-definition here as well.  If the declarator is a
16897    declarator for a function-definition, *FUNCTION_DEFINITION_P will
16898    be TRUE upon return.  By that point, the function-definition will
16899    have been completely parsed.
16900
16901    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16902    is FALSE.
16903
16904    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16905    parsed declaration if it is an uninitialized single declarator not followed
16906    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16907    if present, will not be consumed.  If returned, this declarator will be
16908    created with SD_INITIALIZED but will not call cp_finish_decl.
16909
16910    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16911    and there is an initializer, the pointed location_t is set to the
16912    location of the '=' or `(', or '{' in C++11 token introducing the
16913    initializer.  */
16914
16915 static tree
16916 cp_parser_init_declarator (cp_parser* parser,
16917                            cp_decl_specifier_seq *decl_specifiers,
16918                            vec<deferred_access_check, va_gc> *checks,
16919                            bool function_definition_allowed_p,
16920                            bool member_p,
16921                            int declares_class_or_enum,
16922                            bool* function_definition_p,
16923                            tree* maybe_range_for_decl,
16924                            location_t* init_loc)
16925 {
16926   cp_token *token = NULL, *asm_spec_start_token = NULL,
16927            *attributes_start_token = NULL;
16928   cp_declarator *declarator;
16929   tree prefix_attributes;
16930   tree attributes = NULL;
16931   tree asm_specification;
16932   tree initializer;
16933   tree decl = NULL_TREE;
16934   tree scope;
16935   int is_initialized;
16936   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
16937      initialized with "= ..", CPP_OPEN_PAREN if initialized with
16938      "(...)".  */
16939   enum cpp_ttype initialization_kind;
16940   bool is_direct_init = false;
16941   bool is_non_constant_init;
16942   int ctor_dtor_or_conv_p;
16943   bool friend_p = cp_parser_friend_p (decl_specifiers);
16944   tree pushed_scope = NULL_TREE;
16945   bool range_for_decl_p = false;
16946   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16947   location_t tmp_init_loc = UNKNOWN_LOCATION;
16948
16949   /* Gather the attributes that were provided with the
16950      decl-specifiers.  */
16951   prefix_attributes = decl_specifiers->attributes;
16952
16953   /* Assume that this is not the declarator for a function
16954      definition.  */
16955   if (function_definition_p)
16956     *function_definition_p = false;
16957
16958   /* Default arguments are only permitted for function parameters.  */
16959   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16960     parser->default_arg_ok_p = false;
16961
16962   /* Defer access checks while parsing the declarator; we cannot know
16963      what names are accessible until we know what is being
16964      declared.  */
16965   resume_deferring_access_checks ();
16966
16967   /* Parse the declarator.  */
16968   token = cp_lexer_peek_token (parser->lexer);
16969   declarator
16970     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16971                             &ctor_dtor_or_conv_p,
16972                             /*parenthesized_p=*/NULL,
16973                             member_p, friend_p);
16974   /* Gather up the deferred checks.  */
16975   stop_deferring_access_checks ();
16976
16977   parser->default_arg_ok_p = saved_default_arg_ok_p;
16978
16979   /* If the DECLARATOR was erroneous, there's no need to go
16980      further.  */
16981   if (declarator == cp_error_declarator)
16982     return error_mark_node;
16983
16984   /* Check that the number of template-parameter-lists is OK.  */
16985   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16986                                                        token->location))
16987     return error_mark_node;
16988
16989   if (declares_class_or_enum & 2)
16990     cp_parser_check_for_definition_in_return_type (declarator,
16991                                                    decl_specifiers->type,
16992                                                    decl_specifiers->locations[ds_type_spec]);
16993
16994   /* Figure out what scope the entity declared by the DECLARATOR is
16995      located in.  `grokdeclarator' sometimes changes the scope, so
16996      we compute it now.  */
16997   scope = get_scope_of_declarator (declarator);
16998
16999   /* Perform any lookups in the declared type which were thought to be
17000      dependent, but are not in the scope of the declarator.  */
17001   decl_specifiers->type
17002     = maybe_update_decl_type (decl_specifiers->type, scope);
17003
17004   /* If we're allowing GNU extensions, look for an
17005      asm-specification.  */
17006   if (cp_parser_allow_gnu_extensions_p (parser))
17007     {
17008       /* Look for an asm-specification.  */
17009       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17010       asm_specification = cp_parser_asm_specification_opt (parser);
17011     }
17012   else
17013     asm_specification = NULL_TREE;
17014
17015   /* Look for attributes.  */
17016   attributes_start_token = cp_lexer_peek_token (parser->lexer);
17017   attributes = cp_parser_attributes_opt (parser);
17018
17019   /* Peek at the next token.  */
17020   token = cp_lexer_peek_token (parser->lexer);
17021
17022   bool bogus_implicit_tmpl = false;
17023
17024   if (function_declarator_p (declarator))
17025     {
17026       /* Check to see if the token indicates the start of a
17027          function-definition.  */
17028       if (cp_parser_token_starts_function_definition_p (token))
17029         {
17030           if (!function_definition_allowed_p)
17031             {
17032               /* If a function-definition should not appear here, issue an
17033                  error message.  */
17034               cp_parser_error (parser,
17035                                "a function-definition is not allowed here");
17036               return error_mark_node;
17037             }
17038
17039           location_t func_brace_location
17040             = cp_lexer_peek_token (parser->lexer)->location;
17041
17042           /* Neither attributes nor an asm-specification are allowed
17043              on a function-definition.  */
17044           if (asm_specification)
17045             error_at (asm_spec_start_token->location,
17046                       "an asm-specification is not allowed "
17047                       "on a function-definition");
17048           if (attributes)
17049             error_at (attributes_start_token->location,
17050                       "attributes are not allowed "
17051                       "on a function-definition");
17052           /* This is a function-definition.  */
17053           *function_definition_p = true;
17054
17055           /* Parse the function definition.  */
17056           if (member_p)
17057             decl = cp_parser_save_member_function_body (parser,
17058                                                         decl_specifiers,
17059                                                         declarator,
17060                                                         prefix_attributes);
17061           else
17062             decl =
17063               (cp_parser_function_definition_from_specifiers_and_declarator
17064                (parser, decl_specifiers, prefix_attributes, declarator));
17065
17066           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17067             {
17068               /* This is where the prologue starts...  */
17069               DECL_STRUCT_FUNCTION (decl)->function_start_locus
17070                 = func_brace_location;
17071             }
17072
17073           return decl;
17074         }
17075     }
17076   else if (parser->fully_implicit_function_template_p)
17077     {
17078       /* A non-template declaration involving a function parameter list
17079          containing an implicit template parameter will be made into a
17080          template.  If the resulting declaration is not going to be an
17081          actual function then finish the template scope here to prevent it.
17082          An error message will be issued once we have a decl to talk about.
17083
17084          FIXME probably we should do type deduction rather than create an
17085          implicit template, but the standard currently doesn't allow it. */
17086       bogus_implicit_tmpl = true;
17087       finish_fully_implicit_template (parser, NULL_TREE);
17088     }
17089
17090   /* [dcl.dcl]
17091
17092      Only in function declarations for constructors, destructors, and
17093      type conversions can the decl-specifier-seq be omitted.
17094
17095      We explicitly postpone this check past the point where we handle
17096      function-definitions because we tolerate function-definitions
17097      that are missing their return types in some modes.  */
17098   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17099     {
17100       cp_parser_error (parser,
17101                        "expected constructor, destructor, or type conversion");
17102       return error_mark_node;
17103     }
17104
17105   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
17106   if (token->type == CPP_EQ
17107       || token->type == CPP_OPEN_PAREN
17108       || token->type == CPP_OPEN_BRACE)
17109     {
17110       is_initialized = SD_INITIALIZED;
17111       initialization_kind = token->type;
17112       if (maybe_range_for_decl)
17113         *maybe_range_for_decl = error_mark_node;
17114       tmp_init_loc = token->location;
17115       if (init_loc && *init_loc == UNKNOWN_LOCATION)
17116         *init_loc = tmp_init_loc;
17117
17118       if (token->type == CPP_EQ
17119           && function_declarator_p (declarator))
17120         {
17121           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17122           if (t2->keyword == RID_DEFAULT)
17123             is_initialized = SD_DEFAULTED;
17124           else if (t2->keyword == RID_DELETE)
17125             is_initialized = SD_DELETED;
17126         }
17127     }
17128   else
17129     {
17130       /* If the init-declarator isn't initialized and isn't followed by a
17131          `,' or `;', it's not a valid init-declarator.  */
17132       if (token->type != CPP_COMMA
17133           && token->type != CPP_SEMICOLON)
17134         {
17135           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17136             range_for_decl_p = true;
17137           else
17138             {
17139               if (!maybe_range_for_decl)
17140                 cp_parser_error (parser, "expected initializer");
17141               return error_mark_node;
17142             }
17143         }
17144       is_initialized = SD_UNINITIALIZED;
17145       initialization_kind = CPP_EOF;
17146     }
17147
17148   /* Because start_decl has side-effects, we should only call it if we
17149      know we're going ahead.  By this point, we know that we cannot
17150      possibly be looking at any other construct.  */
17151   cp_parser_commit_to_tentative_parse (parser);
17152
17153   /* Enter the newly declared entry in the symbol table.  If we're
17154      processing a declaration in a class-specifier, we wait until
17155      after processing the initializer.  */
17156   if (!member_p)
17157     {
17158       if (parser->in_unbraced_linkage_specification_p)
17159         decl_specifiers->storage_class = sc_extern;
17160       decl = start_decl (declarator, decl_specifiers,
17161                          range_for_decl_p? SD_INITIALIZED : is_initialized,
17162                          attributes, prefix_attributes, &pushed_scope);
17163       cp_finalize_omp_declare_simd (parser, decl);
17164       /* Adjust location of decl if declarator->id_loc is more appropriate:
17165          set, and decl wasn't merged with another decl, in which case its
17166          location would be different from input_location, and more accurate.  */
17167       if (DECL_P (decl)
17168           && declarator->id_loc != UNKNOWN_LOCATION
17169           && DECL_SOURCE_LOCATION (decl) == input_location)
17170         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17171     }
17172   else if (scope)
17173     /* Enter the SCOPE.  That way unqualified names appearing in the
17174        initializer will be looked up in SCOPE.  */
17175     pushed_scope = push_scope (scope);
17176
17177   /* Perform deferred access control checks, now that we know in which
17178      SCOPE the declared entity resides.  */
17179   if (!member_p && decl)
17180     {
17181       tree saved_current_function_decl = NULL_TREE;
17182
17183       /* If the entity being declared is a function, pretend that we
17184          are in its scope.  If it is a `friend', it may have access to
17185          things that would not otherwise be accessible.  */
17186       if (TREE_CODE (decl) == FUNCTION_DECL)
17187         {
17188           saved_current_function_decl = current_function_decl;
17189           current_function_decl = decl;
17190         }
17191
17192       /* Perform access checks for template parameters.  */
17193       cp_parser_perform_template_parameter_access_checks (checks);
17194
17195       /* Perform the access control checks for the declarator and the
17196          decl-specifiers.  */
17197       perform_deferred_access_checks (tf_warning_or_error);
17198
17199       /* Restore the saved value.  */
17200       if (TREE_CODE (decl) == FUNCTION_DECL)
17201         current_function_decl = saved_current_function_decl;
17202     }
17203
17204   /* Parse the initializer.  */
17205   initializer = NULL_TREE;
17206   is_direct_init = false;
17207   is_non_constant_init = true;
17208   if (is_initialized)
17209     {
17210       if (function_declarator_p (declarator))
17211         {
17212            if (initialization_kind == CPP_EQ)
17213              initializer = cp_parser_pure_specifier (parser);
17214            else
17215              {
17216                /* If the declaration was erroneous, we don't really
17217                   know what the user intended, so just silently
17218                   consume the initializer.  */
17219                if (decl != error_mark_node)
17220                  error_at (tmp_init_loc, "initializer provided for function");
17221                cp_parser_skip_to_closing_parenthesis (parser,
17222                                                       /*recovering=*/true,
17223                                                       /*or_comma=*/false,
17224                                                       /*consume_paren=*/true);
17225              }
17226         }
17227       else
17228         {
17229           /* We want to record the extra mangling scope for in-class
17230              initializers of class members and initializers of static data
17231              member templates.  The former involves deferring
17232              parsing of the initializer until end of class as with default
17233              arguments.  So right here we only handle the latter.  */
17234           if (!member_p && processing_template_decl)
17235             start_lambda_scope (decl);
17236           initializer = cp_parser_initializer (parser,
17237                                                &is_direct_init,
17238                                                &is_non_constant_init);
17239           if (!member_p && processing_template_decl)
17240             finish_lambda_scope ();
17241           if (initializer == error_mark_node)
17242             cp_parser_skip_to_end_of_statement (parser);
17243         }
17244     }
17245
17246   /* The old parser allows attributes to appear after a parenthesized
17247      initializer.  Mark Mitchell proposed removing this functionality
17248      on the GCC mailing lists on 2002-08-13.  This parser accepts the
17249      attributes -- but ignores them.  */
17250   if (cp_parser_allow_gnu_extensions_p (parser)
17251       && initialization_kind == CPP_OPEN_PAREN)
17252     if (cp_parser_attributes_opt (parser))
17253       warning (OPT_Wattributes,
17254                "attributes after parenthesized initializer ignored");
17255
17256   /* And now complain about a non-function implicit template.  */
17257   if (bogus_implicit_tmpl)
17258     error_at (DECL_SOURCE_LOCATION (decl),
17259               "non-function %qD declared as implicit template", decl);
17260
17261   /* For an in-class declaration, use `grokfield' to create the
17262      declaration.  */
17263   if (member_p)
17264     {
17265       if (pushed_scope)
17266         {
17267           pop_scope (pushed_scope);
17268           pushed_scope = NULL_TREE;
17269         }
17270       decl = grokfield (declarator, decl_specifiers,
17271                         initializer, !is_non_constant_init,
17272                         /*asmspec=*/NULL_TREE,
17273                         chainon (attributes, prefix_attributes));
17274       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17275         cp_parser_save_default_args (parser, decl);
17276       cp_finalize_omp_declare_simd (parser, decl);
17277     }
17278
17279   /* Finish processing the declaration.  But, skip member
17280      declarations.  */
17281   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17282     {
17283       cp_finish_decl (decl,
17284                       initializer, !is_non_constant_init,
17285                       asm_specification,
17286                       /* If the initializer is in parentheses, then this is
17287                          a direct-initialization, which means that an
17288                          `explicit' constructor is OK.  Otherwise, an
17289                          `explicit' constructor cannot be used.  */
17290                       ((is_direct_init || !is_initialized)
17291                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17292     }
17293   else if ((cxx_dialect != cxx98) && friend_p
17294            && decl && TREE_CODE (decl) == FUNCTION_DECL)
17295     /* Core issue #226 (C++0x only): A default template-argument
17296        shall not be specified in a friend class template
17297        declaration. */
17298     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
17299                              /*is_partial=*/false, /*is_friend_decl=*/1);
17300
17301   if (!friend_p && pushed_scope)
17302     pop_scope (pushed_scope);
17303
17304   if (function_declarator_p (declarator)
17305       && parser->fully_implicit_function_template_p)
17306     {
17307       if (member_p)
17308         decl = finish_fully_implicit_template (parser, decl);
17309       else
17310         finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17311     }
17312
17313   return decl;
17314 }
17315
17316 /* Parse a declarator.
17317
17318    declarator:
17319      direct-declarator
17320      ptr-operator declarator
17321
17322    abstract-declarator:
17323      ptr-operator abstract-declarator [opt]
17324      direct-abstract-declarator
17325
17326    GNU Extensions:
17327
17328    declarator:
17329      attributes [opt] direct-declarator
17330      attributes [opt] ptr-operator declarator
17331
17332    abstract-declarator:
17333      attributes [opt] ptr-operator abstract-declarator [opt]
17334      attributes [opt] direct-abstract-declarator
17335
17336    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17337    detect constructor, destructor or conversion operators. It is set
17338    to -1 if the declarator is a name, and +1 if it is a
17339    function. Otherwise it is set to zero. Usually you just want to
17340    test for >0, but internally the negative value is used.
17341
17342    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17343    a decl-specifier-seq unless it declares a constructor, destructor,
17344    or conversion.  It might seem that we could check this condition in
17345    semantic analysis, rather than parsing, but that makes it difficult
17346    to handle something like `f()'.  We want to notice that there are
17347    no decl-specifiers, and therefore realize that this is an
17348    expression, not a declaration.)
17349
17350    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17351    the declarator is a direct-declarator of the form "(...)".
17352
17353    MEMBER_P is true iff this declarator is a member-declarator.
17354
17355    FRIEND_P is true iff this declarator is a friend.  */
17356
17357 static cp_declarator *
17358 cp_parser_declarator (cp_parser* parser,
17359                       cp_parser_declarator_kind dcl_kind,
17360                       int* ctor_dtor_or_conv_p,
17361                       bool* parenthesized_p,
17362                       bool member_p, bool friend_p)
17363 {
17364   cp_declarator *declarator;
17365   enum tree_code code;
17366   cp_cv_quals cv_quals;
17367   tree class_type;
17368   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17369
17370   /* Assume this is not a constructor, destructor, or type-conversion
17371      operator.  */
17372   if (ctor_dtor_or_conv_p)
17373     *ctor_dtor_or_conv_p = 0;
17374
17375   if (cp_parser_allow_gnu_extensions_p (parser))
17376     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17377
17378   /* Check for the ptr-operator production.  */
17379   cp_parser_parse_tentatively (parser);
17380   /* Parse the ptr-operator.  */
17381   code = cp_parser_ptr_operator (parser,
17382                                  &class_type,
17383                                  &cv_quals,
17384                                  &std_attributes);
17385
17386   /* If that worked, then we have a ptr-operator.  */
17387   if (cp_parser_parse_definitely (parser))
17388     {
17389       /* If a ptr-operator was found, then this declarator was not
17390          parenthesized.  */
17391       if (parenthesized_p)
17392         *parenthesized_p = true;
17393       /* The dependent declarator is optional if we are parsing an
17394          abstract-declarator.  */
17395       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17396         cp_parser_parse_tentatively (parser);
17397
17398       /* Parse the dependent declarator.  */
17399       declarator = cp_parser_declarator (parser, dcl_kind,
17400                                          /*ctor_dtor_or_conv_p=*/NULL,
17401                                          /*parenthesized_p=*/NULL,
17402                                          /*member_p=*/false,
17403                                          friend_p);
17404
17405       /* If we are parsing an abstract-declarator, we must handle the
17406          case where the dependent declarator is absent.  */
17407       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17408           && !cp_parser_parse_definitely (parser))
17409         declarator = NULL;
17410
17411       declarator = cp_parser_make_indirect_declarator
17412         (code, class_type, cv_quals, declarator, std_attributes);
17413     }
17414   /* Everything else is a direct-declarator.  */
17415   else
17416     {
17417       if (parenthesized_p)
17418         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17419                                                    CPP_OPEN_PAREN);
17420       declarator = cp_parser_direct_declarator (parser, dcl_kind,
17421                                                 ctor_dtor_or_conv_p,
17422                                                 member_p, friend_p);
17423     }
17424
17425   if (gnu_attributes && declarator && declarator != cp_error_declarator)
17426     declarator->attributes = gnu_attributes;
17427   return declarator;
17428 }
17429
17430 /* Parse a direct-declarator or direct-abstract-declarator.
17431
17432    direct-declarator:
17433      declarator-id
17434      direct-declarator ( parameter-declaration-clause )
17435        cv-qualifier-seq [opt]
17436        ref-qualifier [opt]
17437        exception-specification [opt]
17438      direct-declarator [ constant-expression [opt] ]
17439      ( declarator )
17440
17441    direct-abstract-declarator:
17442      direct-abstract-declarator [opt]
17443        ( parameter-declaration-clause )
17444        cv-qualifier-seq [opt]
17445        ref-qualifier [opt]
17446        exception-specification [opt]
17447      direct-abstract-declarator [opt] [ constant-expression [opt] ]
17448      ( abstract-declarator )
17449
17450    Returns a representation of the declarator.  DCL_KIND is
17451    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17452    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
17453    we are parsing a direct-declarator.  It is
17454    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17455    of ambiguity we prefer an abstract declarator, as per
17456    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17457    as for cp_parser_declarator.  */
17458
17459 static cp_declarator *
17460 cp_parser_direct_declarator (cp_parser* parser,
17461                              cp_parser_declarator_kind dcl_kind,
17462                              int* ctor_dtor_or_conv_p,
17463                              bool member_p, bool friend_p)
17464 {
17465   cp_token *token;
17466   cp_declarator *declarator = NULL;
17467   tree scope = NULL_TREE;
17468   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17469   bool saved_in_declarator_p = parser->in_declarator_p;
17470   bool first = true;
17471   tree pushed_scope = NULL_TREE;
17472
17473   while (true)
17474     {
17475       /* Peek at the next token.  */
17476       token = cp_lexer_peek_token (parser->lexer);
17477       if (token->type == CPP_OPEN_PAREN)
17478         {
17479           /* This is either a parameter-declaration-clause, or a
17480              parenthesized declarator. When we know we are parsing a
17481              named declarator, it must be a parenthesized declarator
17482              if FIRST is true. For instance, `(int)' is a
17483              parameter-declaration-clause, with an omitted
17484              direct-abstract-declarator. But `((*))', is a
17485              parenthesized abstract declarator. Finally, when T is a
17486              template parameter `(T)' is a
17487              parameter-declaration-clause, and not a parenthesized
17488              named declarator.
17489
17490              We first try and parse a parameter-declaration-clause,
17491              and then try a nested declarator (if FIRST is true).
17492
17493              It is not an error for it not to be a
17494              parameter-declaration-clause, even when FIRST is
17495              false. Consider,
17496
17497                int i (int);
17498                int i (3);
17499
17500              The first is the declaration of a function while the
17501              second is the definition of a variable, including its
17502              initializer.
17503
17504              Having seen only the parenthesis, we cannot know which of
17505              these two alternatives should be selected.  Even more
17506              complex are examples like:
17507
17508                int i (int (a));
17509                int i (int (3));
17510
17511              The former is a function-declaration; the latter is a
17512              variable initialization.
17513
17514              Thus again, we try a parameter-declaration-clause, and if
17515              that fails, we back out and return.  */
17516
17517           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17518             {
17519               tree params;
17520               bool is_declarator = false;
17521
17522               /* In a member-declarator, the only valid interpretation
17523                  of a parenthesis is the start of a
17524                  parameter-declaration-clause.  (It is invalid to
17525                  initialize a static data member with a parenthesized
17526                  initializer; only the "=" form of initialization is
17527                  permitted.)  */
17528               if (!member_p)
17529                 cp_parser_parse_tentatively (parser);
17530
17531               /* Consume the `('.  */
17532               cp_lexer_consume_token (parser->lexer);
17533               if (first)
17534                 {
17535                   /* If this is going to be an abstract declarator, we're
17536                      in a declarator and we can't have default args.  */
17537                   parser->default_arg_ok_p = false;
17538                   parser->in_declarator_p = true;
17539                 }
17540
17541               begin_scope (sk_function_parms, NULL_TREE);
17542
17543               /* Parse the parameter-declaration-clause.  */
17544               params = cp_parser_parameter_declaration_clause (parser);
17545
17546               /* Consume the `)'.  */
17547               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17548
17549               /* If all went well, parse the cv-qualifier-seq,
17550                  ref-qualifier and the exception-specification.  */
17551               if (member_p || cp_parser_parse_definitely (parser))
17552                 {
17553                   cp_cv_quals cv_quals;
17554                   cp_virt_specifiers virt_specifiers;
17555                   cp_ref_qualifier ref_qual;
17556                   tree exception_specification;
17557                   tree late_return;
17558                   tree attrs;
17559                   bool memfn = (member_p || (pushed_scope
17560                                              && CLASS_TYPE_P (pushed_scope)));
17561
17562                   is_declarator = true;
17563
17564                   if (ctor_dtor_or_conv_p)
17565                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17566                   first = false;
17567
17568                   /* Parse the cv-qualifier-seq.  */
17569                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17570                   /* Parse the ref-qualifier. */
17571                   ref_qual = cp_parser_ref_qualifier_opt (parser);
17572                   /* And the exception-specification.  */
17573                   exception_specification
17574                     = cp_parser_exception_specification_opt (parser);
17575
17576                   attrs = cp_parser_std_attribute_spec_seq (parser);
17577
17578                   /* In here, we handle cases where attribute is used after
17579                      the function declaration.  For example:
17580                      void func (int x) __attribute__((vector(..)));  */
17581                   if (flag_cilkplus
17582                       && cp_next_tokens_can_be_gnu_attribute_p (parser))
17583                     {
17584                       cp_parser_parse_tentatively (parser);
17585                       tree attr = cp_parser_gnu_attributes_opt (parser);
17586                       if (cp_lexer_next_token_is_not (parser->lexer,
17587                                                       CPP_SEMICOLON)
17588                           && cp_lexer_next_token_is_not (parser->lexer,
17589                                                          CPP_OPEN_BRACE))
17590                         cp_parser_abort_tentative_parse (parser);
17591                       else if (!cp_parser_parse_definitely (parser))
17592                         ;
17593                       else
17594                         attrs = chainon (attr, attrs);
17595                     }
17596                   late_return = (cp_parser_late_return_type_opt
17597                                  (parser, declarator,
17598                                   memfn ? cv_quals : -1));
17599
17600
17601                   /* Parse the virt-specifier-seq.  */
17602                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17603
17604                   /* Create the function-declarator.  */
17605                   declarator = make_call_declarator (declarator,
17606                                                      params,
17607                                                      cv_quals,
17608                                                      virt_specifiers,
17609                                                      ref_qual,
17610                                                      exception_specification,
17611                                                      late_return);
17612                   declarator->std_attributes = attrs;
17613                   /* Any subsequent parameter lists are to do with
17614                      return type, so are not those of the declared
17615                      function.  */
17616                   parser->default_arg_ok_p = false;
17617                 }
17618
17619               /* Remove the function parms from scope.  */
17620               pop_bindings_and_leave_scope ();
17621
17622               if (is_declarator)
17623                 /* Repeat the main loop.  */
17624                 continue;
17625             }
17626
17627           /* If this is the first, we can try a parenthesized
17628              declarator.  */
17629           if (first)
17630             {
17631               bool saved_in_type_id_in_expr_p;
17632
17633               parser->default_arg_ok_p = saved_default_arg_ok_p;
17634               parser->in_declarator_p = saved_in_declarator_p;
17635
17636               /* Consume the `('.  */
17637               cp_lexer_consume_token (parser->lexer);
17638               /* Parse the nested declarator.  */
17639               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17640               parser->in_type_id_in_expr_p = true;
17641               declarator
17642                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17643                                         /*parenthesized_p=*/NULL,
17644                                         member_p, friend_p);
17645               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17646               first = false;
17647               /* Expect a `)'.  */
17648               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17649                 declarator = cp_error_declarator;
17650               if (declarator == cp_error_declarator)
17651                 break;
17652
17653               goto handle_declarator;
17654             }
17655           /* Otherwise, we must be done.  */
17656           else
17657             break;
17658         }
17659       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17660                && token->type == CPP_OPEN_SQUARE
17661                && !cp_next_tokens_can_be_attribute_p (parser))
17662         {
17663           /* Parse an array-declarator.  */
17664           tree bounds, attrs;
17665
17666           if (ctor_dtor_or_conv_p)
17667             *ctor_dtor_or_conv_p = 0;
17668
17669           first = false;
17670           parser->default_arg_ok_p = false;
17671           parser->in_declarator_p = true;
17672           /* Consume the `['.  */
17673           cp_lexer_consume_token (parser->lexer);
17674           /* Peek at the next token.  */
17675           token = cp_lexer_peek_token (parser->lexer);
17676           /* If the next token is `]', then there is no
17677              constant-expression.  */
17678           if (token->type != CPP_CLOSE_SQUARE)
17679             {
17680               bool non_constant_p;
17681               bounds
17682                 = cp_parser_constant_expression (parser,
17683                                                  /*allow_non_constant=*/true,
17684                                                  &non_constant_p);
17685               if (!non_constant_p)
17686                 /* OK */;
17687               else if (error_operand_p (bounds))
17688                 /* Already gave an error.  */;
17689               else if (!parser->in_function_body
17690                        || current_binding_level->kind == sk_function_parms)
17691                 {
17692                   /* Normally, the array bound must be an integral constant
17693                      expression.  However, as an extension, we allow VLAs
17694                      in function scopes as long as they aren't part of a
17695                      parameter declaration.  */
17696                   cp_parser_error (parser,
17697                                    "array bound is not an integer constant");
17698                   bounds = error_mark_node;
17699                 }
17700               else if (processing_template_decl
17701                        && !type_dependent_expression_p (bounds))
17702                 {
17703                   /* Remember this wasn't a constant-expression.  */
17704                   bounds = build_nop (TREE_TYPE (bounds), bounds);
17705                   TREE_SIDE_EFFECTS (bounds) = 1;
17706                 }
17707             }
17708           else
17709             bounds = NULL_TREE;
17710           /* Look for the closing `]'.  */
17711           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17712             {
17713               declarator = cp_error_declarator;
17714               break;
17715             }
17716
17717           attrs = cp_parser_std_attribute_spec_seq (parser);
17718           declarator = make_array_declarator (declarator, bounds);
17719           declarator->std_attributes = attrs;
17720         }
17721       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17722         {
17723           {
17724             tree qualifying_scope;
17725             tree unqualified_name;
17726             tree attrs;
17727             special_function_kind sfk;
17728             bool abstract_ok;
17729             bool pack_expansion_p = false;
17730             cp_token *declarator_id_start_token;
17731
17732             /* Parse a declarator-id */
17733             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17734             if (abstract_ok)
17735               {
17736                 cp_parser_parse_tentatively (parser);
17737
17738                 /* If we see an ellipsis, we should be looking at a
17739                    parameter pack. */
17740                 if (token->type == CPP_ELLIPSIS)
17741                   {
17742                     /* Consume the `...' */
17743                     cp_lexer_consume_token (parser->lexer);
17744
17745                     pack_expansion_p = true;
17746                   }
17747               }
17748
17749             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17750             unqualified_name
17751               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17752             qualifying_scope = parser->scope;
17753             if (abstract_ok)
17754               {
17755                 bool okay = false;
17756
17757                 if (!unqualified_name && pack_expansion_p)
17758                   {
17759                     /* Check whether an error occurred. */
17760                     okay = !cp_parser_error_occurred (parser);
17761
17762                     /* We already consumed the ellipsis to mark a
17763                        parameter pack, but we have no way to report it,
17764                        so abort the tentative parse. We will be exiting
17765                        immediately anyway. */
17766                     cp_parser_abort_tentative_parse (parser);
17767                   }
17768                 else
17769                   okay = cp_parser_parse_definitely (parser);
17770
17771                 if (!okay)
17772                   unqualified_name = error_mark_node;
17773                 else if (unqualified_name
17774                          && (qualifying_scope
17775                              || (!identifier_p (unqualified_name))))
17776                   {
17777                     cp_parser_error (parser, "expected unqualified-id");
17778                     unqualified_name = error_mark_node;
17779                   }
17780               }
17781
17782             if (!unqualified_name)
17783               return NULL;
17784             if (unqualified_name == error_mark_node)
17785               {
17786                 declarator = cp_error_declarator;
17787                 pack_expansion_p = false;
17788                 declarator->parameter_pack_p = false;
17789                 break;
17790               }
17791
17792             attrs = cp_parser_std_attribute_spec_seq (parser);
17793
17794             if (qualifying_scope && at_namespace_scope_p ()
17795                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17796               {
17797                 /* In the declaration of a member of a template class
17798                    outside of the class itself, the SCOPE will sometimes
17799                    be a TYPENAME_TYPE.  For example, given:
17800
17801                    template <typename T>
17802                    int S<T>::R::i = 3;
17803
17804                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
17805                    this context, we must resolve S<T>::R to an ordinary
17806                    type, rather than a typename type.
17807
17808                    The reason we normally avoid resolving TYPENAME_TYPEs
17809                    is that a specialization of `S' might render
17810                    `S<T>::R' not a type.  However, if `S' is
17811                    specialized, then this `i' will not be used, so there
17812                    is no harm in resolving the types here.  */
17813                 tree type;
17814
17815                 /* Resolve the TYPENAME_TYPE.  */
17816                 type = resolve_typename_type (qualifying_scope,
17817                                               /*only_current_p=*/false);
17818                 /* If that failed, the declarator is invalid.  */
17819                 if (TREE_CODE (type) == TYPENAME_TYPE)
17820                   {
17821                     if (typedef_variant_p (type))
17822                       error_at (declarator_id_start_token->location,
17823                                 "cannot define member of dependent typedef "
17824                                 "%qT", type);
17825                     else
17826                       error_at (declarator_id_start_token->location,
17827                                 "%<%T::%E%> is not a type",
17828                                 TYPE_CONTEXT (qualifying_scope),
17829                                 TYPE_IDENTIFIER (qualifying_scope));
17830                   }
17831                 qualifying_scope = type;
17832               }
17833
17834             sfk = sfk_none;
17835
17836             if (unqualified_name)
17837               {
17838                 tree class_type;
17839
17840                 if (qualifying_scope
17841                     && CLASS_TYPE_P (qualifying_scope))
17842                   class_type = qualifying_scope;
17843                 else
17844                   class_type = current_class_type;
17845
17846                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17847                   {
17848                     tree name_type = TREE_TYPE (unqualified_name);
17849                     if (class_type && same_type_p (name_type, class_type))
17850                       {
17851                         if (qualifying_scope
17852                             && CLASSTYPE_USE_TEMPLATE (name_type))
17853                           {
17854                             error_at (declarator_id_start_token->location,
17855                                       "invalid use of constructor as a template");
17856                             inform (declarator_id_start_token->location,
17857                                     "use %<%T::%D%> instead of %<%T::%D%> to "
17858                                     "name the constructor in a qualified name",
17859                                     class_type,
17860                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17861                                     class_type, name_type);
17862                             declarator = cp_error_declarator;
17863                             break;
17864                           }
17865                         else
17866                           unqualified_name = constructor_name (class_type);
17867                       }
17868                     else
17869                       {
17870                         /* We do not attempt to print the declarator
17871                            here because we do not have enough
17872                            information about its original syntactic
17873                            form.  */
17874                         cp_parser_error (parser, "invalid declarator");
17875                         declarator = cp_error_declarator;
17876                         break;
17877                       }
17878                   }
17879
17880                 if (class_type)
17881                   {
17882                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17883                       sfk = sfk_destructor;
17884                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17885                       sfk = sfk_conversion;
17886                     else if (/* There's no way to declare a constructor
17887                                 for an anonymous type, even if the type
17888                                 got a name for linkage purposes.  */
17889                              !TYPE_WAS_ANONYMOUS (class_type)
17890                              /* Handle correctly (c++/19200):
17891
17892                                 struct S {
17893                                   struct T{};
17894                                   friend void S(T);
17895                                 };
17896
17897                                 and also:
17898
17899                                 namespace N {
17900                                   void S();
17901                                 }
17902
17903                                 struct S {
17904                                   friend void N::S();
17905                                 };  */
17906                              && !(friend_p
17907                                   && class_type != qualifying_scope)
17908                              && constructor_name_p (unqualified_name,
17909                                                     class_type))
17910                       {
17911                         unqualified_name = constructor_name (class_type);
17912                         sfk = sfk_constructor;
17913                       }
17914                     else if (is_overloaded_fn (unqualified_name)
17915                              && DECL_CONSTRUCTOR_P (get_first_fn
17916                                                     (unqualified_name)))
17917                       sfk = sfk_constructor;
17918
17919                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
17920                       *ctor_dtor_or_conv_p = -1;
17921                   }
17922               }
17923             declarator = make_id_declarator (qualifying_scope,
17924                                              unqualified_name,
17925                                              sfk);
17926             declarator->std_attributes = attrs;
17927             declarator->id_loc = token->location;
17928             declarator->parameter_pack_p = pack_expansion_p;
17929
17930             if (pack_expansion_p)
17931               maybe_warn_variadic_templates ();
17932           }
17933
17934         handle_declarator:;
17935           scope = get_scope_of_declarator (declarator);
17936           if (scope)
17937             {
17938               /* Any names that appear after the declarator-id for a
17939                  member are looked up in the containing scope.  */
17940               if (at_function_scope_p ())
17941                 {
17942                   /* But declarations with qualified-ids can't appear in a
17943                      function.  */
17944                   cp_parser_error (parser, "qualified-id in declaration");
17945                   declarator = cp_error_declarator;
17946                   break;
17947                 }
17948               pushed_scope = push_scope (scope);
17949             }
17950           parser->in_declarator_p = true;
17951           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17952               || (declarator && declarator->kind == cdk_id))
17953             /* Default args are only allowed on function
17954                declarations.  */
17955             parser->default_arg_ok_p = saved_default_arg_ok_p;
17956           else
17957             parser->default_arg_ok_p = false;
17958
17959           first = false;
17960         }
17961       /* We're done.  */
17962       else
17963         break;
17964     }
17965
17966   /* For an abstract declarator, we might wind up with nothing at this
17967      point.  That's an error; the declarator is not optional.  */
17968   if (!declarator)
17969     cp_parser_error (parser, "expected declarator");
17970
17971   /* If we entered a scope, we must exit it now.  */
17972   if (pushed_scope)
17973     pop_scope (pushed_scope);
17974
17975   parser->default_arg_ok_p = saved_default_arg_ok_p;
17976   parser->in_declarator_p = saved_in_declarator_p;
17977
17978   return declarator;
17979 }
17980
17981 /* Parse a ptr-operator.
17982
17983    ptr-operator:
17984      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17985      * cv-qualifier-seq [opt]
17986      &
17987      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17988      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17989
17990    GNU Extension:
17991
17992    ptr-operator:
17993      & cv-qualifier-seq [opt]
17994
17995    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17996    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17997    an rvalue reference. In the case of a pointer-to-member, *TYPE is
17998    filled in with the TYPE containing the member.  *CV_QUALS is
17999    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18000    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
18001    Note that the tree codes returned by this function have nothing
18002    to do with the types of trees that will be eventually be created
18003    to represent the pointer or reference type being parsed. They are
18004    just constants with suggestive names. */
18005 static enum tree_code
18006 cp_parser_ptr_operator (cp_parser* parser,
18007                         tree* type,
18008                         cp_cv_quals *cv_quals,
18009                         tree *attributes)
18010 {
18011   enum tree_code code = ERROR_MARK;
18012   cp_token *token;
18013   tree attrs = NULL_TREE;
18014
18015   /* Assume that it's not a pointer-to-member.  */
18016   *type = NULL_TREE;
18017   /* And that there are no cv-qualifiers.  */
18018   *cv_quals = TYPE_UNQUALIFIED;
18019
18020   /* Peek at the next token.  */
18021   token = cp_lexer_peek_token (parser->lexer);
18022
18023   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
18024   if (token->type == CPP_MULT)
18025     code = INDIRECT_REF;
18026   else if (token->type == CPP_AND)
18027     code = ADDR_EXPR;
18028   else if ((cxx_dialect != cxx98) &&
18029            token->type == CPP_AND_AND) /* C++0x only */
18030     code = NON_LVALUE_EXPR;
18031
18032   if (code != ERROR_MARK)
18033     {
18034       /* Consume the `*', `&' or `&&'.  */
18035       cp_lexer_consume_token (parser->lexer);
18036
18037       /* A `*' can be followed by a cv-qualifier-seq, and so can a
18038          `&', if we are allowing GNU extensions.  (The only qualifier
18039          that can legally appear after `&' is `restrict', but that is
18040          enforced during semantic analysis.  */
18041       if (code == INDIRECT_REF
18042           || cp_parser_allow_gnu_extensions_p (parser))
18043         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18044
18045       attrs = cp_parser_std_attribute_spec_seq (parser);
18046       if (attributes != NULL)
18047         *attributes = attrs;
18048     }
18049   else
18050     {
18051       /* Try the pointer-to-member case.  */
18052       cp_parser_parse_tentatively (parser);
18053       /* Look for the optional `::' operator.  */
18054       cp_parser_global_scope_opt (parser,
18055                                   /*current_scope_valid_p=*/false);
18056       /* Look for the nested-name specifier.  */
18057       token = cp_lexer_peek_token (parser->lexer);
18058       cp_parser_nested_name_specifier (parser,
18059                                        /*typename_keyword_p=*/false,
18060                                        /*check_dependency_p=*/true,
18061                                        /*type_p=*/false,
18062                                        /*is_declaration=*/false);
18063       /* If we found it, and the next token is a `*', then we are
18064          indeed looking at a pointer-to-member operator.  */
18065       if (!cp_parser_error_occurred (parser)
18066           && cp_parser_require (parser, CPP_MULT, RT_MULT))
18067         {
18068           /* Indicate that the `*' operator was used.  */
18069           code = INDIRECT_REF;
18070
18071           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18072             error_at (token->location, "%qD is a namespace", parser->scope);
18073           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18074             error_at (token->location, "cannot form pointer to member of "
18075                       "non-class %q#T", parser->scope);
18076           else
18077             {
18078               /* The type of which the member is a member is given by the
18079                  current SCOPE.  */
18080               *type = parser->scope;
18081               /* The next name will not be qualified.  */
18082               parser->scope = NULL_TREE;
18083               parser->qualifying_scope = NULL_TREE;
18084               parser->object_scope = NULL_TREE;
18085               /* Look for optional c++11 attributes.  */
18086               attrs = cp_parser_std_attribute_spec_seq (parser);
18087               if (attributes != NULL)
18088                 *attributes = attrs;
18089               /* Look for the optional cv-qualifier-seq.  */
18090               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18091             }
18092         }
18093       /* If that didn't work we don't have a ptr-operator.  */
18094       if (!cp_parser_parse_definitely (parser))
18095         cp_parser_error (parser, "expected ptr-operator");
18096     }
18097
18098   return code;
18099 }
18100
18101 /* Parse an (optional) cv-qualifier-seq.
18102
18103    cv-qualifier-seq:
18104      cv-qualifier cv-qualifier-seq [opt]
18105
18106    cv-qualifier:
18107      const
18108      volatile
18109
18110    GNU Extension:
18111
18112    cv-qualifier:
18113      __restrict__
18114
18115    Returns a bitmask representing the cv-qualifiers.  */
18116
18117 static cp_cv_quals
18118 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18119 {
18120   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18121
18122   while (true)
18123     {
18124       cp_token *token;
18125       cp_cv_quals cv_qualifier;
18126
18127       /* Peek at the next token.  */
18128       token = cp_lexer_peek_token (parser->lexer);
18129       /* See if it's a cv-qualifier.  */
18130       switch (token->keyword)
18131         {
18132         case RID_CONST:
18133           cv_qualifier = TYPE_QUAL_CONST;
18134           break;
18135
18136         case RID_VOLATILE:
18137           cv_qualifier = TYPE_QUAL_VOLATILE;
18138           break;
18139
18140         case RID_RESTRICT:
18141           cv_qualifier = TYPE_QUAL_RESTRICT;
18142           break;
18143
18144         default:
18145           cv_qualifier = TYPE_UNQUALIFIED;
18146           break;
18147         }
18148
18149       if (!cv_qualifier)
18150         break;
18151
18152       if (cv_quals & cv_qualifier)
18153         {
18154           error_at (token->location, "duplicate cv-qualifier");
18155           cp_lexer_purge_token (parser->lexer);
18156         }
18157       else
18158         {
18159           cp_lexer_consume_token (parser->lexer);
18160           cv_quals |= cv_qualifier;
18161         }
18162     }
18163
18164   return cv_quals;
18165 }
18166
18167 /* Parse an (optional) ref-qualifier
18168
18169    ref-qualifier:
18170      &
18171      &&
18172
18173    Returns cp_ref_qualifier representing ref-qualifier. */
18174
18175 static cp_ref_qualifier
18176 cp_parser_ref_qualifier_opt (cp_parser* parser)
18177 {
18178   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18179
18180   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
18181   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18182     return ref_qual;
18183
18184   while (true)
18185     {
18186       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18187       cp_token *token = cp_lexer_peek_token (parser->lexer);
18188
18189       switch (token->type)
18190         {
18191         case CPP_AND:
18192           curr_ref_qual = REF_QUAL_LVALUE;
18193           break;
18194
18195         case CPP_AND_AND:
18196           curr_ref_qual = REF_QUAL_RVALUE;
18197           break;
18198
18199         default:
18200           curr_ref_qual = REF_QUAL_NONE;
18201           break;
18202         }
18203
18204       if (!curr_ref_qual)
18205         break;
18206       else if (ref_qual)
18207         {
18208           error_at (token->location, "multiple ref-qualifiers");
18209           cp_lexer_purge_token (parser->lexer);
18210         }
18211       else
18212         {
18213           ref_qual = curr_ref_qual;
18214           cp_lexer_consume_token (parser->lexer);
18215         }
18216     }
18217
18218   return ref_qual;
18219 }
18220
18221 /* Parse an (optional) virt-specifier-seq.
18222
18223    virt-specifier-seq:
18224      virt-specifier virt-specifier-seq [opt]
18225
18226    virt-specifier:
18227      override
18228      final
18229
18230    Returns a bitmask representing the virt-specifiers.  */
18231
18232 static cp_virt_specifiers
18233 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18234 {
18235   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18236
18237   while (true)
18238     {
18239       cp_token *token;
18240       cp_virt_specifiers virt_specifier;
18241
18242       /* Peek at the next token.  */
18243       token = cp_lexer_peek_token (parser->lexer);
18244       /* See if it's a virt-specifier-qualifier.  */
18245       if (token->type != CPP_NAME)
18246         break;
18247       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18248         {
18249           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18250           virt_specifier = VIRT_SPEC_OVERRIDE;
18251         }
18252       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18253         {
18254           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18255           virt_specifier = VIRT_SPEC_FINAL;
18256         }
18257       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18258         {
18259           virt_specifier = VIRT_SPEC_FINAL;
18260         }
18261       else
18262         break;
18263
18264       if (virt_specifiers & virt_specifier)
18265         {
18266           error_at (token->location, "duplicate virt-specifier");
18267           cp_lexer_purge_token (parser->lexer);
18268         }
18269       else
18270         {
18271           cp_lexer_consume_token (parser->lexer);
18272           virt_specifiers |= virt_specifier;
18273         }
18274     }
18275   return virt_specifiers;
18276 }
18277
18278 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18279    is in scope even though it isn't real.  */
18280
18281 void
18282 inject_this_parameter (tree ctype, cp_cv_quals quals)
18283 {
18284   tree this_parm;
18285
18286   if (current_class_ptr)
18287     {
18288       /* We don't clear this between NSDMIs.  Is it already what we want?  */
18289       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18290       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18291           && cp_type_quals (type) == quals)
18292         return;
18293     }
18294
18295   this_parm = build_this_parm (ctype, quals);
18296   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
18297   current_class_ptr = NULL_TREE;
18298   current_class_ref
18299     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18300   current_class_ptr = this_parm;
18301 }
18302
18303 /* Return true iff our current scope is a non-static data member
18304    initializer.  */
18305
18306 bool
18307 parsing_nsdmi (void)
18308 {
18309   /* We recognize NSDMI context by the context-less 'this' pointer set up
18310      by the function above.  */
18311   if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18312     return true;
18313   return false;
18314 }
18315
18316 /* Parse a late-specified return type, if any.  This is not a separate
18317    non-terminal, but part of a function declarator, which looks like
18318
18319    -> trailing-type-specifier-seq abstract-declarator(opt)
18320
18321    Returns the type indicated by the type-id.
18322
18323    In addition to this this parses any queued up omp declare simd
18324    clauses and Cilk Plus SIMD-enabled function's vector attributes.
18325
18326    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18327    function.  */
18328
18329 static tree
18330 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18331                                 cp_cv_quals quals)
18332 {
18333   cp_token *token;
18334   tree type = NULL_TREE;
18335   bool declare_simd_p = (parser->omp_declare_simd
18336                          && declarator
18337                          && declarator->kind == cdk_id);
18338
18339   bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info 
18340                                 && declarator && declarator->kind == cdk_id);
18341   
18342   /* Peek at the next token.  */
18343   token = cp_lexer_peek_token (parser->lexer);
18344   /* A late-specified return type is indicated by an initial '->'. */
18345   if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18346     return NULL_TREE;
18347
18348   tree save_ccp = current_class_ptr;
18349   tree save_ccr = current_class_ref;
18350   if (quals >= 0)
18351     {
18352       /* DR 1207: 'this' is in scope in the trailing return type.  */
18353       inject_this_parameter (current_class_type, quals);
18354     }
18355
18356   if (token->type == CPP_DEREF)
18357     {
18358       /* Consume the ->.  */
18359       cp_lexer_consume_token (parser->lexer);
18360
18361       type = cp_parser_trailing_type_id (parser);
18362     }
18363
18364   if (cilk_simd_fn_vector_p)
18365     declarator->std_attributes
18366       = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18367                                                   declarator->std_attributes);
18368   if (declare_simd_p)
18369     declarator->std_attributes
18370       = cp_parser_late_parsing_omp_declare_simd (parser,
18371                                                  declarator->std_attributes);
18372
18373   if (quals >= 0)
18374     {
18375       current_class_ptr = save_ccp;
18376       current_class_ref = save_ccr;
18377     }
18378
18379   return type;
18380 }
18381
18382 /* Parse a declarator-id.
18383
18384    declarator-id:
18385      id-expression
18386      :: [opt] nested-name-specifier [opt] type-name
18387
18388    In the `id-expression' case, the value returned is as for
18389    cp_parser_id_expression if the id-expression was an unqualified-id.
18390    If the id-expression was a qualified-id, then a SCOPE_REF is
18391    returned.  The first operand is the scope (either a NAMESPACE_DECL
18392    or TREE_TYPE), but the second is still just a representation of an
18393    unqualified-id.  */
18394
18395 static tree
18396 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18397 {
18398   tree id;
18399   /* The expression must be an id-expression.  Assume that qualified
18400      names are the names of types so that:
18401
18402        template <class T>
18403        int S<T>::R::i = 3;
18404
18405      will work; we must treat `S<T>::R' as the name of a type.
18406      Similarly, assume that qualified names are templates, where
18407      required, so that:
18408
18409        template <class T>
18410        int S<T>::R<T>::i = 3;
18411
18412      will work, too.  */
18413   id = cp_parser_id_expression (parser,
18414                                 /*template_keyword_p=*/false,
18415                                 /*check_dependency_p=*/false,
18416                                 /*template_p=*/NULL,
18417                                 /*declarator_p=*/true,
18418                                 optional_p);
18419   if (id && BASELINK_P (id))
18420     id = BASELINK_FUNCTIONS (id);
18421   return id;
18422 }
18423
18424 /* Parse a type-id.
18425
18426    type-id:
18427      type-specifier-seq abstract-declarator [opt]
18428
18429    Returns the TYPE specified.  */
18430
18431 static tree
18432 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18433                      bool is_trailing_return)
18434 {
18435   cp_decl_specifier_seq type_specifier_seq;
18436   cp_declarator *abstract_declarator;
18437
18438   /* Parse the type-specifier-seq.  */
18439   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18440                                 is_trailing_return,
18441                                 &type_specifier_seq);
18442   if (type_specifier_seq.type == error_mark_node)
18443     return error_mark_node;
18444
18445   /* There might or might not be an abstract declarator.  */
18446   cp_parser_parse_tentatively (parser);
18447   /* Look for the declarator.  */
18448   abstract_declarator
18449     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18450                             /*parenthesized_p=*/NULL,
18451                             /*member_p=*/false,
18452                             /*friend_p=*/false);
18453   /* Check to see if there really was a declarator.  */
18454   if (!cp_parser_parse_definitely (parser))
18455     abstract_declarator = NULL;
18456
18457   if (type_specifier_seq.type
18458       /* None of the valid uses of 'auto' in C++14 involve the type-id
18459          nonterminal, but it is valid in a trailing-return-type.  */
18460       && !(cxx_dialect >= cxx14 && is_trailing_return)
18461       && type_uses_auto (type_specifier_seq.type))
18462     {
18463       /* A type-id with type 'auto' is only ok if the abstract declarator
18464          is a function declarator with a late-specified return type.  */
18465       if (abstract_declarator
18466           && abstract_declarator->kind == cdk_function
18467           && abstract_declarator->u.function.late_return_type)
18468         /* OK */;
18469       else
18470         {
18471           error ("invalid use of %<auto%>");
18472           return error_mark_node;
18473         }
18474     }
18475   
18476   return groktypename (&type_specifier_seq, abstract_declarator,
18477                        is_template_arg);
18478 }
18479
18480 static tree cp_parser_type_id (cp_parser *parser)
18481 {
18482   return cp_parser_type_id_1 (parser, false, false);
18483 }
18484
18485 static tree cp_parser_template_type_arg (cp_parser *parser)
18486 {
18487   tree r;
18488   const char *saved_message = parser->type_definition_forbidden_message;
18489   parser->type_definition_forbidden_message
18490     = G_("types may not be defined in template arguments");
18491   r = cp_parser_type_id_1 (parser, true, false);
18492   parser->type_definition_forbidden_message = saved_message;
18493   if (cxx_dialect >= cxx14 && type_uses_auto (r))
18494     {
18495       error ("invalid use of %<auto%> in template argument");
18496       r = error_mark_node;
18497     }
18498   return r;
18499 }
18500
18501 static tree cp_parser_trailing_type_id (cp_parser *parser)
18502 {
18503   return cp_parser_type_id_1 (parser, false, true);
18504 }
18505
18506 /* Parse a type-specifier-seq.
18507
18508    type-specifier-seq:
18509      type-specifier type-specifier-seq [opt]
18510
18511    GNU extension:
18512
18513    type-specifier-seq:
18514      attributes type-specifier-seq [opt]
18515
18516    If IS_DECLARATION is true, we are at the start of a "condition" or
18517    exception-declaration, so we might be followed by a declarator-id.
18518
18519    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18520    i.e. we've just seen "->".
18521
18522    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
18523
18524 static void
18525 cp_parser_type_specifier_seq (cp_parser* parser,
18526                               bool is_declaration,
18527                               bool is_trailing_return,
18528                               cp_decl_specifier_seq *type_specifier_seq)
18529 {
18530   bool seen_type_specifier = false;
18531   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18532   cp_token *start_token = NULL;
18533
18534   /* Clear the TYPE_SPECIFIER_SEQ.  */
18535   clear_decl_specs (type_specifier_seq);
18536
18537   /* In the context of a trailing return type, enum E { } is an
18538      elaborated-type-specifier followed by a function-body, not an
18539      enum-specifier.  */
18540   if (is_trailing_return)
18541     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18542
18543   /* Parse the type-specifiers and attributes.  */
18544   while (true)
18545     {
18546       tree type_specifier;
18547       bool is_cv_qualifier;
18548
18549       /* Check for attributes first.  */
18550       if (cp_next_tokens_can_be_attribute_p (parser))
18551         {
18552           type_specifier_seq->attributes =
18553             chainon (type_specifier_seq->attributes,
18554                      cp_parser_attributes_opt (parser));
18555           continue;
18556         }
18557
18558       /* record the token of the beginning of the type specifier seq,
18559          for error reporting purposes*/
18560      if (!start_token)
18561        start_token = cp_lexer_peek_token (parser->lexer);
18562
18563       /* Look for the type-specifier.  */
18564       type_specifier = cp_parser_type_specifier (parser,
18565                                                  flags,
18566                                                  type_specifier_seq,
18567                                                  /*is_declaration=*/false,
18568                                                  NULL,
18569                                                  &is_cv_qualifier);
18570       if (!type_specifier)
18571         {
18572           /* If the first type-specifier could not be found, this is not a
18573              type-specifier-seq at all.  */
18574           if (!seen_type_specifier)
18575             {
18576               /* Set in_declarator_p to avoid skipping to the semicolon.  */
18577               int in_decl = parser->in_declarator_p;
18578               parser->in_declarator_p = true;
18579
18580               if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18581                   || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18582                 cp_parser_error (parser, "expected type-specifier");
18583
18584               parser->in_declarator_p = in_decl;
18585
18586               type_specifier_seq->type = error_mark_node;
18587               return;
18588             }
18589           /* If subsequent type-specifiers could not be found, the
18590              type-specifier-seq is complete.  */
18591           break;
18592         }
18593
18594       seen_type_specifier = true;
18595       /* The standard says that a condition can be:
18596
18597             type-specifier-seq declarator = assignment-expression
18598
18599          However, given:
18600
18601            struct S {};
18602            if (int S = ...)
18603
18604          we should treat the "S" as a declarator, not as a
18605          type-specifier.  The standard doesn't say that explicitly for
18606          type-specifier-seq, but it does say that for
18607          decl-specifier-seq in an ordinary declaration.  Perhaps it
18608          would be clearer just to allow a decl-specifier-seq here, and
18609          then add a semantic restriction that if any decl-specifiers
18610          that are not type-specifiers appear, the program is invalid.  */
18611       if (is_declaration && !is_cv_qualifier)
18612         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18613     }
18614 }
18615
18616 /* Return whether the function currently being declared has an associated
18617    template parameter list.  */
18618
18619 static bool
18620 function_being_declared_is_template_p (cp_parser* parser)
18621 {
18622   if (!current_template_parms || processing_template_parmlist)
18623     return false;
18624
18625   if (parser->implicit_template_scope)
18626     return true;
18627
18628   if (at_class_scope_p ()
18629       && TYPE_BEING_DEFINED (current_class_type))
18630     return parser->num_template_parameter_lists != 0;
18631
18632   return ((int) parser->num_template_parameter_lists > template_class_depth
18633           (current_class_type));
18634 }
18635
18636 /* Parse a parameter-declaration-clause.
18637
18638    parameter-declaration-clause:
18639      parameter-declaration-list [opt] ... [opt]
18640      parameter-declaration-list , ...
18641
18642    Returns a representation for the parameter declarations.  A return
18643    value of NULL indicates a parameter-declaration-clause consisting
18644    only of an ellipsis.  */
18645
18646 static tree
18647 cp_parser_parameter_declaration_clause (cp_parser* parser)
18648 {
18649   tree parameters;
18650   cp_token *token;
18651   bool ellipsis_p;
18652   bool is_error;
18653
18654   struct cleanup {
18655     cp_parser* parser;
18656     int auto_is_implicit_function_template_parm_p;
18657     ~cleanup() {
18658       parser->auto_is_implicit_function_template_parm_p
18659         = auto_is_implicit_function_template_parm_p;
18660     }
18661   } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18662
18663   (void) cleanup;
18664
18665   if (!processing_specialization
18666       && !processing_template_parmlist
18667       && !processing_explicit_instantiation)
18668     if (!current_function_decl
18669         || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18670       parser->auto_is_implicit_function_template_parm_p = true;
18671
18672   /* Peek at the next token.  */
18673   token = cp_lexer_peek_token (parser->lexer);
18674   /* Check for trivial parameter-declaration-clauses.  */
18675   if (token->type == CPP_ELLIPSIS)
18676     {
18677       /* Consume the `...' token.  */
18678       cp_lexer_consume_token (parser->lexer);
18679       return NULL_TREE;
18680     }
18681   else if (token->type == CPP_CLOSE_PAREN)
18682     /* There are no parameters.  */
18683     {
18684 #ifndef NO_IMPLICIT_EXTERN_C
18685       if (in_system_header_at (input_location)
18686           && current_class_type == NULL
18687           && current_lang_name == lang_name_c)
18688         return NULL_TREE;
18689       else
18690 #endif
18691         return void_list_node;
18692     }
18693   /* Check for `(void)', too, which is a special case.  */
18694   else if (token->keyword == RID_VOID
18695            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18696                == CPP_CLOSE_PAREN))
18697     {
18698       /* Consume the `void' token.  */
18699       cp_lexer_consume_token (parser->lexer);
18700       /* There are no parameters.  */
18701       return void_list_node;
18702     }
18703
18704   /* Parse the parameter-declaration-list.  */
18705   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18706   /* If a parse error occurred while parsing the
18707      parameter-declaration-list, then the entire
18708      parameter-declaration-clause is erroneous.  */
18709   if (is_error)
18710     return NULL;
18711
18712   /* Peek at the next token.  */
18713   token = cp_lexer_peek_token (parser->lexer);
18714   /* If it's a `,', the clause should terminate with an ellipsis.  */
18715   if (token->type == CPP_COMMA)
18716     {
18717       /* Consume the `,'.  */
18718       cp_lexer_consume_token (parser->lexer);
18719       /* Expect an ellipsis.  */
18720       ellipsis_p
18721         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18722     }
18723   /* It might also be `...' if the optional trailing `,' was
18724      omitted.  */
18725   else if (token->type == CPP_ELLIPSIS)
18726     {
18727       /* Consume the `...' token.  */
18728       cp_lexer_consume_token (parser->lexer);
18729       /* And remember that we saw it.  */
18730       ellipsis_p = true;
18731     }
18732   else
18733     ellipsis_p = false;
18734
18735   /* Finish the parameter list.  */
18736   if (!ellipsis_p)
18737     parameters = chainon (parameters, void_list_node);
18738
18739   return parameters;
18740 }
18741
18742 /* Parse a parameter-declaration-list.
18743
18744    parameter-declaration-list:
18745      parameter-declaration
18746      parameter-declaration-list , parameter-declaration
18747
18748    Returns a representation of the parameter-declaration-list, as for
18749    cp_parser_parameter_declaration_clause.  However, the
18750    `void_list_node' is never appended to the list.  Upon return,
18751    *IS_ERROR will be true iff an error occurred.  */
18752
18753 static tree
18754 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18755 {
18756   tree parameters = NULL_TREE;
18757   tree *tail = &parameters;
18758   bool saved_in_unbraced_linkage_specification_p;
18759   int index = 0;
18760
18761   /* Assume all will go well.  */
18762   *is_error = false;
18763   /* The special considerations that apply to a function within an
18764      unbraced linkage specifications do not apply to the parameters
18765      to the function.  */
18766   saved_in_unbraced_linkage_specification_p
18767     = parser->in_unbraced_linkage_specification_p;
18768   parser->in_unbraced_linkage_specification_p = false;
18769
18770   /* Look for more parameters.  */
18771   while (true)
18772     {
18773       cp_parameter_declarator *parameter;
18774       tree decl = error_mark_node;
18775       bool parenthesized_p = false;
18776       int template_parm_idx = (function_being_declared_is_template_p (parser)?
18777                                TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18778                                                 (current_template_parms)) : 0);
18779
18780       /* Parse the parameter.  */
18781       parameter
18782         = cp_parser_parameter_declaration (parser,
18783                                            /*template_parm_p=*/false,
18784                                            &parenthesized_p);
18785
18786       /* We don't know yet if the enclosing context is deprecated, so wait
18787          and warn in grokparms if appropriate.  */
18788       deprecated_state = DEPRECATED_SUPPRESS;
18789
18790       if (parameter)
18791         {
18792           /* If a function parameter pack was specified and an implicit template
18793              parameter was introduced during cp_parser_parameter_declaration,
18794              change any implicit parameters introduced into packs.  */
18795           if (parser->implicit_template_parms
18796               && parameter->declarator
18797               && parameter->declarator->parameter_pack_p)
18798             {
18799               int latest_template_parm_idx = TREE_VEC_LENGTH
18800                 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18801
18802               if (latest_template_parm_idx != template_parm_idx)
18803                 parameter->decl_specifiers.type = convert_generic_types_to_packs
18804                   (parameter->decl_specifiers.type,
18805                    template_parm_idx, latest_template_parm_idx);
18806             }
18807
18808           decl = grokdeclarator (parameter->declarator,
18809                                  &parameter->decl_specifiers,
18810                                  PARM,
18811                                  parameter->default_argument != NULL_TREE,
18812                                  &parameter->decl_specifiers.attributes);
18813         }
18814
18815       deprecated_state = DEPRECATED_NORMAL;
18816
18817       /* If a parse error occurred parsing the parameter declaration,
18818          then the entire parameter-declaration-list is erroneous.  */
18819       if (decl == error_mark_node)
18820         {
18821           *is_error = true;
18822           parameters = error_mark_node;
18823           break;
18824         }
18825
18826       if (parameter->decl_specifiers.attributes)
18827         cplus_decl_attributes (&decl,
18828                                parameter->decl_specifiers.attributes,
18829                                0);
18830       if (DECL_NAME (decl))
18831         decl = pushdecl (decl);
18832
18833       if (decl != error_mark_node)
18834         {
18835           retrofit_lang_decl (decl);
18836           DECL_PARM_INDEX (decl) = ++index;
18837           DECL_PARM_LEVEL (decl) = function_parm_depth ();
18838         }
18839
18840       /* Add the new parameter to the list.  */
18841       *tail = build_tree_list (parameter->default_argument, decl);
18842       tail = &TREE_CHAIN (*tail);
18843
18844       /* Peek at the next token.  */
18845       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18846           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18847           /* These are for Objective-C++ */
18848           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18849           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18850         /* The parameter-declaration-list is complete.  */
18851         break;
18852       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18853         {
18854           cp_token *token;
18855
18856           /* Peek at the next token.  */
18857           token = cp_lexer_peek_nth_token (parser->lexer, 2);
18858           /* If it's an ellipsis, then the list is complete.  */
18859           if (token->type == CPP_ELLIPSIS)
18860             break;
18861           /* Otherwise, there must be more parameters.  Consume the
18862              `,'.  */
18863           cp_lexer_consume_token (parser->lexer);
18864           /* When parsing something like:
18865
18866                 int i(float f, double d)
18867
18868              we can tell after seeing the declaration for "f" that we
18869              are not looking at an initialization of a variable "i",
18870              but rather at the declaration of a function "i".
18871
18872              Due to the fact that the parsing of template arguments
18873              (as specified to a template-id) requires backtracking we
18874              cannot use this technique when inside a template argument
18875              list.  */
18876           if (!parser->in_template_argument_list_p
18877               && !parser->in_type_id_in_expr_p
18878               && cp_parser_uncommitted_to_tentative_parse_p (parser)
18879               /* However, a parameter-declaration of the form
18880                  "float(f)" (which is a valid declaration of a
18881                  parameter "f") can also be interpreted as an
18882                  expression (the conversion of "f" to "float").  */
18883               && !parenthesized_p)
18884             cp_parser_commit_to_tentative_parse (parser);
18885         }
18886       else
18887         {
18888           cp_parser_error (parser, "expected %<,%> or %<...%>");
18889           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18890             cp_parser_skip_to_closing_parenthesis (parser,
18891                                                    /*recovering=*/true,
18892                                                    /*or_comma=*/false,
18893                                                    /*consume_paren=*/false);
18894           break;
18895         }
18896     }
18897
18898   parser->in_unbraced_linkage_specification_p
18899     = saved_in_unbraced_linkage_specification_p;
18900
18901   /* Reset implicit_template_scope if we are about to leave the function
18902      parameter list that introduced it.  Note that for out-of-line member
18903      definitions, there will be one or more class scopes before we get to
18904      the template parameter scope.  */
18905
18906   if (cp_binding_level *its = parser->implicit_template_scope)
18907     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18908       {
18909         while (maybe_its->kind == sk_class)
18910           maybe_its = maybe_its->level_chain;
18911         if (maybe_its == its)
18912           {
18913             parser->implicit_template_parms = 0;
18914             parser->implicit_template_scope = 0;
18915           }
18916       }
18917
18918   return parameters;
18919 }
18920
18921 /* Parse a parameter declaration.
18922
18923    parameter-declaration:
18924      decl-specifier-seq ... [opt] declarator
18925      decl-specifier-seq declarator = assignment-expression
18926      decl-specifier-seq ... [opt] abstract-declarator [opt]
18927      decl-specifier-seq abstract-declarator [opt] = assignment-expression
18928
18929    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18930    declares a template parameter.  (In that case, a non-nested `>'
18931    token encountered during the parsing of the assignment-expression
18932    is not interpreted as a greater-than operator.)
18933
18934    Returns a representation of the parameter, or NULL if an error
18935    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18936    true iff the declarator is of the form "(p)".  */
18937
18938 static cp_parameter_declarator *
18939 cp_parser_parameter_declaration (cp_parser *parser,
18940                                  bool template_parm_p,
18941                                  bool *parenthesized_p)
18942 {
18943   int declares_class_or_enum;
18944   cp_decl_specifier_seq decl_specifiers;
18945   cp_declarator *declarator;
18946   tree default_argument;
18947   cp_token *token = NULL, *declarator_token_start = NULL;
18948   const char *saved_message;
18949
18950   /* In a template parameter, `>' is not an operator.
18951
18952      [temp.param]
18953
18954      When parsing a default template-argument for a non-type
18955      template-parameter, the first non-nested `>' is taken as the end
18956      of the template parameter-list rather than a greater-than
18957      operator.  */
18958
18959   /* Type definitions may not appear in parameter types.  */
18960   saved_message = parser->type_definition_forbidden_message;
18961   parser->type_definition_forbidden_message
18962     = G_("types may not be defined in parameter types");
18963
18964   /* Parse the declaration-specifiers.  */
18965   cp_parser_decl_specifier_seq (parser,
18966                                 CP_PARSER_FLAGS_NONE,
18967                                 &decl_specifiers,
18968                                 &declares_class_or_enum);
18969
18970   /* Complain about missing 'typename' or other invalid type names.  */
18971   if (!decl_specifiers.any_type_specifiers_p
18972       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18973     decl_specifiers.type = error_mark_node;
18974
18975   /* If an error occurred, there's no reason to attempt to parse the
18976      rest of the declaration.  */
18977   if (cp_parser_error_occurred (parser))
18978     {
18979       parser->type_definition_forbidden_message = saved_message;
18980       return NULL;
18981     }
18982
18983   /* Peek at the next token.  */
18984   token = cp_lexer_peek_token (parser->lexer);
18985
18986   /* If the next token is a `)', `,', `=', `>', or `...', then there
18987      is no declarator. However, when variadic templates are enabled,
18988      there may be a declarator following `...'.  */
18989   if (token->type == CPP_CLOSE_PAREN
18990       || token->type == CPP_COMMA
18991       || token->type == CPP_EQ
18992       || token->type == CPP_GREATER)
18993     {
18994       declarator = NULL;
18995       if (parenthesized_p)
18996         *parenthesized_p = false;
18997     }
18998   /* Otherwise, there should be a declarator.  */
18999   else
19000     {
19001       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19002       parser->default_arg_ok_p = false;
19003
19004       /* After seeing a decl-specifier-seq, if the next token is not a
19005          "(", there is no possibility that the code is a valid
19006          expression.  Therefore, if parsing tentatively, we commit at
19007          this point.  */
19008       if (!parser->in_template_argument_list_p
19009           /* In an expression context, having seen:
19010
19011                (int((char ...
19012
19013              we cannot be sure whether we are looking at a
19014              function-type (taking a "char" as a parameter) or a cast
19015              of some object of type "char" to "int".  */
19016           && !parser->in_type_id_in_expr_p
19017           && cp_parser_uncommitted_to_tentative_parse_p (parser)
19018           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19019           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19020         cp_parser_commit_to_tentative_parse (parser);
19021       /* Parse the declarator.  */
19022       declarator_token_start = token;
19023       declarator = cp_parser_declarator (parser,
19024                                          CP_PARSER_DECLARATOR_EITHER,
19025                                          /*ctor_dtor_or_conv_p=*/NULL,
19026                                          parenthesized_p,
19027                                          /*member_p=*/false,
19028                                          /*friend_p=*/false);
19029       parser->default_arg_ok_p = saved_default_arg_ok_p;
19030       /* After the declarator, allow more attributes.  */
19031       decl_specifiers.attributes
19032         = chainon (decl_specifiers.attributes,
19033                    cp_parser_attributes_opt (parser));
19034     }
19035
19036   /* If the next token is an ellipsis, and we have not seen a
19037      declarator name, and the type of the declarator contains parameter
19038      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19039      a parameter pack expansion expression. Otherwise, leave the
19040      ellipsis for a C-style variadic function. */
19041   token = cp_lexer_peek_token (parser->lexer);
19042   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19043     {
19044       tree type = decl_specifiers.type;
19045
19046       if (type && DECL_P (type))
19047         type = TREE_TYPE (type);
19048
19049       if (type
19050           && TREE_CODE (type) != TYPE_PACK_EXPANSION
19051           && declarator_can_be_parameter_pack (declarator)
19052           && (!declarator || !declarator->parameter_pack_p)
19053           && uses_parameter_packs (type))
19054         {
19055           /* Consume the `...'. */
19056           cp_lexer_consume_token (parser->lexer);
19057           maybe_warn_variadic_templates ();
19058           
19059           /* Build a pack expansion type */
19060           if (declarator)
19061             declarator->parameter_pack_p = true;
19062           else
19063             decl_specifiers.type = make_pack_expansion (type);
19064         }
19065     }
19066
19067   /* The restriction on defining new types applies only to the type
19068      of the parameter, not to the default argument.  */
19069   parser->type_definition_forbidden_message = saved_message;
19070
19071   /* If the next token is `=', then process a default argument.  */
19072   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19073     {
19074       token = cp_lexer_peek_token (parser->lexer);
19075       /* If we are defining a class, then the tokens that make up the
19076          default argument must be saved and processed later.  */
19077       if (!template_parm_p && at_class_scope_p ()
19078           && TYPE_BEING_DEFINED (current_class_type)
19079           && !LAMBDA_TYPE_P (current_class_type))
19080         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19081       /* Outside of a class definition, we can just parse the
19082          assignment-expression.  */
19083       else
19084         default_argument
19085           = cp_parser_default_argument (parser, template_parm_p);
19086
19087       if (!parser->default_arg_ok_p)
19088         {
19089           if (flag_permissive)
19090             warning (0, "deprecated use of default argument for parameter of non-function");
19091           else
19092             {
19093               error_at (token->location,
19094                         "default arguments are only "
19095                         "permitted for function parameters");
19096               default_argument = NULL_TREE;
19097             }
19098         }
19099       else if ((declarator && declarator->parameter_pack_p)
19100                || (decl_specifiers.type
19101                    && PACK_EXPANSION_P (decl_specifiers.type)))
19102         {
19103           /* Find the name of the parameter pack.  */     
19104           cp_declarator *id_declarator = declarator;
19105           while (id_declarator && id_declarator->kind != cdk_id)
19106             id_declarator = id_declarator->declarator;
19107           
19108           if (id_declarator && id_declarator->kind == cdk_id)
19109             error_at (declarator_token_start->location,
19110                       template_parm_p
19111                       ? G_("template parameter pack %qD "
19112                            "cannot have a default argument")
19113                       : G_("parameter pack %qD cannot have "
19114                            "a default argument"),
19115                       id_declarator->u.id.unqualified_name);
19116           else
19117             error_at (declarator_token_start->location,
19118                       template_parm_p
19119                       ? G_("template parameter pack cannot have "
19120                            "a default argument")
19121                       : G_("parameter pack cannot have a "
19122                            "default argument"));
19123
19124           default_argument = NULL_TREE;
19125         }
19126     }
19127   else
19128     default_argument = NULL_TREE;
19129
19130   return make_parameter_declarator (&decl_specifiers,
19131                                     declarator,
19132                                     default_argument);
19133 }
19134
19135 /* Parse a default argument and return it.
19136
19137    TEMPLATE_PARM_P is true if this is a default argument for a
19138    non-type template parameter.  */
19139 static tree
19140 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19141 {
19142   tree default_argument = NULL_TREE;
19143   bool saved_greater_than_is_operator_p;
19144   bool saved_local_variables_forbidden_p;
19145   bool non_constant_p, is_direct_init;
19146
19147   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19148      set correctly.  */
19149   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19150   parser->greater_than_is_operator_p = !template_parm_p;
19151   /* Local variable names (and the `this' keyword) may not
19152      appear in a default argument.  */
19153   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19154   parser->local_variables_forbidden_p = true;
19155   /* Parse the assignment-expression.  */
19156   if (template_parm_p)
19157     push_deferring_access_checks (dk_no_deferred);
19158   tree saved_class_ptr = NULL_TREE;
19159   tree saved_class_ref = NULL_TREE;
19160   /* The "this" pointer is not valid in a default argument.  */
19161   if (cfun)
19162     {
19163       saved_class_ptr = current_class_ptr;
19164       cp_function_chain->x_current_class_ptr = NULL_TREE;
19165       saved_class_ref = current_class_ref;
19166       cp_function_chain->x_current_class_ref = NULL_TREE;
19167     }
19168   default_argument
19169     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19170   /* Restore the "this" pointer.  */
19171   if (cfun)
19172     {
19173       cp_function_chain->x_current_class_ptr = saved_class_ptr;
19174       cp_function_chain->x_current_class_ref = saved_class_ref;
19175     }
19176   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19177     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19178   if (template_parm_p)
19179     pop_deferring_access_checks ();
19180   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19181   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19182
19183   return default_argument;
19184 }
19185
19186 /* Parse a function-body.
19187
19188    function-body:
19189      compound_statement  */
19190
19191 static void
19192 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19193 {
19194   cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19195 }
19196
19197 /* Parse a ctor-initializer-opt followed by a function-body.  Return
19198    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
19199    is true we are parsing a function-try-block.  */
19200
19201 static bool
19202 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19203                                                   bool in_function_try_block)
19204 {
19205   tree body, list;
19206   bool ctor_initializer_p;
19207   const bool check_body_p =
19208      DECL_CONSTRUCTOR_P (current_function_decl)
19209      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19210   tree last = NULL;
19211
19212   /* Begin the function body.  */
19213   body = begin_function_body ();
19214   /* Parse the optional ctor-initializer.  */
19215   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19216
19217   /* If we're parsing a constexpr constructor definition, we need
19218      to check that the constructor body is indeed empty.  However,
19219      before we get to cp_parser_function_body lot of junk has been
19220      generated, so we can't just check that we have an empty block.
19221      Rather we take a snapshot of the outermost block, and check whether
19222      cp_parser_function_body changed its state.  */
19223   if (check_body_p)
19224     {
19225       list = cur_stmt_list;
19226       if (STATEMENT_LIST_TAIL (list))
19227         last = STATEMENT_LIST_TAIL (list)->stmt;
19228     }
19229   /* Parse the function-body.  */
19230   cp_parser_function_body (parser, in_function_try_block);
19231   if (check_body_p)
19232     check_constexpr_ctor_body (last, list, /*complain=*/true);
19233   /* Finish the function body.  */
19234   finish_function_body (body);
19235
19236   return ctor_initializer_p;
19237 }
19238
19239 /* Parse an initializer.
19240
19241    initializer:
19242      = initializer-clause
19243      ( expression-list )
19244
19245    Returns an expression representing the initializer.  If no
19246    initializer is present, NULL_TREE is returned.
19247
19248    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19249    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
19250    set to TRUE if there is no initializer present.  If there is an
19251    initializer, and it is not a constant-expression, *NON_CONSTANT_P
19252    is set to true; otherwise it is set to false.  */
19253
19254 static tree
19255 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19256                        bool* non_constant_p)
19257 {
19258   cp_token *token;
19259   tree init;
19260
19261   /* Peek at the next token.  */
19262   token = cp_lexer_peek_token (parser->lexer);
19263
19264   /* Let our caller know whether or not this initializer was
19265      parenthesized.  */
19266   *is_direct_init = (token->type != CPP_EQ);
19267   /* Assume that the initializer is constant.  */
19268   *non_constant_p = false;
19269
19270   if (token->type == CPP_EQ)
19271     {
19272       /* Consume the `='.  */
19273       cp_lexer_consume_token (parser->lexer);
19274       /* Parse the initializer-clause.  */
19275       init = cp_parser_initializer_clause (parser, non_constant_p);
19276     }
19277   else if (token->type == CPP_OPEN_PAREN)
19278     {
19279       vec<tree, va_gc> *vec;
19280       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19281                                                      /*cast_p=*/false,
19282                                                      /*allow_expansion_p=*/true,
19283                                                      non_constant_p);
19284       if (vec == NULL)
19285         return error_mark_node;
19286       init = build_tree_list_vec (vec);
19287       release_tree_vector (vec);
19288     }
19289   else if (token->type == CPP_OPEN_BRACE)
19290     {
19291       cp_lexer_set_source_position (parser->lexer);
19292       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19293       init = cp_parser_braced_list (parser, non_constant_p);
19294       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19295     }
19296   else
19297     {
19298       /* Anything else is an error.  */
19299       cp_parser_error (parser, "expected initializer");
19300       init = error_mark_node;
19301     }
19302
19303   return init;
19304 }
19305
19306 /* Parse an initializer-clause.
19307
19308    initializer-clause:
19309      assignment-expression
19310      braced-init-list
19311
19312    Returns an expression representing the initializer.
19313
19314    If the `assignment-expression' production is used the value
19315    returned is simply a representation for the expression.
19316
19317    Otherwise, calls cp_parser_braced_list.  */
19318
19319 static tree
19320 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19321 {
19322   tree initializer;
19323
19324   /* Assume the expression is constant.  */
19325   *non_constant_p = false;
19326
19327   /* If it is not a `{', then we are looking at an
19328      assignment-expression.  */
19329   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19330     {
19331       initializer
19332         = cp_parser_constant_expression (parser,
19333                                         /*allow_non_constant_p=*/true,
19334                                         non_constant_p);
19335     }
19336   else
19337     initializer = cp_parser_braced_list (parser, non_constant_p);
19338
19339   return initializer;
19340 }
19341
19342 /* Parse a brace-enclosed initializer list.
19343
19344    braced-init-list:
19345      { initializer-list , [opt] }
19346      { }
19347
19348    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
19349    the elements of the initializer-list (or NULL, if the last
19350    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
19351    NULL_TREE.  There is no way to detect whether or not the optional
19352    trailing `,' was provided.  NON_CONSTANT_P is as for
19353    cp_parser_initializer.  */     
19354
19355 static tree
19356 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19357 {
19358   tree initializer;
19359
19360   /* Consume the `{' token.  */
19361   cp_lexer_consume_token (parser->lexer);
19362   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
19363   initializer = make_node (CONSTRUCTOR);
19364   /* If it's not a `}', then there is a non-trivial initializer.  */
19365   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19366     {
19367       /* Parse the initializer list.  */
19368       CONSTRUCTOR_ELTS (initializer)
19369         = cp_parser_initializer_list (parser, non_constant_p);
19370       /* A trailing `,' token is allowed.  */
19371       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19372         cp_lexer_consume_token (parser->lexer);
19373     }
19374   else
19375     *non_constant_p = false;
19376   /* Now, there should be a trailing `}'.  */
19377   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19378   TREE_TYPE (initializer) = init_list_type_node;
19379   return initializer;
19380 }
19381
19382 /* Consume tokens up to, and including, the next non-nested closing `]'.
19383    Returns true iff we found a closing `]'.  */
19384
19385 static bool
19386 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19387 {
19388   unsigned square_depth = 0;
19389
19390   while (true)
19391     {
19392       cp_token * token = cp_lexer_peek_token (parser->lexer);
19393
19394       switch (token->type)
19395         {
19396         case CPP_EOF:
19397         case CPP_PRAGMA_EOL:
19398           /* If we've run out of tokens, then there is no closing `]'.  */
19399           return false;
19400
19401         case CPP_OPEN_SQUARE:
19402           ++square_depth;
19403           break;
19404
19405         case CPP_CLOSE_SQUARE:
19406           if (!square_depth--)
19407             {
19408               cp_lexer_consume_token (parser->lexer);
19409               return true;
19410             }
19411           break;
19412
19413         default:
19414           break;
19415         }
19416
19417       /* Consume the token.  */
19418       cp_lexer_consume_token (parser->lexer);
19419     }
19420 }
19421
19422 /* Return true if we are looking at an array-designator, false otherwise.  */
19423
19424 static bool
19425 cp_parser_array_designator_p (cp_parser *parser)
19426 {
19427   /* Consume the `['.  */
19428   cp_lexer_consume_token (parser->lexer);
19429
19430   cp_lexer_save_tokens (parser->lexer);
19431
19432   /* Skip tokens until the next token is a closing square bracket.
19433      If we find the closing `]', and the next token is a `=', then
19434      we are looking at an array designator.  */
19435   bool array_designator_p
19436     = (cp_parser_skip_to_closing_square_bracket (parser)
19437        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19438   
19439   /* Roll back the tokens we skipped.  */
19440   cp_lexer_rollback_tokens (parser->lexer);
19441
19442   return array_designator_p;
19443 }
19444
19445 /* Parse an initializer-list.
19446
19447    initializer-list:
19448      initializer-clause ... [opt]
19449      initializer-list , initializer-clause ... [opt]
19450
19451    GNU Extension:
19452
19453    initializer-list:
19454      designation initializer-clause ...[opt]
19455      initializer-list , designation initializer-clause ...[opt]
19456
19457    designation:
19458      . identifier =
19459      identifier :
19460      [ constant-expression ] =
19461
19462    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
19463    for the initializer.  If the INDEX of the elt is non-NULL, it is the
19464    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
19465    as for cp_parser_initializer.  */
19466
19467 static vec<constructor_elt, va_gc> *
19468 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19469 {
19470   vec<constructor_elt, va_gc> *v = NULL;
19471
19472   /* Assume all of the expressions are constant.  */
19473   *non_constant_p = false;
19474
19475   /* Parse the rest of the list.  */
19476   while (true)
19477     {
19478       cp_token *token;
19479       tree designator;
19480       tree initializer;
19481       bool clause_non_constant_p;
19482
19483       /* If the next token is an identifier and the following one is a
19484          colon, we are looking at the GNU designated-initializer
19485          syntax.  */
19486       if (cp_parser_allow_gnu_extensions_p (parser)
19487           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19488           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19489         {
19490           /* Warn the user that they are using an extension.  */
19491           pedwarn (input_location, OPT_Wpedantic, 
19492                    "ISO C++ does not allow designated initializers");
19493           /* Consume the identifier.  */
19494           designator = cp_lexer_consume_token (parser->lexer)->u.value;
19495           /* Consume the `:'.  */
19496           cp_lexer_consume_token (parser->lexer);
19497         }
19498       /* Also handle the C99 syntax, '. id ='.  */
19499       else if (cp_parser_allow_gnu_extensions_p (parser)
19500                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19501                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19502                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19503         {
19504           /* Warn the user that they are using an extension.  */
19505           pedwarn (input_location, OPT_Wpedantic,
19506                    "ISO C++ does not allow C99 designated initializers");
19507           /* Consume the `.'.  */
19508           cp_lexer_consume_token (parser->lexer);
19509           /* Consume the identifier.  */
19510           designator = cp_lexer_consume_token (parser->lexer)->u.value;
19511           /* Consume the `='.  */
19512           cp_lexer_consume_token (parser->lexer);
19513         }
19514       /* Also handle C99 array designators, '[ const ] ='.  */
19515       else if (cp_parser_allow_gnu_extensions_p (parser)
19516                && !c_dialect_objc ()
19517                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19518         {
19519           /* In C++11, [ could start a lambda-introducer.  */
19520           bool non_const = false;
19521
19522           cp_parser_parse_tentatively (parser);
19523
19524           if (!cp_parser_array_designator_p (parser))
19525             {
19526               cp_parser_simulate_error (parser);
19527               designator = NULL_TREE;
19528             }
19529           else
19530             {
19531               designator = cp_parser_constant_expression (parser, true,
19532                                                           &non_const);
19533               cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19534               cp_parser_require (parser, CPP_EQ, RT_EQ);
19535             }
19536
19537           if (!cp_parser_parse_definitely (parser))
19538             designator = NULL_TREE;
19539           else if (non_const)
19540             require_potential_rvalue_constant_expression (designator);
19541         }
19542       else
19543         designator = NULL_TREE;
19544
19545       /* Parse the initializer.  */
19546       initializer = cp_parser_initializer_clause (parser,
19547                                                   &clause_non_constant_p);
19548       /* If any clause is non-constant, so is the entire initializer.  */
19549       if (clause_non_constant_p)
19550         *non_constant_p = true;
19551
19552       /* If we have an ellipsis, this is an initializer pack
19553          expansion.  */
19554       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19555         {
19556           /* Consume the `...'.  */
19557           cp_lexer_consume_token (parser->lexer);
19558
19559           /* Turn the initializer into an initializer expansion.  */
19560           initializer = make_pack_expansion (initializer);
19561         }
19562
19563       /* Add it to the vector.  */
19564       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19565
19566       /* If the next token is not a comma, we have reached the end of
19567          the list.  */
19568       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19569         break;
19570
19571       /* Peek at the next token.  */
19572       token = cp_lexer_peek_nth_token (parser->lexer, 2);
19573       /* If the next token is a `}', then we're still done.  An
19574          initializer-clause can have a trailing `,' after the
19575          initializer-list and before the closing `}'.  */
19576       if (token->type == CPP_CLOSE_BRACE)
19577         break;
19578
19579       /* Consume the `,' token.  */
19580       cp_lexer_consume_token (parser->lexer);
19581     }
19582
19583   return v;
19584 }
19585
19586 /* Classes [gram.class] */
19587
19588 /* Parse a class-name.
19589
19590    class-name:
19591      identifier
19592      template-id
19593
19594    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19595    to indicate that names looked up in dependent types should be
19596    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
19597    keyword has been used to indicate that the name that appears next
19598    is a template.  TAG_TYPE indicates the explicit tag given before
19599    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
19600    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
19601    is the class being defined in a class-head.
19602
19603    Returns the TYPE_DECL representing the class.  */
19604
19605 static tree
19606 cp_parser_class_name (cp_parser *parser,
19607                       bool typename_keyword_p,
19608                       bool template_keyword_p,
19609                       enum tag_types tag_type,
19610                       bool check_dependency_p,
19611                       bool class_head_p,
19612                       bool is_declaration)
19613 {
19614   tree decl;
19615   tree scope;
19616   bool typename_p;
19617   cp_token *token;
19618   tree identifier = NULL_TREE;
19619
19620   /* All class-names start with an identifier.  */
19621   token = cp_lexer_peek_token (parser->lexer);
19622   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19623     {
19624       cp_parser_error (parser, "expected class-name");
19625       return error_mark_node;
19626     }
19627
19628   /* PARSER->SCOPE can be cleared when parsing the template-arguments
19629      to a template-id, so we save it here.  */
19630   scope = parser->scope;
19631   if (scope == error_mark_node)
19632     return error_mark_node;
19633
19634   /* Any name names a type if we're following the `typename' keyword
19635      in a qualified name where the enclosing scope is type-dependent.  */
19636   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19637                 && dependent_type_p (scope));
19638   /* Handle the common case (an identifier, but not a template-id)
19639      efficiently.  */
19640   if (token->type == CPP_NAME
19641       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19642     {
19643       cp_token *identifier_token;
19644       bool ambiguous_p;
19645
19646       /* Look for the identifier.  */
19647       identifier_token = cp_lexer_peek_token (parser->lexer);
19648       ambiguous_p = identifier_token->error_reported;
19649       identifier = cp_parser_identifier (parser);
19650       /* If the next token isn't an identifier, we are certainly not
19651          looking at a class-name.  */
19652       if (identifier == error_mark_node)
19653         decl = error_mark_node;
19654       /* If we know this is a type-name, there's no need to look it
19655          up.  */
19656       else if (typename_p)
19657         decl = identifier;
19658       else
19659         {
19660           tree ambiguous_decls;
19661           /* If we already know that this lookup is ambiguous, then
19662              we've already issued an error message; there's no reason
19663              to check again.  */
19664           if (ambiguous_p)
19665             {
19666               cp_parser_simulate_error (parser);
19667               return error_mark_node;
19668             }
19669           /* If the next token is a `::', then the name must be a type
19670              name.
19671
19672              [basic.lookup.qual]
19673
19674              During the lookup for a name preceding the :: scope
19675              resolution operator, object, function, and enumerator
19676              names are ignored.  */
19677           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19678             tag_type = typename_type;
19679           /* Look up the name.  */
19680           decl = cp_parser_lookup_name (parser, identifier,
19681                                         tag_type,
19682                                         /*is_template=*/false,
19683                                         /*is_namespace=*/false,
19684                                         check_dependency_p,
19685                                         &ambiguous_decls,
19686                                         identifier_token->location);
19687           if (ambiguous_decls)
19688             {
19689               if (cp_parser_parsing_tentatively (parser))
19690                 cp_parser_simulate_error (parser);
19691               return error_mark_node;
19692             }
19693         }
19694     }
19695   else
19696     {
19697       /* Try a template-id.  */
19698       decl = cp_parser_template_id (parser, template_keyword_p,
19699                                     check_dependency_p,
19700                                     tag_type,
19701                                     is_declaration);
19702       if (decl == error_mark_node)
19703         return error_mark_node;
19704     }
19705
19706   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19707
19708   /* If this is a typename, create a TYPENAME_TYPE.  */
19709   if (typename_p && decl != error_mark_node)
19710     {
19711       decl = make_typename_type (scope, decl, typename_type,
19712                                  /*complain=*/tf_error);
19713       if (decl != error_mark_node)
19714         decl = TYPE_NAME (decl);
19715     }
19716
19717   decl = strip_using_decl (decl);
19718
19719   /* Check to see that it is really the name of a class.  */
19720   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19721       && identifier_p (TREE_OPERAND (decl, 0))
19722       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19723     /* Situations like this:
19724
19725          template <typename T> struct A {
19726            typename T::template X<int>::I i;
19727          };
19728
19729        are problematic.  Is `T::template X<int>' a class-name?  The
19730        standard does not seem to be definitive, but there is no other
19731        valid interpretation of the following `::'.  Therefore, those
19732        names are considered class-names.  */
19733     {
19734       decl = make_typename_type (scope, decl, tag_type, tf_error);
19735       if (decl != error_mark_node)
19736         decl = TYPE_NAME (decl);
19737     }
19738   else if (TREE_CODE (decl) != TYPE_DECL
19739            || TREE_TYPE (decl) == error_mark_node
19740            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19741            /* In Objective-C 2.0, a classname followed by '.' starts a
19742               dot-syntax expression, and it's not a type-name.  */
19743            || (c_dialect_objc ()
19744                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
19745                && objc_is_class_name (decl)))
19746     decl = error_mark_node;
19747
19748   if (decl == error_mark_node)
19749     cp_parser_error (parser, "expected class-name");
19750   else if (identifier && !parser->scope)
19751     maybe_note_name_used_in_class (identifier, decl);
19752
19753   return decl;
19754 }
19755
19756 /* Parse a class-specifier.
19757
19758    class-specifier:
19759      class-head { member-specification [opt] }
19760
19761    Returns the TREE_TYPE representing the class.  */
19762
19763 static tree
19764 cp_parser_class_specifier_1 (cp_parser* parser)
19765 {
19766   tree type;
19767   tree attributes = NULL_TREE;
19768   bool nested_name_specifier_p;
19769   unsigned saved_num_template_parameter_lists;
19770   bool saved_in_function_body;
19771   unsigned char in_statement;
19772   bool in_switch_statement_p;
19773   bool saved_in_unbraced_linkage_specification_p;
19774   tree old_scope = NULL_TREE;
19775   tree scope = NULL_TREE;
19776   cp_token *closing_brace;
19777
19778   push_deferring_access_checks (dk_no_deferred);
19779
19780   /* Parse the class-head.  */
19781   type = cp_parser_class_head (parser,
19782                                &nested_name_specifier_p);
19783   /* If the class-head was a semantic disaster, skip the entire body
19784      of the class.  */
19785   if (!type)
19786     {
19787       cp_parser_skip_to_end_of_block_or_statement (parser);
19788       pop_deferring_access_checks ();
19789       return error_mark_node;
19790     }
19791
19792   /* Look for the `{'.  */
19793   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19794     {
19795       pop_deferring_access_checks ();
19796       return error_mark_node;
19797     }
19798
19799   cp_ensure_no_omp_declare_simd (parser);
19800
19801   /* Issue an error message if type-definitions are forbidden here.  */
19802   cp_parser_check_type_definition (parser);
19803   /* Remember that we are defining one more class.  */
19804   ++parser->num_classes_being_defined;
19805   /* Inside the class, surrounding template-parameter-lists do not
19806      apply.  */
19807   saved_num_template_parameter_lists
19808     = parser->num_template_parameter_lists;
19809   parser->num_template_parameter_lists = 0;
19810   /* We are not in a function body.  */
19811   saved_in_function_body = parser->in_function_body;
19812   parser->in_function_body = false;
19813   /* Or in a loop.  */
19814   in_statement = parser->in_statement;
19815   parser->in_statement = 0;
19816   /* Or in a switch.  */
19817   in_switch_statement_p = parser->in_switch_statement_p;
19818   parser->in_switch_statement_p = false;
19819   /* We are not immediately inside an extern "lang" block.  */
19820   saved_in_unbraced_linkage_specification_p
19821     = parser->in_unbraced_linkage_specification_p;
19822   parser->in_unbraced_linkage_specification_p = false;
19823
19824   /* Start the class.  */
19825   if (nested_name_specifier_p)
19826     {
19827       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19828       old_scope = push_inner_scope (scope);
19829     }
19830   type = begin_class_definition (type);
19831
19832   if (type == error_mark_node)
19833     /* If the type is erroneous, skip the entire body of the class.  */
19834     cp_parser_skip_to_closing_brace (parser);
19835   else
19836     /* Parse the member-specification.  */
19837     cp_parser_member_specification_opt (parser);
19838
19839   /* Look for the trailing `}'.  */
19840   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19841   /* Look for trailing attributes to apply to this class.  */
19842   if (cp_parser_allow_gnu_extensions_p (parser))
19843     attributes = cp_parser_gnu_attributes_opt (parser);
19844   if (type != error_mark_node)
19845     type = finish_struct (type, attributes);
19846   if (nested_name_specifier_p)
19847     pop_inner_scope (old_scope, scope);
19848
19849   /* We've finished a type definition.  Check for the common syntax
19850      error of forgetting a semicolon after the definition.  We need to
19851      be careful, as we can't just check for not-a-semicolon and be done
19852      with it; the user might have typed:
19853
19854      class X { } c = ...;
19855      class X { } *p = ...;
19856
19857      and so forth.  Instead, enumerate all the possible tokens that
19858      might follow this production; if we don't see one of them, then
19859      complain and silently insert the semicolon.  */
19860   {
19861     cp_token *token = cp_lexer_peek_token (parser->lexer);
19862     bool want_semicolon = true;
19863
19864     if (cp_next_tokens_can_be_std_attribute_p (parser))
19865       /* Don't try to parse c++11 attributes here.  As per the
19866          grammar, that should be a task for
19867          cp_parser_decl_specifier_seq.  */
19868       want_semicolon = false;
19869
19870     switch (token->type)
19871       {
19872       case CPP_NAME:
19873       case CPP_SEMICOLON:
19874       case CPP_MULT:
19875       case CPP_AND:
19876       case CPP_OPEN_PAREN:
19877       case CPP_CLOSE_PAREN:
19878       case CPP_COMMA:
19879         want_semicolon = false;
19880         break;
19881
19882         /* While it's legal for type qualifiers and storage class
19883            specifiers to follow type definitions in the grammar, only
19884            compiler testsuites contain code like that.  Assume that if
19885            we see such code, then what we're really seeing is a case
19886            like:
19887
19888            class X { }
19889            const <type> var = ...;
19890
19891            or
19892
19893            class Y { }
19894            static <type> func (...) ...
19895
19896            i.e. the qualifier or specifier applies to the next
19897            declaration.  To do so, however, we need to look ahead one
19898            more token to see if *that* token is a type specifier.
19899
19900            This code could be improved to handle:
19901
19902            class Z { }
19903            static const <type> var = ...;  */
19904       case CPP_KEYWORD:
19905         if (keyword_is_decl_specifier (token->keyword))
19906           {
19907             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19908
19909             /* Handling user-defined types here would be nice, but very
19910                tricky.  */
19911             want_semicolon
19912               = (lookahead->type == CPP_KEYWORD
19913                  && keyword_begins_type_specifier (lookahead->keyword));
19914           }
19915         break;
19916       default:
19917         break;
19918       }
19919
19920     /* If we don't have a type, then something is very wrong and we
19921        shouldn't try to do anything clever.  Likewise for not seeing the
19922        closing brace.  */
19923     if (closing_brace && TYPE_P (type) && want_semicolon)
19924       {
19925         cp_token_position prev
19926           = cp_lexer_previous_token_position (parser->lexer);
19927         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19928         location_t loc = prev_token->location;
19929
19930         if (CLASSTYPE_DECLARED_CLASS (type))
19931           error_at (loc, "expected %<;%> after class definition");
19932         else if (TREE_CODE (type) == RECORD_TYPE)
19933           error_at (loc, "expected %<;%> after struct definition");
19934         else if (TREE_CODE (type) == UNION_TYPE)
19935           error_at (loc, "expected %<;%> after union definition");
19936         else
19937           gcc_unreachable ();
19938
19939         /* Unget one token and smash it to look as though we encountered
19940            a semicolon in the input stream.  */
19941         cp_lexer_set_token_position (parser->lexer, prev);
19942         token = cp_lexer_peek_token (parser->lexer);
19943         token->type = CPP_SEMICOLON;
19944         token->keyword = RID_MAX;
19945       }
19946   }
19947
19948   /* If this class is not itself within the scope of another class,
19949      then we need to parse the bodies of all of the queued function
19950      definitions.  Note that the queued functions defined in a class
19951      are not always processed immediately following the
19952      class-specifier for that class.  Consider:
19953
19954        struct A {
19955          struct B { void f() { sizeof (A); } };
19956        };
19957
19958      If `f' were processed before the processing of `A' were
19959      completed, there would be no way to compute the size of `A'.
19960      Note that the nesting we are interested in here is lexical --
19961      not the semantic nesting given by TYPE_CONTEXT.  In particular,
19962      for:
19963
19964        struct A { struct B; };
19965        struct A::B { void f() { } };
19966
19967      there is no need to delay the parsing of `A::B::f'.  */
19968   if (--parser->num_classes_being_defined == 0)
19969     {
19970       tree decl;
19971       tree class_type = NULL_TREE;
19972       tree pushed_scope = NULL_TREE;
19973       unsigned ix;
19974       cp_default_arg_entry *e;
19975       tree save_ccp, save_ccr;
19976
19977       /* In a first pass, parse default arguments to the functions.
19978          Then, in a second pass, parse the bodies of the functions.
19979          This two-phased approach handles cases like:
19980
19981             struct S {
19982               void f() { g(); }
19983               void g(int i = 3);
19984             };
19985
19986          */
19987       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19988         {
19989           decl = e->decl;
19990           /* If there are default arguments that have not yet been processed,
19991              take care of them now.  */
19992           if (class_type != e->class_type)
19993             {
19994               if (pushed_scope)
19995                 pop_scope (pushed_scope);
19996               class_type = e->class_type;
19997               pushed_scope = push_scope (class_type);
19998             }
19999           /* Make sure that any template parameters are in scope.  */
20000           maybe_begin_member_template_processing (decl);
20001           /* Parse the default argument expressions.  */
20002           cp_parser_late_parsing_default_args (parser, decl);
20003           /* Remove any template parameters from the symbol table.  */
20004           maybe_end_member_template_processing ();
20005         }
20006       vec_safe_truncate (unparsed_funs_with_default_args, 0);
20007       /* Now parse any NSDMIs.  */
20008       save_ccp = current_class_ptr;
20009       save_ccr = current_class_ref;
20010       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20011         {
20012           if (class_type != DECL_CONTEXT (decl))
20013             {
20014               if (pushed_scope)
20015                 pop_scope (pushed_scope);
20016               class_type = DECL_CONTEXT (decl);
20017               pushed_scope = push_scope (class_type);
20018             }
20019           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20020           cp_parser_late_parsing_nsdmi (parser, decl);
20021         }
20022       vec_safe_truncate (unparsed_nsdmis, 0);
20023       current_class_ptr = save_ccp;
20024       current_class_ref = save_ccr;
20025       if (pushed_scope)
20026         pop_scope (pushed_scope);
20027
20028       /* Now do some post-NSDMI bookkeeping.  */
20029       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20030         after_nsdmi_defaulted_late_checks (class_type);
20031       vec_safe_truncate (unparsed_classes, 0);
20032       after_nsdmi_defaulted_late_checks (type);
20033
20034       /* Now parse the body of the functions.  */
20035       if (flag_openmp)
20036         {
20037           /* OpenMP UDRs need to be parsed before all other functions.  */
20038           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20039             if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20040               cp_parser_late_parsing_for_member (parser, decl);
20041           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20042             if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20043               cp_parser_late_parsing_for_member (parser, decl);
20044         }
20045       else
20046         FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20047           cp_parser_late_parsing_for_member (parser, decl);
20048       vec_safe_truncate (unparsed_funs_with_definitions, 0);
20049     }
20050   else
20051     vec_safe_push (unparsed_classes, type);
20052
20053   /* Put back any saved access checks.  */
20054   pop_deferring_access_checks ();
20055
20056   /* Restore saved state.  */
20057   parser->in_switch_statement_p = in_switch_statement_p;
20058   parser->in_statement = in_statement;
20059   parser->in_function_body = saved_in_function_body;
20060   parser->num_template_parameter_lists
20061     = saved_num_template_parameter_lists;
20062   parser->in_unbraced_linkage_specification_p
20063     = saved_in_unbraced_linkage_specification_p;
20064
20065   return type;
20066 }
20067
20068 static tree
20069 cp_parser_class_specifier (cp_parser* parser)
20070 {
20071   tree ret;
20072   timevar_push (TV_PARSE_STRUCT);
20073   ret = cp_parser_class_specifier_1 (parser);
20074   timevar_pop (TV_PARSE_STRUCT);
20075   return ret;
20076 }
20077
20078 /* Parse a class-head.
20079
20080    class-head:
20081      class-key identifier [opt] base-clause [opt]
20082      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20083      class-key nested-name-specifier [opt] template-id
20084        base-clause [opt]
20085
20086    class-virt-specifier:
20087      final
20088
20089    GNU Extensions:
20090      class-key attributes identifier [opt] base-clause [opt]
20091      class-key attributes nested-name-specifier identifier base-clause [opt]
20092      class-key attributes nested-name-specifier [opt] template-id
20093        base-clause [opt]
20094
20095    Upon return BASES is initialized to the list of base classes (or
20096    NULL, if there are none) in the same form returned by
20097    cp_parser_base_clause.
20098
20099    Returns the TYPE of the indicated class.  Sets
20100    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20101    involving a nested-name-specifier was used, and FALSE otherwise.
20102
20103    Returns error_mark_node if this is not a class-head.
20104
20105    Returns NULL_TREE if the class-head is syntactically valid, but
20106    semantically invalid in a way that means we should skip the entire
20107    body of the class.  */
20108
20109 static tree
20110 cp_parser_class_head (cp_parser* parser,
20111                       bool* nested_name_specifier_p)
20112 {
20113   tree nested_name_specifier;
20114   enum tag_types class_key;
20115   tree id = NULL_TREE;
20116   tree type = NULL_TREE;
20117   tree attributes;
20118   tree bases;
20119   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20120   bool template_id_p = false;
20121   bool qualified_p = false;
20122   bool invalid_nested_name_p = false;
20123   bool invalid_explicit_specialization_p = false;
20124   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20125   tree pushed_scope = NULL_TREE;
20126   unsigned num_templates;
20127   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20128   /* Assume no nested-name-specifier will be present.  */
20129   *nested_name_specifier_p = false;
20130   /* Assume no template parameter lists will be used in defining the
20131      type.  */
20132   num_templates = 0;
20133   parser->colon_corrects_to_scope_p = false;
20134
20135   /* Look for the class-key.  */
20136   class_key = cp_parser_class_key (parser);
20137   if (class_key == none_type)
20138     return error_mark_node;
20139
20140   /* Parse the attributes.  */
20141   attributes = cp_parser_attributes_opt (parser);
20142
20143   /* If the next token is `::', that is invalid -- but sometimes
20144      people do try to write:
20145
20146        struct ::S {};
20147
20148      Handle this gracefully by accepting the extra qualifier, and then
20149      issuing an error about it later if this really is a
20150      class-head.  If it turns out just to be an elaborated type
20151      specifier, remain silent.  */
20152   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20153     qualified_p = true;
20154
20155   push_deferring_access_checks (dk_no_check);
20156
20157   /* Determine the name of the class.  Begin by looking for an
20158      optional nested-name-specifier.  */
20159   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20160   nested_name_specifier
20161     = cp_parser_nested_name_specifier_opt (parser,
20162                                            /*typename_keyword_p=*/false,
20163                                            /*check_dependency_p=*/false,
20164                                            /*type_p=*/true,
20165                                            /*is_declaration=*/false);
20166   /* If there was a nested-name-specifier, then there *must* be an
20167      identifier.  */
20168   if (nested_name_specifier)
20169     {
20170       type_start_token = cp_lexer_peek_token (parser->lexer);
20171       /* Although the grammar says `identifier', it really means
20172          `class-name' or `template-name'.  You are only allowed to
20173          define a class that has already been declared with this
20174          syntax.
20175
20176          The proposed resolution for Core Issue 180 says that wherever
20177          you see `class T::X' you should treat `X' as a type-name.
20178
20179          It is OK to define an inaccessible class; for example:
20180
20181            class A { class B; };
20182            class A::B {};
20183
20184          We do not know if we will see a class-name, or a
20185          template-name.  We look for a class-name first, in case the
20186          class-name is a template-id; if we looked for the
20187          template-name first we would stop after the template-name.  */
20188       cp_parser_parse_tentatively (parser);
20189       type = cp_parser_class_name (parser,
20190                                    /*typename_keyword_p=*/false,
20191                                    /*template_keyword_p=*/false,
20192                                    class_type,
20193                                    /*check_dependency_p=*/false,
20194                                    /*class_head_p=*/true,
20195                                    /*is_declaration=*/false);
20196       /* If that didn't work, ignore the nested-name-specifier.  */
20197       if (!cp_parser_parse_definitely (parser))
20198         {
20199           invalid_nested_name_p = true;
20200           type_start_token = cp_lexer_peek_token (parser->lexer);
20201           id = cp_parser_identifier (parser);
20202           if (id == error_mark_node)
20203             id = NULL_TREE;
20204         }
20205       /* If we could not find a corresponding TYPE, treat this
20206          declaration like an unqualified declaration.  */
20207       if (type == error_mark_node)
20208         nested_name_specifier = NULL_TREE;
20209       /* Otherwise, count the number of templates used in TYPE and its
20210          containing scopes.  */
20211       else
20212         {
20213           tree scope;
20214
20215           for (scope = TREE_TYPE (type);
20216                scope && TREE_CODE (scope) != NAMESPACE_DECL;
20217                scope = get_containing_scope (scope))
20218             if (TYPE_P (scope)
20219                 && CLASS_TYPE_P (scope)
20220                 && CLASSTYPE_TEMPLATE_INFO (scope)
20221                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20222                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20223                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20224               ++num_templates;
20225         }
20226     }
20227   /* Otherwise, the identifier is optional.  */
20228   else
20229     {
20230       /* We don't know whether what comes next is a template-id,
20231          an identifier, or nothing at all.  */
20232       cp_parser_parse_tentatively (parser);
20233       /* Check for a template-id.  */
20234       type_start_token = cp_lexer_peek_token (parser->lexer);
20235       id = cp_parser_template_id (parser,
20236                                   /*template_keyword_p=*/false,
20237                                   /*check_dependency_p=*/true,
20238                                   class_key,
20239                                   /*is_declaration=*/true);
20240       /* If that didn't work, it could still be an identifier.  */
20241       if (!cp_parser_parse_definitely (parser))
20242         {
20243           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20244             {
20245               type_start_token = cp_lexer_peek_token (parser->lexer);
20246               id = cp_parser_identifier (parser);
20247             }
20248           else
20249             id = NULL_TREE;
20250         }
20251       else
20252         {
20253           template_id_p = true;
20254           ++num_templates;
20255         }
20256     }
20257
20258   pop_deferring_access_checks ();
20259
20260   if (id)
20261     {
20262       cp_parser_check_for_invalid_template_id (parser, id,
20263                                                class_key,
20264                                                type_start_token->location);
20265     }
20266   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20267
20268   /* If it's not a `:' or a `{' then we can't really be looking at a
20269      class-head, since a class-head only appears as part of a
20270      class-specifier.  We have to detect this situation before calling
20271      xref_tag, since that has irreversible side-effects.  */
20272   if (!cp_parser_next_token_starts_class_definition_p (parser))
20273     {
20274       cp_parser_error (parser, "expected %<{%> or %<:%>");
20275       type = error_mark_node;
20276       goto out;
20277     }
20278
20279   /* At this point, we're going ahead with the class-specifier, even
20280      if some other problem occurs.  */
20281   cp_parser_commit_to_tentative_parse (parser);
20282   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20283     {
20284       cp_parser_error (parser,
20285                        "cannot specify %<override%> for a class");
20286       type = error_mark_node;
20287       goto out;
20288     }
20289   /* Issue the error about the overly-qualified name now.  */
20290   if (qualified_p)
20291     {
20292       cp_parser_error (parser,
20293                        "global qualification of class name is invalid");
20294       type = error_mark_node;
20295       goto out;
20296     }
20297   else if (invalid_nested_name_p)
20298     {
20299       cp_parser_error (parser,
20300                        "qualified name does not name a class");
20301       type = error_mark_node;
20302       goto out;
20303     }
20304   else if (nested_name_specifier)
20305     {
20306       tree scope;
20307
20308       /* Reject typedef-names in class heads.  */
20309       if (!DECL_IMPLICIT_TYPEDEF_P (type))
20310         {
20311           error_at (type_start_token->location,
20312                     "invalid class name in declaration of %qD",
20313                     type);
20314           type = NULL_TREE;
20315           goto done;
20316         }
20317
20318       /* Figure out in what scope the declaration is being placed.  */
20319       scope = current_scope ();
20320       /* If that scope does not contain the scope in which the
20321          class was originally declared, the program is invalid.  */
20322       if (scope && !is_ancestor (scope, nested_name_specifier))
20323         {
20324           if (at_namespace_scope_p ())
20325             error_at (type_start_token->location,
20326                       "declaration of %qD in namespace %qD which does not "
20327                       "enclose %qD",
20328                       type, scope, nested_name_specifier);
20329           else
20330             error_at (type_start_token->location,
20331                       "declaration of %qD in %qD which does not enclose %qD",
20332                       type, scope, nested_name_specifier);
20333           type = NULL_TREE;
20334           goto done;
20335         }
20336       /* [dcl.meaning]
20337
20338          A declarator-id shall not be qualified except for the
20339          definition of a ... nested class outside of its class
20340          ... [or] the definition or explicit instantiation of a
20341          class member of a namespace outside of its namespace.  */
20342       if (scope == nested_name_specifier)
20343         {
20344           permerror (nested_name_specifier_token_start->location,
20345                      "extra qualification not allowed");
20346           nested_name_specifier = NULL_TREE;
20347           num_templates = 0;
20348         }
20349     }
20350   /* An explicit-specialization must be preceded by "template <>".  If
20351      it is not, try to recover gracefully.  */
20352   if (at_namespace_scope_p ()
20353       && parser->num_template_parameter_lists == 0
20354       && template_id_p)
20355     {
20356       error_at (type_start_token->location,
20357                 "an explicit specialization must be preceded by %<template <>%>");
20358       invalid_explicit_specialization_p = true;
20359       /* Take the same action that would have been taken by
20360          cp_parser_explicit_specialization.  */
20361       ++parser->num_template_parameter_lists;
20362       begin_specialization ();
20363     }
20364   /* There must be no "return" statements between this point and the
20365      end of this function; set "type "to the correct return value and
20366      use "goto done;" to return.  */
20367   /* Make sure that the right number of template parameters were
20368      present.  */
20369   if (!cp_parser_check_template_parameters (parser, num_templates,
20370                                             type_start_token->location,
20371                                             /*declarator=*/NULL))
20372     {
20373       /* If something went wrong, there is no point in even trying to
20374          process the class-definition.  */
20375       type = NULL_TREE;
20376       goto done;
20377     }
20378
20379   /* Look up the type.  */
20380   if (template_id_p)
20381     {
20382       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20383           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20384               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20385         {
20386           error_at (type_start_token->location,
20387                     "function template %qD redeclared as a class template", id);
20388           type = error_mark_node;
20389         }
20390       else
20391         {
20392           type = TREE_TYPE (id);
20393           type = maybe_process_partial_specialization (type);
20394         }
20395       if (nested_name_specifier)
20396         pushed_scope = push_scope (nested_name_specifier);
20397     }
20398   else if (nested_name_specifier)
20399     {
20400       tree class_type;
20401
20402       /* Given:
20403
20404             template <typename T> struct S { struct T };
20405             template <typename T> struct S<T>::T { };
20406
20407          we will get a TYPENAME_TYPE when processing the definition of
20408          `S::T'.  We need to resolve it to the actual type before we
20409          try to define it.  */
20410       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20411         {
20412           class_type = resolve_typename_type (TREE_TYPE (type),
20413                                               /*only_current_p=*/false);
20414           if (TREE_CODE (class_type) != TYPENAME_TYPE)
20415             type = TYPE_NAME (class_type);
20416           else
20417             {
20418               cp_parser_error (parser, "could not resolve typename type");
20419               type = error_mark_node;
20420             }
20421         }
20422
20423       if (maybe_process_partial_specialization (TREE_TYPE (type))
20424           == error_mark_node)
20425         {
20426           type = NULL_TREE;
20427           goto done;
20428         }
20429
20430       class_type = current_class_type;
20431       /* Enter the scope indicated by the nested-name-specifier.  */
20432       pushed_scope = push_scope (nested_name_specifier);
20433       /* Get the canonical version of this type.  */
20434       type = TYPE_MAIN_DECL (TREE_TYPE (type));
20435       /* Call push_template_decl if it seems like we should be defining a
20436          template either from the template headers or the type we're
20437          defining, so that we diagnose both extra and missing headers.  */
20438       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20439            || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20440           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20441         {
20442           type = push_template_decl (type);
20443           if (type == error_mark_node)
20444             {
20445               type = NULL_TREE;
20446               goto done;
20447             }
20448         }
20449
20450       type = TREE_TYPE (type);
20451       *nested_name_specifier_p = true;
20452     }
20453   else      /* The name is not a nested name.  */
20454     {
20455       /* If the class was unnamed, create a dummy name.  */
20456       if (!id)
20457         id = make_anon_name ();
20458       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20459                        parser->num_template_parameter_lists);
20460     }
20461
20462   /* Indicate whether this class was declared as a `class' or as a
20463      `struct'.  */
20464   if (TREE_CODE (type) == RECORD_TYPE)
20465     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20466   cp_parser_check_class_key (class_key, type);
20467
20468   /* If this type was already complete, and we see another definition,
20469      that's an error.  */
20470   if (type != error_mark_node && COMPLETE_TYPE_P (type))
20471     {
20472       error_at (type_start_token->location, "redefinition of %q#T",
20473                 type);
20474       error_at (type_start_token->location, "previous definition of %q+#T",
20475                 type);
20476       type = NULL_TREE;
20477       goto done;
20478     }
20479   else if (type == error_mark_node)
20480     type = NULL_TREE;
20481
20482   if (type)
20483     {
20484       /* Apply attributes now, before any use of the class as a template
20485          argument in its base list.  */
20486       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20487       fixup_attribute_variants (type);
20488     }
20489
20490   /* We will have entered the scope containing the class; the names of
20491      base classes should be looked up in that context.  For example:
20492
20493        struct A { struct B {}; struct C; };
20494        struct A::C : B {};
20495
20496      is valid.  */
20497
20498   /* Get the list of base-classes, if there is one.  */
20499   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20500     {
20501       /* PR59482: enter the class scope so that base-specifiers are looked
20502          up correctly.  */
20503       if (type)
20504         pushclass (type);
20505       bases = cp_parser_base_clause (parser);
20506       /* PR59482: get out of the previously pushed class scope so that the
20507          subsequent pops pop the right thing.  */
20508       if (type)
20509         popclass ();
20510     }
20511   else
20512     bases = NULL_TREE;
20513
20514   /* If we're really defining a class, process the base classes.
20515      If they're invalid, fail.  */
20516   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20517       && !xref_basetypes (type, bases))
20518     type = NULL_TREE;
20519
20520  done:
20521   /* Leave the scope given by the nested-name-specifier.  We will
20522      enter the class scope itself while processing the members.  */
20523   if (pushed_scope)
20524     pop_scope (pushed_scope);
20525
20526   if (invalid_explicit_specialization_p)
20527     {
20528       end_specialization ();
20529       --parser->num_template_parameter_lists;
20530     }
20531
20532   if (type)
20533     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20534   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20535     CLASSTYPE_FINAL (type) = 1;
20536  out:
20537   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20538   return type;
20539 }
20540
20541 /* Parse a class-key.
20542
20543    class-key:
20544      class
20545      struct
20546      union
20547
20548    Returns the kind of class-key specified, or none_type to indicate
20549    error.  */
20550
20551 static enum tag_types
20552 cp_parser_class_key (cp_parser* parser)
20553 {
20554   cp_token *token;
20555   enum tag_types tag_type;
20556
20557   /* Look for the class-key.  */
20558   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20559   if (!token)
20560     return none_type;
20561
20562   /* Check to see if the TOKEN is a class-key.  */
20563   tag_type = cp_parser_token_is_class_key (token);
20564   if (!tag_type)
20565     cp_parser_error (parser, "expected class-key");
20566   return tag_type;
20567 }
20568
20569 /* Parse a type-parameter-key.
20570
20571    type-parameter-key:
20572      class
20573      typename
20574  */
20575
20576 static void
20577 cp_parser_type_parameter_key (cp_parser* parser)
20578 {
20579   /* Look for the type-parameter-key.  */
20580   enum tag_types tag_type = none_type;
20581   cp_token *token = cp_lexer_peek_token (parser->lexer);
20582   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20583     {
20584       cp_lexer_consume_token (parser->lexer);
20585       if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20586         /* typename is not allowed in a template template parameter
20587            by the standard until C++1Z.  */
20588         pedwarn (token->location, OPT_Wpedantic, 
20589                  "ISO C++ forbids typename key in template template parameter;"
20590                  " use -std=c++1z or -std=gnu++1z");
20591     }
20592   else
20593     cp_parser_error (parser, "expected %<class%> or %<typename%>");
20594
20595   return;
20596 }
20597
20598 /* Parse an (optional) member-specification.
20599
20600    member-specification:
20601      member-declaration member-specification [opt]
20602      access-specifier : member-specification [opt]  */
20603
20604 static void
20605 cp_parser_member_specification_opt (cp_parser* parser)
20606 {
20607   while (true)
20608     {
20609       cp_token *token;
20610       enum rid keyword;
20611
20612       /* Peek at the next token.  */
20613       token = cp_lexer_peek_token (parser->lexer);
20614       /* If it's a `}', or EOF then we've seen all the members.  */
20615       if (token->type == CPP_CLOSE_BRACE
20616           || token->type == CPP_EOF
20617           || token->type == CPP_PRAGMA_EOL)
20618         break;
20619
20620       /* See if this token is a keyword.  */
20621       keyword = token->keyword;
20622       switch (keyword)
20623         {
20624         case RID_PUBLIC:
20625         case RID_PROTECTED:
20626         case RID_PRIVATE:
20627           /* Consume the access-specifier.  */
20628           cp_lexer_consume_token (parser->lexer);
20629           /* Remember which access-specifier is active.  */
20630           current_access_specifier = token->u.value;
20631           /* Look for the `:'.  */
20632           cp_parser_require (parser, CPP_COLON, RT_COLON);
20633           break;
20634
20635         default:
20636           /* Accept #pragmas at class scope.  */
20637           if (token->type == CPP_PRAGMA)
20638             {
20639               cp_parser_pragma (parser, pragma_member);
20640               break;
20641             }
20642
20643           /* Otherwise, the next construction must be a
20644              member-declaration.  */
20645           cp_parser_member_declaration (parser);
20646         }
20647     }
20648 }
20649
20650 /* Parse a member-declaration.
20651
20652    member-declaration:
20653      decl-specifier-seq [opt] member-declarator-list [opt] ;
20654      function-definition ; [opt]
20655      :: [opt] nested-name-specifier template [opt] unqualified-id ;
20656      using-declaration
20657      template-declaration
20658      alias-declaration
20659
20660    member-declarator-list:
20661      member-declarator
20662      member-declarator-list , member-declarator
20663
20664    member-declarator:
20665      declarator pure-specifier [opt]
20666      declarator constant-initializer [opt]
20667      identifier [opt] : constant-expression
20668
20669    GNU Extensions:
20670
20671    member-declaration:
20672      __extension__ member-declaration
20673
20674    member-declarator:
20675      declarator attributes [opt] pure-specifier [opt]
20676      declarator attributes [opt] constant-initializer [opt]
20677      identifier [opt] attributes [opt] : constant-expression  
20678
20679    C++0x Extensions:
20680
20681    member-declaration:
20682      static_assert-declaration  */
20683
20684 static void
20685 cp_parser_member_declaration (cp_parser* parser)
20686 {
20687   cp_decl_specifier_seq decl_specifiers;
20688   tree prefix_attributes;
20689   tree decl;
20690   int declares_class_or_enum;
20691   bool friend_p;
20692   cp_token *token = NULL;
20693   cp_token *decl_spec_token_start = NULL;
20694   cp_token *initializer_token_start = NULL;
20695   int saved_pedantic;
20696   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20697
20698   /* Check for the `__extension__' keyword.  */
20699   if (cp_parser_extension_opt (parser, &saved_pedantic))
20700     {
20701       /* Recurse.  */
20702       cp_parser_member_declaration (parser);
20703       /* Restore the old value of the PEDANTIC flag.  */
20704       pedantic = saved_pedantic;
20705
20706       return;
20707     }
20708
20709   /* Check for a template-declaration.  */
20710   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20711     {
20712       /* An explicit specialization here is an error condition, and we
20713          expect the specialization handler to detect and report this.  */
20714       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20715           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20716         cp_parser_explicit_specialization (parser);
20717       else
20718         cp_parser_template_declaration (parser, /*member_p=*/true);
20719
20720       return;
20721     }
20722
20723   /* Check for a using-declaration.  */
20724   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20725     {
20726       if (cxx_dialect < cxx11)
20727         {
20728           /* Parse the using-declaration.  */
20729           cp_parser_using_declaration (parser,
20730                                        /*access_declaration_p=*/false);
20731           return;
20732         }
20733       else
20734         {
20735           tree decl;
20736           bool alias_decl_expected;
20737           cp_parser_parse_tentatively (parser);
20738           decl = cp_parser_alias_declaration (parser);
20739           /* Note that if we actually see the '=' token after the
20740              identifier, cp_parser_alias_declaration commits the
20741              tentative parse.  In that case, we really expects an
20742              alias-declaration.  Otherwise, we expect a using
20743              declaration.  */
20744           alias_decl_expected =
20745             !cp_parser_uncommitted_to_tentative_parse_p (parser);
20746           cp_parser_parse_definitely (parser);
20747
20748           if (alias_decl_expected)
20749             finish_member_declaration (decl);
20750           else
20751             cp_parser_using_declaration (parser,
20752                                          /*access_declaration_p=*/false);
20753           return;
20754         }
20755     }
20756
20757   /* Check for @defs.  */
20758   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20759     {
20760       tree ivar, member;
20761       tree ivar_chains = cp_parser_objc_defs_expression (parser);
20762       ivar = ivar_chains;
20763       while (ivar)
20764         {
20765           member = ivar;
20766           ivar = TREE_CHAIN (member);
20767           TREE_CHAIN (member) = NULL_TREE;
20768           finish_member_declaration (member);
20769         }
20770       return;
20771     }
20772
20773   /* If the next token is `static_assert' we have a static assertion.  */
20774   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20775     {
20776       cp_parser_static_assert (parser, /*member_p=*/true);
20777       return;
20778     }
20779
20780   parser->colon_corrects_to_scope_p = false;
20781
20782   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20783       goto out;
20784
20785   /* Parse the decl-specifier-seq.  */
20786   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20787   cp_parser_decl_specifier_seq (parser,
20788                                 CP_PARSER_FLAGS_OPTIONAL,
20789                                 &decl_specifiers,
20790                                 &declares_class_or_enum);
20791   /* Check for an invalid type-name.  */
20792   if (!decl_specifiers.any_type_specifiers_p
20793       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20794     goto out;
20795   /* If there is no declarator, then the decl-specifier-seq should
20796      specify a type.  */
20797   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20798     {
20799       /* If there was no decl-specifier-seq, and the next token is a
20800          `;', then we have something like:
20801
20802            struct S { ; };
20803
20804          [class.mem]
20805
20806          Each member-declaration shall declare at least one member
20807          name of the class.  */
20808       if (!decl_specifiers.any_specifiers_p)
20809         {
20810           cp_token *token = cp_lexer_peek_token (parser->lexer);
20811           if (!in_system_header_at (token->location))
20812             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20813         }
20814       else
20815         {
20816           tree type;
20817
20818           /* See if this declaration is a friend.  */
20819           friend_p = cp_parser_friend_p (&decl_specifiers);
20820           /* If there were decl-specifiers, check to see if there was
20821              a class-declaration.  */
20822           type = check_tag_decl (&decl_specifiers,
20823                                  /*explicit_type_instantiation_p=*/false);
20824           /* Nested classes have already been added to the class, but
20825              a `friend' needs to be explicitly registered.  */
20826           if (friend_p)
20827             {
20828               /* If the `friend' keyword was present, the friend must
20829                  be introduced with a class-key.  */
20830                if (!declares_class_or_enum && cxx_dialect < cxx11)
20831                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20832                           "in C++03 a class-key must be used "
20833                           "when declaring a friend");
20834                /* In this case:
20835
20836                     template <typename T> struct A {
20837                       friend struct A<T>::B;
20838                     };
20839
20840                   A<T>::B will be represented by a TYPENAME_TYPE, and
20841                   therefore not recognized by check_tag_decl.  */
20842                if (!type)
20843                  {
20844                    type = decl_specifiers.type;
20845                    if (type && TREE_CODE (type) == TYPE_DECL)
20846                      type = TREE_TYPE (type);
20847                  }
20848                if (!type || !TYPE_P (type))
20849                  error_at (decl_spec_token_start->location,
20850                            "friend declaration does not name a class or "
20851                            "function");
20852                else
20853                  make_friend_class (current_class_type, type,
20854                                     /*complain=*/true);
20855             }
20856           /* If there is no TYPE, an error message will already have
20857              been issued.  */
20858           else if (!type || type == error_mark_node)
20859             ;
20860           /* An anonymous aggregate has to be handled specially; such
20861              a declaration really declares a data member (with a
20862              particular type), as opposed to a nested class.  */
20863           else if (ANON_AGGR_TYPE_P (type))
20864             {
20865               /* C++11 9.5/6.  */
20866               if (decl_specifiers.storage_class != sc_none)
20867                 error_at (decl_spec_token_start->location,
20868                           "a storage class on an anonymous aggregate "
20869                           "in class scope is not allowed");
20870
20871               /* Remove constructors and such from TYPE, now that we
20872                  know it is an anonymous aggregate.  */
20873               fixup_anonymous_aggr (type);
20874               /* And make the corresponding data member.  */
20875               decl = build_decl (decl_spec_token_start->location,
20876                                  FIELD_DECL, NULL_TREE, type);
20877               /* Add it to the class.  */
20878               finish_member_declaration (decl);
20879             }
20880           else
20881             cp_parser_check_access_in_redeclaration
20882                                               (TYPE_NAME (type),
20883                                                decl_spec_token_start->location);
20884         }
20885     }
20886   else
20887     {
20888       bool assume_semicolon = false;
20889
20890       /* Clear attributes from the decl_specifiers but keep them
20891          around as prefix attributes that apply them to the entity
20892          being declared.  */
20893       prefix_attributes = decl_specifiers.attributes;
20894       decl_specifiers.attributes = NULL_TREE;
20895
20896       /* See if these declarations will be friends.  */
20897       friend_p = cp_parser_friend_p (&decl_specifiers);
20898
20899       /* Keep going until we hit the `;' at the end of the
20900          declaration.  */
20901       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20902         {
20903           tree attributes = NULL_TREE;
20904           tree first_attribute;
20905
20906           /* Peek at the next token.  */
20907           token = cp_lexer_peek_token (parser->lexer);
20908
20909           /* Check for a bitfield declaration.  */
20910           if (token->type == CPP_COLON
20911               || (token->type == CPP_NAME
20912                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20913                   == CPP_COLON))
20914             {
20915               tree identifier;
20916               tree width;
20917
20918               /* Get the name of the bitfield.  Note that we cannot just
20919                  check TOKEN here because it may have been invalidated by
20920                  the call to cp_lexer_peek_nth_token above.  */
20921               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20922                 identifier = cp_parser_identifier (parser);
20923               else
20924                 identifier = NULL_TREE;
20925
20926               /* Consume the `:' token.  */
20927               cp_lexer_consume_token (parser->lexer);
20928               /* Get the width of the bitfield.  */
20929               width
20930                 = cp_parser_constant_expression (parser);
20931
20932               /* Look for attributes that apply to the bitfield.  */
20933               attributes = cp_parser_attributes_opt (parser);
20934               /* Remember which attributes are prefix attributes and
20935                  which are not.  */
20936               first_attribute = attributes;
20937               /* Combine the attributes.  */
20938               attributes = chainon (prefix_attributes, attributes);
20939
20940               /* Create the bitfield declaration.  */
20941               decl = grokbitfield (identifier
20942                                    ? make_id_declarator (NULL_TREE,
20943                                                          identifier,
20944                                                          sfk_none)
20945                                    : NULL,
20946                                    &decl_specifiers,
20947                                    width,
20948                                    attributes);
20949             }
20950           else
20951             {
20952               cp_declarator *declarator;
20953               tree initializer;
20954               tree asm_specification;
20955               int ctor_dtor_or_conv_p;
20956
20957               /* Parse the declarator.  */
20958               declarator
20959                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20960                                         &ctor_dtor_or_conv_p,
20961                                         /*parenthesized_p=*/NULL,
20962                                         /*member_p=*/true,
20963                                         friend_p);
20964
20965               /* If something went wrong parsing the declarator, make sure
20966                  that we at least consume some tokens.  */
20967               if (declarator == cp_error_declarator)
20968                 {
20969                   /* Skip to the end of the statement.  */
20970                   cp_parser_skip_to_end_of_statement (parser);
20971                   /* If the next token is not a semicolon, that is
20972                      probably because we just skipped over the body of
20973                      a function.  So, we consume a semicolon if
20974                      present, but do not issue an error message if it
20975                      is not present.  */
20976                   if (cp_lexer_next_token_is (parser->lexer,
20977                                               CPP_SEMICOLON))
20978                     cp_lexer_consume_token (parser->lexer);
20979                   goto out;
20980                 }
20981
20982               if (declares_class_or_enum & 2)
20983                 cp_parser_check_for_definition_in_return_type
20984                                             (declarator, decl_specifiers.type,
20985                                              decl_specifiers.locations[ds_type_spec]);
20986
20987               /* Look for an asm-specification.  */
20988               asm_specification = cp_parser_asm_specification_opt (parser);
20989               /* Look for attributes that apply to the declaration.  */
20990               attributes = cp_parser_attributes_opt (parser);
20991               /* Remember which attributes are prefix attributes and
20992                  which are not.  */
20993               first_attribute = attributes;
20994               /* Combine the attributes.  */
20995               attributes = chainon (prefix_attributes, attributes);
20996
20997               /* If it's an `=', then we have a constant-initializer or a
20998                  pure-specifier.  It is not correct to parse the
20999                  initializer before registering the member declaration
21000                  since the member declaration should be in scope while
21001                  its initializer is processed.  However, the rest of the
21002                  front end does not yet provide an interface that allows
21003                  us to handle this correctly.  */
21004               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21005                 {
21006                   /* In [class.mem]:
21007
21008                      A pure-specifier shall be used only in the declaration of
21009                      a virtual function.
21010
21011                      A member-declarator can contain a constant-initializer
21012                      only if it declares a static member of integral or
21013                      enumeration type.
21014
21015                      Therefore, if the DECLARATOR is for a function, we look
21016                      for a pure-specifier; otherwise, we look for a
21017                      constant-initializer.  When we call `grokfield', it will
21018                      perform more stringent semantics checks.  */
21019                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
21020                   if (function_declarator_p (declarator)
21021                       || (decl_specifiers.type
21022                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21023                           && declarator->kind == cdk_id
21024                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21025                               == FUNCTION_TYPE)))
21026                     initializer = cp_parser_pure_specifier (parser);
21027                   else if (decl_specifiers.storage_class != sc_static)
21028                     initializer = cp_parser_save_nsdmi (parser);
21029                   else if (cxx_dialect >= cxx11)
21030                     {
21031                       bool nonconst;
21032                       /* Don't require a constant rvalue in C++11, since we
21033                          might want a reference constant.  We'll enforce
21034                          constancy later.  */
21035                       cp_lexer_consume_token (parser->lexer);
21036                       /* Parse the initializer.  */
21037                       initializer = cp_parser_initializer_clause (parser,
21038                                                                   &nonconst);
21039                     }
21040                   else
21041                     /* Parse the initializer.  */
21042                     initializer = cp_parser_constant_initializer (parser);
21043                 }
21044               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21045                        && !function_declarator_p (declarator))
21046                 {
21047                   bool x;
21048                   if (decl_specifiers.storage_class != sc_static)
21049                     initializer = cp_parser_save_nsdmi (parser);
21050                   else
21051                     initializer = cp_parser_initializer (parser, &x, &x);
21052                 }
21053               /* Otherwise, there is no initializer.  */
21054               else
21055                 initializer = NULL_TREE;
21056
21057               /* See if we are probably looking at a function
21058                  definition.  We are certainly not looking at a
21059                  member-declarator.  Calling `grokfield' has
21060                  side-effects, so we must not do it unless we are sure
21061                  that we are looking at a member-declarator.  */
21062               if (cp_parser_token_starts_function_definition_p
21063                   (cp_lexer_peek_token (parser->lexer)))
21064                 {
21065                   /* The grammar does not allow a pure-specifier to be
21066                      used when a member function is defined.  (It is
21067                      possible that this fact is an oversight in the
21068                      standard, since a pure function may be defined
21069                      outside of the class-specifier.  */
21070                   if (initializer && initializer_token_start)
21071                     error_at (initializer_token_start->location,
21072                               "pure-specifier on function-definition");
21073                   decl = cp_parser_save_member_function_body (parser,
21074                                                               &decl_specifiers,
21075                                                               declarator,
21076                                                               attributes);
21077                   if (parser->fully_implicit_function_template_p)
21078                     decl = finish_fully_implicit_template (parser, decl);
21079                   /* If the member was not a friend, declare it here.  */
21080                   if (!friend_p)
21081                     finish_member_declaration (decl);
21082                   /* Peek at the next token.  */
21083                   token = cp_lexer_peek_token (parser->lexer);
21084                   /* If the next token is a semicolon, consume it.  */
21085                   if (token->type == CPP_SEMICOLON)
21086                     cp_lexer_consume_token (parser->lexer);
21087                   goto out;
21088                 }
21089               else
21090                 if (declarator->kind == cdk_function)
21091                   declarator->id_loc = token->location;
21092               /* Create the declaration.  */
21093               decl = grokfield (declarator, &decl_specifiers,
21094                                 initializer, /*init_const_expr_p=*/true,
21095                                 asm_specification, attributes);
21096               if (parser->fully_implicit_function_template_p)
21097                 {
21098                   if (friend_p)
21099                     finish_fully_implicit_template (parser, 0);
21100                   else
21101                     decl = finish_fully_implicit_template (parser, decl);
21102                 }
21103             }
21104
21105           cp_finalize_omp_declare_simd (parser, decl);
21106
21107           /* Reset PREFIX_ATTRIBUTES.  */
21108           while (attributes && TREE_CHAIN (attributes) != first_attribute)
21109             attributes = TREE_CHAIN (attributes);
21110           if (attributes)
21111             TREE_CHAIN (attributes) = NULL_TREE;
21112
21113           /* If there is any qualification still in effect, clear it
21114              now; we will be starting fresh with the next declarator.  */
21115           parser->scope = NULL_TREE;
21116           parser->qualifying_scope = NULL_TREE;
21117           parser->object_scope = NULL_TREE;
21118           /* If it's a `,', then there are more declarators.  */
21119           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21120             {
21121               cp_lexer_consume_token (parser->lexer);
21122               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21123                 {
21124                   cp_token *token = cp_lexer_previous_token (parser->lexer);
21125                   error_at (token->location,
21126                             "stray %<,%> at end of member declaration");
21127                 }
21128             }
21129           /* If the next token isn't a `;', then we have a parse error.  */
21130           else if (cp_lexer_next_token_is_not (parser->lexer,
21131                                                CPP_SEMICOLON))
21132             {
21133               /* The next token might be a ways away from where the
21134                  actual semicolon is missing.  Find the previous token
21135                  and use that for our error position.  */
21136               cp_token *token = cp_lexer_previous_token (parser->lexer);
21137               error_at (token->location,
21138                         "expected %<;%> at end of member declaration");
21139
21140               /* Assume that the user meant to provide a semicolon.  If
21141                  we were to cp_parser_skip_to_end_of_statement, we might
21142                  skip to a semicolon inside a member function definition
21143                  and issue nonsensical error messages.  */
21144               assume_semicolon = true;
21145             }
21146
21147           if (decl)
21148             {
21149               /* Add DECL to the list of members.  */
21150               if (!friend_p
21151                   /* Explicitly include, eg, NSDMIs, for better error
21152                      recovery (c++/58650).  */
21153                   || !DECL_DECLARES_FUNCTION_P (decl))
21154                 finish_member_declaration (decl);
21155
21156               if (TREE_CODE (decl) == FUNCTION_DECL)
21157                 cp_parser_save_default_args (parser, decl);
21158               else if (TREE_CODE (decl) == FIELD_DECL
21159                        && !DECL_C_BIT_FIELD (decl)
21160                        && DECL_INITIAL (decl))
21161                 /* Add DECL to the queue of NSDMI to be parsed later.  */
21162                 vec_safe_push (unparsed_nsdmis, decl);
21163             }
21164
21165           if (assume_semicolon)
21166             goto out;
21167         }
21168     }
21169
21170   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21171  out:
21172   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21173 }
21174
21175 /* Parse a pure-specifier.
21176
21177    pure-specifier:
21178      = 0
21179
21180    Returns INTEGER_ZERO_NODE if a pure specifier is found.
21181    Otherwise, ERROR_MARK_NODE is returned.  */
21182
21183 static tree
21184 cp_parser_pure_specifier (cp_parser* parser)
21185 {
21186   cp_token *token;
21187
21188   /* Look for the `=' token.  */
21189   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21190     return error_mark_node;
21191   /* Look for the `0' token.  */
21192   token = cp_lexer_peek_token (parser->lexer);
21193
21194   if (token->type == CPP_EOF
21195       || token->type == CPP_PRAGMA_EOL)
21196     return error_mark_node;
21197
21198   cp_lexer_consume_token (parser->lexer);
21199
21200   /* Accept = default or = delete in c++0x mode.  */
21201   if (token->keyword == RID_DEFAULT
21202       || token->keyword == RID_DELETE)
21203     {
21204       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21205       return token->u.value;
21206     }
21207
21208   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
21209   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21210     {
21211       cp_parser_error (parser,
21212                        "invalid pure specifier (only %<= 0%> is allowed)");
21213       cp_parser_skip_to_end_of_statement (parser);
21214       return error_mark_node;
21215     }
21216   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21217     {
21218       error_at (token->location, "templates may not be %<virtual%>");
21219       return error_mark_node;
21220     }
21221
21222   return integer_zero_node;
21223 }
21224
21225 /* Parse a constant-initializer.
21226
21227    constant-initializer:
21228      = constant-expression
21229
21230    Returns a representation of the constant-expression.  */
21231
21232 static tree
21233 cp_parser_constant_initializer (cp_parser* parser)
21234 {
21235   /* Look for the `=' token.  */
21236   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21237     return error_mark_node;
21238
21239   /* It is invalid to write:
21240
21241        struct S { static const int i = { 7 }; };
21242
21243      */
21244   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21245     {
21246       cp_parser_error (parser,
21247                        "a brace-enclosed initializer is not allowed here");
21248       /* Consume the opening brace.  */
21249       cp_lexer_consume_token (parser->lexer);
21250       /* Skip the initializer.  */
21251       cp_parser_skip_to_closing_brace (parser);
21252       /* Look for the trailing `}'.  */
21253       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21254
21255       return error_mark_node;
21256     }
21257
21258   return cp_parser_constant_expression (parser);
21259 }
21260
21261 /* Derived classes [gram.class.derived] */
21262
21263 /* Parse a base-clause.
21264
21265    base-clause:
21266      : base-specifier-list
21267
21268    base-specifier-list:
21269      base-specifier ... [opt]
21270      base-specifier-list , base-specifier ... [opt]
21271
21272    Returns a TREE_LIST representing the base-classes, in the order in
21273    which they were declared.  The representation of each node is as
21274    described by cp_parser_base_specifier.
21275
21276    In the case that no bases are specified, this function will return
21277    NULL_TREE, not ERROR_MARK_NODE.  */
21278
21279 static tree
21280 cp_parser_base_clause (cp_parser* parser)
21281 {
21282   tree bases = NULL_TREE;
21283
21284   /* Look for the `:' that begins the list.  */
21285   cp_parser_require (parser, CPP_COLON, RT_COLON);
21286
21287   /* Scan the base-specifier-list.  */
21288   while (true)
21289     {
21290       cp_token *token;
21291       tree base;
21292       bool pack_expansion_p = false;
21293
21294       /* Look for the base-specifier.  */
21295       base = cp_parser_base_specifier (parser);
21296       /* Look for the (optional) ellipsis. */
21297       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21298         {
21299           /* Consume the `...'. */
21300           cp_lexer_consume_token (parser->lexer);
21301
21302           pack_expansion_p = true;
21303         }
21304
21305       /* Add BASE to the front of the list.  */
21306       if (base && base != error_mark_node)
21307         {
21308           if (pack_expansion_p)
21309             /* Make this a pack expansion type. */
21310             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21311
21312           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21313             {
21314               TREE_CHAIN (base) = bases;
21315               bases = base;
21316             }
21317         }
21318       /* Peek at the next token.  */
21319       token = cp_lexer_peek_token (parser->lexer);
21320       /* If it's not a comma, then the list is complete.  */
21321       if (token->type != CPP_COMMA)
21322         break;
21323       /* Consume the `,'.  */
21324       cp_lexer_consume_token (parser->lexer);
21325     }
21326
21327   /* PARSER->SCOPE may still be non-NULL at this point, if the last
21328      base class had a qualified name.  However, the next name that
21329      appears is certainly not qualified.  */
21330   parser->scope = NULL_TREE;
21331   parser->qualifying_scope = NULL_TREE;
21332   parser->object_scope = NULL_TREE;
21333
21334   return nreverse (bases);
21335 }
21336
21337 /* Parse a base-specifier.
21338
21339    base-specifier:
21340      :: [opt] nested-name-specifier [opt] class-name
21341      virtual access-specifier [opt] :: [opt] nested-name-specifier
21342        [opt] class-name
21343      access-specifier virtual [opt] :: [opt] nested-name-specifier
21344        [opt] class-name
21345
21346    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
21347    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21348    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
21349    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
21350
21351 static tree
21352 cp_parser_base_specifier (cp_parser* parser)
21353 {
21354   cp_token *token;
21355   bool done = false;
21356   bool virtual_p = false;
21357   bool duplicate_virtual_error_issued_p = false;
21358   bool duplicate_access_error_issued_p = false;
21359   bool class_scope_p, template_p;
21360   tree access = access_default_node;
21361   tree type;
21362
21363   /* Process the optional `virtual' and `access-specifier'.  */
21364   while (!done)
21365     {
21366       /* Peek at the next token.  */
21367       token = cp_lexer_peek_token (parser->lexer);
21368       /* Process `virtual'.  */
21369       switch (token->keyword)
21370         {
21371         case RID_VIRTUAL:
21372           /* If `virtual' appears more than once, issue an error.  */
21373           if (virtual_p && !duplicate_virtual_error_issued_p)
21374             {
21375               cp_parser_error (parser,
21376                                "%<virtual%> specified more than once in base-specified");
21377               duplicate_virtual_error_issued_p = true;
21378             }
21379
21380           virtual_p = true;
21381
21382           /* Consume the `virtual' token.  */
21383           cp_lexer_consume_token (parser->lexer);
21384
21385           break;
21386
21387         case RID_PUBLIC:
21388         case RID_PROTECTED:
21389         case RID_PRIVATE:
21390           /* If more than one access specifier appears, issue an
21391              error.  */
21392           if (access != access_default_node
21393               && !duplicate_access_error_issued_p)
21394             {
21395               cp_parser_error (parser,
21396                                "more than one access specifier in base-specified");
21397               duplicate_access_error_issued_p = true;
21398             }
21399
21400           access = ridpointers[(int) token->keyword];
21401
21402           /* Consume the access-specifier.  */
21403           cp_lexer_consume_token (parser->lexer);
21404
21405           break;
21406
21407         default:
21408           done = true;
21409           break;
21410         }
21411     }
21412   /* It is not uncommon to see programs mechanically, erroneously, use
21413      the 'typename' keyword to denote (dependent) qualified types
21414      as base classes.  */
21415   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21416     {
21417       token = cp_lexer_peek_token (parser->lexer);
21418       if (!processing_template_decl)
21419         error_at (token->location,
21420                   "keyword %<typename%> not allowed outside of templates");
21421       else
21422         error_at (token->location,
21423                   "keyword %<typename%> not allowed in this context "
21424                   "(the base class is implicitly a type)");
21425       cp_lexer_consume_token (parser->lexer);
21426     }
21427
21428   /* Look for the optional `::' operator.  */
21429   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21430   /* Look for the nested-name-specifier.  The simplest way to
21431      implement:
21432
21433        [temp.res]
21434
21435        The keyword `typename' is not permitted in a base-specifier or
21436        mem-initializer; in these contexts a qualified name that
21437        depends on a template-parameter is implicitly assumed to be a
21438        type name.
21439
21440      is to pretend that we have seen the `typename' keyword at this
21441      point.  */
21442   cp_parser_nested_name_specifier_opt (parser,
21443                                        /*typename_keyword_p=*/true,
21444                                        /*check_dependency_p=*/true,
21445                                        typename_type,
21446                                        /*is_declaration=*/true);
21447   /* If the base class is given by a qualified name, assume that names
21448      we see are type names or templates, as appropriate.  */
21449   class_scope_p = (parser->scope && TYPE_P (parser->scope));
21450   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21451
21452   if (!parser->scope
21453       && cp_lexer_next_token_is_decltype (parser->lexer))
21454     /* DR 950 allows decltype as a base-specifier.  */
21455     type = cp_parser_decltype (parser);
21456   else
21457     {
21458       /* Otherwise, look for the class-name.  */
21459       type = cp_parser_class_name (parser,
21460                                    class_scope_p,
21461                                    template_p,
21462                                    typename_type,
21463                                    /*check_dependency_p=*/true,
21464                                    /*class_head_p=*/false,
21465                                    /*is_declaration=*/true);
21466       type = TREE_TYPE (type);
21467     }
21468
21469   if (type == error_mark_node)
21470     return error_mark_node;
21471
21472   return finish_base_specifier (type, access, virtual_p);
21473 }
21474
21475 /* Exception handling [gram.exception] */
21476
21477 /* Parse an (optional) noexcept-specification.
21478
21479    noexcept-specification:
21480      noexcept ( constant-expression ) [opt]
21481
21482    If no noexcept-specification is present, returns NULL_TREE.
21483    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21484    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21485    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
21486    Otherwise, returns a noexcept specification unless RETURN_COND is true,
21487    in which case a boolean condition is returned instead.  */
21488
21489 static tree
21490 cp_parser_noexcept_specification_opt (cp_parser* parser,
21491                                       bool require_constexpr,
21492                                       bool* consumed_expr,
21493                                       bool return_cond)
21494 {
21495   cp_token *token;
21496   const char *saved_message;
21497
21498   /* Peek at the next token.  */
21499   token = cp_lexer_peek_token (parser->lexer);
21500
21501   /* Is it a noexcept-specification?  */
21502   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21503     {
21504       tree expr;
21505       cp_lexer_consume_token (parser->lexer);
21506
21507       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21508         {
21509           cp_lexer_consume_token (parser->lexer);
21510
21511           if (require_constexpr)
21512             {
21513               /* Types may not be defined in an exception-specification.  */
21514               saved_message = parser->type_definition_forbidden_message;
21515               parser->type_definition_forbidden_message
21516               = G_("types may not be defined in an exception-specification");
21517
21518               expr = cp_parser_constant_expression (parser);
21519
21520               /* Restore the saved message.  */
21521               parser->type_definition_forbidden_message = saved_message;
21522             }
21523           else
21524             {
21525               expr = cp_parser_expression (parser);
21526               *consumed_expr = true;
21527             }
21528
21529           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21530         }
21531       else
21532         {
21533           expr = boolean_true_node;
21534           if (!require_constexpr)
21535             *consumed_expr = false;
21536         }
21537
21538       /* We cannot build a noexcept-spec right away because this will check
21539          that expr is a constexpr.  */
21540       if (!return_cond)
21541         return build_noexcept_spec (expr, tf_warning_or_error);
21542       else
21543         return expr;
21544     }
21545   else
21546     return NULL_TREE;
21547 }
21548
21549 /* Parse an (optional) exception-specification.
21550
21551    exception-specification:
21552      throw ( type-id-list [opt] )
21553
21554    Returns a TREE_LIST representing the exception-specification.  The
21555    TREE_VALUE of each node is a type.  */
21556
21557 static tree
21558 cp_parser_exception_specification_opt (cp_parser* parser)
21559 {
21560   cp_token *token;
21561   tree type_id_list;
21562   const char *saved_message;
21563
21564   /* Peek at the next token.  */
21565   token = cp_lexer_peek_token (parser->lexer);
21566
21567   /* Is it a noexcept-specification?  */
21568   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21569                                                       false);
21570   if (type_id_list != NULL_TREE)
21571     return type_id_list;
21572
21573   /* If it's not `throw', then there's no exception-specification.  */
21574   if (!cp_parser_is_keyword (token, RID_THROW))
21575     return NULL_TREE;
21576
21577 #if 0
21578   /* Enable this once a lot of code has transitioned to noexcept?  */
21579   if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21580     warning (OPT_Wdeprecated, "dynamic exception specifications are "
21581              "deprecated in C++0x; use %<noexcept%> instead");
21582 #endif
21583
21584   /* Consume the `throw'.  */
21585   cp_lexer_consume_token (parser->lexer);
21586
21587   /* Look for the `('.  */
21588   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21589
21590   /* Peek at the next token.  */
21591   token = cp_lexer_peek_token (parser->lexer);
21592   /* If it's not a `)', then there is a type-id-list.  */
21593   if (token->type != CPP_CLOSE_PAREN)
21594     {
21595       /* Types may not be defined in an exception-specification.  */
21596       saved_message = parser->type_definition_forbidden_message;
21597       parser->type_definition_forbidden_message
21598         = G_("types may not be defined in an exception-specification");
21599       /* Parse the type-id-list.  */
21600       type_id_list = cp_parser_type_id_list (parser);
21601       /* Restore the saved message.  */
21602       parser->type_definition_forbidden_message = saved_message;
21603     }
21604   else
21605     type_id_list = empty_except_spec;
21606
21607   /* Look for the `)'.  */
21608   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21609
21610   return type_id_list;
21611 }
21612
21613 /* Parse an (optional) type-id-list.
21614
21615    type-id-list:
21616      type-id ... [opt]
21617      type-id-list , type-id ... [opt]
21618
21619    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
21620    in the order that the types were presented.  */
21621
21622 static tree
21623 cp_parser_type_id_list (cp_parser* parser)
21624 {
21625   tree types = NULL_TREE;
21626
21627   while (true)
21628     {
21629       cp_token *token;
21630       tree type;
21631
21632       /* Get the next type-id.  */
21633       type = cp_parser_type_id (parser);
21634       /* Parse the optional ellipsis. */
21635       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21636         {
21637           /* Consume the `...'. */
21638           cp_lexer_consume_token (parser->lexer);
21639
21640           /* Turn the type into a pack expansion expression. */
21641           type = make_pack_expansion (type);
21642         }
21643       /* Add it to the list.  */
21644       types = add_exception_specifier (types, type, /*complain=*/1);
21645       /* Peek at the next token.  */
21646       token = cp_lexer_peek_token (parser->lexer);
21647       /* If it is not a `,', we are done.  */
21648       if (token->type != CPP_COMMA)
21649         break;
21650       /* Consume the `,'.  */
21651       cp_lexer_consume_token (parser->lexer);
21652     }
21653
21654   return nreverse (types);
21655 }
21656
21657 /* Parse a try-block.
21658
21659    try-block:
21660      try compound-statement handler-seq  */
21661
21662 static tree
21663 cp_parser_try_block (cp_parser* parser)
21664 {
21665   tree try_block;
21666
21667   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21668   if (parser->in_function_body
21669       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21670     error ("%<try%> in %<constexpr%> function");
21671
21672   try_block = begin_try_block ();
21673   cp_parser_compound_statement (parser, NULL, true, false);
21674   finish_try_block (try_block);
21675   cp_parser_handler_seq (parser);
21676   finish_handler_sequence (try_block);
21677
21678   return try_block;
21679 }
21680
21681 /* Parse a function-try-block.
21682
21683    function-try-block:
21684      try ctor-initializer [opt] function-body handler-seq  */
21685
21686 static bool
21687 cp_parser_function_try_block (cp_parser* parser)
21688 {
21689   tree compound_stmt;
21690   tree try_block;
21691   bool ctor_initializer_p;
21692
21693   /* Look for the `try' keyword.  */
21694   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21695     return false;
21696   /* Let the rest of the front end know where we are.  */
21697   try_block = begin_function_try_block (&compound_stmt);
21698   /* Parse the function-body.  */
21699   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21700     (parser, /*in_function_try_block=*/true);
21701   /* We're done with the `try' part.  */
21702   finish_function_try_block (try_block);
21703   /* Parse the handlers.  */
21704   cp_parser_handler_seq (parser);
21705   /* We're done with the handlers.  */
21706   finish_function_handler_sequence (try_block, compound_stmt);
21707
21708   return ctor_initializer_p;
21709 }
21710
21711 /* Parse a handler-seq.
21712
21713    handler-seq:
21714      handler handler-seq [opt]  */
21715
21716 static void
21717 cp_parser_handler_seq (cp_parser* parser)
21718 {
21719   while (true)
21720     {
21721       cp_token *token;
21722
21723       /* Parse the handler.  */
21724       cp_parser_handler (parser);
21725       /* Peek at the next token.  */
21726       token = cp_lexer_peek_token (parser->lexer);
21727       /* If it's not `catch' then there are no more handlers.  */
21728       if (!cp_parser_is_keyword (token, RID_CATCH))
21729         break;
21730     }
21731 }
21732
21733 /* Parse a handler.
21734
21735    handler:
21736      catch ( exception-declaration ) compound-statement  */
21737
21738 static void
21739 cp_parser_handler (cp_parser* parser)
21740 {
21741   tree handler;
21742   tree declaration;
21743
21744   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21745   handler = begin_handler ();
21746   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21747   declaration = cp_parser_exception_declaration (parser);
21748   finish_handler_parms (declaration, handler);
21749   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21750   cp_parser_compound_statement (parser, NULL, false, false);
21751   finish_handler (handler);
21752 }
21753
21754 /* Parse an exception-declaration.
21755
21756    exception-declaration:
21757      type-specifier-seq declarator
21758      type-specifier-seq abstract-declarator
21759      type-specifier-seq
21760      ...
21761
21762    Returns a VAR_DECL for the declaration, or NULL_TREE if the
21763    ellipsis variant is used.  */
21764
21765 static tree
21766 cp_parser_exception_declaration (cp_parser* parser)
21767 {
21768   cp_decl_specifier_seq type_specifiers;
21769   cp_declarator *declarator;
21770   const char *saved_message;
21771
21772   /* If it's an ellipsis, it's easy to handle.  */
21773   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21774     {
21775       /* Consume the `...' token.  */
21776       cp_lexer_consume_token (parser->lexer);
21777       return NULL_TREE;
21778     }
21779
21780   /* Types may not be defined in exception-declarations.  */
21781   saved_message = parser->type_definition_forbidden_message;
21782   parser->type_definition_forbidden_message
21783     = G_("types may not be defined in exception-declarations");
21784
21785   /* Parse the type-specifier-seq.  */
21786   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21787                                 /*is_trailing_return=*/false,
21788                                 &type_specifiers);
21789   /* If it's a `)', then there is no declarator.  */
21790   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21791     declarator = NULL;
21792   else
21793     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21794                                        /*ctor_dtor_or_conv_p=*/NULL,
21795                                        /*parenthesized_p=*/NULL,
21796                                        /*member_p=*/false,
21797                                        /*friend_p=*/false);
21798
21799   /* Restore the saved message.  */
21800   parser->type_definition_forbidden_message = saved_message;
21801
21802   if (!type_specifiers.any_specifiers_p)
21803     return error_mark_node;
21804
21805   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21806 }
21807
21808 /* Parse a throw-expression.
21809
21810    throw-expression:
21811      throw assignment-expression [opt]
21812
21813    Returns a THROW_EXPR representing the throw-expression.  */
21814
21815 static tree
21816 cp_parser_throw_expression (cp_parser* parser)
21817 {
21818   tree expression;
21819   cp_token* token;
21820
21821   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21822   token = cp_lexer_peek_token (parser->lexer);
21823   /* Figure out whether or not there is an assignment-expression
21824      following the "throw" keyword.  */
21825   if (token->type == CPP_COMMA
21826       || token->type == CPP_SEMICOLON
21827       || token->type == CPP_CLOSE_PAREN
21828       || token->type == CPP_CLOSE_SQUARE
21829       || token->type == CPP_CLOSE_BRACE
21830       || token->type == CPP_COLON)
21831     expression = NULL_TREE;
21832   else
21833     expression = cp_parser_assignment_expression (parser);
21834
21835   return build_throw (expression);
21836 }
21837
21838 /* GNU Extensions */
21839
21840 /* Parse an (optional) asm-specification.
21841
21842    asm-specification:
21843      asm ( string-literal )
21844
21845    If the asm-specification is present, returns a STRING_CST
21846    corresponding to the string-literal.  Otherwise, returns
21847    NULL_TREE.  */
21848
21849 static tree
21850 cp_parser_asm_specification_opt (cp_parser* parser)
21851 {
21852   cp_token *token;
21853   tree asm_specification;
21854
21855   /* Peek at the next token.  */
21856   token = cp_lexer_peek_token (parser->lexer);
21857   /* If the next token isn't the `asm' keyword, then there's no
21858      asm-specification.  */
21859   if (!cp_parser_is_keyword (token, RID_ASM))
21860     return NULL_TREE;
21861
21862   /* Consume the `asm' token.  */
21863   cp_lexer_consume_token (parser->lexer);
21864   /* Look for the `('.  */
21865   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21866
21867   /* Look for the string-literal.  */
21868   asm_specification = cp_parser_string_literal (parser, false, false);
21869
21870   /* Look for the `)'.  */
21871   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21872
21873   return asm_specification;
21874 }
21875
21876 /* Parse an asm-operand-list.
21877
21878    asm-operand-list:
21879      asm-operand
21880      asm-operand-list , asm-operand
21881
21882    asm-operand:
21883      string-literal ( expression )
21884      [ string-literal ] string-literal ( expression )
21885
21886    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
21887    each node is the expression.  The TREE_PURPOSE is itself a
21888    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21889    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21890    is a STRING_CST for the string literal before the parenthesis. Returns
21891    ERROR_MARK_NODE if any of the operands are invalid.  */
21892
21893 static tree
21894 cp_parser_asm_operand_list (cp_parser* parser)
21895 {
21896   tree asm_operands = NULL_TREE;
21897   bool invalid_operands = false;
21898
21899   while (true)
21900     {
21901       tree string_literal;
21902       tree expression;
21903       tree name;
21904
21905       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21906         {
21907           /* Consume the `[' token.  */
21908           cp_lexer_consume_token (parser->lexer);
21909           /* Read the operand name.  */
21910           name = cp_parser_identifier (parser);
21911           if (name != error_mark_node)
21912             name = build_string (IDENTIFIER_LENGTH (name),
21913                                  IDENTIFIER_POINTER (name));
21914           /* Look for the closing `]'.  */
21915           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21916         }
21917       else
21918         name = NULL_TREE;
21919       /* Look for the string-literal.  */
21920       string_literal = cp_parser_string_literal (parser, false, false);
21921
21922       /* Look for the `('.  */
21923       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21924       /* Parse the expression.  */
21925       expression = cp_parser_expression (parser);
21926       /* Look for the `)'.  */
21927       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21928
21929       if (name == error_mark_node 
21930           || string_literal == error_mark_node 
21931           || expression == error_mark_node)
21932         invalid_operands = true;
21933
21934       /* Add this operand to the list.  */
21935       asm_operands = tree_cons (build_tree_list (name, string_literal),
21936                                 expression,
21937                                 asm_operands);
21938       /* If the next token is not a `,', there are no more
21939          operands.  */
21940       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21941         break;
21942       /* Consume the `,'.  */
21943       cp_lexer_consume_token (parser->lexer);
21944     }
21945
21946   return invalid_operands ? error_mark_node : nreverse (asm_operands);
21947 }
21948
21949 /* Parse an asm-clobber-list.
21950
21951    asm-clobber-list:
21952      string-literal
21953      asm-clobber-list , string-literal
21954
21955    Returns a TREE_LIST, indicating the clobbers in the order that they
21956    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
21957
21958 static tree
21959 cp_parser_asm_clobber_list (cp_parser* parser)
21960 {
21961   tree clobbers = NULL_TREE;
21962
21963   while (true)
21964     {
21965       tree string_literal;
21966
21967       /* Look for the string literal.  */
21968       string_literal = cp_parser_string_literal (parser, false, false);
21969       /* Add it to the list.  */
21970       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21971       /* If the next token is not a `,', then the list is
21972          complete.  */
21973       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21974         break;
21975       /* Consume the `,' token.  */
21976       cp_lexer_consume_token (parser->lexer);
21977     }
21978
21979   return clobbers;
21980 }
21981
21982 /* Parse an asm-label-list.
21983
21984    asm-label-list:
21985      identifier
21986      asm-label-list , identifier
21987
21988    Returns a TREE_LIST, indicating the labels in the order that they
21989    appeared.  The TREE_VALUE of each node is a label.  */
21990
21991 static tree
21992 cp_parser_asm_label_list (cp_parser* parser)
21993 {
21994   tree labels = NULL_TREE;
21995
21996   while (true)
21997     {
21998       tree identifier, label, name;
21999
22000       /* Look for the identifier.  */
22001       identifier = cp_parser_identifier (parser);
22002       if (!error_operand_p (identifier))
22003         {
22004           label = lookup_label (identifier);
22005           if (TREE_CODE (label) == LABEL_DECL)
22006             {
22007               TREE_USED (label) = 1;
22008               check_goto (label);
22009               name = build_string (IDENTIFIER_LENGTH (identifier),
22010                                    IDENTIFIER_POINTER (identifier));
22011               labels = tree_cons (name, label, labels);
22012             }
22013         }
22014       /* If the next token is not a `,', then the list is
22015          complete.  */
22016       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22017         break;
22018       /* Consume the `,' token.  */
22019       cp_lexer_consume_token (parser->lexer);
22020     }
22021
22022   return nreverse (labels);
22023 }
22024
22025 /* Return TRUE iff the next tokens in the stream are possibly the
22026    beginning of a GNU extension attribute. */
22027
22028 static bool
22029 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22030 {
22031   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22032 }
22033
22034 /* Return TRUE iff the next tokens in the stream are possibly the
22035    beginning of a standard C++-11 attribute specifier.  */
22036
22037 static bool
22038 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22039 {
22040   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22041 }
22042
22043 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22044    beginning of a standard C++-11 attribute specifier.  */
22045
22046 static bool
22047 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22048 {
22049   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22050
22051   return (cxx_dialect >= cxx11
22052           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22053               || (token->type == CPP_OPEN_SQUARE
22054                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22055                   && token->type == CPP_OPEN_SQUARE)));
22056 }
22057
22058 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22059    beginning of a GNU extension attribute.  */
22060
22061 static bool
22062 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22063 {
22064   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22065
22066   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22067 }
22068
22069 /* Return true iff the next tokens can be the beginning of either a
22070    GNU attribute list, or a standard C++11 attribute sequence.  */
22071
22072 static bool
22073 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22074 {
22075   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22076           || cp_next_tokens_can_be_std_attribute_p (parser));
22077 }
22078
22079 /* Return true iff the next Nth tokens can be the beginning of either
22080    a GNU attribute list, or a standard C++11 attribute sequence.  */
22081
22082 static bool
22083 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22084 {
22085   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22086           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22087 }
22088
22089 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22090    of GNU attributes, or return NULL.  */
22091
22092 static tree
22093 cp_parser_attributes_opt (cp_parser *parser)
22094 {
22095   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22096       return cp_parser_gnu_attributes_opt (parser);
22097   return cp_parser_std_attribute_spec_seq (parser);
22098 }
22099
22100 #define CILK_SIMD_FN_CLAUSE_MASK                                  \
22101         ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH)   \
22102          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR)       \
22103          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM)      \
22104          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK)         \
22105          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22106
22107 /* Parses the Cilk Plus SIMD-enabled function's attribute.  Syntax:
22108    vector [(<clauses>)]  */
22109
22110 static void
22111 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22112 {  
22113   bool first_p = parser->cilk_simd_fn_info == NULL;
22114   cp_token *token = v_token;
22115   if (first_p)
22116     {
22117       parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22118       parser->cilk_simd_fn_info->error_seen = false;
22119       parser->cilk_simd_fn_info->fndecl_seen = false;
22120       parser->cilk_simd_fn_info->tokens = vNULL;
22121     }
22122   int paren_scope = 0;
22123   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22124     {
22125       cp_lexer_consume_token (parser->lexer);
22126       v_token = cp_lexer_peek_token (parser->lexer);
22127       paren_scope++;
22128     }
22129   while (paren_scope > 0)
22130     {
22131       token = cp_lexer_peek_token (parser->lexer);
22132       if (token->type == CPP_OPEN_PAREN)
22133         paren_scope++;
22134       else if (token->type == CPP_CLOSE_PAREN)
22135         paren_scope--;
22136       /* Do not push the last ')'  */
22137       if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22138         cp_lexer_consume_token (parser->lexer);
22139     }
22140
22141   token->type = CPP_PRAGMA_EOL;
22142   parser->lexer->next_token = token;
22143   cp_lexer_consume_token (parser->lexer);
22144
22145   struct cp_token_cache *cp
22146     = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22147   parser->cilk_simd_fn_info->tokens.safe_push (cp);
22148 }
22149
22150 /* Parse an (optional) series of attributes.
22151
22152    attributes:
22153      attributes attribute
22154
22155    attribute:
22156      __attribute__ (( attribute-list [opt] ))
22157
22158    The return value is as for cp_parser_gnu_attribute_list.  */
22159
22160 static tree
22161 cp_parser_gnu_attributes_opt (cp_parser* parser)
22162 {
22163   tree attributes = NULL_TREE;
22164
22165   while (true)
22166     {
22167       cp_token *token;
22168       tree attribute_list;
22169       bool ok = true;
22170
22171       /* Peek at the next token.  */
22172       token = cp_lexer_peek_token (parser->lexer);
22173       /* If it's not `__attribute__', then we're done.  */
22174       if (token->keyword != RID_ATTRIBUTE)
22175         break;
22176
22177       /* Consume the `__attribute__' keyword.  */
22178       cp_lexer_consume_token (parser->lexer);
22179       /* Look for the two `(' tokens.  */
22180       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22181       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22182
22183       /* Peek at the next token.  */
22184       token = cp_lexer_peek_token (parser->lexer);
22185       if (token->type != CPP_CLOSE_PAREN)
22186         /* Parse the attribute-list.  */
22187         attribute_list = cp_parser_gnu_attribute_list (parser);
22188       else
22189         /* If the next token is a `)', then there is no attribute
22190            list.  */
22191         attribute_list = NULL;
22192
22193       /* Look for the two `)' tokens.  */
22194       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22195         ok = false;
22196       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22197         ok = false;
22198       if (!ok)
22199         cp_parser_skip_to_end_of_statement (parser);
22200
22201       /* Add these new attributes to the list.  */
22202       attributes = chainon (attributes, attribute_list);
22203     }
22204
22205   return attributes;
22206 }
22207
22208 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22209    "__vector" or "__vector__."  */
22210
22211 static inline bool
22212 is_cilkplus_vector_p (tree name)
22213
22214   if (flag_cilkplus && is_attribute_p ("vector", name)) 
22215     return true;
22216   return false;
22217 }
22218
22219 /* Parse a GNU attribute-list.
22220
22221    attribute-list:
22222      attribute
22223      attribute-list , attribute
22224
22225    attribute:
22226      identifier
22227      identifier ( identifier )
22228      identifier ( identifier , expression-list )
22229      identifier ( expression-list )
22230
22231    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
22232    to an attribute.  The TREE_PURPOSE of each node is the identifier
22233    indicating which attribute is in use.  The TREE_VALUE represents
22234    the arguments, if any.  */
22235
22236 static tree
22237 cp_parser_gnu_attribute_list (cp_parser* parser)
22238 {
22239   tree attribute_list = NULL_TREE;
22240   bool save_translate_strings_p = parser->translate_strings_p;
22241
22242   parser->translate_strings_p = false;
22243   while (true)
22244     {
22245       cp_token *token;
22246       tree identifier;
22247       tree attribute;
22248
22249       /* Look for the identifier.  We also allow keywords here; for
22250          example `__attribute__ ((const))' is legal.  */
22251       token = cp_lexer_peek_token (parser->lexer);
22252       if (token->type == CPP_NAME
22253           || token->type == CPP_KEYWORD)
22254         {
22255           tree arguments = NULL_TREE;
22256
22257           /* Consume the token, but save it since we need it for the
22258              SIMD enabled function parsing.  */
22259           cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22260
22261           /* Save away the identifier that indicates which attribute
22262              this is.  */
22263           identifier = (token->type == CPP_KEYWORD) 
22264             /* For keywords, use the canonical spelling, not the
22265                parsed identifier.  */
22266             ? ridpointers[(int) token->keyword]
22267             : id_token->u.value;
22268           
22269           attribute = build_tree_list (identifier, NULL_TREE);
22270
22271           /* Peek at the next token.  */
22272           token = cp_lexer_peek_token (parser->lexer);
22273           /* If it's an `(', then parse the attribute arguments.  */
22274           if (token->type == CPP_OPEN_PAREN)
22275             {
22276               vec<tree, va_gc> *vec;
22277               int attr_flag = (attribute_takes_identifier_p (identifier)
22278                                ? id_attr : normal_attr);
22279               if (is_cilkplus_vector_p (identifier))
22280                 {
22281                   cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22282                   continue;
22283                 }
22284               else
22285                 vec = cp_parser_parenthesized_expression_list 
22286                   (parser, attr_flag, /*cast_p=*/false, 
22287                    /*allow_expansion_p=*/false, 
22288                    /*non_constant_p=*/NULL);
22289               if (vec == NULL)
22290                 arguments = error_mark_node;
22291               else
22292                 {
22293                   arguments = build_tree_list_vec (vec);
22294                   release_tree_vector (vec);
22295                 }
22296               /* Save the arguments away.  */
22297               TREE_VALUE (attribute) = arguments;
22298             }
22299           else if (is_cilkplus_vector_p (identifier))
22300             {
22301               cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22302               continue;
22303             }
22304
22305           if (arguments != error_mark_node)
22306             {
22307               /* Add this attribute to the list.  */
22308               TREE_CHAIN (attribute) = attribute_list;
22309               attribute_list = attribute;
22310             }
22311
22312           token = cp_lexer_peek_token (parser->lexer);
22313         }
22314       /* Now, look for more attributes.  If the next token isn't a
22315          `,', we're done.  */
22316       if (token->type != CPP_COMMA)
22317         break;
22318
22319       /* Consume the comma and keep going.  */
22320       cp_lexer_consume_token (parser->lexer);
22321     }
22322   parser->translate_strings_p = save_translate_strings_p;
22323
22324   /* We built up the list in reverse order.  */
22325   return nreverse (attribute_list);
22326 }
22327
22328 /*  Parse a standard C++11 attribute.
22329
22330     The returned representation is a TREE_LIST which TREE_PURPOSE is
22331     the scoped name of the attribute, and the TREE_VALUE is its
22332     arguments list.
22333
22334     Note that the scoped name of the attribute is itself a TREE_LIST
22335     which TREE_PURPOSE is the namespace of the attribute, and
22336     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
22337     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22338     and which TREE_PURPOSE is directly the attribute name.
22339
22340     Clients of the attribute code should use get_attribute_namespace
22341     and get_attribute_name to get the actual namespace and name of
22342     attributes, regardless of their being GNU or C++11 attributes.
22343
22344     attribute:
22345       attribute-token attribute-argument-clause [opt]
22346
22347     attribute-token:
22348       identifier
22349       attribute-scoped-token
22350
22351     attribute-scoped-token:
22352       attribute-namespace :: identifier
22353
22354     attribute-namespace:
22355       identifier
22356
22357     attribute-argument-clause:
22358       ( balanced-token-seq )
22359
22360     balanced-token-seq:
22361       balanced-token [opt]
22362       balanced-token-seq balanced-token
22363
22364     balanced-token:
22365       ( balanced-token-seq )
22366       [ balanced-token-seq ]
22367       { balanced-token-seq }.  */
22368
22369 static tree
22370 cp_parser_std_attribute (cp_parser *parser)
22371 {
22372   tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22373   cp_token *token;
22374
22375   /* First, parse name of the the attribute, a.k.a
22376      attribute-token.  */
22377
22378   token = cp_lexer_peek_token (parser->lexer);
22379   if (token->type == CPP_NAME)
22380     attr_id = token->u.value;
22381   else if (token->type == CPP_KEYWORD)
22382     attr_id = ridpointers[(int) token->keyword];
22383   else if (token->flags & NAMED_OP)
22384     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22385
22386   if (attr_id == NULL_TREE)
22387     return NULL_TREE;
22388
22389   cp_lexer_consume_token (parser->lexer);
22390
22391   token = cp_lexer_peek_token (parser->lexer);
22392   if (token->type == CPP_SCOPE)
22393     {
22394       /* We are seeing a scoped attribute token.  */
22395
22396       cp_lexer_consume_token (parser->lexer);
22397       attr_ns = attr_id;
22398
22399       token = cp_lexer_consume_token (parser->lexer);
22400       if (token->type == CPP_NAME)
22401         attr_id = token->u.value;
22402       else if (token->type == CPP_KEYWORD)
22403         attr_id = ridpointers[(int) token->keyword];
22404       else
22405         {
22406           error_at (token->location,
22407                     "expected an identifier for the attribute name");
22408           return error_mark_node;
22409         }
22410       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22411                                    NULL_TREE);
22412       token = cp_lexer_peek_token (parser->lexer);
22413     }
22414   else
22415     {
22416       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22417                                    NULL_TREE);
22418       /* C++11 noreturn attribute is equivalent to GNU's.  */
22419       if (is_attribute_p ("noreturn", attr_id))
22420         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22421       /* C++14 deprecated attribute is equivalent to GNU's.  */
22422       else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22423         {
22424           if (cxx_dialect == cxx11)
22425             pedwarn (token->location, OPT_Wpedantic,
22426                      "%<deprecated%> is a C++14 feature;"
22427                      " use %<gnu::deprecated%>");
22428           TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22429         }
22430     }
22431
22432   /* Now parse the optional argument clause of the attribute.  */
22433
22434   if (token->type != CPP_OPEN_PAREN)
22435     return attribute;
22436
22437   {
22438     vec<tree, va_gc> *vec;
22439     int attr_flag = normal_attr;
22440
22441     if (attr_ns == get_identifier ("gnu")
22442         && attribute_takes_identifier_p (attr_id))
22443       /* A GNU attribute that takes an identifier in parameter.  */
22444       attr_flag = id_attr;
22445
22446     vec = cp_parser_parenthesized_expression_list
22447       (parser, attr_flag, /*cast_p=*/false,
22448        /*allow_expansion_p=*/true,
22449        /*non_constant_p=*/NULL);
22450     if (vec == NULL)
22451       arguments = error_mark_node;
22452     else
22453       {
22454         arguments = build_tree_list_vec (vec);
22455         release_tree_vector (vec);
22456       }
22457
22458     if (arguments == error_mark_node)
22459       attribute = error_mark_node;
22460     else
22461       TREE_VALUE (attribute) = arguments;
22462   }
22463
22464   return attribute;
22465 }
22466
22467 /* Parse a list of standard C++-11 attributes.
22468
22469    attribute-list:
22470      attribute [opt]
22471      attribute-list , attribute[opt]
22472      attribute ...
22473      attribute-list , attribute ...
22474 */
22475
22476 static tree
22477 cp_parser_std_attribute_list (cp_parser *parser)
22478 {
22479   tree attributes = NULL_TREE, attribute = NULL_TREE;
22480   cp_token *token = NULL;
22481
22482   while (true)
22483     {
22484       attribute = cp_parser_std_attribute (parser);
22485       if (attribute == error_mark_node)
22486         break;
22487       if (attribute != NULL_TREE)
22488         {
22489           TREE_CHAIN (attribute) = attributes;
22490           attributes = attribute;
22491         }
22492       token = cp_lexer_peek_token (parser->lexer);
22493       if (token->type != CPP_COMMA)
22494         break;
22495       cp_lexer_consume_token (parser->lexer);
22496     }
22497   attributes = nreverse (attributes);
22498   return attributes;
22499 }
22500
22501 /* Parse a standard C++-11 attribute specifier.
22502
22503    attribute-specifier:
22504      [ [ attribute-list ] ]
22505      alignment-specifier
22506
22507    alignment-specifier:
22508      alignas ( type-id ... [opt] )
22509      alignas ( alignment-expression ... [opt] ).  */
22510
22511 static tree
22512 cp_parser_std_attribute_spec (cp_parser *parser)
22513 {
22514   tree attributes = NULL_TREE;
22515   cp_token *token = cp_lexer_peek_token (parser->lexer);
22516
22517   if (token->type == CPP_OPEN_SQUARE
22518       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22519     {
22520       cp_lexer_consume_token (parser->lexer);
22521       cp_lexer_consume_token (parser->lexer);
22522
22523       attributes = cp_parser_std_attribute_list (parser);
22524
22525       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22526           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22527         cp_parser_skip_to_end_of_statement (parser);
22528       else
22529         /* Warn about parsing c++11 attribute in non-c++1 mode, only
22530            when we are sure that we have actually parsed them.  */
22531         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22532     }
22533   else
22534     {
22535       tree alignas_expr;
22536
22537       /* Look for an alignment-specifier.  */
22538
22539       token = cp_lexer_peek_token (parser->lexer);
22540
22541       if (token->type != CPP_KEYWORD
22542           || token->keyword != RID_ALIGNAS)
22543         return NULL_TREE;
22544
22545       cp_lexer_consume_token (parser->lexer);
22546       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22547
22548       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22549         {
22550           cp_parser_error (parser, "expected %<(%>");
22551           return error_mark_node;
22552         }
22553
22554       cp_parser_parse_tentatively (parser);
22555       alignas_expr = cp_parser_type_id (parser);
22556
22557       if (!cp_parser_parse_definitely (parser))
22558         {
22559           gcc_assert (alignas_expr == error_mark_node
22560                       || alignas_expr == NULL_TREE);
22561
22562           alignas_expr =
22563             cp_parser_assignment_expression (parser);
22564           if (alignas_expr == error_mark_node)
22565             cp_parser_skip_to_end_of_statement (parser);
22566           if (alignas_expr == NULL_TREE
22567               || alignas_expr == error_mark_node)
22568             return alignas_expr;
22569         }
22570
22571       if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22572         {
22573           cp_parser_error (parser, "expected %<)%>");
22574           return error_mark_node;
22575         }
22576
22577       alignas_expr = cxx_alignas_expr (alignas_expr);
22578
22579       /* Build the C++-11 representation of an 'aligned'
22580          attribute.  */
22581       attributes =
22582         build_tree_list (build_tree_list (get_identifier ("gnu"),
22583                                           get_identifier ("aligned")),
22584                          build_tree_list (NULL_TREE, alignas_expr));
22585     }
22586
22587   return attributes;
22588 }
22589
22590 /* Parse a standard C++-11 attribute-specifier-seq.
22591
22592    attribute-specifier-seq:
22593      attribute-specifier-seq [opt] attribute-specifier
22594  */
22595
22596 static tree
22597 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22598 {
22599   tree attr_specs = NULL;
22600
22601   while (true)
22602     {
22603       tree attr_spec = cp_parser_std_attribute_spec (parser);
22604       if (attr_spec == NULL_TREE)
22605         break;
22606       if (attr_spec == error_mark_node)
22607         return error_mark_node;
22608
22609       TREE_CHAIN (attr_spec) = attr_specs;
22610       attr_specs = attr_spec;
22611     }
22612
22613   attr_specs = nreverse (attr_specs);
22614   return attr_specs;
22615 }
22616
22617 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
22618    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
22619    current value of the PEDANTIC flag, regardless of whether or not
22620    the `__extension__' keyword is present.  The caller is responsible
22621    for restoring the value of the PEDANTIC flag.  */
22622
22623 static bool
22624 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22625 {
22626   /* Save the old value of the PEDANTIC flag.  */
22627   *saved_pedantic = pedantic;
22628
22629   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22630     {
22631       /* Consume the `__extension__' token.  */
22632       cp_lexer_consume_token (parser->lexer);
22633       /* We're not being pedantic while the `__extension__' keyword is
22634          in effect.  */
22635       pedantic = 0;
22636
22637       return true;
22638     }
22639
22640   return false;
22641 }
22642
22643 /* Parse a label declaration.
22644
22645    label-declaration:
22646      __label__ label-declarator-seq ;
22647
22648    label-declarator-seq:
22649      identifier , label-declarator-seq
22650      identifier  */
22651
22652 static void
22653 cp_parser_label_declaration (cp_parser* parser)
22654 {
22655   /* Look for the `__label__' keyword.  */
22656   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22657
22658   while (true)
22659     {
22660       tree identifier;
22661
22662       /* Look for an identifier.  */
22663       identifier = cp_parser_identifier (parser);
22664       /* If we failed, stop.  */
22665       if (identifier == error_mark_node)
22666         break;
22667       /* Declare it as a label.  */
22668       finish_label_decl (identifier);
22669       /* If the next token is a `;', stop.  */
22670       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22671         break;
22672       /* Look for the `,' separating the label declarations.  */
22673       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22674     }
22675
22676   /* Look for the final `;'.  */
22677   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22678 }
22679
22680 /* Support Functions */
22681
22682 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22683    NAME should have one of the representations used for an
22684    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22685    is returned.  If PARSER->SCOPE is a dependent type, then a
22686    SCOPE_REF is returned.
22687
22688    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22689    returned; the name was already resolved when the TEMPLATE_ID_EXPR
22690    was formed.  Abstractly, such entities should not be passed to this
22691    function, because they do not need to be looked up, but it is
22692    simpler to check for this special case here, rather than at the
22693    call-sites.
22694
22695    In cases not explicitly covered above, this function returns a
22696    DECL, OVERLOAD, or baselink representing the result of the lookup.
22697    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22698    is returned.
22699
22700    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22701    (e.g., "struct") that was used.  In that case bindings that do not
22702    refer to types are ignored.
22703
22704    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22705    ignored.
22706
22707    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22708    are ignored.
22709
22710    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22711    types.
22712
22713    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22714    TREE_LIST of candidates if name-lookup results in an ambiguity, and
22715    NULL_TREE otherwise.  */
22716
22717 static tree
22718 cp_parser_lookup_name (cp_parser *parser, tree name,
22719                        enum tag_types tag_type,
22720                        bool is_template,
22721                        bool is_namespace,
22722                        bool check_dependency,
22723                        tree *ambiguous_decls,
22724                        location_t name_location)
22725 {
22726   tree decl;
22727   tree object_type = parser->context->object_type;
22728
22729   /* Assume that the lookup will be unambiguous.  */
22730   if (ambiguous_decls)
22731     *ambiguous_decls = NULL_TREE;
22732
22733   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22734      no longer valid.  Note that if we are parsing tentatively, and
22735      the parse fails, OBJECT_TYPE will be automatically restored.  */
22736   parser->context->object_type = NULL_TREE;
22737
22738   if (name == error_mark_node)
22739     return error_mark_node;
22740
22741   /* A template-id has already been resolved; there is no lookup to
22742      do.  */
22743   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22744     return name;
22745   if (BASELINK_P (name))
22746     {
22747       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22748                   == TEMPLATE_ID_EXPR);
22749       return name;
22750     }
22751
22752   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
22753      it should already have been checked to make sure that the name
22754      used matches the type being destroyed.  */
22755   if (TREE_CODE (name) == BIT_NOT_EXPR)
22756     {
22757       tree type;
22758
22759       /* Figure out to which type this destructor applies.  */
22760       if (parser->scope)
22761         type = parser->scope;
22762       else if (object_type)
22763         type = object_type;
22764       else
22765         type = current_class_type;
22766       /* If that's not a class type, there is no destructor.  */
22767       if (!type || !CLASS_TYPE_P (type))
22768         return error_mark_node;
22769       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22770         lazily_declare_fn (sfk_destructor, type);
22771       if (!CLASSTYPE_DESTRUCTORS (type))
22772           return error_mark_node;
22773       /* If it was a class type, return the destructor.  */
22774       return CLASSTYPE_DESTRUCTORS (type);
22775     }
22776
22777   /* By this point, the NAME should be an ordinary identifier.  If
22778      the id-expression was a qualified name, the qualifying scope is
22779      stored in PARSER->SCOPE at this point.  */
22780   gcc_assert (identifier_p (name));
22781
22782   /* Perform the lookup.  */
22783   if (parser->scope)
22784     {
22785       bool dependent_p;
22786
22787       if (parser->scope == error_mark_node)
22788         return error_mark_node;
22789
22790       /* If the SCOPE is dependent, the lookup must be deferred until
22791          the template is instantiated -- unless we are explicitly
22792          looking up names in uninstantiated templates.  Even then, we
22793          cannot look up the name if the scope is not a class type; it
22794          might, for example, be a template type parameter.  */
22795       dependent_p = (TYPE_P (parser->scope)
22796                      && dependent_scope_p (parser->scope));
22797       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22798           && dependent_p)
22799         /* Defer lookup.  */
22800         decl = error_mark_node;
22801       else
22802         {
22803           tree pushed_scope = NULL_TREE;
22804
22805           /* If PARSER->SCOPE is a dependent type, then it must be a
22806              class type, and we must not be checking dependencies;
22807              otherwise, we would have processed this lookup above.  So
22808              that PARSER->SCOPE is not considered a dependent base by
22809              lookup_member, we must enter the scope here.  */
22810           if (dependent_p)
22811             pushed_scope = push_scope (parser->scope);
22812
22813           /* If the PARSER->SCOPE is a template specialization, it
22814              may be instantiated during name lookup.  In that case,
22815              errors may be issued.  Even if we rollback the current
22816              tentative parse, those errors are valid.  */
22817           decl = lookup_qualified_name (parser->scope, name,
22818                                         tag_type != none_type,
22819                                         /*complain=*/true);
22820
22821           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22822              lookup result and the nested-name-specifier nominates a class C:
22823                * if the name specified after the nested-name-specifier, when
22824                looked up in C, is the injected-class-name of C (Clause 9), or
22825                * if the name specified after the nested-name-specifier is the
22826                same as the identifier or the simple-template-id's template-
22827                name in the last component of the nested-name-specifier,
22828              the name is instead considered to name the constructor of
22829              class C. [ Note: for example, the constructor is not an
22830              acceptable lookup result in an elaborated-type-specifier so
22831              the constructor would not be used in place of the
22832              injected-class-name. --end note ] Such a constructor name
22833              shall be used only in the declarator-id of a declaration that
22834              names a constructor or in a using-declaration.  */
22835           if (tag_type == none_type
22836               && DECL_SELF_REFERENCE_P (decl)
22837               && same_type_p (DECL_CONTEXT (decl), parser->scope))
22838             decl = lookup_qualified_name (parser->scope, ctor_identifier,
22839                                           tag_type != none_type,
22840                                           /*complain=*/true);
22841
22842           /* If we have a single function from a using decl, pull it out.  */
22843           if (TREE_CODE (decl) == OVERLOAD
22844               && !really_overloaded_fn (decl))
22845             decl = OVL_FUNCTION (decl);
22846
22847           if (pushed_scope)
22848             pop_scope (pushed_scope);
22849         }
22850
22851       /* If the scope is a dependent type and either we deferred lookup or
22852          we did lookup but didn't find the name, rememeber the name.  */
22853       if (decl == error_mark_node && TYPE_P (parser->scope)
22854           && dependent_type_p (parser->scope))
22855         {
22856           if (tag_type)
22857             {
22858               tree type;
22859
22860               /* The resolution to Core Issue 180 says that `struct
22861                  A::B' should be considered a type-name, even if `A'
22862                  is dependent.  */
22863               type = make_typename_type (parser->scope, name, tag_type,
22864                                          /*complain=*/tf_error);
22865               if (type != error_mark_node)
22866                 decl = TYPE_NAME (type);
22867             }
22868           else if (is_template
22869                    && (cp_parser_next_token_ends_template_argument_p (parser)
22870                        || cp_lexer_next_token_is (parser->lexer,
22871                                                   CPP_CLOSE_PAREN)))
22872             decl = make_unbound_class_template (parser->scope,
22873                                                 name, NULL_TREE,
22874                                                 /*complain=*/tf_error);
22875           else
22876             decl = build_qualified_name (/*type=*/NULL_TREE,
22877                                          parser->scope, name,
22878                                          is_template);
22879         }
22880       parser->qualifying_scope = parser->scope;
22881       parser->object_scope = NULL_TREE;
22882     }
22883   else if (object_type)
22884     {
22885       /* Look up the name in the scope of the OBJECT_TYPE, unless the
22886          OBJECT_TYPE is not a class.  */
22887       if (CLASS_TYPE_P (object_type))
22888         /* If the OBJECT_TYPE is a template specialization, it may
22889            be instantiated during name lookup.  In that case, errors
22890            may be issued.  Even if we rollback the current tentative
22891            parse, those errors are valid.  */
22892         decl = lookup_member (object_type,
22893                               name,
22894                               /*protect=*/0,
22895                               tag_type != none_type,
22896                               tf_warning_or_error);
22897       else
22898         decl = NULL_TREE;
22899
22900       if (!decl)
22901         /* Look it up in the enclosing context.  */
22902         decl = lookup_name_real (name, tag_type != none_type,
22903                                  /*nonclass=*/0,
22904                                  /*block_p=*/true, is_namespace, 0);
22905       parser->object_scope = object_type;
22906       parser->qualifying_scope = NULL_TREE;
22907     }
22908   else
22909     {
22910       decl = lookup_name_real (name, tag_type != none_type,
22911                                /*nonclass=*/0,
22912                                /*block_p=*/true, is_namespace, 0);
22913       parser->qualifying_scope = NULL_TREE;
22914       parser->object_scope = NULL_TREE;
22915     }
22916
22917   /* If the lookup failed, let our caller know.  */
22918   if (!decl || decl == error_mark_node)
22919     return error_mark_node;
22920
22921   /* Pull out the template from an injected-class-name (or multiple).  */
22922   if (is_template)
22923     decl = maybe_get_template_decl_from_type_decl (decl);
22924
22925   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
22926   if (TREE_CODE (decl) == TREE_LIST)
22927     {
22928       if (ambiguous_decls)
22929         *ambiguous_decls = decl;
22930       /* The error message we have to print is too complicated for
22931          cp_parser_error, so we incorporate its actions directly.  */
22932       if (!cp_parser_simulate_error (parser))
22933         {
22934           error_at (name_location, "reference to %qD is ambiguous",
22935                     name);
22936           print_candidates (decl);
22937         }
22938       return error_mark_node;
22939     }
22940
22941   gcc_assert (DECL_P (decl)
22942               || TREE_CODE (decl) == OVERLOAD
22943               || TREE_CODE (decl) == SCOPE_REF
22944               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22945               || BASELINK_P (decl));
22946
22947   /* If we have resolved the name of a member declaration, check to
22948      see if the declaration is accessible.  When the name resolves to
22949      set of overloaded functions, accessibility is checked when
22950      overload resolution is done.
22951
22952      During an explicit instantiation, access is not checked at all,
22953      as per [temp.explicit].  */
22954   if (DECL_P (decl))
22955     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22956
22957   maybe_record_typedef_use (decl);
22958
22959   return decl;
22960 }
22961
22962 /* Like cp_parser_lookup_name, but for use in the typical case where
22963    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22964    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
22965
22966 static tree
22967 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22968 {
22969   return cp_parser_lookup_name (parser, name,
22970                                 none_type,
22971                                 /*is_template=*/false,
22972                                 /*is_namespace=*/false,
22973                                 /*check_dependency=*/true,
22974                                 /*ambiguous_decls=*/NULL,
22975                                 location);
22976 }
22977
22978 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22979    the current context, return the TYPE_DECL.  If TAG_NAME_P is
22980    true, the DECL indicates the class being defined in a class-head,
22981    or declared in an elaborated-type-specifier.
22982
22983    Otherwise, return DECL.  */
22984
22985 static tree
22986 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22987 {
22988   /* If the TEMPLATE_DECL is being declared as part of a class-head,
22989      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22990
22991        struct A {
22992          template <typename T> struct B;
22993        };
22994
22995        template <typename T> struct A::B {};
22996
22997      Similarly, in an elaborated-type-specifier:
22998
22999        namespace N { struct X{}; }
23000
23001        struct A {
23002          template <typename T> friend struct N::X;
23003        };
23004
23005      However, if the DECL refers to a class type, and we are in
23006      the scope of the class, then the name lookup automatically
23007      finds the TYPE_DECL created by build_self_reference rather
23008      than a TEMPLATE_DECL.  For example, in:
23009
23010        template <class T> struct S {
23011          S s;
23012        };
23013
23014      there is no need to handle such case.  */
23015
23016   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23017     return DECL_TEMPLATE_RESULT (decl);
23018
23019   return decl;
23020 }
23021
23022 /* If too many, or too few, template-parameter lists apply to the
23023    declarator, issue an error message.  Returns TRUE if all went well,
23024    and FALSE otherwise.  */
23025
23026 static bool
23027 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23028                                                 cp_declarator *declarator,
23029                                                 location_t declarator_location)
23030 {
23031   switch (declarator->kind)
23032     {
23033     case cdk_id:
23034       {
23035         unsigned num_templates = 0;
23036         tree scope = declarator->u.id.qualifying_scope;
23037
23038         if (scope)
23039           num_templates = num_template_headers_for_class (scope);
23040         else if (TREE_CODE (declarator->u.id.unqualified_name)
23041                  == TEMPLATE_ID_EXPR)
23042           /* If the DECLARATOR has the form `X<y>' then it uses one
23043              additional level of template parameters.  */
23044           ++num_templates;
23045
23046         return cp_parser_check_template_parameters 
23047           (parser, num_templates, declarator_location, declarator);
23048       }
23049
23050     case cdk_function:
23051     case cdk_array:
23052     case cdk_pointer:
23053     case cdk_reference:
23054     case cdk_ptrmem:
23055       return (cp_parser_check_declarator_template_parameters
23056               (parser, declarator->declarator, declarator_location));
23057
23058     case cdk_error:
23059       return true;
23060
23061     default:
23062       gcc_unreachable ();
23063     }
23064   return false;
23065 }
23066
23067 /* NUM_TEMPLATES were used in the current declaration.  If that is
23068    invalid, return FALSE and issue an error messages.  Otherwise,
23069    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
23070    declarator and we can print more accurate diagnostics.  */
23071
23072 static bool
23073 cp_parser_check_template_parameters (cp_parser* parser,
23074                                      unsigned num_templates,
23075                                      location_t location,
23076                                      cp_declarator *declarator)
23077 {
23078   /* If there are the same number of template classes and parameter
23079      lists, that's OK.  */
23080   if (parser->num_template_parameter_lists == num_templates)
23081     return true;
23082   /* If there are more, but only one more, then we are referring to a
23083      member template.  That's OK too.  */
23084   if (parser->num_template_parameter_lists == num_templates + 1)
23085     return true;
23086   /* If there are more template classes than parameter lists, we have
23087      something like:
23088
23089        template <class T> void S<T>::R<T>::f ();  */
23090   if (parser->num_template_parameter_lists < num_templates)
23091     {
23092       if (declarator && !current_function_decl)
23093         error_at (location, "specializing member %<%T::%E%> "
23094                   "requires %<template<>%> syntax", 
23095                   declarator->u.id.qualifying_scope,
23096                   declarator->u.id.unqualified_name);
23097       else if (declarator)
23098         error_at (location, "invalid declaration of %<%T::%E%>",
23099                   declarator->u.id.qualifying_scope,
23100                   declarator->u.id.unqualified_name);
23101       else 
23102         error_at (location, "too few template-parameter-lists");
23103       return false;
23104     }
23105   /* Otherwise, there are too many template parameter lists.  We have
23106      something like:
23107
23108      template <class T> template <class U> void S::f();  */
23109   error_at (location, "too many template-parameter-lists");
23110   return false;
23111 }
23112
23113 /* Parse an optional `::' token indicating that the following name is
23114    from the global namespace.  If so, PARSER->SCOPE is set to the
23115    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23116    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23117    Returns the new value of PARSER->SCOPE, if the `::' token is
23118    present, and NULL_TREE otherwise.  */
23119
23120 static tree
23121 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23122 {
23123   cp_token *token;
23124
23125   /* Peek at the next token.  */
23126   token = cp_lexer_peek_token (parser->lexer);
23127   /* If we're looking at a `::' token then we're starting from the
23128      global namespace, not our current location.  */
23129   if (token->type == CPP_SCOPE)
23130     {
23131       /* Consume the `::' token.  */
23132       cp_lexer_consume_token (parser->lexer);
23133       /* Set the SCOPE so that we know where to start the lookup.  */
23134       parser->scope = global_namespace;
23135       parser->qualifying_scope = global_namespace;
23136       parser->object_scope = NULL_TREE;
23137
23138       return parser->scope;
23139     }
23140   else if (!current_scope_valid_p)
23141     {
23142       parser->scope = NULL_TREE;
23143       parser->qualifying_scope = NULL_TREE;
23144       parser->object_scope = NULL_TREE;
23145     }
23146
23147   return NULL_TREE;
23148 }
23149
23150 /* Returns TRUE if the upcoming token sequence is the start of a
23151    constructor declarator.  If FRIEND_P is true, the declarator is
23152    preceded by the `friend' specifier.  */
23153
23154 static bool
23155 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23156 {
23157   bool constructor_p;
23158   bool outside_class_specifier_p;
23159   tree nested_name_specifier;
23160   cp_token *next_token;
23161
23162   /* The common case is that this is not a constructor declarator, so
23163      try to avoid doing lots of work if at all possible.  It's not
23164      valid declare a constructor at function scope.  */
23165   if (parser->in_function_body)
23166     return false;
23167   /* And only certain tokens can begin a constructor declarator.  */
23168   next_token = cp_lexer_peek_token (parser->lexer);
23169   if (next_token->type != CPP_NAME
23170       && next_token->type != CPP_SCOPE
23171       && next_token->type != CPP_NESTED_NAME_SPECIFIER
23172       && next_token->type != CPP_TEMPLATE_ID)
23173     return false;
23174
23175   /* Parse tentatively; we are going to roll back all of the tokens
23176      consumed here.  */
23177   cp_parser_parse_tentatively (parser);
23178   /* Assume that we are looking at a constructor declarator.  */
23179   constructor_p = true;
23180
23181   /* Look for the optional `::' operator.  */
23182   cp_parser_global_scope_opt (parser,
23183                               /*current_scope_valid_p=*/false);
23184   /* Look for the nested-name-specifier.  */
23185   nested_name_specifier
23186     = (cp_parser_nested_name_specifier_opt (parser,
23187                                             /*typename_keyword_p=*/false,
23188                                             /*check_dependency_p=*/false,
23189                                             /*type_p=*/false,
23190                                             /*is_declaration=*/false));
23191
23192   outside_class_specifier_p = (!at_class_scope_p ()
23193                                || !TYPE_BEING_DEFINED (current_class_type)
23194                                || friend_p);
23195
23196   /* Outside of a class-specifier, there must be a
23197      nested-name-specifier.  */
23198   if (!nested_name_specifier && outside_class_specifier_p)
23199     constructor_p = false;
23200   else if (nested_name_specifier == error_mark_node)
23201     constructor_p = false;
23202
23203   /* If we have a class scope, this is easy; DR 147 says that S::S always
23204      names the constructor, and no other qualified name could.  */
23205   if (constructor_p && nested_name_specifier
23206       && CLASS_TYPE_P (nested_name_specifier))
23207     {
23208       tree id = cp_parser_unqualified_id (parser,
23209                                           /*template_keyword_p=*/false,
23210                                           /*check_dependency_p=*/false,
23211                                           /*declarator_p=*/true,
23212                                           /*optional_p=*/false);
23213       if (is_overloaded_fn (id))
23214         id = DECL_NAME (get_first_fn (id));
23215       if (!constructor_name_p (id, nested_name_specifier))
23216         constructor_p = false;
23217     }
23218   /* If we still think that this might be a constructor-declarator,
23219      look for a class-name.  */
23220   else if (constructor_p)
23221     {
23222       /* If we have:
23223
23224            template <typename T> struct S {
23225              S();
23226            };
23227
23228          we must recognize that the nested `S' names a class.  */
23229       tree type_decl;
23230       type_decl = cp_parser_class_name (parser,
23231                                         /*typename_keyword_p=*/false,
23232                                         /*template_keyword_p=*/false,
23233                                         none_type,
23234                                         /*check_dependency_p=*/false,
23235                                         /*class_head_p=*/false,
23236                                         /*is_declaration=*/false);
23237       /* If there was no class-name, then this is not a constructor.
23238          Otherwise, if we are in a class-specifier and we aren't
23239          handling a friend declaration, check that its type matches
23240          current_class_type (c++/38313).  Note: error_mark_node
23241          is left alone for error recovery purposes.  */
23242       constructor_p = (!cp_parser_error_occurred (parser)
23243                        && (outside_class_specifier_p
23244                            || type_decl == error_mark_node
23245                            || same_type_p (current_class_type,
23246                                            TREE_TYPE (type_decl))));
23247
23248       /* If we're still considering a constructor, we have to see a `(',
23249          to begin the parameter-declaration-clause, followed by either a
23250          `)', an `...', or a decl-specifier.  We need to check for a
23251          type-specifier to avoid being fooled into thinking that:
23252
23253            S (f) (int);
23254
23255          is a constructor.  (It is actually a function named `f' that
23256          takes one parameter (of type `int') and returns a value of type
23257          `S'.  */
23258       if (constructor_p
23259           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23260         constructor_p = false;
23261
23262       if (constructor_p
23263           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23264           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23265           /* A parameter declaration begins with a decl-specifier,
23266              which is either the "attribute" keyword, a storage class
23267              specifier, or (usually) a type-specifier.  */
23268           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23269         {
23270           tree type;
23271           tree pushed_scope = NULL_TREE;
23272           unsigned saved_num_template_parameter_lists;
23273
23274           /* Names appearing in the type-specifier should be looked up
23275              in the scope of the class.  */
23276           if (current_class_type)
23277             type = NULL_TREE;
23278           else
23279             {
23280               type = TREE_TYPE (type_decl);
23281               if (TREE_CODE (type) == TYPENAME_TYPE)
23282                 {
23283                   type = resolve_typename_type (type,
23284                                                 /*only_current_p=*/false);
23285                   if (TREE_CODE (type) == TYPENAME_TYPE)
23286                     {
23287                       cp_parser_abort_tentative_parse (parser);
23288                       return false;
23289                     }
23290                 }
23291               pushed_scope = push_scope (type);
23292             }
23293
23294           /* Inside the constructor parameter list, surrounding
23295              template-parameter-lists do not apply.  */
23296           saved_num_template_parameter_lists
23297             = parser->num_template_parameter_lists;
23298           parser->num_template_parameter_lists = 0;
23299
23300           /* Look for the type-specifier.  */
23301           cp_parser_type_specifier (parser,
23302                                     CP_PARSER_FLAGS_NONE,
23303                                     /*decl_specs=*/NULL,
23304                                     /*is_declarator=*/true,
23305                                     /*declares_class_or_enum=*/NULL,
23306                                     /*is_cv_qualifier=*/NULL);
23307
23308           parser->num_template_parameter_lists
23309             = saved_num_template_parameter_lists;
23310
23311           /* Leave the scope of the class.  */
23312           if (pushed_scope)
23313             pop_scope (pushed_scope);
23314
23315           constructor_p = !cp_parser_error_occurred (parser);
23316         }
23317     }
23318
23319   /* We did not really want to consume any tokens.  */
23320   cp_parser_abort_tentative_parse (parser);
23321
23322   return constructor_p;
23323 }
23324
23325 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23326    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
23327    they must be performed once we are in the scope of the function.
23328
23329    Returns the function defined.  */
23330
23331 static tree
23332 cp_parser_function_definition_from_specifiers_and_declarator
23333   (cp_parser* parser,
23334    cp_decl_specifier_seq *decl_specifiers,
23335    tree attributes,
23336    const cp_declarator *declarator)
23337 {
23338   tree fn;
23339   bool success_p;
23340
23341   /* Begin the function-definition.  */
23342   success_p = start_function (decl_specifiers, declarator, attributes);
23343
23344   /* The things we're about to see are not directly qualified by any
23345      template headers we've seen thus far.  */
23346   reset_specialization ();
23347
23348   /* If there were names looked up in the decl-specifier-seq that we
23349      did not check, check them now.  We must wait until we are in the
23350      scope of the function to perform the checks, since the function
23351      might be a friend.  */
23352   perform_deferred_access_checks (tf_warning_or_error);
23353
23354   if (success_p)
23355     {
23356       cp_finalize_omp_declare_simd (parser, current_function_decl);
23357       parser->omp_declare_simd = NULL;
23358     }
23359
23360   if (!success_p)
23361     {
23362       /* Skip the entire function.  */
23363       cp_parser_skip_to_end_of_block_or_statement (parser);
23364       fn = error_mark_node;
23365     }
23366   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23367     {
23368       /* Seen already, skip it.  An error message has already been output.  */
23369       cp_parser_skip_to_end_of_block_or_statement (parser);
23370       fn = current_function_decl;
23371       current_function_decl = NULL_TREE;
23372       /* If this is a function from a class, pop the nested class.  */
23373       if (current_class_name)
23374         pop_nested_class ();
23375     }
23376   else
23377     {
23378       timevar_id_t tv;
23379       if (DECL_DECLARED_INLINE_P (current_function_decl))
23380         tv = TV_PARSE_INLINE;
23381       else
23382         tv = TV_PARSE_FUNC;
23383       timevar_push (tv);
23384       fn = cp_parser_function_definition_after_declarator (parser,
23385                                                          /*inline_p=*/false);
23386       timevar_pop (tv);
23387     }
23388
23389   return fn;
23390 }
23391
23392 /* Parse the part of a function-definition that follows the
23393    declarator.  INLINE_P is TRUE iff this function is an inline
23394    function defined within a class-specifier.
23395
23396    Returns the function defined.  */
23397
23398 static tree
23399 cp_parser_function_definition_after_declarator (cp_parser* parser,
23400                                                 bool inline_p)
23401 {
23402   tree fn;
23403   bool ctor_initializer_p = false;
23404   bool saved_in_unbraced_linkage_specification_p;
23405   bool saved_in_function_body;
23406   unsigned saved_num_template_parameter_lists;
23407   cp_token *token;
23408   bool fully_implicit_function_template_p
23409     = parser->fully_implicit_function_template_p;
23410   parser->fully_implicit_function_template_p = false;
23411   tree implicit_template_parms
23412     = parser->implicit_template_parms;
23413   parser->implicit_template_parms = 0;
23414   cp_binding_level* implicit_template_scope
23415     = parser->implicit_template_scope;
23416   parser->implicit_template_scope = 0;
23417
23418   saved_in_function_body = parser->in_function_body;
23419   parser->in_function_body = true;
23420   /* If the next token is `return', then the code may be trying to
23421      make use of the "named return value" extension that G++ used to
23422      support.  */
23423   token = cp_lexer_peek_token (parser->lexer);
23424   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23425     {
23426       /* Consume the `return' keyword.  */
23427       cp_lexer_consume_token (parser->lexer);
23428       /* Look for the identifier that indicates what value is to be
23429          returned.  */
23430       cp_parser_identifier (parser);
23431       /* Issue an error message.  */
23432       error_at (token->location,
23433                 "named return values are no longer supported");
23434       /* Skip tokens until we reach the start of the function body.  */
23435       while (true)
23436         {
23437           cp_token *token = cp_lexer_peek_token (parser->lexer);
23438           if (token->type == CPP_OPEN_BRACE
23439               || token->type == CPP_EOF
23440               || token->type == CPP_PRAGMA_EOL)
23441             break;
23442           cp_lexer_consume_token (parser->lexer);
23443         }
23444     }
23445   /* The `extern' in `extern "C" void f () { ... }' does not apply to
23446      anything declared inside `f'.  */
23447   saved_in_unbraced_linkage_specification_p
23448     = parser->in_unbraced_linkage_specification_p;
23449   parser->in_unbraced_linkage_specification_p = false;
23450   /* Inside the function, surrounding template-parameter-lists do not
23451      apply.  */
23452   saved_num_template_parameter_lists
23453     = parser->num_template_parameter_lists;
23454   parser->num_template_parameter_lists = 0;
23455
23456   start_lambda_scope (current_function_decl);
23457
23458   /* If the next token is `try', `__transaction_atomic', or
23459      `__transaction_relaxed`, then we are looking at either function-try-block
23460      or function-transaction-block.  Note that all of these include the
23461      function-body.  */
23462   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23463     ctor_initializer_p = cp_parser_function_transaction (parser,
23464         RID_TRANSACTION_ATOMIC);
23465   else if (cp_lexer_next_token_is_keyword (parser->lexer,
23466       RID_TRANSACTION_RELAXED))
23467     ctor_initializer_p = cp_parser_function_transaction (parser,
23468         RID_TRANSACTION_RELAXED);
23469   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23470     ctor_initializer_p = cp_parser_function_try_block (parser);
23471   else
23472     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23473       (parser, /*in_function_try_block=*/false);
23474
23475   finish_lambda_scope ();
23476
23477   /* Finish the function.  */
23478   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23479                         (inline_p ? 2 : 0));
23480   /* Generate code for it, if necessary.  */
23481   expand_or_defer_fn (fn);
23482   /* Restore the saved values.  */
23483   parser->in_unbraced_linkage_specification_p
23484     = saved_in_unbraced_linkage_specification_p;
23485   parser->num_template_parameter_lists
23486     = saved_num_template_parameter_lists;
23487   parser->in_function_body = saved_in_function_body;
23488
23489   parser->fully_implicit_function_template_p
23490     = fully_implicit_function_template_p;
23491   parser->implicit_template_parms
23492     = implicit_template_parms;
23493   parser->implicit_template_scope
23494     = implicit_template_scope;
23495
23496   if (parser->fully_implicit_function_template_p)
23497     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23498
23499   return fn;
23500 }
23501
23502 /* Parse a template-declaration, assuming that the `export' (and
23503    `extern') keywords, if present, has already been scanned.  MEMBER_P
23504    is as for cp_parser_template_declaration.  */
23505
23506 static void
23507 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23508 {
23509   tree decl = NULL_TREE;
23510   vec<deferred_access_check, va_gc> *checks;
23511   tree parameter_list;
23512   bool friend_p = false;
23513   bool need_lang_pop;
23514   cp_token *token;
23515
23516   /* Look for the `template' keyword.  */
23517   token = cp_lexer_peek_token (parser->lexer);
23518   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23519     return;
23520
23521   /* And the `<'.  */
23522   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23523     return;
23524   if (at_class_scope_p () && current_function_decl)
23525     {
23526       /* 14.5.2.2 [temp.mem]
23527
23528          A local class shall not have member templates.  */
23529       error_at (token->location,
23530                 "invalid declaration of member template in local class");
23531       cp_parser_skip_to_end_of_block_or_statement (parser);
23532       return;
23533     }
23534   /* [temp]
23535
23536      A template ... shall not have C linkage.  */
23537   if (current_lang_name == lang_name_c)
23538     {
23539       error_at (token->location, "template with C linkage");
23540       /* Give it C++ linkage to avoid confusing other parts of the
23541          front end.  */
23542       push_lang_context (lang_name_cplusplus);
23543       need_lang_pop = true;
23544     }
23545   else
23546     need_lang_pop = false;
23547
23548   /* We cannot perform access checks on the template parameter
23549      declarations until we know what is being declared, just as we
23550      cannot check the decl-specifier list.  */
23551   push_deferring_access_checks (dk_deferred);
23552
23553   /* If the next token is `>', then we have an invalid
23554      specialization.  Rather than complain about an invalid template
23555      parameter, issue an error message here.  */
23556   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23557     {
23558       cp_parser_error (parser, "invalid explicit specialization");
23559       begin_specialization ();
23560       parameter_list = NULL_TREE;
23561     }
23562   else
23563     {
23564       /* Parse the template parameters.  */
23565       parameter_list = cp_parser_template_parameter_list (parser);
23566     }
23567
23568   /* Get the deferred access checks from the parameter list.  These
23569      will be checked once we know what is being declared, as for a
23570      member template the checks must be performed in the scope of the
23571      class containing the member.  */
23572   checks = get_deferred_access_checks ();
23573
23574   /* Look for the `>'.  */
23575   cp_parser_skip_to_end_of_template_parameter_list (parser);
23576   /* We just processed one more parameter list.  */
23577   ++parser->num_template_parameter_lists;
23578   /* If the next token is `template', there are more template
23579      parameters.  */
23580   if (cp_lexer_next_token_is_keyword (parser->lexer,
23581                                       RID_TEMPLATE))
23582     cp_parser_template_declaration_after_export (parser, member_p);
23583   else if (cxx_dialect >= cxx11
23584            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23585     decl = cp_parser_alias_declaration (parser);
23586   else
23587     {
23588       /* There are no access checks when parsing a template, as we do not
23589          know if a specialization will be a friend.  */
23590       push_deferring_access_checks (dk_no_check);
23591       token = cp_lexer_peek_token (parser->lexer);
23592       decl = cp_parser_single_declaration (parser,
23593                                            checks,
23594                                            member_p,
23595                                            /*explicit_specialization_p=*/false,
23596                                            &friend_p);
23597       pop_deferring_access_checks ();
23598
23599       /* If this is a member template declaration, let the front
23600          end know.  */
23601       if (member_p && !friend_p && decl)
23602         {
23603           if (TREE_CODE (decl) == TYPE_DECL)
23604             cp_parser_check_access_in_redeclaration (decl, token->location);
23605
23606           decl = finish_member_template_decl (decl);
23607         }
23608       else if (friend_p && decl
23609                && DECL_DECLARES_TYPE_P (decl))
23610         make_friend_class (current_class_type, TREE_TYPE (decl),
23611                            /*complain=*/true);
23612     }
23613   /* We are done with the current parameter list.  */
23614   --parser->num_template_parameter_lists;
23615
23616   pop_deferring_access_checks ();
23617
23618   /* Finish up.  */
23619   finish_template_decl (parameter_list);
23620
23621   /* Check the template arguments for a literal operator template.  */
23622   if (decl
23623       && DECL_DECLARES_FUNCTION_P (decl)
23624       && UDLIT_OPER_P (DECL_NAME (decl)))
23625     {
23626       bool ok = true;
23627       if (parameter_list == NULL_TREE)
23628         ok = false;
23629       else
23630         {
23631           int num_parms = TREE_VEC_LENGTH (parameter_list);
23632           if (num_parms == 1)
23633             {
23634               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23635               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23636               if (TREE_TYPE (parm) != char_type_node
23637                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23638                 ok = false;
23639             }
23640           else if (num_parms == 2 && cxx_dialect >= cxx14)
23641             {
23642               tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23643               tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23644               tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23645               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23646               if (TREE_TYPE (parm) != TREE_TYPE (type)
23647                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23648                 ok = false;
23649             }
23650           else
23651             ok = false;
23652         }
23653       if (!ok)
23654         {
23655           if (cxx_dialect >= cxx14)
23656             error ("literal operator template %qD has invalid parameter list."
23657                    "  Expected non-type template argument pack <char...>"
23658                    " or <typename CharT, CharT...>",
23659                    decl);
23660           else
23661             error ("literal operator template %qD has invalid parameter list."
23662                    "  Expected non-type template argument pack <char...>",
23663                    decl);
23664         }
23665     }
23666   /* Register member declarations.  */
23667   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23668     finish_member_declaration (decl);
23669   /* For the erroneous case of a template with C linkage, we pushed an
23670      implicit C++ linkage scope; exit that scope now.  */
23671   if (need_lang_pop)
23672     pop_lang_context ();
23673   /* If DECL is a function template, we must return to parse it later.
23674      (Even though there is no definition, there might be default
23675      arguments that need handling.)  */
23676   if (member_p && decl
23677       && DECL_DECLARES_FUNCTION_P (decl))
23678     vec_safe_push (unparsed_funs_with_definitions, decl);
23679 }
23680
23681 /* Perform the deferred access checks from a template-parameter-list.
23682    CHECKS is a TREE_LIST of access checks, as returned by
23683    get_deferred_access_checks.  */
23684
23685 static void
23686 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23687 {
23688   ++processing_template_parmlist;
23689   perform_access_checks (checks, tf_warning_or_error);
23690   --processing_template_parmlist;
23691 }
23692
23693 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23694    `function-definition' sequence that follows a template header.
23695    If MEMBER_P is true, this declaration appears in a class scope.
23696
23697    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
23698    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
23699
23700 static tree
23701 cp_parser_single_declaration (cp_parser* parser,
23702                               vec<deferred_access_check, va_gc> *checks,
23703                               bool member_p,
23704                               bool explicit_specialization_p,
23705                               bool* friend_p)
23706 {
23707   int declares_class_or_enum;
23708   tree decl = NULL_TREE;
23709   cp_decl_specifier_seq decl_specifiers;
23710   bool function_definition_p = false;
23711   cp_token *decl_spec_token_start;
23712
23713   /* This function is only used when processing a template
23714      declaration.  */
23715   gcc_assert (innermost_scope_kind () == sk_template_parms
23716               || innermost_scope_kind () == sk_template_spec);
23717
23718   /* Defer access checks until we know what is being declared.  */
23719   push_deferring_access_checks (dk_deferred);
23720
23721   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23722      alternative.  */
23723   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23724   cp_parser_decl_specifier_seq (parser,
23725                                 CP_PARSER_FLAGS_OPTIONAL,
23726                                 &decl_specifiers,
23727                                 &declares_class_or_enum);
23728   if (friend_p)
23729     *friend_p = cp_parser_friend_p (&decl_specifiers);
23730
23731   /* There are no template typedefs.  */
23732   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23733     {
23734       error_at (decl_spec_token_start->location,
23735                 "template declaration of %<typedef%>");
23736       decl = error_mark_node;
23737     }
23738
23739   /* Gather up the access checks that occurred the
23740      decl-specifier-seq.  */
23741   stop_deferring_access_checks ();
23742
23743   /* Check for the declaration of a template class.  */
23744   if (declares_class_or_enum)
23745     {
23746       if (cp_parser_declares_only_class_p (parser))
23747         {
23748           decl = shadow_tag (&decl_specifiers);
23749
23750           /* In this case:
23751
23752                struct C {
23753                  friend template <typename T> struct A<T>::B;
23754                };
23755
23756              A<T>::B will be represented by a TYPENAME_TYPE, and
23757              therefore not recognized by shadow_tag.  */
23758           if (friend_p && *friend_p
23759               && !decl
23760               && decl_specifiers.type
23761               && TYPE_P (decl_specifiers.type))
23762             decl = decl_specifiers.type;
23763
23764           if (decl && decl != error_mark_node)
23765             decl = TYPE_NAME (decl);
23766           else
23767             decl = error_mark_node;
23768
23769           /* Perform access checks for template parameters.  */
23770           cp_parser_perform_template_parameter_access_checks (checks);
23771         }
23772     }
23773
23774   /* Complain about missing 'typename' or other invalid type names.  */
23775   if (!decl_specifiers.any_type_specifiers_p
23776       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23777     {
23778       /* cp_parser_parse_and_diagnose_invalid_type_name calls
23779          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23780          the rest of this declaration.  */
23781       decl = error_mark_node;
23782       goto out;
23783     }
23784
23785   /* If it's not a template class, try for a template function.  If
23786      the next token is a `;', then this declaration does not declare
23787      anything.  But, if there were errors in the decl-specifiers, then
23788      the error might well have come from an attempted class-specifier.
23789      In that case, there's no need to warn about a missing declarator.  */
23790   if (!decl
23791       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23792           || decl_specifiers.type != error_mark_node))
23793     {
23794       decl = cp_parser_init_declarator (parser,
23795                                         &decl_specifiers,
23796                                         checks,
23797                                         /*function_definition_allowed_p=*/true,
23798                                         member_p,
23799                                         declares_class_or_enum,
23800                                         &function_definition_p,
23801                                         NULL, NULL);
23802
23803     /* 7.1.1-1 [dcl.stc]
23804
23805        A storage-class-specifier shall not be specified in an explicit
23806        specialization...  */
23807     if (decl
23808         && explicit_specialization_p
23809         && decl_specifiers.storage_class != sc_none)
23810       {
23811         error_at (decl_spec_token_start->location,
23812                   "explicit template specialization cannot have a storage class");
23813         decl = error_mark_node;
23814       }
23815
23816     if (decl && VAR_P (decl))
23817       check_template_variable (decl);
23818     }
23819
23820   /* Look for a trailing `;' after the declaration.  */
23821   if (!function_definition_p
23822       && (decl == error_mark_node
23823           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23824     cp_parser_skip_to_end_of_block_or_statement (parser);
23825
23826  out:
23827   pop_deferring_access_checks ();
23828
23829   /* Clear any current qualification; whatever comes next is the start
23830      of something new.  */
23831   parser->scope = NULL_TREE;
23832   parser->qualifying_scope = NULL_TREE;
23833   parser->object_scope = NULL_TREE;
23834
23835   return decl;
23836 }
23837
23838 /* Parse a cast-expression that is not the operand of a unary "&".  */
23839
23840 static tree
23841 cp_parser_simple_cast_expression (cp_parser *parser)
23842 {
23843   return cp_parser_cast_expression (parser, /*address_p=*/false,
23844                                     /*cast_p=*/false, /*decltype*/false, NULL);
23845 }
23846
23847 /* Parse a functional cast to TYPE.  Returns an expression
23848    representing the cast.  */
23849
23850 static tree
23851 cp_parser_functional_cast (cp_parser* parser, tree type)
23852 {
23853   vec<tree, va_gc> *vec;
23854   tree expression_list;
23855   tree cast;
23856   bool nonconst_p;
23857
23858   if (!type)
23859     type = error_mark_node;
23860
23861   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23862     {
23863       cp_lexer_set_source_position (parser->lexer);
23864       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23865       expression_list = cp_parser_braced_list (parser, &nonconst_p);
23866       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23867       if (TREE_CODE (type) == TYPE_DECL)
23868         type = TREE_TYPE (type);
23869       return finish_compound_literal (type, expression_list,
23870                                       tf_warning_or_error);
23871     }
23872
23873
23874   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23875                                                  /*cast_p=*/true,
23876                                                  /*allow_expansion_p=*/true,
23877                                                  /*non_constant_p=*/NULL);
23878   if (vec == NULL)
23879     expression_list = error_mark_node;
23880   else
23881     {
23882       expression_list = build_tree_list_vec (vec);
23883       release_tree_vector (vec);
23884     }
23885
23886   cast = build_functional_cast (type, expression_list,
23887                                 tf_warning_or_error);
23888   /* [expr.const]/1: In an integral constant expression "only type
23889      conversions to integral or enumeration type can be used".  */
23890   if (TREE_CODE (type) == TYPE_DECL)
23891     type = TREE_TYPE (type);
23892   if (cast != error_mark_node
23893       && !cast_valid_in_integral_constant_expression_p (type)
23894       && cp_parser_non_integral_constant_expression (parser,
23895                                                      NIC_CONSTRUCTOR))
23896     return error_mark_node;
23897   return cast;
23898 }
23899
23900 /* Save the tokens that make up the body of a member function defined
23901    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
23902    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
23903    specifiers applied to the declaration.  Returns the FUNCTION_DECL
23904    for the member function.  */
23905
23906 static tree
23907 cp_parser_save_member_function_body (cp_parser* parser,
23908                                      cp_decl_specifier_seq *decl_specifiers,
23909                                      cp_declarator *declarator,
23910                                      tree attributes)
23911 {
23912   cp_token *first;
23913   cp_token *last;
23914   tree fn;
23915
23916   /* Create the FUNCTION_DECL.  */
23917   fn = grokmethod (decl_specifiers, declarator, attributes);
23918   cp_finalize_omp_declare_simd (parser, fn);
23919   /* If something went badly wrong, bail out now.  */
23920   if (fn == error_mark_node)
23921     {
23922       /* If there's a function-body, skip it.  */
23923       if (cp_parser_token_starts_function_definition_p
23924           (cp_lexer_peek_token (parser->lexer)))
23925         cp_parser_skip_to_end_of_block_or_statement (parser);
23926       return error_mark_node;
23927     }
23928
23929   /* Remember it, if there default args to post process.  */
23930   cp_parser_save_default_args (parser, fn);
23931
23932   /* Save away the tokens that make up the body of the
23933      function.  */
23934   first = parser->lexer->next_token;
23935   /* Handle function try blocks.  */
23936   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23937     cp_lexer_consume_token (parser->lexer);
23938   /* We can have braced-init-list mem-initializers before the fn body.  */
23939   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23940     {
23941       cp_lexer_consume_token (parser->lexer);
23942       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23943         {
23944           /* cache_group will stop after an un-nested { } pair, too.  */
23945           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23946             break;
23947
23948           /* variadic mem-inits have ... after the ')'.  */
23949           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23950             cp_lexer_consume_token (parser->lexer);
23951         }
23952     }
23953   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23954   /* Handle function try blocks.  */
23955   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23956     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23957   last = parser->lexer->next_token;
23958
23959   /* Save away the inline definition; we will process it when the
23960      class is complete.  */
23961   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23962   DECL_PENDING_INLINE_P (fn) = 1;
23963
23964   /* We need to know that this was defined in the class, so that
23965      friend templates are handled correctly.  */
23966   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23967
23968   /* Add FN to the queue of functions to be parsed later.  */
23969   vec_safe_push (unparsed_funs_with_definitions, fn);
23970
23971   return fn;
23972 }
23973
23974 /* Save the tokens that make up the in-class initializer for a non-static
23975    data member.  Returns a DEFAULT_ARG.  */
23976
23977 static tree
23978 cp_parser_save_nsdmi (cp_parser* parser)
23979 {
23980   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23981 }
23982
23983 /* Parse a template-argument-list, as well as the trailing ">" (but
23984    not the opening "<").  See cp_parser_template_argument_list for the
23985    return value.  */
23986
23987 static tree
23988 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23989 {
23990   tree arguments;
23991   tree saved_scope;
23992   tree saved_qualifying_scope;
23993   tree saved_object_scope;
23994   bool saved_greater_than_is_operator_p;
23995   int saved_unevaluated_operand;
23996   int saved_inhibit_evaluation_warnings;
23997
23998   /* [temp.names]
23999
24000      When parsing a template-id, the first non-nested `>' is taken as
24001      the end of the template-argument-list rather than a greater-than
24002      operator.  */
24003   saved_greater_than_is_operator_p
24004     = parser->greater_than_is_operator_p;
24005   parser->greater_than_is_operator_p = false;
24006   /* Parsing the argument list may modify SCOPE, so we save it
24007      here.  */
24008   saved_scope = parser->scope;
24009   saved_qualifying_scope = parser->qualifying_scope;
24010   saved_object_scope = parser->object_scope;
24011   /* We need to evaluate the template arguments, even though this
24012      template-id may be nested within a "sizeof".  */
24013   saved_unevaluated_operand = cp_unevaluated_operand;
24014   cp_unevaluated_operand = 0;
24015   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24016   c_inhibit_evaluation_warnings = 0;
24017   /* Parse the template-argument-list itself.  */
24018   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24019       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24020     arguments = NULL_TREE;
24021   else
24022     arguments = cp_parser_template_argument_list (parser);
24023   /* Look for the `>' that ends the template-argument-list. If we find
24024      a '>>' instead, it's probably just a typo.  */
24025   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24026     {
24027       if (cxx_dialect != cxx98)
24028         {
24029           /* In C++0x, a `>>' in a template argument list or cast
24030              expression is considered to be two separate `>'
24031              tokens. So, change the current token to a `>', but don't
24032              consume it: it will be consumed later when the outer
24033              template argument list (or cast expression) is parsed.
24034              Note that this replacement of `>' for `>>' is necessary
24035              even if we are parsing tentatively: in the tentative
24036              case, after calling
24037              cp_parser_enclosed_template_argument_list we will always
24038              throw away all of the template arguments and the first
24039              closing `>', either because the template argument list
24040              was erroneous or because we are replacing those tokens
24041              with a CPP_TEMPLATE_ID token.  The second `>' (which will
24042              not have been thrown away) is needed either to close an
24043              outer template argument list or to complete a new-style
24044              cast.  */
24045           cp_token *token = cp_lexer_peek_token (parser->lexer);
24046           token->type = CPP_GREATER;
24047         }
24048       else if (!saved_greater_than_is_operator_p)
24049         {
24050           /* If we're in a nested template argument list, the '>>' has
24051             to be a typo for '> >'. We emit the error message, but we
24052             continue parsing and we push a '>' as next token, so that
24053             the argument list will be parsed correctly.  Note that the
24054             global source location is still on the token before the
24055             '>>', so we need to say explicitly where we want it.  */
24056           cp_token *token = cp_lexer_peek_token (parser->lexer);
24057           error_at (token->location, "%<>>%> should be %<> >%> "
24058                     "within a nested template argument list");
24059
24060           token->type = CPP_GREATER;
24061         }
24062       else
24063         {
24064           /* If this is not a nested template argument list, the '>>'
24065             is a typo for '>'. Emit an error message and continue.
24066             Same deal about the token location, but here we can get it
24067             right by consuming the '>>' before issuing the diagnostic.  */
24068           cp_token *token = cp_lexer_consume_token (parser->lexer);
24069           error_at (token->location,
24070                     "spurious %<>>%>, use %<>%> to terminate "
24071                     "a template argument list");
24072         }
24073     }
24074   else
24075     cp_parser_skip_to_end_of_template_parameter_list (parser);
24076   /* The `>' token might be a greater-than operator again now.  */
24077   parser->greater_than_is_operator_p
24078     = saved_greater_than_is_operator_p;
24079   /* Restore the SAVED_SCOPE.  */
24080   parser->scope = saved_scope;
24081   parser->qualifying_scope = saved_qualifying_scope;
24082   parser->object_scope = saved_object_scope;
24083   cp_unevaluated_operand = saved_unevaluated_operand;
24084   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24085
24086   return arguments;
24087 }
24088
24089 /* MEMBER_FUNCTION is a member function, or a friend.  If default
24090    arguments, or the body of the function have not yet been parsed,
24091    parse them now.  */
24092
24093 static void
24094 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24095 {
24096   timevar_push (TV_PARSE_INMETH);
24097   /* If this member is a template, get the underlying
24098      FUNCTION_DECL.  */
24099   if (DECL_FUNCTION_TEMPLATE_P (member_function))
24100     member_function = DECL_TEMPLATE_RESULT (member_function);
24101
24102   /* There should not be any class definitions in progress at this
24103      point; the bodies of members are only parsed outside of all class
24104      definitions.  */
24105   gcc_assert (parser->num_classes_being_defined == 0);
24106   /* While we're parsing the member functions we might encounter more
24107      classes.  We want to handle them right away, but we don't want
24108      them getting mixed up with functions that are currently in the
24109      queue.  */
24110   push_unparsed_function_queues (parser);
24111
24112   /* Make sure that any template parameters are in scope.  */
24113   maybe_begin_member_template_processing (member_function);
24114
24115   /* If the body of the function has not yet been parsed, parse it
24116      now.  */
24117   if (DECL_PENDING_INLINE_P (member_function))
24118     {
24119       tree function_scope;
24120       cp_token_cache *tokens;
24121
24122       /* The function is no longer pending; we are processing it.  */
24123       tokens = DECL_PENDING_INLINE_INFO (member_function);
24124       DECL_PENDING_INLINE_INFO (member_function) = NULL;
24125       DECL_PENDING_INLINE_P (member_function) = 0;
24126
24127       /* If this is a local class, enter the scope of the containing
24128          function.  */
24129       function_scope = current_function_decl;
24130       if (function_scope)
24131         push_function_context ();
24132
24133       /* Push the body of the function onto the lexer stack.  */
24134       cp_parser_push_lexer_for_tokens (parser, tokens);
24135
24136       /* Let the front end know that we going to be defining this
24137          function.  */
24138       start_preparsed_function (member_function, NULL_TREE,
24139                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
24140
24141       /* Don't do access checking if it is a templated function.  */
24142       if (processing_template_decl)
24143         push_deferring_access_checks (dk_no_check);
24144
24145       /* #pragma omp declare reduction needs special parsing.  */
24146       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24147         {
24148           parser->lexer->in_pragma = true;
24149           cp_parser_omp_declare_reduction_exprs (member_function, parser);
24150           finish_function (/*inline*/2);
24151           cp_check_omp_declare_reduction (member_function);
24152         }
24153       else
24154         /* Now, parse the body of the function.  */
24155         cp_parser_function_definition_after_declarator (parser,
24156                                                         /*inline_p=*/true);
24157
24158       if (processing_template_decl)
24159         pop_deferring_access_checks ();
24160
24161       /* Leave the scope of the containing function.  */
24162       if (function_scope)
24163         pop_function_context ();
24164       cp_parser_pop_lexer (parser);
24165     }
24166
24167   /* Remove any template parameters from the symbol table.  */
24168   maybe_end_member_template_processing ();
24169
24170   /* Restore the queue.  */
24171   pop_unparsed_function_queues (parser);
24172   timevar_pop (TV_PARSE_INMETH);
24173 }
24174
24175 /* If DECL contains any default args, remember it on the unparsed
24176    functions queue.  */
24177
24178 static void
24179 cp_parser_save_default_args (cp_parser* parser, tree decl)
24180 {
24181   tree probe;
24182
24183   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24184        probe;
24185        probe = TREE_CHAIN (probe))
24186     if (TREE_PURPOSE (probe))
24187       {
24188         cp_default_arg_entry entry = {current_class_type, decl};
24189         vec_safe_push (unparsed_funs_with_default_args, entry);
24190         break;
24191       }
24192 }
24193
24194 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24195    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
24196    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
24197    from the parameter-type-list.  */
24198
24199 static tree
24200 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24201                                       tree default_arg, tree parmtype)
24202 {
24203   cp_token_cache *tokens;
24204   tree parsed_arg;
24205   bool dummy;
24206
24207   if (default_arg == error_mark_node)
24208     return error_mark_node;
24209
24210   /* Push the saved tokens for the default argument onto the parser's
24211      lexer stack.  */
24212   tokens = DEFARG_TOKENS (default_arg);
24213   cp_parser_push_lexer_for_tokens (parser, tokens);
24214
24215   start_lambda_scope (decl);
24216
24217   /* Parse the default argument.  */
24218   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24219   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24220     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24221
24222   finish_lambda_scope ();
24223
24224   if (parsed_arg == error_mark_node)
24225     cp_parser_skip_to_end_of_statement (parser);
24226
24227   if (!processing_template_decl)
24228     {
24229       /* In a non-template class, check conversions now.  In a template,
24230          we'll wait and instantiate these as needed.  */
24231       if (TREE_CODE (decl) == PARM_DECL)
24232         parsed_arg = check_default_argument (parmtype, parsed_arg,
24233                                              tf_warning_or_error);
24234       else
24235         parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24236     }
24237
24238   /* If the token stream has not been completely used up, then
24239      there was extra junk after the end of the default
24240      argument.  */
24241   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24242     {
24243       if (TREE_CODE (decl) == PARM_DECL)
24244         cp_parser_error (parser, "expected %<,%>");
24245       else
24246         cp_parser_error (parser, "expected %<;%>");
24247     }
24248
24249   /* Revert to the main lexer.  */
24250   cp_parser_pop_lexer (parser);
24251
24252   return parsed_arg;
24253 }
24254
24255 /* FIELD is a non-static data member with an initializer which we saved for
24256    later; parse it now.  */
24257
24258 static void
24259 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24260 {
24261   tree def;
24262
24263   maybe_begin_member_template_processing (field);
24264
24265   push_unparsed_function_queues (parser);
24266   def = cp_parser_late_parse_one_default_arg (parser, field,
24267                                               DECL_INITIAL (field),
24268                                               NULL_TREE);
24269   pop_unparsed_function_queues (parser);
24270
24271   maybe_end_member_template_processing ();
24272
24273   DECL_INITIAL (field) = def;
24274 }
24275
24276 /* FN is a FUNCTION_DECL which may contains a parameter with an
24277    unparsed DEFAULT_ARG.  Parse the default args now.  This function
24278    assumes that the current scope is the scope in which the default
24279    argument should be processed.  */
24280
24281 static void
24282 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24283 {
24284   bool saved_local_variables_forbidden_p;
24285   tree parm, parmdecl;
24286
24287   /* While we're parsing the default args, we might (due to the
24288      statement expression extension) encounter more classes.  We want
24289      to handle them right away, but we don't want them getting mixed
24290      up with default args that are currently in the queue.  */
24291   push_unparsed_function_queues (parser);
24292
24293   /* Local variable names (and the `this' keyword) may not appear
24294      in a default argument.  */
24295   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24296   parser->local_variables_forbidden_p = true;
24297
24298   push_defarg_context (fn);
24299
24300   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24301          parmdecl = DECL_ARGUMENTS (fn);
24302        parm && parm != void_list_node;
24303        parm = TREE_CHAIN (parm),
24304          parmdecl = DECL_CHAIN (parmdecl))
24305     {
24306       tree default_arg = TREE_PURPOSE (parm);
24307       tree parsed_arg;
24308       vec<tree, va_gc> *insts;
24309       tree copy;
24310       unsigned ix;
24311
24312       if (!default_arg)
24313         continue;
24314
24315       if (TREE_CODE (default_arg) != DEFAULT_ARG)
24316         /* This can happen for a friend declaration for a function
24317            already declared with default arguments.  */
24318         continue;
24319
24320       parsed_arg
24321         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24322                                                 default_arg,
24323                                                 TREE_VALUE (parm));
24324       if (parsed_arg == error_mark_node)
24325         {
24326           continue;
24327         }
24328
24329       TREE_PURPOSE (parm) = parsed_arg;
24330
24331       /* Update any instantiations we've already created.  */
24332       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24333            vec_safe_iterate (insts, ix, &copy); ix++)
24334         TREE_PURPOSE (copy) = parsed_arg;
24335     }
24336
24337   pop_defarg_context ();
24338
24339   /* Make sure no default arg is missing.  */
24340   check_default_args (fn);
24341
24342   /* Restore the state of local_variables_forbidden_p.  */
24343   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24344
24345   /* Restore the queue.  */
24346   pop_unparsed_function_queues (parser);
24347 }
24348
24349 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24350
24351      sizeof ... ( identifier )
24352
24353    where the 'sizeof' token has already been consumed.  */
24354
24355 static tree
24356 cp_parser_sizeof_pack (cp_parser *parser)
24357 {
24358   /* Consume the `...'.  */
24359   cp_lexer_consume_token (parser->lexer);
24360   maybe_warn_variadic_templates ();
24361
24362   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24363   if (paren)
24364     cp_lexer_consume_token (parser->lexer);
24365   else
24366     permerror (cp_lexer_peek_token (parser->lexer)->location,
24367                "%<sizeof...%> argument must be surrounded by parentheses");
24368
24369   cp_token *token = cp_lexer_peek_token (parser->lexer);
24370   tree name = cp_parser_identifier (parser);
24371   if (name == error_mark_node)
24372     return error_mark_node;
24373   /* The name is not qualified.  */
24374   parser->scope = NULL_TREE;
24375   parser->qualifying_scope = NULL_TREE;
24376   parser->object_scope = NULL_TREE;
24377   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24378   if (expr == error_mark_node)
24379     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24380                                  token->location);
24381   if (TREE_CODE (expr) == TYPE_DECL)
24382     expr = TREE_TYPE (expr);
24383   else if (TREE_CODE (expr) == CONST_DECL)
24384     expr = DECL_INITIAL (expr);
24385   expr = make_pack_expansion (expr);
24386
24387   if (paren)
24388     cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24389
24390   return expr;
24391 }
24392
24393 /* Parse the operand of `sizeof' (or a similar operator).  Returns
24394    either a TYPE or an expression, depending on the form of the
24395    input.  The KEYWORD indicates which kind of expression we have
24396    encountered.  */
24397
24398 static tree
24399 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24400 {
24401   tree expr = NULL_TREE;
24402   const char *saved_message;
24403   char *tmp;
24404   bool saved_integral_constant_expression_p;
24405   bool saved_non_integral_constant_expression_p;
24406
24407   /* If it's a `...', then we are computing the length of a parameter
24408      pack.  */
24409   if (keyword == RID_SIZEOF
24410       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24411     return cp_parser_sizeof_pack (parser);
24412
24413   /* Types cannot be defined in a `sizeof' expression.  Save away the
24414      old message.  */
24415   saved_message = parser->type_definition_forbidden_message;
24416   /* And create the new one.  */
24417   tmp = concat ("types may not be defined in %<",
24418                 IDENTIFIER_POINTER (ridpointers[keyword]),
24419                 "%> expressions", NULL);
24420   parser->type_definition_forbidden_message = tmp;
24421
24422   /* The restrictions on constant-expressions do not apply inside
24423      sizeof expressions.  */
24424   saved_integral_constant_expression_p
24425     = parser->integral_constant_expression_p;
24426   saved_non_integral_constant_expression_p
24427     = parser->non_integral_constant_expression_p;
24428   parser->integral_constant_expression_p = false;
24429
24430   /* Do not actually evaluate the expression.  */
24431   ++cp_unevaluated_operand;
24432   ++c_inhibit_evaluation_warnings;
24433   /* If it's a `(', then we might be looking at the type-id
24434      construction.  */
24435   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24436     {
24437       tree type = NULL_TREE;
24438
24439       /* We can't be sure yet whether we're looking at a type-id or an
24440          expression.  */
24441       cp_parser_parse_tentatively (parser);
24442       /* Note: as a GNU Extension, compound literals are considered
24443          postfix-expressions as they are in C99, so they are valid
24444          arguments to sizeof.  See comment in cp_parser_cast_expression
24445          for details.  */
24446       if (cp_parser_compound_literal_p (parser))
24447         cp_parser_simulate_error (parser);
24448       else
24449         {
24450           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24451           parser->in_type_id_in_expr_p = true;
24452           /* Look for the type-id.  */
24453           type = cp_parser_type_id (parser);
24454           /* Look for the closing `)'.  */
24455           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24456           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24457         }
24458
24459       /* If all went well, then we're done.  */
24460       if (cp_parser_parse_definitely (parser))
24461         {
24462           cp_decl_specifier_seq decl_specs;
24463
24464           /* Build a trivial decl-specifier-seq.  */
24465           clear_decl_specs (&decl_specs);
24466           decl_specs.type = type;
24467
24468           /* Call grokdeclarator to figure out what type this is.  */
24469           expr = grokdeclarator (NULL,
24470                                  &decl_specs,
24471                                  TYPENAME,
24472                                  /*initialized=*/0,
24473                                  /*attrlist=*/NULL);
24474         }
24475     }
24476
24477   /* If the type-id production did not work out, then we must be
24478      looking at the unary-expression production.  */
24479   if (!expr)
24480     expr = cp_parser_unary_expression (parser);
24481
24482   /* Go back to evaluating expressions.  */
24483   --cp_unevaluated_operand;
24484   --c_inhibit_evaluation_warnings;
24485
24486   /* Free the message we created.  */
24487   free (tmp);
24488   /* And restore the old one.  */
24489   parser->type_definition_forbidden_message = saved_message;
24490   parser->integral_constant_expression_p
24491     = saved_integral_constant_expression_p;
24492   parser->non_integral_constant_expression_p
24493     = saved_non_integral_constant_expression_p;
24494
24495   return expr;
24496 }
24497
24498 /* If the current declaration has no declarator, return true.  */
24499
24500 static bool
24501 cp_parser_declares_only_class_p (cp_parser *parser)
24502 {
24503   /* If the next token is a `;' or a `,' then there is no
24504      declarator.  */
24505   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24506           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24507 }
24508
24509 /* Update the DECL_SPECS to reflect the storage class indicated by
24510    KEYWORD.  */
24511
24512 static void
24513 cp_parser_set_storage_class (cp_parser *parser,
24514                              cp_decl_specifier_seq *decl_specs,
24515                              enum rid keyword,
24516                              cp_token *token)
24517 {
24518   cp_storage_class storage_class;
24519
24520   if (parser->in_unbraced_linkage_specification_p)
24521     {
24522       error_at (token->location, "invalid use of %qD in linkage specification",
24523                 ridpointers[keyword]);
24524       return;
24525     }
24526   else if (decl_specs->storage_class != sc_none)
24527     {
24528       decl_specs->conflicting_specifiers_p = true;
24529       return;
24530     }
24531
24532   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24533       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24534       && decl_specs->gnu_thread_keyword_p)
24535     {
24536       pedwarn (decl_specs->locations[ds_thread], 0,
24537                 "%<__thread%> before %qD", ridpointers[keyword]);
24538     }
24539
24540   switch (keyword)
24541     {
24542     case RID_AUTO:
24543       storage_class = sc_auto;
24544       break;
24545     case RID_REGISTER:
24546       storage_class = sc_register;
24547       break;
24548     case RID_STATIC:
24549       storage_class = sc_static;
24550       break;
24551     case RID_EXTERN:
24552       storage_class = sc_extern;
24553       break;
24554     case RID_MUTABLE:
24555       storage_class = sc_mutable;
24556       break;
24557     default:
24558       gcc_unreachable ();
24559     }
24560   decl_specs->storage_class = storage_class;
24561   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24562
24563   /* A storage class specifier cannot be applied alongside a typedef 
24564      specifier. If there is a typedef specifier present then set 
24565      conflicting_specifiers_p which will trigger an error later
24566      on in grokdeclarator. */
24567   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24568     decl_specs->conflicting_specifiers_p = true;
24569 }
24570
24571 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
24572    is true, the type is a class or enum definition.  */
24573
24574 static void
24575 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24576                               tree type_spec,
24577                               cp_token *token,
24578                               bool type_definition_p)
24579 {
24580   decl_specs->any_specifiers_p = true;
24581
24582   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24583      (with, for example, in "typedef int wchar_t;") we remember that
24584      this is what happened.  In system headers, we ignore these
24585      declarations so that G++ can work with system headers that are not
24586      C++-safe.  */
24587   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24588       && !type_definition_p
24589       && (type_spec == boolean_type_node
24590           || type_spec == char16_type_node
24591           || type_spec == char32_type_node
24592           || type_spec == wchar_type_node)
24593       && (decl_specs->type
24594           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24595           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24596           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24597           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24598     {
24599       decl_specs->redefined_builtin_type = type_spec;
24600       set_and_check_decl_spec_loc (decl_specs,
24601                                    ds_redefined_builtin_type_spec,
24602                                    token);
24603       if (!decl_specs->type)
24604         {
24605           decl_specs->type = type_spec;
24606           decl_specs->type_definition_p = false;
24607           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24608         }
24609     }
24610   else if (decl_specs->type)
24611     decl_specs->multiple_types_p = true;
24612   else
24613     {
24614       decl_specs->type = type_spec;
24615       decl_specs->type_definition_p = type_definition_p;
24616       decl_specs->redefined_builtin_type = NULL_TREE;
24617       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24618     }
24619 }
24620
24621 /* True iff TOKEN is the GNU keyword __thread.  */
24622
24623 static bool
24624 token_is__thread (cp_token *token)
24625 {
24626   gcc_assert (token->keyword == RID_THREAD);
24627   return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24628 }
24629
24630 /* Set the location for a declarator specifier and check if it is
24631    duplicated.
24632
24633    DECL_SPECS is the sequence of declarator specifiers onto which to
24634    set the location.
24635
24636    DS is the single declarator specifier to set which location  is to
24637    be set onto the existing sequence of declarators.
24638
24639    LOCATION is the location for the declarator specifier to
24640    consider.  */
24641
24642 static void
24643 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24644                              cp_decl_spec ds, cp_token *token)
24645 {
24646   gcc_assert (ds < ds_last);
24647
24648   if (decl_specs == NULL)
24649     return;
24650
24651   source_location location = token->location;
24652
24653   if (decl_specs->locations[ds] == 0)
24654     {
24655       decl_specs->locations[ds] = location;
24656       if (ds == ds_thread)
24657         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24658     }
24659   else
24660     {
24661       if (ds == ds_long)
24662         {
24663           if (decl_specs->locations[ds_long_long] != 0)
24664             error_at (location,
24665                       "%<long long long%> is too long for GCC");
24666           else
24667             {
24668               decl_specs->locations[ds_long_long] = location;
24669               pedwarn_cxx98 (location,
24670                              OPT_Wlong_long, 
24671                              "ISO C++ 1998 does not support %<long long%>");
24672             }
24673         }
24674       else if (ds == ds_thread)
24675         {
24676           bool gnu = token_is__thread (token);
24677           if (gnu != decl_specs->gnu_thread_keyword_p)
24678             error_at (location,
24679                       "both %<__thread%> and %<thread_local%> specified");
24680           else
24681             error_at (location, "duplicate %qD", token->u.value);
24682         }
24683       else
24684         {
24685           static const char *const decl_spec_names[] = {
24686             "signed",
24687             "unsigned",
24688             "short",
24689             "long",
24690             "const",
24691             "volatile",
24692             "restrict",
24693             "inline",
24694             "virtual",
24695             "explicit",
24696             "friend",
24697             "typedef",
24698             "using",
24699             "constexpr",
24700             "__complex"
24701           };
24702           error_at (location,
24703                     "duplicate %qs", decl_spec_names[ds]);
24704         }
24705     }
24706 }
24707
24708 /* Return true iff the declarator specifier DS is present in the
24709    sequence of declarator specifiers DECL_SPECS.  */
24710
24711 bool
24712 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24713                           cp_decl_spec ds)
24714 {
24715   gcc_assert (ds < ds_last);
24716
24717   if (decl_specs == NULL)
24718     return false;
24719
24720   return decl_specs->locations[ds] != 0;
24721 }
24722
24723 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24724    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
24725
24726 static bool
24727 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24728 {
24729   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24730 }
24731
24732 /* Issue an error message indicating that TOKEN_DESC was expected.
24733    If KEYWORD is true, it indicated this function is called by
24734    cp_parser_require_keword and the required token can only be
24735    a indicated keyword. */
24736
24737 static void
24738 cp_parser_required_error (cp_parser *parser,
24739                           required_token token_desc,
24740                           bool keyword)
24741 {
24742   switch (token_desc)
24743     {
24744       case RT_NEW:
24745         cp_parser_error (parser, "expected %<new%>");
24746         return;
24747       case RT_DELETE:
24748         cp_parser_error (parser, "expected %<delete%>");
24749         return;
24750       case RT_RETURN:
24751         cp_parser_error (parser, "expected %<return%>");
24752         return;
24753       case RT_WHILE:
24754         cp_parser_error (parser, "expected %<while%>");
24755         return;
24756       case RT_EXTERN:
24757         cp_parser_error (parser, "expected %<extern%>");
24758         return;
24759       case RT_STATIC_ASSERT:
24760         cp_parser_error (parser, "expected %<static_assert%>");
24761         return;
24762       case RT_DECLTYPE:
24763         cp_parser_error (parser, "expected %<decltype%>");
24764         return;
24765       case RT_OPERATOR:
24766         cp_parser_error (parser, "expected %<operator%>");
24767         return;
24768       case RT_CLASS:
24769         cp_parser_error (parser, "expected %<class%>");
24770         return;
24771       case RT_TEMPLATE:
24772         cp_parser_error (parser, "expected %<template%>");
24773         return;
24774       case RT_NAMESPACE:
24775         cp_parser_error (parser, "expected %<namespace%>");
24776         return;
24777       case RT_USING:
24778         cp_parser_error (parser, "expected %<using%>");
24779         return;
24780       case RT_ASM:
24781         cp_parser_error (parser, "expected %<asm%>");
24782         return;
24783       case RT_TRY:
24784         cp_parser_error (parser, "expected %<try%>");
24785         return;
24786       case RT_CATCH:
24787         cp_parser_error (parser, "expected %<catch%>");
24788         return;
24789       case RT_THROW:
24790         cp_parser_error (parser, "expected %<throw%>");
24791         return;
24792       case RT_LABEL:
24793         cp_parser_error (parser, "expected %<__label__%>");
24794         return;
24795       case RT_AT_TRY:
24796         cp_parser_error (parser, "expected %<@try%>");
24797         return;
24798       case RT_AT_SYNCHRONIZED:
24799         cp_parser_error (parser, "expected %<@synchronized%>");
24800         return;
24801       case RT_AT_THROW:
24802         cp_parser_error (parser, "expected %<@throw%>");
24803         return;
24804       case RT_TRANSACTION_ATOMIC:
24805         cp_parser_error (parser, "expected %<__transaction_atomic%>");
24806         return;
24807       case RT_TRANSACTION_RELAXED:
24808         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24809         return;
24810       default:
24811         break;
24812     }
24813   if (!keyword)
24814     {
24815       switch (token_desc)
24816         {
24817           case RT_SEMICOLON:
24818             cp_parser_error (parser, "expected %<;%>");
24819             return;
24820           case RT_OPEN_PAREN:
24821             cp_parser_error (parser, "expected %<(%>");
24822             return;
24823           case RT_CLOSE_BRACE:
24824             cp_parser_error (parser, "expected %<}%>");
24825             return;
24826           case RT_OPEN_BRACE:
24827             cp_parser_error (parser, "expected %<{%>");
24828             return;
24829           case RT_CLOSE_SQUARE:
24830             cp_parser_error (parser, "expected %<]%>");
24831             return;
24832           case RT_OPEN_SQUARE:
24833             cp_parser_error (parser, "expected %<[%>");
24834             return;
24835           case RT_COMMA:
24836             cp_parser_error (parser, "expected %<,%>");
24837             return;
24838           case RT_SCOPE:
24839             cp_parser_error (parser, "expected %<::%>");
24840             return;
24841           case RT_LESS:
24842             cp_parser_error (parser, "expected %<<%>");
24843             return;
24844           case RT_GREATER:
24845             cp_parser_error (parser, "expected %<>%>");
24846             return;
24847           case RT_EQ:
24848             cp_parser_error (parser, "expected %<=%>");
24849             return;
24850           case RT_ELLIPSIS:
24851             cp_parser_error (parser, "expected %<...%>");
24852             return;
24853           case RT_MULT:
24854             cp_parser_error (parser, "expected %<*%>");
24855             return;
24856           case RT_COMPL:
24857             cp_parser_error (parser, "expected %<~%>");
24858             return;
24859           case RT_COLON:
24860             cp_parser_error (parser, "expected %<:%>");
24861             return;
24862           case RT_COLON_SCOPE:
24863             cp_parser_error (parser, "expected %<:%> or %<::%>");
24864             return;
24865           case RT_CLOSE_PAREN:
24866             cp_parser_error (parser, "expected %<)%>");
24867             return;
24868           case RT_COMMA_CLOSE_PAREN:
24869             cp_parser_error (parser, "expected %<,%> or %<)%>");
24870             return;
24871           case RT_PRAGMA_EOL:
24872             cp_parser_error (parser, "expected end of line");
24873             return;
24874           case RT_NAME:
24875             cp_parser_error (parser, "expected identifier");
24876             return;
24877           case RT_SELECT:
24878             cp_parser_error (parser, "expected selection-statement");
24879             return;
24880           case RT_INTERATION:
24881             cp_parser_error (parser, "expected iteration-statement");
24882             return;
24883           case RT_JUMP:
24884             cp_parser_error (parser, "expected jump-statement");
24885             return;
24886           case RT_CLASS_KEY:
24887             cp_parser_error (parser, "expected class-key");
24888             return;
24889           case RT_CLASS_TYPENAME_TEMPLATE:
24890             cp_parser_error (parser,
24891                  "expected %<class%>, %<typename%>, or %<template%>");
24892             return;
24893           default:
24894             gcc_unreachable ();
24895         }
24896     }
24897   else
24898     gcc_unreachable ();
24899 }
24900
24901
24902
24903 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
24904    issue an error message indicating that TOKEN_DESC was expected.
24905
24906    Returns the token consumed, if the token had the appropriate type.
24907    Otherwise, returns NULL.  */
24908
24909 static cp_token *
24910 cp_parser_require (cp_parser* parser,
24911                    enum cpp_ttype type,
24912                    required_token token_desc)
24913 {
24914   if (cp_lexer_next_token_is (parser->lexer, type))
24915     return cp_lexer_consume_token (parser->lexer);
24916   else
24917     {
24918       /* Output the MESSAGE -- unless we're parsing tentatively.  */
24919       if (!cp_parser_simulate_error (parser))
24920         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24921       return NULL;
24922     }
24923 }
24924
24925 /* An error message is produced if the next token is not '>'.
24926    All further tokens are skipped until the desired token is
24927    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
24928
24929 static void
24930 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24931 {
24932   /* Current level of '< ... >'.  */
24933   unsigned level = 0;
24934   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
24935   unsigned nesting_depth = 0;
24936
24937   /* Are we ready, yet?  If not, issue error message.  */
24938   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24939     return;
24940
24941   /* Skip tokens until the desired token is found.  */
24942   while (true)
24943     {
24944       /* Peek at the next token.  */
24945       switch (cp_lexer_peek_token (parser->lexer)->type)
24946         {
24947         case CPP_LESS:
24948           if (!nesting_depth)
24949             ++level;
24950           break;
24951
24952         case CPP_RSHIFT:
24953           if (cxx_dialect == cxx98)
24954             /* C++0x views the `>>' operator as two `>' tokens, but
24955                C++98 does not. */
24956             break;
24957           else if (!nesting_depth && level-- == 0)
24958             {
24959               /* We've hit a `>>' where the first `>' closes the
24960                  template argument list, and the second `>' is
24961                  spurious.  Just consume the `>>' and stop; we've
24962                  already produced at least one error.  */
24963               cp_lexer_consume_token (parser->lexer);
24964               return;
24965             }
24966           /* Fall through for C++0x, so we handle the second `>' in
24967              the `>>'.  */
24968
24969         case CPP_GREATER:
24970           if (!nesting_depth && level-- == 0)
24971             {
24972               /* We've reached the token we want, consume it and stop.  */
24973               cp_lexer_consume_token (parser->lexer);
24974               return;
24975             }
24976           break;
24977
24978         case CPP_OPEN_PAREN:
24979         case CPP_OPEN_SQUARE:
24980           ++nesting_depth;
24981           break;
24982
24983         case CPP_CLOSE_PAREN:
24984         case CPP_CLOSE_SQUARE:
24985           if (nesting_depth-- == 0)
24986             return;
24987           break;
24988
24989         case CPP_EOF:
24990         case CPP_PRAGMA_EOL:
24991         case CPP_SEMICOLON:
24992         case CPP_OPEN_BRACE:
24993         case CPP_CLOSE_BRACE:
24994           /* The '>' was probably forgotten, don't look further.  */
24995           return;
24996
24997         default:
24998           break;
24999         }
25000
25001       /* Consume this token.  */
25002       cp_lexer_consume_token (parser->lexer);
25003     }
25004 }
25005
25006 /* If the next token is the indicated keyword, consume it.  Otherwise,
25007    issue an error message indicating that TOKEN_DESC was expected.
25008
25009    Returns the token consumed, if the token had the appropriate type.
25010    Otherwise, returns NULL.  */
25011
25012 static cp_token *
25013 cp_parser_require_keyword (cp_parser* parser,
25014                            enum rid keyword,
25015                            required_token token_desc)
25016 {
25017   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25018
25019   if (token && token->keyword != keyword)
25020     {
25021       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
25022       return NULL;
25023     }
25024
25025   return token;
25026 }
25027
25028 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25029    function-definition.  */
25030
25031 static bool
25032 cp_parser_token_starts_function_definition_p (cp_token* token)
25033 {
25034   return (/* An ordinary function-body begins with an `{'.  */
25035           token->type == CPP_OPEN_BRACE
25036           /* A ctor-initializer begins with a `:'.  */
25037           || token->type == CPP_COLON
25038           /* A function-try-block begins with `try'.  */
25039           || token->keyword == RID_TRY
25040           /* A function-transaction-block begins with `__transaction_atomic'
25041              or `__transaction_relaxed'.  */
25042           || token->keyword == RID_TRANSACTION_ATOMIC
25043           || token->keyword == RID_TRANSACTION_RELAXED
25044           /* The named return value extension begins with `return'.  */
25045           || token->keyword == RID_RETURN);
25046 }
25047
25048 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25049    definition.  */
25050
25051 static bool
25052 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25053 {
25054   cp_token *token;
25055
25056   token = cp_lexer_peek_token (parser->lexer);
25057   return (token->type == CPP_OPEN_BRACE
25058           || (token->type == CPP_COLON
25059               && !parser->colon_doesnt_start_class_def_p));
25060 }
25061
25062 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25063    C++0x) ending a template-argument.  */
25064
25065 static bool
25066 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25067 {
25068   cp_token *token;
25069
25070   token = cp_lexer_peek_token (parser->lexer);
25071   return (token->type == CPP_COMMA 
25072           || token->type == CPP_GREATER
25073           || token->type == CPP_ELLIPSIS
25074           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25075 }
25076
25077 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25078    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
25079
25080 static bool
25081 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25082                                                      size_t n)
25083 {
25084   cp_token *token;
25085
25086   token = cp_lexer_peek_nth_token (parser->lexer, n);
25087   if (token->type == CPP_LESS)
25088     return true;
25089   /* Check for the sequence `<::' in the original code. It would be lexed as
25090      `[:', where `[' is a digraph, and there is no whitespace before
25091      `:'.  */
25092   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25093     {
25094       cp_token *token2;
25095       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25096       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25097         return true;
25098     }
25099   return false;
25100 }
25101
25102 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25103    or none_type otherwise.  */
25104
25105 static enum tag_types
25106 cp_parser_token_is_class_key (cp_token* token)
25107 {
25108   switch (token->keyword)
25109     {
25110     case RID_CLASS:
25111       return class_type;
25112     case RID_STRUCT:
25113       return record_type;
25114     case RID_UNION:
25115       return union_type;
25116
25117     default:
25118       return none_type;
25119     }
25120 }
25121
25122 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25123    or none_type otherwise or if the token is null.  */
25124
25125 static enum tag_types
25126 cp_parser_token_is_type_parameter_key (cp_token* token)
25127 {
25128   if (!token)
25129     return none_type;
25130
25131   switch (token->keyword)
25132     {
25133     case RID_CLASS:
25134       return class_type;
25135     case RID_TYPENAME:
25136       return typename_type;
25137
25138     default:
25139       return none_type;
25140     }
25141 }
25142
25143 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
25144
25145 static void
25146 cp_parser_check_class_key (enum tag_types class_key, tree type)
25147 {
25148   if (type == error_mark_node)
25149     return;
25150   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25151     {
25152       if (permerror (input_location, "%qs tag used in naming %q#T",
25153                      class_key == union_type ? "union"
25154                      : class_key == record_type ? "struct" : "class",
25155                      type))
25156         inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25157                 "%q#T was previously declared here", type);
25158     }
25159 }
25160
25161 /* Issue an error message if DECL is redeclared with different
25162    access than its original declaration [class.access.spec/3].
25163    This applies to nested classes and nested class templates.
25164    [class.mem/1].  */
25165
25166 static void
25167 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25168 {
25169   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25170     return;
25171
25172   if ((TREE_PRIVATE (decl)
25173        != (current_access_specifier == access_private_node))
25174       || (TREE_PROTECTED (decl)
25175           != (current_access_specifier == access_protected_node)))
25176     error_at (location, "%qD redeclared with different access", decl);
25177 }
25178
25179 /* Look for the `template' keyword, as a syntactic disambiguator.
25180    Return TRUE iff it is present, in which case it will be
25181    consumed.  */
25182
25183 static bool
25184 cp_parser_optional_template_keyword (cp_parser *parser)
25185 {
25186   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25187     {
25188       /* In C++98 the `template' keyword can only be used within templates;
25189          outside templates the parser can always figure out what is a
25190          template and what is not.  In C++11,  per the resolution of DR 468,
25191          `template' is allowed in cases where it is not strictly necessary.  */
25192       if (!processing_template_decl
25193           && pedantic && cxx_dialect == cxx98)
25194         {
25195           cp_token *token = cp_lexer_peek_token (parser->lexer);
25196           pedwarn (token->location, OPT_Wpedantic,
25197                    "in C++98 %<template%> (as a disambiguator) is only "
25198                    "allowed within templates");
25199           /* If this part of the token stream is rescanned, the same
25200              error message would be generated.  So, we purge the token
25201              from the stream.  */
25202           cp_lexer_purge_token (parser->lexer);
25203           return false;
25204         }
25205       else
25206         {
25207           /* Consume the `template' keyword.  */
25208           cp_lexer_consume_token (parser->lexer);
25209           return true;
25210         }
25211     }
25212   return false;
25213 }
25214
25215 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
25216    set PARSER->SCOPE, and perform other related actions.  */
25217
25218 static void
25219 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25220 {
25221   int i;
25222   struct tree_check *check_value;
25223   deferred_access_check *chk;
25224   vec<deferred_access_check, va_gc> *checks;
25225
25226   /* Get the stored value.  */
25227   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25228   /* Perform any access checks that were deferred.  */
25229   checks = check_value->checks;
25230   if (checks)
25231     {
25232       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25233         perform_or_defer_access_check (chk->binfo,
25234                                        chk->decl,
25235                                        chk->diag_decl, tf_warning_or_error);
25236     }
25237   /* Set the scope from the stored value.  */
25238   parser->scope = check_value->value;
25239   parser->qualifying_scope = check_value->qualifying_scope;
25240   parser->object_scope = NULL_TREE;
25241 }
25242
25243 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
25244    encounter the end of a block before what we were looking for.  */
25245
25246 static bool
25247 cp_parser_cache_group (cp_parser *parser,
25248                        enum cpp_ttype end,
25249                        unsigned depth)
25250 {
25251   while (true)
25252     {
25253       cp_token *token = cp_lexer_peek_token (parser->lexer);
25254
25255       /* Abort a parenthesized expression if we encounter a semicolon.  */
25256       if ((end == CPP_CLOSE_PAREN || depth == 0)
25257           && token->type == CPP_SEMICOLON)
25258         return true;
25259       /* If we've reached the end of the file, stop.  */
25260       if (token->type == CPP_EOF
25261           || (end != CPP_PRAGMA_EOL
25262               && token->type == CPP_PRAGMA_EOL))
25263         return true;
25264       if (token->type == CPP_CLOSE_BRACE && depth == 0)
25265         /* We've hit the end of an enclosing block, so there's been some
25266            kind of syntax error.  */
25267         return true;
25268
25269       /* Consume the token.  */
25270       cp_lexer_consume_token (parser->lexer);
25271       /* See if it starts a new group.  */
25272       if (token->type == CPP_OPEN_BRACE)
25273         {
25274           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25275           /* In theory this should probably check end == '}', but
25276              cp_parser_save_member_function_body needs it to exit
25277              after either '}' or ')' when called with ')'.  */
25278           if (depth == 0)
25279             return false;
25280         }
25281       else if (token->type == CPP_OPEN_PAREN)
25282         {
25283           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25284           if (depth == 0 && end == CPP_CLOSE_PAREN)
25285             return false;
25286         }
25287       else if (token->type == CPP_PRAGMA)
25288         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25289       else if (token->type == end)
25290         return false;
25291     }
25292 }
25293
25294 /* Like above, for caching a default argument or NSDMI.  Both of these are
25295    terminated by a non-nested comma, but it can be unclear whether or not a
25296    comma is nested in a template argument list unless we do more parsing.
25297    In order to handle this ambiguity, when we encounter a ',' after a '<'
25298    we try to parse what follows as a parameter-declaration-list (in the
25299    case of a default argument) or a member-declarator (in the case of an
25300    NSDMI).  If that succeeds, then we stop caching.  */
25301
25302 static tree
25303 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25304 {
25305   unsigned depth = 0;
25306   int maybe_template_id = 0;
25307   cp_token *first_token;
25308   cp_token *token;
25309   tree default_argument;
25310
25311   /* Add tokens until we have processed the entire default
25312      argument.  We add the range [first_token, token).  */
25313   first_token = cp_lexer_peek_token (parser->lexer);
25314   if (first_token->type == CPP_OPEN_BRACE)
25315     {
25316       /* For list-initialization, this is straightforward.  */
25317       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25318       token = cp_lexer_peek_token (parser->lexer);
25319     }
25320   else while (true)
25321     {
25322       bool done = false;
25323
25324       /* Peek at the next token.  */
25325       token = cp_lexer_peek_token (parser->lexer);
25326       /* What we do depends on what token we have.  */
25327       switch (token->type)
25328         {
25329           /* In valid code, a default argument must be
25330              immediately followed by a `,' `)', or `...'.  */
25331         case CPP_COMMA:
25332           if (depth == 0 && maybe_template_id)
25333             {
25334               /* If we've seen a '<', we might be in a
25335                  template-argument-list.  Until Core issue 325 is
25336                  resolved, we don't know how this situation ought
25337                  to be handled, so try to DTRT.  We check whether
25338                  what comes after the comma is a valid parameter
25339                  declaration list.  If it is, then the comma ends
25340                  the default argument; otherwise the default
25341                  argument continues.  */
25342               bool error = false;
25343
25344               /* Set ITALP so cp_parser_parameter_declaration_list
25345                  doesn't decide to commit to this parse.  */
25346               bool saved_italp = parser->in_template_argument_list_p;
25347               parser->in_template_argument_list_p = true;
25348
25349               cp_parser_parse_tentatively (parser);
25350               cp_lexer_consume_token (parser->lexer);
25351
25352               if (nsdmi)
25353                 {
25354                   int ctor_dtor_or_conv_p;
25355                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25356                                         &ctor_dtor_or_conv_p,
25357                                         /*parenthesized_p=*/NULL,
25358                                         /*member_p=*/true,
25359                                         /*friend_p=*/false);
25360                 }
25361               else
25362                 {
25363                   begin_scope (sk_function_parms, NULL_TREE);
25364                   cp_parser_parameter_declaration_list (parser, &error);
25365                   pop_bindings_and_leave_scope ();
25366                 }
25367               if (!cp_parser_error_occurred (parser) && !error)
25368                 done = true;
25369               cp_parser_abort_tentative_parse (parser);
25370
25371               parser->in_template_argument_list_p = saved_italp;
25372               break;
25373             }
25374         case CPP_CLOSE_PAREN:
25375         case CPP_ELLIPSIS:
25376           /* If we run into a non-nested `;', `}', or `]',
25377              then the code is invalid -- but the default
25378              argument is certainly over.  */
25379         case CPP_SEMICOLON:
25380         case CPP_CLOSE_BRACE:
25381         case CPP_CLOSE_SQUARE:
25382           if (depth == 0
25383               /* Handle correctly int n = sizeof ... ( p );  */
25384               && token->type != CPP_ELLIPSIS)
25385             done = true;
25386           /* Update DEPTH, if necessary.  */
25387           else if (token->type == CPP_CLOSE_PAREN
25388                    || token->type == CPP_CLOSE_BRACE
25389                    || token->type == CPP_CLOSE_SQUARE)
25390             --depth;
25391           break;
25392
25393         case CPP_OPEN_PAREN:
25394         case CPP_OPEN_SQUARE:
25395         case CPP_OPEN_BRACE:
25396           ++depth;
25397           break;
25398
25399         case CPP_LESS:
25400           if (depth == 0)
25401             /* This might be the comparison operator, or it might
25402                start a template argument list.  */
25403             ++maybe_template_id;
25404           break;
25405
25406         case CPP_RSHIFT:
25407           if (cxx_dialect == cxx98)
25408             break;
25409           /* Fall through for C++0x, which treats the `>>'
25410              operator like two `>' tokens in certain
25411              cases.  */
25412
25413         case CPP_GREATER:
25414           if (depth == 0)
25415             {
25416               /* This might be an operator, or it might close a
25417                  template argument list.  But if a previous '<'
25418                  started a template argument list, this will have
25419                  closed it, so we can't be in one anymore.  */
25420               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25421               if (maybe_template_id < 0)
25422                 maybe_template_id = 0;
25423             }
25424           break;
25425
25426           /* If we run out of tokens, issue an error message.  */
25427         case CPP_EOF:
25428         case CPP_PRAGMA_EOL:
25429           error_at (token->location, "file ends in default argument");
25430           done = true;
25431           break;
25432
25433         case CPP_NAME:
25434         case CPP_SCOPE:
25435           /* In these cases, we should look for template-ids.
25436              For example, if the default argument is
25437              `X<int, double>()', we need to do name lookup to
25438              figure out whether or not `X' is a template; if
25439              so, the `,' does not end the default argument.
25440
25441              That is not yet done.  */
25442           break;
25443
25444         default:
25445           break;
25446         }
25447
25448       /* If we've reached the end, stop.  */
25449       if (done)
25450         break;
25451
25452       /* Add the token to the token block.  */
25453       token = cp_lexer_consume_token (parser->lexer);
25454     }
25455
25456   /* Create a DEFAULT_ARG to represent the unparsed default
25457      argument.  */
25458   default_argument = make_node (DEFAULT_ARG);
25459   DEFARG_TOKENS (default_argument)
25460     = cp_token_cache_new (first_token, token);
25461   DEFARG_INSTANTIATIONS (default_argument) = NULL;
25462
25463   return default_argument;
25464 }
25465
25466 /* Begin parsing tentatively.  We always save tokens while parsing
25467    tentatively so that if the tentative parsing fails we can restore the
25468    tokens.  */
25469
25470 static void
25471 cp_parser_parse_tentatively (cp_parser* parser)
25472 {
25473   /* Enter a new parsing context.  */
25474   parser->context = cp_parser_context_new (parser->context);
25475   /* Begin saving tokens.  */
25476   cp_lexer_save_tokens (parser->lexer);
25477   /* In order to avoid repetitive access control error messages,
25478      access checks are queued up until we are no longer parsing
25479      tentatively.  */
25480   push_deferring_access_checks (dk_deferred);
25481 }
25482
25483 /* Commit to the currently active tentative parse.  */
25484
25485 static void
25486 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25487 {
25488   cp_parser_context *context;
25489   cp_lexer *lexer;
25490
25491   /* Mark all of the levels as committed.  */
25492   lexer = parser->lexer;
25493   for (context = parser->context; context->next; context = context->next)
25494     {
25495       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25496         break;
25497       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25498       while (!cp_lexer_saving_tokens (lexer))
25499         lexer = lexer->next;
25500       cp_lexer_commit_tokens (lexer);
25501     }
25502 }
25503
25504 /* Commit to the topmost currently active tentative parse.
25505
25506    Note that this function shouldn't be called when there are
25507    irreversible side-effects while in a tentative state.  For
25508    example, we shouldn't create a permanent entry in the symbol
25509    table, or issue an error message that might not apply if the
25510    tentative parse is aborted.  */
25511
25512 static void
25513 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25514 {
25515   cp_parser_context *context = parser->context;
25516   cp_lexer *lexer = parser->lexer;
25517
25518   if (context)
25519     {
25520       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25521         return;
25522       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25523
25524       while (!cp_lexer_saving_tokens (lexer))
25525         lexer = lexer->next;
25526       cp_lexer_commit_tokens (lexer);
25527     }
25528 }
25529
25530 /* Abort the currently active tentative parse.  All consumed tokens
25531    will be rolled back, and no diagnostics will be issued.  */
25532
25533 static void
25534 cp_parser_abort_tentative_parse (cp_parser* parser)
25535 {
25536   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25537               || errorcount > 0);
25538   cp_parser_simulate_error (parser);
25539   /* Now, pretend that we want to see if the construct was
25540      successfully parsed.  */
25541   cp_parser_parse_definitely (parser);
25542 }
25543
25544 /* Stop parsing tentatively.  If a parse error has occurred, restore the
25545    token stream.  Otherwise, commit to the tokens we have consumed.
25546    Returns true if no error occurred; false otherwise.  */
25547
25548 static bool
25549 cp_parser_parse_definitely (cp_parser* parser)
25550 {
25551   bool error_occurred;
25552   cp_parser_context *context;
25553
25554   /* Remember whether or not an error occurred, since we are about to
25555      destroy that information.  */
25556   error_occurred = cp_parser_error_occurred (parser);
25557   /* Remove the topmost context from the stack.  */
25558   context = parser->context;
25559   parser->context = context->next;
25560   /* If no parse errors occurred, commit to the tentative parse.  */
25561   if (!error_occurred)
25562     {
25563       /* Commit to the tokens read tentatively, unless that was
25564          already done.  */
25565       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25566         cp_lexer_commit_tokens (parser->lexer);
25567
25568       pop_to_parent_deferring_access_checks ();
25569     }
25570   /* Otherwise, if errors occurred, roll back our state so that things
25571      are just as they were before we began the tentative parse.  */
25572   else
25573     {
25574       cp_lexer_rollback_tokens (parser->lexer);
25575       pop_deferring_access_checks ();
25576     }
25577   /* Add the context to the front of the free list.  */
25578   context->next = cp_parser_context_free_list;
25579   cp_parser_context_free_list = context;
25580
25581   return !error_occurred;
25582 }
25583
25584 /* Returns true if we are parsing tentatively and are not committed to
25585    this tentative parse.  */
25586
25587 static bool
25588 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25589 {
25590   return (cp_parser_parsing_tentatively (parser)
25591           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25592 }
25593
25594 /* Returns nonzero iff an error has occurred during the most recent
25595    tentative parse.  */
25596
25597 static bool
25598 cp_parser_error_occurred (cp_parser* parser)
25599 {
25600   return (cp_parser_parsing_tentatively (parser)
25601           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25602 }
25603
25604 /* Returns nonzero if GNU extensions are allowed.  */
25605
25606 static bool
25607 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25608 {
25609   return parser->allow_gnu_extensions_p;
25610 }
25611 \f
25612 /* Objective-C++ Productions */
25613
25614
25615 /* Parse an Objective-C expression, which feeds into a primary-expression
25616    above.
25617
25618    objc-expression:
25619      objc-message-expression
25620      objc-string-literal
25621      objc-encode-expression
25622      objc-protocol-expression
25623      objc-selector-expression
25624
25625   Returns a tree representation of the expression.  */
25626
25627 static tree
25628 cp_parser_objc_expression (cp_parser* parser)
25629 {
25630   /* Try to figure out what kind of declaration is present.  */
25631   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25632
25633   switch (kwd->type)
25634     {
25635     case CPP_OPEN_SQUARE:
25636       return cp_parser_objc_message_expression (parser);
25637
25638     case CPP_OBJC_STRING:
25639       kwd = cp_lexer_consume_token (parser->lexer);
25640       return objc_build_string_object (kwd->u.value);
25641
25642     case CPP_KEYWORD:
25643       switch (kwd->keyword)
25644         {
25645         case RID_AT_ENCODE:
25646           return cp_parser_objc_encode_expression (parser);
25647
25648         case RID_AT_PROTOCOL:
25649           return cp_parser_objc_protocol_expression (parser);
25650
25651         case RID_AT_SELECTOR:
25652           return cp_parser_objc_selector_expression (parser);
25653
25654         default:
25655           break;
25656         }
25657     default:
25658       error_at (kwd->location,
25659                 "misplaced %<@%D%> Objective-C++ construct",
25660                 kwd->u.value);
25661       cp_parser_skip_to_end_of_block_or_statement (parser);
25662     }
25663
25664   return error_mark_node;
25665 }
25666
25667 /* Parse an Objective-C message expression.
25668
25669    objc-message-expression:
25670      [ objc-message-receiver objc-message-args ]
25671
25672    Returns a representation of an Objective-C message.  */
25673
25674 static tree
25675 cp_parser_objc_message_expression (cp_parser* parser)
25676 {
25677   tree receiver, messageargs;
25678
25679   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
25680   receiver = cp_parser_objc_message_receiver (parser);
25681   messageargs = cp_parser_objc_message_args (parser);
25682   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25683
25684   return objc_build_message_expr (receiver, messageargs);
25685 }
25686
25687 /* Parse an objc-message-receiver.
25688
25689    objc-message-receiver:
25690      expression
25691      simple-type-specifier
25692
25693   Returns a representation of the type or expression.  */
25694
25695 static tree
25696 cp_parser_objc_message_receiver (cp_parser* parser)
25697 {
25698   tree rcv;
25699
25700   /* An Objective-C message receiver may be either (1) a type
25701      or (2) an expression.  */
25702   cp_parser_parse_tentatively (parser);
25703   rcv = cp_parser_expression (parser);
25704
25705   /* If that worked out, fine.  */
25706   if (cp_parser_parse_definitely (parser))
25707     return rcv;
25708
25709   cp_parser_parse_tentatively (parser);
25710   rcv = cp_parser_simple_type_specifier (parser,
25711                                          /*decl_specs=*/NULL,
25712                                          CP_PARSER_FLAGS_NONE);
25713
25714   if (cp_parser_parse_definitely (parser))
25715     return objc_get_class_reference (rcv);
25716   
25717   cp_parser_error (parser, "objective-c++ message receiver expected");
25718   return error_mark_node;
25719 }
25720
25721 /* Parse the arguments and selectors comprising an Objective-C message.
25722
25723    objc-message-args:
25724      objc-selector
25725      objc-selector-args
25726      objc-selector-args , objc-comma-args
25727
25728    objc-selector-args:
25729      objc-selector [opt] : assignment-expression
25730      objc-selector-args objc-selector [opt] : assignment-expression
25731
25732    objc-comma-args:
25733      assignment-expression
25734      objc-comma-args , assignment-expression
25735
25736    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25737    selector arguments and TREE_VALUE containing a list of comma
25738    arguments.  */
25739
25740 static tree
25741 cp_parser_objc_message_args (cp_parser* parser)
25742 {
25743   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25744   bool maybe_unary_selector_p = true;
25745   cp_token *token = cp_lexer_peek_token (parser->lexer);
25746
25747   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25748     {
25749       tree selector = NULL_TREE, arg;
25750
25751       if (token->type != CPP_COLON)
25752         selector = cp_parser_objc_selector (parser);
25753
25754       /* Detect if we have a unary selector.  */
25755       if (maybe_unary_selector_p
25756           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25757         return build_tree_list (selector, NULL_TREE);
25758
25759       maybe_unary_selector_p = false;
25760       cp_parser_require (parser, CPP_COLON, RT_COLON);
25761       arg = cp_parser_assignment_expression (parser);
25762
25763       sel_args
25764         = chainon (sel_args,
25765                    build_tree_list (selector, arg));
25766
25767       token = cp_lexer_peek_token (parser->lexer);
25768     }
25769
25770   /* Handle non-selector arguments, if any. */
25771   while (token->type == CPP_COMMA)
25772     {
25773       tree arg;
25774
25775       cp_lexer_consume_token (parser->lexer);
25776       arg = cp_parser_assignment_expression (parser);
25777
25778       addl_args
25779         = chainon (addl_args,
25780                    build_tree_list (NULL_TREE, arg));
25781
25782       token = cp_lexer_peek_token (parser->lexer);
25783     }
25784
25785   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25786     {
25787       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25788       return build_tree_list (error_mark_node, error_mark_node);
25789     }
25790
25791   return build_tree_list (sel_args, addl_args);
25792 }
25793
25794 /* Parse an Objective-C encode expression.
25795
25796    objc-encode-expression:
25797      @encode objc-typename
25798
25799    Returns an encoded representation of the type argument.  */
25800
25801 static tree
25802 cp_parser_objc_encode_expression (cp_parser* parser)
25803 {
25804   tree type;
25805   cp_token *token;
25806
25807   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
25808   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25809   token = cp_lexer_peek_token (parser->lexer);
25810   type = complete_type (cp_parser_type_id (parser));
25811   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25812
25813   if (!type)
25814     {
25815       error_at (token->location, 
25816                 "%<@encode%> must specify a type as an argument");
25817       return error_mark_node;
25818     }
25819
25820   /* This happens if we find @encode(T) (where T is a template
25821      typename or something dependent on a template typename) when
25822      parsing a template.  In that case, we can't compile it
25823      immediately, but we rather create an AT_ENCODE_EXPR which will
25824      need to be instantiated when the template is used.
25825   */
25826   if (dependent_type_p (type))
25827     {
25828       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25829       TREE_READONLY (value) = 1;
25830       return value;
25831     }
25832
25833   return objc_build_encode_expr (type);
25834 }
25835
25836 /* Parse an Objective-C @defs expression.  */
25837
25838 static tree
25839 cp_parser_objc_defs_expression (cp_parser *parser)
25840 {
25841   tree name;
25842
25843   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
25844   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25845   name = cp_parser_identifier (parser);
25846   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25847
25848   return objc_get_class_ivars (name);
25849 }
25850
25851 /* Parse an Objective-C protocol expression.
25852
25853   objc-protocol-expression:
25854     @protocol ( identifier )
25855
25856   Returns a representation of the protocol expression.  */
25857
25858 static tree
25859 cp_parser_objc_protocol_expression (cp_parser* parser)
25860 {
25861   tree proto;
25862
25863   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
25864   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25865   proto = cp_parser_identifier (parser);
25866   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25867
25868   return objc_build_protocol_expr (proto);
25869 }
25870
25871 /* Parse an Objective-C selector expression.
25872
25873    objc-selector-expression:
25874      @selector ( objc-method-signature )
25875
25876    objc-method-signature:
25877      objc-selector
25878      objc-selector-seq
25879
25880    objc-selector-seq:
25881      objc-selector :
25882      objc-selector-seq objc-selector :
25883
25884   Returns a representation of the method selector.  */
25885
25886 static tree
25887 cp_parser_objc_selector_expression (cp_parser* parser)
25888 {
25889   tree sel_seq = NULL_TREE;
25890   bool maybe_unary_selector_p = true;
25891   cp_token *token;
25892   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25893
25894   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
25895   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25896   token = cp_lexer_peek_token (parser->lexer);
25897
25898   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25899          || token->type == CPP_SCOPE)
25900     {
25901       tree selector = NULL_TREE;
25902
25903       if (token->type != CPP_COLON
25904           || token->type == CPP_SCOPE)
25905         selector = cp_parser_objc_selector (parser);
25906
25907       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25908           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25909         {
25910           /* Detect if we have a unary selector.  */
25911           if (maybe_unary_selector_p)
25912             {
25913               sel_seq = selector;
25914               goto finish_selector;
25915             }
25916           else
25917             {
25918               cp_parser_error (parser, "expected %<:%>");
25919             }
25920         }
25921       maybe_unary_selector_p = false;
25922       token = cp_lexer_consume_token (parser->lexer);
25923
25924       if (token->type == CPP_SCOPE)
25925         {
25926           sel_seq
25927             = chainon (sel_seq,
25928                        build_tree_list (selector, NULL_TREE));
25929           sel_seq
25930             = chainon (sel_seq,
25931                        build_tree_list (NULL_TREE, NULL_TREE));
25932         }
25933       else
25934         sel_seq
25935           = chainon (sel_seq,
25936                      build_tree_list (selector, NULL_TREE));
25937
25938       token = cp_lexer_peek_token (parser->lexer);
25939     }
25940
25941  finish_selector:
25942   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25943
25944   return objc_build_selector_expr (loc, sel_seq);
25945 }
25946
25947 /* Parse a list of identifiers.
25948
25949    objc-identifier-list:
25950      identifier
25951      objc-identifier-list , identifier
25952
25953    Returns a TREE_LIST of identifier nodes.  */
25954
25955 static tree
25956 cp_parser_objc_identifier_list (cp_parser* parser)
25957 {
25958   tree identifier;
25959   tree list;
25960   cp_token *sep;
25961
25962   identifier = cp_parser_identifier (parser);
25963   if (identifier == error_mark_node)
25964     return error_mark_node;      
25965
25966   list = build_tree_list (NULL_TREE, identifier);
25967   sep = cp_lexer_peek_token (parser->lexer);
25968
25969   while (sep->type == CPP_COMMA)
25970     {
25971       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
25972       identifier = cp_parser_identifier (parser);
25973       if (identifier == error_mark_node)
25974         return list;
25975
25976       list = chainon (list, build_tree_list (NULL_TREE,
25977                                              identifier));
25978       sep = cp_lexer_peek_token (parser->lexer);
25979     }
25980   
25981   return list;
25982 }
25983
25984 /* Parse an Objective-C alias declaration.
25985
25986    objc-alias-declaration:
25987      @compatibility_alias identifier identifier ;
25988
25989    This function registers the alias mapping with the Objective-C front end.
25990    It returns nothing.  */
25991
25992 static void
25993 cp_parser_objc_alias_declaration (cp_parser* parser)
25994 {
25995   tree alias, orig;
25996
25997   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
25998   alias = cp_parser_identifier (parser);
25999   orig = cp_parser_identifier (parser);
26000   objc_declare_alias (alias, orig);
26001   cp_parser_consume_semicolon_at_end_of_statement (parser);
26002 }
26003
26004 /* Parse an Objective-C class forward-declaration.
26005
26006    objc-class-declaration:
26007      @class objc-identifier-list ;
26008
26009    The function registers the forward declarations with the Objective-C
26010    front end.  It returns nothing.  */
26011
26012 static void
26013 cp_parser_objc_class_declaration (cp_parser* parser)
26014 {
26015   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
26016   while (true)
26017     {
26018       tree id;
26019       
26020       id = cp_parser_identifier (parser);
26021       if (id == error_mark_node)
26022         break;
26023       
26024       objc_declare_class (id);
26025
26026       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26027         cp_lexer_consume_token (parser->lexer);
26028       else
26029         break;
26030     }
26031   cp_parser_consume_semicolon_at_end_of_statement (parser);
26032 }
26033
26034 /* Parse a list of Objective-C protocol references.
26035
26036    objc-protocol-refs-opt:
26037      objc-protocol-refs [opt]
26038
26039    objc-protocol-refs:
26040      < objc-identifier-list >
26041
26042    Returns a TREE_LIST of identifiers, if any.  */
26043
26044 static tree
26045 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26046 {
26047   tree protorefs = NULL_TREE;
26048
26049   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26050     {
26051       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
26052       protorefs = cp_parser_objc_identifier_list (parser);
26053       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26054     }
26055
26056   return protorefs;
26057 }
26058
26059 /* Parse a Objective-C visibility specification.  */
26060
26061 static void
26062 cp_parser_objc_visibility_spec (cp_parser* parser)
26063 {
26064   cp_token *vis = cp_lexer_peek_token (parser->lexer);
26065
26066   switch (vis->keyword)
26067     {
26068     case RID_AT_PRIVATE:
26069       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26070       break;
26071     case RID_AT_PROTECTED:
26072       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26073       break;
26074     case RID_AT_PUBLIC:
26075       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26076       break;
26077     case RID_AT_PACKAGE:
26078       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26079       break;
26080     default:
26081       return;
26082     }
26083
26084   /* Eat '@private'/'@protected'/'@public'.  */
26085   cp_lexer_consume_token (parser->lexer);
26086 }
26087
26088 /* Parse an Objective-C method type.  Return 'true' if it is a class
26089    (+) method, and 'false' if it is an instance (-) method.  */
26090
26091 static inline bool
26092 cp_parser_objc_method_type (cp_parser* parser)
26093 {
26094   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26095     return true;
26096   else
26097     return false;
26098 }
26099
26100 /* Parse an Objective-C protocol qualifier.  */
26101
26102 static tree
26103 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26104 {
26105   tree quals = NULL_TREE, node;
26106   cp_token *token = cp_lexer_peek_token (parser->lexer);
26107
26108   node = token->u.value;
26109
26110   while (node && identifier_p (node)
26111          && (node == ridpointers [(int) RID_IN]
26112              || node == ridpointers [(int) RID_OUT]
26113              || node == ridpointers [(int) RID_INOUT]
26114              || node == ridpointers [(int) RID_BYCOPY]
26115              || node == ridpointers [(int) RID_BYREF]
26116              || node == ridpointers [(int) RID_ONEWAY]))
26117     {
26118       quals = tree_cons (NULL_TREE, node, quals);
26119       cp_lexer_consume_token (parser->lexer);
26120       token = cp_lexer_peek_token (parser->lexer);
26121       node = token->u.value;
26122     }
26123
26124   return quals;
26125 }
26126
26127 /* Parse an Objective-C typename.  */
26128
26129 static tree
26130 cp_parser_objc_typename (cp_parser* parser)
26131 {
26132   tree type_name = NULL_TREE;
26133
26134   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26135     {
26136       tree proto_quals, cp_type = NULL_TREE;
26137
26138       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
26139       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26140
26141       /* An ObjC type name may consist of just protocol qualifiers, in which
26142          case the type shall default to 'id'.  */
26143       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26144         {
26145           cp_type = cp_parser_type_id (parser);
26146           
26147           /* If the type could not be parsed, an error has already
26148              been produced.  For error recovery, behave as if it had
26149              not been specified, which will use the default type
26150              'id'.  */
26151           if (cp_type == error_mark_node)
26152             {
26153               cp_type = NULL_TREE;
26154               /* We need to skip to the closing parenthesis as
26155                  cp_parser_type_id() does not seem to do it for
26156                  us.  */
26157               cp_parser_skip_to_closing_parenthesis (parser,
26158                                                      /*recovering=*/true,
26159                                                      /*or_comma=*/false,
26160                                                      /*consume_paren=*/false);
26161             }
26162         }
26163
26164       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26165       type_name = build_tree_list (proto_quals, cp_type);
26166     }
26167
26168   return type_name;
26169 }
26170
26171 /* Check to see if TYPE refers to an Objective-C selector name.  */
26172
26173 static bool
26174 cp_parser_objc_selector_p (enum cpp_ttype type)
26175 {
26176   return (type == CPP_NAME || type == CPP_KEYWORD
26177           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26178           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26179           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26180           || type == CPP_XOR || type == CPP_XOR_EQ);
26181 }
26182
26183 /* Parse an Objective-C selector.  */
26184
26185 static tree
26186 cp_parser_objc_selector (cp_parser* parser)
26187 {
26188   cp_token *token = cp_lexer_consume_token (parser->lexer);
26189
26190   if (!cp_parser_objc_selector_p (token->type))
26191     {
26192       error_at (token->location, "invalid Objective-C++ selector name");
26193       return error_mark_node;
26194     }
26195
26196   /* C++ operator names are allowed to appear in ObjC selectors.  */
26197   switch (token->type)
26198     {
26199     case CPP_AND_AND: return get_identifier ("and");
26200     case CPP_AND_EQ: return get_identifier ("and_eq");
26201     case CPP_AND: return get_identifier ("bitand");
26202     case CPP_OR: return get_identifier ("bitor");
26203     case CPP_COMPL: return get_identifier ("compl");
26204     case CPP_NOT: return get_identifier ("not");
26205     case CPP_NOT_EQ: return get_identifier ("not_eq");
26206     case CPP_OR_OR: return get_identifier ("or");
26207     case CPP_OR_EQ: return get_identifier ("or_eq");
26208     case CPP_XOR: return get_identifier ("xor");
26209     case CPP_XOR_EQ: return get_identifier ("xor_eq");
26210     default: return token->u.value;
26211     }
26212 }
26213
26214 /* Parse an Objective-C params list.  */
26215
26216 static tree
26217 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26218 {
26219   tree params = NULL_TREE;
26220   bool maybe_unary_selector_p = true;
26221   cp_token *token = cp_lexer_peek_token (parser->lexer);
26222
26223   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26224     {
26225       tree selector = NULL_TREE, type_name, identifier;
26226       tree parm_attr = NULL_TREE;
26227
26228       if (token->keyword == RID_ATTRIBUTE)
26229         break;
26230
26231       if (token->type != CPP_COLON)
26232         selector = cp_parser_objc_selector (parser);
26233
26234       /* Detect if we have a unary selector.  */
26235       if (maybe_unary_selector_p
26236           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26237         {
26238           params = selector; /* Might be followed by attributes.  */
26239           break;
26240         }
26241
26242       maybe_unary_selector_p = false;
26243       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26244         {
26245           /* Something went quite wrong.  There should be a colon
26246              here, but there is not.  Stop parsing parameters.  */
26247           break;
26248         }
26249       type_name = cp_parser_objc_typename (parser);
26250       /* New ObjC allows attributes on parameters too.  */
26251       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26252         parm_attr = cp_parser_attributes_opt (parser);
26253       identifier = cp_parser_identifier (parser);
26254
26255       params
26256         = chainon (params,
26257                    objc_build_keyword_decl (selector,
26258                                             type_name,
26259                                             identifier,
26260                                             parm_attr));
26261
26262       token = cp_lexer_peek_token (parser->lexer);
26263     }
26264
26265   if (params == NULL_TREE)
26266     {
26267       cp_parser_error (parser, "objective-c++ method declaration is expected");
26268       return error_mark_node;
26269     }
26270
26271   /* We allow tail attributes for the method.  */
26272   if (token->keyword == RID_ATTRIBUTE)
26273     {
26274       *attributes = cp_parser_attributes_opt (parser);
26275       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26276           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26277         return params;
26278       cp_parser_error (parser, 
26279                        "method attributes must be specified at the end");
26280       return error_mark_node;
26281     }
26282
26283   if (params == NULL_TREE)
26284     {
26285       cp_parser_error (parser, "objective-c++ method declaration is expected");
26286       return error_mark_node;
26287     }
26288   return params;
26289 }
26290
26291 /* Parse the non-keyword Objective-C params.  */
26292
26293 static tree
26294 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
26295                                        tree* attributes)
26296 {
26297   tree params = make_node (TREE_LIST);
26298   cp_token *token = cp_lexer_peek_token (parser->lexer);
26299   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
26300
26301   while (token->type == CPP_COMMA)
26302     {
26303       cp_parameter_declarator *parmdecl;
26304       tree parm;
26305
26306       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26307       token = cp_lexer_peek_token (parser->lexer);
26308
26309       if (token->type == CPP_ELLIPSIS)
26310         {
26311           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
26312           *ellipsisp = true;
26313           token = cp_lexer_peek_token (parser->lexer);
26314           break;
26315         }
26316
26317       /* TODO: parse attributes for tail parameters.  */
26318       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26319       parm = grokdeclarator (parmdecl->declarator,
26320                              &parmdecl->decl_specifiers,
26321                              PARM, /*initialized=*/0,
26322                              /*attrlist=*/NULL);
26323
26324       chainon (params, build_tree_list (NULL_TREE, parm));
26325       token = cp_lexer_peek_token (parser->lexer);
26326     }
26327
26328   /* We allow tail attributes for the method.  */
26329   if (token->keyword == RID_ATTRIBUTE)
26330     {
26331       if (*attributes == NULL_TREE)
26332         {
26333           *attributes = cp_parser_attributes_opt (parser);
26334           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26335               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26336             return params;
26337         }
26338       else        
26339         /* We have an error, but parse the attributes, so that we can 
26340            carry on.  */
26341         *attributes = cp_parser_attributes_opt (parser);
26342
26343       cp_parser_error (parser, 
26344                        "method attributes must be specified at the end");
26345       return error_mark_node;
26346     }
26347
26348   return params;
26349 }
26350
26351 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
26352
26353 static void
26354 cp_parser_objc_interstitial_code (cp_parser* parser)
26355 {
26356   cp_token *token = cp_lexer_peek_token (parser->lexer);
26357
26358   /* If the next token is `extern' and the following token is a string
26359      literal, then we have a linkage specification.  */
26360   if (token->keyword == RID_EXTERN
26361       && cp_parser_is_pure_string_literal
26362          (cp_lexer_peek_nth_token (parser->lexer, 2)))
26363     cp_parser_linkage_specification (parser);
26364   /* Handle #pragma, if any.  */
26365   else if (token->type == CPP_PRAGMA)
26366     cp_parser_pragma (parser, pragma_objc_icode);
26367   /* Allow stray semicolons.  */
26368   else if (token->type == CPP_SEMICOLON)
26369     cp_lexer_consume_token (parser->lexer);
26370   /* Mark methods as optional or required, when building protocols.  */
26371   else if (token->keyword == RID_AT_OPTIONAL)
26372     {
26373       cp_lexer_consume_token (parser->lexer);
26374       objc_set_method_opt (true);
26375     }
26376   else if (token->keyword == RID_AT_REQUIRED)
26377     {
26378       cp_lexer_consume_token (parser->lexer);
26379       objc_set_method_opt (false);
26380     }
26381   else if (token->keyword == RID_NAMESPACE)
26382     cp_parser_namespace_definition (parser);
26383   /* Other stray characters must generate errors.  */
26384   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26385     {
26386       cp_lexer_consume_token (parser->lexer);
26387       error ("stray %qs between Objective-C++ methods",
26388              token->type == CPP_OPEN_BRACE ? "{" : "}");
26389     }
26390   /* Finally, try to parse a block-declaration, or a function-definition.  */
26391   else
26392     cp_parser_block_declaration (parser, /*statement_p=*/false);
26393 }
26394
26395 /* Parse a method signature.  */
26396
26397 static tree
26398 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26399 {
26400   tree rettype, kwdparms, optparms;
26401   bool ellipsis = false;
26402   bool is_class_method;
26403
26404   is_class_method = cp_parser_objc_method_type (parser);
26405   rettype = cp_parser_objc_typename (parser);
26406   *attributes = NULL_TREE;
26407   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26408   if (kwdparms == error_mark_node)
26409     return error_mark_node;
26410   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26411   if (optparms == error_mark_node)
26412     return error_mark_node;
26413
26414   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26415 }
26416
26417 static bool
26418 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26419 {
26420   tree tattr;  
26421   cp_lexer_save_tokens (parser->lexer);
26422   tattr = cp_parser_attributes_opt (parser);
26423   gcc_assert (tattr) ;
26424   
26425   /* If the attributes are followed by a method introducer, this is not allowed.
26426      Dump the attributes and flag the situation.  */
26427   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26428       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26429     return true;
26430
26431   /* Otherwise, the attributes introduce some interstitial code, possibly so
26432      rewind to allow that check.  */
26433   cp_lexer_rollback_tokens (parser->lexer);
26434   return false;  
26435 }
26436
26437 /* Parse an Objective-C method prototype list.  */
26438
26439 static void
26440 cp_parser_objc_method_prototype_list (cp_parser* parser)
26441 {
26442   cp_token *token = cp_lexer_peek_token (parser->lexer);
26443
26444   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26445     {
26446       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26447         {
26448           tree attributes, sig;
26449           bool is_class_method;
26450           if (token->type == CPP_PLUS)
26451             is_class_method = true;
26452           else
26453             is_class_method = false;
26454           sig = cp_parser_objc_method_signature (parser, &attributes);
26455           if (sig == error_mark_node)
26456             {
26457               cp_parser_skip_to_end_of_block_or_statement (parser);
26458               token = cp_lexer_peek_token (parser->lexer);
26459               continue;
26460             }
26461           objc_add_method_declaration (is_class_method, sig, attributes);
26462           cp_parser_consume_semicolon_at_end_of_statement (parser);
26463         }
26464       else if (token->keyword == RID_AT_PROPERTY)
26465         cp_parser_objc_at_property_declaration (parser);
26466       else if (token->keyword == RID_ATTRIBUTE 
26467                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26468         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
26469                     OPT_Wattributes, 
26470                     "prefix attributes are ignored for methods");
26471       else
26472         /* Allow for interspersed non-ObjC++ code.  */
26473         cp_parser_objc_interstitial_code (parser);
26474
26475       token = cp_lexer_peek_token (parser->lexer);
26476     }
26477
26478   if (token->type != CPP_EOF)
26479     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26480   else
26481     cp_parser_error (parser, "expected %<@end%>");
26482
26483   objc_finish_interface ();
26484 }
26485
26486 /* Parse an Objective-C method definition list.  */
26487
26488 static void
26489 cp_parser_objc_method_definition_list (cp_parser* parser)
26490 {
26491   cp_token *token = cp_lexer_peek_token (parser->lexer);
26492
26493   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26494     {
26495       tree meth;
26496
26497       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26498         {
26499           cp_token *ptk;
26500           tree sig, attribute;
26501           bool is_class_method;
26502           if (token->type == CPP_PLUS)
26503             is_class_method = true;
26504           else
26505             is_class_method = false;
26506           push_deferring_access_checks (dk_deferred);
26507           sig = cp_parser_objc_method_signature (parser, &attribute);
26508           if (sig == error_mark_node)
26509             {
26510               cp_parser_skip_to_end_of_block_or_statement (parser);
26511               token = cp_lexer_peek_token (parser->lexer);
26512               continue;
26513             }
26514           objc_start_method_definition (is_class_method, sig, attribute,
26515                                         NULL_TREE);
26516
26517           /* For historical reasons, we accept an optional semicolon.  */
26518           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26519             cp_lexer_consume_token (parser->lexer);
26520
26521           ptk = cp_lexer_peek_token (parser->lexer);
26522           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
26523                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26524             {
26525               perform_deferred_access_checks (tf_warning_or_error);
26526               stop_deferring_access_checks ();
26527               meth = cp_parser_function_definition_after_declarator (parser,
26528                                                                      false);
26529               pop_deferring_access_checks ();
26530               objc_finish_method_definition (meth);
26531             }
26532         }
26533       /* The following case will be removed once @synthesize is
26534          completely implemented.  */
26535       else if (token->keyword == RID_AT_PROPERTY)
26536         cp_parser_objc_at_property_declaration (parser);
26537       else if (token->keyword == RID_AT_SYNTHESIZE)
26538         cp_parser_objc_at_synthesize_declaration (parser);
26539       else if (token->keyword == RID_AT_DYNAMIC)
26540         cp_parser_objc_at_dynamic_declaration (parser);
26541       else if (token->keyword == RID_ATTRIBUTE 
26542                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26543         warning_at (token->location, OPT_Wattributes,
26544                     "prefix attributes are ignored for methods");
26545       else
26546         /* Allow for interspersed non-ObjC++ code.  */
26547         cp_parser_objc_interstitial_code (parser);
26548
26549       token = cp_lexer_peek_token (parser->lexer);
26550     }
26551
26552   if (token->type != CPP_EOF)
26553     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26554   else
26555     cp_parser_error (parser, "expected %<@end%>");
26556
26557   objc_finish_implementation ();
26558 }
26559
26560 /* Parse Objective-C ivars.  */
26561
26562 static void
26563 cp_parser_objc_class_ivars (cp_parser* parser)
26564 {
26565   cp_token *token = cp_lexer_peek_token (parser->lexer);
26566
26567   if (token->type != CPP_OPEN_BRACE)
26568     return;     /* No ivars specified.  */
26569
26570   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
26571   token = cp_lexer_peek_token (parser->lexer);
26572
26573   while (token->type != CPP_CLOSE_BRACE 
26574         && token->keyword != RID_AT_END && token->type != CPP_EOF)
26575     {
26576       cp_decl_specifier_seq declspecs;
26577       int decl_class_or_enum_p;
26578       tree prefix_attributes;
26579
26580       cp_parser_objc_visibility_spec (parser);
26581
26582       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26583         break;
26584
26585       cp_parser_decl_specifier_seq (parser,
26586                                     CP_PARSER_FLAGS_OPTIONAL,
26587                                     &declspecs,
26588                                     &decl_class_or_enum_p);
26589
26590       /* auto, register, static, extern, mutable.  */
26591       if (declspecs.storage_class != sc_none)
26592         {
26593           cp_parser_error (parser, "invalid type for instance variable");         
26594           declspecs.storage_class = sc_none;
26595         }
26596
26597       /* thread_local.  */
26598       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26599         {
26600           cp_parser_error (parser, "invalid type for instance variable");
26601           declspecs.locations[ds_thread] = 0;
26602         }
26603       
26604       /* typedef.  */
26605       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26606         {
26607           cp_parser_error (parser, "invalid type for instance variable");
26608           declspecs.locations[ds_typedef] = 0;
26609         }
26610
26611       prefix_attributes = declspecs.attributes;
26612       declspecs.attributes = NULL_TREE;
26613
26614       /* Keep going until we hit the `;' at the end of the
26615          declaration.  */
26616       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26617         {
26618           tree width = NULL_TREE, attributes, first_attribute, decl;
26619           cp_declarator *declarator = NULL;
26620           int ctor_dtor_or_conv_p;
26621
26622           /* Check for a (possibly unnamed) bitfield declaration.  */
26623           token = cp_lexer_peek_token (parser->lexer);
26624           if (token->type == CPP_COLON)
26625             goto eat_colon;
26626
26627           if (token->type == CPP_NAME
26628               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26629                   == CPP_COLON))
26630             {
26631               /* Get the name of the bitfield.  */
26632               declarator = make_id_declarator (NULL_TREE,
26633                                                cp_parser_identifier (parser),
26634                                                sfk_none);
26635
26636              eat_colon:
26637               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26638               /* Get the width of the bitfield.  */
26639               width
26640                 = cp_parser_constant_expression (parser);
26641             }
26642           else
26643             {
26644               /* Parse the declarator.  */
26645               declarator
26646                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26647                                         &ctor_dtor_or_conv_p,
26648                                         /*parenthesized_p=*/NULL,
26649                                         /*member_p=*/false,
26650                                         /*friend_p=*/false);
26651             }
26652
26653           /* Look for attributes that apply to the ivar.  */
26654           attributes = cp_parser_attributes_opt (parser);
26655           /* Remember which attributes are prefix attributes and
26656              which are not.  */
26657           first_attribute = attributes;
26658           /* Combine the attributes.  */
26659           attributes = chainon (prefix_attributes, attributes);
26660
26661           if (width)
26662               /* Create the bitfield declaration.  */
26663               decl = grokbitfield (declarator, &declspecs,
26664                                    width,
26665                                    attributes);
26666           else
26667             decl = grokfield (declarator, &declspecs,
26668                               NULL_TREE, /*init_const_expr_p=*/false,
26669                               NULL_TREE, attributes);
26670
26671           /* Add the instance variable.  */
26672           if (decl != error_mark_node && decl != NULL_TREE)
26673             objc_add_instance_variable (decl);
26674
26675           /* Reset PREFIX_ATTRIBUTES.  */
26676           while (attributes && TREE_CHAIN (attributes) != first_attribute)
26677             attributes = TREE_CHAIN (attributes);
26678           if (attributes)
26679             TREE_CHAIN (attributes) = NULL_TREE;
26680
26681           token = cp_lexer_peek_token (parser->lexer);
26682
26683           if (token->type == CPP_COMMA)
26684             {
26685               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26686               continue;
26687             }
26688           break;
26689         }
26690
26691       cp_parser_consume_semicolon_at_end_of_statement (parser);
26692       token = cp_lexer_peek_token (parser->lexer);
26693     }
26694
26695   if (token->keyword == RID_AT_END)
26696     cp_parser_error (parser, "expected %<}%>");
26697
26698   /* Do not consume the RID_AT_END, so it will be read again as terminating
26699      the @interface of @implementation.  */ 
26700   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26701     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
26702     
26703   /* For historical reasons, we accept an optional semicolon.  */
26704   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26705     cp_lexer_consume_token (parser->lexer);
26706 }
26707
26708 /* Parse an Objective-C protocol declaration.  */
26709
26710 static void
26711 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26712 {
26713   tree proto, protorefs;
26714   cp_token *tok;
26715
26716   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
26717   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26718     {
26719       tok = cp_lexer_peek_token (parser->lexer);
26720       error_at (tok->location, "identifier expected after %<@protocol%>");
26721       cp_parser_consume_semicolon_at_end_of_statement (parser);
26722       return;
26723     }
26724
26725   /* See if we have a forward declaration or a definition.  */
26726   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26727
26728   /* Try a forward declaration first.  */
26729   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26730     {
26731       while (true)
26732         {
26733           tree id;
26734           
26735           id = cp_parser_identifier (parser);
26736           if (id == error_mark_node)
26737             break;
26738           
26739           objc_declare_protocol (id, attributes);
26740           
26741           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26742             cp_lexer_consume_token (parser->lexer);
26743           else
26744             break;
26745         }
26746       cp_parser_consume_semicolon_at_end_of_statement (parser);
26747     }
26748
26749   /* Ok, we got a full-fledged definition (or at least should).  */
26750   else
26751     {
26752       proto = cp_parser_identifier (parser);
26753       protorefs = cp_parser_objc_protocol_refs_opt (parser);
26754       objc_start_protocol (proto, protorefs, attributes);
26755       cp_parser_objc_method_prototype_list (parser);
26756     }
26757 }
26758
26759 /* Parse an Objective-C superclass or category.  */
26760
26761 static void
26762 cp_parser_objc_superclass_or_category (cp_parser *parser, 
26763                                        bool iface_p,
26764                                        tree *super,
26765                                        tree *categ, bool *is_class_extension)
26766 {
26767   cp_token *next = cp_lexer_peek_token (parser->lexer);
26768
26769   *super = *categ = NULL_TREE;
26770   *is_class_extension = false;
26771   if (next->type == CPP_COLON)
26772     {
26773       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26774       *super = cp_parser_identifier (parser);
26775     }
26776   else if (next->type == CPP_OPEN_PAREN)
26777     {
26778       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
26779
26780       /* If there is no category name, and this is an @interface, we
26781          have a class extension.  */
26782       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26783         {
26784           *categ = NULL_TREE;
26785           *is_class_extension = true;
26786         }
26787       else
26788         *categ = cp_parser_identifier (parser);
26789
26790       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26791     }
26792 }
26793
26794 /* Parse an Objective-C class interface.  */
26795
26796 static void
26797 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26798 {
26799   tree name, super, categ, protos;
26800   bool is_class_extension;
26801
26802   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
26803   name = cp_parser_identifier (parser);
26804   if (name == error_mark_node)
26805     {
26806       /* It's hard to recover because even if valid @interface stuff
26807          is to follow, we can't compile it (or validate it) if we
26808          don't even know which class it refers to.  Let's assume this
26809          was a stray '@interface' token in the stream and skip it.
26810       */
26811       return;
26812     }
26813   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26814                                          &is_class_extension);
26815   protos = cp_parser_objc_protocol_refs_opt (parser);
26816
26817   /* We have either a class or a category on our hands.  */
26818   if (categ || is_class_extension)
26819     objc_start_category_interface (name, categ, protos, attributes);
26820   else
26821     {
26822       objc_start_class_interface (name, super, protos, attributes);
26823       /* Handle instance variable declarations, if any.  */
26824       cp_parser_objc_class_ivars (parser);
26825       objc_continue_interface ();
26826     }
26827
26828   cp_parser_objc_method_prototype_list (parser);
26829 }
26830
26831 /* Parse an Objective-C class implementation.  */
26832
26833 static void
26834 cp_parser_objc_class_implementation (cp_parser* parser)
26835 {
26836   tree name, super, categ;
26837   bool is_class_extension;
26838
26839   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
26840   name = cp_parser_identifier (parser);
26841   if (name == error_mark_node)
26842     {
26843       /* It's hard to recover because even if valid @implementation
26844          stuff is to follow, we can't compile it (or validate it) if
26845          we don't even know which class it refers to.  Let's assume
26846          this was a stray '@implementation' token in the stream and
26847          skip it.
26848       */
26849       return;
26850     }
26851   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26852                                          &is_class_extension);
26853
26854   /* We have either a class or a category on our hands.  */
26855   if (categ)
26856     objc_start_category_implementation (name, categ);
26857   else
26858     {
26859       objc_start_class_implementation (name, super);
26860       /* Handle instance variable declarations, if any.  */
26861       cp_parser_objc_class_ivars (parser);
26862       objc_continue_implementation ();
26863     }
26864
26865   cp_parser_objc_method_definition_list (parser);
26866 }
26867
26868 /* Consume the @end token and finish off the implementation.  */
26869
26870 static void
26871 cp_parser_objc_end_implementation (cp_parser* parser)
26872 {
26873   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26874   objc_finish_implementation ();
26875 }
26876
26877 /* Parse an Objective-C declaration.  */
26878
26879 static void
26880 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26881 {
26882   /* Try to figure out what kind of declaration is present.  */
26883   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26884
26885   if (attributes)
26886     switch (kwd->keyword)
26887       {
26888         case RID_AT_ALIAS:
26889         case RID_AT_CLASS:
26890         case RID_AT_END:
26891           error_at (kwd->location, "attributes may not be specified before"
26892                     " the %<@%D%> Objective-C++ keyword",
26893                     kwd->u.value);
26894           attributes = NULL;
26895           break;
26896         case RID_AT_IMPLEMENTATION:
26897           warning_at (kwd->location, OPT_Wattributes,
26898                       "prefix attributes are ignored before %<@%D%>",
26899                       kwd->u.value);
26900           attributes = NULL;
26901         default:
26902           break;
26903       }
26904
26905   switch (kwd->keyword)
26906     {
26907     case RID_AT_ALIAS:
26908       cp_parser_objc_alias_declaration (parser);
26909       break;
26910     case RID_AT_CLASS:
26911       cp_parser_objc_class_declaration (parser);
26912       break;
26913     case RID_AT_PROTOCOL:
26914       cp_parser_objc_protocol_declaration (parser, attributes);
26915       break;
26916     case RID_AT_INTERFACE:
26917       cp_parser_objc_class_interface (parser, attributes);
26918       break;
26919     case RID_AT_IMPLEMENTATION:
26920       cp_parser_objc_class_implementation (parser);
26921       break;
26922     case RID_AT_END:
26923       cp_parser_objc_end_implementation (parser);
26924       break;
26925     default:
26926       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26927                 kwd->u.value);
26928       cp_parser_skip_to_end_of_block_or_statement (parser);
26929     }
26930 }
26931
26932 /* Parse an Objective-C try-catch-finally statement.
26933
26934    objc-try-catch-finally-stmt:
26935      @try compound-statement objc-catch-clause-seq [opt]
26936        objc-finally-clause [opt]
26937
26938    objc-catch-clause-seq:
26939      objc-catch-clause objc-catch-clause-seq [opt]
26940
26941    objc-catch-clause:
26942      @catch ( objc-exception-declaration ) compound-statement
26943
26944    objc-finally-clause:
26945      @finally compound-statement
26946
26947    objc-exception-declaration:
26948      parameter-declaration
26949      '...'
26950
26951    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26952
26953    Returns NULL_TREE.
26954
26955    PS: This function is identical to c_parser_objc_try_catch_finally_statement
26956    for C.  Keep them in sync.  */   
26957
26958 static tree
26959 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26960 {
26961   location_t location;
26962   tree stmt;
26963
26964   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26965   location = cp_lexer_peek_token (parser->lexer)->location;
26966   objc_maybe_warn_exceptions (location);
26967   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26968      node, lest it get absorbed into the surrounding block.  */
26969   stmt = push_stmt_list ();
26970   cp_parser_compound_statement (parser, NULL, false, false);
26971   objc_begin_try_stmt (location, pop_stmt_list (stmt));
26972
26973   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26974     {
26975       cp_parameter_declarator *parm;
26976       tree parameter_declaration = error_mark_node;
26977       bool seen_open_paren = false;
26978
26979       cp_lexer_consume_token (parser->lexer);
26980       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26981         seen_open_paren = true;
26982       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26983         {
26984           /* We have "@catch (...)" (where the '...' are literally
26985              what is in the code).  Skip the '...'.
26986              parameter_declaration is set to NULL_TREE, and
26987              objc_being_catch_clauses() knows that that means
26988              '...'.  */
26989           cp_lexer_consume_token (parser->lexer);
26990           parameter_declaration = NULL_TREE;
26991         }
26992       else
26993         {
26994           /* We have "@catch (NSException *exception)" or something
26995              like that.  Parse the parameter declaration.  */
26996           parm = cp_parser_parameter_declaration (parser, false, NULL);
26997           if (parm == NULL)
26998             parameter_declaration = error_mark_node;
26999           else
27000             parameter_declaration = grokdeclarator (parm->declarator,
27001                                                     &parm->decl_specifiers,
27002                                                     PARM, /*initialized=*/0,
27003                                                     /*attrlist=*/NULL);
27004         }
27005       if (seen_open_paren)
27006         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27007       else
27008         {
27009           /* If there was no open parenthesis, we are recovering from
27010              an error, and we are trying to figure out what mistake
27011              the user has made.  */
27012
27013           /* If there is an immediate closing parenthesis, the user
27014              probably forgot the opening one (ie, they typed "@catch
27015              NSException *e)".  Parse the closing parenthesis and keep
27016              going.  */
27017           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27018             cp_lexer_consume_token (parser->lexer);
27019           
27020           /* If these is no immediate closing parenthesis, the user
27021              probably doesn't know that parenthesis are required at
27022              all (ie, they typed "@catch NSException *e").  So, just
27023              forget about the closing parenthesis and keep going.  */
27024         }
27025       objc_begin_catch_clause (parameter_declaration);
27026       cp_parser_compound_statement (parser, NULL, false, false);
27027       objc_finish_catch_clause ();
27028     }
27029   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27030     {
27031       cp_lexer_consume_token (parser->lexer);
27032       location = cp_lexer_peek_token (parser->lexer)->location;
27033       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27034          node, lest it get absorbed into the surrounding block.  */
27035       stmt = push_stmt_list ();
27036       cp_parser_compound_statement (parser, NULL, false, false);
27037       objc_build_finally_clause (location, pop_stmt_list (stmt));
27038     }
27039
27040   return objc_finish_try_stmt ();
27041 }
27042
27043 /* Parse an Objective-C synchronized statement.
27044
27045    objc-synchronized-stmt:
27046      @synchronized ( expression ) compound-statement
27047
27048    Returns NULL_TREE.  */
27049
27050 static tree
27051 cp_parser_objc_synchronized_statement (cp_parser *parser)
27052 {
27053   location_t location;
27054   tree lock, stmt;
27055
27056   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27057
27058   location = cp_lexer_peek_token (parser->lexer)->location;
27059   objc_maybe_warn_exceptions (location);
27060   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27061   lock = cp_parser_expression (parser);
27062   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27063
27064   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27065      node, lest it get absorbed into the surrounding block.  */
27066   stmt = push_stmt_list ();
27067   cp_parser_compound_statement (parser, NULL, false, false);
27068
27069   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27070 }
27071
27072 /* Parse an Objective-C throw statement.
27073
27074    objc-throw-stmt:
27075      @throw assignment-expression [opt] ;
27076
27077    Returns a constructed '@throw' statement.  */
27078
27079 static tree
27080 cp_parser_objc_throw_statement (cp_parser *parser)
27081 {
27082   tree expr = NULL_TREE;
27083   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27084
27085   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27086
27087   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27088     expr = cp_parser_expression (parser);
27089
27090   cp_parser_consume_semicolon_at_end_of_statement (parser);
27091
27092   return objc_build_throw_stmt (loc, expr);
27093 }
27094
27095 /* Parse an Objective-C statement.  */
27096
27097 static tree
27098 cp_parser_objc_statement (cp_parser * parser)
27099 {
27100   /* Try to figure out what kind of declaration is present.  */
27101   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27102
27103   switch (kwd->keyword)
27104     {
27105     case RID_AT_TRY:
27106       return cp_parser_objc_try_catch_finally_statement (parser);
27107     case RID_AT_SYNCHRONIZED:
27108       return cp_parser_objc_synchronized_statement (parser);
27109     case RID_AT_THROW:
27110       return cp_parser_objc_throw_statement (parser);
27111     default:
27112       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27113                kwd->u.value);
27114       cp_parser_skip_to_end_of_block_or_statement (parser);
27115     }
27116
27117   return error_mark_node;
27118 }
27119
27120 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
27121    look ahead to see if an objc keyword follows the attributes.  This
27122    is to detect the use of prefix attributes on ObjC @interface and 
27123    @protocol.  */
27124
27125 static bool
27126 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27127 {
27128   cp_lexer_save_tokens (parser->lexer);
27129   *attrib = cp_parser_attributes_opt (parser);
27130   gcc_assert (*attrib);
27131   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27132     {
27133       cp_lexer_commit_tokens (parser->lexer);
27134       return true;
27135     }
27136   cp_lexer_rollback_tokens (parser->lexer);
27137   return false;  
27138 }
27139
27140 /* This routine is a minimal replacement for
27141    c_parser_struct_declaration () used when parsing the list of
27142    types/names or ObjC++ properties.  For example, when parsing the
27143    code
27144
27145    @property (readonly) int a, b, c;
27146
27147    this function is responsible for parsing "int a, int b, int c" and
27148    returning the declarations as CHAIN of DECLs.
27149
27150    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
27151    similar parsing.  */
27152 static tree
27153 cp_parser_objc_struct_declaration (cp_parser *parser)
27154 {
27155   tree decls = NULL_TREE;
27156   cp_decl_specifier_seq declspecs;
27157   int decl_class_or_enum_p;
27158   tree prefix_attributes;
27159
27160   cp_parser_decl_specifier_seq (parser,
27161                                 CP_PARSER_FLAGS_NONE,
27162                                 &declspecs,
27163                                 &decl_class_or_enum_p);
27164
27165   if (declspecs.type == error_mark_node)
27166     return error_mark_node;
27167
27168   /* auto, register, static, extern, mutable.  */
27169   if (declspecs.storage_class != sc_none)
27170     {
27171       cp_parser_error (parser, "invalid type for property");
27172       declspecs.storage_class = sc_none;
27173     }
27174   
27175   /* thread_local.  */
27176   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27177     {
27178       cp_parser_error (parser, "invalid type for property");
27179       declspecs.locations[ds_thread] = 0;
27180     }
27181   
27182   /* typedef.  */
27183   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27184     {
27185       cp_parser_error (parser, "invalid type for property");
27186       declspecs.locations[ds_typedef] = 0;
27187     }
27188
27189   prefix_attributes = declspecs.attributes;
27190   declspecs.attributes = NULL_TREE;
27191
27192   /* Keep going until we hit the `;' at the end of the declaration. */
27193   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27194     {
27195       tree attributes, first_attribute, decl;
27196       cp_declarator *declarator;
27197       cp_token *token;
27198
27199       /* Parse the declarator.  */
27200       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27201                                          NULL, NULL, false, false);
27202
27203       /* Look for attributes that apply to the ivar.  */
27204       attributes = cp_parser_attributes_opt (parser);
27205       /* Remember which attributes are prefix attributes and
27206          which are not.  */
27207       first_attribute = attributes;
27208       /* Combine the attributes.  */
27209       attributes = chainon (prefix_attributes, attributes);
27210       
27211       decl = grokfield (declarator, &declspecs,
27212                         NULL_TREE, /*init_const_expr_p=*/false,
27213                         NULL_TREE, attributes);
27214
27215       if (decl == error_mark_node || decl == NULL_TREE)
27216         return error_mark_node;
27217       
27218       /* Reset PREFIX_ATTRIBUTES.  */
27219       while (attributes && TREE_CHAIN (attributes) != first_attribute)
27220         attributes = TREE_CHAIN (attributes);
27221       if (attributes)
27222         TREE_CHAIN (attributes) = NULL_TREE;
27223
27224       DECL_CHAIN (decl) = decls;
27225       decls = decl;
27226
27227       token = cp_lexer_peek_token (parser->lexer);
27228       if (token->type == CPP_COMMA)
27229         {
27230           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
27231           continue;
27232         }
27233       else
27234         break;
27235     }
27236   return decls;
27237 }
27238
27239 /* Parse an Objective-C @property declaration.  The syntax is:
27240
27241    objc-property-declaration:
27242      '@property' objc-property-attributes[opt] struct-declaration ;
27243
27244    objc-property-attributes:
27245     '(' objc-property-attribute-list ')'
27246
27247    objc-property-attribute-list:
27248      objc-property-attribute
27249      objc-property-attribute-list, objc-property-attribute
27250
27251    objc-property-attribute
27252      'getter' = identifier
27253      'setter' = identifier
27254      'readonly'
27255      'readwrite'
27256      'assign'
27257      'retain'
27258      'copy'
27259      'nonatomic'
27260
27261   For example:
27262     @property NSString *name;
27263     @property (readonly) id object;
27264     @property (retain, nonatomic, getter=getTheName) id name;
27265     @property int a, b, c;
27266
27267    PS: This function is identical to
27268    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
27269 static void 
27270 cp_parser_objc_at_property_declaration (cp_parser *parser)
27271 {
27272   /* The following variables hold the attributes of the properties as
27273      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
27274      seen.  When we see an attribute, we set them to 'true' (if they
27275      are boolean properties) or to the identifier (if they have an
27276      argument, ie, for getter and setter).  Note that here we only
27277      parse the list of attributes, check the syntax and accumulate the
27278      attributes that we find.  objc_add_property_declaration() will
27279      then process the information.  */
27280   bool property_assign = false;
27281   bool property_copy = false;
27282   tree property_getter_ident = NULL_TREE;
27283   bool property_nonatomic = false;
27284   bool property_readonly = false;
27285   bool property_readwrite = false;
27286   bool property_retain = false;
27287   tree property_setter_ident = NULL_TREE;
27288
27289   /* 'properties' is the list of properties that we read.  Usually a
27290      single one, but maybe more (eg, in "@property int a, b, c;" there
27291      are three).  */
27292   tree properties;
27293   location_t loc;
27294
27295   loc = cp_lexer_peek_token (parser->lexer)->location;
27296
27297   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
27298
27299   /* Parse the optional attribute list...  */
27300   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27301     {
27302       /* Eat the '('.  */
27303       cp_lexer_consume_token (parser->lexer);
27304
27305       while (true)
27306         {
27307           bool syntax_error = false;
27308           cp_token *token = cp_lexer_peek_token (parser->lexer);
27309           enum rid keyword;
27310
27311           if (token->type != CPP_NAME)
27312             {
27313               cp_parser_error (parser, "expected identifier");
27314               break;
27315             }
27316           keyword = C_RID_CODE (token->u.value);
27317           cp_lexer_consume_token (parser->lexer);
27318           switch (keyword)
27319             {
27320             case RID_ASSIGN:    property_assign = true;    break;
27321             case RID_COPY:      property_copy = true;      break;
27322             case RID_NONATOMIC: property_nonatomic = true; break;
27323             case RID_READONLY:  property_readonly = true;  break;
27324             case RID_READWRITE: property_readwrite = true; break;
27325             case RID_RETAIN:    property_retain = true;    break;
27326
27327             case RID_GETTER:
27328             case RID_SETTER:
27329               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27330                 {
27331                   if (keyword == RID_GETTER)
27332                     cp_parser_error (parser,
27333                                      "missing %<=%> (after %<getter%> attribute)");
27334                   else
27335                     cp_parser_error (parser,
27336                                      "missing %<=%> (after %<setter%> attribute)");
27337                   syntax_error = true;
27338                   break;
27339                 }
27340               cp_lexer_consume_token (parser->lexer); /* eat the = */
27341               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27342                 {
27343                   cp_parser_error (parser, "expected identifier");
27344                   syntax_error = true;
27345                   break;
27346                 }
27347               if (keyword == RID_SETTER)
27348                 {
27349                   if (property_setter_ident != NULL_TREE)
27350                     {
27351                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27352                       cp_lexer_consume_token (parser->lexer);
27353                     }
27354                   else
27355                     property_setter_ident = cp_parser_objc_selector (parser);
27356                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27357                     cp_parser_error (parser, "setter name must terminate with %<:%>");
27358                   else
27359                     cp_lexer_consume_token (parser->lexer);
27360                 }
27361               else
27362                 {
27363                   if (property_getter_ident != NULL_TREE)
27364                     {
27365                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27366                       cp_lexer_consume_token (parser->lexer);
27367                     }
27368                   else
27369                     property_getter_ident = cp_parser_objc_selector (parser);
27370                 }
27371               break;
27372             default:
27373               cp_parser_error (parser, "unknown property attribute");
27374               syntax_error = true;
27375               break;
27376             }
27377
27378           if (syntax_error)
27379             break;
27380
27381           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27382             cp_lexer_consume_token (parser->lexer);
27383           else
27384             break;
27385         }
27386
27387       /* FIXME: "@property (setter, assign);" will generate a spurious
27388          "error: expected â€˜)’ before â€˜,’ token".  This is because
27389          cp_parser_require, unlike the C counterpart, will produce an
27390          error even if we are in error recovery.  */
27391       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27392         {
27393           cp_parser_skip_to_closing_parenthesis (parser,
27394                                                  /*recovering=*/true,
27395                                                  /*or_comma=*/false,
27396                                                  /*consume_paren=*/true);
27397         }
27398     }
27399
27400   /* ... and the property declaration(s).  */
27401   properties = cp_parser_objc_struct_declaration (parser);
27402
27403   if (properties == error_mark_node)
27404     {
27405       cp_parser_skip_to_end_of_statement (parser);
27406       /* If the next token is now a `;', consume it.  */
27407       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27408         cp_lexer_consume_token (parser->lexer);
27409       return;
27410     }
27411
27412   if (properties == NULL_TREE)
27413     cp_parser_error (parser, "expected identifier");
27414   else
27415     {
27416       /* Comma-separated properties are chained together in
27417          reverse order; add them one by one.  */
27418       properties = nreverse (properties);
27419       
27420       for (; properties; properties = TREE_CHAIN (properties))
27421         objc_add_property_declaration (loc, copy_node (properties),
27422                                        property_readonly, property_readwrite,
27423                                        property_assign, property_retain,
27424                                        property_copy, property_nonatomic,
27425                                        property_getter_ident, property_setter_ident);
27426     }
27427   
27428   cp_parser_consume_semicolon_at_end_of_statement (parser);
27429 }
27430
27431 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
27432
27433    objc-synthesize-declaration:
27434      @synthesize objc-synthesize-identifier-list ;
27435
27436    objc-synthesize-identifier-list:
27437      objc-synthesize-identifier
27438      objc-synthesize-identifier-list, objc-synthesize-identifier
27439
27440    objc-synthesize-identifier
27441      identifier
27442      identifier = identifier
27443
27444   For example:
27445     @synthesize MyProperty;
27446     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27447
27448   PS: This function is identical to c_parser_objc_at_synthesize_declaration
27449   for C.  Keep them in sync.
27450 */
27451 static void 
27452 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27453 {
27454   tree list = NULL_TREE;
27455   location_t loc;
27456   loc = cp_lexer_peek_token (parser->lexer)->location;
27457
27458   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
27459   while (true)
27460     {
27461       tree property, ivar;
27462       property = cp_parser_identifier (parser);
27463       if (property == error_mark_node)
27464         {
27465           cp_parser_consume_semicolon_at_end_of_statement (parser);
27466           return;
27467         }
27468       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27469         {
27470           cp_lexer_consume_token (parser->lexer);
27471           ivar = cp_parser_identifier (parser);
27472           if (ivar == error_mark_node)
27473             {
27474               cp_parser_consume_semicolon_at_end_of_statement (parser);
27475               return;
27476             }
27477         }
27478       else
27479         ivar = NULL_TREE;
27480       list = chainon (list, build_tree_list (ivar, property));
27481       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27482         cp_lexer_consume_token (parser->lexer);
27483       else
27484         break;
27485     }
27486   cp_parser_consume_semicolon_at_end_of_statement (parser);
27487   objc_add_synthesize_declaration (loc, list);
27488 }
27489
27490 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
27491
27492    objc-dynamic-declaration:
27493      @dynamic identifier-list ;
27494
27495    For example:
27496      @dynamic MyProperty;
27497      @dynamic MyProperty, AnotherProperty;
27498
27499   PS: This function is identical to c_parser_objc_at_dynamic_declaration
27500   for C.  Keep them in sync.
27501 */
27502 static void 
27503 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27504 {
27505   tree list = NULL_TREE;
27506   location_t loc;
27507   loc = cp_lexer_peek_token (parser->lexer)->location;
27508
27509   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
27510   while (true)
27511     {
27512       tree property;
27513       property = cp_parser_identifier (parser);
27514       if (property == error_mark_node)
27515         {
27516           cp_parser_consume_semicolon_at_end_of_statement (parser);
27517           return;
27518         }
27519       list = chainon (list, build_tree_list (NULL, property));
27520       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27521         cp_lexer_consume_token (parser->lexer);
27522       else
27523         break;
27524     }
27525   cp_parser_consume_semicolon_at_end_of_statement (parser);
27526   objc_add_dynamic_declaration (loc, list);
27527 }
27528
27529 \f
27530 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
27531
27532 /* Returns name of the next clause.
27533    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27534    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
27535    returned and the token is consumed.  */
27536
27537 static pragma_omp_clause
27538 cp_parser_omp_clause_name (cp_parser *parser)
27539 {
27540   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27541
27542   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27543     result = PRAGMA_OMP_CLAUSE_IF;
27544   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27545     result = PRAGMA_OMP_CLAUSE_DEFAULT;
27546   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27547     result = PRAGMA_OACC_CLAUSE_DELETE;
27548   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27549     result = PRAGMA_OMP_CLAUSE_PRIVATE;
27550   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27551     result = PRAGMA_OMP_CLAUSE_FOR;
27552   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27553     {
27554       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27555       const char *p = IDENTIFIER_POINTER (id);
27556
27557       switch (p[0])
27558         {
27559         case 'a':
27560           if (!strcmp ("aligned", p))
27561             result = PRAGMA_OMP_CLAUSE_ALIGNED;
27562           else if (!strcmp ("async", p))
27563             result = PRAGMA_OACC_CLAUSE_ASYNC;
27564           break;
27565         case 'c':
27566           if (!strcmp ("collapse", p))
27567             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27568           else if (!strcmp ("copy", p))
27569             result = PRAGMA_OACC_CLAUSE_COPY;
27570           else if (!strcmp ("copyin", p))
27571             result = PRAGMA_OMP_CLAUSE_COPYIN;
27572           else if (!strcmp ("copyout", p))
27573             result = PRAGMA_OACC_CLAUSE_COPYOUT;
27574           else if (!strcmp ("copyprivate", p))
27575             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27576           else if (!strcmp ("create", p))
27577             result = PRAGMA_OACC_CLAUSE_CREATE;
27578           break;
27579         case 'd':
27580           if (!strcmp ("depend", p))
27581             result = PRAGMA_OMP_CLAUSE_DEPEND;
27582           else if (!strcmp ("device", p))
27583             result = PRAGMA_OMP_CLAUSE_DEVICE;
27584           else if (!strcmp ("deviceptr", p))
27585             result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27586           else if (!strcmp ("dist_schedule", p))
27587             result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27588           break;
27589         case 'f':
27590           if (!strcmp ("final", p))
27591             result = PRAGMA_OMP_CLAUSE_FINAL;
27592           else if (!strcmp ("firstprivate", p))
27593             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27594           else if (!strcmp ("from", p))
27595             result = PRAGMA_OMP_CLAUSE_FROM;
27596           break;
27597         case 'h':
27598           if (!strcmp ("host", p))
27599             result = PRAGMA_OACC_CLAUSE_HOST;
27600           break;
27601         case 'i':
27602           if (!strcmp ("inbranch", p))
27603             result = PRAGMA_OMP_CLAUSE_INBRANCH;
27604           break;
27605         case 'l':
27606           if (!strcmp ("lastprivate", p))
27607             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27608           else if (!strcmp ("linear", p))
27609             result = PRAGMA_OMP_CLAUSE_LINEAR;
27610           break;
27611         case 'm':
27612           if (!strcmp ("map", p))
27613             result = PRAGMA_OMP_CLAUSE_MAP;
27614           else if (!strcmp ("mergeable", p))
27615             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27616           else if (flag_cilkplus && !strcmp ("mask", p))
27617             result = PRAGMA_CILK_CLAUSE_MASK;
27618           break;
27619         case 'n':
27620           if (!strcmp ("notinbranch", p))
27621             result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27622           else if (!strcmp ("nowait", p))
27623             result = PRAGMA_OMP_CLAUSE_NOWAIT;
27624           else if (flag_cilkplus && !strcmp ("nomask", p))
27625             result = PRAGMA_CILK_CLAUSE_NOMASK;
27626           else if (!strcmp ("num_gangs", p))
27627             result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27628           else if (!strcmp ("num_teams", p))
27629             result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27630           else if (!strcmp ("num_threads", p))
27631             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27632           else if (!strcmp ("num_workers", p))
27633             result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27634           break;
27635         case 'o':
27636           if (!strcmp ("ordered", p))
27637             result = PRAGMA_OMP_CLAUSE_ORDERED;
27638           break;
27639         case 'p':
27640           if (!strcmp ("parallel", p))
27641             result = PRAGMA_OMP_CLAUSE_PARALLEL;
27642           else if (!strcmp ("present", p))
27643             result = PRAGMA_OACC_CLAUSE_PRESENT;
27644           else if (!strcmp ("present_or_copy", p)
27645                    || !strcmp ("pcopy", p))
27646             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27647           else if (!strcmp ("present_or_copyin", p)
27648                    || !strcmp ("pcopyin", p))
27649             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27650           else if (!strcmp ("present_or_copyout", p)
27651                    || !strcmp ("pcopyout", p))
27652             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27653           else if (!strcmp ("present_or_create", p)
27654                    || !strcmp ("pcreate", p))
27655             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27656           else if (!strcmp ("proc_bind", p))
27657             result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27658           break;
27659         case 'r':
27660           if (!strcmp ("reduction", p))
27661             result = PRAGMA_OMP_CLAUSE_REDUCTION;
27662           break;
27663         case 's':
27664           if (!strcmp ("safelen", p))
27665             result = PRAGMA_OMP_CLAUSE_SAFELEN;
27666           else if (!strcmp ("schedule", p))
27667             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27668           else if (!strcmp ("sections", p))
27669             result = PRAGMA_OMP_CLAUSE_SECTIONS;
27670           else if (!strcmp ("self", p))
27671             result = PRAGMA_OACC_CLAUSE_SELF;
27672           else if (!strcmp ("shared", p))
27673             result = PRAGMA_OMP_CLAUSE_SHARED;
27674           else if (!strcmp ("simdlen", p))
27675             result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27676           break;
27677         case 't':
27678           if (!strcmp ("taskgroup", p))
27679             result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27680           else if (!strcmp ("thread_limit", p))
27681             result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27682           else if (!strcmp ("to", p))
27683             result = PRAGMA_OMP_CLAUSE_TO;
27684           break;
27685         case 'u':
27686           if (!strcmp ("uniform", p))
27687             result = PRAGMA_OMP_CLAUSE_UNIFORM;
27688           else if (!strcmp ("untied", p))
27689             result = PRAGMA_OMP_CLAUSE_UNTIED;
27690           break;
27691         case 'v':
27692           if (!strcmp ("vector_length", p))
27693             result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27694           else if (flag_cilkplus && !strcmp ("vectorlength", p))
27695             result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27696           break;
27697         case 'w':
27698           if (!strcmp ("wait", p))
27699             result = PRAGMA_OACC_CLAUSE_WAIT;
27700           break;
27701         }
27702     }
27703
27704   if (result != PRAGMA_OMP_CLAUSE_NONE)
27705     cp_lexer_consume_token (parser->lexer);
27706
27707   return result;
27708 }
27709
27710 /* Validate that a clause of the given type does not already exist.  */
27711
27712 static void
27713 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27714                            const char *name, location_t location)
27715 {
27716   tree c;
27717
27718   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27719     if (OMP_CLAUSE_CODE (c) == code)
27720       {
27721         error_at (location, "too many %qs clauses", name);
27722         break;
27723       }
27724 }
27725
27726 /* OpenMP 2.5:
27727    variable-list:
27728      identifier
27729      variable-list , identifier
27730
27731    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27732    colon).  An opening parenthesis will have been consumed by the caller.
27733
27734    If KIND is nonzero, create the appropriate node and install the decl
27735    in OMP_CLAUSE_DECL and add the node to the head of the list.
27736
27737    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27738    return the list created.
27739
27740    COLON can be NULL if only closing parenthesis should end the list,
27741    or pointer to bool which will receive false if the list is terminated
27742    by closing parenthesis or true if the list is terminated by colon.  */
27743
27744 static tree
27745 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27746                                 tree list, bool *colon)
27747 {
27748   cp_token *token;
27749   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27750   if (colon)
27751     {
27752       parser->colon_corrects_to_scope_p = false;
27753       *colon = false;
27754     }
27755   while (1)
27756     {
27757       tree name, decl;
27758
27759       token = cp_lexer_peek_token (parser->lexer);
27760       name = cp_parser_id_expression (parser, /*template_p=*/false,
27761                                       /*check_dependency_p=*/true,
27762                                       /*template_p=*/NULL,
27763                                       /*declarator_p=*/false,
27764                                       /*optional_p=*/false);
27765       if (name == error_mark_node)
27766         goto skip_comma;
27767
27768       decl = cp_parser_lookup_name_simple (parser, name, token->location);
27769       if (decl == error_mark_node)
27770         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27771                                      token->location);
27772       else if (kind != 0)
27773         {
27774           switch (kind)
27775             {
27776             case OMP_CLAUSE__CACHE_:
27777               if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27778                 {
27779                   error_at (token->location, "expected %<[%>");
27780                   decl = error_mark_node;
27781                   break;
27782                 }
27783               /* FALL THROUGH.  */
27784             case OMP_CLAUSE_MAP:
27785             case OMP_CLAUSE_FROM:
27786             case OMP_CLAUSE_TO:
27787             case OMP_CLAUSE_DEPEND:
27788               while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27789                 {
27790                   tree low_bound = NULL_TREE, length = NULL_TREE;
27791
27792                   parser->colon_corrects_to_scope_p = false;
27793                   cp_lexer_consume_token (parser->lexer);
27794                   if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27795                     low_bound = cp_parser_expression (parser);
27796                   if (!colon)
27797                     parser->colon_corrects_to_scope_p
27798                       = saved_colon_corrects_to_scope_p;
27799                   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27800                     length = integer_one_node;
27801                   else
27802                     {
27803                       /* Look for `:'.  */
27804                       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27805                         goto skip_comma;
27806                       if (!cp_lexer_next_token_is (parser->lexer,
27807                                                    CPP_CLOSE_SQUARE))
27808                         length = cp_parser_expression (parser);
27809                     }
27810                   /* Look for the closing `]'.  */
27811                   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27812                                           RT_CLOSE_SQUARE))
27813                     goto skip_comma;
27814
27815                   if (kind == OMP_CLAUSE__CACHE_)
27816                     {
27817                       if (TREE_CODE (low_bound) != INTEGER_CST
27818                           && !TREE_READONLY (low_bound))
27819                         {
27820                           error_at (token->location,
27821                                         "%qD is not a constant", low_bound);
27822                           decl = error_mark_node;
27823                         }
27824
27825                       if (TREE_CODE (length) != INTEGER_CST
27826                           && !TREE_READONLY (length))
27827                         {
27828                           error_at (token->location,
27829                                         "%qD is not a constant", length);
27830                           decl = error_mark_node;
27831                         }
27832                     }
27833
27834                   decl = tree_cons (low_bound, length, decl);
27835                 }
27836               break;
27837             default:
27838               break;
27839             }
27840
27841           tree u = build_omp_clause (token->location, kind);
27842           OMP_CLAUSE_DECL (u) = decl;
27843           OMP_CLAUSE_CHAIN (u) = list;
27844           list = u;
27845         }
27846       else
27847         list = tree_cons (decl, NULL_TREE, list);
27848
27849     get_comma:
27850       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27851         break;
27852       cp_lexer_consume_token (parser->lexer);
27853     }
27854
27855   if (colon)
27856     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27857
27858   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27859     {
27860       *colon = true;
27861       cp_parser_require (parser, CPP_COLON, RT_COLON);
27862       return list;
27863     }
27864
27865   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27866     {
27867       int ending;
27868
27869       /* Try to resync to an unnested comma.  Copied from
27870          cp_parser_parenthesized_expression_list.  */
27871     skip_comma:
27872       if (colon)
27873         parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27874       ending = cp_parser_skip_to_closing_parenthesis (parser,
27875                                                       /*recovering=*/true,
27876                                                       /*or_comma=*/true,
27877                                                       /*consume_paren=*/true);
27878       if (ending < 0)
27879         goto get_comma;
27880     }
27881
27882   return list;
27883 }
27884
27885 /* Similarly, but expect leading and trailing parenthesis.  This is a very
27886    common case for omp clauses.  */
27887
27888 static tree
27889 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27890 {
27891   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27892     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27893   return list;
27894 }
27895
27896 /* OpenACC 2.0:
27897    copy ( variable-list )
27898    copyin ( variable-list )
27899    copyout ( variable-list )
27900    create ( variable-list )
27901    delete ( variable-list )
27902    present ( variable-list )
27903    present_or_copy ( variable-list )
27904      pcopy ( variable-list )
27905    present_or_copyin ( variable-list )
27906      pcopyin ( variable-list )
27907    present_or_copyout ( variable-list )
27908      pcopyout ( variable-list )
27909    present_or_create ( variable-list )
27910      pcreate ( variable-list ) */
27911
27912 static tree
27913 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27914                             tree list)
27915 {
27916   enum gomp_map_kind kind;
27917   switch (c_kind)
27918     {
27919     case PRAGMA_OACC_CLAUSE_COPY:
27920       kind = GOMP_MAP_FORCE_TOFROM;
27921       break;
27922     case PRAGMA_OACC_CLAUSE_COPYIN:
27923       kind = GOMP_MAP_FORCE_TO;
27924       break;
27925     case PRAGMA_OACC_CLAUSE_COPYOUT:
27926       kind = GOMP_MAP_FORCE_FROM;
27927       break;
27928     case PRAGMA_OACC_CLAUSE_CREATE:
27929       kind = GOMP_MAP_FORCE_ALLOC;
27930       break;
27931     case PRAGMA_OACC_CLAUSE_DELETE:
27932       kind = GOMP_MAP_FORCE_DEALLOC;
27933       break;
27934     case PRAGMA_OACC_CLAUSE_DEVICE:
27935       kind = GOMP_MAP_FORCE_TO;
27936       break;
27937     case PRAGMA_OACC_CLAUSE_HOST:
27938     case PRAGMA_OACC_CLAUSE_SELF:
27939       kind = GOMP_MAP_FORCE_FROM;
27940       break;
27941     case PRAGMA_OACC_CLAUSE_PRESENT:
27942       kind = GOMP_MAP_FORCE_PRESENT;
27943       break;
27944     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
27945       kind = GOMP_MAP_TOFROM;
27946       break;
27947     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
27948       kind = GOMP_MAP_TO;
27949       break;
27950     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
27951       kind = GOMP_MAP_FROM;
27952       break;
27953     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
27954       kind = GOMP_MAP_ALLOC;
27955       break;
27956     default:
27957       gcc_unreachable ();
27958     }
27959   tree nl, c;
27960   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
27961
27962   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
27963     OMP_CLAUSE_SET_MAP_KIND (c, kind);
27964
27965   return nl;
27966 }
27967
27968 /* OpenACC 2.0:
27969    deviceptr ( variable-list ) */
27970
27971 static tree
27972 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
27973 {
27974   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27975   tree vars, t;
27976
27977   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27978      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
27979      variable-list must only allow for pointer variables.  */
27980   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27981   for (t = vars; t; t = TREE_CHAIN (t))
27982     {
27983       tree v = TREE_PURPOSE (t);
27984
27985       /* FIXME diagnostics: Ideally we should keep individual
27986          locations for all the variables in the var list to make the
27987          following errors more precise.  Perhaps
27988          c_parser_omp_var_list_parens should construct a list of
27989          locations to go along with the var list.  */
27990
27991       if (TREE_CODE (v) != VAR_DECL)
27992         error_at (loc, "%qD is not a variable", v);
27993       else if (TREE_TYPE (v) == error_mark_node)
27994         ;
27995       else if (!POINTER_TYPE_P (TREE_TYPE (v)))
27996         error_at (loc, "%qD is not a pointer variable", v);
27997
27998       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
27999       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28000       OMP_CLAUSE_DECL (u) = v;
28001       OMP_CLAUSE_CHAIN (u) = list;
28002       list = u;
28003     }
28004
28005   return list;
28006 }
28007
28008 /* OpenACC:
28009    vector_length ( expression ) */
28010
28011 static tree
28012 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28013 {
28014   tree t, c;
28015   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28016   bool error = false;
28017
28018   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28019     return list;
28020
28021   t = cp_parser_condition (parser);
28022   if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28023     {
28024       error_at (location, "expected positive integer expression");
28025       error = true;
28026     }
28027
28028   if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28029     {
28030       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28031                                            /*or_comma=*/false,
28032                                            /*consume_paren=*/true);
28033       return list;
28034     }
28035
28036   check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28037                              location);
28038
28039   c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28040   OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28041   OMP_CLAUSE_CHAIN (c) = list;
28042   list = c;
28043
28044   return list;
28045 }
28046
28047 /* OpenACC 2.0
28048    Parse wait clause or directive parameters.  */
28049
28050 static tree
28051 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28052 {
28053   vec<tree, va_gc> *args;
28054   tree t, args_tree;
28055
28056   args = cp_parser_parenthesized_expression_list (parser, non_attr,
28057                                                   /*cast_p=*/false,
28058                                                   /*allow_expansion_p=*/true,
28059                                                   /*non_constant_p=*/NULL);
28060
28061   if (args == NULL || args->length () == 0)
28062     {
28063       cp_parser_error (parser, "expected integer expression before ')'");
28064       if (args != NULL)
28065         release_tree_vector (args);
28066       return list;
28067     }
28068
28069   args_tree = build_tree_list_vec (args);
28070
28071   release_tree_vector (args);
28072
28073   for (t = args_tree; t; t = TREE_CHAIN (t))
28074     {
28075       tree targ = TREE_VALUE (t);
28076
28077       if (targ != error_mark_node)
28078         {
28079           if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28080             error ("%<wait%> expression must be integral");
28081           else
28082             {
28083               tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28084
28085               mark_rvalue_use (targ);
28086               OMP_CLAUSE_DECL (c) = targ;
28087               OMP_CLAUSE_CHAIN (c) = list;
28088               list = c;
28089             }
28090         }
28091     }
28092
28093   return list;
28094 }
28095
28096 /* OpenACC:
28097    wait ( int-expr-list ) */
28098
28099 static tree
28100 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28101 {
28102   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28103
28104   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28105     return list;
28106
28107   list = cp_parser_oacc_wait_list (parser, location, list);
28108
28109   return list;
28110 }
28111
28112 /* OpenMP 3.0:
28113    collapse ( constant-expression ) */
28114
28115 static tree
28116 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28117 {
28118   tree c, num;
28119   location_t loc;
28120   HOST_WIDE_INT n;
28121
28122   loc = cp_lexer_peek_token (parser->lexer)->location;
28123   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28124     return list;
28125
28126   num = cp_parser_constant_expression (parser);
28127
28128   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28129     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28130                                            /*or_comma=*/false,
28131                                            /*consume_paren=*/true);
28132
28133   if (num == error_mark_node)
28134     return list;
28135   num = fold_non_dependent_expr (num);
28136   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28137       || !tree_fits_shwi_p (num)
28138       || (n = tree_to_shwi (num)) <= 0
28139       || (int) n != n)
28140     {
28141       error_at (loc, "collapse argument needs positive constant integer expression");
28142       return list;
28143     }
28144
28145   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28146   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28147   OMP_CLAUSE_CHAIN (c) = list;
28148   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28149
28150   return c;
28151 }
28152
28153 /* OpenMP 2.5:
28154    default ( shared | none ) */
28155
28156 static tree
28157 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28158 {
28159   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28160   tree c;
28161
28162   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28163     return list;
28164   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28165     {
28166       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28167       const char *p = IDENTIFIER_POINTER (id);
28168
28169       switch (p[0])
28170         {
28171         case 'n':
28172           if (strcmp ("none", p) != 0)
28173             goto invalid_kind;
28174           kind = OMP_CLAUSE_DEFAULT_NONE;
28175           break;
28176
28177         case 's':
28178           if (strcmp ("shared", p) != 0)
28179             goto invalid_kind;
28180           kind = OMP_CLAUSE_DEFAULT_SHARED;
28181           break;
28182
28183         default:
28184           goto invalid_kind;
28185         }
28186
28187       cp_lexer_consume_token (parser->lexer);
28188     }
28189   else
28190     {
28191     invalid_kind:
28192       cp_parser_error (parser, "expected %<none%> or %<shared%>");
28193     }
28194
28195   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28196     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28197                                            /*or_comma=*/false,
28198                                            /*consume_paren=*/true);
28199
28200   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28201     return list;
28202
28203   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28204   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28205   OMP_CLAUSE_CHAIN (c) = list;
28206   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28207
28208   return c;
28209 }
28210
28211 /* OpenMP 3.1:
28212    final ( expression ) */
28213
28214 static tree
28215 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28216 {
28217   tree t, c;
28218
28219   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28220     return list;
28221
28222   t = cp_parser_condition (parser);
28223
28224   if (t == error_mark_node
28225       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28226     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28227                                            /*or_comma=*/false,
28228                                            /*consume_paren=*/true);
28229
28230   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28231
28232   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28233   OMP_CLAUSE_FINAL_EXPR (c) = t;
28234   OMP_CLAUSE_CHAIN (c) = list;
28235
28236   return c;
28237 }
28238
28239 /* OpenMP 2.5:
28240    if ( expression ) */
28241
28242 static tree
28243 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28244 {
28245   tree t, c;
28246
28247   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28248     return list;
28249
28250   t = cp_parser_condition (parser);
28251
28252   if (t == error_mark_node
28253       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28254     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28255                                            /*or_comma=*/false,
28256                                            /*consume_paren=*/true);
28257
28258   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28259
28260   c = build_omp_clause (location, OMP_CLAUSE_IF);
28261   OMP_CLAUSE_IF_EXPR (c) = t;
28262   OMP_CLAUSE_CHAIN (c) = list;
28263
28264   return c;
28265 }
28266
28267 /* OpenMP 3.1:
28268    mergeable */
28269
28270 static tree
28271 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28272                                 tree list, location_t location)
28273 {
28274   tree c;
28275
28276   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28277                              location);
28278
28279   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28280   OMP_CLAUSE_CHAIN (c) = list;
28281   return c;
28282 }
28283
28284 /* OpenMP 2.5:
28285    nowait */
28286
28287 static tree
28288 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28289                              tree list, location_t location)
28290 {
28291   tree c;
28292
28293   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28294
28295   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28296   OMP_CLAUSE_CHAIN (c) = list;
28297   return c;
28298 }
28299
28300 /* OpenACC:
28301    num_gangs ( expression ) */
28302
28303 static tree
28304 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28305 {
28306   tree t, c;
28307   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28308
28309   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28310     return list;
28311
28312   t = cp_parser_condition (parser);
28313
28314   if (t == error_mark_node
28315       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28316     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28317                                            /*or_comma=*/false,
28318                                            /*consume_paren=*/true);
28319
28320   if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28321     {
28322       error_at (location, "expected positive integer expression");
28323       return list;
28324     }
28325
28326   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28327
28328   c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28329   OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28330   OMP_CLAUSE_CHAIN (c) = list;
28331   list = c;
28332
28333   return list;
28334 }
28335
28336 /* OpenMP 2.5:
28337    num_threads ( expression ) */
28338
28339 static tree
28340 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28341                                   location_t location)
28342 {
28343   tree t, c;
28344
28345   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28346     return list;
28347
28348   t = cp_parser_expression (parser);
28349
28350   if (t == error_mark_node
28351       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28352     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28353                                            /*or_comma=*/false,
28354                                            /*consume_paren=*/true);
28355
28356   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28357                              "num_threads", location);
28358
28359   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28360   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28361   OMP_CLAUSE_CHAIN (c) = list;
28362
28363   return c;
28364 }
28365
28366 /* OpenACC:
28367    num_workers ( expression ) */
28368
28369 static tree
28370 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28371 {
28372   tree t, c;
28373   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28374
28375   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28376     return list;
28377
28378   t = cp_parser_condition (parser);
28379
28380   if (t == error_mark_node
28381       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28382     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28383                                            /*or_comma=*/false,
28384                                            /*consume_paren=*/true);
28385
28386   if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28387     {
28388       error_at (location, "expected positive integer expression");
28389       return list;
28390     }
28391
28392   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28393                                                                 location);
28394
28395   c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28396   OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28397   OMP_CLAUSE_CHAIN (c) = list;
28398   list = c;
28399
28400   return list;
28401 }
28402
28403 /* OpenMP 2.5:
28404    ordered */
28405
28406 static tree
28407 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28408                               tree list, location_t location)
28409 {
28410   tree c;
28411
28412   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28413                              "ordered", location);
28414
28415   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28416   OMP_CLAUSE_CHAIN (c) = list;
28417   return c;
28418 }
28419
28420 /* OpenMP 2.5:
28421    reduction ( reduction-operator : variable-list )
28422
28423    reduction-operator:
28424      One of: + * - & ^ | && ||
28425
28426    OpenMP 3.1:
28427
28428    reduction-operator:
28429      One of: + * - & ^ | && || min max
28430
28431    OpenMP 4.0:
28432
28433    reduction-operator:
28434      One of: + * - & ^ | && ||
28435      id-expression  */
28436
28437 static tree
28438 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28439 {
28440   enum tree_code code = ERROR_MARK;
28441   tree nlist, c, id = NULL_TREE;
28442
28443   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28444     return list;
28445
28446   switch (cp_lexer_peek_token (parser->lexer)->type)
28447     {
28448     case CPP_PLUS: code = PLUS_EXPR; break;
28449     case CPP_MULT: code = MULT_EXPR; break;
28450     case CPP_MINUS: code = MINUS_EXPR; break;
28451     case CPP_AND: code = BIT_AND_EXPR; break;
28452     case CPP_XOR: code = BIT_XOR_EXPR; break;
28453     case CPP_OR: code = BIT_IOR_EXPR; break;
28454     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28455     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28456     default: break;
28457     }
28458
28459   if (code != ERROR_MARK)
28460     cp_lexer_consume_token (parser->lexer);
28461   else
28462     {
28463       bool saved_colon_corrects_to_scope_p;
28464       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28465       parser->colon_corrects_to_scope_p = false;
28466       id = cp_parser_id_expression (parser, /*template_p=*/false,
28467                                     /*check_dependency_p=*/true,
28468                                     /*template_p=*/NULL,
28469                                     /*declarator_p=*/false,
28470                                     /*optional_p=*/false);
28471       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28472       if (identifier_p (id))
28473         {
28474           const char *p = IDENTIFIER_POINTER (id);
28475
28476           if (strcmp (p, "min") == 0)
28477             code = MIN_EXPR;
28478           else if (strcmp (p, "max") == 0)
28479             code = MAX_EXPR;
28480           else if (id == ansi_opname (PLUS_EXPR))
28481             code = PLUS_EXPR;
28482           else if (id == ansi_opname (MULT_EXPR))
28483             code = MULT_EXPR;
28484           else if (id == ansi_opname (MINUS_EXPR))
28485             code = MINUS_EXPR;
28486           else if (id == ansi_opname (BIT_AND_EXPR))
28487             code = BIT_AND_EXPR;
28488           else if (id == ansi_opname (BIT_IOR_EXPR))
28489             code = BIT_IOR_EXPR;
28490           else if (id == ansi_opname (BIT_XOR_EXPR))
28491             code = BIT_XOR_EXPR;
28492           else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28493             code = TRUTH_ANDIF_EXPR;
28494           else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28495             code = TRUTH_ORIF_EXPR;
28496           id = omp_reduction_id (code, id, NULL_TREE);
28497           tree scope = parser->scope;
28498           if (scope)
28499             id = build_qualified_name (NULL_TREE, scope, id, false);
28500           parser->scope = NULL_TREE;
28501           parser->qualifying_scope = NULL_TREE;
28502           parser->object_scope = NULL_TREE;
28503         }
28504       else
28505         {
28506           error ("invalid reduction-identifier");
28507          resync_fail:
28508           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28509                                                  /*or_comma=*/false,
28510                                                  /*consume_paren=*/true);
28511           return list;
28512         }
28513     }
28514
28515   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28516     goto resync_fail;
28517
28518   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28519                                           NULL);
28520   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28521     {
28522       OMP_CLAUSE_REDUCTION_CODE (c) = code;
28523       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28524     }
28525
28526   return nlist;
28527 }
28528
28529 /* OpenMP 2.5:
28530    schedule ( schedule-kind )
28531    schedule ( schedule-kind , expression )
28532
28533    schedule-kind:
28534      static | dynamic | guided | runtime | auto  */
28535
28536 static tree
28537 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28538 {
28539   tree c, t;
28540
28541   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28542     return list;
28543
28544   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28545
28546   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28547     {
28548       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28549       const char *p = IDENTIFIER_POINTER (id);
28550
28551       switch (p[0])
28552         {
28553         case 'd':
28554           if (strcmp ("dynamic", p) != 0)
28555             goto invalid_kind;
28556           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28557           break;
28558
28559         case 'g':
28560           if (strcmp ("guided", p) != 0)
28561             goto invalid_kind;
28562           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28563           break;
28564
28565         case 'r':
28566           if (strcmp ("runtime", p) != 0)
28567             goto invalid_kind;
28568           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28569           break;
28570
28571         default:
28572           goto invalid_kind;
28573         }
28574     }
28575   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28576     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28577   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28578     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28579   else
28580     goto invalid_kind;
28581   cp_lexer_consume_token (parser->lexer);
28582
28583   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28584     {
28585       cp_token *token;
28586       cp_lexer_consume_token (parser->lexer);
28587
28588       token = cp_lexer_peek_token (parser->lexer);
28589       t = cp_parser_assignment_expression (parser);
28590
28591       if (t == error_mark_node)
28592         goto resync_fail;
28593       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28594         error_at (token->location, "schedule %<runtime%> does not take "
28595                   "a %<chunk_size%> parameter");
28596       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28597         error_at (token->location, "schedule %<auto%> does not take "
28598                   "a %<chunk_size%> parameter");
28599       else
28600         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28601
28602       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28603         goto resync_fail;
28604     }
28605   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28606     goto resync_fail;
28607
28608   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28609   OMP_CLAUSE_CHAIN (c) = list;
28610   return c;
28611
28612  invalid_kind:
28613   cp_parser_error (parser, "invalid schedule kind");
28614  resync_fail:
28615   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28616                                          /*or_comma=*/false,
28617                                          /*consume_paren=*/true);
28618   return list;
28619 }
28620
28621 /* OpenMP 3.0:
28622    untied */
28623
28624 static tree
28625 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28626                              tree list, location_t location)
28627 {
28628   tree c;
28629
28630   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28631
28632   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28633   OMP_CLAUSE_CHAIN (c) = list;
28634   return c;
28635 }
28636
28637 /* OpenMP 4.0:
28638    inbranch
28639    notinbranch */
28640
28641 static tree
28642 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28643                              tree list, location_t location)
28644 {
28645   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28646   tree c = build_omp_clause (location, code);
28647   OMP_CLAUSE_CHAIN (c) = list;
28648   return c;
28649 }
28650
28651 /* OpenMP 4.0:
28652    parallel
28653    for
28654    sections
28655    taskgroup */
28656
28657 static tree
28658 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28659                                  enum omp_clause_code code,
28660                                  tree list, location_t location)
28661 {
28662   tree c = build_omp_clause (location, code);
28663   OMP_CLAUSE_CHAIN (c) = list;
28664   return c;
28665 }
28666
28667 /* OpenMP 4.0:
28668    num_teams ( expression ) */
28669
28670 static tree
28671 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28672                                 location_t location)
28673 {
28674   tree t, c;
28675
28676   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28677     return list;
28678
28679   t = cp_parser_expression (parser);
28680
28681   if (t == error_mark_node
28682       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28683     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28684                                            /*or_comma=*/false,
28685                                            /*consume_paren=*/true);
28686
28687   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28688                              "num_teams", location);
28689
28690   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28691   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28692   OMP_CLAUSE_CHAIN (c) = list;
28693
28694   return c;
28695 }
28696
28697 /* OpenMP 4.0:
28698    thread_limit ( expression ) */
28699
28700 static tree
28701 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28702                                    location_t location)
28703 {
28704   tree t, c;
28705
28706   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28707     return list;
28708
28709   t = cp_parser_expression (parser);
28710
28711   if (t == error_mark_node
28712       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28713     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28714                                            /*or_comma=*/false,
28715                                            /*consume_paren=*/true);
28716
28717   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28718                              "thread_limit", location);
28719
28720   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28721   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28722   OMP_CLAUSE_CHAIN (c) = list;
28723
28724   return c;
28725 }
28726
28727 /* OpenMP 4.0:
28728    aligned ( variable-list )
28729    aligned ( variable-list : constant-expression )  */
28730
28731 static tree
28732 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28733 {
28734   tree nlist, c, alignment = NULL_TREE;
28735   bool colon;
28736
28737   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28738     return list;
28739
28740   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28741                                           &colon);
28742
28743   if (colon)
28744     {
28745       alignment = cp_parser_constant_expression (parser);
28746
28747       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28748         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28749                                                /*or_comma=*/false,
28750                                                /*consume_paren=*/true);
28751
28752       if (alignment == error_mark_node)
28753         alignment = NULL_TREE;
28754     }
28755
28756   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28757     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28758
28759   return nlist;
28760 }
28761
28762 /* OpenMP 4.0:
28763    linear ( variable-list )
28764    linear ( variable-list : expression )  */
28765
28766 static tree
28767 cp_parser_omp_clause_linear (cp_parser *parser, tree list, 
28768                              bool is_cilk_simd_fn)
28769 {
28770   tree nlist, c, step = integer_one_node;
28771   bool colon;
28772
28773   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28774     return list;
28775
28776   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28777                                           &colon);
28778
28779   if (colon)
28780     {
28781       step = cp_parser_expression (parser);
28782
28783       if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28784         {
28785           sorry ("using parameters for %<linear%> step is not supported yet");
28786           step = integer_one_node;
28787         }
28788       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28789         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28790                                                /*or_comma=*/false,
28791                                                /*consume_paren=*/true);
28792
28793       if (step == error_mark_node)
28794         return list;
28795     }
28796
28797   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28798     OMP_CLAUSE_LINEAR_STEP (c) = step;
28799
28800   return nlist;
28801 }
28802
28803 /* OpenMP 4.0:
28804    safelen ( constant-expression )  */
28805
28806 static tree
28807 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28808                               location_t location)
28809 {
28810   tree t, c;
28811
28812   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28813     return list;
28814
28815   t = cp_parser_constant_expression (parser);
28816
28817   if (t == error_mark_node
28818       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28819     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28820                                            /*or_comma=*/false,
28821                                            /*consume_paren=*/true);
28822
28823   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28824
28825   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28826   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28827   OMP_CLAUSE_CHAIN (c) = list;
28828
28829   return c;
28830 }
28831
28832 /* OpenMP 4.0:
28833    simdlen ( constant-expression )  */
28834
28835 static tree
28836 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28837                               location_t location)
28838 {
28839   tree t, c;
28840
28841   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28842     return list;
28843
28844   t = cp_parser_constant_expression (parser);
28845
28846   if (t == error_mark_node
28847       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28848     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28849                                            /*or_comma=*/false,
28850                                            /*consume_paren=*/true);
28851
28852   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28853
28854   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28855   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28856   OMP_CLAUSE_CHAIN (c) = list;
28857
28858   return c;
28859 }
28860
28861 /* OpenMP 4.0:
28862    depend ( depend-kind : variable-list )
28863
28864    depend-kind:
28865      in | out | inout  */
28866
28867 static tree
28868 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28869 {
28870   tree nlist, c;
28871   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28872
28873   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28874     return list;
28875
28876   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28877     {
28878       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28879       const char *p = IDENTIFIER_POINTER (id);
28880
28881       if (strcmp ("in", p) == 0)
28882         kind = OMP_CLAUSE_DEPEND_IN;
28883       else if (strcmp ("inout", p) == 0)
28884         kind = OMP_CLAUSE_DEPEND_INOUT;
28885       else if (strcmp ("out", p) == 0)
28886         kind = OMP_CLAUSE_DEPEND_OUT;
28887       else
28888         goto invalid_kind;
28889     }
28890   else
28891     goto invalid_kind;
28892
28893   cp_lexer_consume_token (parser->lexer);
28894   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28895     goto resync_fail;
28896
28897   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28898                                           NULL);
28899
28900   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28901     OMP_CLAUSE_DEPEND_KIND (c) = kind;
28902
28903   return nlist;
28904
28905  invalid_kind:
28906   cp_parser_error (parser, "invalid depend kind");
28907  resync_fail:
28908   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28909                                          /*or_comma=*/false,
28910                                          /*consume_paren=*/true);
28911   return list;
28912 }
28913
28914 /* OpenMP 4.0:
28915    map ( map-kind : variable-list )
28916    map ( variable-list )
28917
28918    map-kind:
28919      alloc | to | from | tofrom  */
28920
28921 static tree
28922 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28923 {
28924   tree nlist, c;
28925   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
28926
28927   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28928     return list;
28929
28930   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28931       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28932     {
28933       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28934       const char *p = IDENTIFIER_POINTER (id);
28935
28936       if (strcmp ("alloc", p) == 0)
28937         kind = GOMP_MAP_ALLOC;
28938       else if (strcmp ("to", p) == 0)
28939         kind = GOMP_MAP_TO;
28940       else if (strcmp ("from", p) == 0)
28941         kind = GOMP_MAP_FROM;
28942       else if (strcmp ("tofrom", p) == 0)
28943         kind = GOMP_MAP_TOFROM;
28944       else
28945         {
28946           cp_parser_error (parser, "invalid map kind");
28947           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28948                                                  /*or_comma=*/false,
28949                                                  /*consume_paren=*/true);
28950           return list;
28951         }
28952       cp_lexer_consume_token (parser->lexer);
28953       cp_lexer_consume_token (parser->lexer);
28954     }
28955
28956   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28957                                           NULL);
28958
28959   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28960     OMP_CLAUSE_SET_MAP_KIND (c, kind);
28961
28962   return nlist;
28963 }
28964
28965 /* OpenMP 4.0:
28966    device ( expression ) */
28967
28968 static tree
28969 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28970                              location_t location)
28971 {
28972   tree t, c;
28973
28974   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28975     return list;
28976
28977   t = cp_parser_expression (parser);
28978
28979   if (t == error_mark_node
28980       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28981     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28982                                            /*or_comma=*/false,
28983                                            /*consume_paren=*/true);
28984
28985   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28986                              "device", location);
28987
28988   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28989   OMP_CLAUSE_DEVICE_ID (c) = t;
28990   OMP_CLAUSE_CHAIN (c) = list;
28991
28992   return c;
28993 }
28994
28995 /* OpenMP 4.0:
28996    dist_schedule ( static )
28997    dist_schedule ( static , expression )  */
28998
28999 static tree
29000 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29001                                     location_t location)
29002 {
29003   tree c, t;
29004
29005   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29006     return list;
29007
29008   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29009
29010   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29011     goto invalid_kind;
29012   cp_lexer_consume_token (parser->lexer);
29013
29014   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29015     {
29016       cp_lexer_consume_token (parser->lexer);
29017
29018       t = cp_parser_assignment_expression (parser);
29019
29020       if (t == error_mark_node)
29021         goto resync_fail;
29022       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29023
29024       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29025         goto resync_fail;
29026     }
29027   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29028     goto resync_fail;
29029
29030   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29031                              location);
29032   OMP_CLAUSE_CHAIN (c) = list;
29033   return c;
29034
29035  invalid_kind:
29036   cp_parser_error (parser, "invalid dist_schedule kind");
29037  resync_fail:
29038   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29039                                          /*or_comma=*/false,
29040                                          /*consume_paren=*/true);
29041   return list;
29042 }
29043
29044 /* OpenMP 4.0:
29045    proc_bind ( proc-bind-kind )
29046
29047    proc-bind-kind:
29048      master | close | spread  */
29049
29050 static tree
29051 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29052                                 location_t location)
29053 {
29054   tree c;
29055   enum omp_clause_proc_bind_kind kind;
29056
29057   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29058     return list;
29059
29060   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29061     {
29062       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29063       const char *p = IDENTIFIER_POINTER (id);
29064
29065       if (strcmp ("master", p) == 0)
29066         kind = OMP_CLAUSE_PROC_BIND_MASTER;
29067       else if (strcmp ("close", p) == 0)
29068         kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29069       else if (strcmp ("spread", p) == 0)
29070         kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29071       else
29072         goto invalid_kind;
29073     }
29074   else
29075     goto invalid_kind;
29076
29077   cp_lexer_consume_token (parser->lexer);
29078   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29079     goto resync_fail;
29080
29081   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29082   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29083                              location);
29084   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29085   OMP_CLAUSE_CHAIN (c) = list;
29086   return c;
29087
29088  invalid_kind:
29089   cp_parser_error (parser, "invalid depend kind");
29090  resync_fail:
29091   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29092                                          /*or_comma=*/false,
29093                                          /*consume_paren=*/true);
29094   return list;
29095 }
29096
29097 /* OpenACC:
29098    async [( int-expr )] */
29099
29100 static tree
29101 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29102 {
29103   tree c, t;
29104   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29105
29106   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29107
29108   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29109     {
29110       cp_lexer_consume_token (parser->lexer);
29111
29112       t = cp_parser_expression (parser);
29113       if (t == error_mark_node
29114           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29115         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29116                                                 /*or_comma=*/false,
29117                                                 /*consume_paren=*/true);
29118     }
29119
29120   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29121
29122   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29123   OMP_CLAUSE_ASYNC_EXPR (c) = t;
29124   OMP_CLAUSE_CHAIN (c) = list;
29125   list = c;
29126
29127   return list;
29128 }
29129
29130 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
29131    is a bitmask in MASK.  Return the list of clauses found.  */
29132
29133 static tree
29134 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29135                            const char *where, cp_token *pragma_tok,
29136                            bool finish_p = true)
29137 {
29138   tree clauses = NULL;
29139   bool first = true;
29140
29141   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29142     {
29143       location_t here;
29144       pragma_omp_clause c_kind;
29145       const char *c_name;
29146       tree prev = clauses;
29147
29148       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29149         cp_lexer_consume_token (parser->lexer);
29150
29151       here = cp_lexer_peek_token (parser->lexer)->location;
29152       c_kind = cp_parser_omp_clause_name (parser);
29153
29154       switch (c_kind)
29155         {
29156         case PRAGMA_OACC_CLAUSE_ASYNC:
29157           clauses = cp_parser_oacc_clause_async (parser, clauses);
29158           c_name = "async";
29159           break;
29160         case PRAGMA_OACC_CLAUSE_COLLAPSE:
29161           clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29162           c_name = "collapse";
29163           break;
29164         case PRAGMA_OACC_CLAUSE_COPY:
29165           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29166           c_name = "copy";
29167           break;
29168         case PRAGMA_OACC_CLAUSE_COPYIN:
29169           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29170           c_name = "copyin";
29171           break;
29172         case PRAGMA_OACC_CLAUSE_COPYOUT:
29173           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29174           c_name = "copyout";
29175           break;
29176         case PRAGMA_OACC_CLAUSE_CREATE:
29177           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29178           c_name = "create";
29179           break;
29180         case PRAGMA_OACC_CLAUSE_DELETE:
29181           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29182           c_name = "delete";
29183           break;
29184         case PRAGMA_OACC_CLAUSE_DEVICE:
29185           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29186           c_name = "device";
29187           break;
29188         case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29189           clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29190           c_name = "deviceptr";
29191           break;
29192         case PRAGMA_OACC_CLAUSE_HOST:
29193           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29194           c_name = "host";
29195           break;
29196         case PRAGMA_OACC_CLAUSE_IF:
29197           clauses = cp_parser_omp_clause_if (parser, clauses, here);
29198           c_name = "if";
29199           break;
29200         case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29201           clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29202           c_name = "num_gangs";
29203           break;
29204         case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29205           clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29206           c_name = "num_workers";
29207           break;
29208         case PRAGMA_OACC_CLAUSE_PRESENT:
29209           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29210           c_name = "present";
29211           break;
29212         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29213           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29214           c_name = "present_or_copy";
29215           break;
29216         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29217           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29218           c_name = "present_or_copyin";
29219           break;
29220         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29221           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29222           c_name = "present_or_copyout";
29223           break;
29224         case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29225           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29226           c_name = "present_or_create";
29227           break;
29228         case PRAGMA_OACC_CLAUSE_REDUCTION:
29229           clauses = cp_parser_omp_clause_reduction (parser, clauses);
29230           c_name = "reduction";
29231           break;
29232         case PRAGMA_OACC_CLAUSE_SELF:
29233           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29234           c_name = "self";
29235           break;
29236         case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29237           clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29238           c_name = "vector_length";
29239           break;
29240         case PRAGMA_OACC_CLAUSE_WAIT:
29241           clauses = cp_parser_oacc_clause_wait (parser, clauses);
29242           c_name = "wait";
29243           break;
29244         default:
29245           cp_parser_error (parser, "expected %<#pragma acc%> clause");
29246           goto saw_error;
29247         }
29248
29249       first = false;
29250
29251       if (((mask >> c_kind) & 1) == 0)
29252         {
29253           /* Remove the invalid clause(s) from the list to avoid
29254              confusing the rest of the compiler.  */
29255           clauses = prev;
29256           error_at (here, "%qs is not valid for %qs", c_name, where);
29257         }
29258     }
29259
29260  saw_error:
29261   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29262
29263   if (finish_p)
29264     return finish_omp_clauses (clauses);
29265
29266   return clauses;
29267 }
29268
29269 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
29270    is a bitmask in MASK.  Return the list of clauses found; the result
29271    of clause default goes in *pdefault.  */
29272
29273 static tree
29274 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29275                            const char *where, cp_token *pragma_tok,
29276                            bool finish_p = true)
29277 {
29278   tree clauses = NULL;
29279   bool first = true;
29280   cp_token *token = NULL;
29281   bool cilk_simd_fn = false;
29282
29283   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29284     {
29285       pragma_omp_clause c_kind;
29286       const char *c_name;
29287       tree prev = clauses;
29288
29289       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29290         cp_lexer_consume_token (parser->lexer);
29291
29292       token = cp_lexer_peek_token (parser->lexer);
29293       c_kind = cp_parser_omp_clause_name (parser);
29294
29295       switch (c_kind)
29296         {
29297         case PRAGMA_OMP_CLAUSE_COLLAPSE:
29298           clauses = cp_parser_omp_clause_collapse (parser, clauses,
29299                                                    token->location);
29300           c_name = "collapse";
29301           break;
29302         case PRAGMA_OMP_CLAUSE_COPYIN:
29303           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29304           c_name = "copyin";
29305           break;
29306         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29307           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29308                                             clauses);
29309           c_name = "copyprivate";
29310           break;
29311         case PRAGMA_OMP_CLAUSE_DEFAULT:
29312           clauses = cp_parser_omp_clause_default (parser, clauses,
29313                                                   token->location);
29314           c_name = "default";
29315           break;
29316         case PRAGMA_OMP_CLAUSE_FINAL:
29317           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29318           c_name = "final";
29319           break;
29320         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29321           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29322                                             clauses);
29323           c_name = "firstprivate";
29324           break;
29325         case PRAGMA_OMP_CLAUSE_IF:
29326           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29327           c_name = "if";
29328           break;
29329         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29330           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29331                                             clauses);
29332           c_name = "lastprivate";
29333           break;
29334         case PRAGMA_OMP_CLAUSE_MERGEABLE:
29335           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29336                                                     token->location);
29337           c_name = "mergeable";
29338           break;
29339         case PRAGMA_OMP_CLAUSE_NOWAIT:
29340           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29341           c_name = "nowait";
29342           break;
29343         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29344           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29345                                                       token->location);
29346           c_name = "num_threads";
29347           break;
29348         case PRAGMA_OMP_CLAUSE_ORDERED:
29349           clauses = cp_parser_omp_clause_ordered (parser, clauses,
29350                                                   token->location);
29351           c_name = "ordered";
29352           break;
29353         case PRAGMA_OMP_CLAUSE_PRIVATE:
29354           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29355                                             clauses);
29356           c_name = "private";
29357           break;
29358         case PRAGMA_OMP_CLAUSE_REDUCTION:
29359           clauses = cp_parser_omp_clause_reduction (parser, clauses);
29360           c_name = "reduction";
29361           break;
29362         case PRAGMA_OMP_CLAUSE_SCHEDULE:
29363           clauses = cp_parser_omp_clause_schedule (parser, clauses,
29364                                                    token->location);
29365           c_name = "schedule";
29366           break;
29367         case PRAGMA_OMP_CLAUSE_SHARED:
29368           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29369                                             clauses);
29370           c_name = "shared";
29371           break;
29372         case PRAGMA_OMP_CLAUSE_UNTIED:
29373           clauses = cp_parser_omp_clause_untied (parser, clauses,
29374                                                  token->location);
29375           c_name = "untied";
29376           break;
29377         case PRAGMA_OMP_CLAUSE_INBRANCH:
29378         case PRAGMA_CILK_CLAUSE_MASK:
29379           clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29380                                                  clauses, token->location);
29381           c_name = "inbranch";
29382           break;
29383         case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29384         case PRAGMA_CILK_CLAUSE_NOMASK:
29385           clauses = cp_parser_omp_clause_branch (parser,
29386                                                  OMP_CLAUSE_NOTINBRANCH,
29387                                                  clauses, token->location);
29388           c_name = "notinbranch";
29389           break;
29390         case PRAGMA_OMP_CLAUSE_PARALLEL:
29391           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29392                                                      clauses, token->location);
29393           c_name = "parallel";
29394           if (!first)
29395             {
29396              clause_not_first:
29397               error_at (token->location, "%qs must be the first clause of %qs",
29398                         c_name, where);
29399               clauses = prev;
29400             }
29401           break;
29402         case PRAGMA_OMP_CLAUSE_FOR:
29403           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29404                                                      clauses, token->location);
29405           c_name = "for";
29406           if (!first)
29407             goto clause_not_first;
29408           break;
29409         case PRAGMA_OMP_CLAUSE_SECTIONS:
29410           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29411                                                      clauses, token->location);
29412           c_name = "sections";
29413           if (!first)
29414             goto clause_not_first;
29415           break;
29416         case PRAGMA_OMP_CLAUSE_TASKGROUP:
29417           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29418                                                      clauses, token->location);
29419           c_name = "taskgroup";
29420           if (!first)
29421             goto clause_not_first;
29422           break;
29423         case PRAGMA_OMP_CLAUSE_TO:
29424           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29425                                             clauses);
29426           c_name = "to";
29427           break;
29428         case PRAGMA_OMP_CLAUSE_FROM:
29429           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29430                                             clauses);
29431           c_name = "from";
29432           break;
29433         case PRAGMA_OMP_CLAUSE_UNIFORM:
29434           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29435                                             clauses);
29436           c_name = "uniform";
29437           break;
29438         case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29439           clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29440                                                     token->location);
29441           c_name = "num_teams";
29442           break;
29443         case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29444           clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29445                                                        token->location);
29446           c_name = "thread_limit";
29447           break;
29448         case PRAGMA_OMP_CLAUSE_ALIGNED:
29449           clauses = cp_parser_omp_clause_aligned (parser, clauses);
29450           c_name = "aligned";
29451           break;
29452         case PRAGMA_OMP_CLAUSE_LINEAR:
29453           if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29454             cilk_simd_fn = true;
29455           clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29456           c_name = "linear";
29457           break;
29458         case PRAGMA_OMP_CLAUSE_DEPEND:
29459           clauses = cp_parser_omp_clause_depend (parser, clauses);
29460           c_name = "depend";
29461           break;
29462         case PRAGMA_OMP_CLAUSE_MAP:
29463           clauses = cp_parser_omp_clause_map (parser, clauses);
29464           c_name = "map";
29465           break;
29466         case PRAGMA_OMP_CLAUSE_DEVICE:
29467           clauses = cp_parser_omp_clause_device (parser, clauses,
29468                                                  token->location);
29469           c_name = "device";
29470           break;
29471         case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29472           clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29473                                                         token->location);
29474           c_name = "dist_schedule";
29475           break;
29476         case PRAGMA_OMP_CLAUSE_PROC_BIND:
29477           clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29478                                                     token->location);
29479           c_name = "proc_bind";
29480           break;
29481         case PRAGMA_OMP_CLAUSE_SAFELEN:
29482           clauses = cp_parser_omp_clause_safelen (parser, clauses,
29483                                                   token->location);
29484           c_name = "safelen";
29485           break;
29486         case PRAGMA_OMP_CLAUSE_SIMDLEN:
29487           clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29488                                                   token->location);
29489           c_name = "simdlen";
29490           break;
29491         case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29492           clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29493           c_name = "simdlen";
29494           break;
29495         default:
29496           cp_parser_error (parser, "expected %<#pragma omp%> clause");
29497           goto saw_error;
29498         }
29499
29500       first = false;
29501
29502       if (((mask >> c_kind) & 1) == 0)
29503         {
29504           /* Remove the invalid clause(s) from the list to avoid
29505              confusing the rest of the compiler.  */
29506           clauses = prev;
29507           error_at (token->location, "%qs is not valid for %qs", c_name, where);
29508         }
29509     }
29510  saw_error:
29511   /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29512      no reason to skip to the end.  */
29513   if (!(flag_cilkplus && pragma_tok == NULL))
29514     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29515   if (finish_p)
29516     return finish_omp_clauses (clauses);
29517   return clauses;
29518 }
29519
29520 /* OpenMP 2.5:
29521    structured-block:
29522      statement
29523
29524    In practice, we're also interested in adding the statement to an
29525    outer node.  So it is convenient if we work around the fact that
29526    cp_parser_statement calls add_stmt.  */
29527
29528 static unsigned
29529 cp_parser_begin_omp_structured_block (cp_parser *parser)
29530 {
29531   unsigned save = parser->in_statement;
29532
29533   /* Only move the values to IN_OMP_BLOCK if they weren't false.
29534      This preserves the "not within loop or switch" style error messages
29535      for nonsense cases like
29536         void foo() {
29537         #pragma omp single
29538           break;
29539         }
29540   */
29541   if (parser->in_statement)
29542     parser->in_statement = IN_OMP_BLOCK;
29543
29544   return save;
29545 }
29546
29547 static void
29548 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29549 {
29550   parser->in_statement = save;
29551 }
29552
29553 static tree
29554 cp_parser_omp_structured_block (cp_parser *parser)
29555 {
29556   tree stmt = begin_omp_structured_block ();
29557   unsigned int save = cp_parser_begin_omp_structured_block (parser);
29558
29559   cp_parser_statement (parser, NULL_TREE, false, NULL);
29560
29561   cp_parser_end_omp_structured_block (parser, save);
29562   return finish_omp_structured_block (stmt);
29563 }
29564
29565 /* OpenMP 2.5:
29566    # pragma omp atomic new-line
29567      expression-stmt
29568
29569    expression-stmt:
29570      x binop= expr | x++ | ++x | x-- | --x
29571    binop:
29572      +, *, -, /, &, ^, |, <<, >>
29573
29574   where x is an lvalue expression with scalar type.
29575
29576    OpenMP 3.1:
29577    # pragma omp atomic new-line
29578      update-stmt
29579
29580    # pragma omp atomic read new-line
29581      read-stmt
29582
29583    # pragma omp atomic write new-line
29584      write-stmt
29585
29586    # pragma omp atomic update new-line
29587      update-stmt
29588
29589    # pragma omp atomic capture new-line
29590      capture-stmt
29591
29592    # pragma omp atomic capture new-line
29593      capture-block
29594
29595    read-stmt:
29596      v = x
29597    write-stmt:
29598      x = expr
29599    update-stmt:
29600      expression-stmt | x = x binop expr
29601    capture-stmt:
29602      v = expression-stmt
29603    capture-block:
29604      { v = x; update-stmt; } | { update-stmt; v = x; }
29605
29606    OpenMP 4.0:
29607    update-stmt:
29608      expression-stmt | x = x binop expr | x = expr binop x
29609    capture-stmt:
29610      v = update-stmt
29611    capture-block:
29612      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29613
29614   where x and v are lvalue expressions with scalar type.  */
29615
29616 static void
29617 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29618 {
29619   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29620   tree rhs1 = NULL_TREE, orig_lhs;
29621   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29622   bool structured_block = false;
29623   bool seq_cst = false;
29624
29625   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29626     {
29627       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29628       const char *p = IDENTIFIER_POINTER (id);
29629
29630       if (!strcmp (p, "seq_cst"))
29631         {
29632           seq_cst = true;
29633           cp_lexer_consume_token (parser->lexer);
29634           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29635               && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29636             cp_lexer_consume_token (parser->lexer);
29637         }
29638     }
29639   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29640     {
29641       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29642       const char *p = IDENTIFIER_POINTER (id);
29643
29644       if (!strcmp (p, "read"))
29645         code = OMP_ATOMIC_READ;
29646       else if (!strcmp (p, "write"))
29647         code = NOP_EXPR;
29648       else if (!strcmp (p, "update"))
29649         code = OMP_ATOMIC;
29650       else if (!strcmp (p, "capture"))
29651         code = OMP_ATOMIC_CAPTURE_NEW;
29652       else
29653         p = NULL;
29654       if (p)
29655         cp_lexer_consume_token (parser->lexer);
29656     }
29657   if (!seq_cst)
29658     {
29659       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29660           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29661         cp_lexer_consume_token (parser->lexer);
29662
29663       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29664         {
29665           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29666           const char *p = IDENTIFIER_POINTER (id);
29667
29668           if (!strcmp (p, "seq_cst"))
29669             {
29670               seq_cst = true;
29671               cp_lexer_consume_token (parser->lexer);
29672             }
29673         }
29674     }
29675   cp_parser_require_pragma_eol (parser, pragma_tok);
29676
29677   switch (code)
29678     {
29679     case OMP_ATOMIC_READ:
29680     case NOP_EXPR: /* atomic write */
29681       v = cp_parser_unary_expression (parser);
29682       if (v == error_mark_node)
29683         goto saw_error;
29684       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29685         goto saw_error;
29686       if (code == NOP_EXPR)
29687         lhs = cp_parser_expression (parser);
29688       else
29689         lhs = cp_parser_unary_expression (parser);
29690       if (lhs == error_mark_node)
29691         goto saw_error;
29692       if (code == NOP_EXPR)
29693         {
29694           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29695              opcode.  */
29696           code = OMP_ATOMIC;
29697           rhs = lhs;
29698           lhs = v;
29699           v = NULL_TREE;
29700         }
29701       goto done;
29702     case OMP_ATOMIC_CAPTURE_NEW:
29703       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29704         {
29705           cp_lexer_consume_token (parser->lexer);
29706           structured_block = true;
29707         }
29708       else
29709         {
29710           v = cp_parser_unary_expression (parser);
29711           if (v == error_mark_node)
29712             goto saw_error;
29713           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29714             goto saw_error;
29715         }
29716     default:
29717       break;
29718     }
29719
29720 restart:
29721   lhs = cp_parser_unary_expression (parser);
29722   orig_lhs = lhs;
29723   switch (TREE_CODE (lhs))
29724     {
29725     case ERROR_MARK:
29726       goto saw_error;
29727
29728     case POSTINCREMENT_EXPR:
29729       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29730         code = OMP_ATOMIC_CAPTURE_OLD;
29731       /* FALLTHROUGH */
29732     case PREINCREMENT_EXPR:
29733       lhs = TREE_OPERAND (lhs, 0);
29734       opcode = PLUS_EXPR;
29735       rhs = integer_one_node;
29736       break;
29737
29738     case POSTDECREMENT_EXPR:
29739       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29740         code = OMP_ATOMIC_CAPTURE_OLD;
29741       /* FALLTHROUGH */
29742     case PREDECREMENT_EXPR:
29743       lhs = TREE_OPERAND (lhs, 0);
29744       opcode = MINUS_EXPR;
29745       rhs = integer_one_node;
29746       break;
29747
29748     case COMPOUND_EXPR:
29749       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29750          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29751          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29752          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29753          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29754                                              (TREE_OPERAND (lhs, 1), 0), 0)))
29755             == BOOLEAN_TYPE)
29756        /* Undo effects of boolean_increment for post {in,de}crement.  */
29757        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29758       /* FALLTHRU */
29759     case MODIFY_EXPR:
29760       if (TREE_CODE (lhs) == MODIFY_EXPR
29761          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29762         {
29763           /* Undo effects of boolean_increment.  */
29764           if (integer_onep (TREE_OPERAND (lhs, 1)))
29765             {
29766               /* This is pre or post increment.  */
29767               rhs = TREE_OPERAND (lhs, 1);
29768               lhs = TREE_OPERAND (lhs, 0);
29769               opcode = NOP_EXPR;
29770               if (code == OMP_ATOMIC_CAPTURE_NEW
29771                   && !structured_block
29772                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29773                 code = OMP_ATOMIC_CAPTURE_OLD;
29774               break;
29775             }
29776         }
29777       /* FALLTHRU */
29778     default:
29779       switch (cp_lexer_peek_token (parser->lexer)->type)
29780         {
29781         case CPP_MULT_EQ:
29782           opcode = MULT_EXPR;
29783           break;
29784         case CPP_DIV_EQ:
29785           opcode = TRUNC_DIV_EXPR;
29786           break;
29787         case CPP_PLUS_EQ:
29788           opcode = PLUS_EXPR;
29789           break;
29790         case CPP_MINUS_EQ:
29791           opcode = MINUS_EXPR;
29792           break;
29793         case CPP_LSHIFT_EQ:
29794           opcode = LSHIFT_EXPR;
29795           break;
29796         case CPP_RSHIFT_EQ:
29797           opcode = RSHIFT_EXPR;
29798           break;
29799         case CPP_AND_EQ:
29800           opcode = BIT_AND_EXPR;
29801           break;
29802         case CPP_OR_EQ:
29803           opcode = BIT_IOR_EXPR;
29804           break;
29805         case CPP_XOR_EQ:
29806           opcode = BIT_XOR_EXPR;
29807           break;
29808         case CPP_EQ:
29809           enum cp_parser_prec oprec;
29810           cp_token *token;
29811           cp_lexer_consume_token (parser->lexer);
29812           cp_parser_parse_tentatively (parser);
29813           rhs1 = cp_parser_simple_cast_expression (parser);
29814           if (rhs1 == error_mark_node)
29815             {
29816               cp_parser_abort_tentative_parse (parser);
29817               cp_parser_simple_cast_expression (parser);
29818               goto saw_error;
29819             }
29820           token = cp_lexer_peek_token (parser->lexer);
29821           if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29822             {
29823               cp_parser_abort_tentative_parse (parser);
29824               cp_parser_parse_tentatively (parser);
29825               rhs = cp_parser_binary_expression (parser, false, true,
29826                                                  PREC_NOT_OPERATOR, NULL);
29827               if (rhs == error_mark_node)
29828                 {
29829                   cp_parser_abort_tentative_parse (parser);
29830                   cp_parser_binary_expression (parser, false, true,
29831                                                PREC_NOT_OPERATOR, NULL);
29832                   goto saw_error;
29833                 }
29834               switch (TREE_CODE (rhs))
29835                 {
29836                 case MULT_EXPR:
29837                 case TRUNC_DIV_EXPR:
29838                 case PLUS_EXPR:
29839                 case MINUS_EXPR:
29840                 case LSHIFT_EXPR:
29841                 case RSHIFT_EXPR:
29842                 case BIT_AND_EXPR:
29843                 case BIT_IOR_EXPR:
29844                 case BIT_XOR_EXPR:
29845                   if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29846                     {
29847                       if (cp_parser_parse_definitely (parser))
29848                         {
29849                           opcode = TREE_CODE (rhs);
29850                           rhs1 = TREE_OPERAND (rhs, 0);
29851                           rhs = TREE_OPERAND (rhs, 1);
29852                           goto stmt_done;
29853                         }
29854                       else
29855                         goto saw_error;
29856                     }
29857                   break;
29858                 default:
29859                   break;
29860                 }
29861               cp_parser_abort_tentative_parse (parser);
29862               if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29863                 {
29864                   rhs = cp_parser_expression (parser);
29865                   if (rhs == error_mark_node)
29866                     goto saw_error;
29867                   opcode = NOP_EXPR;
29868                   rhs1 = NULL_TREE;
29869                   goto stmt_done;
29870                 }
29871               cp_parser_error (parser,
29872                                "invalid form of %<#pragma omp atomic%>");
29873               goto saw_error;
29874             }
29875           if (!cp_parser_parse_definitely (parser))
29876             goto saw_error;
29877           switch (token->type)
29878             {
29879             case CPP_SEMICOLON:
29880               if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29881                 {
29882                   code = OMP_ATOMIC_CAPTURE_OLD;
29883                   v = lhs;
29884                   lhs = NULL_TREE;
29885                   lhs1 = rhs1;
29886                   rhs1 = NULL_TREE;
29887                   cp_lexer_consume_token (parser->lexer);
29888                   goto restart;
29889                 }
29890               else if (structured_block)
29891                 {
29892                   opcode = NOP_EXPR;
29893                   rhs = rhs1;
29894                   rhs1 = NULL_TREE;
29895                   goto stmt_done;
29896                 }
29897               cp_parser_error (parser,
29898                                "invalid form of %<#pragma omp atomic%>");
29899               goto saw_error;
29900             case CPP_MULT:
29901               opcode = MULT_EXPR;
29902               break;
29903             case CPP_DIV:
29904               opcode = TRUNC_DIV_EXPR;
29905               break;
29906             case CPP_PLUS:
29907               opcode = PLUS_EXPR;
29908               break;
29909             case CPP_MINUS:
29910               opcode = MINUS_EXPR;
29911               break;
29912             case CPP_LSHIFT:
29913               opcode = LSHIFT_EXPR;
29914               break;
29915             case CPP_RSHIFT:
29916               opcode = RSHIFT_EXPR;
29917               break;
29918             case CPP_AND:
29919               opcode = BIT_AND_EXPR;
29920               break;
29921             case CPP_OR:
29922               opcode = BIT_IOR_EXPR;
29923               break;
29924             case CPP_XOR:
29925               opcode = BIT_XOR_EXPR;
29926               break;
29927             default:
29928               cp_parser_error (parser,
29929                                "invalid operator for %<#pragma omp atomic%>");
29930               goto saw_error;
29931             }
29932           oprec = TOKEN_PRECEDENCE (token);
29933           gcc_assert (oprec != PREC_NOT_OPERATOR);
29934           if (commutative_tree_code (opcode))
29935             oprec = (enum cp_parser_prec) (oprec - 1);
29936           cp_lexer_consume_token (parser->lexer);
29937           rhs = cp_parser_binary_expression (parser, false, false,
29938                                              oprec, NULL);
29939           if (rhs == error_mark_node)
29940             goto saw_error;
29941           goto stmt_done;
29942           /* FALLTHROUGH */
29943         default:
29944           cp_parser_error (parser,
29945                            "invalid operator for %<#pragma omp atomic%>");
29946           goto saw_error;
29947         }
29948       cp_lexer_consume_token (parser->lexer);
29949
29950       rhs = cp_parser_expression (parser);
29951       if (rhs == error_mark_node)
29952         goto saw_error;
29953       break;
29954     }
29955 stmt_done:
29956   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29957     {
29958       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29959         goto saw_error;
29960       v = cp_parser_unary_expression (parser);
29961       if (v == error_mark_node)
29962         goto saw_error;
29963       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29964         goto saw_error;
29965       lhs1 = cp_parser_unary_expression (parser);
29966       if (lhs1 == error_mark_node)
29967         goto saw_error;
29968     }
29969   if (structured_block)
29970     {
29971       cp_parser_consume_semicolon_at_end_of_statement (parser);
29972       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29973     }
29974 done:
29975   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29976   if (!structured_block)
29977     cp_parser_consume_semicolon_at_end_of_statement (parser);
29978   return;
29979
29980  saw_error:
29981   cp_parser_skip_to_end_of_block_or_statement (parser);
29982   if (structured_block)
29983     {
29984       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29985         cp_lexer_consume_token (parser->lexer);
29986       else if (code == OMP_ATOMIC_CAPTURE_NEW)
29987         {
29988           cp_parser_skip_to_end_of_block_or_statement (parser);
29989           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29990             cp_lexer_consume_token (parser->lexer);
29991         }
29992     }
29993 }
29994
29995
29996 /* OpenMP 2.5:
29997    # pragma omp barrier new-line  */
29998
29999 static void
30000 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30001 {
30002   cp_parser_require_pragma_eol (parser, pragma_tok);
30003   finish_omp_barrier ();
30004 }
30005
30006 /* OpenMP 2.5:
30007    # pragma omp critical [(name)] new-line
30008      structured-block  */
30009
30010 static tree
30011 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30012 {
30013   tree stmt, name = NULL;
30014
30015   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30016     {
30017       cp_lexer_consume_token (parser->lexer);
30018
30019       name = cp_parser_identifier (parser);
30020
30021       if (name == error_mark_node
30022           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30023         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30024                                                /*or_comma=*/false,
30025                                                /*consume_paren=*/true);
30026       if (name == error_mark_node)
30027         name = NULL;
30028     }
30029   cp_parser_require_pragma_eol (parser, pragma_tok);
30030
30031   stmt = cp_parser_omp_structured_block (parser);
30032   return c_finish_omp_critical (input_location, stmt, name);
30033 }
30034
30035 /* OpenMP 2.5:
30036    # pragma omp flush flush-vars[opt] new-line
30037
30038    flush-vars:
30039      ( variable-list ) */
30040
30041 static void
30042 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30043 {
30044   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30045     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30046   cp_parser_require_pragma_eol (parser, pragma_tok);
30047
30048   finish_omp_flush ();
30049 }
30050
30051 /* Helper function, to parse omp for increment expression.  */
30052
30053 static tree
30054 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30055 {
30056   tree cond = cp_parser_binary_expression (parser, false, true,
30057                                            PREC_NOT_OPERATOR, NULL);
30058   if (cond == error_mark_node
30059       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30060     {
30061       cp_parser_skip_to_end_of_statement (parser);
30062       return error_mark_node;
30063     }
30064
30065   switch (TREE_CODE (cond))
30066     {
30067     case GT_EXPR:
30068     case GE_EXPR:
30069     case LT_EXPR:
30070     case LE_EXPR:
30071       break;
30072     case NE_EXPR:
30073       if (code == CILK_SIMD || code == CILK_FOR)
30074         break;
30075       /* Fall through: OpenMP disallows NE_EXPR.  */
30076     default:
30077       return error_mark_node;
30078     }
30079
30080   /* If decl is an iterator, preserve LHS and RHS of the relational
30081      expr until finish_omp_for.  */
30082   if (decl
30083       && (type_dependent_expression_p (decl)
30084           || CLASS_TYPE_P (TREE_TYPE (decl))))
30085     return cond;
30086
30087   return build_x_binary_op (input_location, TREE_CODE (cond),
30088                             TREE_OPERAND (cond, 0), ERROR_MARK,
30089                             TREE_OPERAND (cond, 1), ERROR_MARK,
30090                             /*overload=*/NULL, tf_warning_or_error);
30091 }
30092
30093 /* Helper function, to parse omp for increment expression.  */
30094
30095 static tree
30096 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30097 {
30098   cp_token *token = cp_lexer_peek_token (parser->lexer);
30099   enum tree_code op;
30100   tree lhs, rhs;
30101   cp_id_kind idk;
30102   bool decl_first;
30103
30104   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30105     {
30106       op = (token->type == CPP_PLUS_PLUS
30107             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30108       cp_lexer_consume_token (parser->lexer);
30109       lhs = cp_parser_simple_cast_expression (parser);
30110       if (lhs != decl)
30111         return error_mark_node;
30112       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30113     }
30114
30115   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30116   if (lhs != decl)
30117     return error_mark_node;
30118
30119   token = cp_lexer_peek_token (parser->lexer);
30120   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30121     {
30122       op = (token->type == CPP_PLUS_PLUS
30123             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30124       cp_lexer_consume_token (parser->lexer);
30125       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30126     }
30127
30128   op = cp_parser_assignment_operator_opt (parser);
30129   if (op == ERROR_MARK)
30130     return error_mark_node;
30131
30132   if (op != NOP_EXPR)
30133     {
30134       rhs = cp_parser_assignment_expression (parser);
30135       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30136       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30137     }
30138
30139   lhs = cp_parser_binary_expression (parser, false, false,
30140                                      PREC_ADDITIVE_EXPRESSION, NULL);
30141   token = cp_lexer_peek_token (parser->lexer);
30142   decl_first = lhs == decl;
30143   if (decl_first)
30144     lhs = NULL_TREE;
30145   if (token->type != CPP_PLUS
30146       && token->type != CPP_MINUS)
30147     return error_mark_node;
30148
30149   do
30150     {
30151       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30152       cp_lexer_consume_token (parser->lexer);
30153       rhs = cp_parser_binary_expression (parser, false, false,
30154                                          PREC_ADDITIVE_EXPRESSION, NULL);
30155       token = cp_lexer_peek_token (parser->lexer);
30156       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30157         {
30158           if (lhs == NULL_TREE)
30159             {
30160               if (op == PLUS_EXPR)
30161                 lhs = rhs;
30162               else
30163                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30164                                         tf_warning_or_error);
30165             }
30166           else
30167             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30168                                      ERROR_MARK, NULL, tf_warning_or_error);
30169         }
30170     }
30171   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30172
30173   if (!decl_first)
30174     {
30175       if (rhs != decl || op == MINUS_EXPR)
30176         return error_mark_node;
30177       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30178     }
30179   else
30180     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30181
30182   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30183 }
30184
30185 /* Parse the initialization statement of either an OpenMP for loop or
30186    a Cilk Plus for loop.
30187
30188    Return true if the resulting construct should have an
30189    OMP_CLAUSE_PRIVATE added to it.  */
30190
30191 static bool
30192 cp_parser_omp_for_loop_init (cp_parser *parser,
30193                              enum tree_code code,
30194                              tree &this_pre_body,
30195                              vec<tree, va_gc> *for_block,
30196                              tree &init,
30197                              tree &decl,
30198                              tree &real_decl)
30199 {
30200   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30201     return false;
30202
30203   bool add_private_clause = false;
30204
30205   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30206
30207      init-expr:
30208      var = lb
30209      integer-type var = lb
30210      random-access-iterator-type var = lb
30211      pointer-type var = lb
30212   */
30213   cp_decl_specifier_seq type_specifiers;
30214
30215   /* First, try to parse as an initialized declaration.  See
30216      cp_parser_condition, from whence the bulk of this is copied.  */
30217
30218   cp_parser_parse_tentatively (parser);
30219   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30220                                 /*is_trailing_return=*/false,
30221                                 &type_specifiers);
30222   if (cp_parser_parse_definitely (parser))
30223     {
30224       /* If parsing a type specifier seq succeeded, then this
30225          MUST be a initialized declaration.  */
30226       tree asm_specification, attributes;
30227       cp_declarator *declarator;
30228
30229       declarator = cp_parser_declarator (parser,
30230                                          CP_PARSER_DECLARATOR_NAMED,
30231                                          /*ctor_dtor_or_conv_p=*/NULL,
30232                                          /*parenthesized_p=*/NULL,
30233                                          /*member_p=*/false,
30234                                          /*friend_p=*/false);
30235       attributes = cp_parser_attributes_opt (parser);
30236       asm_specification = cp_parser_asm_specification_opt (parser);
30237
30238       if (declarator == cp_error_declarator) 
30239         cp_parser_skip_to_end_of_statement (parser);
30240
30241       else 
30242         {
30243           tree pushed_scope, auto_node;
30244
30245           decl = start_decl (declarator, &type_specifiers,
30246                              SD_INITIALIZED, attributes,
30247                              /*prefix_attributes=*/NULL_TREE,
30248                              &pushed_scope);
30249
30250           auto_node = type_uses_auto (TREE_TYPE (decl));
30251           if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30252             {
30253               if (cp_lexer_next_token_is (parser->lexer, 
30254                                           CPP_OPEN_PAREN))
30255                 {
30256                   if (code != CILK_SIMD && code != CILK_FOR)
30257                     error ("parenthesized initialization is not allowed in "
30258                            "OpenMP %<for%> loop");
30259                   else
30260                     error ("parenthesized initialization is "
30261                            "not allowed in for-loop");
30262                 }
30263               else
30264                 /* Trigger an error.  */
30265                 cp_parser_require (parser, CPP_EQ, RT_EQ);
30266
30267               init = error_mark_node;
30268               cp_parser_skip_to_end_of_statement (parser);
30269             }
30270           else if (CLASS_TYPE_P (TREE_TYPE (decl))
30271                    || type_dependent_expression_p (decl)
30272                    || auto_node)
30273             {
30274               bool is_direct_init, is_non_constant_init;
30275
30276               init = cp_parser_initializer (parser,
30277                                             &is_direct_init,
30278                                             &is_non_constant_init);
30279
30280               if (auto_node)
30281                 {
30282                   TREE_TYPE (decl)
30283                     = do_auto_deduction (TREE_TYPE (decl), init,
30284                                          auto_node);
30285
30286                   if (!CLASS_TYPE_P (TREE_TYPE (decl))
30287                       && !type_dependent_expression_p (decl))
30288                     goto non_class;
30289                 }
30290                       
30291               cp_finish_decl (decl, init, !is_non_constant_init,
30292                               asm_specification,
30293                               LOOKUP_ONLYCONVERTING);
30294               if (CLASS_TYPE_P (TREE_TYPE (decl)))
30295                 {
30296                   vec_safe_push (for_block, this_pre_body);
30297                   init = NULL_TREE;
30298                 }
30299               else
30300                 init = pop_stmt_list (this_pre_body);
30301               this_pre_body = NULL_TREE;
30302             }
30303           else
30304             {
30305               /* Consume '='.  */
30306               cp_lexer_consume_token (parser->lexer);
30307               init = cp_parser_assignment_expression (parser);
30308
30309             non_class:
30310               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30311                 init = error_mark_node;
30312               else
30313                 cp_finish_decl (decl, NULL_TREE,
30314                                 /*init_const_expr_p=*/false,
30315                                 asm_specification,
30316                                 LOOKUP_ONLYCONVERTING);
30317             }
30318
30319           if (pushed_scope)
30320             pop_scope (pushed_scope);
30321         }
30322     }
30323   else 
30324     {
30325       cp_id_kind idk;
30326       /* If parsing a type specifier sequence failed, then
30327          this MUST be a simple expression.  */
30328       if (code == CILK_FOR)
30329         error ("%<_Cilk_for%> allows expression instead of declaration only "
30330                "in C, not in C++");
30331       cp_parser_parse_tentatively (parser);
30332       decl = cp_parser_primary_expression (parser, false, false,
30333                                            false, &idk);
30334       if (!cp_parser_error_occurred (parser)
30335           && decl
30336           && DECL_P (decl)
30337           && CLASS_TYPE_P (TREE_TYPE (decl)))
30338         {
30339           tree rhs;
30340
30341           cp_parser_parse_definitely (parser);
30342           cp_parser_require (parser, CPP_EQ, RT_EQ);
30343           rhs = cp_parser_assignment_expression (parser);
30344           finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30345                                                  decl, NOP_EXPR,
30346                                                  rhs,
30347                                                  tf_warning_or_error));
30348           add_private_clause = true;
30349         }
30350       else
30351         {
30352           decl = NULL;
30353           cp_parser_abort_tentative_parse (parser);
30354           init = cp_parser_expression (parser);
30355           if (init)
30356             {
30357               if (TREE_CODE (init) == MODIFY_EXPR
30358                   || TREE_CODE (init) == MODOP_EXPR)
30359                 real_decl = TREE_OPERAND (init, 0);
30360             }
30361         }
30362     }
30363   return add_private_clause;
30364 }
30365
30366 /* Parse the restricted form of the for statement allowed by OpenMP.  */
30367
30368 static tree
30369 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30370                         tree *cclauses)
30371 {
30372   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30373   tree real_decl, initv, condv, incrv, declv;
30374   tree this_pre_body, cl;
30375   location_t loc_first;
30376   bool collapse_err = false;
30377   int i, collapse = 1, nbraces = 0;
30378   vec<tree, va_gc> *for_block = make_tree_vector ();
30379
30380   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30381     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30382       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30383
30384   gcc_assert (collapse >= 1);
30385
30386   declv = make_tree_vec (collapse);
30387   initv = make_tree_vec (collapse);
30388   condv = make_tree_vec (collapse);
30389   incrv = make_tree_vec (collapse);
30390
30391   loc_first = cp_lexer_peek_token (parser->lexer)->location;
30392
30393   for (i = 0; i < collapse; i++)
30394     {
30395       int bracecount = 0;
30396       bool add_private_clause = false;
30397       location_t loc;
30398
30399       if (code != CILK_FOR
30400           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30401         {
30402           cp_parser_error (parser, "for statement expected");
30403           return NULL;
30404         }
30405       if (code == CILK_FOR
30406           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30407         {
30408           cp_parser_error (parser, "_Cilk_for statement expected");
30409           return NULL;
30410         }
30411       loc = cp_lexer_consume_token (parser->lexer)->location;
30412
30413       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30414         return NULL;
30415
30416       init = decl = real_decl = NULL;
30417       this_pre_body = push_stmt_list ();
30418
30419       add_private_clause
30420         |= cp_parser_omp_for_loop_init (parser, code,
30421                                         this_pre_body, for_block,
30422                                         init, decl, real_decl);
30423
30424       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30425       if (this_pre_body)
30426         {
30427           this_pre_body = pop_stmt_list (this_pre_body);
30428           if (pre_body)
30429             {
30430               tree t = pre_body;
30431               pre_body = push_stmt_list ();
30432               add_stmt (t);
30433               add_stmt (this_pre_body);
30434               pre_body = pop_stmt_list (pre_body);
30435             }
30436           else
30437             pre_body = this_pre_body;
30438         }
30439
30440       if (decl)
30441         real_decl = decl;
30442       if (cclauses != NULL
30443           && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30444           && real_decl != NULL_TREE)
30445         {
30446           tree *c;
30447           for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30448             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30449                 && OMP_CLAUSE_DECL (*c) == real_decl)
30450               {
30451                 error_at (loc, "iteration variable %qD"
30452                           " should not be firstprivate", real_decl);
30453                 *c = OMP_CLAUSE_CHAIN (*c);
30454               }
30455             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30456                      && OMP_CLAUSE_DECL (*c) == real_decl)
30457               {
30458                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30459                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
30460                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30461                 OMP_CLAUSE_DECL (l) = real_decl;
30462                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30463                 if (code == OMP_SIMD)
30464                   {
30465                     OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30466                     cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30467                   }
30468                 else
30469                   {
30470                     OMP_CLAUSE_CHAIN (l) = clauses;
30471                     clauses = l;
30472                   }
30473                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30474                 CP_OMP_CLAUSE_INFO (*c) = NULL;
30475                 add_private_clause = false;
30476               }
30477             else
30478               {
30479                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30480                     && OMP_CLAUSE_DECL (*c) == real_decl)
30481                   add_private_clause = false;
30482                 c = &OMP_CLAUSE_CHAIN (*c);
30483               }
30484         }
30485
30486       if (add_private_clause)
30487         {
30488           tree c;
30489           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30490             {
30491               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30492                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30493                   && OMP_CLAUSE_DECL (c) == decl)
30494                 break;
30495               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30496                        && OMP_CLAUSE_DECL (c) == decl)
30497                 error_at (loc, "iteration variable %qD "
30498                           "should not be firstprivate",
30499                           decl);
30500               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30501                        && OMP_CLAUSE_DECL (c) == decl)
30502                 error_at (loc, "iteration variable %qD should not be reduction",
30503                           decl);
30504             }
30505           if (c == NULL)
30506             {
30507               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30508               OMP_CLAUSE_DECL (c) = decl;
30509               c = finish_omp_clauses (c);
30510               if (c)
30511                 {
30512                   OMP_CLAUSE_CHAIN (c) = clauses;
30513                   clauses = c;
30514                 }
30515             }
30516         }
30517
30518       cond = NULL;
30519       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30520         cond = cp_parser_omp_for_cond (parser, decl, code);
30521       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30522
30523       incr = NULL;
30524       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30525         {
30526           /* If decl is an iterator, preserve the operator on decl
30527              until finish_omp_for.  */
30528           if (real_decl
30529               && ((processing_template_decl
30530                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30531                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30532             incr = cp_parser_omp_for_incr (parser, real_decl);
30533           else
30534             incr = cp_parser_expression (parser);
30535           if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30536             SET_EXPR_LOCATION (incr, input_location);
30537         }
30538
30539       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30540         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30541                                                /*or_comma=*/false,
30542                                                /*consume_paren=*/true);
30543
30544       TREE_VEC_ELT (declv, i) = decl;
30545       TREE_VEC_ELT (initv, i) = init;
30546       TREE_VEC_ELT (condv, i) = cond;
30547       TREE_VEC_ELT (incrv, i) = incr;
30548
30549       if (i == collapse - 1)
30550         break;
30551
30552       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30553          in between the collapsed for loops to be still considered perfectly
30554          nested.  Hopefully the final version clarifies this.
30555          For now handle (multiple) {'s and empty statements.  */
30556       cp_parser_parse_tentatively (parser);
30557       do
30558         {
30559           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30560             break;
30561           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30562             {
30563               cp_lexer_consume_token (parser->lexer);
30564               bracecount++;
30565             }
30566           else if (bracecount
30567                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30568             cp_lexer_consume_token (parser->lexer);
30569           else
30570             {
30571               loc = cp_lexer_peek_token (parser->lexer)->location;
30572               error_at (loc, "not enough collapsed for loops");
30573               collapse_err = true;
30574               cp_parser_abort_tentative_parse (parser);
30575               declv = NULL_TREE;
30576               break;
30577             }
30578         }
30579       while (1);
30580
30581       if (declv)
30582         {
30583           cp_parser_parse_definitely (parser);
30584           nbraces += bracecount;
30585         }
30586     }
30587
30588   /* Note that we saved the original contents of this flag when we entered
30589      the structured block, and so we don't need to re-save it here.  */
30590   if (code == CILK_SIMD || code == CILK_FOR)
30591     parser->in_statement = IN_CILK_SIMD_FOR;
30592   else
30593     parser->in_statement = IN_OMP_FOR;
30594
30595   /* Note that the grammar doesn't call for a structured block here,
30596      though the loop as a whole is a structured block.  */
30597   body = push_stmt_list ();
30598   cp_parser_statement (parser, NULL_TREE, false, NULL);
30599   body = pop_stmt_list (body);
30600
30601   if (declv == NULL_TREE)
30602     ret = NULL_TREE;
30603   else
30604     ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30605                           pre_body, clauses);
30606
30607   while (nbraces)
30608     {
30609       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30610         {
30611           cp_lexer_consume_token (parser->lexer);
30612           nbraces--;
30613         }
30614       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30615         cp_lexer_consume_token (parser->lexer);
30616       else
30617         {
30618           if (!collapse_err)
30619             {
30620               error_at (cp_lexer_peek_token (parser->lexer)->location,
30621                         "collapsed loops not perfectly nested");
30622             }
30623           collapse_err = true;
30624           cp_parser_statement_seq_opt (parser, NULL);
30625           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30626             break;
30627         }
30628     }
30629
30630   while (!for_block->is_empty ())
30631     add_stmt (pop_stmt_list (for_block->pop ()));
30632   release_tree_vector (for_block);
30633
30634   return ret;
30635 }
30636
30637 /* Helper function for OpenMP parsing, split clauses and call
30638    finish_omp_clauses on each of the set of clauses afterwards.  */
30639
30640 static void
30641 cp_omp_split_clauses (location_t loc, enum tree_code code,
30642                       omp_clause_mask mask, tree clauses, tree *cclauses)
30643 {
30644   int i;
30645   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30646   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30647     if (cclauses[i])
30648       cclauses[i] = finish_omp_clauses (cclauses[i]);
30649 }
30650
30651 /* OpenMP 4.0:
30652    #pragma omp simd simd-clause[optseq] new-line
30653      for-loop  */
30654
30655 #define OMP_SIMD_CLAUSE_MASK                                    \
30656         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)      \
30657         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
30658         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
30659         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30660         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30661         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30662         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30663
30664 static tree
30665 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30666                     char *p_name, omp_clause_mask mask, tree *cclauses)
30667 {
30668   tree clauses, sb, ret;
30669   unsigned int save;
30670   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30671
30672   strcat (p_name, " simd");
30673   mask |= OMP_SIMD_CLAUSE_MASK;
30674   mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30675
30676   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30677                                        cclauses == NULL);
30678   if (cclauses)
30679     {
30680       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30681       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30682     }
30683
30684   sb = begin_omp_structured_block ();
30685   save = cp_parser_begin_omp_structured_block (parser);
30686
30687   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30688
30689   cp_parser_end_omp_structured_block (parser, save);
30690   add_stmt (finish_omp_structured_block (sb));
30691
30692   return ret;
30693 }
30694
30695 /* OpenMP 2.5:
30696    #pragma omp for for-clause[optseq] new-line
30697      for-loop
30698
30699    OpenMP 4.0:
30700    #pragma omp for simd for-simd-clause[optseq] new-line
30701      for-loop  */
30702
30703 #define OMP_FOR_CLAUSE_MASK                                     \
30704         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30705         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30706         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30707         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30708         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)      \
30709         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)     \
30710         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
30711         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30712
30713 static tree
30714 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30715                    char *p_name, omp_clause_mask mask, tree *cclauses)
30716 {
30717   tree clauses, sb, ret;
30718   unsigned int save;
30719   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30720
30721   strcat (p_name, " for");
30722   mask |= OMP_FOR_CLAUSE_MASK;
30723   if (cclauses)
30724     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30725
30726   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30727     {
30728       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30729       const char *p = IDENTIFIER_POINTER (id);
30730
30731       if (strcmp (p, "simd") == 0)
30732         {
30733           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30734           if (cclauses == NULL)
30735             cclauses = cclauses_buf;
30736
30737           cp_lexer_consume_token (parser->lexer);
30738           if (!flag_openmp)  /* flag_openmp_simd  */
30739             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30740                                        cclauses);
30741           sb = begin_omp_structured_block ();
30742           save = cp_parser_begin_omp_structured_block (parser);
30743           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30744                                     cclauses);
30745           cp_parser_end_omp_structured_block (parser, save);
30746           tree body = finish_omp_structured_block (sb);
30747           if (ret == NULL)
30748             return ret;
30749           ret = make_node (OMP_FOR);
30750           TREE_TYPE (ret) = void_type_node;
30751           OMP_FOR_BODY (ret) = body;
30752           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30753           SET_EXPR_LOCATION (ret, loc);
30754           add_stmt (ret);
30755           return ret;
30756         }
30757     }
30758   if (!flag_openmp)  /* flag_openmp_simd  */
30759     {
30760       cp_parser_require_pragma_eol (parser, pragma_tok);
30761       return NULL_TREE;
30762     }
30763
30764   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30765                                        cclauses == NULL);
30766   if (cclauses)
30767     {
30768       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30769       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30770     }
30771
30772   sb = begin_omp_structured_block ();
30773   save = cp_parser_begin_omp_structured_block (parser);
30774
30775   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30776
30777   cp_parser_end_omp_structured_block (parser, save);
30778   add_stmt (finish_omp_structured_block (sb));
30779
30780   return ret;
30781 }
30782
30783 /* OpenMP 2.5:
30784    # pragma omp master new-line
30785      structured-block  */
30786
30787 static tree
30788 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30789 {
30790   cp_parser_require_pragma_eol (parser, pragma_tok);
30791   return c_finish_omp_master (input_location,
30792                               cp_parser_omp_structured_block (parser));
30793 }
30794
30795 /* OpenMP 2.5:
30796    # pragma omp ordered new-line
30797      structured-block  */
30798
30799 static tree
30800 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30801 {
30802   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30803   cp_parser_require_pragma_eol (parser, pragma_tok);
30804   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30805 }
30806
30807 /* OpenMP 2.5:
30808
30809    section-scope:
30810      { section-sequence }
30811
30812    section-sequence:
30813      section-directive[opt] structured-block
30814      section-sequence section-directive structured-block  */
30815
30816 static tree
30817 cp_parser_omp_sections_scope (cp_parser *parser)
30818 {
30819   tree stmt, substmt;
30820   bool error_suppress = false;
30821   cp_token *tok;
30822
30823   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30824     return NULL_TREE;
30825
30826   stmt = push_stmt_list ();
30827
30828   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30829     {
30830       substmt = cp_parser_omp_structured_block (parser);
30831       substmt = build1 (OMP_SECTION, void_type_node, substmt);
30832       add_stmt (substmt);
30833     }
30834
30835   while (1)
30836     {
30837       tok = cp_lexer_peek_token (parser->lexer);
30838       if (tok->type == CPP_CLOSE_BRACE)
30839         break;
30840       if (tok->type == CPP_EOF)
30841         break;
30842
30843       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30844         {
30845           cp_lexer_consume_token (parser->lexer);
30846           cp_parser_require_pragma_eol (parser, tok);
30847           error_suppress = false;
30848         }
30849       else if (!error_suppress)
30850         {
30851           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30852           error_suppress = true;
30853         }
30854
30855       substmt = cp_parser_omp_structured_block (parser);
30856       substmt = build1 (OMP_SECTION, void_type_node, substmt);
30857       add_stmt (substmt);
30858     }
30859   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30860
30861   substmt = pop_stmt_list (stmt);
30862
30863   stmt = make_node (OMP_SECTIONS);
30864   TREE_TYPE (stmt) = void_type_node;
30865   OMP_SECTIONS_BODY (stmt) = substmt;
30866
30867   add_stmt (stmt);
30868   return stmt;
30869 }
30870
30871 /* OpenMP 2.5:
30872    # pragma omp sections sections-clause[optseq] newline
30873      sections-scope  */
30874
30875 #define OMP_SECTIONS_CLAUSE_MASK                                \
30876         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30877         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30878         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30879         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30880         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30881
30882 static tree
30883 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30884                         char *p_name, omp_clause_mask mask, tree *cclauses)
30885 {
30886   tree clauses, ret;
30887   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30888
30889   strcat (p_name, " sections");
30890   mask |= OMP_SECTIONS_CLAUSE_MASK;
30891   if (cclauses)
30892     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30893
30894   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30895                                        cclauses == NULL);
30896   if (cclauses)
30897     {
30898       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30899       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30900     }
30901
30902   ret = cp_parser_omp_sections_scope (parser);
30903   if (ret)
30904     OMP_SECTIONS_CLAUSES (ret) = clauses;
30905
30906   return ret;
30907 }
30908
30909 /* OpenMP 2.5:
30910    # pragma omp parallel parallel-clause[optseq] new-line
30911      structured-block
30912    # pragma omp parallel for parallel-for-clause[optseq] new-line
30913      structured-block
30914    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30915      structured-block
30916
30917    OpenMP 4.0:
30918    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30919      structured-block */
30920
30921 #define OMP_PARALLEL_CLAUSE_MASK                                \
30922         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
30923         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30924         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30925         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
30926         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
30927         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)       \
30928         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30929         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)  \
30930         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30931
30932 static tree
30933 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30934                         char *p_name, omp_clause_mask mask, tree *cclauses)
30935 {
30936   tree stmt, clauses, block;
30937   unsigned int save;
30938   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30939
30940   strcat (p_name, " parallel");
30941   mask |= OMP_PARALLEL_CLAUSE_MASK;
30942
30943   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30944     {
30945       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30946       if (cclauses == NULL)
30947         cclauses = cclauses_buf;
30948
30949       cp_lexer_consume_token (parser->lexer);
30950       if (!flag_openmp)  /* flag_openmp_simd  */
30951         return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30952       block = begin_omp_parallel ();
30953       save = cp_parser_begin_omp_structured_block (parser);
30954       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30955       cp_parser_end_omp_structured_block (parser, save);
30956       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30957                                   block);
30958       if (ret == NULL_TREE)
30959         return ret;
30960       OMP_PARALLEL_COMBINED (stmt) = 1;
30961       return stmt;
30962     }
30963   else if (cclauses)
30964     {
30965       error_at (loc, "expected %<for%> after %qs", p_name);
30966       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30967       return NULL_TREE;
30968     }
30969   else if (!flag_openmp)  /* flag_openmp_simd  */
30970     {
30971       cp_parser_require_pragma_eol (parser, pragma_tok);
30972       return NULL_TREE;
30973     }
30974   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30975     {
30976       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30977       const char *p = IDENTIFIER_POINTER (id);
30978       if (strcmp (p, "sections") == 0)
30979         {
30980           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30981           cclauses = cclauses_buf;
30982
30983           cp_lexer_consume_token (parser->lexer);
30984           block = begin_omp_parallel ();
30985           save = cp_parser_begin_omp_structured_block (parser);
30986           cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30987           cp_parser_end_omp_structured_block (parser, save);
30988           stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30989                                       block);
30990           OMP_PARALLEL_COMBINED (stmt) = 1;
30991           return stmt;
30992         }
30993     }
30994
30995   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30996
30997   block = begin_omp_parallel ();
30998   save = cp_parser_begin_omp_structured_block (parser);
30999   cp_parser_statement (parser, NULL_TREE, false, NULL);
31000   cp_parser_end_omp_structured_block (parser, save);
31001   stmt = finish_omp_parallel (clauses, block);
31002   return stmt;
31003 }
31004
31005 /* OpenMP 2.5:
31006    # pragma omp single single-clause[optseq] new-line
31007      structured-block  */
31008
31009 #define OMP_SINGLE_CLAUSE_MASK                                  \
31010         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31011         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31012         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)  \
31013         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31014
31015 static tree
31016 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31017 {
31018   tree stmt = make_node (OMP_SINGLE);
31019   TREE_TYPE (stmt) = void_type_node;
31020
31021   OMP_SINGLE_CLAUSES (stmt)
31022     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31023                                  "#pragma omp single", pragma_tok);
31024   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31025
31026   return add_stmt (stmt);
31027 }
31028
31029 /* OpenMP 3.0:
31030    # pragma omp task task-clause[optseq] new-line
31031      structured-block  */
31032
31033 #define OMP_TASK_CLAUSE_MASK                                    \
31034         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
31035         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
31036         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
31037         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31038         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31039         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
31040         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
31041         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
31042         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31043
31044 static tree
31045 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31046 {
31047   tree clauses, block;
31048   unsigned int save;
31049
31050   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31051                                        "#pragma omp task", pragma_tok);
31052   block = begin_omp_task ();
31053   save = cp_parser_begin_omp_structured_block (parser);
31054   cp_parser_statement (parser, NULL_TREE, false, NULL);
31055   cp_parser_end_omp_structured_block (parser, save);
31056   return finish_omp_task (clauses, block);
31057 }
31058
31059 /* OpenMP 3.0:
31060    # pragma omp taskwait new-line  */
31061
31062 static void
31063 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31064 {
31065   cp_parser_require_pragma_eol (parser, pragma_tok);
31066   finish_omp_taskwait ();
31067 }
31068
31069 /* OpenMP 3.1:
31070    # pragma omp taskyield new-line  */
31071
31072 static void
31073 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31074 {
31075   cp_parser_require_pragma_eol (parser, pragma_tok);
31076   finish_omp_taskyield ();
31077 }
31078
31079 /* OpenMP 4.0:
31080    # pragma omp taskgroup new-line
31081      structured-block  */
31082
31083 static tree
31084 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31085 {
31086   cp_parser_require_pragma_eol (parser, pragma_tok);
31087   return c_finish_omp_taskgroup (input_location,
31088                                  cp_parser_omp_structured_block (parser));
31089 }
31090
31091
31092 /* OpenMP 2.5:
31093    # pragma omp threadprivate (variable-list) */
31094
31095 static void
31096 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31097 {
31098   tree vars;
31099
31100   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31101   cp_parser_require_pragma_eol (parser, pragma_tok);
31102
31103   finish_omp_threadprivate (vars);
31104 }
31105
31106 /* OpenMP 4.0:
31107    # pragma omp cancel cancel-clause[optseq] new-line  */
31108
31109 #define OMP_CANCEL_CLAUSE_MASK                                  \
31110         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
31111         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
31112         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
31113         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)    \
31114         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31115
31116 static void
31117 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31118 {
31119   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31120                                             "#pragma omp cancel", pragma_tok);
31121   finish_omp_cancel (clauses);
31122 }
31123
31124 /* OpenMP 4.0:
31125    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
31126
31127 #define OMP_CANCELLATION_POINT_CLAUSE_MASK                      \
31128         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
31129         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
31130         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
31131         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31132
31133 static void
31134 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31135 {
31136   tree clauses;
31137   bool point_seen = false;
31138
31139   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31140     {
31141       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31142       const char *p = IDENTIFIER_POINTER (id);
31143
31144       if (strcmp (p, "point") == 0)
31145         {
31146           cp_lexer_consume_token (parser->lexer);
31147           point_seen = true;
31148         }
31149     }
31150   if (!point_seen)
31151     {
31152       cp_parser_error (parser, "expected %<point%>");
31153       cp_parser_require_pragma_eol (parser, pragma_tok);
31154       return;
31155     }
31156
31157   clauses = cp_parser_omp_all_clauses (parser,
31158                                        OMP_CANCELLATION_POINT_CLAUSE_MASK,
31159                                        "#pragma omp cancellation point",
31160                                        pragma_tok);
31161   finish_omp_cancellation_point (clauses);
31162 }
31163
31164 /* OpenMP 4.0:
31165    #pragma omp distribute distribute-clause[optseq] new-line
31166      for-loop  */
31167
31168 #define OMP_DISTRIBUTE_CLAUSE_MASK                              \
31169         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31170         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31171         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31172         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31173
31174 static tree
31175 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31176                           char *p_name, omp_clause_mask mask, tree *cclauses)
31177 {
31178   tree clauses, sb, ret;
31179   unsigned int save;
31180   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31181
31182   strcat (p_name, " distribute");
31183   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31184
31185   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31186     {
31187       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31188       const char *p = IDENTIFIER_POINTER (id);
31189       bool simd = false;
31190       bool parallel = false;
31191
31192       if (strcmp (p, "simd") == 0)
31193         simd = true;
31194       else
31195         parallel = strcmp (p, "parallel") == 0;
31196       if (parallel || simd)
31197         {
31198           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31199           if (cclauses == NULL)
31200             cclauses = cclauses_buf;
31201           cp_lexer_consume_token (parser->lexer);
31202           if (!flag_openmp)  /* flag_openmp_simd  */
31203             {
31204               if (simd)
31205                 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31206                                            cclauses);
31207               else
31208                 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31209                                                cclauses);
31210             }
31211           sb = begin_omp_structured_block ();
31212           save = cp_parser_begin_omp_structured_block (parser);
31213           if (simd)
31214             ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31215                                       cclauses);
31216           else
31217             ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31218                                           cclauses);
31219           cp_parser_end_omp_structured_block (parser, save);
31220           tree body = finish_omp_structured_block (sb);
31221           if (ret == NULL)
31222             return ret;
31223           ret = make_node (OMP_DISTRIBUTE);
31224           TREE_TYPE (ret) = void_type_node;
31225           OMP_FOR_BODY (ret) = body;
31226           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31227           SET_EXPR_LOCATION (ret, loc);
31228           add_stmt (ret);
31229           return ret;
31230         }
31231     }
31232   if (!flag_openmp)  /* flag_openmp_simd  */
31233     {
31234       cp_parser_require_pragma_eol (parser, pragma_tok);
31235       return NULL_TREE;
31236     }
31237
31238   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31239                                        cclauses == NULL);
31240   if (cclauses)
31241     {
31242       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31243       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31244     }
31245
31246   sb = begin_omp_structured_block ();
31247   save = cp_parser_begin_omp_structured_block (parser);
31248
31249   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31250
31251   cp_parser_end_omp_structured_block (parser, save);
31252   add_stmt (finish_omp_structured_block (sb));
31253
31254   return ret;
31255 }
31256
31257 /* OpenMP 4.0:
31258    # pragma omp teams teams-clause[optseq] new-line
31259      structured-block  */
31260
31261 #define OMP_TEAMS_CLAUSE_MASK                                   \
31262         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31263         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31264         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
31265         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
31266         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)    \
31267         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31268         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31269
31270 static tree
31271 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31272                      char *p_name, omp_clause_mask mask, tree *cclauses)
31273 {
31274   tree clauses, sb, ret;
31275   unsigned int save;
31276   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31277
31278   strcat (p_name, " teams");
31279   mask |= OMP_TEAMS_CLAUSE_MASK;
31280
31281   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31282     {
31283       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31284       const char *p = IDENTIFIER_POINTER (id);
31285       if (strcmp (p, "distribute") == 0)
31286         {
31287           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31288           if (cclauses == NULL)
31289             cclauses = cclauses_buf;
31290
31291           cp_lexer_consume_token (parser->lexer);
31292           if (!flag_openmp)  /* flag_openmp_simd  */
31293             return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31294                                              cclauses);
31295           sb = begin_omp_structured_block ();
31296           save = cp_parser_begin_omp_structured_block (parser);
31297           ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31298                                           cclauses);
31299           cp_parser_end_omp_structured_block (parser, save);
31300           tree body = finish_omp_structured_block (sb);
31301           if (ret == NULL)
31302             return ret;
31303           clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31304           ret = make_node (OMP_TEAMS);
31305           TREE_TYPE (ret) = void_type_node;
31306           OMP_TEAMS_CLAUSES (ret) = clauses;
31307           OMP_TEAMS_BODY (ret) = body;
31308           return add_stmt (ret);
31309         }
31310     }
31311   if (!flag_openmp)  /* flag_openmp_simd  */
31312     {
31313       cp_parser_require_pragma_eol (parser, pragma_tok);
31314       return NULL_TREE;
31315     }
31316
31317   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31318                                        cclauses == NULL);
31319   if (cclauses)
31320     {
31321       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31322       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31323     }
31324
31325   tree stmt = make_node (OMP_TEAMS);
31326   TREE_TYPE (stmt) = void_type_node;
31327   OMP_TEAMS_CLAUSES (stmt) = clauses;
31328   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31329
31330   return add_stmt (stmt);
31331 }
31332
31333 /* OpenMP 4.0:
31334    # pragma omp target data target-data-clause[optseq] new-line
31335      structured-block  */
31336
31337 #define OMP_TARGET_DATA_CLAUSE_MASK                             \
31338         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31339         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
31340         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31341
31342 static tree
31343 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31344 {
31345   tree stmt = make_node (OMP_TARGET_DATA);
31346   TREE_TYPE (stmt) = void_type_node;
31347
31348   OMP_TARGET_DATA_CLAUSES (stmt)
31349     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31350                                  "#pragma omp target data", pragma_tok);
31351   keep_next_level (true);
31352   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31353
31354   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31355   return add_stmt (stmt);
31356 }
31357
31358 /* OpenMP 4.0:
31359    # pragma omp target update target-update-clause[optseq] new-line */
31360
31361 #define OMP_TARGET_UPDATE_CLAUSE_MASK                           \
31362         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)         \
31363         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
31364         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31365         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31366
31367 static bool
31368 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31369                              enum pragma_context context)
31370 {
31371   if (context == pragma_stmt)
31372     {
31373       error_at (pragma_tok->location,
31374                 "%<#pragma omp target update%> may only be "
31375                 "used in compound statements");
31376       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31377       return false;
31378     }
31379
31380   tree clauses
31381     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31382                                  "#pragma omp target update", pragma_tok);
31383   if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31384       && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31385     {
31386       error_at (pragma_tok->location,
31387                 "%<#pragma omp target update must contain at least one "
31388                 "%<from%> or %<to%> clauses");
31389       return false;
31390     }
31391
31392   tree stmt = make_node (OMP_TARGET_UPDATE);
31393   TREE_TYPE (stmt) = void_type_node;
31394   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31395   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31396   add_stmt (stmt);
31397   return false;
31398 }
31399
31400 /* OpenMP 4.0:
31401    # pragma omp target target-clause[optseq] new-line
31402      structured-block  */
31403
31404 #define OMP_TARGET_CLAUSE_MASK                                  \
31405         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31406         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
31407         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31408
31409 static bool
31410 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31411                       enum pragma_context context)
31412 {
31413   if (context != pragma_stmt && context != pragma_compound)
31414     {
31415       cp_parser_error (parser, "expected declaration specifiers");
31416       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31417       return false;
31418     }
31419
31420   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31421     {
31422       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31423       const char *p = IDENTIFIER_POINTER (id);
31424
31425       if (strcmp (p, "teams") == 0)
31426         {
31427           tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31428           char p_name[sizeof ("#pragma omp target teams distribute "
31429                               "parallel for simd")];
31430
31431           cp_lexer_consume_token (parser->lexer);
31432           strcpy (p_name, "#pragma omp target");
31433           if (!flag_openmp)  /* flag_openmp_simd  */
31434             {
31435               tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31436                                                OMP_TARGET_CLAUSE_MASK,
31437                                                cclauses);
31438               return stmt != NULL_TREE;
31439             }
31440           keep_next_level (true);
31441           tree sb = begin_omp_structured_block ();
31442           unsigned save = cp_parser_begin_omp_structured_block (parser);
31443           tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31444                                           OMP_TARGET_CLAUSE_MASK, cclauses);
31445           cp_parser_end_omp_structured_block (parser, save);
31446           tree body = finish_omp_structured_block (sb);
31447           if (ret == NULL_TREE)
31448             return false;
31449           tree stmt = make_node (OMP_TARGET);
31450           TREE_TYPE (stmt) = void_type_node;
31451           OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31452           OMP_TARGET_BODY (stmt) = body;
31453           add_stmt (stmt);
31454           return true;
31455         }
31456       else if (!flag_openmp)  /* flag_openmp_simd  */
31457         {
31458           cp_parser_require_pragma_eol (parser, pragma_tok);
31459           return false;
31460         }
31461       else if (strcmp (p, "data") == 0)
31462         {
31463           cp_lexer_consume_token (parser->lexer);
31464           cp_parser_omp_target_data (parser, pragma_tok);
31465           return true;
31466         }
31467       else if (strcmp (p, "update") == 0)
31468         {
31469           cp_lexer_consume_token (parser->lexer);
31470           return cp_parser_omp_target_update (parser, pragma_tok, context);
31471         }
31472     }
31473
31474   tree stmt = make_node (OMP_TARGET);
31475   TREE_TYPE (stmt) = void_type_node;
31476
31477   OMP_TARGET_CLAUSES (stmt)
31478     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31479                                  "#pragma omp target", pragma_tok);
31480   keep_next_level (true);
31481   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31482
31483   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31484   add_stmt (stmt);
31485   return true;
31486 }
31487
31488 /* OpenACC 2.0:
31489    # pragma acc cache (variable-list) new-line
31490 */
31491
31492 static tree
31493 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31494 {
31495   tree stmt, clauses;
31496
31497   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31498   clauses = finish_omp_clauses (clauses);
31499
31500   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31501
31502   stmt = make_node (OACC_CACHE);
31503   TREE_TYPE (stmt) = void_type_node;
31504   OACC_CACHE_CLAUSES (stmt) = clauses;
31505   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31506   add_stmt (stmt);
31507
31508   return stmt;
31509 }
31510
31511 /* OpenACC 2.0:
31512    # pragma acc data oacc-data-clause[optseq] new-line
31513      structured-block  */
31514
31515 #define OACC_DATA_CLAUSE_MASK                                           \
31516         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31517         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31518         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31519         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31520         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31521         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31522         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31523         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31524         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31525         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31526         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31527
31528 static tree
31529 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31530 {
31531   tree stmt, clauses, block;
31532   unsigned int save;
31533
31534   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31535                                         "#pragma acc data", pragma_tok);
31536
31537   block = begin_omp_parallel ();
31538   save = cp_parser_begin_omp_structured_block (parser);
31539   cp_parser_statement (parser, NULL_TREE, false, NULL);
31540   cp_parser_end_omp_structured_block (parser, save);
31541   stmt = finish_oacc_data (clauses, block);
31542   return stmt;
31543 }
31544
31545 /* OpenACC 2.0:
31546    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31547
31548    or
31549
31550    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31551
31552    LOC is the location of the #pragma token.
31553 */
31554
31555 #define OACC_ENTER_DATA_CLAUSE_MASK                                     \
31556         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31557         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31558         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31559         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31560         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31561         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31562         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31563
31564 #define OACC_EXIT_DATA_CLAUSE_MASK                                      \
31565         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31566         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31567         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31568         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE)              \
31569         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31570
31571 static tree
31572 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31573                                 bool enter)
31574 {
31575   tree stmt, clauses;
31576
31577   if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31578      || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31579     {
31580       cp_parser_error (parser, enter
31581                        ? "expected %<data%> in %<#pragma acc enter data%>"
31582                        : "expected %<data%> in %<#pragma acc exit data%>");
31583       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31584       return NULL_TREE;
31585     }
31586
31587   const char *p =
31588     IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31589   if (strcmp (p, "data") != 0)
31590     {
31591       cp_parser_error (parser, "invalid pragma");
31592       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31593       return NULL_TREE;
31594     }
31595
31596   cp_lexer_consume_token (parser->lexer);
31597
31598   if (enter)
31599     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31600                                          "#pragma acc enter data", pragma_tok);
31601   else
31602     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31603                                          "#pragma acc exit data", pragma_tok);
31604
31605   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31606     {
31607       error_at (pragma_tok->location,
31608                 "%<#pragma acc enter data%> has no data movement clause");
31609       return NULL_TREE;
31610     }
31611
31612   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31613   TREE_TYPE (stmt) = void_type_node;
31614   if (enter)
31615     OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
31616   else
31617     OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
31618   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31619   add_stmt (stmt);
31620   return stmt;
31621 }
31622
31623 /* OpenACC 2.0:
31624    # pragma acc kernels oacc-kernels-clause[optseq] new-line
31625      structured-block  */
31626
31627 #define OACC_KERNELS_CLAUSE_MASK                                        \
31628         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31629         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31630         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31631         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31632         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31633         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31634         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31635         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31636         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31637         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31638         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31639         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31640         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31641
31642 static tree
31643 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31644 {
31645   tree stmt, clauses, block;
31646   unsigned int save;
31647
31648   clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31649                                         "#pragma acc kernels", pragma_tok);
31650
31651   block = begin_omp_parallel ();
31652   save = cp_parser_begin_omp_structured_block (parser);
31653   cp_parser_statement (parser, NULL_TREE, false, NULL);
31654   cp_parser_end_omp_structured_block (parser, save);
31655   stmt = finish_oacc_kernels (clauses, block);
31656   return stmt;
31657 }
31658
31659 /* OpenACC 2.0:
31660    # pragma acc loop oacc-loop-clause[optseq] new-line
31661      structured-block  */
31662
31663 #define OACC_LOOP_CLAUSE_MASK                                           \
31664         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)            \
31665         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31666
31667 static tree
31668 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31669 {
31670   tree stmt, clauses, block;
31671   int save;
31672
31673   clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31674                                         "#pragma acc loop", pragma_tok);
31675
31676   block = begin_omp_structured_block ();
31677   save = cp_parser_begin_omp_structured_block (parser);
31678   stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31679   cp_parser_end_omp_structured_block (parser, save);
31680   add_stmt (finish_omp_structured_block (block));
31681   return stmt;
31682 }
31683
31684 /* OpenACC 2.0:
31685    # pragma acc parallel oacc-parallel-clause[optseq] new-line
31686      structured-block  */
31687
31688 #define OACC_PARALLEL_CLAUSE_MASK                                       \
31689         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31690         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31691         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31692         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31693         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31694         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31695         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31696         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
31697         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
31698         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31699         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31700         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31701         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31702         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31703         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
31704         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
31705         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31706
31707 static tree
31708 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31709 {
31710   tree stmt, clauses, block;
31711   unsigned int save;
31712
31713   clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31714                                          "#pragma acc parallel", pragma_tok);
31715
31716   block = begin_omp_parallel ();
31717   save = cp_parser_begin_omp_structured_block (parser);
31718   cp_parser_statement (parser, NULL_TREE, false, NULL);
31719   cp_parser_end_omp_structured_block (parser, save);
31720   stmt = finish_oacc_parallel (clauses, block);
31721   return stmt;
31722 }
31723
31724 /* OpenACC 2.0:
31725    # pragma acc update oacc-update-clause[optseq] new-line
31726 */
31727
31728 #define OACC_UPDATE_CLAUSE_MASK                                         \
31729         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31730         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)              \
31731         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)                \
31732         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31733         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)                \
31734         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31735
31736 static tree
31737 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31738 {
31739   tree stmt, clauses;
31740
31741   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31742                                          "#pragma acc update", pragma_tok);
31743
31744   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31745     {
31746       error_at (pragma_tok->location,
31747                 "%<#pragma acc update%> must contain at least one "
31748                 "%<device%> or %<host/self%> clause");
31749       return NULL_TREE;
31750     }
31751
31752   stmt = make_node (OACC_UPDATE);
31753   TREE_TYPE (stmt) = void_type_node;
31754   OACC_UPDATE_CLAUSES (stmt) = clauses;
31755   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31756   add_stmt (stmt);
31757   return stmt;
31758 }
31759
31760 /* OpenACC 2.0:
31761    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31762
31763    LOC is the location of the #pragma token.
31764 */
31765
31766 #define OACC_WAIT_CLAUSE_MASK                                   \
31767         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31768
31769 static tree
31770 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31771 {
31772   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31773   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31774
31775   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31776     list = cp_parser_oacc_wait_list (parser, loc, list);
31777
31778   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31779                                         "#pragma acc wait", pragma_tok);
31780
31781   stmt = c_finish_oacc_wait (loc, list, clauses);
31782
31783   return stmt;
31784 }
31785
31786 /* OpenMP 4.0:
31787    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
31788
31789 #define OMP_DECLARE_SIMD_CLAUSE_MASK                            \
31790         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
31791         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
31792         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
31793         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)      \
31794         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)     \
31795         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31796
31797 static void
31798 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31799                             enum pragma_context context)
31800 {
31801   bool first_p = parser->omp_declare_simd == NULL;
31802   cp_omp_declare_simd_data data;
31803   if (first_p)
31804     {
31805       data.error_seen = false;
31806       data.fndecl_seen = false;
31807       data.tokens = vNULL;
31808       parser->omp_declare_simd = &data;
31809     }
31810   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31811          && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31812     cp_lexer_consume_token (parser->lexer);
31813   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31814     parser->omp_declare_simd->error_seen = true;
31815   cp_parser_require_pragma_eol (parser, pragma_tok);
31816   struct cp_token_cache *cp
31817     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31818   parser->omp_declare_simd->tokens.safe_push (cp);
31819   if (first_p)
31820     {
31821       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31822         cp_parser_pragma (parser, context);
31823       switch (context)
31824         {
31825         case pragma_external:
31826           cp_parser_declaration (parser);
31827           break;
31828         case pragma_member:
31829           cp_parser_member_declaration (parser);
31830           break;
31831         case pragma_objc_icode:
31832           cp_parser_block_declaration (parser, /*statement_p=*/false);
31833           break;
31834         default:
31835           cp_parser_declaration_statement (parser);
31836           break;
31837         }
31838       if (parser->omp_declare_simd
31839           && !parser->omp_declare_simd->error_seen
31840           && !parser->omp_declare_simd->fndecl_seen)
31841         error_at (pragma_tok->location,
31842                   "%<#pragma omp declare simd%> not immediately followed by "
31843                   "function declaration or definition");
31844       data.tokens.release ();
31845       parser->omp_declare_simd = NULL;
31846     }
31847 }
31848
31849 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.  
31850    This function is modelled similar to the late parsing of omp declare 
31851    simd.  */
31852
31853 static tree
31854 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31855 {
31856   struct cp_token_cache *ce;
31857   cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31858   int ii = 0;
31859
31860   if (parser->omp_declare_simd != NULL)
31861     {
31862       error ("%<#pragma omp declare simd%> cannot be used in the same function"
31863              " marked as a Cilk Plus SIMD-enabled function");
31864       XDELETE (parser->cilk_simd_fn_info);
31865       parser->cilk_simd_fn_info = NULL;
31866       return attrs;
31867     }
31868   if (!info->error_seen && info->fndecl_seen)
31869     {
31870       error ("vector attribute not immediately followed by a single function"
31871              " declaration or definition");
31872       info->error_seen = true;
31873     }
31874   if (info->error_seen)
31875     return attrs;
31876
31877   FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31878     {
31879       tree c, cl;
31880
31881       cp_parser_push_lexer_for_tokens (parser, ce);
31882       parser->lexer->in_pragma = true;
31883       cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31884                                       "SIMD-enabled functions attribute", 
31885                                       NULL);
31886       cp_parser_pop_lexer (parser);
31887       if (cl)
31888         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31889
31890       c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31891       TREE_CHAIN (c) = attrs;
31892       attrs = c;
31893
31894       c = build_tree_list (get_identifier ("omp declare simd"), cl);
31895       TREE_CHAIN (c) = attrs;
31896       if (processing_template_decl)
31897         ATTR_IS_DEPENDENT (c) = 1;
31898       attrs = c;
31899     }
31900   info->fndecl_seen = true;
31901   XDELETE (parser->cilk_simd_fn_info);
31902   parser->cilk_simd_fn_info = NULL;
31903   return attrs;
31904 }
31905
31906 /* Finalize #pragma omp declare simd clauses after direct declarator has
31907    been parsed, and put that into "omp declare simd" attribute.  */
31908
31909 static tree
31910 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31911 {
31912   struct cp_token_cache *ce;
31913   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31914   int i;
31915
31916   if (!data->error_seen && data->fndecl_seen)
31917     {
31918       error ("%<#pragma omp declare simd%> not immediately followed by "
31919              "a single function declaration or definition");
31920       data->error_seen = true;
31921       return attrs;
31922     }
31923   if (data->error_seen)
31924     return attrs;
31925
31926   FOR_EACH_VEC_ELT (data->tokens, i, ce)
31927     {
31928       tree c, cl;
31929
31930       cp_parser_push_lexer_for_tokens (parser, ce);
31931       parser->lexer->in_pragma = true;
31932       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31933       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31934       cp_lexer_consume_token (parser->lexer);
31935       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31936                                       "#pragma omp declare simd", pragma_tok);
31937       cp_parser_pop_lexer (parser);
31938       if (cl)
31939         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
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
31947   data->fndecl_seen = true;
31948   return attrs;
31949 }
31950
31951
31952 /* OpenMP 4.0:
31953    # pragma omp declare target new-line
31954    declarations and definitions
31955    # pragma omp end declare target new-line  */
31956
31957 static void
31958 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31959 {
31960   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31961   scope_chain->omp_declare_target_attribute++;
31962 }
31963
31964 static void
31965 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31966 {
31967   const char *p = "";
31968   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31969     {
31970       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31971       p = IDENTIFIER_POINTER (id);
31972     }
31973   if (strcmp (p, "declare") == 0)
31974     {
31975       cp_lexer_consume_token (parser->lexer);
31976       p = "";
31977       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31978         {
31979           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31980           p = IDENTIFIER_POINTER (id);
31981         }
31982       if (strcmp (p, "target") == 0)
31983         cp_lexer_consume_token (parser->lexer);
31984       else
31985         {
31986           cp_parser_error (parser, "expected %<target%>");
31987           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31988           return;
31989         }
31990     }
31991   else
31992     {
31993       cp_parser_error (parser, "expected %<declare%>");
31994       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31995       return;
31996     }
31997   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31998   if (!scope_chain->omp_declare_target_attribute)
31999     error_at (pragma_tok->location,
32000               "%<#pragma omp end declare target%> without corresponding "
32001               "%<#pragma omp declare target%>");
32002   else
32003     scope_chain->omp_declare_target_attribute--;
32004 }
32005
32006 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
32007    expression and optional initializer clause of
32008    #pragma omp declare reduction.  We store the expression(s) as
32009    either 3, 6 or 7 special statements inside of the artificial function's
32010    body.  The first two statements are DECL_EXPRs for the artificial
32011    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32012    expression that uses those variables.
32013    If there was any INITIALIZER clause, this is followed by further statements,
32014    the fourth and fifth statements are DECL_EXPRs for the artificial
32015    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
32016    constructor variant (first token after open paren is not omp_priv),
32017    then the sixth statement is a statement with the function call expression
32018    that uses the OMP_PRIV and optionally OMP_ORIG variable.
32019    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32020    to initialize the OMP_PRIV artificial variable and there is seventh
32021    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
32022
32023 static bool
32024 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32025 {
32026   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32027   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32028   type = TREE_TYPE (type);
32029   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32030   DECL_ARTIFICIAL (omp_out) = 1;
32031   pushdecl (omp_out);
32032   add_decl_expr (omp_out);
32033   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32034   DECL_ARTIFICIAL (omp_in) = 1;
32035   pushdecl (omp_in);
32036   add_decl_expr (omp_in);
32037   tree combiner;
32038   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32039
32040   keep_next_level (true);
32041   tree block = begin_omp_structured_block ();
32042   combiner = cp_parser_expression (parser);
32043   finish_expr_stmt (combiner);
32044   block = finish_omp_structured_block (block);
32045   add_stmt (block);
32046
32047   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32048     return false;
32049
32050   const char *p = "";
32051   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32052     {
32053       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32054       p = IDENTIFIER_POINTER (id);
32055     }
32056
32057   if (strcmp (p, "initializer") == 0)
32058     {
32059       cp_lexer_consume_token (parser->lexer);
32060       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32061         return false;
32062
32063       p = "";
32064       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32065         {
32066           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32067           p = IDENTIFIER_POINTER (id);
32068         }
32069
32070       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32071       DECL_ARTIFICIAL (omp_priv) = 1;
32072       pushdecl (omp_priv);
32073       add_decl_expr (omp_priv);
32074       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32075       DECL_ARTIFICIAL (omp_orig) = 1;
32076       pushdecl (omp_orig);
32077       add_decl_expr (omp_orig);
32078
32079       keep_next_level (true);
32080       block = begin_omp_structured_block ();
32081
32082       bool ctor = false;
32083       if (strcmp (p, "omp_priv") == 0)
32084         {
32085           bool is_direct_init, is_non_constant_init;
32086           ctor = true;
32087           cp_lexer_consume_token (parser->lexer);
32088           /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
32089           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32090               || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32091                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32092                      == CPP_CLOSE_PAREN
32093                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32094                      == CPP_CLOSE_PAREN))
32095             {
32096               finish_omp_structured_block (block);
32097               error ("invalid initializer clause");
32098               return false;
32099             }
32100           initializer = cp_parser_initializer (parser, &is_direct_init,
32101                                                &is_non_constant_init);
32102           cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32103                           NULL_TREE, LOOKUP_ONLYCONVERTING);
32104         }
32105       else
32106         {
32107           cp_parser_parse_tentatively (parser);
32108           tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32109                                                   /*check_dependency_p=*/true,
32110                                                   /*template_p=*/NULL,
32111                                                   /*declarator_p=*/false,
32112                                                   /*optional_p=*/false);
32113           vec<tree, va_gc> *args;
32114           if (fn_name == error_mark_node
32115               || cp_parser_error_occurred (parser)
32116               || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32117               || ((args = cp_parser_parenthesized_expression_list
32118                                 (parser, non_attr, /*cast_p=*/false,
32119                                  /*allow_expansion_p=*/true,
32120                                  /*non_constant_p=*/NULL)),
32121                   cp_parser_error_occurred (parser)))
32122             {
32123               finish_omp_structured_block (block);
32124               cp_parser_abort_tentative_parse (parser);
32125               cp_parser_error (parser, "expected id-expression (arguments)");
32126               return false;
32127             }
32128           unsigned int i;
32129           tree arg;
32130           FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32131             if (arg == omp_priv
32132                 || (TREE_CODE (arg) == ADDR_EXPR
32133                     && TREE_OPERAND (arg, 0) == omp_priv))
32134               break;
32135           cp_parser_abort_tentative_parse (parser);
32136           if (arg == NULL_TREE)
32137             error ("one of the initializer call arguments should be %<omp_priv%>"
32138                    " or %<&omp_priv%>");
32139           initializer = cp_parser_postfix_expression (parser, false, false, false,
32140                                                       false, NULL);
32141           finish_expr_stmt (initializer);
32142         }
32143
32144       block = finish_omp_structured_block (block);
32145       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32146       add_stmt (block);
32147
32148       if (ctor)
32149         add_decl_expr (omp_orig);
32150
32151       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32152         return false;
32153     }
32154
32155   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32156     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32157
32158   return true;
32159 }
32160
32161 /* OpenMP 4.0
32162    #pragma omp declare reduction (reduction-id : typename-list : expression) \
32163       initializer-clause[opt] new-line
32164
32165    initializer-clause:
32166       initializer (omp_priv initializer)
32167       initializer (function-name (argument-list))  */
32168
32169 static void
32170 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32171                                  enum pragma_context)
32172 {
32173   auto_vec<tree> types;
32174   enum tree_code reduc_code = ERROR_MARK;
32175   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32176   unsigned int i;
32177   cp_token *first_token;
32178   cp_token_cache *cp;
32179   int errs;
32180   void *p;
32181     
32182   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
32183   p = obstack_alloc (&declarator_obstack, 0);
32184
32185   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32186     goto fail;
32187
32188   switch (cp_lexer_peek_token (parser->lexer)->type)
32189     {
32190     case CPP_PLUS:
32191       reduc_code = PLUS_EXPR;
32192       break;
32193     case CPP_MULT:
32194       reduc_code = MULT_EXPR;
32195       break;
32196     case CPP_MINUS:
32197       reduc_code = MINUS_EXPR;
32198       break;
32199     case CPP_AND:
32200       reduc_code = BIT_AND_EXPR;
32201       break;
32202     case CPP_XOR:
32203       reduc_code = BIT_XOR_EXPR;
32204       break;
32205     case CPP_OR:
32206       reduc_code = BIT_IOR_EXPR;
32207       break;
32208     case CPP_AND_AND:
32209       reduc_code = TRUTH_ANDIF_EXPR;
32210       break;
32211     case CPP_OR_OR:
32212       reduc_code = TRUTH_ORIF_EXPR;
32213       break;
32214     case CPP_NAME:
32215       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32216       break;
32217     default:
32218       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32219                                "%<|%>, %<&&%>, %<||%> or identifier");
32220       goto fail;
32221     }
32222
32223   if (reduc_code != ERROR_MARK)
32224     cp_lexer_consume_token (parser->lexer);
32225
32226   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32227   if (reduc_id == error_mark_node)
32228     goto fail;
32229
32230   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32231     goto fail;
32232
32233   /* Types may not be defined in declare reduction type list.  */
32234   const char *saved_message;
32235   saved_message = parser->type_definition_forbidden_message;
32236   parser->type_definition_forbidden_message
32237     = G_("types may not be defined in declare reduction type list");
32238   bool saved_colon_corrects_to_scope_p;
32239   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32240   parser->colon_corrects_to_scope_p = false;
32241   bool saved_colon_doesnt_start_class_def_p;
32242   saved_colon_doesnt_start_class_def_p
32243     = parser->colon_doesnt_start_class_def_p;
32244   parser->colon_doesnt_start_class_def_p = true;
32245
32246   while (true)
32247     {
32248       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32249       type = cp_parser_type_id (parser);
32250       if (type == error_mark_node)
32251         ;
32252       else if (ARITHMETIC_TYPE_P (type)
32253                && (orig_reduc_id == NULL_TREE
32254                    || (TREE_CODE (type) != COMPLEX_TYPE
32255                        && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32256                                    "min") == 0
32257                            || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32258                                       "max") == 0))))
32259         error_at (loc, "predeclared arithmetic type %qT in "
32260                        "%<#pragma omp declare reduction%>", type);
32261       else if (TREE_CODE (type) == FUNCTION_TYPE
32262                || TREE_CODE (type) == METHOD_TYPE
32263                || TREE_CODE (type) == ARRAY_TYPE)
32264         error_at (loc, "function or array type %qT in "
32265                        "%<#pragma omp declare reduction%>", type);
32266       else if (TREE_CODE (type) == REFERENCE_TYPE)
32267         error_at (loc, "reference type %qT in "
32268                        "%<#pragma omp declare reduction%>", type);
32269       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32270         error_at (loc, "const, volatile or __restrict qualified type %qT in "
32271                        "%<#pragma omp declare reduction%>", type);
32272       else
32273         types.safe_push (type);
32274
32275       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32276         cp_lexer_consume_token (parser->lexer);
32277       else
32278         break;
32279     }
32280
32281   /* Restore the saved message.  */
32282   parser->type_definition_forbidden_message = saved_message;
32283   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32284   parser->colon_doesnt_start_class_def_p
32285     = saved_colon_doesnt_start_class_def_p;
32286
32287   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32288       || types.is_empty ())
32289     {
32290      fail:
32291       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32292       goto done;
32293     }
32294
32295   first_token = cp_lexer_peek_token (parser->lexer);
32296   cp = NULL;
32297   errs = errorcount;
32298   FOR_EACH_VEC_ELT (types, i, type)
32299     {
32300       tree fntype
32301         = build_function_type_list (void_type_node,
32302                                     cp_build_reference_type (type, false),
32303                                     NULL_TREE);
32304       tree this_reduc_id = reduc_id;
32305       if (!dependent_type_p (type))
32306         this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32307       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32308       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32309       DECL_ARTIFICIAL (fndecl) = 1;
32310       DECL_EXTERNAL (fndecl) = 1;
32311       DECL_DECLARED_INLINE_P (fndecl) = 1;
32312       DECL_IGNORED_P (fndecl) = 1;
32313       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32314       DECL_ATTRIBUTES (fndecl)
32315         = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32316                      DECL_ATTRIBUTES (fndecl));
32317       if (processing_template_decl)
32318         fndecl = push_template_decl (fndecl);
32319       bool block_scope = false;
32320       tree block = NULL_TREE;
32321       if (current_function_decl)
32322         {
32323           block_scope = true;
32324           DECL_CONTEXT (fndecl) = global_namespace;
32325           if (!processing_template_decl)
32326             pushdecl (fndecl);
32327         }
32328       else if (current_class_type)
32329         {
32330           if (cp == NULL)
32331             {
32332               while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32333                      && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32334                 cp_lexer_consume_token (parser->lexer);
32335               if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32336                 goto fail;
32337               cp = cp_token_cache_new (first_token,
32338                                        cp_lexer_peek_nth_token (parser->lexer,
32339                                                                 2));
32340             }
32341           DECL_STATIC_FUNCTION_P (fndecl) = 1;
32342           finish_member_declaration (fndecl);
32343           DECL_PENDING_INLINE_INFO (fndecl) = cp;
32344           DECL_PENDING_INLINE_P (fndecl) = 1;
32345           vec_safe_push (unparsed_funs_with_definitions, fndecl);
32346           continue;
32347         }
32348       else
32349         {
32350           DECL_CONTEXT (fndecl) = current_namespace;
32351           pushdecl (fndecl);
32352         }
32353       if (!block_scope)
32354         start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32355       else
32356         block = begin_omp_structured_block ();
32357       if (cp)
32358         {
32359           cp_parser_push_lexer_for_tokens (parser, cp);
32360           parser->lexer->in_pragma = true;
32361         }
32362       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32363         {
32364           if (!block_scope)
32365             finish_function (0);
32366           else
32367             DECL_CONTEXT (fndecl) = current_function_decl;
32368           if (cp)
32369             cp_parser_pop_lexer (parser);
32370           goto fail;
32371         }
32372       if (cp)
32373         cp_parser_pop_lexer (parser);
32374       if (!block_scope)
32375         finish_function (0);
32376       else
32377         {
32378           DECL_CONTEXT (fndecl) = current_function_decl;
32379           block = finish_omp_structured_block (block);
32380           if (TREE_CODE (block) == BIND_EXPR)
32381             DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32382           else if (TREE_CODE (block) == STATEMENT_LIST)
32383             DECL_SAVED_TREE (fndecl) = block;
32384           if (processing_template_decl)
32385             add_decl_expr (fndecl);
32386         }
32387       cp_check_omp_declare_reduction (fndecl);
32388       if (cp == NULL && types.length () > 1)
32389         cp = cp_token_cache_new (first_token,
32390                                  cp_lexer_peek_nth_token (parser->lexer, 2));
32391       if (errs != errorcount)
32392         break;
32393     }
32394
32395   cp_parser_require_pragma_eol (parser, pragma_tok);
32396
32397  done:
32398   /* Free any declarators allocated.  */
32399   obstack_free (&declarator_obstack, p);
32400 }
32401
32402 /* OpenMP 4.0
32403    #pragma omp declare simd declare-simd-clauses[optseq] new-line
32404    #pragma omp declare reduction (reduction-id : typename-list : expression) \
32405       initializer-clause[opt] new-line
32406    #pragma omp declare target new-line  */
32407
32408 static void
32409 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32410                        enum pragma_context context)
32411 {
32412   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32413     {
32414       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32415       const char *p = IDENTIFIER_POINTER (id);
32416
32417       if (strcmp (p, "simd") == 0)
32418         {
32419           cp_lexer_consume_token (parser->lexer);
32420           cp_parser_omp_declare_simd (parser, pragma_tok,
32421                                       context);
32422           return;
32423         }
32424       cp_ensure_no_omp_declare_simd (parser);
32425       if (strcmp (p, "reduction") == 0)
32426         {
32427           cp_lexer_consume_token (parser->lexer);
32428           cp_parser_omp_declare_reduction (parser, pragma_tok,
32429                                            context);
32430           return;
32431         }
32432       if (!flag_openmp)  /* flag_openmp_simd  */
32433         {
32434           cp_parser_require_pragma_eol (parser, pragma_tok);
32435           return;
32436         }
32437       if (strcmp (p, "target") == 0)
32438         {
32439           cp_lexer_consume_token (parser->lexer);
32440           cp_parser_omp_declare_target (parser, pragma_tok);
32441           return;
32442         }
32443     }
32444   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32445                            "or %<target%>");
32446   cp_parser_require_pragma_eol (parser, pragma_tok);
32447 }
32448
32449 /* Main entry point to OpenMP statement pragmas.  */
32450
32451 static void
32452 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32453 {
32454   tree stmt;
32455   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32456   omp_clause_mask mask (0);
32457
32458   switch (pragma_tok->pragma_kind)
32459     {
32460     case PRAGMA_OACC_CACHE:
32461       stmt = cp_parser_oacc_cache (parser, pragma_tok);
32462       break;
32463     case PRAGMA_OACC_DATA:
32464       stmt = cp_parser_oacc_data (parser, pragma_tok);
32465       break;
32466     case PRAGMA_OACC_ENTER_DATA:
32467       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32468       break;
32469     case PRAGMA_OACC_EXIT_DATA:
32470       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32471       break;
32472     case PRAGMA_OACC_KERNELS:
32473       stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32474       break;
32475     case PRAGMA_OACC_LOOP:
32476       stmt = cp_parser_oacc_loop (parser, pragma_tok);
32477       break;
32478     case PRAGMA_OACC_PARALLEL:
32479       stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32480       break;
32481     case PRAGMA_OACC_UPDATE:
32482       stmt = cp_parser_oacc_update (parser, pragma_tok);
32483       break;
32484     case PRAGMA_OACC_WAIT:
32485       stmt = cp_parser_oacc_wait (parser, pragma_tok);
32486       break;
32487     case PRAGMA_OMP_ATOMIC:
32488       cp_parser_omp_atomic (parser, pragma_tok);
32489       return;
32490     case PRAGMA_OMP_CRITICAL:
32491       stmt = cp_parser_omp_critical (parser, pragma_tok);
32492       break;
32493     case PRAGMA_OMP_DISTRIBUTE:
32494       strcpy (p_name, "#pragma omp");
32495       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32496       break;
32497     case PRAGMA_OMP_FOR:
32498       strcpy (p_name, "#pragma omp");
32499       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32500       break;
32501     case PRAGMA_OMP_MASTER:
32502       stmt = cp_parser_omp_master (parser, pragma_tok);
32503       break;
32504     case PRAGMA_OMP_ORDERED:
32505       stmt = cp_parser_omp_ordered (parser, pragma_tok);
32506       break;
32507     case PRAGMA_OMP_PARALLEL:
32508       strcpy (p_name, "#pragma omp");
32509       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32510       break;
32511     case PRAGMA_OMP_SECTIONS:
32512       strcpy (p_name, "#pragma omp");
32513       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32514       break;
32515     case PRAGMA_OMP_SIMD:
32516       strcpy (p_name, "#pragma omp");
32517       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32518       break;
32519     case PRAGMA_OMP_SINGLE:
32520       stmt = cp_parser_omp_single (parser, pragma_tok);
32521       break;
32522     case PRAGMA_OMP_TASK:
32523       stmt = cp_parser_omp_task (parser, pragma_tok);
32524       break;
32525     case PRAGMA_OMP_TASKGROUP:
32526       stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32527       break;
32528     case PRAGMA_OMP_TEAMS:
32529       strcpy (p_name, "#pragma omp");
32530       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32531       break;
32532     default:
32533       gcc_unreachable ();
32534     }
32535
32536   if (stmt)
32537     SET_EXPR_LOCATION (stmt, pragma_tok->location);
32538 }
32539 \f
32540 /* Transactional Memory parsing routines.  */
32541
32542 /* Parse a transaction attribute.
32543
32544    txn-attribute:
32545         attribute
32546         [ [ identifier ] ]
32547
32548    ??? Simplify this when C++0x bracket attributes are
32549    implemented properly.  */
32550
32551 static tree
32552 cp_parser_txn_attribute_opt (cp_parser *parser)
32553 {
32554   cp_token *token;
32555   tree attr_name, attr = NULL;
32556
32557   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32558     return cp_parser_attributes_opt (parser);
32559
32560   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32561     return NULL_TREE;
32562   cp_lexer_consume_token (parser->lexer);
32563   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32564     goto error1;
32565
32566   token = cp_lexer_peek_token (parser->lexer);
32567   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32568     {
32569       token = cp_lexer_consume_token (parser->lexer);
32570
32571       attr_name = (token->type == CPP_KEYWORD
32572                    /* For keywords, use the canonical spelling,
32573                       not the parsed identifier.  */
32574                    ? ridpointers[(int) token->keyword]
32575                    : token->u.value);
32576       attr = build_tree_list (attr_name, NULL_TREE);
32577     }
32578   else
32579     cp_parser_error (parser, "expected identifier");
32580
32581   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32582  error1:
32583   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32584   return attr;
32585 }
32586
32587 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32588
32589    transaction-statement:
32590      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32591        compound-statement
32592      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32593 */
32594
32595 static tree
32596 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32597 {
32598   unsigned char old_in = parser->in_transaction;
32599   unsigned char this_in = 1, new_in;
32600   cp_token *token;
32601   tree stmt, attrs, noex;
32602
32603   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32604       || keyword == RID_TRANSACTION_RELAXED);
32605   token = cp_parser_require_keyword (parser, keyword,
32606       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32607           : RT_TRANSACTION_RELAXED));
32608   gcc_assert (token != NULL);
32609
32610   if (keyword == RID_TRANSACTION_RELAXED)
32611     this_in |= TM_STMT_ATTR_RELAXED;
32612   else
32613     {
32614       attrs = cp_parser_txn_attribute_opt (parser);
32615       if (attrs)
32616         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32617     }
32618
32619   /* Parse a noexcept specification.  */
32620   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32621
32622   /* Keep track if we're in the lexical scope of an outer transaction.  */
32623   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32624
32625   stmt = begin_transaction_stmt (token->location, NULL, this_in);
32626
32627   parser->in_transaction = new_in;
32628   cp_parser_compound_statement (parser, NULL, false, false);
32629   parser->in_transaction = old_in;
32630
32631   finish_transaction_stmt (stmt, NULL, this_in, noex);
32632
32633   return stmt;
32634 }
32635
32636 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32637
32638    transaction-expression:
32639      __transaction_atomic txn-noexcept-spec[opt] ( expression )
32640      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32641 */
32642
32643 static tree
32644 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32645 {
32646   unsigned char old_in = parser->in_transaction;
32647   unsigned char this_in = 1;
32648   cp_token *token;
32649   tree expr, noex;
32650   bool noex_expr;
32651
32652   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32653       || keyword == RID_TRANSACTION_RELAXED);
32654
32655   if (!flag_tm)
32656     error (keyword == RID_TRANSACTION_RELAXED
32657            ? G_("%<__transaction_relaxed%> without transactional memory "
32658                 "support enabled")
32659            : G_("%<__transaction_atomic%> without transactional memory "
32660                 "support enabled"));
32661
32662   token = cp_parser_require_keyword (parser, keyword,
32663       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32664           : RT_TRANSACTION_RELAXED));
32665   gcc_assert (token != NULL);
32666
32667   if (keyword == RID_TRANSACTION_RELAXED)
32668     this_in |= TM_STMT_ATTR_RELAXED;
32669
32670   /* Set this early.  This might mean that we allow transaction_cancel in
32671      an expression that we find out later actually has to be a constexpr.
32672      However, we expect that cxx_constant_value will be able to deal with
32673      this; also, if the noexcept has no constexpr, then what we parse next
32674      really is a transaction's body.  */
32675   parser->in_transaction = this_in;
32676
32677   /* Parse a noexcept specification.  */
32678   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32679                                                true);
32680
32681   if (!noex || !noex_expr
32682       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32683     {
32684       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32685
32686       expr = cp_parser_expression (parser);
32687       expr = finish_parenthesized_expr (expr);
32688
32689       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32690     }
32691   else
32692     {
32693       /* The only expression that is available got parsed for the noexcept
32694          already.  noexcept is true then.  */
32695       expr = noex;
32696       noex = boolean_true_node;
32697     }
32698
32699   expr = build_transaction_expr (token->location, expr, this_in, noex);
32700   parser->in_transaction = old_in;
32701
32702   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32703     return error_mark_node;
32704
32705   return (flag_tm ? expr : error_mark_node);
32706 }
32707
32708 /* Parse a function-transaction-block.
32709
32710    function-transaction-block:
32711      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32712          function-body
32713      __transaction_atomic txn-attribute[opt] function-try-block
32714      __transaction_relaxed ctor-initializer[opt] function-body
32715      __transaction_relaxed function-try-block
32716 */
32717
32718 static bool
32719 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32720 {
32721   unsigned char old_in = parser->in_transaction;
32722   unsigned char new_in = 1;
32723   tree compound_stmt, stmt, attrs;
32724   bool ctor_initializer_p;
32725   cp_token *token;
32726
32727   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32728       || keyword == RID_TRANSACTION_RELAXED);
32729   token = cp_parser_require_keyword (parser, keyword,
32730       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32731           : RT_TRANSACTION_RELAXED));
32732   gcc_assert (token != NULL);
32733
32734   if (keyword == RID_TRANSACTION_RELAXED)
32735     new_in |= TM_STMT_ATTR_RELAXED;
32736   else
32737     {
32738       attrs = cp_parser_txn_attribute_opt (parser);
32739       if (attrs)
32740         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32741     }
32742
32743   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32744
32745   parser->in_transaction = new_in;
32746
32747   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32748     ctor_initializer_p = cp_parser_function_try_block (parser);
32749   else
32750     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32751       (parser, /*in_function_try_block=*/false);
32752
32753   parser->in_transaction = old_in;
32754
32755   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32756
32757   return ctor_initializer_p;
32758 }
32759
32760 /* Parse a __transaction_cancel statement.
32761
32762    cancel-statement:
32763      __transaction_cancel txn-attribute[opt] ;
32764      __transaction_cancel txn-attribute[opt] throw-expression ;
32765
32766    ??? Cancel and throw is not yet implemented.  */
32767
32768 static tree
32769 cp_parser_transaction_cancel (cp_parser *parser)
32770 {
32771   cp_token *token;
32772   bool is_outer = false;
32773   tree stmt, attrs;
32774
32775   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32776                                      RT_TRANSACTION_CANCEL);
32777   gcc_assert (token != NULL);
32778
32779   attrs = cp_parser_txn_attribute_opt (parser);
32780   if (attrs)
32781     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32782
32783   /* ??? Parse cancel-and-throw here.  */
32784
32785   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32786
32787   if (!flag_tm)
32788     {
32789       error_at (token->location, "%<__transaction_cancel%> without "
32790                 "transactional memory support enabled");
32791       return error_mark_node;
32792     }
32793   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32794     {
32795       error_at (token->location, "%<__transaction_cancel%> within a "
32796                 "%<__transaction_relaxed%>");
32797       return error_mark_node;
32798     }
32799   else if (is_outer)
32800     {
32801       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32802           && !is_tm_may_cancel_outer (current_function_decl))
32803         {
32804           error_at (token->location, "outer %<__transaction_cancel%> not "
32805                     "within outer %<__transaction_atomic%>");
32806           error_at (token->location,
32807                     "  or a %<transaction_may_cancel_outer%> function");
32808           return error_mark_node;
32809         }
32810     }
32811   else if (parser->in_transaction == 0)
32812     {
32813       error_at (token->location, "%<__transaction_cancel%> not within "
32814                 "%<__transaction_atomic%>");
32815       return error_mark_node;
32816     }
32817
32818   stmt = build_tm_abort_call (token->location, is_outer);
32819   add_stmt (stmt);
32820
32821   return stmt;
32822 }
32823 \f
32824 /* The parser.  */
32825
32826 static GTY (()) cp_parser *the_parser;
32827
32828 \f
32829 /* Special handling for the first token or line in the file.  The first
32830    thing in the file might be #pragma GCC pch_preprocess, which loads a
32831    PCH file, which is a GC collection point.  So we need to handle this
32832    first pragma without benefit of an existing lexer structure.
32833
32834    Always returns one token to the caller in *FIRST_TOKEN.  This is
32835    either the true first token of the file, or the first token after
32836    the initial pragma.  */
32837
32838 static void
32839 cp_parser_initial_pragma (cp_token *first_token)
32840 {
32841   tree name = NULL;
32842
32843   cp_lexer_get_preprocessor_token (NULL, first_token);
32844   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32845     return;
32846
32847   cp_lexer_get_preprocessor_token (NULL, first_token);
32848   if (first_token->type == CPP_STRING)
32849     {
32850       name = first_token->u.value;
32851
32852       cp_lexer_get_preprocessor_token (NULL, first_token);
32853       if (first_token->type != CPP_PRAGMA_EOL)
32854         error_at (first_token->location,
32855                   "junk at end of %<#pragma GCC pch_preprocess%>");
32856     }
32857   else
32858     error_at (first_token->location, "expected string literal");
32859
32860   /* Skip to the end of the pragma.  */
32861   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32862     cp_lexer_get_preprocessor_token (NULL, first_token);
32863
32864   /* Now actually load the PCH file.  */
32865   if (name)
32866     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32867
32868   /* Read one more token to return to our caller.  We have to do this
32869      after reading the PCH file in, since its pointers have to be
32870      live.  */
32871   cp_lexer_get_preprocessor_token (NULL, first_token);
32872 }
32873
32874 /* Parses the grainsize pragma for the _Cilk_for statement.
32875    Syntax:
32876    #pragma cilk grainsize = <VALUE>.  */
32877
32878 static void
32879 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32880 {
32881   if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32882     {
32883       tree exp = cp_parser_binary_expression (parser, false, false,
32884                                               PREC_NOT_OPERATOR, NULL);
32885       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32886       if (!exp || exp == error_mark_node)
32887         {
32888           error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32889           return;
32890         }
32891
32892       /* Make sure the next token is _Cilk_for, it is invalid otherwise.  */
32893       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32894         cp_parser_cilk_for (parser, exp);
32895       else
32896         warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32897                     "%<#pragma cilk grainsize%> is not followed by "
32898                     "%<_Cilk_for%>");
32899       return;
32900     }
32901   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32902 }
32903
32904 /* Normal parsing of a pragma token.  Here we can (and must) use the
32905    regular lexer.  */
32906
32907 static bool
32908 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32909 {
32910   cp_token *pragma_tok;
32911   unsigned int id;
32912
32913   pragma_tok = cp_lexer_consume_token (parser->lexer);
32914   gcc_assert (pragma_tok->type == CPP_PRAGMA);
32915   parser->lexer->in_pragma = true;
32916
32917   id = pragma_tok->pragma_kind;
32918   if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32919     cp_ensure_no_omp_declare_simd (parser);
32920   switch (id)
32921     {
32922     case PRAGMA_GCC_PCH_PREPROCESS:
32923       error_at (pragma_tok->location,
32924                 "%<#pragma GCC pch_preprocess%> must be first");
32925       break;
32926
32927     case PRAGMA_OMP_BARRIER:
32928       switch (context)
32929         {
32930         case pragma_compound:
32931           cp_parser_omp_barrier (parser, pragma_tok);
32932           return false;
32933         case pragma_stmt:
32934           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32935                     "used in compound statements");
32936           break;
32937         default:
32938           goto bad_stmt;
32939         }
32940       break;
32941
32942     case PRAGMA_OMP_FLUSH:
32943       switch (context)
32944         {
32945         case pragma_compound:
32946           cp_parser_omp_flush (parser, pragma_tok);
32947           return false;
32948         case pragma_stmt:
32949           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32950                     "used in compound statements");
32951           break;
32952         default:
32953           goto bad_stmt;
32954         }
32955       break;
32956
32957     case PRAGMA_OMP_TASKWAIT:
32958       switch (context)
32959         {
32960         case pragma_compound:
32961           cp_parser_omp_taskwait (parser, pragma_tok);
32962           return false;
32963         case pragma_stmt:
32964           error_at (pragma_tok->location,
32965                     "%<#pragma omp taskwait%> may only be "
32966                     "used in compound statements");
32967           break;
32968         default:
32969           goto bad_stmt;
32970         }
32971       break;
32972
32973     case PRAGMA_OMP_TASKYIELD:
32974       switch (context)
32975         {
32976         case pragma_compound:
32977           cp_parser_omp_taskyield (parser, pragma_tok);
32978           return false;
32979         case pragma_stmt:
32980           error_at (pragma_tok->location,
32981                     "%<#pragma omp taskyield%> may only be "
32982                     "used in compound statements");
32983           break;
32984         default:
32985           goto bad_stmt;
32986         }
32987       break;
32988
32989     case PRAGMA_OMP_CANCEL:
32990       switch (context)
32991         {
32992         case pragma_compound:
32993           cp_parser_omp_cancel (parser, pragma_tok);
32994           return false;
32995         case pragma_stmt:
32996           error_at (pragma_tok->location,
32997                     "%<#pragma omp cancel%> may only be "
32998                     "used in compound statements");
32999           break;
33000         default:
33001           goto bad_stmt;
33002         }
33003       break;
33004
33005     case PRAGMA_OMP_CANCELLATION_POINT:
33006       switch (context)
33007         {
33008         case pragma_compound:
33009           cp_parser_omp_cancellation_point (parser, pragma_tok);
33010           return false;
33011         case pragma_stmt:
33012           error_at (pragma_tok->location,
33013                     "%<#pragma omp cancellation point%> may only be "
33014                     "used in compound statements");
33015           break;
33016         default:
33017           goto bad_stmt;
33018         }
33019       break;
33020
33021     case PRAGMA_OMP_THREADPRIVATE:
33022       cp_parser_omp_threadprivate (parser, pragma_tok);
33023       return false;
33024
33025     case PRAGMA_OMP_DECLARE_REDUCTION:
33026       cp_parser_omp_declare (parser, pragma_tok, context);
33027       return false;
33028
33029     case PRAGMA_OACC_CACHE:
33030     case PRAGMA_OACC_DATA:
33031     case PRAGMA_OACC_ENTER_DATA:
33032     case PRAGMA_OACC_EXIT_DATA:
33033     case PRAGMA_OACC_KERNELS:
33034     case PRAGMA_OACC_PARALLEL:
33035     case PRAGMA_OACC_LOOP:
33036     case PRAGMA_OACC_UPDATE:
33037     case PRAGMA_OACC_WAIT:
33038     case PRAGMA_OMP_ATOMIC:
33039     case PRAGMA_OMP_CRITICAL:
33040     case PRAGMA_OMP_DISTRIBUTE:
33041     case PRAGMA_OMP_FOR:
33042     case PRAGMA_OMP_MASTER:
33043     case PRAGMA_OMP_ORDERED:
33044     case PRAGMA_OMP_PARALLEL:
33045     case PRAGMA_OMP_SECTIONS:
33046     case PRAGMA_OMP_SIMD:
33047     case PRAGMA_OMP_SINGLE:
33048     case PRAGMA_OMP_TASK:
33049     case PRAGMA_OMP_TASKGROUP:
33050     case PRAGMA_OMP_TEAMS:
33051       if (context != pragma_stmt && context != pragma_compound)
33052         goto bad_stmt;
33053       cp_parser_omp_construct (parser, pragma_tok);
33054       return true;
33055
33056     case PRAGMA_OMP_TARGET:
33057       return cp_parser_omp_target (parser, pragma_tok, context);
33058
33059     case PRAGMA_OMP_END_DECLARE_TARGET:
33060       cp_parser_omp_end_declare_target (parser, pragma_tok);
33061       return false;
33062
33063     case PRAGMA_OMP_SECTION:
33064       error_at (pragma_tok->location, 
33065                 "%<#pragma omp section%> may only be used in "
33066                 "%<#pragma omp sections%> construct");
33067       break;
33068
33069     case PRAGMA_IVDEP:
33070       {
33071         cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33072         cp_token *tok;
33073         tok = cp_lexer_peek_token (the_parser->lexer);
33074         if (tok->type != CPP_KEYWORD
33075             || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33076                 && tok->keyword != RID_DO))
33077           {
33078             cp_parser_error (parser, "for, while or do statement expected");
33079             return false;
33080           }
33081         cp_parser_iteration_statement (parser, true);
33082         return true;
33083       }
33084
33085     case PRAGMA_CILK_SIMD:
33086       if (context == pragma_external)
33087         {
33088           error_at (pragma_tok->location,
33089                     "%<#pragma simd%> must be inside a function");
33090           break;
33091         }
33092       cp_parser_cilk_simd (parser, pragma_tok);
33093       return true;
33094
33095     case PRAGMA_CILK_GRAINSIZE:
33096       if (context == pragma_external)
33097         {
33098           error_at (pragma_tok->location,
33099                     "%<#pragma cilk grainsize%> must be inside a function");
33100           break;
33101         }
33102
33103       /* Ignore the pragma if Cilk Plus is not enabled.  */
33104       if (flag_cilkplus)
33105         {
33106           cp_parser_cilk_grainsize (parser, pragma_tok);
33107           return true;
33108         }
33109       else
33110         {
33111           error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33112                     "%<#pragma cilk grainsize%>");
33113           break;
33114         }
33115
33116     default:
33117       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33118       c_invoke_pragma_handler (id);
33119       break;
33120
33121     bad_stmt:
33122       cp_parser_error (parser, "expected declaration specifiers");
33123       break;
33124     }
33125
33126   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33127   return false;
33128 }
33129
33130 /* The interface the pragma parsers have to the lexer.  */
33131
33132 enum cpp_ttype
33133 pragma_lex (tree *value)
33134 {
33135   cp_token *tok;
33136   enum cpp_ttype ret;
33137
33138   tok = cp_lexer_peek_token (the_parser->lexer);
33139
33140   ret = tok->type;
33141   *value = tok->u.value;
33142
33143   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33144     ret = CPP_EOF;
33145   else if (ret == CPP_STRING)
33146     *value = cp_parser_string_literal (the_parser, false, false);
33147   else
33148     {
33149       cp_lexer_consume_token (the_parser->lexer);
33150       if (ret == CPP_KEYWORD)
33151         ret = CPP_NAME;
33152     }
33153
33154   return ret;
33155 }
33156
33157 \f
33158 /* External interface.  */
33159
33160 /* Parse one entire translation unit.  */
33161
33162 void
33163 c_parse_file (void)
33164 {
33165   static bool already_called = false;
33166
33167   if (already_called)
33168     fatal_error (input_location,
33169                  "inter-module optimizations not implemented for C++");
33170   already_called = true;
33171
33172   the_parser = cp_parser_new ();
33173   push_deferring_access_checks (flag_access_control
33174                                 ? dk_no_deferred : dk_no_check);
33175   cp_parser_translation_unit (the_parser);
33176   the_parser = NULL;
33177 }
33178
33179 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's 
33180    vectorlength clause:
33181    Syntax:
33182    vectorlength ( constant-expression )  */
33183
33184 static tree
33185 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33186                                   bool is_simd_fn)
33187 {
33188   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33189   tree expr;
33190   /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33191      safelen clause.  Thus, vectorlength is represented as OMP 4.0
33192      safelen.  For SIMD-enabled function it is represented by OMP 4.0
33193      simdlen.  */
33194   if (!is_simd_fn)
33195     check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", 
33196                                loc);
33197   else
33198     check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33199                                loc);
33200
33201   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33202     return error_mark_node;
33203
33204   expr = cp_parser_constant_expression (parser);
33205   expr = maybe_constant_value (expr);
33206
33207   /* If expr == error_mark_node, then don't emit any errors nor
33208      create a clause.  if any of the above functions returns
33209      error mark node then they would have emitted an error message.  */
33210   if (expr == error_mark_node)
33211     ;
33212   else if (!TREE_TYPE (expr)
33213            || !TREE_CONSTANT (expr)
33214            || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33215     error_at (loc, "vectorlength must be an integer constant");
33216   else if (TREE_CONSTANT (expr)
33217            && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33218     error_at (loc, "vectorlength must be a power of 2");
33219   else 
33220     {
33221       tree c;
33222       if (!is_simd_fn)
33223         { 
33224           c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN); 
33225           OMP_CLAUSE_SAFELEN_EXPR (c) = expr; 
33226           OMP_CLAUSE_CHAIN (c) = clauses; 
33227           clauses = c;
33228         }
33229       else
33230         {
33231           c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33232           OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33233           OMP_CLAUSE_CHAIN (c) = clauses;
33234           clauses = c;
33235         }
33236     }
33237
33238   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33239     return error_mark_node;
33240   return clauses;
33241 }
33242
33243 /* Handles the Cilk Plus #pragma simd linear clause.
33244    Syntax:
33245    linear ( simd-linear-variable-list )
33246
33247    simd-linear-variable-list:
33248      simd-linear-variable
33249      simd-linear-variable-list , simd-linear-variable
33250
33251    simd-linear-variable:
33252      id-expression
33253      id-expression : simd-linear-step
33254
33255    simd-linear-step:
33256    conditional-expression */
33257
33258 static tree
33259 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33260 {
33261   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33262
33263   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33264     return clauses;
33265   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33266     {
33267       cp_parser_error (parser, "expected identifier");
33268       cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33269       return error_mark_node;
33270     }
33271
33272   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33273   parser->colon_corrects_to_scope_p = false;
33274   while (1)
33275     {
33276       cp_token *token = cp_lexer_peek_token (parser->lexer);
33277       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33278         {
33279           cp_parser_error (parser, "expected variable-name");
33280           clauses = error_mark_node;
33281           break;
33282         }
33283
33284       tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33285                                                false, false);
33286       tree decl = cp_parser_lookup_name_simple (parser, var_name,
33287                                                 token->location);
33288       if (decl == error_mark_node)
33289         {
33290           cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33291                                        token->location);
33292           clauses = error_mark_node;
33293         }
33294       else
33295         {
33296           tree e = NULL_TREE;
33297           tree step_size = integer_one_node;
33298
33299           /* If present, parse the linear step.  Otherwise, assume the default
33300              value of 1.  */
33301           if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33302             {
33303               cp_lexer_consume_token (parser->lexer);
33304
33305               e = cp_parser_assignment_expression (parser);
33306               e = maybe_constant_value (e);
33307
33308               if (e == error_mark_node)
33309                 {
33310                   /* If an error has occurred,  then the whole pragma is
33311                      considered ill-formed.  Thus, no reason to keep
33312                      parsing.  */
33313                   clauses = error_mark_node;
33314                   break;
33315                 }
33316               else if (type_dependent_expression_p (e)
33317                        || value_dependent_expression_p (e)
33318                        || (TREE_TYPE (e)
33319                            && INTEGRAL_TYPE_P (TREE_TYPE (e))
33320                            && (TREE_CONSTANT (e)
33321                                || DECL_P (e))))
33322                 step_size = e;
33323               else
33324                 cp_parser_error (parser,
33325                                  "step size must be an integer constant "
33326                                  "expression or an integer variable");
33327             }
33328
33329           /* Use the OMP_CLAUSE_LINEAR,  which has the same semantics.  */
33330           tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33331           OMP_CLAUSE_DECL (l) = decl;
33332           OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33333           OMP_CLAUSE_CHAIN (l) = clauses;
33334           clauses = l;
33335         }
33336       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33337         cp_lexer_consume_token (parser->lexer);
33338       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33339         break;
33340       else
33341         {
33342           error_at (cp_lexer_peek_token (parser->lexer)->location,
33343                     "expected %<,%> or %<)%> after %qE", decl);
33344           clauses = error_mark_node;
33345           break;
33346         }
33347     }
33348   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33349   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33350   return clauses;
33351 }
33352
33353 /* Returns the name of the next clause.  If the clause is not
33354    recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33355    token is not consumed.  Otherwise, the appropriate enum from the
33356    pragma_simd_clause is returned and the token is consumed.  */
33357
33358 static pragma_omp_clause
33359 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33360 {
33361   pragma_omp_clause clause_type;
33362   cp_token *token = cp_lexer_peek_token (parser->lexer);
33363
33364   if (token->keyword == RID_PRIVATE)
33365     clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33366   else if (!token->u.value || token->type != CPP_NAME)
33367     return PRAGMA_CILK_CLAUSE_NONE;
33368   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33369     clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33370   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33371     clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33372   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33373     clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33374   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33375     clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33376   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33377     clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33378   else
33379     return PRAGMA_CILK_CLAUSE_NONE;
33380
33381   cp_lexer_consume_token (parser->lexer);
33382   return clause_type;
33383 }
33384
33385 /* Parses all the #pragma simd clauses.  Returns a list of clauses found.  */
33386
33387 static tree
33388 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33389 {
33390   tree clauses = NULL_TREE;
33391
33392   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33393          && clauses != error_mark_node)
33394     {
33395       pragma_omp_clause c_kind;
33396       c_kind = cp_parser_cilk_simd_clause_name (parser);
33397       if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33398         clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33399       else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33400         clauses = cp_parser_cilk_simd_linear (parser, clauses);
33401       else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33402         /* Use the OpenMP 4.0 equivalent function.  */
33403         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33404       else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33405         /* Use the OpenMP 4.0 equivalent function.  */
33406         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33407                                           clauses);
33408       else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33409         /* Use the OMP 4.0 equivalent function.  */
33410         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33411                                           clauses);
33412       else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33413         /* Use the OMP 4.0 equivalent function.  */
33414         clauses = cp_parser_omp_clause_reduction (parser, clauses);
33415       else
33416         {
33417           clauses = error_mark_node;
33418           cp_parser_error (parser, "expected %<#pragma simd%> clause");
33419           break;
33420         }
33421     }
33422
33423   cp_parser_skip_to_pragma_eol (parser, pragma_token);
33424
33425   if (clauses == error_mark_node)
33426     return error_mark_node;
33427   else
33428     return c_finish_cilk_clauses (clauses);
33429 }
33430
33431 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops.  */
33432
33433 static void
33434 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33435 {
33436   tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33437
33438   if (clauses == error_mark_node)
33439     return;
33440
33441   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33442     {
33443       error_at (cp_lexer_peek_token (parser->lexer)->location,
33444                 "for statement expected");
33445       return;
33446     }
33447
33448   tree sb = begin_omp_structured_block ();
33449   int save = cp_parser_begin_omp_structured_block (parser);
33450   tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33451   if (ret)
33452     cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33453   cp_parser_end_omp_structured_block (parser, save);
33454   add_stmt (finish_omp_structured_block (sb));
33455 }
33456
33457 /* Main entry-point for parsing Cilk Plus _Cilk_for
33458    loops.  The return value is error_mark_node
33459    when errors happen and CILK_FOR tree on success.  */
33460
33461 static tree
33462 cp_parser_cilk_for (cp_parser *parser, tree grain)
33463 {
33464   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33465     gcc_unreachable ();
33466
33467   tree sb = begin_omp_structured_block ();
33468   int save = cp_parser_begin_omp_structured_block (parser);
33469
33470   tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33471   OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33472   OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33473   clauses = finish_omp_clauses (clauses);
33474
33475   tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33476   if (ret)
33477     cpp_validate_cilk_plus_loop (ret);
33478   else
33479     ret = error_mark_node;
33480
33481   cp_parser_end_omp_structured_block (parser, save);
33482   add_stmt (finish_omp_structured_block (sb));
33483   return ret;
33484 }
33485
33486 /* Create an identifier for a generic parameter type (a synthesized
33487    template parameter implied by `auto' or a concept identifier). */
33488
33489 static GTY(()) int generic_parm_count;
33490 static tree
33491 make_generic_type_name ()
33492 {
33493   char buf[32];
33494   sprintf (buf, "auto:%d", ++generic_parm_count);
33495   return get_identifier (buf);
33496 }
33497
33498 /* Predicate that behaves as is_auto_or_concept but matches the parent
33499    node of the generic type rather than the generic type itself.  This
33500    allows for type transformation in add_implicit_template_parms.  */
33501
33502 static inline bool
33503 tree_type_is_auto_or_concept (const_tree t)
33504 {
33505   return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33506 }
33507
33508 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33509    (creating a new template parameter list if necessary).  Returns the newly
33510    created template type parm.  */
33511
33512 tree
33513 synthesize_implicit_template_parm  (cp_parser *parser)
33514 {
33515   gcc_assert (current_binding_level->kind == sk_function_parms);
33516
33517   /* We are either continuing a function template that already contains implicit
33518      template parameters, creating a new fully-implicit function template, or
33519      extending an existing explicit function template with implicit template
33520      parameters.  */
33521
33522   cp_binding_level *const entry_scope = current_binding_level;
33523
33524   bool become_template = false;
33525   cp_binding_level *parent_scope = 0;
33526
33527   if (parser->implicit_template_scope)
33528     {
33529       gcc_assert (parser->implicit_template_parms);
33530
33531       current_binding_level = parser->implicit_template_scope;
33532     }
33533   else
33534     {
33535       /* Roll back to the existing template parameter scope (in the case of
33536          extending an explicit function template) or introduce a new template
33537          parameter scope ahead of the function parameter scope (or class scope
33538          in the case of out-of-line member definitions).  The function scope is
33539          added back after template parameter synthesis below.  */
33540
33541       cp_binding_level *scope = entry_scope;
33542
33543       while (scope->kind == sk_function_parms)
33544         {
33545           parent_scope = scope;
33546           scope = scope->level_chain;
33547         }
33548       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33549         {
33550           /* If not defining a class, then any class scope is a scope level in
33551              an out-of-line member definition.  In this case simply wind back
33552              beyond the first such scope to inject the template parameter list.
33553              Otherwise wind back to the class being defined.  The latter can
33554              occur in class member friend declarations such as:
33555
33556                class A {
33557                  void foo (auto);
33558                };
33559                class B {
33560                  friend void A::foo (auto);
33561                };
33562
33563             The template parameter list synthesized for the friend declaration
33564             must be injected in the scope of 'B'.  This can also occur in
33565             erroneous cases such as:
33566
33567                struct A {
33568                  struct B {
33569                    void foo (auto);
33570                  };
33571                  void B::foo (auto) {}
33572                };
33573
33574             Here the attempted definition of 'B::foo' within 'A' is ill-formed
33575             but, nevertheless, the template parameter list synthesized for the
33576             declarator should be injected into the scope of 'A' as if the
33577             ill-formed template was specified explicitly.  */
33578
33579           while (scope->kind == sk_class && !scope->defining_class_p)
33580             {
33581               parent_scope = scope;
33582               scope = scope->level_chain;
33583             }
33584         }
33585
33586       current_binding_level = scope;
33587
33588       if (scope->kind != sk_template_parms
33589           || !function_being_declared_is_template_p (parser))
33590         {
33591           /* Introduce a new template parameter list for implicit template
33592              parameters.  */
33593
33594           become_template = true;
33595
33596           parser->implicit_template_scope
33597               = begin_scope (sk_template_parms, NULL);
33598
33599           ++processing_template_decl;
33600
33601           parser->fully_implicit_function_template_p = true;
33602           ++parser->num_template_parameter_lists;
33603         }
33604       else
33605         {
33606           /* Synthesize implicit template parameters at the end of the explicit
33607              template parameter list.  */
33608
33609           gcc_assert (current_template_parms);
33610
33611           parser->implicit_template_scope = scope;
33612
33613           tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33614           parser->implicit_template_parms
33615             = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33616         }
33617     }
33618
33619   /* Synthesize a new template parameter and track the current template
33620      parameter chain with implicit_template_parms.  */
33621
33622   tree synth_id = make_generic_type_name ();
33623   tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33624                                                     synth_id);
33625   tree new_parm
33626     = process_template_parm (parser->implicit_template_parms,
33627                              input_location,
33628                              build_tree_list (NULL_TREE, synth_tmpl_parm),
33629                              /*non_type=*/false,
33630                              /*param_pack=*/false);
33631
33632
33633   if (parser->implicit_template_parms)
33634     parser->implicit_template_parms
33635       = TREE_CHAIN (parser->implicit_template_parms);
33636   else
33637     parser->implicit_template_parms = new_parm;
33638
33639   tree new_type = TREE_TYPE (getdecls ());
33640
33641   /* If creating a fully implicit function template, start the new implicit
33642      template parameter list with this synthesized type, otherwise grow the
33643      current template parameter list.  */
33644
33645   if (become_template)
33646     {
33647       parent_scope->level_chain = current_binding_level;
33648
33649       tree new_parms = make_tree_vec (1);
33650       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33651       current_template_parms = tree_cons (size_int (processing_template_decl),
33652                                           new_parms, current_template_parms);
33653     }
33654   else
33655     {
33656       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33657       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33658       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33659       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33660     }
33661
33662   current_binding_level = entry_scope;
33663
33664   return new_type;
33665 }
33666
33667 /* Finish the declaration of a fully implicit function template.  Such a
33668    template has no explicit template parameter list so has not been through the
33669    normal template head and tail processing.  synthesize_implicit_template_parm
33670    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
33671    provided if the declaration is a class member such that its template
33672    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
33673    form is returned.  Otherwise NULL_TREE is returned. */
33674
33675 tree
33676 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33677 {
33678   gcc_assert (parser->fully_implicit_function_template_p);
33679
33680   if (member_decl_opt && member_decl_opt != error_mark_node
33681       && DECL_VIRTUAL_P (member_decl_opt))
33682     {
33683       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33684                 "implicit templates may not be %<virtual%>");
33685       DECL_VIRTUAL_P (member_decl_opt) = false;
33686     }
33687
33688   if (member_decl_opt)
33689     member_decl_opt = finish_member_template_decl (member_decl_opt);
33690   end_template_decl ();
33691
33692   parser->fully_implicit_function_template_p = false;
33693   --parser->num_template_parameter_lists;
33694
33695   return member_decl_opt;
33696 }
33697
33698 #include "gt-cp-parser.h"