390f7d0d16f8b011194f79d1e42b2bb53bde7485
[platform/upstream/linaro-gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2    Copyright (C) 2000-2016 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 "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "omp-low.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
41 #include "context.h"
42
43 \f
44 /* The lexer.  */
45
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47    and c-lex.c) and the C++ parser.  */
48
49 static cp_token eof_token =
50 {
51   CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
52 };
53
54 /* The various kinds of non integral constant we encounter. */
55 enum non_integral_constant {
56   NIC_NONE,
57   /* floating-point literal */
58   NIC_FLOAT,
59   /* %<this%> */
60   NIC_THIS,
61   /* %<__FUNCTION__%> */
62   NIC_FUNC_NAME,
63   /* %<__PRETTY_FUNCTION__%> */
64   NIC_PRETTY_FUNC,
65   /* %<__func__%> */
66   NIC_C99_FUNC,
67   /* "%<va_arg%> */
68   NIC_VA_ARG,
69   /* a cast */
70   NIC_CAST,
71   /* %<typeid%> operator */
72   NIC_TYPEID,
73   /* non-constant compound literals */
74   NIC_NCC,
75   /* a function call */
76   NIC_FUNC_CALL,
77   /* an increment */
78   NIC_INC,
79   /* an decrement */
80   NIC_DEC,
81   /* an array reference */
82   NIC_ARRAY_REF,
83   /* %<->%> */
84   NIC_ARROW,
85   /* %<.%> */
86   NIC_POINT,
87   /* the address of a label */
88   NIC_ADDR_LABEL,
89   /* %<*%> */
90   NIC_STAR,
91   /* %<&%> */
92   NIC_ADDR,
93   /* %<++%> */
94   NIC_PREINCREMENT,
95   /* %<--%> */
96   NIC_PREDECREMENT,
97   /* %<new%> */
98   NIC_NEW,
99   /* %<delete%> */
100   NIC_DEL,
101   /* calls to overloaded operators */
102   NIC_OVERLOADED,
103   /* an assignment */
104   NIC_ASSIGNMENT,
105   /* a comma operator */
106   NIC_COMMA,
107   /* a call to a constructor */
108   NIC_CONSTRUCTOR,
109   /* a transaction expression */
110   NIC_TRANSACTION
111 };
112
113 /* The various kinds of errors about name-lookup failing. */
114 enum name_lookup_error {
115   /* NULL */
116   NLE_NULL,
117   /* is not a type */
118   NLE_TYPE,
119   /* is not a class or namespace */
120   NLE_CXX98,
121   /* is not a class, namespace, or enumeration */
122   NLE_NOT_CXX98
123 };
124
125 /* The various kinds of required token */
126 enum required_token {
127   RT_NONE,
128   RT_SEMICOLON,  /* ';' */
129   RT_OPEN_PAREN, /* '(' */
130   RT_CLOSE_BRACE, /* '}' */
131   RT_OPEN_BRACE,  /* '{' */
132   RT_CLOSE_SQUARE, /* ']' */
133   RT_OPEN_SQUARE,  /* '[' */
134   RT_COMMA, /* ',' */
135   RT_SCOPE, /* '::' */
136   RT_LESS, /* '<' */
137   RT_GREATER, /* '>' */
138   RT_EQ, /* '=' */
139   RT_ELLIPSIS, /* '...' */
140   RT_MULT, /* '*' */
141   RT_COMPL, /* '~' */
142   RT_COLON, /* ':' */
143   RT_COLON_SCOPE, /* ':' or '::' */
144   RT_CLOSE_PAREN, /* ')' */
145   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146   RT_PRAGMA_EOL, /* end of line */
147   RT_NAME, /* identifier */
148
149   /* The type is CPP_KEYWORD */
150   RT_NEW, /* new */
151   RT_DELETE, /* delete */
152   RT_RETURN, /* return */
153   RT_WHILE, /* while */
154   RT_EXTERN, /* extern */
155   RT_STATIC_ASSERT, /* static_assert */
156   RT_DECLTYPE, /* decltype */
157   RT_OPERATOR, /* operator */
158   RT_CLASS, /* class */
159   RT_TEMPLATE, /* template */
160   RT_NAMESPACE, /* namespace */
161   RT_USING, /* using */
162   RT_ASM, /* asm */
163   RT_TRY, /* try */
164   RT_CATCH, /* catch */
165   RT_THROW, /* throw */
166   RT_LABEL, /* __label__ */
167   RT_AT_TRY, /* @try */
168   RT_AT_SYNCHRONIZED, /* @synchronized */
169   RT_AT_THROW, /* @throw */
170
171   RT_SELECT,  /* selection-statement */
172   RT_INTERATION, /* iteration-statement */
173   RT_JUMP, /* jump-statement */
174   RT_CLASS_KEY, /* class-key */
175   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178   RT_TRANSACTION_CANCEL /* __transaction_cancel */
179 };
180
181 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
182    reverting it on destruction.  */
183
184 class type_id_in_expr_sentinel
185 {
186   cp_parser *parser;
187   bool saved;
188 public:
189   type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
190     : parser (parser),
191       saved (parser->in_type_id_in_expr_p)
192   { parser->in_type_id_in_expr_p = set; }
193   ~type_id_in_expr_sentinel ()
194   { parser->in_type_id_in_expr_p = saved; }
195 };
196
197 /* Prototypes.  */
198
199 static cp_lexer *cp_lexer_new_main
200   (void);
201 static cp_lexer *cp_lexer_new_from_tokens
202   (cp_token_cache *tokens);
203 static void cp_lexer_destroy
204   (cp_lexer *);
205 static int cp_lexer_saving_tokens
206   (const cp_lexer *);
207 static cp_token *cp_lexer_token_at
208   (cp_lexer *, cp_token_position);
209 static void cp_lexer_get_preprocessor_token
210   (cp_lexer *, cp_token *);
211 static inline cp_token *cp_lexer_peek_token
212   (cp_lexer *);
213 static cp_token *cp_lexer_peek_nth_token
214   (cp_lexer *, size_t);
215 static inline bool cp_lexer_next_token_is
216   (cp_lexer *, enum cpp_ttype);
217 static bool cp_lexer_next_token_is_not
218   (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_keyword
220   (cp_lexer *, enum rid);
221 static cp_token *cp_lexer_consume_token
222   (cp_lexer *);
223 static void cp_lexer_purge_token
224   (cp_lexer *);
225 static void cp_lexer_purge_tokens_after
226   (cp_lexer *, cp_token_position);
227 static void cp_lexer_save_tokens
228   (cp_lexer *);
229 static void cp_lexer_commit_tokens
230   (cp_lexer *);
231 static void cp_lexer_rollback_tokens
232   (cp_lexer *);
233 static void cp_lexer_print_token
234   (FILE *, cp_token *);
235 static inline bool cp_lexer_debugging_p
236   (cp_lexer *);
237 static void cp_lexer_start_debugging
238   (cp_lexer *) ATTRIBUTE_UNUSED;
239 static void cp_lexer_stop_debugging
240   (cp_lexer *) ATTRIBUTE_UNUSED;
241
242 static cp_token_cache *cp_token_cache_new
243   (cp_token *, cp_token *);
244
245 static void cp_parser_initial_pragma
246   (cp_token *);
247
248 static tree cp_literal_operator_id
249   (const char *);
250
251 static void cp_parser_cilk_simd
252   (cp_parser *, cp_token *, bool *);
253 static tree cp_parser_cilk_for
254   (cp_parser *, tree, bool *);
255 static bool cp_parser_omp_declare_reduction_exprs
256   (tree, cp_parser *);
257 static tree cp_parser_cilk_simd_vectorlength 
258   (cp_parser *, tree, bool);
259 static void cp_finalize_oacc_routine
260   (cp_parser *, tree, bool);
261
262 /* Manifest constants.  */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
265
266 /* Variables.  */
267
268 /* The stream to which debugging output should be written.  */
269 static FILE *cp_lexer_debug_stream;
270
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272    sizeof, typeof, or alignof.  */
273 int cp_unevaluated_operand;
274
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
277    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
278    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279    highlighted by surrounding it in [[ ]].  */
280
281 static void
282 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283                       cp_token *start_token, unsigned num,
284                       cp_token *curr_token)
285 {
286   unsigned i, nprinted;
287   cp_token *token;
288   bool do_print;
289
290   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
291
292   if (buffer == NULL)
293     return;
294
295   if (num == 0)
296     num = buffer->length ();
297
298   if (start_token == NULL)
299     start_token = buffer->address ();
300
301   if (start_token > buffer->address ())
302     {
303       cp_lexer_print_token (file, &(*buffer)[0]);
304       fprintf (file, " ... ");
305     }
306
307   do_print = false;
308   nprinted = 0;
309   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
310     {
311       if (token == start_token)
312         do_print = true;
313
314       if (!do_print)
315         continue;
316
317       nprinted++;
318       if (token == curr_token)
319         fprintf (file, "[[");
320
321       cp_lexer_print_token (file, token);
322
323       if (token == curr_token)
324         fprintf (file, "]]");
325
326       switch (token->type)
327         {
328           case CPP_SEMICOLON:
329           case CPP_OPEN_BRACE:
330           case CPP_CLOSE_BRACE:
331           case CPP_EOF:
332             fputc ('\n', file);
333             break;
334
335           default:
336             fputc (' ', file);
337         }
338     }
339
340   if (i == num && i < buffer->length ())
341     {
342       fprintf (file, " ... ");
343       cp_lexer_print_token (file, &buffer->last ());
344     }
345
346   fprintf (file, "\n");
347 }
348
349
350 /* Dump all tokens in BUFFER to stderr.  */
351
352 void
353 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
354 {
355   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356 }
357
358 DEBUG_FUNCTION void
359 debug (vec<cp_token, va_gc> &ref)
360 {
361   cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362 }
363
364 DEBUG_FUNCTION void
365 debug (vec<cp_token, va_gc> *ptr)
366 {
367   if (ptr)
368     debug (*ptr);
369   else
370     fprintf (stderr, "<nil>\n");
371 }
372
373
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
375    description for T.  */
376
377 static void
378 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
379 {
380   if (t)
381     {
382       fprintf (file, "%s: ", desc);
383       print_node_brief (file, "", t, 0);
384     }
385 }
386
387
388 /* Dump parser context C to FILE.  */
389
390 static void
391 cp_debug_print_context (FILE *file, cp_parser_context *c)
392 {
393   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395   print_node_brief (file, "", c->object_type, 0);
396   fprintf (file, "}\n");
397 }
398
399
400 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
401
402 static void
403 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
404 {
405   unsigned i;
406   cp_parser_context *c;
407
408   fprintf (file, "Parsing context stack:\n");
409   for (i = 0, c = first; c; c = c->next, i++)
410     {
411       fprintf (file, "\t#%u: ", i);
412       cp_debug_print_context (file, c);
413     }
414 }
415
416
417 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
418
419 static void
420 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
421 {
422   if (flag)
423     fprintf (file, "%s: true\n", desc);
424 }
425
426
427 /* Print an unparsed function entry UF to FILE.  */
428
429 static void
430 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
431 {
432   unsigned i;
433   cp_default_arg_entry *default_arg_fn;
434   tree fn;
435
436   fprintf (file, "\tFunctions with default args:\n");
437   for (i = 0;
438        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
439        i++)
440     {
441       fprintf (file, "\t\tClass type: ");
442       print_node_brief (file, "", default_arg_fn->class_type, 0);
443       fprintf (file, "\t\tDeclaration: ");
444       print_node_brief (file, "", default_arg_fn->decl, 0);
445       fprintf (file, "\n");
446     }
447
448   fprintf (file, "\n\tFunctions with definitions that require "
449            "post-processing\n\t\t");
450   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
451     {
452       print_node_brief (file, "", fn, 0);
453       fprintf (file, " ");
454     }
455   fprintf (file, "\n");
456
457   fprintf (file, "\n\tNon-static data members with initializers that require "
458            "post-processing\n\t\t");
459   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
460     {
461       print_node_brief (file, "", fn, 0);
462       fprintf (file, " ");
463     }
464   fprintf (file, "\n");
465 }
466
467
468 /* Print the stack of unparsed member functions S to FILE.  */
469
470 static void
471 cp_debug_print_unparsed_queues (FILE *file,
472                                 vec<cp_unparsed_functions_entry, va_gc> *s)
473 {
474   unsigned i;
475   cp_unparsed_functions_entry *uf;
476
477   fprintf (file, "Unparsed functions\n");
478   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
479     {
480       fprintf (file, "#%u:\n", i);
481       cp_debug_print_unparsed_function (file, uf);
482     }
483 }
484
485
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
488
489 static void
490 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
491 {
492   cp_token *next_token, *first_token, *start_token;
493
494   if (file == NULL)
495     file = stderr;
496
497   next_token = parser->lexer->next_token;
498   first_token = parser->lexer->buffer->address ();
499   start_token = (next_token > first_token + window_size / 2)
500                 ? next_token - window_size / 2
501                 : first_token;
502   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503                         next_token);
504 }
505
506
507 /* Dump debugging information for the given PARSER.  If FILE is NULL,
508    the output is printed on stderr.  */
509
510 void
511 cp_debug_parser (FILE *file, cp_parser *parser)
512 {
513   const size_t window_size = 20;
514   cp_token *token;
515   expanded_location eloc;
516
517   if (file == NULL)
518     file = stderr;
519
520   fprintf (file, "Parser state\n\n");
521   fprintf (file, "Number of tokens: %u\n",
522            vec_safe_length (parser->lexer->buffer));
523   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524   cp_debug_print_tree_if_set (file, "Object scope",
525                                      parser->object_scope);
526   cp_debug_print_tree_if_set (file, "Qualifying scope",
527                                      parser->qualifying_scope);
528   cp_debug_print_context_stack (file, parser->context);
529   cp_debug_print_flag (file, "Allow GNU extensions",
530                               parser->allow_gnu_extensions_p);
531   cp_debug_print_flag (file, "'>' token is greater-than",
532                               parser->greater_than_is_operator_p);
533   cp_debug_print_flag (file, "Default args allowed in current "
534                               "parameter list", parser->default_arg_ok_p);
535   cp_debug_print_flag (file, "Parsing integral constant-expression",
536                               parser->integral_constant_expression_p);
537   cp_debug_print_flag (file, "Allow non-constant expression in current "
538                               "constant-expression",
539                               parser->allow_non_integral_constant_expression_p);
540   cp_debug_print_flag (file, "Seen non-constant expression",
541                               parser->non_integral_constant_expression_p);
542   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
543                               "current context",
544                               parser->local_variables_forbidden_p);
545   cp_debug_print_flag (file, "In unbraced linkage specification",
546                               parser->in_unbraced_linkage_specification_p);
547   cp_debug_print_flag (file, "Parsing a declarator",
548                               parser->in_declarator_p);
549   cp_debug_print_flag (file, "In template argument list",
550                               parser->in_template_argument_list_p);
551   cp_debug_print_flag (file, "Parsing an iteration statement",
552                               parser->in_statement & IN_ITERATION_STMT);
553   cp_debug_print_flag (file, "Parsing a switch statement",
554                               parser->in_statement & IN_SWITCH_STMT);
555   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556                               parser->in_statement & IN_OMP_BLOCK);
557   cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
558                               parser->in_statement & IN_CILK_SIMD_FOR);
559   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
560                               parser->in_statement & IN_OMP_FOR);
561   cp_debug_print_flag (file, "Parsing an if statement",
562                               parser->in_statement & IN_IF_STMT);
563   cp_debug_print_flag (file, "Parsing a type-id in an expression "
564                               "context", parser->in_type_id_in_expr_p);
565   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
566                               parser->implicit_extern_c);
567   cp_debug_print_flag (file, "String expressions should be translated "
568                               "to execution character set",
569                               parser->translate_strings_p);
570   cp_debug_print_flag (file, "Parsing function body outside of a "
571                               "local class", parser->in_function_body);
572   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
573                               parser->colon_corrects_to_scope_p);
574   cp_debug_print_flag (file, "Colon doesn't start a class definition",
575                               parser->colon_doesnt_start_class_def_p);
576   if (parser->type_definition_forbidden_message)
577     fprintf (file, "Error message for forbidden type definitions: %s\n",
578              parser->type_definition_forbidden_message);
579   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
580   fprintf (file, "Number of class definitions in progress: %u\n",
581            parser->num_classes_being_defined);
582   fprintf (file, "Number of template parameter lists for the current "
583            "declaration: %u\n", parser->num_template_parameter_lists);
584   cp_debug_parser_tokens (file, parser, window_size);
585   token = parser->lexer->next_token;
586   fprintf (file, "Next token to parse:\n");
587   fprintf (file, "\tToken:  ");
588   cp_lexer_print_token (file, token);
589   eloc = expand_location (token->location);
590   fprintf (file, "\n\tFile:   %s\n", eloc.file);
591   fprintf (file, "\tLine:   %d\n", eloc.line);
592   fprintf (file, "\tColumn: %d\n", eloc.column);
593 }
594
595 DEBUG_FUNCTION void
596 debug (cp_parser &ref)
597 {
598   cp_debug_parser (stderr, &ref);
599 }
600
601 DEBUG_FUNCTION void
602 debug (cp_parser *ptr)
603 {
604   if (ptr)
605     debug (*ptr);
606   else
607     fprintf (stderr, "<nil>\n");
608 }
609
610 /* Allocate memory for a new lexer object and return it.  */
611
612 static cp_lexer *
613 cp_lexer_alloc (void)
614 {
615   cp_lexer *lexer;
616
617   c_common_no_more_pch ();
618
619   /* Allocate the memory.  */
620   lexer = ggc_cleared_alloc<cp_lexer> ();
621
622   /* Initially we are not debugging.  */
623   lexer->debugging_p = false;
624
625   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
626
627   /* Create the buffer.  */
628   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
629
630   return lexer;
631 }
632
633
634 /* Create a new main C++ lexer, the lexer that gets tokens from the
635    preprocessor.  */
636
637 static cp_lexer *
638 cp_lexer_new_main (void)
639 {
640   cp_lexer *lexer;
641   cp_token token;
642
643   /* It's possible that parsing the first pragma will load a PCH file,
644      which is a GC collection point.  So we have to do that before
645      allocating any memory.  */
646   cp_parser_initial_pragma (&token);
647
648   lexer = cp_lexer_alloc ();
649
650   /* Put the first token in the buffer.  */
651   lexer->buffer->quick_push (token);
652
653   /* Get the remaining tokens from the preprocessor.  */
654   while (token.type != CPP_EOF)
655     {
656       cp_lexer_get_preprocessor_token (lexer, &token);
657       vec_safe_push (lexer->buffer, token);
658     }
659
660   lexer->last_token = lexer->buffer->address ()
661                       + lexer->buffer->length ()
662                       - 1;
663   lexer->next_token = lexer->buffer->length ()
664                       ? lexer->buffer->address ()
665                       : &eof_token;
666
667   /* Subsequent preprocessor diagnostics should use compiler
668      diagnostic functions to get the compiler source location.  */
669   done_lexing = true;
670
671   gcc_assert (!lexer->next_token->purged_p);
672   return lexer;
673 }
674
675 /* Create a new lexer whose token stream is primed with the tokens in
676    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
677
678 static cp_lexer *
679 cp_lexer_new_from_tokens (cp_token_cache *cache)
680 {
681   cp_token *first = cache->first;
682   cp_token *last = cache->last;
683   cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
684
685   /* We do not own the buffer.  */
686   lexer->buffer = NULL;
687   lexer->next_token = first == last ? &eof_token : first;
688   lexer->last_token = last;
689
690   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
691
692   /* Initially we are not debugging.  */
693   lexer->debugging_p = false;
694
695   gcc_assert (!lexer->next_token->purged_p);
696   return lexer;
697 }
698
699 /* Frees all resources associated with LEXER.  */
700
701 static void
702 cp_lexer_destroy (cp_lexer *lexer)
703 {
704   vec_free (lexer->buffer);
705   lexer->saved_tokens.release ();
706   ggc_free (lexer);
707 }
708
709 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
710    be used.  The point of this flag is to help the compiler to fold away calls
711    to cp_lexer_debugging_p within this source file at compile time, when the
712    lexer is not being debugged.  */
713
714 #define LEXER_DEBUGGING_ENABLED_P false
715
716 /* Returns nonzero if debugging information should be output.  */
717
718 static inline bool
719 cp_lexer_debugging_p (cp_lexer *lexer)
720 {
721   if (!LEXER_DEBUGGING_ENABLED_P)
722     return false;
723
724   return lexer->debugging_p;
725 }
726
727
728 static inline cp_token_position
729 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
730 {
731   gcc_assert (!previous_p || lexer->next_token != &eof_token);
732
733   return lexer->next_token - previous_p;
734 }
735
736 static inline cp_token *
737 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
738 {
739   return pos;
740 }
741
742 static inline void
743 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
744 {
745   lexer->next_token = cp_lexer_token_at (lexer, pos);
746 }
747
748 static inline cp_token_position
749 cp_lexer_previous_token_position (cp_lexer *lexer)
750 {
751   if (lexer->next_token == &eof_token)
752     return lexer->last_token - 1;
753   else
754     return cp_lexer_token_position (lexer, true);
755 }
756
757 static inline cp_token *
758 cp_lexer_previous_token (cp_lexer *lexer)
759 {
760   cp_token_position tp = cp_lexer_previous_token_position (lexer);
761
762   /* Skip past purged tokens.  */
763   while (tp->purged_p)
764     {
765       gcc_assert (tp != vec_safe_address (lexer->buffer));
766       tp--;
767     }
768
769   return cp_lexer_token_at (lexer, tp);
770 }
771
772 /* nonzero if we are presently saving tokens.  */
773
774 static inline int
775 cp_lexer_saving_tokens (const cp_lexer* lexer)
776 {
777   return lexer->saved_tokens.length () != 0;
778 }
779
780 /* Store the next token from the preprocessor in *TOKEN.  Return true
781    if we reach EOF.  If LEXER is NULL, assume we are handling an
782    initial #pragma pch_preprocess, and thus want the lexer to return
783    processed strings.  */
784
785 static void
786 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
787 {
788   static int is_extern_c = 0;
789
790    /* Get a new token from the preprocessor.  */
791   token->type
792     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
793                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
794   token->keyword = RID_MAX;
795   token->purged_p = false;
796   token->error_reported = false;
797
798   /* On some systems, some header files are surrounded by an
799      implicit extern "C" block.  Set a flag in the token if it
800      comes from such a header.  */
801   is_extern_c += pending_lang_change;
802   pending_lang_change = 0;
803   token->implicit_extern_c = is_extern_c > 0;
804
805   /* Check to see if this token is a keyword.  */
806   if (token->type == CPP_NAME)
807     {
808       if (C_IS_RESERVED_WORD (token->u.value))
809         {
810           /* Mark this token as a keyword.  */
811           token->type = CPP_KEYWORD;
812           /* Record which keyword.  */
813           token->keyword = C_RID_CODE (token->u.value);
814         }
815       else
816         {
817           if (warn_cxx11_compat
818               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
819               && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
820             {
821               /* Warn about the C++0x keyword (but still treat it as
822                  an identifier).  */
823               warning (OPT_Wc__11_compat, 
824                        "identifier %qE is a keyword in C++11",
825                        token->u.value);
826
827               /* Clear out the C_RID_CODE so we don't warn about this
828                  particular identifier-turned-keyword again.  */
829               C_SET_RID_CODE (token->u.value, RID_MAX);
830             }
831
832           token->keyword = RID_MAX;
833         }
834     }
835   else if (token->type == CPP_AT_NAME)
836     {
837       /* This only happens in Objective-C++; it must be a keyword.  */
838       token->type = CPP_KEYWORD;
839       switch (C_RID_CODE (token->u.value))
840         {
841           /* Replace 'class' with '@class', 'private' with '@private',
842              etc.  This prevents confusion with the C++ keyword
843              'class', and makes the tokens consistent with other
844              Objective-C 'AT' keywords.  For example '@class' is
845              reported as RID_AT_CLASS which is consistent with
846              '@synchronized', which is reported as
847              RID_AT_SYNCHRONIZED.
848           */
849         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
850         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
851         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
852         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
853         case RID_THROW:     token->keyword = RID_AT_THROW; break;
854         case RID_TRY:       token->keyword = RID_AT_TRY; break;
855         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
856         case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
857         default:            token->keyword = C_RID_CODE (token->u.value);
858         }
859     }
860 }
861
862 /* Update the globals input_location and the input file stack from TOKEN.  */
863 static inline void
864 cp_lexer_set_source_position_from_token (cp_token *token)
865 {
866   if (token->type != CPP_EOF)
867     {
868       input_location = token->location;
869     }
870 }
871
872 /* Update the globals input_location and the input file stack from LEXER.  */
873 static inline void
874 cp_lexer_set_source_position (cp_lexer *lexer)
875 {
876   cp_token *token = cp_lexer_peek_token (lexer);
877   cp_lexer_set_source_position_from_token (token);
878 }
879
880 /* Return a pointer to the next token in the token stream, but do not
881    consume it.  */
882
883 static inline cp_token *
884 cp_lexer_peek_token (cp_lexer *lexer)
885 {
886   if (cp_lexer_debugging_p (lexer))
887     {
888       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
889       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
890       putc ('\n', cp_lexer_debug_stream);
891     }
892   return lexer->next_token;
893 }
894
895 /* Return true if the next token has the indicated TYPE.  */
896
897 static inline bool
898 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
899 {
900   return cp_lexer_peek_token (lexer)->type == type;
901 }
902
903 /* Return true if the next token does not have the indicated TYPE.  */
904
905 static inline bool
906 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
907 {
908   return !cp_lexer_next_token_is (lexer, type);
909 }
910
911 /* Return true if the next token is the indicated KEYWORD.  */
912
913 static inline bool
914 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
915 {
916   return cp_lexer_peek_token (lexer)->keyword == keyword;
917 }
918
919 static inline bool
920 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
921 {
922   return cp_lexer_peek_nth_token (lexer, n)->type == type;
923 }
924
925 static inline bool
926 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
927 {
928   return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
929 }
930
931 /* Return true if the next token is not the indicated KEYWORD.  */
932
933 static inline bool
934 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
935 {
936   return cp_lexer_peek_token (lexer)->keyword != keyword;
937 }
938
939 /* Return true if the next token is a keyword for a decl-specifier.  */
940
941 static bool
942 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
943 {
944   cp_token *token;
945
946   token = cp_lexer_peek_token (lexer);
947   switch (token->keyword) 
948     {
949       /* auto specifier: storage-class-specifier in C++,
950          simple-type-specifier in C++0x.  */
951     case RID_AUTO:
952       /* Storage classes.  */
953     case RID_REGISTER:
954     case RID_STATIC:
955     case RID_EXTERN:
956     case RID_MUTABLE:
957     case RID_THREAD:
958       /* Elaborated type specifiers.  */
959     case RID_ENUM:
960     case RID_CLASS:
961     case RID_STRUCT:
962     case RID_UNION:
963     case RID_TYPENAME:
964       /* Simple type specifiers.  */
965     case RID_CHAR:
966     case RID_CHAR16:
967     case RID_CHAR32:
968     case RID_WCHAR:
969     case RID_BOOL:
970     case RID_SHORT:
971     case RID_INT:
972     case RID_LONG:
973     case RID_SIGNED:
974     case RID_UNSIGNED:
975     case RID_FLOAT:
976     case RID_DOUBLE:
977     case RID_VOID:
978       /* GNU extensions.  */ 
979     case RID_ATTRIBUTE:
980     case RID_TYPEOF:
981       /* C++0x extensions.  */
982     case RID_DECLTYPE:
983     case RID_UNDERLYING_TYPE:
984       return true;
985
986     default:
987       if (token->keyword >= RID_FIRST_INT_N
988           && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
989           && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
990         return true;
991       return false;
992     }
993 }
994
995 /* Returns TRUE iff the token T begins a decltype type.  */
996
997 static bool
998 token_is_decltype (cp_token *t)
999 {
1000   return (t->keyword == RID_DECLTYPE
1001           || t->type == CPP_DECLTYPE);
1002 }
1003
1004 /* Returns TRUE iff the next token begins a decltype type.  */
1005
1006 static bool
1007 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1008 {
1009   cp_token *t = cp_lexer_peek_token (lexer);
1010   return token_is_decltype (t);
1011 }
1012
1013 /* Called when processing a token with tree_check_value; perform or defer the
1014    associated checks and return the value.  */
1015
1016 static tree
1017 saved_checks_value (struct tree_check *check_value)
1018 {
1019   /* Perform any access checks that were deferred.  */
1020   vec<deferred_access_check, va_gc> *checks;
1021   deferred_access_check *chk;
1022   checks = check_value->checks;
1023   if (checks)
1024     {
1025       int i;
1026       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1027         perform_or_defer_access_check (chk->binfo,
1028                                        chk->decl,
1029                                        chk->diag_decl, tf_warning_or_error);
1030     }
1031   /* Return the stored value.  */
1032   return check_value->value;
1033 }
1034
1035 /* Return a pointer to the Nth token in the token stream.  If N is 1,
1036    then this is precisely equivalent to cp_lexer_peek_token (except
1037    that it is not inline).  One would like to disallow that case, but
1038    there is one case (cp_parser_nth_token_starts_template_id) where
1039    the caller passes a variable for N and it might be 1.  */
1040
1041 static cp_token *
1042 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1043 {
1044   cp_token *token;
1045
1046   /* N is 1-based, not zero-based.  */
1047   gcc_assert (n > 0);
1048
1049   if (cp_lexer_debugging_p (lexer))
1050     fprintf (cp_lexer_debug_stream,
1051              "cp_lexer: peeking ahead %ld at token: ", (long)n);
1052
1053   --n;
1054   token = lexer->next_token;
1055   gcc_assert (!n || token != &eof_token);
1056   while (n != 0)
1057     {
1058       ++token;
1059       if (token == lexer->last_token)
1060         {
1061           token = &eof_token;
1062           break;
1063         }
1064
1065       if (!token->purged_p)
1066         --n;
1067     }
1068
1069   if (cp_lexer_debugging_p (lexer))
1070     {
1071       cp_lexer_print_token (cp_lexer_debug_stream, token);
1072       putc ('\n', cp_lexer_debug_stream);
1073     }
1074
1075   return token;
1076 }
1077
1078 /* Return the next token, and advance the lexer's next_token pointer
1079    to point to the next non-purged token.  */
1080
1081 static cp_token *
1082 cp_lexer_consume_token (cp_lexer* lexer)
1083 {
1084   cp_token *token = lexer->next_token;
1085
1086   gcc_assert (token != &eof_token);
1087   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1088
1089   do
1090     {
1091       lexer->next_token++;
1092       if (lexer->next_token == lexer->last_token)
1093         {
1094           lexer->next_token = &eof_token;
1095           break;
1096         }
1097
1098     }
1099   while (lexer->next_token->purged_p);
1100
1101   cp_lexer_set_source_position_from_token (token);
1102
1103   /* Provide debugging output.  */
1104   if (cp_lexer_debugging_p (lexer))
1105     {
1106       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1107       cp_lexer_print_token (cp_lexer_debug_stream, token);
1108       putc ('\n', cp_lexer_debug_stream);
1109     }
1110
1111   return token;
1112 }
1113
1114 /* Permanently remove the next token from the token stream, and
1115    advance the next_token pointer to refer to the next non-purged
1116    token.  */
1117
1118 static void
1119 cp_lexer_purge_token (cp_lexer *lexer)
1120 {
1121   cp_token *tok = lexer->next_token;
1122
1123   gcc_assert (tok != &eof_token);
1124   tok->purged_p = true;
1125   tok->location = UNKNOWN_LOCATION;
1126   tok->u.value = NULL_TREE;
1127   tok->keyword = RID_MAX;
1128
1129   do
1130     {
1131       tok++;
1132       if (tok == lexer->last_token)
1133         {
1134           tok = &eof_token;
1135           break;
1136         }
1137     }
1138   while (tok->purged_p);
1139   lexer->next_token = tok;
1140 }
1141
1142 /* Permanently remove all tokens after TOK, up to, but not
1143    including, the token that will be returned next by
1144    cp_lexer_peek_token.  */
1145
1146 static void
1147 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1148 {
1149   cp_token *peek = lexer->next_token;
1150
1151   if (peek == &eof_token)
1152     peek = lexer->last_token;
1153
1154   gcc_assert (tok < peek);
1155
1156   for ( tok += 1; tok != peek; tok += 1)
1157     {
1158       tok->purged_p = true;
1159       tok->location = UNKNOWN_LOCATION;
1160       tok->u.value = NULL_TREE;
1161       tok->keyword = RID_MAX;
1162     }
1163 }
1164
1165 /* Begin saving tokens.  All tokens consumed after this point will be
1166    preserved.  */
1167
1168 static void
1169 cp_lexer_save_tokens (cp_lexer* lexer)
1170 {
1171   /* Provide debugging output.  */
1172   if (cp_lexer_debugging_p (lexer))
1173     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1174
1175   lexer->saved_tokens.safe_push (lexer->next_token);
1176 }
1177
1178 /* Commit to the portion of the token stream most recently saved.  */
1179
1180 static void
1181 cp_lexer_commit_tokens (cp_lexer* lexer)
1182 {
1183   /* Provide debugging output.  */
1184   if (cp_lexer_debugging_p (lexer))
1185     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1186
1187   lexer->saved_tokens.pop ();
1188 }
1189
1190 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1191    to the token stream.  Stop saving tokens.  */
1192
1193 static void
1194 cp_lexer_rollback_tokens (cp_lexer* lexer)
1195 {
1196   /* Provide debugging output.  */
1197   if (cp_lexer_debugging_p (lexer))
1198     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1199
1200   lexer->next_token = lexer->saved_tokens.pop ();
1201 }
1202
1203 /* RAII wrapper around the above functions, with sanity checking.  Creating
1204    a variable saves tokens, which are committed when the variable is
1205    destroyed unless they are explicitly rolled back by calling the rollback
1206    member function.  */
1207
1208 struct saved_token_sentinel
1209 {
1210   cp_lexer *lexer;
1211   unsigned len;
1212   bool commit;
1213   saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1214   {
1215     len = lexer->saved_tokens.length ();
1216     cp_lexer_save_tokens (lexer);
1217   }
1218   void rollback ()
1219   {
1220     cp_lexer_rollback_tokens (lexer);
1221     commit = false;
1222   }
1223   ~saved_token_sentinel()
1224   {
1225     if (commit)
1226       cp_lexer_commit_tokens (lexer);
1227     gcc_assert (lexer->saved_tokens.length () == len);
1228   }
1229 };
1230
1231 /* Print a representation of the TOKEN on the STREAM.  */
1232
1233 static void
1234 cp_lexer_print_token (FILE * stream, cp_token *token)
1235 {
1236   /* We don't use cpp_type2name here because the parser defines
1237      a few tokens of its own.  */
1238   static const char *const token_names[] = {
1239     /* cpplib-defined token types */
1240 #define OP(e, s) #e,
1241 #define TK(e, s) #e,
1242     TTYPE_TABLE
1243 #undef OP
1244 #undef TK
1245     /* C++ parser token types - see "Manifest constants", above.  */
1246     "KEYWORD",
1247     "TEMPLATE_ID",
1248     "NESTED_NAME_SPECIFIER",
1249   };
1250
1251   /* For some tokens, print the associated data.  */
1252   switch (token->type)
1253     {
1254     case CPP_KEYWORD:
1255       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1256          For example, `struct' is mapped to an INTEGER_CST.  */
1257       if (!identifier_p (token->u.value))
1258         break;
1259       /* else fall through */
1260     case CPP_NAME:
1261       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1262       break;
1263
1264     case CPP_STRING:
1265     case CPP_STRING16:
1266     case CPP_STRING32:
1267     case CPP_WSTRING:
1268     case CPP_UTF8STRING:
1269       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1270       break;
1271
1272     case CPP_NUMBER:
1273       print_generic_expr (stream, token->u.value, 0);
1274       break;
1275
1276     default:
1277       /* If we have a name for the token, print it out.  Otherwise, we
1278          simply give the numeric code.  */
1279       if (token->type < ARRAY_SIZE(token_names))
1280         fputs (token_names[token->type], stream);
1281       else
1282         fprintf (stream, "[%d]", token->type);
1283       break;
1284     }
1285 }
1286
1287 DEBUG_FUNCTION void
1288 debug (cp_token &ref)
1289 {
1290   cp_lexer_print_token (stderr, &ref);
1291   fprintf (stderr, "\n");
1292 }
1293
1294 DEBUG_FUNCTION void
1295 debug (cp_token *ptr)
1296 {
1297   if (ptr)
1298     debug (*ptr);
1299   else
1300     fprintf (stderr, "<nil>\n");
1301 }
1302
1303
1304 /* Start emitting debugging information.  */
1305
1306 static void
1307 cp_lexer_start_debugging (cp_lexer* lexer)
1308 {
1309   if (!LEXER_DEBUGGING_ENABLED_P)
1310     fatal_error (input_location,
1311                  "LEXER_DEBUGGING_ENABLED_P is not set to true");
1312
1313   lexer->debugging_p = true;
1314   cp_lexer_debug_stream = stderr;
1315 }
1316
1317 /* Stop emitting debugging information.  */
1318
1319 static void
1320 cp_lexer_stop_debugging (cp_lexer* lexer)
1321 {
1322   if (!LEXER_DEBUGGING_ENABLED_P)
1323     fatal_error (input_location,
1324                  "LEXER_DEBUGGING_ENABLED_P is not set to true");
1325
1326   lexer->debugging_p = false;
1327   cp_lexer_debug_stream = NULL;
1328 }
1329
1330 /* Create a new cp_token_cache, representing a range of tokens.  */
1331
1332 static cp_token_cache *
1333 cp_token_cache_new (cp_token *first, cp_token *last)
1334 {
1335   cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1336   cache->first = first;
1337   cache->last = last;
1338   return cache;
1339 }
1340
1341 /* Diagnose if #pragma omp declare simd isn't followed immediately
1342    by function declaration or definition.  */
1343
1344 static inline void
1345 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1346 {
1347   if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1348     {
1349       error ("%<#pragma omp declare simd%> not immediately followed by "
1350              "function declaration or definition");
1351       parser->omp_declare_simd = NULL;
1352     }
1353 }
1354
1355 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1356    and put that into "omp declare simd" attribute.  */
1357
1358 static inline void
1359 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1360 {
1361   if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1362     {
1363       if (fndecl == error_mark_node)
1364         {
1365           parser->omp_declare_simd = NULL;
1366           return;
1367         }
1368       if (TREE_CODE (fndecl) != FUNCTION_DECL)
1369         {
1370           cp_ensure_no_omp_declare_simd (parser);
1371           return;
1372         }
1373     }
1374 }
1375
1376 /* Diagnose if #pragma acc routine isn't followed immediately by function
1377    declaration or definition.  */
1378
1379 static inline void
1380 cp_ensure_no_oacc_routine (cp_parser *parser)
1381 {
1382   if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1383     {
1384       tree clauses = parser->oacc_routine->clauses;
1385       location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1386
1387       error_at (loc, "%<#pragma acc routine%> not followed by a function "
1388                 "declaration or definition");
1389       parser->oacc_routine = NULL;
1390     }
1391 }
1392 \f
1393 /* Decl-specifiers.  */
1394
1395 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1396
1397 static void
1398 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1399 {
1400   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1401 }
1402
1403 /* Declarators.  */
1404
1405 /* Nothing other than the parser should be creating declarators;
1406    declarators are a semi-syntactic representation of C++ entities.
1407    Other parts of the front end that need to create entities (like
1408    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1409
1410 static cp_declarator *make_call_declarator
1411   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1412 static cp_declarator *make_array_declarator
1413   (cp_declarator *, tree);
1414 static cp_declarator *make_pointer_declarator
1415   (cp_cv_quals, cp_declarator *, tree);
1416 static cp_declarator *make_reference_declarator
1417   (cp_cv_quals, cp_declarator *, bool, tree);
1418 static cp_declarator *make_ptrmem_declarator
1419   (cp_cv_quals, tree, cp_declarator *, tree);
1420
1421 /* An erroneous declarator.  */
1422 static cp_declarator *cp_error_declarator;
1423
1424 /* The obstack on which declarators and related data structures are
1425    allocated.  */
1426 static struct obstack declarator_obstack;
1427
1428 /* Alloc BYTES from the declarator memory pool.  */
1429
1430 static inline void *
1431 alloc_declarator (size_t bytes)
1432 {
1433   return obstack_alloc (&declarator_obstack, bytes);
1434 }
1435
1436 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1437    common to all declarators.  */
1438
1439 static cp_declarator *
1440 make_declarator (cp_declarator_kind kind)
1441 {
1442   cp_declarator *declarator;
1443
1444   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1445   declarator->kind = kind;
1446   declarator->attributes = NULL_TREE;
1447   declarator->std_attributes = NULL_TREE;
1448   declarator->declarator = NULL;
1449   declarator->parameter_pack_p = false;
1450   declarator->id_loc = UNKNOWN_LOCATION;
1451
1452   return declarator;
1453 }
1454
1455 /* Make a declarator for a generalized identifier.  If
1456    QUALIFYING_SCOPE is non-NULL, the identifier is
1457    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1458    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1459    is, if any.   */
1460
1461 static cp_declarator *
1462 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1463                     special_function_kind sfk)
1464 {
1465   cp_declarator *declarator;
1466
1467   /* It is valid to write:
1468
1469        class C { void f(); };
1470        typedef C D;
1471        void D::f();
1472
1473      The standard is not clear about whether `typedef const C D' is
1474      legal; as of 2002-09-15 the committee is considering that
1475      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1476      well.  */
1477   if (qualifying_scope && TYPE_P (qualifying_scope))
1478     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1479
1480   gcc_assert (identifier_p (unqualified_name)
1481               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1482               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1483
1484   declarator = make_declarator (cdk_id);
1485   declarator->u.id.qualifying_scope = qualifying_scope;
1486   declarator->u.id.unqualified_name = unqualified_name;
1487   declarator->u.id.sfk = sfk;
1488   
1489   return declarator;
1490 }
1491
1492 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1493    of modifiers such as const or volatile to apply to the pointer
1494    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1495    appertain to the pointer or reference.  */
1496
1497 cp_declarator *
1498 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1499                          tree attributes)
1500 {
1501   cp_declarator *declarator;
1502
1503   declarator = make_declarator (cdk_pointer);
1504   declarator->declarator = target;
1505   declarator->u.pointer.qualifiers = cv_qualifiers;
1506   declarator->u.pointer.class_type = NULL_TREE;
1507   if (target)
1508     {
1509       declarator->id_loc = target->id_loc;
1510       declarator->parameter_pack_p = target->parameter_pack_p;
1511       target->parameter_pack_p = false;
1512     }
1513   else
1514     declarator->parameter_pack_p = false;
1515
1516   declarator->std_attributes = attributes;
1517
1518   return declarator;
1519 }
1520
1521 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1522    represent the attributes that appertain to the pointer or
1523    reference.  */
1524
1525 cp_declarator *
1526 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1527                            bool rvalue_ref, tree attributes)
1528 {
1529   cp_declarator *declarator;
1530
1531   declarator = make_declarator (cdk_reference);
1532   declarator->declarator = target;
1533   declarator->u.reference.qualifiers = cv_qualifiers;
1534   declarator->u.reference.rvalue_ref = rvalue_ref;
1535   if (target)
1536     {
1537       declarator->id_loc = target->id_loc;
1538       declarator->parameter_pack_p = target->parameter_pack_p;
1539       target->parameter_pack_p = false;
1540     }
1541   else
1542     declarator->parameter_pack_p = false;
1543
1544   declarator->std_attributes = attributes;
1545
1546   return declarator;
1547 }
1548
1549 /* Like make_pointer_declarator -- but for a pointer to a non-static
1550    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1551    appertain to the pointer or reference.  */
1552
1553 cp_declarator *
1554 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1555                         cp_declarator *pointee,
1556                         tree attributes)
1557 {
1558   cp_declarator *declarator;
1559
1560   declarator = make_declarator (cdk_ptrmem);
1561   declarator->declarator = pointee;
1562   declarator->u.pointer.qualifiers = cv_qualifiers;
1563   declarator->u.pointer.class_type = class_type;
1564
1565   if (pointee)
1566     {
1567       declarator->parameter_pack_p = pointee->parameter_pack_p;
1568       pointee->parameter_pack_p = false;
1569     }
1570   else
1571     declarator->parameter_pack_p = false;
1572
1573   declarator->std_attributes = attributes;
1574
1575   return declarator;
1576 }
1577
1578 /* Make a declarator for the function given by TARGET, with the
1579    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1580    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1581    indicates what exceptions can be thrown.  */
1582
1583 cp_declarator *
1584 make_call_declarator (cp_declarator *target,
1585                       tree parms,
1586                       cp_cv_quals cv_qualifiers,
1587                       cp_virt_specifiers virt_specifiers,
1588                       cp_ref_qualifier ref_qualifier,
1589                       tree tx_qualifier,
1590                       tree exception_specification,
1591                       tree late_return_type,
1592                       tree requires_clause)
1593 {
1594   cp_declarator *declarator;
1595
1596   declarator = make_declarator (cdk_function);
1597   declarator->declarator = target;
1598   declarator->u.function.parameters = parms;
1599   declarator->u.function.qualifiers = cv_qualifiers;
1600   declarator->u.function.virt_specifiers = virt_specifiers;
1601   declarator->u.function.ref_qualifier = ref_qualifier;
1602   declarator->u.function.tx_qualifier = tx_qualifier;
1603   declarator->u.function.exception_specification = exception_specification;
1604   declarator->u.function.late_return_type = late_return_type;
1605   declarator->u.function.requires_clause = requires_clause;
1606   if (target)
1607     {
1608       declarator->id_loc = target->id_loc;
1609       declarator->parameter_pack_p = target->parameter_pack_p;
1610       target->parameter_pack_p = false;
1611     }
1612   else
1613     declarator->parameter_pack_p = false;
1614
1615   return declarator;
1616 }
1617
1618 /* Make a declarator for an array of BOUNDS elements, each of which is
1619    defined by ELEMENT.  */
1620
1621 cp_declarator *
1622 make_array_declarator (cp_declarator *element, tree bounds)
1623 {
1624   cp_declarator *declarator;
1625
1626   declarator = make_declarator (cdk_array);
1627   declarator->declarator = element;
1628   declarator->u.array.bounds = bounds;
1629   if (element)
1630     {
1631       declarator->id_loc = element->id_loc;
1632       declarator->parameter_pack_p = element->parameter_pack_p;
1633       element->parameter_pack_p = false;
1634     }
1635   else
1636     declarator->parameter_pack_p = false;
1637
1638   return declarator;
1639 }
1640
1641 /* Determine whether the declarator we've seen so far can be a
1642    parameter pack, when followed by an ellipsis.  */
1643 static bool 
1644 declarator_can_be_parameter_pack (cp_declarator *declarator)
1645 {
1646   if (declarator && declarator->parameter_pack_p)
1647     /* We already saw an ellipsis.  */
1648     return false;
1649
1650   /* Search for a declarator name, or any other declarator that goes
1651      after the point where the ellipsis could appear in a parameter
1652      pack. If we find any of these, then this declarator can not be
1653      made into a parameter pack.  */
1654   bool found = false;
1655   while (declarator && !found)
1656     {
1657       switch ((int)declarator->kind)
1658         {
1659         case cdk_id:
1660         case cdk_array:
1661           found = true;
1662           break;
1663
1664         case cdk_error:
1665           return true;
1666
1667         default:
1668           declarator = declarator->declarator;
1669           break;
1670         }
1671     }
1672
1673   return !found;
1674 }
1675
1676 cp_parameter_declarator *no_parameters;
1677
1678 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1679    DECLARATOR and DEFAULT_ARGUMENT.  */
1680
1681 cp_parameter_declarator *
1682 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1683                            cp_declarator *declarator,
1684                            tree default_argument,
1685                            bool template_parameter_pack_p = false)
1686 {
1687   cp_parameter_declarator *parameter;
1688
1689   parameter = ((cp_parameter_declarator *)
1690                alloc_declarator (sizeof (cp_parameter_declarator)));
1691   parameter->next = NULL;
1692   if (decl_specifiers)
1693     parameter->decl_specifiers = *decl_specifiers;
1694   else
1695     clear_decl_specs (&parameter->decl_specifiers);
1696   parameter->declarator = declarator;
1697   parameter->default_argument = default_argument;
1698   parameter->template_parameter_pack_p = template_parameter_pack_p;
1699
1700   return parameter;
1701 }
1702
1703 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1704
1705 static bool
1706 function_declarator_p (const cp_declarator *declarator)
1707 {
1708   while (declarator)
1709     {
1710       if (declarator->kind == cdk_function
1711           && declarator->declarator->kind == cdk_id)
1712         return true;
1713       if (declarator->kind == cdk_id
1714           || declarator->kind == cdk_error)
1715         return false;
1716       declarator = declarator->declarator;
1717     }
1718   return false;
1719 }
1720  
1721 /* The parser.  */
1722
1723 /* Overview
1724    --------
1725
1726    A cp_parser parses the token stream as specified by the C++
1727    grammar.  Its job is purely parsing, not semantic analysis.  For
1728    example, the parser breaks the token stream into declarators,
1729    expressions, statements, and other similar syntactic constructs.
1730    It does not check that the types of the expressions on either side
1731    of an assignment-statement are compatible, or that a function is
1732    not declared with a parameter of type `void'.
1733
1734    The parser invokes routines elsewhere in the compiler to perform
1735    semantic analysis and to build up the abstract syntax tree for the
1736    code processed.
1737
1738    The parser (and the template instantiation code, which is, in a
1739    way, a close relative of parsing) are the only parts of the
1740    compiler that should be calling push_scope and pop_scope, or
1741    related functions.  The parser (and template instantiation code)
1742    keeps track of what scope is presently active; everything else
1743    should simply honor that.  (The code that generates static
1744    initializers may also need to set the scope, in order to check
1745    access control correctly when emitting the initializers.)
1746
1747    Methodology
1748    -----------
1749
1750    The parser is of the standard recursive-descent variety.  Upcoming
1751    tokens in the token stream are examined in order to determine which
1752    production to use when parsing a non-terminal.  Some C++ constructs
1753    require arbitrary look ahead to disambiguate.  For example, it is
1754    impossible, in the general case, to tell whether a statement is an
1755    expression or declaration without scanning the entire statement.
1756    Therefore, the parser is capable of "parsing tentatively."  When the
1757    parser is not sure what construct comes next, it enters this mode.
1758    Then, while we attempt to parse the construct, the parser queues up
1759    error messages, rather than issuing them immediately, and saves the
1760    tokens it consumes.  If the construct is parsed successfully, the
1761    parser "commits", i.e., it issues any queued error messages and
1762    the tokens that were being preserved are permanently discarded.
1763    If, however, the construct is not parsed successfully, the parser
1764    rolls back its state completely so that it can resume parsing using
1765    a different alternative.
1766
1767    Future Improvements
1768    -------------------
1769
1770    The performance of the parser could probably be improved substantially.
1771    We could often eliminate the need to parse tentatively by looking ahead
1772    a little bit.  In some places, this approach might not entirely eliminate
1773    the need to parse tentatively, but it might still speed up the average
1774    case.  */
1775
1776 /* Flags that are passed to some parsing functions.  These values can
1777    be bitwise-ored together.  */
1778
1779 enum
1780 {
1781   /* No flags.  */
1782   CP_PARSER_FLAGS_NONE = 0x0,
1783   /* The construct is optional.  If it is not present, then no error
1784      should be issued.  */
1785   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1786   /* When parsing a type-specifier, treat user-defined type-names
1787      as non-type identifiers.  */
1788   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1789   /* When parsing a type-specifier, do not try to parse a class-specifier
1790      or enum-specifier.  */
1791   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1792   /* When parsing a decl-specifier-seq, only allow type-specifier or
1793      constexpr.  */
1794   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1795 };
1796
1797 /* This type is used for parameters and variables which hold
1798    combinations of the above flags.  */
1799 typedef int cp_parser_flags;
1800
1801 /* The different kinds of declarators we want to parse.  */
1802
1803 enum cp_parser_declarator_kind
1804 {
1805   /* We want an abstract declarator.  */
1806   CP_PARSER_DECLARATOR_ABSTRACT,
1807   /* We want a named declarator.  */
1808   CP_PARSER_DECLARATOR_NAMED,
1809   /* We don't mind, but the name must be an unqualified-id.  */
1810   CP_PARSER_DECLARATOR_EITHER
1811 };
1812
1813 /* The precedence values used to parse binary expressions.  The minimum value
1814    of PREC must be 1, because zero is reserved to quickly discriminate
1815    binary operators from other tokens.  */
1816
1817 enum cp_parser_prec
1818 {
1819   PREC_NOT_OPERATOR,
1820   PREC_LOGICAL_OR_EXPRESSION,
1821   PREC_LOGICAL_AND_EXPRESSION,
1822   PREC_INCLUSIVE_OR_EXPRESSION,
1823   PREC_EXCLUSIVE_OR_EXPRESSION,
1824   PREC_AND_EXPRESSION,
1825   PREC_EQUALITY_EXPRESSION,
1826   PREC_RELATIONAL_EXPRESSION,
1827   PREC_SHIFT_EXPRESSION,
1828   PREC_ADDITIVE_EXPRESSION,
1829   PREC_MULTIPLICATIVE_EXPRESSION,
1830   PREC_PM_EXPRESSION,
1831   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1832 };
1833
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835    precedence value.  */
1836
1837 struct cp_parser_binary_operations_map_node
1838 {
1839   /* The token type.  */
1840   enum cpp_ttype token_type;
1841   /* The corresponding tree code.  */
1842   enum tree_code tree_type;
1843   /* The precedence of this operator.  */
1844   enum cp_parser_prec prec;
1845 };
1846
1847 struct cp_parser_expression_stack_entry
1848 {
1849   /* Left hand side of the binary operation we are currently
1850      parsing.  */
1851   cp_expr lhs;
1852   /* Original tree code for left hand side, if it was a binary
1853      expression itself (used for -Wparentheses).  */
1854   enum tree_code lhs_type;
1855   /* Tree code for the binary operation we are parsing.  */
1856   enum tree_code tree_type;
1857   /* Precedence of the binary operation we are parsing.  */
1858   enum cp_parser_prec prec;
1859   /* Location of the binary operation we are parsing.  */
1860   location_t loc;
1861 };
1862
1863 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1864    entries because precedence levels on the stack are monotonically
1865    increasing.  */
1866 typedef struct cp_parser_expression_stack_entry
1867   cp_parser_expression_stack[NUM_PREC_VALUES];
1868
1869 /* Prototypes.  */
1870
1871 /* Constructors and destructors.  */
1872
1873 static cp_parser_context *cp_parser_context_new
1874   (cp_parser_context *);
1875
1876 /* Class variables.  */
1877
1878 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1879
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881    Transformed into an associative array (binops_by_token) by
1882    cp_parser_new.  */
1883
1884 static const cp_parser_binary_operations_map_node binops[] = {
1885   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1886   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1887
1888   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1891
1892   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1893   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1894
1895   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1896   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1897
1898   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1901   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1902
1903   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1904   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1905
1906   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1907
1908   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1909
1910   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1911
1912   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1913
1914   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1915 };
1916
1917 /* The same as binops, but initialized by cp_parser_new so that
1918    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1919    for speed.  */
1920 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1921
1922 /* Constructors and destructors.  */
1923
1924 /* Construct a new context.  The context below this one on the stack
1925    is given by NEXT.  */
1926
1927 static cp_parser_context *
1928 cp_parser_context_new (cp_parser_context* next)
1929 {
1930   cp_parser_context *context;
1931
1932   /* Allocate the storage.  */
1933   if (cp_parser_context_free_list != NULL)
1934     {
1935       /* Pull the first entry from the free list.  */
1936       context = cp_parser_context_free_list;
1937       cp_parser_context_free_list = context->next;
1938       memset (context, 0, sizeof (*context));
1939     }
1940   else
1941     context = ggc_cleared_alloc<cp_parser_context> ();
1942
1943   /* No errors have occurred yet in this context.  */
1944   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1945   /* If this is not the bottommost context, copy information that we
1946      need from the previous context.  */
1947   if (next)
1948     {
1949       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950          expression, then we are parsing one in this context, too.  */
1951       context->object_type = next->object_type;
1952       /* Thread the stack.  */
1953       context->next = next;
1954     }
1955
1956   return context;
1957 }
1958
1959 /* Managing the unparsed function queues.  */
1960
1961 #define unparsed_funs_with_default_args \
1962   parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964   parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966   parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968   parser->unparsed_queues->last ().classes
1969
1970 static void
1971 push_unparsed_function_queues (cp_parser *parser)
1972 {
1973   cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1974   vec_safe_push (parser->unparsed_queues, e);
1975 }
1976
1977 static void
1978 pop_unparsed_function_queues (cp_parser *parser)
1979 {
1980   release_tree_vector (unparsed_funs_with_definitions);
1981   parser->unparsed_queues->pop ();
1982 }
1983
1984 /* Prototypes.  */
1985
1986 /* Constructors and destructors.  */
1987
1988 static cp_parser *cp_parser_new
1989   (void);
1990
1991 /* Routines to parse various constructs.
1992
1993    Those that return `tree' will return the error_mark_node (rather
1994    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995    Sometimes, they will return an ordinary node if error-recovery was
1996    attempted, even though a parse error occurred.  So, to check
1997    whether or not a parse error occurred, you should always use
1998    cp_parser_error_occurred.  If the construct is optional (indicated
1999    either by an `_opt' in the name of the function that does the
2000    parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001    the construct is not present.  */
2002
2003 /* Lexical conventions [gram.lex]  */
2004
2005 static cp_expr cp_parser_identifier
2006   (cp_parser *);
2007 static cp_expr cp_parser_string_literal
2008   (cp_parser *, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2010   (cp_parser *);
2011 static tree cp_parser_userdef_string_literal
2012   (tree);
2013 static cp_expr cp_parser_userdef_numeric_literal
2014   (cp_parser *);
2015
2016 /* Basic concepts [gram.basic]  */
2017
2018 static bool cp_parser_translation_unit
2019   (cp_parser *);
2020
2021 /* Expressions [gram.expr]  */
2022
2023 static cp_expr cp_parser_primary_expression
2024   (cp_parser *, bool, bool, bool, cp_id_kind *);
2025 static cp_expr cp_parser_id_expression
2026   (cp_parser *, bool, bool, bool *, bool, bool);
2027 static cp_expr cp_parser_unqualified_id
2028   (cp_parser *, bool, bool, bool, bool);
2029 static tree cp_parser_nested_name_specifier_opt
2030   (cp_parser *, bool, bool, bool, bool);
2031 static tree cp_parser_nested_name_specifier
2032   (cp_parser *, bool, bool, bool, bool);
2033 static tree cp_parser_qualifying_entity
2034   (cp_parser *, bool, bool, bool, bool, bool);
2035 static cp_expr cp_parser_postfix_expression
2036   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2037 static tree cp_parser_postfix_open_square_expression
2038   (cp_parser *, tree, bool, bool);
2039 static tree cp_parser_postfix_dot_deref_expression
2040   (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2041 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2042   (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2043 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
2044 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2045 static void cp_parser_pseudo_destructor_name
2046   (cp_parser *, tree, tree *, tree *);
2047 static cp_expr cp_parser_unary_expression
2048   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2049 static enum tree_code cp_parser_unary_operator
2050   (cp_token *);
2051 static tree cp_parser_new_expression
2052   (cp_parser *);
2053 static vec<tree, va_gc> *cp_parser_new_placement
2054   (cp_parser *);
2055 static tree cp_parser_new_type_id
2056   (cp_parser *, tree *);
2057 static cp_declarator *cp_parser_new_declarator_opt
2058   (cp_parser *);
2059 static cp_declarator *cp_parser_direct_new_declarator
2060   (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_initializer
2062   (cp_parser *);
2063 static tree cp_parser_delete_expression
2064   (cp_parser *);
2065 static cp_expr cp_parser_cast_expression
2066   (cp_parser *, bool, bool, bool, cp_id_kind *);
2067 static cp_expr cp_parser_binary_expression
2068   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2069 static tree cp_parser_question_colon_clause
2070   (cp_parser *, cp_expr);
2071 static cp_expr cp_parser_assignment_expression
2072   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2073 static enum tree_code cp_parser_assignment_operator_opt
2074   (cp_parser *);
2075 static cp_expr cp_parser_expression
2076   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2077 static cp_expr cp_parser_constant_expression
2078   (cp_parser *, bool = false, bool * = NULL);
2079 static cp_expr cp_parser_builtin_offsetof
2080   (cp_parser *);
2081 static cp_expr cp_parser_lambda_expression
2082   (cp_parser *);
2083 static void cp_parser_lambda_introducer
2084   (cp_parser *, tree);
2085 static bool cp_parser_lambda_declarator_opt
2086   (cp_parser *, tree);
2087 static void cp_parser_lambda_body
2088   (cp_parser *, tree);
2089
2090 /* Statements [gram.stmt.stmt]  */
2091
2092 static void cp_parser_statement
2093   (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2094 static void cp_parser_label_for_labeled_statement
2095 (cp_parser *, tree);
2096 static tree cp_parser_expression_statement
2097   (cp_parser *, tree);
2098 static tree cp_parser_compound_statement
2099   (cp_parser *, tree, int, bool);
2100 static void cp_parser_statement_seq_opt
2101   (cp_parser *, tree);
2102 static tree cp_parser_selection_statement
2103   (cp_parser *, bool *, vec<tree> *);
2104 static tree cp_parser_condition
2105   (cp_parser *);
2106 static tree cp_parser_iteration_statement
2107   (cp_parser *, bool *, bool);
2108 static bool cp_parser_for_init_statement
2109   (cp_parser *, tree *decl);
2110 static tree cp_parser_for
2111   (cp_parser *, bool);
2112 static tree cp_parser_c_for
2113   (cp_parser *, tree, tree, bool);
2114 static tree cp_parser_range_for
2115   (cp_parser *, tree, tree, tree, bool);
2116 static void do_range_for_auto_deduction
2117   (tree, tree);
2118 static tree cp_parser_perform_range_for_lookup
2119   (tree, tree *, tree *);
2120 static tree cp_parser_range_for_member_function
2121   (tree, tree);
2122 static tree cp_parser_jump_statement
2123   (cp_parser *);
2124 static void cp_parser_declaration_statement
2125   (cp_parser *);
2126
2127 static tree cp_parser_implicitly_scoped_statement
2128   (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2129 static void cp_parser_already_scoped_statement
2130   (cp_parser *, bool *, const token_indent_info &);
2131
2132 /* Declarations [gram.dcl.dcl] */
2133
2134 static void cp_parser_declaration_seq_opt
2135   (cp_parser *);
2136 static void cp_parser_declaration
2137   (cp_parser *);
2138 static void cp_parser_block_declaration
2139   (cp_parser *, bool);
2140 static void cp_parser_simple_declaration
2141   (cp_parser *, bool, tree *);
2142 static void cp_parser_decl_specifier_seq
2143   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2144 static tree cp_parser_storage_class_specifier_opt
2145   (cp_parser *);
2146 static tree cp_parser_function_specifier_opt
2147   (cp_parser *, cp_decl_specifier_seq *);
2148 static tree cp_parser_type_specifier
2149   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2150    int *, bool *);
2151 static tree cp_parser_simple_type_specifier
2152   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2153 static tree cp_parser_type_name
2154   (cp_parser *, bool);
2155 static tree cp_parser_type_name
2156   (cp_parser *);
2157 static tree cp_parser_nonclass_name 
2158   (cp_parser* parser);
2159 static tree cp_parser_elaborated_type_specifier
2160   (cp_parser *, bool, bool);
2161 static tree cp_parser_enum_specifier
2162   (cp_parser *);
2163 static void cp_parser_enumerator_list
2164   (cp_parser *, tree);
2165 static void cp_parser_enumerator_definition
2166   (cp_parser *, tree);
2167 static tree cp_parser_namespace_name
2168   (cp_parser *);
2169 static void cp_parser_namespace_definition
2170   (cp_parser *);
2171 static void cp_parser_namespace_body
2172   (cp_parser *);
2173 static tree cp_parser_qualified_namespace_specifier
2174   (cp_parser *);
2175 static void cp_parser_namespace_alias_definition
2176   (cp_parser *);
2177 static bool cp_parser_using_declaration
2178   (cp_parser *, bool);
2179 static void cp_parser_using_directive
2180   (cp_parser *);
2181 static tree cp_parser_alias_declaration
2182   (cp_parser *);
2183 static void cp_parser_asm_definition
2184   (cp_parser *);
2185 static void cp_parser_linkage_specification
2186   (cp_parser *);
2187 static void cp_parser_static_assert
2188   (cp_parser *, bool);
2189 static tree cp_parser_decltype
2190   (cp_parser *);
2191
2192 /* Declarators [gram.dcl.decl] */
2193
2194 static tree cp_parser_init_declarator
2195   (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2196    bool, bool, int, bool *, tree *, location_t *, tree *);
2197 static cp_declarator *cp_parser_declarator
2198   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2199 static cp_declarator *cp_parser_direct_declarator
2200   (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2201 static enum tree_code cp_parser_ptr_operator
2202   (cp_parser *, tree *, cp_cv_quals *, tree *);
2203 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2204   (cp_parser *);
2205 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2206   (cp_parser *);
2207 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2208   (cp_parser *);
2209 static tree cp_parser_tx_qualifier_opt
2210   (cp_parser *);
2211 static tree cp_parser_late_return_type_opt
2212   (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2213 static tree cp_parser_declarator_id
2214   (cp_parser *, bool);
2215 static tree cp_parser_type_id
2216   (cp_parser *);
2217 static tree cp_parser_template_type_arg
2218   (cp_parser *);
2219 static tree cp_parser_trailing_type_id (cp_parser *);
2220 static tree cp_parser_type_id_1
2221   (cp_parser *, bool, bool);
2222 static void cp_parser_type_specifier_seq
2223   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2224 static tree cp_parser_parameter_declaration_clause
2225   (cp_parser *);
2226 static tree cp_parser_parameter_declaration_list
2227   (cp_parser *, bool *);
2228 static cp_parameter_declarator *cp_parser_parameter_declaration
2229   (cp_parser *, bool, bool *);
2230 static tree cp_parser_default_argument 
2231   (cp_parser *, bool);
2232 static void cp_parser_function_body
2233   (cp_parser *, bool);
2234 static tree cp_parser_initializer
2235   (cp_parser *, bool *, bool *);
2236 static cp_expr cp_parser_initializer_clause
2237   (cp_parser *, bool *);
2238 static cp_expr cp_parser_braced_list
2239   (cp_parser*, bool*);
2240 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2241   (cp_parser *, bool *);
2242
2243 static bool cp_parser_ctor_initializer_opt_and_function_body
2244   (cp_parser *, bool);
2245
2246 static tree cp_parser_late_parsing_omp_declare_simd
2247   (cp_parser *, tree);
2248
2249 static tree cp_parser_late_parsing_cilk_simd_fn_info
2250   (cp_parser *, tree);
2251
2252 static tree cp_parser_late_parsing_oacc_routine
2253   (cp_parser *, tree);
2254
2255 static tree synthesize_implicit_template_parm
2256   (cp_parser *, tree);
2257 static tree finish_fully_implicit_template
2258   (cp_parser *, tree);
2259
2260 /* Classes [gram.class] */
2261
2262 static tree cp_parser_class_name
2263   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2264 static tree cp_parser_class_specifier
2265   (cp_parser *);
2266 static tree cp_parser_class_head
2267   (cp_parser *, bool *);
2268 static enum tag_types cp_parser_class_key
2269   (cp_parser *);
2270 static void cp_parser_type_parameter_key
2271   (cp_parser* parser);
2272 static void cp_parser_member_specification_opt
2273   (cp_parser *);
2274 static void cp_parser_member_declaration
2275   (cp_parser *);
2276 static tree cp_parser_pure_specifier
2277   (cp_parser *);
2278 static tree cp_parser_constant_initializer
2279   (cp_parser *);
2280
2281 /* Derived classes [gram.class.derived] */
2282
2283 static tree cp_parser_base_clause
2284   (cp_parser *);
2285 static tree cp_parser_base_specifier
2286   (cp_parser *);
2287
2288 /* Special member functions [gram.special] */
2289
2290 static tree cp_parser_conversion_function_id
2291   (cp_parser *);
2292 static tree cp_parser_conversion_type_id
2293   (cp_parser *);
2294 static cp_declarator *cp_parser_conversion_declarator_opt
2295   (cp_parser *);
2296 static bool cp_parser_ctor_initializer_opt
2297   (cp_parser *);
2298 static void cp_parser_mem_initializer_list
2299   (cp_parser *);
2300 static tree cp_parser_mem_initializer
2301   (cp_parser *);
2302 static tree cp_parser_mem_initializer_id
2303   (cp_parser *);
2304
2305 /* Overloading [gram.over] */
2306
2307 static cp_expr cp_parser_operator_function_id
2308   (cp_parser *);
2309 static cp_expr cp_parser_operator
2310   (cp_parser *);
2311
2312 /* Templates [gram.temp] */
2313
2314 static void cp_parser_template_declaration
2315   (cp_parser *, bool);
2316 static tree cp_parser_template_parameter_list
2317   (cp_parser *);
2318 static tree cp_parser_template_parameter
2319   (cp_parser *, bool *, bool *);
2320 static tree cp_parser_type_parameter
2321   (cp_parser *, bool *);
2322 static tree cp_parser_template_id
2323   (cp_parser *, bool, bool, enum tag_types, bool);
2324 static tree cp_parser_template_name
2325   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2326 static tree cp_parser_template_argument_list
2327   (cp_parser *);
2328 static tree cp_parser_template_argument
2329   (cp_parser *);
2330 static void cp_parser_explicit_instantiation
2331   (cp_parser *);
2332 static void cp_parser_explicit_specialization
2333   (cp_parser *);
2334
2335 /* Exception handling [gram.exception] */
2336
2337 static tree cp_parser_try_block
2338   (cp_parser *);
2339 static bool cp_parser_function_try_block
2340   (cp_parser *);
2341 static void cp_parser_handler_seq
2342   (cp_parser *);
2343 static void cp_parser_handler
2344   (cp_parser *);
2345 static tree cp_parser_exception_declaration
2346   (cp_parser *);
2347 static tree cp_parser_throw_expression
2348   (cp_parser *);
2349 static tree cp_parser_exception_specification_opt
2350   (cp_parser *);
2351 static tree cp_parser_type_id_list
2352   (cp_parser *);
2353
2354 /* GNU Extensions */
2355
2356 static tree cp_parser_asm_specification_opt
2357   (cp_parser *);
2358 static tree cp_parser_asm_operand_list
2359   (cp_parser *);
2360 static tree cp_parser_asm_clobber_list
2361   (cp_parser *);
2362 static tree cp_parser_asm_label_list
2363   (cp_parser *);
2364 static bool cp_next_tokens_can_be_attribute_p
2365   (cp_parser *);
2366 static bool cp_next_tokens_can_be_gnu_attribute_p
2367   (cp_parser *);
2368 static bool cp_next_tokens_can_be_std_attribute_p
2369   (cp_parser *);
2370 static bool cp_nth_tokens_can_be_std_attribute_p
2371   (cp_parser *, size_t);
2372 static bool cp_nth_tokens_can_be_gnu_attribute_p
2373   (cp_parser *, size_t);
2374 static bool cp_nth_tokens_can_be_attribute_p
2375   (cp_parser *, size_t);
2376 static tree cp_parser_attributes_opt
2377   (cp_parser *);
2378 static tree cp_parser_gnu_attributes_opt
2379   (cp_parser *);
2380 static tree cp_parser_gnu_attribute_list
2381   (cp_parser *);
2382 static tree cp_parser_std_attribute
2383   (cp_parser *);
2384 static tree cp_parser_std_attribute_spec
2385   (cp_parser *);
2386 static tree cp_parser_std_attribute_spec_seq
2387   (cp_parser *);
2388 static bool cp_parser_extension_opt
2389   (cp_parser *, int *);
2390 static void cp_parser_label_declaration
2391   (cp_parser *);
2392
2393 /* Concept Extensions */
2394
2395 static tree cp_parser_requires_clause
2396   (cp_parser *);
2397 static tree cp_parser_requires_clause_opt
2398   (cp_parser *);
2399 static tree cp_parser_requires_expression
2400   (cp_parser *);
2401 static tree cp_parser_requirement_parameter_list
2402   (cp_parser *);
2403 static tree cp_parser_requirement_body
2404   (cp_parser *);
2405 static tree cp_parser_requirement_list
2406   (cp_parser *);
2407 static tree cp_parser_requirement
2408   (cp_parser *);
2409 static tree cp_parser_simple_requirement
2410   (cp_parser *);
2411 static tree cp_parser_compound_requirement
2412   (cp_parser *);
2413 static tree cp_parser_type_requirement
2414   (cp_parser *);
2415 static tree cp_parser_nested_requirement
2416   (cp_parser *);
2417
2418 /* Transactional Memory Extensions */
2419
2420 static tree cp_parser_transaction
2421   (cp_parser *, cp_token *);
2422 static tree cp_parser_transaction_expression
2423   (cp_parser *, enum rid);
2424 static bool cp_parser_function_transaction
2425   (cp_parser *, enum rid);
2426 static tree cp_parser_transaction_cancel
2427   (cp_parser *);
2428
2429 enum pragma_context {
2430   pragma_external,
2431   pragma_member,
2432   pragma_objc_icode,
2433   pragma_stmt,
2434   pragma_compound
2435 };
2436 static bool cp_parser_pragma
2437   (cp_parser *, enum pragma_context, bool *);
2438
2439 /* Objective-C++ Productions */
2440
2441 static tree cp_parser_objc_message_receiver
2442   (cp_parser *);
2443 static tree cp_parser_objc_message_args
2444   (cp_parser *);
2445 static tree cp_parser_objc_message_expression
2446   (cp_parser *);
2447 static cp_expr cp_parser_objc_encode_expression
2448   (cp_parser *);
2449 static tree cp_parser_objc_defs_expression
2450   (cp_parser *);
2451 static tree cp_parser_objc_protocol_expression
2452   (cp_parser *);
2453 static tree cp_parser_objc_selector_expression
2454   (cp_parser *);
2455 static cp_expr cp_parser_objc_expression
2456   (cp_parser *);
2457 static bool cp_parser_objc_selector_p
2458   (enum cpp_ttype);
2459 static tree cp_parser_objc_selector
2460   (cp_parser *);
2461 static tree cp_parser_objc_protocol_refs_opt
2462   (cp_parser *);
2463 static void cp_parser_objc_declaration
2464   (cp_parser *, tree);
2465 static tree cp_parser_objc_statement
2466   (cp_parser *);
2467 static bool cp_parser_objc_valid_prefix_attributes
2468   (cp_parser *, tree *);
2469 static void cp_parser_objc_at_property_declaration 
2470   (cp_parser *) ;
2471 static void cp_parser_objc_at_synthesize_declaration 
2472   (cp_parser *) ;
2473 static void cp_parser_objc_at_dynamic_declaration
2474   (cp_parser *) ;
2475 static tree cp_parser_objc_struct_declaration
2476   (cp_parser *) ;
2477
2478 /* Utility Routines */
2479
2480 static cp_expr cp_parser_lookup_name
2481   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2482 static tree cp_parser_lookup_name_simple
2483   (cp_parser *, tree, location_t);
2484 static tree cp_parser_maybe_treat_template_as_class
2485   (tree, bool);
2486 static bool cp_parser_check_declarator_template_parameters
2487   (cp_parser *, cp_declarator *, location_t);
2488 static bool cp_parser_check_template_parameters
2489   (cp_parser *, unsigned, location_t, cp_declarator *);
2490 static cp_expr cp_parser_simple_cast_expression
2491   (cp_parser *);
2492 static tree cp_parser_global_scope_opt
2493   (cp_parser *, bool);
2494 static bool cp_parser_constructor_declarator_p
2495   (cp_parser *, bool);
2496 static tree cp_parser_function_definition_from_specifiers_and_declarator
2497   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2498 static tree cp_parser_function_definition_after_declarator
2499   (cp_parser *, bool);
2500 static bool cp_parser_template_declaration_after_export
2501   (cp_parser *, bool);
2502 static void cp_parser_perform_template_parameter_access_checks
2503   (vec<deferred_access_check, va_gc> *);
2504 static tree cp_parser_single_declaration
2505   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2506 static cp_expr cp_parser_functional_cast
2507   (cp_parser *, tree);
2508 static tree cp_parser_save_member_function_body
2509   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2510 static tree cp_parser_save_nsdmi
2511   (cp_parser *);
2512 static tree cp_parser_enclosed_template_argument_list
2513   (cp_parser *);
2514 static void cp_parser_save_default_args
2515   (cp_parser *, tree);
2516 static void cp_parser_late_parsing_for_member
2517   (cp_parser *, tree);
2518 static tree cp_parser_late_parse_one_default_arg
2519   (cp_parser *, tree, tree, tree);
2520 static void cp_parser_late_parsing_nsdmi
2521   (cp_parser *, tree);
2522 static void cp_parser_late_parsing_default_args
2523   (cp_parser *, tree);
2524 static tree cp_parser_sizeof_operand
2525   (cp_parser *, enum rid);
2526 static tree cp_parser_trait_expr
2527   (cp_parser *, enum rid);
2528 static bool cp_parser_declares_only_class_p
2529   (cp_parser *);
2530 static void cp_parser_set_storage_class
2531   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2532 static void cp_parser_set_decl_spec_type
2533   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2534 static void set_and_check_decl_spec_loc
2535   (cp_decl_specifier_seq *decl_specs,
2536    cp_decl_spec ds, cp_token *);
2537 static bool cp_parser_friend_p
2538   (const cp_decl_specifier_seq *);
2539 static void cp_parser_required_error
2540   (cp_parser *, required_token, bool);
2541 static cp_token *cp_parser_require
2542   (cp_parser *, enum cpp_ttype, required_token);
2543 static cp_token *cp_parser_require_keyword
2544   (cp_parser *, enum rid, required_token);
2545 static bool cp_parser_token_starts_function_definition_p
2546   (cp_token *);
2547 static bool cp_parser_next_token_starts_class_definition_p
2548   (cp_parser *);
2549 static bool cp_parser_next_token_ends_template_argument_p
2550   (cp_parser *);
2551 static bool cp_parser_nth_token_starts_template_argument_list_p
2552   (cp_parser *, size_t);
2553 static enum tag_types cp_parser_token_is_class_key
2554   (cp_token *);
2555 static enum tag_types cp_parser_token_is_type_parameter_key
2556   (cp_token *);
2557 static void cp_parser_check_class_key
2558   (enum tag_types, tree type);
2559 static void cp_parser_check_access_in_redeclaration
2560   (tree type, location_t location);
2561 static bool cp_parser_optional_template_keyword
2562   (cp_parser *);
2563 static void cp_parser_pre_parsed_nested_name_specifier
2564   (cp_parser *);
2565 static bool cp_parser_cache_group
2566   (cp_parser *, enum cpp_ttype, unsigned);
2567 static tree cp_parser_cache_defarg
2568   (cp_parser *parser, bool nsdmi);
2569 static void cp_parser_parse_tentatively
2570   (cp_parser *);
2571 static void cp_parser_commit_to_tentative_parse
2572   (cp_parser *);
2573 static void cp_parser_commit_to_topmost_tentative_parse
2574   (cp_parser *);
2575 static void cp_parser_abort_tentative_parse
2576   (cp_parser *);
2577 static bool cp_parser_parse_definitely
2578   (cp_parser *);
2579 static inline bool cp_parser_parsing_tentatively
2580   (cp_parser *);
2581 static bool cp_parser_uncommitted_to_tentative_parse_p
2582   (cp_parser *);
2583 static void cp_parser_error
2584   (cp_parser *, const char *);
2585 static void cp_parser_name_lookup_error
2586   (cp_parser *, tree, tree, name_lookup_error, location_t);
2587 static bool cp_parser_simulate_error
2588   (cp_parser *);
2589 static bool cp_parser_check_type_definition
2590   (cp_parser *);
2591 static void cp_parser_check_for_definition_in_return_type
2592   (cp_declarator *, tree, location_t type_location);
2593 static void cp_parser_check_for_invalid_template_id
2594   (cp_parser *, tree, enum tag_types, location_t location);
2595 static bool cp_parser_non_integral_constant_expression
2596   (cp_parser *, non_integral_constant);
2597 static void cp_parser_diagnose_invalid_type_name
2598   (cp_parser *, tree, location_t);
2599 static bool cp_parser_parse_and_diagnose_invalid_type_name
2600   (cp_parser *);
2601 static int cp_parser_skip_to_closing_parenthesis
2602   (cp_parser *, bool, bool, bool);
2603 static void cp_parser_skip_to_end_of_statement
2604   (cp_parser *);
2605 static void cp_parser_consume_semicolon_at_end_of_statement
2606   (cp_parser *);
2607 static void cp_parser_skip_to_end_of_block_or_statement
2608   (cp_parser *);
2609 static bool cp_parser_skip_to_closing_brace
2610   (cp_parser *);
2611 static void cp_parser_skip_to_end_of_template_parameter_list
2612   (cp_parser *);
2613 static void cp_parser_skip_to_pragma_eol
2614   (cp_parser*, cp_token *);
2615 static bool cp_parser_error_occurred
2616   (cp_parser *);
2617 static bool cp_parser_allow_gnu_extensions_p
2618   (cp_parser *);
2619 static bool cp_parser_is_pure_string_literal
2620   (cp_token *);
2621 static bool cp_parser_is_string_literal
2622   (cp_token *);
2623 static bool cp_parser_is_keyword
2624   (cp_token *, enum rid);
2625 static tree cp_parser_make_typename_type
2626   (cp_parser *, tree, location_t location);
2627 static cp_declarator * cp_parser_make_indirect_declarator
2628   (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2629 static bool cp_parser_compound_literal_p
2630   (cp_parser *);
2631 static bool cp_parser_array_designator_p
2632   (cp_parser *);
2633 static bool cp_parser_skip_to_closing_square_bracket
2634   (cp_parser *);
2635
2636 /* Concept-related syntactic transformations */
2637
2638 static tree cp_parser_maybe_concept_name       (cp_parser *, tree);
2639 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2640
2641 // -------------------------------------------------------------------------- //
2642 // Unevaluated Operand Guard
2643 //
2644 // Implementation of an RAII helper for unevaluated operand parsing.
2645 cp_unevaluated::cp_unevaluated ()
2646 {
2647   ++cp_unevaluated_operand;
2648   ++c_inhibit_evaluation_warnings;
2649 }
2650
2651 cp_unevaluated::~cp_unevaluated ()
2652 {
2653   --c_inhibit_evaluation_warnings;
2654   --cp_unevaluated_operand;
2655 }
2656
2657 // -------------------------------------------------------------------------- //
2658 // Tentative Parsing
2659
2660 /* Returns nonzero if we are parsing tentatively.  */
2661
2662 static inline bool
2663 cp_parser_parsing_tentatively (cp_parser* parser)
2664 {
2665   return parser->context->next != NULL;
2666 }
2667
2668 /* Returns nonzero if TOKEN is a string literal.  */
2669
2670 static bool
2671 cp_parser_is_pure_string_literal (cp_token* token)
2672 {
2673   return (token->type == CPP_STRING ||
2674           token->type == CPP_STRING16 ||
2675           token->type == CPP_STRING32 ||
2676           token->type == CPP_WSTRING ||
2677           token->type == CPP_UTF8STRING);
2678 }
2679
2680 /* Returns nonzero if TOKEN is a string literal
2681    of a user-defined string literal.  */
2682
2683 static bool
2684 cp_parser_is_string_literal (cp_token* token)
2685 {
2686   return (cp_parser_is_pure_string_literal (token) ||
2687           token->type == CPP_STRING_USERDEF ||
2688           token->type == CPP_STRING16_USERDEF ||
2689           token->type == CPP_STRING32_USERDEF ||
2690           token->type == CPP_WSTRING_USERDEF ||
2691           token->type == CPP_UTF8STRING_USERDEF);
2692 }
2693
2694 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2695
2696 static bool
2697 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2698 {
2699   return token->keyword == keyword;
2700 }
2701
2702 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2703    PRAGMA_NONE.  */
2704
2705 static enum pragma_kind
2706 cp_parser_pragma_kind (cp_token *token)
2707 {
2708   if (token->type != CPP_PRAGMA)
2709     return PRAGMA_NONE;
2710   /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
2711   return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2712 }
2713
2714 /* Helper function for cp_parser_error.
2715    Having peeked a token of kind TOK1_KIND that might signify
2716    a conflict marker, peek successor tokens to determine
2717    if we actually do have a conflict marker.
2718    Specifically, we consider a run of 7 '<', '=' or '>' characters
2719    at the start of a line as a conflict marker.
2720    These come through the lexer as three pairs and a single,
2721    e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2722    If it returns true, *OUT_LOC is written to with the location/range
2723    of the marker.  */
2724
2725 static bool
2726 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2727                                location_t *out_loc)
2728 {
2729   cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2730   if (token2->type != tok1_kind)
2731     return false;
2732   cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2733   if (token3->type != tok1_kind)
2734     return false;
2735   cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2736   if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2737     return false;
2738
2739   /* It must be at the start of the line.  */
2740   location_t start_loc = cp_lexer_peek_token (lexer)->location;
2741   if (LOCATION_COLUMN (start_loc) != 1)
2742     return false;
2743
2744   /* We have a conflict marker.  Construct a location of the form:
2745        <<<<<<<
2746        ^~~~~~~
2747      with start == caret, finishing at the end of the marker.  */
2748   location_t finish_loc = get_finish (token4->location);
2749   *out_loc = make_location (start_loc, start_loc, finish_loc);
2750
2751   return true;
2752 }
2753
2754 /* If not parsing tentatively, issue a diagnostic of the form
2755       FILE:LINE: MESSAGE before TOKEN
2756    where TOKEN is the next token in the input stream.  MESSAGE
2757    (specified by the caller) is usually of the form "expected
2758    OTHER-TOKEN".  */
2759
2760 static void
2761 cp_parser_error (cp_parser* parser, const char* gmsgid)
2762 {
2763   if (!cp_parser_simulate_error (parser))
2764     {
2765       cp_token *token = cp_lexer_peek_token (parser->lexer);
2766       /* This diagnostic makes more sense if it is tagged to the line
2767          of the token we just peeked at.  */
2768       cp_lexer_set_source_position_from_token (token);
2769
2770       if (token->type == CPP_PRAGMA)
2771         {
2772           error_at (token->location,
2773                     "%<#pragma%> is not allowed here");
2774           cp_parser_skip_to_pragma_eol (parser, token);
2775           return;
2776         }
2777
2778       /* If this is actually a conflict marker, report it as such.  */
2779       if (token->type == CPP_LSHIFT
2780           || token->type == CPP_RSHIFT
2781           || token->type == CPP_EQ_EQ)
2782         {
2783           location_t loc;
2784           if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2785             {
2786               error_at (loc, "version control conflict marker in file");
2787               return;
2788             }
2789         }
2790
2791       c_parse_error (gmsgid,
2792                      /* Because c_parser_error does not understand
2793                         CPP_KEYWORD, keywords are treated like
2794                         identifiers.  */
2795                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2796                      token->u.value, token->flags);
2797     }
2798 }
2799
2800 /* Issue an error about name-lookup failing.  NAME is the
2801    IDENTIFIER_NODE DECL is the result of
2802    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2803    the thing that we hoped to find.  */
2804
2805 static void
2806 cp_parser_name_lookup_error (cp_parser* parser,
2807                              tree name,
2808                              tree decl,
2809                              name_lookup_error desired,
2810                              location_t location)
2811 {
2812   /* If name lookup completely failed, tell the user that NAME was not
2813      declared.  */
2814   if (decl == error_mark_node)
2815     {
2816       if (parser->scope && parser->scope != global_namespace)
2817         error_at (location, "%<%E::%E%> has not been declared",
2818                   parser->scope, name);
2819       else if (parser->scope == global_namespace)
2820         error_at (location, "%<::%E%> has not been declared", name);
2821       else if (parser->object_scope
2822                && !CLASS_TYPE_P (parser->object_scope))
2823         error_at (location, "request for member %qE in non-class type %qT",
2824                   name, parser->object_scope);
2825       else if (parser->object_scope)
2826         error_at (location, "%<%T::%E%> has not been declared",
2827                   parser->object_scope, name);
2828       else
2829         error_at (location, "%qE has not been declared", name);
2830     }
2831   else if (parser->scope && parser->scope != global_namespace)
2832     {
2833       switch (desired)
2834         {
2835           case NLE_TYPE:
2836             error_at (location, "%<%E::%E%> is not a type",
2837                                 parser->scope, name);
2838             break;
2839           case NLE_CXX98:
2840             error_at (location, "%<%E::%E%> is not a class or namespace",
2841                                 parser->scope, name);
2842             break;
2843           case NLE_NOT_CXX98:
2844             error_at (location,
2845                       "%<%E::%E%> is not a class, namespace, or enumeration",
2846                       parser->scope, name);
2847             break;
2848           default:
2849             gcc_unreachable ();
2850             
2851         }
2852     }
2853   else if (parser->scope == global_namespace)
2854     {
2855       switch (desired)
2856         {
2857           case NLE_TYPE:
2858             error_at (location, "%<::%E%> is not a type", name);
2859             break;
2860           case NLE_CXX98:
2861             error_at (location, "%<::%E%> is not a class or namespace", name);
2862             break;
2863           case NLE_NOT_CXX98:
2864             error_at (location,
2865                       "%<::%E%> is not a class, namespace, or enumeration",
2866                       name);
2867             break;
2868           default:
2869             gcc_unreachable ();
2870         }
2871     }
2872   else
2873     {
2874       switch (desired)
2875         {
2876           case NLE_TYPE:
2877             error_at (location, "%qE is not a type", name);
2878             break;
2879           case NLE_CXX98:
2880             error_at (location, "%qE is not a class or namespace", name);
2881             break;
2882           case NLE_NOT_CXX98:
2883             error_at (location,
2884                       "%qE is not a class, namespace, or enumeration", name);
2885             break;
2886           default:
2887             gcc_unreachable ();
2888         }
2889     }
2890 }
2891
2892 /* If we are parsing tentatively, remember that an error has occurred
2893    during this tentative parse.  Returns true if the error was
2894    simulated; false if a message should be issued by the caller.  */
2895
2896 static bool
2897 cp_parser_simulate_error (cp_parser* parser)
2898 {
2899   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2900     {
2901       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2902       return true;
2903     }
2904   return false;
2905 }
2906
2907 /* This function is called when a type is defined.  If type
2908    definitions are forbidden at this point, an error message is
2909    issued.  */
2910
2911 static bool
2912 cp_parser_check_type_definition (cp_parser* parser)
2913 {
2914   /* If types are forbidden here, issue a message.  */
2915   if (parser->type_definition_forbidden_message)
2916     {
2917       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2918          in the message need to be interpreted.  */
2919       error (parser->type_definition_forbidden_message);
2920       return false;
2921     }
2922   return true;
2923 }
2924
2925 /* This function is called when the DECLARATOR is processed.  The TYPE
2926    was a type defined in the decl-specifiers.  If it is invalid to
2927    define a type in the decl-specifiers for DECLARATOR, an error is
2928    issued. TYPE_LOCATION is the location of TYPE and is used
2929    for error reporting.  */
2930
2931 static void
2932 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2933                                                tree type, location_t type_location)
2934 {
2935   /* [dcl.fct] forbids type definitions in return types.
2936      Unfortunately, it's not easy to know whether or not we are
2937      processing a return type until after the fact.  */
2938   while (declarator
2939          && (declarator->kind == cdk_pointer
2940              || declarator->kind == cdk_reference
2941              || declarator->kind == cdk_ptrmem))
2942     declarator = declarator->declarator;
2943   if (declarator
2944       && declarator->kind == cdk_function)
2945     {
2946       error_at (type_location,
2947                 "new types may not be defined in a return type");
2948       inform (type_location, 
2949               "(perhaps a semicolon is missing after the definition of %qT)",
2950               type);
2951     }
2952 }
2953
2954 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2955    "<" in any valid C++ program.  If the next token is indeed "<",
2956    issue a message warning the user about what appears to be an
2957    invalid attempt to form a template-id. LOCATION is the location
2958    of the type-specifier (TYPE) */
2959
2960 static void
2961 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2962                                          tree type,
2963                                          enum tag_types tag_type,
2964                                          location_t location)
2965 {
2966   cp_token_position start = 0;
2967
2968   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2969     {
2970       if (TYPE_P (type))
2971         error_at (location, "%qT is not a template", type);
2972       else if (identifier_p (type))
2973         {
2974           if (tag_type != none_type)
2975             error_at (location, "%qE is not a class template", type);
2976           else
2977             error_at (location, "%qE is not a template", type);
2978         }
2979       else
2980         error_at (location, "invalid template-id");
2981       /* Remember the location of the invalid "<".  */
2982       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2983         start = cp_lexer_token_position (parser->lexer, true);
2984       /* Consume the "<".  */
2985       cp_lexer_consume_token (parser->lexer);
2986       /* Parse the template arguments.  */
2987       cp_parser_enclosed_template_argument_list (parser);
2988       /* Permanently remove the invalid template arguments so that
2989          this error message is not issued again.  */
2990       if (start)
2991         cp_lexer_purge_tokens_after (parser->lexer, start);
2992     }
2993 }
2994
2995 /* If parsing an integral constant-expression, issue an error message
2996    about the fact that THING appeared and return true.  Otherwise,
2997    return false.  In either case, set
2998    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2999
3000 static bool
3001 cp_parser_non_integral_constant_expression (cp_parser  *parser,
3002                                             non_integral_constant thing)
3003 {
3004   parser->non_integral_constant_expression_p = true;
3005   if (parser->integral_constant_expression_p)
3006     {
3007       if (!parser->allow_non_integral_constant_expression_p)
3008         {
3009           const char *msg = NULL;
3010           switch (thing)
3011             {
3012               case NIC_FLOAT:
3013                 error ("floating-point literal "
3014                        "cannot appear in a constant-expression");
3015                 return true;
3016               case NIC_CAST:
3017                 error ("a cast to a type other than an integral or "
3018                        "enumeration type cannot appear in a "
3019                        "constant-expression");
3020                 return true;
3021               case NIC_TYPEID:
3022                 error ("%<typeid%> operator "
3023                        "cannot appear in a constant-expression");
3024                 return true;
3025               case NIC_NCC:
3026                 error ("non-constant compound literals "
3027                        "cannot appear in a constant-expression");
3028                 return true;
3029               case NIC_FUNC_CALL:
3030                 error ("a function call "
3031                        "cannot appear in a constant-expression");
3032                 return true;
3033               case NIC_INC:
3034                 error ("an increment "
3035                        "cannot appear in a constant-expression");
3036                 return true;
3037               case NIC_DEC:
3038                 error ("an decrement "
3039                        "cannot appear in a constant-expression");
3040                 return true;
3041               case NIC_ARRAY_REF:
3042                 error ("an array reference "
3043                        "cannot appear in a constant-expression");
3044                 return true;
3045               case NIC_ADDR_LABEL:
3046                 error ("the address of a label "
3047                        "cannot appear in a constant-expression");
3048                 return true;
3049               case NIC_OVERLOADED:
3050                 error ("calls to overloaded operators "
3051                        "cannot appear in a constant-expression");
3052                 return true;
3053               case NIC_ASSIGNMENT:
3054                 error ("an assignment cannot appear in a constant-expression");
3055                 return true;
3056               case NIC_COMMA:
3057                 error ("a comma operator "
3058                        "cannot appear in a constant-expression");
3059                 return true;
3060               case NIC_CONSTRUCTOR:
3061                 error ("a call to a constructor "
3062                        "cannot appear in a constant-expression");
3063                 return true;
3064               case NIC_TRANSACTION:
3065                 error ("a transaction expression "
3066                        "cannot appear in a constant-expression");
3067                 return true;
3068               case NIC_THIS:
3069                 msg = "this";
3070                 break;
3071               case NIC_FUNC_NAME:
3072                 msg = "__FUNCTION__";
3073                 break;
3074               case NIC_PRETTY_FUNC:
3075                 msg = "__PRETTY_FUNCTION__";
3076                 break;
3077               case NIC_C99_FUNC:
3078                 msg = "__func__";
3079                 break;
3080               case NIC_VA_ARG:
3081                 msg = "va_arg";
3082                 break;
3083               case NIC_ARROW:
3084                 msg = "->";
3085                 break;
3086               case NIC_POINT:
3087                 msg = ".";
3088                 break;
3089               case NIC_STAR:
3090                 msg = "*";
3091                 break;
3092               case NIC_ADDR:
3093                 msg = "&";
3094                 break;
3095               case NIC_PREINCREMENT:
3096                 msg = "++";
3097                 break;
3098               case NIC_PREDECREMENT:
3099                 msg = "--";
3100                 break;
3101               case NIC_NEW:
3102                 msg = "new";
3103                 break;
3104               case NIC_DEL:
3105                 msg = "delete";
3106                 break;
3107               default:
3108                 gcc_unreachable ();
3109             }
3110           if (msg)
3111             error ("%qs cannot appear in a constant-expression", msg);
3112           return true;
3113         }
3114     }
3115   return false;
3116 }
3117
3118 /* Emit a diagnostic for an invalid type name.  This function commits
3119    to the current active tentative parse, if any.  (Otherwise, the
3120    problematic construct might be encountered again later, resulting
3121    in duplicate error messages.) LOCATION is the location of ID.  */
3122
3123 static void
3124 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3125                                       location_t location)
3126 {
3127   tree decl, ambiguous_decls;
3128   cp_parser_commit_to_tentative_parse (parser);
3129   /* Try to lookup the identifier.  */
3130   decl = cp_parser_lookup_name (parser, id, none_type,
3131                                 /*is_template=*/false,
3132                                 /*is_namespace=*/false,
3133                                 /*check_dependency=*/true,
3134                                 &ambiguous_decls, location);
3135   if (ambiguous_decls)
3136     /* If the lookup was ambiguous, an error will already have
3137        been issued.  */
3138     return;
3139   /* If the lookup found a template-name, it means that the user forgot
3140   to specify an argument list. Emit a useful error message.  */
3141   if (DECL_TYPE_TEMPLATE_P (decl))
3142     {
3143       error_at (location,
3144                 "invalid use of template-name %qE without an argument list",
3145                 decl);
3146       inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3147     }
3148   else if (TREE_CODE (id) == BIT_NOT_EXPR)
3149     error_at (location, "invalid use of destructor %qD as a type", id);
3150   else if (TREE_CODE (decl) == TYPE_DECL)
3151     /* Something like 'unsigned A a;'  */
3152     error_at (location, "invalid combination of multiple type-specifiers");
3153   else if (!parser->scope)
3154     {
3155       /* Issue an error message.  */
3156       error_at (location, "%qE does not name a type", id);
3157       /* If we're in a template class, it's possible that the user was
3158          referring to a type from a base class.  For example:
3159
3160            template <typename T> struct A { typedef T X; };
3161            template <typename T> struct B : public A<T> { X x; };
3162
3163          The user should have said "typename A<T>::X".  */
3164       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3165         inform (location, "C++11 %<constexpr%> only available with "
3166                 "-std=c++11 or -std=gnu++11");
3167       else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3168         inform (location, "C++11 %<noexcept%> only available with "
3169                 "-std=c++11 or -std=gnu++11");
3170       else if (cxx_dialect < cxx11
3171                && TREE_CODE (id) == IDENTIFIER_NODE
3172                && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3173         inform (location, "C++11 %<thread_local%> only available with "
3174                 "-std=c++11 or -std=gnu++11");
3175       else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3176         inform (location, "%<concept%> only available with -fconcepts");
3177       else if (processing_template_decl && current_class_type
3178                && TYPE_BINFO (current_class_type))
3179         {
3180           tree b;
3181
3182           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3183                b;
3184                b = TREE_CHAIN (b))
3185             {
3186               tree base_type = BINFO_TYPE (b);
3187               if (CLASS_TYPE_P (base_type)
3188                   && dependent_type_p (base_type))
3189                 {
3190                   tree field;
3191                   /* Go from a particular instantiation of the
3192                      template (which will have an empty TYPE_FIELDs),
3193                      to the main version.  */
3194                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3195                   for (field = TYPE_FIELDS (base_type);
3196                        field;
3197                        field = DECL_CHAIN (field))
3198                     if (TREE_CODE (field) == TYPE_DECL
3199                         && DECL_NAME (field) == id)
3200                       {
3201                         inform (location, 
3202                                 "(perhaps %<typename %T::%E%> was intended)",
3203                                 BINFO_TYPE (b), id);
3204                         break;
3205                       }
3206                   if (field)
3207                     break;
3208                 }
3209             }
3210         }
3211     }
3212   /* Here we diagnose qualified-ids where the scope is actually correct,
3213      but the identifier does not resolve to a valid type name.  */
3214   else if (parser->scope != error_mark_node)
3215     {
3216       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3217         {
3218           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3219             error_at (location_of (id),
3220                       "%qE in namespace %qE does not name a template type",
3221                       id, parser->scope);
3222           else
3223             error_at (location_of (id),
3224                       "%qE in namespace %qE does not name a type",
3225                       id, parser->scope);
3226           if (DECL_P (decl))
3227             inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3228         }
3229       else if (CLASS_TYPE_P (parser->scope)
3230                && constructor_name_p (id, parser->scope))
3231         {
3232           /* A<T>::A<T>() */
3233           error_at (location, "%<%T::%E%> names the constructor, not"
3234                     " the type", parser->scope, id);
3235           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3236             error_at (location, "and %qT has no template constructors",
3237                       parser->scope);
3238         }
3239       else if (TYPE_P (parser->scope)
3240                && dependent_scope_p (parser->scope))
3241         error_at (location, "need %<typename%> before %<%T::%E%> because "
3242                   "%qT is a dependent scope",
3243                   parser->scope, id, parser->scope);
3244       else if (TYPE_P (parser->scope))
3245         {
3246           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3247             error_at (location_of (id),
3248                       "%qE in %q#T does not name a template type",
3249                       id, parser->scope);
3250           else
3251             error_at (location_of (id),
3252                       "%qE in %q#T does not name a type",
3253                       id, parser->scope);
3254           if (DECL_P (decl))
3255             inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3256         }
3257       else
3258         gcc_unreachable ();
3259     }
3260 }
3261
3262 /* Check for a common situation where a type-name should be present,
3263    but is not, and issue a sensible error message.  Returns true if an
3264    invalid type-name was detected.
3265
3266    The situation handled by this function are variable declarations of the
3267    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3268    Usually, `ID' should name a type, but if we got here it means that it
3269    does not. We try to emit the best possible error message depending on
3270    how exactly the id-expression looks like.  */
3271
3272 static bool
3273 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3274 {
3275   tree id;
3276   cp_token *token = cp_lexer_peek_token (parser->lexer);
3277
3278   /* Avoid duplicate error about ambiguous lookup.  */
3279   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3280     {
3281       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3282       if (next->type == CPP_NAME && next->error_reported)
3283         goto out;
3284     }
3285
3286   cp_parser_parse_tentatively (parser);
3287   id = cp_parser_id_expression (parser,
3288                                 /*template_keyword_p=*/false,
3289                                 /*check_dependency_p=*/true,
3290                                 /*template_p=*/NULL,
3291                                 /*declarator_p=*/true,
3292                                 /*optional_p=*/false);
3293   /* If the next token is a (, this is a function with no explicit return
3294      type, i.e. constructor, destructor or conversion op.  */
3295   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3296       || TREE_CODE (id) == TYPE_DECL)
3297     {
3298       cp_parser_abort_tentative_parse (parser);
3299       return false;
3300     }
3301   if (!cp_parser_parse_definitely (parser))
3302     return false;
3303
3304   /* Emit a diagnostic for the invalid type.  */
3305   cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3306  out:
3307   /* If we aren't in the middle of a declarator (i.e. in a
3308      parameter-declaration-clause), skip to the end of the declaration;
3309      there's no point in trying to process it.  */
3310   if (!parser->in_declarator_p)
3311     cp_parser_skip_to_end_of_block_or_statement (parser);
3312   return true;
3313 }
3314
3315 /* Consume tokens up to, and including, the next non-nested closing `)'.
3316    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3317    are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3318    found an unnested token of that type.  */
3319
3320 static int
3321 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3322                                          bool recovering,
3323                                          cpp_ttype or_ttype,
3324                                          bool consume_paren)
3325 {
3326   unsigned paren_depth = 0;
3327   unsigned brace_depth = 0;
3328   unsigned square_depth = 0;
3329
3330   if (recovering && or_ttype == CPP_EOF
3331       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3332     return 0;
3333
3334   while (true)
3335     {
3336       cp_token * token = cp_lexer_peek_token (parser->lexer);
3337
3338       /* Have we found what we're looking for before the closing paren?  */
3339       if (token->type == or_ttype && or_ttype != CPP_EOF
3340           && !brace_depth && !paren_depth && !square_depth)
3341         return -1;
3342
3343       switch (token->type)
3344         {
3345         case CPP_EOF:
3346         case CPP_PRAGMA_EOL:
3347           /* If we've run out of tokens, then there is no closing `)'.  */
3348           return 0;
3349
3350         /* This is good for lambda expression capture-lists.  */
3351         case CPP_OPEN_SQUARE:
3352           ++square_depth;
3353           break;
3354         case CPP_CLOSE_SQUARE:
3355           if (!square_depth--)
3356             return 0;
3357           break;
3358
3359         case CPP_SEMICOLON:
3360           /* This matches the processing in skip_to_end_of_statement.  */
3361           if (!brace_depth)
3362             return 0;
3363           break;
3364
3365         case CPP_OPEN_BRACE:
3366           ++brace_depth;
3367           break;
3368         case CPP_CLOSE_BRACE:
3369           if (!brace_depth--)
3370             return 0;
3371           break;
3372
3373         case CPP_OPEN_PAREN:
3374           if (!brace_depth)
3375             ++paren_depth;
3376           break;
3377
3378         case CPP_CLOSE_PAREN:
3379           if (!brace_depth && !paren_depth--)
3380             {
3381               if (consume_paren)
3382                 cp_lexer_consume_token (parser->lexer);
3383               return 1;
3384             }
3385           break;
3386
3387         default:
3388           break;
3389         }
3390
3391       /* Consume the token.  */
3392       cp_lexer_consume_token (parser->lexer);
3393     }
3394 }
3395
3396 /* Consume tokens up to, and including, the next non-nested closing `)'.
3397    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3398    are doing error recovery. Returns -1 if OR_COMMA is true and we
3399    found an unnested token of that type.  */
3400
3401 static int
3402 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3403                                        bool recovering,
3404                                        bool or_comma,
3405                                        bool consume_paren)
3406 {
3407   cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3408   return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3409                                                   ttype, consume_paren);
3410 }
3411
3412 /* Consume tokens until we reach the end of the current statement.
3413    Normally, that will be just before consuming a `;'.  However, if a
3414    non-nested `}' comes first, then we stop before consuming that.  */
3415
3416 static void
3417 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3418 {
3419   unsigned nesting_depth = 0;
3420
3421   /* Unwind generic function template scope if necessary.  */
3422   if (parser->fully_implicit_function_template_p)
3423     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3424
3425   while (true)
3426     {
3427       cp_token *token = cp_lexer_peek_token (parser->lexer);
3428
3429       switch (token->type)
3430         {
3431         case CPP_EOF:
3432         case CPP_PRAGMA_EOL:
3433           /* If we've run out of tokens, stop.  */
3434           return;
3435
3436         case CPP_SEMICOLON:
3437           /* If the next token is a `;', we have reached the end of the
3438              statement.  */
3439           if (!nesting_depth)
3440             return;
3441           break;
3442
3443         case CPP_CLOSE_BRACE:
3444           /* If this is a non-nested '}', stop before consuming it.
3445              That way, when confronted with something like:
3446
3447                { 3 + }
3448
3449              we stop before consuming the closing '}', even though we
3450              have not yet reached a `;'.  */
3451           if (nesting_depth == 0)
3452             return;
3453
3454           /* If it is the closing '}' for a block that we have
3455              scanned, stop -- but only after consuming the token.
3456              That way given:
3457
3458                 void f g () { ... }
3459                 typedef int I;
3460
3461              we will stop after the body of the erroneously declared
3462              function, but before consuming the following `typedef'
3463              declaration.  */
3464           if (--nesting_depth == 0)
3465             {
3466               cp_lexer_consume_token (parser->lexer);
3467               return;
3468             }
3469
3470         case CPP_OPEN_BRACE:
3471           ++nesting_depth;
3472           break;
3473
3474         default:
3475           break;
3476         }
3477
3478       /* Consume the token.  */
3479       cp_lexer_consume_token (parser->lexer);
3480     }
3481 }
3482
3483 /* This function is called at the end of a statement or declaration.
3484    If the next token is a semicolon, it is consumed; otherwise, error
3485    recovery is attempted.  */
3486
3487 static void
3488 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3489 {
3490   /* Look for the trailing `;'.  */
3491   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3492     {
3493       /* If there is additional (erroneous) input, skip to the end of
3494          the statement.  */
3495       cp_parser_skip_to_end_of_statement (parser);
3496       /* If the next token is now a `;', consume it.  */
3497       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3498         cp_lexer_consume_token (parser->lexer);
3499     }
3500 }
3501
3502 /* Skip tokens until we have consumed an entire block, or until we
3503    have consumed a non-nested `;'.  */
3504
3505 static void
3506 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3507 {
3508   int nesting_depth = 0;
3509
3510   /* Unwind generic function template scope if necessary.  */
3511   if (parser->fully_implicit_function_template_p)
3512     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3513
3514   while (nesting_depth >= 0)
3515     {
3516       cp_token *token = cp_lexer_peek_token (parser->lexer);
3517
3518       switch (token->type)
3519         {
3520         case CPP_EOF:
3521         case CPP_PRAGMA_EOL:
3522           /* If we've run out of tokens, stop.  */
3523           return;
3524
3525         case CPP_SEMICOLON:
3526           /* Stop if this is an unnested ';'. */
3527           if (!nesting_depth)
3528             nesting_depth = -1;
3529           break;
3530
3531         case CPP_CLOSE_BRACE:
3532           /* Stop if this is an unnested '}', or closes the outermost
3533              nesting level.  */
3534           nesting_depth--;
3535           if (nesting_depth < 0)
3536             return;
3537           if (!nesting_depth)
3538             nesting_depth = -1;
3539           break;
3540
3541         case CPP_OPEN_BRACE:
3542           /* Nest. */
3543           nesting_depth++;
3544           break;
3545
3546         default:
3547           break;
3548         }
3549
3550       /* Consume the token.  */
3551       cp_lexer_consume_token (parser->lexer);
3552     }
3553 }
3554
3555 /* Skip tokens until a non-nested closing curly brace is the next
3556    token, or there are no more tokens. Return true in the first case,
3557    false otherwise.  */
3558
3559 static bool
3560 cp_parser_skip_to_closing_brace (cp_parser *parser)
3561 {
3562   unsigned nesting_depth = 0;
3563
3564   while (true)
3565     {
3566       cp_token *token = cp_lexer_peek_token (parser->lexer);
3567
3568       switch (token->type)
3569         {
3570         case CPP_EOF:
3571         case CPP_PRAGMA_EOL:
3572           /* If we've run out of tokens, stop.  */
3573           return false;
3574
3575         case CPP_CLOSE_BRACE:
3576           /* If the next token is a non-nested `}', then we have reached
3577              the end of the current block.  */
3578           if (nesting_depth-- == 0)
3579             return true;
3580           break;
3581
3582         case CPP_OPEN_BRACE:
3583           /* If it the next token is a `{', then we are entering a new
3584              block.  Consume the entire block.  */
3585           ++nesting_depth;
3586           break;
3587
3588         default:
3589           break;
3590         }
3591
3592       /* Consume the token.  */
3593       cp_lexer_consume_token (parser->lexer);
3594     }
3595 }
3596
3597 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3598    parameter is the PRAGMA token, allowing us to purge the entire pragma
3599    sequence.  */
3600
3601 static void
3602 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3603 {
3604   cp_token *token;
3605
3606   parser->lexer->in_pragma = false;
3607
3608   do
3609     token = cp_lexer_consume_token (parser->lexer);
3610   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3611
3612   /* Ensure that the pragma is not parsed again.  */
3613   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3614 }
3615
3616 /* Require pragma end of line, resyncing with it as necessary.  The
3617    arguments are as for cp_parser_skip_to_pragma_eol.  */
3618
3619 static void
3620 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3621 {
3622   parser->lexer->in_pragma = false;
3623   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3624     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3625 }
3626
3627 /* This is a simple wrapper around make_typename_type. When the id is
3628    an unresolved identifier node, we can provide a superior diagnostic
3629    using cp_parser_diagnose_invalid_type_name.  */
3630
3631 static tree
3632 cp_parser_make_typename_type (cp_parser *parser, tree id,
3633                               location_t id_location)
3634 {
3635   tree result;
3636   if (identifier_p (id))
3637     {
3638       result = make_typename_type (parser->scope, id, typename_type,
3639                                    /*complain=*/tf_none);
3640       if (result == error_mark_node)
3641         cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3642       return result;
3643     }
3644   return make_typename_type (parser->scope, id, typename_type, tf_error);
3645 }
3646
3647 /* This is a wrapper around the
3648    make_{pointer,ptrmem,reference}_declarator functions that decides
3649    which one to call based on the CODE and CLASS_TYPE arguments. The
3650    CODE argument should be one of the values returned by
3651    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3652    appertain to the pointer or reference.  */
3653
3654 static cp_declarator *
3655 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3656                                     cp_cv_quals cv_qualifiers,
3657                                     cp_declarator *target,
3658                                     tree attributes)
3659 {
3660   if (code == ERROR_MARK)
3661     return cp_error_declarator;
3662
3663   if (code == INDIRECT_REF)
3664     if (class_type == NULL_TREE)
3665       return make_pointer_declarator (cv_qualifiers, target, attributes);
3666     else
3667       return make_ptrmem_declarator (cv_qualifiers, class_type,
3668                                      target, attributes);
3669   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3670     return make_reference_declarator (cv_qualifiers, target,
3671                                       false, attributes);
3672   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3673     return make_reference_declarator (cv_qualifiers, target,
3674                                       true, attributes);
3675   gcc_unreachable ();
3676 }
3677
3678 /* Create a new C++ parser.  */
3679
3680 static cp_parser *
3681 cp_parser_new (void)
3682 {
3683   cp_parser *parser;
3684   cp_lexer *lexer;
3685   unsigned i;
3686
3687   /* cp_lexer_new_main is called before doing GC allocation because
3688      cp_lexer_new_main might load a PCH file.  */
3689   lexer = cp_lexer_new_main ();
3690
3691   /* Initialize the binops_by_token so that we can get the tree
3692      directly from the token.  */
3693   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3694     binops_by_token[binops[i].token_type] = binops[i];
3695
3696   parser = ggc_cleared_alloc<cp_parser> ();
3697   parser->lexer = lexer;
3698   parser->context = cp_parser_context_new (NULL);
3699
3700   /* For now, we always accept GNU extensions.  */
3701   parser->allow_gnu_extensions_p = 1;
3702
3703   /* The `>' token is a greater-than operator, not the end of a
3704      template-id.  */
3705   parser->greater_than_is_operator_p = true;
3706
3707   parser->default_arg_ok_p = true;
3708
3709   /* We are not parsing a constant-expression.  */
3710   parser->integral_constant_expression_p = false;
3711   parser->allow_non_integral_constant_expression_p = false;
3712   parser->non_integral_constant_expression_p = false;
3713
3714   /* Local variable names are not forbidden.  */
3715   parser->local_variables_forbidden_p = false;
3716
3717   /* We are not processing an `extern "C"' declaration.  */
3718   parser->in_unbraced_linkage_specification_p = false;
3719
3720   /* We are not processing a declarator.  */
3721   parser->in_declarator_p = false;
3722
3723   /* We are not processing a template-argument-list.  */
3724   parser->in_template_argument_list_p = false;
3725
3726   /* We are not in an iteration statement.  */
3727   parser->in_statement = 0;
3728
3729   /* We are not in a switch statement.  */
3730   parser->in_switch_statement_p = false;
3731
3732   /* We are not parsing a type-id inside an expression.  */
3733   parser->in_type_id_in_expr_p = false;
3734
3735   /* Declarations aren't implicitly extern "C".  */
3736   parser->implicit_extern_c = false;
3737
3738   /* String literals should be translated to the execution character set.  */
3739   parser->translate_strings_p = true;
3740
3741   /* We are not parsing a function body.  */
3742   parser->in_function_body = false;
3743
3744   /* We can correct until told otherwise.  */
3745   parser->colon_corrects_to_scope_p = true;
3746
3747   /* The unparsed function queue is empty.  */
3748   push_unparsed_function_queues (parser);
3749
3750   /* There are no classes being defined.  */
3751   parser->num_classes_being_defined = 0;
3752
3753   /* No template parameters apply.  */
3754   parser->num_template_parameter_lists = 0;
3755
3756   /* Not declaring an implicit function template.  */
3757   parser->auto_is_implicit_function_template_parm_p = false;
3758   parser->fully_implicit_function_template_p = false;
3759   parser->implicit_template_parms = 0;
3760   parser->implicit_template_scope = 0;
3761
3762   /* Active OpenACC routine clauses.  */
3763   parser->oacc_routine = NULL;
3764
3765   /* Allow constrained-type-specifiers. */
3766   parser->prevent_constrained_type_specifiers = 0;
3767
3768   return parser;
3769 }
3770
3771 /* Create a cp_lexer structure which will emit the tokens in CACHE
3772    and push it onto the parser's lexer stack.  This is used for delayed
3773    parsing of in-class method bodies and default arguments, and should
3774    not be confused with tentative parsing.  */
3775 static void
3776 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3777 {
3778   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3779   lexer->next = parser->lexer;
3780   parser->lexer = lexer;
3781
3782   /* Move the current source position to that of the first token in the
3783      new lexer.  */
3784   cp_lexer_set_source_position_from_token (lexer->next_token);
3785 }
3786
3787 /* Pop the top lexer off the parser stack.  This is never used for the
3788    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3789 static void
3790 cp_parser_pop_lexer (cp_parser *parser)
3791 {
3792   cp_lexer *lexer = parser->lexer;
3793   parser->lexer = lexer->next;
3794   cp_lexer_destroy (lexer);
3795
3796   /* Put the current source position back where it was before this
3797      lexer was pushed.  */
3798   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3799 }
3800
3801 /* Lexical conventions [gram.lex]  */
3802
3803 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3804    identifier.  */
3805
3806 static cp_expr
3807 cp_parser_identifier (cp_parser* parser)
3808 {
3809   cp_token *token;
3810
3811   /* Look for the identifier.  */
3812   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3813   /* Return the value.  */
3814   if (token)
3815     return cp_expr (token->u.value, token->location);
3816   else
3817     return error_mark_node;
3818 }
3819
3820 /* Parse a sequence of adjacent string constants.  Returns a
3821    TREE_STRING representing the combined, nul-terminated string
3822    constant.  If TRANSLATE is true, translate the string to the
3823    execution character set.  If WIDE_OK is true, a wide string is
3824    invalid here.
3825
3826    C++98 [lex.string] says that if a narrow string literal token is
3827    adjacent to a wide string literal token, the behavior is undefined.
3828    However, C99 6.4.5p4 says that this results in a wide string literal.
3829    We follow C99 here, for consistency with the C front end.
3830
3831    This code is largely lifted from lex_string() in c-lex.c.
3832
3833    FUTURE: ObjC++ will need to handle @-strings here.  */
3834 static cp_expr
3835 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3836                           bool lookup_udlit = true)
3837 {
3838   tree value;
3839   size_t count;
3840   struct obstack str_ob;
3841   cpp_string str, istr, *strs;
3842   cp_token *tok;
3843   enum cpp_ttype type, curr_type;
3844   int have_suffix_p = 0;
3845   tree string_tree;
3846   tree suffix_id = NULL_TREE;
3847   bool curr_tok_is_userdef_p = false;
3848
3849   tok = cp_lexer_peek_token (parser->lexer);
3850   if (!cp_parser_is_string_literal (tok))
3851     {
3852       cp_parser_error (parser, "expected string-literal");
3853       return error_mark_node;
3854     }
3855
3856   location_t loc = tok->location;
3857
3858   if (cpp_userdef_string_p (tok->type))
3859     {
3860       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3861       curr_type = cpp_userdef_string_remove_type (tok->type);
3862       curr_tok_is_userdef_p = true;
3863     }
3864   else
3865     {
3866       string_tree = tok->u.value;
3867       curr_type = tok->type;
3868     }
3869   type = curr_type;
3870
3871   /* Try to avoid the overhead of creating and destroying an obstack
3872      for the common case of just one string.  */
3873   if (!cp_parser_is_string_literal
3874       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3875     {
3876       cp_lexer_consume_token (parser->lexer);
3877
3878       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3879       str.len = TREE_STRING_LENGTH (string_tree);
3880       count = 1;
3881
3882       if (curr_tok_is_userdef_p)
3883         {
3884           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3885           have_suffix_p = 1;
3886           curr_type = cpp_userdef_string_remove_type (tok->type);
3887         }
3888       else
3889         curr_type = tok->type;
3890
3891       strs = &str;
3892     }
3893   else
3894     {
3895       location_t last_tok_loc;
3896       gcc_obstack_init (&str_ob);
3897       count = 0;
3898
3899       do
3900         {
3901           last_tok_loc = tok->location;
3902           cp_lexer_consume_token (parser->lexer);
3903           count++;
3904           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3905           str.len = TREE_STRING_LENGTH (string_tree);
3906
3907           if (curr_tok_is_userdef_p)
3908             {
3909               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3910               if (have_suffix_p == 0)
3911                 {
3912                   suffix_id = curr_suffix_id;
3913                   have_suffix_p = 1;
3914                 }
3915               else if (have_suffix_p == 1
3916                        && curr_suffix_id != suffix_id)
3917                 {
3918                   error ("inconsistent user-defined literal suffixes"
3919                          " %qD and %qD in string literal",
3920                          suffix_id, curr_suffix_id);
3921                   have_suffix_p = -1;
3922                 }
3923               curr_type = cpp_userdef_string_remove_type (tok->type);
3924             }
3925           else
3926             curr_type = tok->type;
3927
3928           if (type != curr_type)
3929             {
3930               if (type == CPP_STRING)
3931                 type = curr_type;
3932               else if (curr_type != CPP_STRING)
3933                 error_at (tok->location,
3934                           "unsupported non-standard concatenation "
3935                           "of string literals");
3936             }
3937
3938           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3939
3940           tok = cp_lexer_peek_token (parser->lexer);
3941           if (cpp_userdef_string_p (tok->type))
3942             {
3943               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3944               curr_type = cpp_userdef_string_remove_type (tok->type);
3945               curr_tok_is_userdef_p = true;
3946             }
3947           else
3948             {
3949               string_tree = tok->u.value;
3950               curr_type = tok->type;
3951               curr_tok_is_userdef_p = false;
3952             }
3953         }
3954       while (cp_parser_is_string_literal (tok));
3955
3956       /* A string literal built by concatenation has its caret=start at
3957          the start of the initial string, and its finish at the finish of
3958          the final string literal.  */
3959       loc = make_location (loc, loc, get_finish (last_tok_loc));
3960
3961       strs = (cpp_string *) obstack_finish (&str_ob);
3962     }
3963
3964   if (type != CPP_STRING && !wide_ok)
3965     {
3966       cp_parser_error (parser, "a wide string is invalid in this context");
3967       type = CPP_STRING;
3968     }
3969
3970   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3971       (parse_in, strs, count, &istr, type))
3972     {
3973       value = build_string (istr.len, (const char *)istr.text);
3974       free (CONST_CAST (unsigned char *, istr.text));
3975
3976       switch (type)
3977         {
3978         default:
3979         case CPP_STRING:
3980         case CPP_UTF8STRING:
3981           TREE_TYPE (value) = char_array_type_node;
3982           break;
3983         case CPP_STRING16:
3984           TREE_TYPE (value) = char16_array_type_node;
3985           break;
3986         case CPP_STRING32:
3987           TREE_TYPE (value) = char32_array_type_node;
3988           break;
3989         case CPP_WSTRING:
3990           TREE_TYPE (value) = wchar_array_type_node;
3991           break;
3992         }
3993
3994       value = fix_string_type (value);
3995
3996       if (have_suffix_p)
3997         {
3998           tree literal = build_userdef_literal (suffix_id, value,
3999                                                 OT_NONE, NULL_TREE);
4000           if (lookup_udlit)
4001             value = cp_parser_userdef_string_literal (literal);
4002           else
4003             value = literal;
4004         }
4005     }
4006   else
4007     /* cpp_interpret_string has issued an error.  */
4008     value = error_mark_node;
4009
4010   if (count > 1)
4011     obstack_free (&str_ob, 0);
4012
4013   return cp_expr (value, loc);
4014 }
4015
4016 /* Look up a literal operator with the name and the exact arguments.  */
4017
4018 static tree
4019 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4020 {
4021   tree decl, fns;
4022   decl = lookup_name (name);
4023   if (!decl || !is_overloaded_fn (decl))
4024     return error_mark_node;
4025
4026   for (fns = decl; fns; fns = OVL_NEXT (fns))
4027     {
4028       unsigned int ix;
4029       bool found = true;
4030       tree fn = OVL_CURRENT (fns);
4031       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4032       if (parmtypes != NULL_TREE)
4033         {
4034           for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4035                ++ix, parmtypes = TREE_CHAIN (parmtypes))
4036             {
4037               tree tparm = TREE_VALUE (parmtypes);
4038               tree targ = TREE_TYPE ((*args)[ix]);
4039               bool ptr = TYPE_PTR_P (tparm);
4040               bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4041               if ((ptr || arr || !same_type_p (tparm, targ))
4042                   && (!ptr || !arr
4043                       || !same_type_p (TREE_TYPE (tparm),
4044                                        TREE_TYPE (targ))))
4045                 found = false;
4046             }
4047           if (found
4048               && ix == vec_safe_length (args)
4049               /* May be this should be sufficient_parms_p instead,
4050                  depending on how exactly should user-defined literals
4051                  work in presence of default arguments on the literal
4052                  operator parameters.  */
4053               && parmtypes == void_list_node)
4054             return decl;
4055         }
4056     }
4057
4058   return error_mark_node;
4059 }
4060
4061 /* Parse a user-defined char constant.  Returns a call to a user-defined
4062    literal operator taking the character as an argument.  */
4063
4064 static cp_expr
4065 cp_parser_userdef_char_literal (cp_parser *parser)
4066 {
4067   cp_token *token = cp_lexer_consume_token (parser->lexer);
4068   tree literal = token->u.value;
4069   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4070   tree value = USERDEF_LITERAL_VALUE (literal);
4071   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4072   tree decl, result;
4073
4074   /* Build up a call to the user-defined operator  */
4075   /* Lookup the name we got back from the id-expression.  */
4076   vec<tree, va_gc> *args = make_tree_vector ();
4077   vec_safe_push (args, value);
4078   decl = lookup_literal_operator (name, args);
4079   if (!decl || decl == error_mark_node)
4080     {
4081       error ("unable to find character literal operator %qD with %qT argument",
4082              name, TREE_TYPE (value));
4083       release_tree_vector (args);
4084       return error_mark_node;
4085     }
4086   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4087   release_tree_vector (args);
4088   return result;
4089 }
4090
4091 /* A subroutine of cp_parser_userdef_numeric_literal to
4092    create a char... template parameter pack from a string node.  */
4093
4094 static tree
4095 make_char_string_pack (tree value)
4096 {
4097   tree charvec;
4098   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4099   const char *str = TREE_STRING_POINTER (value);
4100   int i, len = TREE_STRING_LENGTH (value) - 1;
4101   tree argvec = make_tree_vec (1);
4102
4103   /* Fill in CHARVEC with all of the parameters.  */
4104   charvec = make_tree_vec (len);
4105   for (i = 0; i < len; ++i)
4106     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4107
4108   /* Build the argument packs.  */
4109   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4110   TREE_TYPE (argpack) = char_type_node;
4111
4112   TREE_VEC_ELT (argvec, 0) = argpack;
4113
4114   return argvec;
4115 }
4116
4117 /* A subroutine of cp_parser_userdef_numeric_literal to
4118    create a char... template parameter pack from a string node.  */
4119
4120 static tree
4121 make_string_pack (tree value)
4122 {
4123   tree charvec;
4124   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4125   const unsigned char *str
4126     = (const unsigned char *) TREE_STRING_POINTER (value);
4127   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4128   int len = TREE_STRING_LENGTH (value) / sz - 1;
4129   tree argvec = make_tree_vec (2);
4130
4131   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4132   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4133
4134   /* First template parm is character type.  */
4135   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4136
4137   /* Fill in CHARVEC with all of the parameters.  */
4138   charvec = make_tree_vec (len);
4139   for (int i = 0; i < len; ++i)
4140     TREE_VEC_ELT (charvec, i)
4141       = double_int_to_tree (str_char_type_node,
4142                             double_int::from_buffer (str + i * sz, sz));
4143
4144   /* Build the argument packs.  */
4145   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4146   TREE_TYPE (argpack) = str_char_type_node;
4147
4148   TREE_VEC_ELT (argvec, 1) = argpack;
4149
4150   return argvec;
4151 }
4152
4153 /* Parse a user-defined numeric constant.  returns a call to a user-defined
4154    literal operator.  */
4155
4156 static cp_expr
4157 cp_parser_userdef_numeric_literal (cp_parser *parser)
4158 {
4159   cp_token *token = cp_lexer_consume_token (parser->lexer);
4160   tree literal = token->u.value;
4161   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4162   tree value = USERDEF_LITERAL_VALUE (literal);
4163   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4164   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4165   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4166   tree decl, result;
4167   vec<tree, va_gc> *args;
4168
4169   /* Look for a literal operator taking the exact type of numeric argument
4170      as the literal value.  */
4171   args = make_tree_vector ();
4172   vec_safe_push (args, value);
4173   decl = lookup_literal_operator (name, args);
4174   if (decl && decl != error_mark_node)
4175     {
4176       result = finish_call_expr (decl, &args, false, true,
4177                                  tf_warning_or_error);
4178
4179       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4180         {
4181           warning_at (token->location, OPT_Woverflow,
4182                       "integer literal exceeds range of %qT type",
4183                       long_long_unsigned_type_node);
4184         }
4185       else
4186         {
4187           if (overflow > 0)
4188             warning_at (token->location, OPT_Woverflow,
4189                         "floating literal exceeds range of %qT type",
4190                         long_double_type_node);
4191           else if (overflow < 0)
4192             warning_at (token->location, OPT_Woverflow,
4193                         "floating literal truncated to zero");
4194         }
4195
4196       release_tree_vector (args);
4197       return result;
4198     }
4199   release_tree_vector (args);
4200
4201   /* If the numeric argument didn't work, look for a raw literal
4202      operator taking a const char* argument consisting of the number
4203      in string format.  */
4204   args = make_tree_vector ();
4205   vec_safe_push (args, num_string);
4206   decl = lookup_literal_operator (name, args);
4207   if (decl && decl != error_mark_node)
4208     {
4209       result = finish_call_expr (decl, &args, false, true,
4210                                  tf_warning_or_error);
4211       release_tree_vector (args);
4212       return result;
4213     }
4214   release_tree_vector (args);
4215
4216   /* If the raw literal didn't work, look for a non-type template
4217      function with parameter pack char....  Call the function with
4218      template parameter characters representing the number.  */
4219   args = make_tree_vector ();
4220   decl = lookup_literal_operator (name, args);
4221   if (decl && decl != error_mark_node)
4222     {
4223       tree tmpl_args = make_char_string_pack (num_string);
4224       decl = lookup_template_function (decl, tmpl_args);
4225       result = finish_call_expr (decl, &args, false, true,
4226                                  tf_warning_or_error);
4227       release_tree_vector (args);
4228       return result;
4229     }
4230
4231   release_tree_vector (args);
4232
4233   error ("unable to find numeric literal operator %qD", name);
4234   if (!cpp_get_options (parse_in)->ext_numeric_literals)
4235     inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4236             "to enable more built-in suffixes");
4237   return error_mark_node;
4238 }
4239
4240 /* Parse a user-defined string constant.  Returns a call to a user-defined
4241    literal operator taking a character pointer and the length of the string
4242    as arguments.  */
4243
4244 static tree
4245 cp_parser_userdef_string_literal (tree literal)
4246 {
4247   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4248   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4249   tree value = USERDEF_LITERAL_VALUE (literal);
4250   int len = TREE_STRING_LENGTH (value)
4251         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4252   tree decl, result;
4253   vec<tree, va_gc> *args;
4254
4255   /* Build up a call to the user-defined operator.  */
4256   /* Lookup the name we got back from the id-expression.  */
4257   args = make_tree_vector ();
4258   vec_safe_push (args, value);
4259   vec_safe_push (args, build_int_cst (size_type_node, len));
4260   decl = lookup_literal_operator (name, args);
4261
4262   if (decl && decl != error_mark_node)
4263     {
4264       result = finish_call_expr (decl, &args, false, true,
4265                                  tf_warning_or_error);
4266       release_tree_vector (args);
4267       return result;
4268     }
4269   release_tree_vector (args);
4270
4271   /* Look for a template function with typename parameter CharT
4272      and parameter pack CharT...  Call the function with
4273      template parameter characters representing the string.  */
4274   args = make_tree_vector ();
4275   decl = lookup_literal_operator (name, args);
4276   if (decl && decl != error_mark_node)
4277     {
4278       tree tmpl_args = make_string_pack (value);
4279       decl = lookup_template_function (decl, tmpl_args);
4280       result = finish_call_expr (decl, &args, false, true,
4281                                  tf_warning_or_error);
4282       release_tree_vector (args);
4283       return result;
4284     }
4285   release_tree_vector (args);
4286
4287   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4288          name, TREE_TYPE (value), size_type_node);
4289   return error_mark_node;
4290 }
4291
4292
4293 /* Basic concepts [gram.basic]  */
4294
4295 /* Parse a translation-unit.
4296
4297    translation-unit:
4298      declaration-seq [opt]
4299
4300    Returns TRUE if all went well.  */
4301
4302 static bool
4303 cp_parser_translation_unit (cp_parser* parser)
4304 {
4305   /* The address of the first non-permanent object on the declarator
4306      obstack.  */
4307   static void *declarator_obstack_base;
4308
4309   bool success;
4310
4311   /* Create the declarator obstack, if necessary.  */
4312   if (!cp_error_declarator)
4313     {
4314       gcc_obstack_init (&declarator_obstack);
4315       /* Create the error declarator.  */
4316       cp_error_declarator = make_declarator (cdk_error);
4317       /* Create the empty parameter list.  */
4318       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4319       /* Remember where the base of the declarator obstack lies.  */
4320       declarator_obstack_base = obstack_next_free (&declarator_obstack);
4321     }
4322
4323   cp_parser_declaration_seq_opt (parser);
4324
4325   /* If there are no tokens left then all went well.  */
4326   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4327     {
4328       /* Get rid of the token array; we don't need it any more.  */
4329       cp_lexer_destroy (parser->lexer);
4330       parser->lexer = NULL;
4331
4332       /* This file might have been a context that's implicitly extern
4333          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
4334       if (parser->implicit_extern_c)
4335         {
4336           pop_lang_context ();
4337           parser->implicit_extern_c = false;
4338         }
4339
4340       /* Finish up.  */
4341       finish_translation_unit ();
4342
4343       success = true;
4344     }
4345   else
4346     {
4347       cp_parser_error (parser, "expected declaration");
4348       success = false;
4349     }
4350
4351   /* Make sure the declarator obstack was fully cleaned up.  */
4352   gcc_assert (obstack_next_free (&declarator_obstack)
4353               == declarator_obstack_base);
4354
4355   /* All went well.  */
4356   return success;
4357 }
4358
4359 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4360    decltype context.  */
4361
4362 static inline tsubst_flags_t
4363 complain_flags (bool decltype_p)
4364 {
4365   tsubst_flags_t complain = tf_warning_or_error;
4366   if (decltype_p)
4367     complain |= tf_decltype;
4368   return complain;
4369 }
4370
4371 /* We're about to parse a collection of statements.  If we're currently
4372    parsing tentatively, set up a firewall so that any nested
4373    cp_parser_commit_to_tentative_parse won't affect the current context.  */
4374
4375 static cp_token_position
4376 cp_parser_start_tentative_firewall (cp_parser *parser)
4377 {
4378   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4379     return 0;
4380
4381   cp_parser_parse_tentatively (parser);
4382   cp_parser_commit_to_topmost_tentative_parse (parser);
4383   return cp_lexer_token_position (parser->lexer, false);
4384 }
4385
4386 /* We've finished parsing the collection of statements.  Wrap up the
4387    firewall and replace the relevant tokens with the parsed form.  */
4388
4389 static void
4390 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4391                                   tree expr)
4392 {
4393   if (!start)
4394     return;
4395
4396   /* Finish the firewall level.  */
4397   cp_parser_parse_definitely (parser);
4398   /* And remember the result of the parse for when we try again.  */
4399   cp_token *token = cp_lexer_token_at (parser->lexer, start);
4400   token->type = CPP_PREPARSED_EXPR;
4401   token->u.value = expr;
4402   token->keyword = RID_MAX;
4403   cp_lexer_purge_tokens_after (parser->lexer, start);
4404 }
4405
4406 /* Like the above functions, but let the user modify the tokens.  Used by
4407    CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4408    later parses, so it makes sense to localize the effects of
4409    cp_parser_commit_to_tentative_parse.  */
4410
4411 struct tentative_firewall
4412 {
4413   cp_parser *parser;
4414   bool set;
4415
4416   tentative_firewall (cp_parser *p): parser(p)
4417   {
4418     /* If we're currently parsing tentatively, start a committed level as a
4419        firewall and then an inner tentative parse.  */
4420     if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4421       {
4422         cp_parser_parse_tentatively (parser);
4423         cp_parser_commit_to_topmost_tentative_parse (parser);
4424         cp_parser_parse_tentatively (parser);
4425       }
4426   }
4427
4428   ~tentative_firewall()
4429   {
4430     if (set)
4431       {
4432         /* Finish the inner tentative parse and the firewall, propagating any
4433            uncommitted error state to the outer tentative parse.  */
4434         bool err = cp_parser_error_occurred (parser);
4435         cp_parser_parse_definitely (parser);
4436         cp_parser_parse_definitely (parser);
4437         if (err)
4438           cp_parser_simulate_error (parser);
4439       }
4440   }
4441 };
4442
4443 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4444    enclosing parentheses.  */
4445
4446 static cp_expr
4447 cp_parser_statement_expr (cp_parser *parser)
4448 {
4449   cp_token_position start = cp_parser_start_tentative_firewall (parser);
4450
4451   /* Consume the '('.  */
4452   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4453   cp_lexer_consume_token (parser->lexer);
4454   /* Start the statement-expression.  */
4455   tree expr = begin_stmt_expr ();
4456   /* Parse the compound-statement.  */
4457   cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4458   /* Finish up.  */
4459   expr = finish_stmt_expr (expr, false);
4460   /* Consume the ')'.  */
4461   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4462   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4463     cp_parser_skip_to_end_of_statement (parser);
4464
4465   cp_parser_end_tentative_firewall (parser, start, expr);
4466   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4467   return cp_expr (expr, combined_loc);
4468 }
4469
4470 /* Expressions [gram.expr] */
4471
4472 /* Parse a fold-operator.
4473
4474     fold-operator:
4475         -  *  /  %  ^  &  |  =  <  >  <<  >>
4476       =  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=
4477       ==  !=  <=  >=  &&  ||  ,  .*  ->*
4478
4479    This returns the tree code corresponding to the matched operator
4480    as an int. When the current token matches a compound assignment
4481    opertor, the resulting tree code is the negative value of the
4482    non-assignment operator. */
4483
4484 static int
4485 cp_parser_fold_operator (cp_token *token)
4486 {
4487   switch (token->type)
4488     {
4489     case CPP_PLUS: return PLUS_EXPR;
4490     case CPP_MINUS: return MINUS_EXPR;
4491     case CPP_MULT: return MULT_EXPR;
4492     case CPP_DIV: return TRUNC_DIV_EXPR;
4493     case CPP_MOD: return TRUNC_MOD_EXPR;
4494     case CPP_XOR: return BIT_XOR_EXPR;
4495     case CPP_AND: return BIT_AND_EXPR;
4496     case CPP_OR: return BIT_IOR_EXPR;
4497     case CPP_LSHIFT: return LSHIFT_EXPR;
4498     case CPP_RSHIFT: return RSHIFT_EXPR;
4499
4500     case CPP_EQ: return -NOP_EXPR;
4501     case CPP_PLUS_EQ: return -PLUS_EXPR;
4502     case CPP_MINUS_EQ: return -MINUS_EXPR;
4503     case CPP_MULT_EQ: return -MULT_EXPR;
4504     case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4505     case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4506     case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4507     case CPP_AND_EQ: return -BIT_AND_EXPR;
4508     case CPP_OR_EQ: return -BIT_IOR_EXPR;
4509     case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4510     case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4511
4512     case CPP_EQ_EQ: return EQ_EXPR;
4513     case CPP_NOT_EQ: return NE_EXPR;
4514     case CPP_LESS: return LT_EXPR;
4515     case CPP_GREATER: return GT_EXPR;
4516     case CPP_LESS_EQ: return LE_EXPR;
4517     case CPP_GREATER_EQ: return GE_EXPR;
4518
4519     case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4520     case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4521
4522     case CPP_COMMA: return COMPOUND_EXPR;
4523
4524     case CPP_DOT_STAR: return DOTSTAR_EXPR;
4525     case CPP_DEREF_STAR: return MEMBER_REF;
4526
4527     default: return ERROR_MARK;
4528     }
4529 }
4530
4531 /* Returns true if CODE indicates a binary expression, which is not allowed in
4532    the LHS of a fold-expression.  More codes will need to be added to use this
4533    function in other contexts.  */
4534
4535 static bool
4536 is_binary_op (tree_code code)
4537 {
4538   switch (code)
4539     {
4540     case PLUS_EXPR:
4541     case POINTER_PLUS_EXPR:
4542     case MINUS_EXPR:
4543     case MULT_EXPR:
4544     case TRUNC_DIV_EXPR:
4545     case TRUNC_MOD_EXPR:
4546     case BIT_XOR_EXPR:
4547     case BIT_AND_EXPR:
4548     case BIT_IOR_EXPR:
4549     case LSHIFT_EXPR:
4550     case RSHIFT_EXPR:
4551
4552     case MODOP_EXPR:
4553
4554     case EQ_EXPR:
4555     case NE_EXPR:
4556     case LE_EXPR:
4557     case GE_EXPR:
4558     case LT_EXPR:
4559     case GT_EXPR:
4560
4561     case TRUTH_ANDIF_EXPR:
4562     case TRUTH_ORIF_EXPR:
4563
4564     case COMPOUND_EXPR:
4565
4566     case DOTSTAR_EXPR:
4567     case MEMBER_REF:
4568       return true;
4569
4570     default:
4571       return false;
4572     }
4573 }
4574
4575 /* If the next token is a suitable fold operator, consume it and return as
4576    the function above.  */
4577
4578 static int
4579 cp_parser_fold_operator (cp_parser *parser)
4580 {
4581   cp_token* token = cp_lexer_peek_token (parser->lexer);
4582   int code = cp_parser_fold_operator (token);
4583   if (code != ERROR_MARK)
4584     cp_lexer_consume_token (parser->lexer);
4585   return code;
4586 }
4587
4588 /* Parse a fold-expression.
4589
4590      fold-expression:
4591        ( ... folding-operator cast-expression)
4592        ( cast-expression folding-operator ... )
4593        ( cast-expression folding operator ... folding-operator cast-expression)
4594
4595    Note that the '(' and ')' are matched in primary expression. */
4596
4597 static cp_expr
4598 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4599 {
4600   cp_id_kind pidk;
4601
4602   // Left fold.
4603   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4604     {
4605       cp_lexer_consume_token (parser->lexer);
4606       int op = cp_parser_fold_operator (parser);
4607       if (op == ERROR_MARK)
4608         {
4609           cp_parser_error (parser, "expected binary operator");
4610           return error_mark_node;
4611         }
4612
4613       tree expr = cp_parser_cast_expression (parser, false, false,
4614                                              false, &pidk);
4615       if (expr == error_mark_node)
4616         return error_mark_node;
4617       return finish_left_unary_fold_expr (expr, op);
4618     }
4619
4620   const cp_token* token = cp_lexer_peek_token (parser->lexer);
4621   int op = cp_parser_fold_operator (parser);
4622   if (op == ERROR_MARK)
4623     {
4624       cp_parser_error (parser, "expected binary operator");
4625       return error_mark_node;
4626     }
4627
4628   if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4629     {
4630       cp_parser_error (parser, "expected ...");
4631       return error_mark_node;
4632     }
4633   cp_lexer_consume_token (parser->lexer);
4634
4635   /* The operands of a fold-expression are cast-expressions, so binary or
4636      conditional expressions are not allowed.  We check this here to avoid
4637      tentative parsing.  */
4638   if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4639     /* OK, the expression was parenthesized.  */;
4640   else if (is_binary_op (TREE_CODE (expr1)))
4641     error_at (location_of (expr1),
4642               "binary expression in operand of fold-expression");
4643   else if (TREE_CODE (expr1) == COND_EXPR)
4644     error_at (location_of (expr1),
4645               "conditional expression in operand of fold-expression");
4646
4647   // Right fold.
4648   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4649     return finish_right_unary_fold_expr (expr1, op);
4650
4651   if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4652     {
4653       cp_parser_error (parser, "mismatched operator in fold-expression");
4654       return error_mark_node;
4655     }
4656   cp_lexer_consume_token (parser->lexer);
4657
4658   // Binary left or right fold.
4659   tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4660   if (expr2 == error_mark_node)
4661     return error_mark_node;
4662   return finish_binary_fold_expr (expr1, expr2, op);
4663 }
4664
4665 /* Parse a primary-expression.
4666
4667    primary-expression:
4668      literal
4669      this
4670      ( expression )
4671      id-expression
4672      lambda-expression (C++11)
4673
4674    GNU Extensions:
4675
4676    primary-expression:
4677      ( compound-statement )
4678      __builtin_va_arg ( assignment-expression , type-id )
4679      __builtin_offsetof ( type-id , offsetof-expression )
4680
4681    C++ Extensions:
4682      __has_nothrow_assign ( type-id )   
4683      __has_nothrow_constructor ( type-id )
4684      __has_nothrow_copy ( type-id )
4685      __has_trivial_assign ( type-id )   
4686      __has_trivial_constructor ( type-id )
4687      __has_trivial_copy ( type-id )
4688      __has_trivial_destructor ( type-id )
4689      __has_virtual_destructor ( type-id )     
4690      __is_abstract ( type-id )
4691      __is_base_of ( type-id , type-id )
4692      __is_class ( type-id )
4693      __is_empty ( type-id )
4694      __is_enum ( type-id )
4695      __is_final ( type-id )
4696      __is_literal_type ( type-id )
4697      __is_pod ( type-id )
4698      __is_polymorphic ( type-id )
4699      __is_std_layout ( type-id )
4700      __is_trivial ( type-id )
4701      __is_union ( type-id )
4702
4703    Objective-C++ Extension:
4704
4705    primary-expression:
4706      objc-expression
4707
4708    literal:
4709      __null
4710
4711    ADDRESS_P is true iff this expression was immediately preceded by
4712    "&" and therefore might denote a pointer-to-member.  CAST_P is true
4713    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
4714    true iff this expression is a template argument.
4715
4716    Returns a representation of the expression.  Upon return, *IDK
4717    indicates what kind of id-expression (if any) was present.  */
4718
4719 static cp_expr
4720 cp_parser_primary_expression (cp_parser *parser,
4721                               bool address_p,
4722                               bool cast_p,
4723                               bool template_arg_p,
4724                               bool decltype_p,
4725                               cp_id_kind *idk)
4726 {
4727   cp_token *token = NULL;
4728
4729   /* Assume the primary expression is not an id-expression.  */
4730   *idk = CP_ID_KIND_NONE;
4731
4732   /* Peek at the next token.  */
4733   token = cp_lexer_peek_token (parser->lexer);
4734   switch ((int) token->type)
4735     {
4736       /* literal:
4737            integer-literal
4738            character-literal
4739            floating-literal
4740            string-literal
4741            boolean-literal
4742            pointer-literal
4743            user-defined-literal  */
4744     case CPP_CHAR:
4745     case CPP_CHAR16:
4746     case CPP_CHAR32:
4747     case CPP_WCHAR:
4748     case CPP_UTF8CHAR:
4749     case CPP_NUMBER:
4750     case CPP_PREPARSED_EXPR:
4751       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4752         return cp_parser_userdef_numeric_literal (parser);
4753       token = cp_lexer_consume_token (parser->lexer);
4754       if (TREE_CODE (token->u.value) == FIXED_CST)
4755         {
4756           error_at (token->location,
4757                     "fixed-point types not supported in C++");
4758           return error_mark_node;
4759         }
4760       /* Floating-point literals are only allowed in an integral
4761          constant expression if they are cast to an integral or
4762          enumeration type.  */
4763       if (TREE_CODE (token->u.value) == REAL_CST
4764           && parser->integral_constant_expression_p
4765           && pedantic)
4766         {
4767           /* CAST_P will be set even in invalid code like "int(2.7 +
4768              ...)".   Therefore, we have to check that the next token
4769              is sure to end the cast.  */
4770           if (cast_p)
4771             {
4772               cp_token *next_token;
4773
4774               next_token = cp_lexer_peek_token (parser->lexer);
4775               if (/* The comma at the end of an
4776                      enumerator-definition.  */
4777                   next_token->type != CPP_COMMA
4778                   /* The curly brace at the end of an enum-specifier.  */
4779                   && next_token->type != CPP_CLOSE_BRACE
4780                   /* The end of a statement.  */
4781                   && next_token->type != CPP_SEMICOLON
4782                   /* The end of the cast-expression.  */
4783                   && next_token->type != CPP_CLOSE_PAREN
4784                   /* The end of an array bound.  */
4785                   && next_token->type != CPP_CLOSE_SQUARE
4786                   /* The closing ">" in a template-argument-list.  */
4787                   && (next_token->type != CPP_GREATER
4788                       || parser->greater_than_is_operator_p)
4789                   /* C++0x only: A ">>" treated like two ">" tokens,
4790                      in a template-argument-list.  */
4791                   && (next_token->type != CPP_RSHIFT
4792                       || (cxx_dialect == cxx98)
4793                       || parser->greater_than_is_operator_p))
4794                 cast_p = false;
4795             }
4796
4797           /* If we are within a cast, then the constraint that the
4798              cast is to an integral or enumeration type will be
4799              checked at that point.  If we are not within a cast, then
4800              this code is invalid.  */
4801           if (!cast_p)
4802             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4803         }
4804       return cp_expr (token->u.value, token->location);
4805
4806     case CPP_CHAR_USERDEF:
4807     case CPP_CHAR16_USERDEF:
4808     case CPP_CHAR32_USERDEF:
4809     case CPP_WCHAR_USERDEF:
4810     case CPP_UTF8CHAR_USERDEF:
4811       return cp_parser_userdef_char_literal (parser);
4812
4813     case CPP_STRING:
4814     case CPP_STRING16:
4815     case CPP_STRING32:
4816     case CPP_WSTRING:
4817     case CPP_UTF8STRING:
4818     case CPP_STRING_USERDEF:
4819     case CPP_STRING16_USERDEF:
4820     case CPP_STRING32_USERDEF:
4821     case CPP_WSTRING_USERDEF:
4822     case CPP_UTF8STRING_USERDEF:
4823       /* ??? Should wide strings be allowed when parser->translate_strings_p
4824          is false (i.e. in attributes)?  If not, we can kill the third
4825          argument to cp_parser_string_literal.  */
4826       return cp_parser_string_literal (parser,
4827                                        parser->translate_strings_p,
4828                                        true);
4829
4830     case CPP_OPEN_PAREN:
4831       /* If we see `( { ' then we are looking at the beginning of
4832          a GNU statement-expression.  */
4833       if (cp_parser_allow_gnu_extensions_p (parser)
4834           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4835         {
4836           /* Statement-expressions are not allowed by the standard.  */
4837           pedwarn (token->location, OPT_Wpedantic,
4838                    "ISO C++ forbids braced-groups within expressions");
4839
4840           /* And they're not allowed outside of a function-body; you
4841              cannot, for example, write:
4842
4843              int i = ({ int j = 3; j + 1; });
4844
4845              at class or namespace scope.  */
4846           if (!parser->in_function_body
4847               || parser->in_template_argument_list_p)
4848             {
4849               error_at (token->location,
4850                         "statement-expressions are not allowed outside "
4851                         "functions nor in template-argument lists");
4852               cp_parser_skip_to_end_of_block_or_statement (parser);
4853               if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4854                 cp_lexer_consume_token (parser->lexer);
4855               return error_mark_node;
4856             }
4857           else
4858             return cp_parser_statement_expr (parser);
4859         }
4860       /* Otherwise it's a normal parenthesized expression.  */
4861       {
4862         cp_expr expr;
4863         bool saved_greater_than_is_operator_p;
4864
4865         location_t open_paren_loc = token->location;
4866
4867         /* Consume the `('.  */
4868         cp_lexer_consume_token (parser->lexer);
4869         /* Within a parenthesized expression, a `>' token is always
4870            the greater-than operator.  */
4871         saved_greater_than_is_operator_p
4872           = parser->greater_than_is_operator_p;
4873         parser->greater_than_is_operator_p = true;
4874
4875         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4876           /* Left fold expression. */
4877           expr = NULL_TREE;
4878         else
4879           /* Parse the parenthesized expression.  */
4880           expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4881
4882         token = cp_lexer_peek_token (parser->lexer);
4883         if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4884           {
4885             expr = cp_parser_fold_expression (parser, expr);
4886             if (expr != error_mark_node
4887                 && cxx_dialect < cxx1z
4888                 && !in_system_header_at (input_location))
4889               pedwarn (input_location, 0, "fold-expressions only available "
4890                        "with -std=c++1z or -std=gnu++1z");
4891           }
4892         else
4893           /* Let the front end know that this expression was
4894              enclosed in parentheses. This matters in case, for
4895              example, the expression is of the form `A::B', since
4896              `&A::B' might be a pointer-to-member, but `&(A::B)' is
4897              not.  */
4898           expr = finish_parenthesized_expr (expr);
4899
4900         /* DR 705: Wrapping an unqualified name in parentheses
4901            suppresses arg-dependent lookup.  We want to pass back
4902            CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4903            (c++/37862), but none of the others.  */
4904         if (*idk != CP_ID_KIND_QUALIFIED)
4905           *idk = CP_ID_KIND_NONE;
4906
4907         /* The `>' token might be the end of a template-id or
4908            template-parameter-list now.  */
4909         parser->greater_than_is_operator_p
4910           = saved_greater_than_is_operator_p;
4911
4912         /* Consume the `)'.  */
4913         token = cp_lexer_peek_token (parser->lexer);
4914         location_t close_paren_loc = token->location;
4915         expr.set_range (open_paren_loc, close_paren_loc);
4916         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4917             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4918           cp_parser_skip_to_end_of_statement (parser);
4919
4920         return expr;
4921       }
4922
4923     case CPP_OPEN_SQUARE:
4924       {
4925         if (c_dialect_objc ())
4926           {
4927             /* We might have an Objective-C++ message. */
4928             cp_parser_parse_tentatively (parser);
4929             tree msg = cp_parser_objc_message_expression (parser);
4930             /* If that works out, we're done ... */
4931             if (cp_parser_parse_definitely (parser))
4932               return msg;
4933             /* ... else, fall though to see if it's a lambda.  */
4934           }
4935         cp_expr lam = cp_parser_lambda_expression (parser);
4936         /* Don't warn about a failed tentative parse.  */
4937         if (cp_parser_error_occurred (parser))
4938           return error_mark_node;
4939         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4940         return lam;
4941       }
4942
4943     case CPP_OBJC_STRING:
4944       if (c_dialect_objc ())
4945         /* We have an Objective-C++ string literal. */
4946         return cp_parser_objc_expression (parser);
4947       cp_parser_error (parser, "expected primary-expression");
4948       return error_mark_node;
4949
4950     case CPP_KEYWORD:
4951       switch (token->keyword)
4952         {
4953           /* These two are the boolean literals.  */
4954         case RID_TRUE:
4955           cp_lexer_consume_token (parser->lexer);
4956           return cp_expr (boolean_true_node, token->location);
4957         case RID_FALSE:
4958           cp_lexer_consume_token (parser->lexer);
4959           return cp_expr (boolean_false_node, token->location);
4960
4961           /* The `__null' literal.  */
4962         case RID_NULL:
4963           cp_lexer_consume_token (parser->lexer);
4964           return cp_expr (null_node, token->location);
4965
4966           /* The `nullptr' literal.  */
4967         case RID_NULLPTR:
4968           cp_lexer_consume_token (parser->lexer);
4969           return cp_expr (nullptr_node, token->location);
4970
4971           /* Recognize the `this' keyword.  */
4972         case RID_THIS:
4973           cp_lexer_consume_token (parser->lexer);
4974           if (parser->local_variables_forbidden_p)
4975             {
4976               error_at (token->location,
4977                         "%<this%> may not be used in this context");
4978               return error_mark_node;
4979             }
4980           /* Pointers cannot appear in constant-expressions.  */
4981           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4982             return error_mark_node;
4983           return cp_expr (finish_this_expr (), token->location);
4984
4985           /* The `operator' keyword can be the beginning of an
4986              id-expression.  */
4987         case RID_OPERATOR:
4988           goto id_expression;
4989
4990         case RID_FUNCTION_NAME:
4991         case RID_PRETTY_FUNCTION_NAME:
4992         case RID_C99_FUNCTION_NAME:
4993           {
4994             non_integral_constant name;
4995
4996             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4997                __func__ are the names of variables -- but they are
4998                treated specially.  Therefore, they are handled here,
4999                rather than relying on the generic id-expression logic
5000                below.  Grammatically, these names are id-expressions.
5001
5002                Consume the token.  */
5003             token = cp_lexer_consume_token (parser->lexer);
5004
5005             switch (token->keyword)
5006               {
5007               case RID_FUNCTION_NAME:
5008                 name = NIC_FUNC_NAME;
5009                 break;
5010               case RID_PRETTY_FUNCTION_NAME:
5011                 name = NIC_PRETTY_FUNC;
5012                 break;
5013               case RID_C99_FUNCTION_NAME:
5014                 name = NIC_C99_FUNC;
5015                 break;
5016               default:
5017                 gcc_unreachable ();
5018               }
5019
5020             if (cp_parser_non_integral_constant_expression (parser, name))
5021               return error_mark_node;
5022
5023             /* Look up the name.  */
5024             return finish_fname (token->u.value);
5025           }
5026
5027         case RID_VA_ARG:
5028           {
5029             tree expression;
5030             tree type;
5031             source_location type_location;
5032             location_t start_loc
5033               = cp_lexer_peek_token (parser->lexer)->location;
5034             /* The `__builtin_va_arg' construct is used to handle
5035                `va_arg'.  Consume the `__builtin_va_arg' token.  */
5036             cp_lexer_consume_token (parser->lexer);
5037             /* Look for the opening `('.  */
5038             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5039             /* Now, parse the assignment-expression.  */
5040             expression = cp_parser_assignment_expression (parser);
5041             /* Look for the `,'.  */
5042             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5043             type_location = cp_lexer_peek_token (parser->lexer)->location;
5044             /* Parse the type-id.  */
5045             {
5046               type_id_in_expr_sentinel s (parser);
5047               type = cp_parser_type_id (parser);
5048             }
5049             /* Look for the closing `)'.  */
5050             location_t finish_loc
5051               = cp_lexer_peek_token (parser->lexer)->location;
5052             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5053             /* Using `va_arg' in a constant-expression is not
5054                allowed.  */
5055             if (cp_parser_non_integral_constant_expression (parser,
5056                                                             NIC_VA_ARG))
5057               return error_mark_node;
5058             /* Construct a location of the form:
5059                  __builtin_va_arg (v, int)
5060                  ~~~~~~~~~~~~~~~~~~~~~^~~~
5061                with the caret at the type, ranging from the start of the
5062                "__builtin_va_arg" token to the close paren.  */
5063             location_t combined_loc
5064               = make_location (type_location, start_loc, finish_loc);
5065             return build_x_va_arg (combined_loc, expression, type);
5066           }
5067
5068         case RID_OFFSETOF:
5069           return cp_parser_builtin_offsetof (parser);
5070
5071         case RID_HAS_NOTHROW_ASSIGN:
5072         case RID_HAS_NOTHROW_CONSTRUCTOR:
5073         case RID_HAS_NOTHROW_COPY:        
5074         case RID_HAS_TRIVIAL_ASSIGN:
5075         case RID_HAS_TRIVIAL_CONSTRUCTOR:
5076         case RID_HAS_TRIVIAL_COPY:        
5077         case RID_HAS_TRIVIAL_DESTRUCTOR:
5078         case RID_HAS_VIRTUAL_DESTRUCTOR:
5079         case RID_IS_ABSTRACT:
5080         case RID_IS_BASE_OF:
5081         case RID_IS_CLASS:
5082         case RID_IS_EMPTY:
5083         case RID_IS_ENUM:
5084         case RID_IS_FINAL:
5085         case RID_IS_LITERAL_TYPE:
5086         case RID_IS_POD:
5087         case RID_IS_POLYMORPHIC:
5088         case RID_IS_SAME_AS:
5089         case RID_IS_STD_LAYOUT:
5090         case RID_IS_TRIVIAL:
5091         case RID_IS_TRIVIALLY_ASSIGNABLE:
5092         case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5093         case RID_IS_TRIVIALLY_COPYABLE:
5094         case RID_IS_UNION:
5095           return cp_parser_trait_expr (parser, token->keyword);
5096
5097         // C++ concepts
5098         case RID_REQUIRES:
5099           return cp_parser_requires_expression (parser);
5100
5101         /* Objective-C++ expressions.  */
5102         case RID_AT_ENCODE:
5103         case RID_AT_PROTOCOL:
5104         case RID_AT_SELECTOR:
5105           return cp_parser_objc_expression (parser);
5106
5107         case RID_TEMPLATE:
5108           if (parser->in_function_body
5109               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5110                   == CPP_LESS))
5111             {
5112               error_at (token->location,
5113                         "a template declaration cannot appear at block scope");
5114               cp_parser_skip_to_end_of_block_or_statement (parser);
5115               return error_mark_node;
5116             }
5117         default:
5118           cp_parser_error (parser, "expected primary-expression");
5119           return error_mark_node;
5120         }
5121
5122       /* An id-expression can start with either an identifier, a
5123          `::' as the beginning of a qualified-id, or the "operator"
5124          keyword.  */
5125     case CPP_NAME:
5126     case CPP_SCOPE:
5127     case CPP_TEMPLATE_ID:
5128     case CPP_NESTED_NAME_SPECIFIER:
5129       {
5130       id_expression:
5131         cp_expr id_expression;
5132         cp_expr decl;
5133         const char *error_msg;
5134         bool template_p;
5135         bool done;
5136         cp_token *id_expr_token;
5137
5138         /* Parse the id-expression.  */
5139         id_expression
5140           = cp_parser_id_expression (parser,
5141                                      /*template_keyword_p=*/false,
5142                                      /*check_dependency_p=*/true,
5143                                      &template_p,
5144                                      /*declarator_p=*/false,
5145                                      /*optional_p=*/false);
5146         if (id_expression == error_mark_node)
5147           return error_mark_node;
5148         id_expr_token = token;
5149         token = cp_lexer_peek_token (parser->lexer);
5150         done = (token->type != CPP_OPEN_SQUARE
5151                 && token->type != CPP_OPEN_PAREN
5152                 && token->type != CPP_DOT
5153                 && token->type != CPP_DEREF
5154                 && token->type != CPP_PLUS_PLUS
5155                 && token->type != CPP_MINUS_MINUS);
5156         /* If we have a template-id, then no further lookup is
5157            required.  If the template-id was for a template-class, we
5158            will sometimes have a TYPE_DECL at this point.  */
5159         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5160                  || TREE_CODE (id_expression) == TYPE_DECL)
5161           decl = id_expression;
5162         /* Look up the name.  */
5163         else
5164           {
5165             tree ambiguous_decls;
5166
5167             /* If we already know that this lookup is ambiguous, then
5168                we've already issued an error message; there's no reason
5169                to check again.  */
5170             if (id_expr_token->type == CPP_NAME
5171                 && id_expr_token->error_reported)
5172               {
5173                 cp_parser_simulate_error (parser);
5174                 return error_mark_node;
5175               }
5176
5177             decl = cp_parser_lookup_name (parser, id_expression,
5178                                           none_type,
5179                                           template_p,
5180                                           /*is_namespace=*/false,
5181                                           /*check_dependency=*/true,
5182                                           &ambiguous_decls,
5183                                           id_expr_token->location);
5184             /* If the lookup was ambiguous, an error will already have
5185                been issued.  */
5186             if (ambiguous_decls)
5187               return error_mark_node;
5188
5189             /* In Objective-C++, we may have an Objective-C 2.0
5190                dot-syntax for classes here.  */
5191             if (c_dialect_objc ()
5192                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5193                 && TREE_CODE (decl) == TYPE_DECL
5194                 && objc_is_class_name (decl))
5195               {
5196                 tree component;
5197                 cp_lexer_consume_token (parser->lexer);
5198                 component = cp_parser_identifier (parser);
5199                 if (component == error_mark_node)
5200                   return error_mark_node;
5201
5202                 tree result = objc_build_class_component_ref (id_expression,
5203                                                               component);
5204                 /* Build a location of the form:
5205                      expr.component
5206                      ~~~~~^~~~~~~~~
5207                    with caret at the start of the component name (at
5208                    input_location), ranging from the start of the id_expression
5209                    to the end of the component name.  */
5210                 location_t combined_loc
5211                   = make_location (input_location, id_expression.get_start (),
5212                                    get_finish (input_location));
5213                 protected_set_expr_location (result, combined_loc);
5214                 return result;
5215               }
5216
5217             /* In Objective-C++, an instance variable (ivar) may be preferred
5218                to whatever cp_parser_lookup_name() found.
5219                Call objc_lookup_ivar.  To avoid exposing cp_expr to the
5220                rest of c-family, we have to do a little extra work to preserve
5221                any location information in cp_expr "decl".  Given that
5222                objc_lookup_ivar is implemented in "c-family" and "objc", we
5223                have a trip through the pure "tree" type, rather than cp_expr.
5224                Naively copying it back to "decl" would implicitly give the
5225                new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5226                store an EXPR_LOCATION.  Hence we only update "decl" (and
5227                hence its location_t) if we get back a different tree node.  */
5228             tree decl_tree = objc_lookup_ivar (decl.get_value (),
5229                                                id_expression);
5230             if (decl_tree != decl.get_value ())
5231               decl = cp_expr (decl_tree);
5232
5233             /* If name lookup gives us a SCOPE_REF, then the
5234                qualifying scope was dependent.  */
5235             if (TREE_CODE (decl) == SCOPE_REF)
5236               {
5237                 /* At this point, we do not know if DECL is a valid
5238                    integral constant expression.  We assume that it is
5239                    in fact such an expression, so that code like:
5240
5241                       template <int N> struct A {
5242                         int a[B<N>::i];
5243                       };
5244                      
5245                    is accepted.  At template-instantiation time, we
5246                    will check that B<N>::i is actually a constant.  */
5247                 return decl;
5248               }
5249             /* Check to see if DECL is a local variable in a context
5250                where that is forbidden.  */
5251             if (parser->local_variables_forbidden_p
5252                 && local_variable_p (decl))
5253               {
5254                 /* It might be that we only found DECL because we are
5255                    trying to be generous with pre-ISO scoping rules.
5256                    For example, consider:
5257
5258                      int i;
5259                      void g() {
5260                        for (int i = 0; i < 10; ++i) {}
5261                        extern void f(int j = i);
5262                      }
5263
5264                    Here, name look up will originally find the out
5265                    of scope `i'.  We need to issue a warning message,
5266                    but then use the global `i'.  */
5267                 decl = check_for_out_of_scope_variable (decl);
5268                 if (local_variable_p (decl))
5269                   {
5270                     error_at (id_expr_token->location,
5271                               "local variable %qD may not appear in this context",
5272                               decl.get_value ());
5273                     return error_mark_node;
5274                   }
5275               }
5276           }
5277
5278         decl = (finish_id_expression
5279                 (id_expression, decl, parser->scope,
5280                  idk,
5281                  parser->integral_constant_expression_p,
5282                  parser->allow_non_integral_constant_expression_p,
5283                  &parser->non_integral_constant_expression_p,
5284                  template_p, done, address_p,
5285                  template_arg_p,
5286                  &error_msg,
5287                  id_expr_token->location));
5288         if (error_msg)
5289           cp_parser_error (parser, error_msg);
5290         decl.set_location (id_expr_token->location);
5291         return decl;
5292       }
5293
5294       /* Anything else is an error.  */
5295     default:
5296       cp_parser_error (parser, "expected primary-expression");
5297       return error_mark_node;
5298     }
5299 }
5300
5301 static inline cp_expr
5302 cp_parser_primary_expression (cp_parser *parser,
5303                               bool address_p,
5304                               bool cast_p,
5305                               bool template_arg_p,
5306                               cp_id_kind *idk)
5307 {
5308   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5309                                        /*decltype*/false, idk);
5310 }
5311
5312 /* Parse an id-expression.
5313
5314    id-expression:
5315      unqualified-id
5316      qualified-id
5317
5318    qualified-id:
5319      :: [opt] nested-name-specifier template [opt] unqualified-id
5320      :: identifier
5321      :: operator-function-id
5322      :: template-id
5323
5324    Return a representation of the unqualified portion of the
5325    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
5326    a `::' or nested-name-specifier.
5327
5328    Often, if the id-expression was a qualified-id, the caller will
5329    want to make a SCOPE_REF to represent the qualified-id.  This
5330    function does not do this in order to avoid wastefully creating
5331    SCOPE_REFs when they are not required.
5332
5333    If TEMPLATE_KEYWORD_P is true, then we have just seen the
5334    `template' keyword.
5335
5336    If CHECK_DEPENDENCY_P is false, then names are looked up inside
5337    uninstantiated templates.
5338
5339    If *TEMPLATE_P is non-NULL, it is set to true iff the
5340    `template' keyword is used to explicitly indicate that the entity
5341    named is a template.
5342
5343    If DECLARATOR_P is true, the id-expression is appearing as part of
5344    a declarator, rather than as part of an expression.  */
5345
5346 static cp_expr
5347 cp_parser_id_expression (cp_parser *parser,
5348                          bool template_keyword_p,
5349                          bool check_dependency_p,
5350                          bool *template_p,
5351                          bool declarator_p,
5352                          bool optional_p)
5353 {
5354   bool global_scope_p;
5355   bool nested_name_specifier_p;
5356
5357   /* Assume the `template' keyword was not used.  */
5358   if (template_p)
5359     *template_p = template_keyword_p;
5360
5361   /* Look for the optional `::' operator.  */
5362   global_scope_p
5363     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5364        != NULL_TREE);
5365   /* Look for the optional nested-name-specifier.  */
5366   nested_name_specifier_p
5367     = (cp_parser_nested_name_specifier_opt (parser,
5368                                             /*typename_keyword_p=*/false,
5369                                             check_dependency_p,
5370                                             /*type_p=*/false,
5371                                             declarator_p)
5372        != NULL_TREE);
5373   /* If there is a nested-name-specifier, then we are looking at
5374      the first qualified-id production.  */
5375   if (nested_name_specifier_p)
5376     {
5377       tree saved_scope;
5378       tree saved_object_scope;
5379       tree saved_qualifying_scope;
5380       tree unqualified_id;
5381       bool is_template;
5382
5383       /* See if the next token is the `template' keyword.  */
5384       if (!template_p)
5385         template_p = &is_template;
5386       *template_p = cp_parser_optional_template_keyword (parser);
5387       /* Name lookup we do during the processing of the
5388          unqualified-id might obliterate SCOPE.  */
5389       saved_scope = parser->scope;
5390       saved_object_scope = parser->object_scope;
5391       saved_qualifying_scope = parser->qualifying_scope;
5392       /* Process the final unqualified-id.  */
5393       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5394                                                  check_dependency_p,
5395                                                  declarator_p,
5396                                                  /*optional_p=*/false);
5397       /* Restore the SAVED_SCOPE for our caller.  */
5398       parser->scope = saved_scope;
5399       parser->object_scope = saved_object_scope;
5400       parser->qualifying_scope = saved_qualifying_scope;
5401
5402       return unqualified_id;
5403     }
5404   /* Otherwise, if we are in global scope, then we are looking at one
5405      of the other qualified-id productions.  */
5406   else if (global_scope_p)
5407     {
5408       cp_token *token;
5409       tree id;
5410
5411       /* Peek at the next token.  */
5412       token = cp_lexer_peek_token (parser->lexer);
5413
5414       /* If it's an identifier, and the next token is not a "<", then
5415          we can avoid the template-id case.  This is an optimization
5416          for this common case.  */
5417       if (token->type == CPP_NAME
5418           && !cp_parser_nth_token_starts_template_argument_list_p
5419                (parser, 2))
5420         return cp_parser_identifier (parser);
5421
5422       cp_parser_parse_tentatively (parser);
5423       /* Try a template-id.  */
5424       id = cp_parser_template_id (parser,
5425                                   /*template_keyword_p=*/false,
5426                                   /*check_dependency_p=*/true,
5427                                   none_type,
5428                                   declarator_p);
5429       /* If that worked, we're done.  */
5430       if (cp_parser_parse_definitely (parser))
5431         return id;
5432
5433       /* Peek at the next token.  (Changes in the token buffer may
5434          have invalidated the pointer obtained above.)  */
5435       token = cp_lexer_peek_token (parser->lexer);
5436
5437       switch (token->type)
5438         {
5439         case CPP_NAME:
5440           return cp_parser_identifier (parser);
5441
5442         case CPP_KEYWORD:
5443           if (token->keyword == RID_OPERATOR)
5444             return cp_parser_operator_function_id (parser);
5445           /* Fall through.  */
5446
5447         default:
5448           cp_parser_error (parser, "expected id-expression");
5449           return error_mark_node;
5450         }
5451     }
5452   else
5453     return cp_parser_unqualified_id (parser, template_keyword_p,
5454                                      /*check_dependency_p=*/true,
5455                                      declarator_p,
5456                                      optional_p);
5457 }
5458
5459 /* Parse an unqualified-id.
5460
5461    unqualified-id:
5462      identifier
5463      operator-function-id
5464      conversion-function-id
5465      ~ class-name
5466      template-id
5467
5468    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5469    keyword, in a construct like `A::template ...'.
5470
5471    Returns a representation of unqualified-id.  For the `identifier'
5472    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
5473    production a BIT_NOT_EXPR is returned; the operand of the
5474    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
5475    other productions, see the documentation accompanying the
5476    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
5477    names are looked up in uninstantiated templates.  If DECLARATOR_P
5478    is true, the unqualified-id is appearing as part of a declarator,
5479    rather than as part of an expression.  */
5480
5481 static cp_expr
5482 cp_parser_unqualified_id (cp_parser* parser,
5483                           bool template_keyword_p,
5484                           bool check_dependency_p,
5485                           bool declarator_p,
5486                           bool optional_p)
5487 {
5488   cp_token *token;
5489
5490   /* Peek at the next token.  */
5491   token = cp_lexer_peek_token (parser->lexer);
5492
5493   switch ((int) token->type)
5494     {
5495     case CPP_NAME:
5496       {
5497         tree id;
5498
5499         /* We don't know yet whether or not this will be a
5500            template-id.  */
5501         cp_parser_parse_tentatively (parser);
5502         /* Try a template-id.  */
5503         id = cp_parser_template_id (parser, template_keyword_p,
5504                                     check_dependency_p,
5505                                     none_type,
5506                                     declarator_p);
5507         /* If it worked, we're done.  */
5508         if (cp_parser_parse_definitely (parser))
5509           return id;
5510         /* Otherwise, it's an ordinary identifier.  */
5511         return cp_parser_identifier (parser);
5512       }
5513
5514     case CPP_TEMPLATE_ID:
5515       return cp_parser_template_id (parser, template_keyword_p,
5516                                     check_dependency_p,
5517                                     none_type,
5518                                     declarator_p);
5519
5520     case CPP_COMPL:
5521       {
5522         tree type_decl;
5523         tree qualifying_scope;
5524         tree object_scope;
5525         tree scope;
5526         bool done;
5527
5528         /* Consume the `~' token.  */
5529         cp_lexer_consume_token (parser->lexer);
5530         /* Parse the class-name.  The standard, as written, seems to
5531            say that:
5532
5533              template <typename T> struct S { ~S (); };
5534              template <typename T> S<T>::~S() {}
5535
5536            is invalid, since `~' must be followed by a class-name, but
5537            `S<T>' is dependent, and so not known to be a class.
5538            That's not right; we need to look in uninstantiated
5539            templates.  A further complication arises from:
5540
5541              template <typename T> void f(T t) {
5542                t.T::~T();
5543              }
5544
5545            Here, it is not possible to look up `T' in the scope of `T'
5546            itself.  We must look in both the current scope, and the
5547            scope of the containing complete expression.
5548
5549            Yet another issue is:
5550
5551              struct S {
5552                int S;
5553                ~S();
5554              };
5555
5556              S::~S() {}
5557
5558            The standard does not seem to say that the `S' in `~S'
5559            should refer to the type `S' and not the data member
5560            `S::S'.  */
5561
5562         /* DR 244 says that we look up the name after the "~" in the
5563            same scope as we looked up the qualifying name.  That idea
5564            isn't fully worked out; it's more complicated than that.  */
5565         scope = parser->scope;
5566         object_scope = parser->object_scope;
5567         qualifying_scope = parser->qualifying_scope;
5568
5569         /* Check for invalid scopes.  */
5570         if (scope == error_mark_node)
5571           {
5572             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5573               cp_lexer_consume_token (parser->lexer);
5574             return error_mark_node;
5575           }
5576         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5577           {
5578             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5579               error_at (token->location,
5580                         "scope %qT before %<~%> is not a class-name",
5581                         scope);
5582             cp_parser_simulate_error (parser);
5583             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5584               cp_lexer_consume_token (parser->lexer);
5585             return error_mark_node;
5586           }
5587         gcc_assert (!scope || TYPE_P (scope));
5588
5589         /* If the name is of the form "X::~X" it's OK even if X is a
5590            typedef.  */
5591         token = cp_lexer_peek_token (parser->lexer);
5592         if (scope
5593             && token->type == CPP_NAME
5594             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5595                 != CPP_LESS)
5596             && (token->u.value == TYPE_IDENTIFIER (scope)
5597                 || (CLASS_TYPE_P (scope)
5598                     && constructor_name_p (token->u.value, scope))))
5599           {
5600             cp_lexer_consume_token (parser->lexer);
5601             return build_nt (BIT_NOT_EXPR, scope);
5602           }
5603
5604         /* ~auto means the destructor of whatever the object is.  */
5605         if (cp_parser_is_keyword (token, RID_AUTO))
5606           {
5607             if (cxx_dialect < cxx14)
5608               pedwarn (input_location, 0,
5609                        "%<~auto%> only available with "
5610                        "-std=c++14 or -std=gnu++14");
5611             cp_lexer_consume_token (parser->lexer);
5612             return build_nt (BIT_NOT_EXPR, make_auto ());
5613           }
5614
5615         /* If there was an explicit qualification (S::~T), first look
5616            in the scope given by the qualification (i.e., S).
5617
5618            Note: in the calls to cp_parser_class_name below we pass
5619            typename_type so that lookup finds the injected-class-name
5620            rather than the constructor.  */
5621         done = false;
5622         type_decl = NULL_TREE;
5623         if (scope)
5624           {
5625             cp_parser_parse_tentatively (parser);
5626             type_decl = cp_parser_class_name (parser,
5627                                               /*typename_keyword_p=*/false,
5628                                               /*template_keyword_p=*/false,
5629                                               typename_type,
5630                                               /*check_dependency=*/false,
5631                                               /*class_head_p=*/false,
5632                                               declarator_p);
5633             if (cp_parser_parse_definitely (parser))
5634               done = true;
5635           }
5636         /* In "N::S::~S", look in "N" as well.  */
5637         if (!done && scope && qualifying_scope)
5638           {
5639             cp_parser_parse_tentatively (parser);
5640             parser->scope = qualifying_scope;
5641             parser->object_scope = NULL_TREE;
5642             parser->qualifying_scope = NULL_TREE;
5643             type_decl
5644               = cp_parser_class_name (parser,
5645                                       /*typename_keyword_p=*/false,
5646                                       /*template_keyword_p=*/false,
5647                                       typename_type,
5648                                       /*check_dependency=*/false,
5649                                       /*class_head_p=*/false,
5650                                       declarator_p);
5651             if (cp_parser_parse_definitely (parser))
5652               done = true;
5653           }
5654         /* In "p->S::~T", look in the scope given by "*p" as well.  */
5655         else if (!done && object_scope)
5656           {
5657             cp_parser_parse_tentatively (parser);
5658             parser->scope = object_scope;
5659             parser->object_scope = NULL_TREE;
5660             parser->qualifying_scope = NULL_TREE;
5661             type_decl
5662               = cp_parser_class_name (parser,
5663                                       /*typename_keyword_p=*/false,
5664                                       /*template_keyword_p=*/false,
5665                                       typename_type,
5666                                       /*check_dependency=*/false,
5667                                       /*class_head_p=*/false,
5668                                       declarator_p);
5669             if (cp_parser_parse_definitely (parser))
5670               done = true;
5671           }
5672         /* Look in the surrounding context.  */
5673         if (!done)
5674           {
5675             parser->scope = NULL_TREE;
5676             parser->object_scope = NULL_TREE;
5677             parser->qualifying_scope = NULL_TREE;
5678             if (processing_template_decl)
5679               cp_parser_parse_tentatively (parser);
5680             type_decl
5681               = cp_parser_class_name (parser,
5682                                       /*typename_keyword_p=*/false,
5683                                       /*template_keyword_p=*/false,
5684                                       typename_type,
5685                                       /*check_dependency=*/false,
5686                                       /*class_head_p=*/false,
5687                                       declarator_p);
5688             if (processing_template_decl
5689                 && ! cp_parser_parse_definitely (parser))
5690               {
5691                 /* We couldn't find a type with this name.  If we're parsing
5692                    tentatively, fail and try something else.  */
5693                 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5694                   {
5695                     cp_parser_simulate_error (parser);
5696                     return error_mark_node;
5697                   }
5698                 /* Otherwise, accept it and check for a match at instantiation
5699                    time.  */
5700                 type_decl = cp_parser_identifier (parser);
5701                 if (type_decl != error_mark_node)
5702                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5703                 return type_decl;
5704               }
5705           }
5706         /* If an error occurred, assume that the name of the
5707            destructor is the same as the name of the qualifying
5708            class.  That allows us to keep parsing after running
5709            into ill-formed destructor names.  */
5710         if (type_decl == error_mark_node && scope)
5711           return build_nt (BIT_NOT_EXPR, scope);
5712         else if (type_decl == error_mark_node)
5713           return error_mark_node;
5714
5715         /* Check that destructor name and scope match.  */
5716         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5717           {
5718             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5719               error_at (token->location,
5720                         "declaration of %<~%T%> as member of %qT",
5721                         type_decl, scope);
5722             cp_parser_simulate_error (parser);
5723             return error_mark_node;
5724           }
5725
5726         /* [class.dtor]
5727
5728            A typedef-name that names a class shall not be used as the
5729            identifier in the declarator for a destructor declaration.  */
5730         if (declarator_p
5731             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5732             && !DECL_SELF_REFERENCE_P (type_decl)
5733             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5734           error_at (token->location,
5735                     "typedef-name %qD used as destructor declarator",
5736                     type_decl);
5737
5738         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5739       }
5740
5741     case CPP_KEYWORD:
5742       if (token->keyword == RID_OPERATOR)
5743         {
5744           cp_expr id;
5745
5746           /* This could be a template-id, so we try that first.  */
5747           cp_parser_parse_tentatively (parser);
5748           /* Try a template-id.  */
5749           id = cp_parser_template_id (parser, template_keyword_p,
5750                                       /*check_dependency_p=*/true,
5751                                       none_type,
5752                                       declarator_p);
5753           /* If that worked, we're done.  */
5754           if (cp_parser_parse_definitely (parser))
5755             return id;
5756           /* We still don't know whether we're looking at an
5757              operator-function-id or a conversion-function-id.  */
5758           cp_parser_parse_tentatively (parser);
5759           /* Try an operator-function-id.  */
5760           id = cp_parser_operator_function_id (parser);
5761           /* If that didn't work, try a conversion-function-id.  */
5762           if (!cp_parser_parse_definitely (parser))
5763             id = cp_parser_conversion_function_id (parser);
5764           else if (UDLIT_OPER_P (id))
5765             {
5766               /* 17.6.3.3.5  */
5767               const char *name = UDLIT_OP_SUFFIX (id);
5768               if (name[0] != '_' && !in_system_header_at (input_location)
5769                   && declarator_p)
5770                 warning (0, "literal operator suffixes not preceded by %<_%>"
5771                             " are reserved for future standardization");
5772             }
5773
5774           return id;
5775         }
5776       /* Fall through.  */
5777
5778     default:
5779       if (optional_p)
5780         return NULL_TREE;
5781       cp_parser_error (parser, "expected unqualified-id");
5782       return error_mark_node;
5783     }
5784 }
5785
5786 /* Parse an (optional) nested-name-specifier.
5787
5788    nested-name-specifier: [C++98]
5789      class-or-namespace-name :: nested-name-specifier [opt]
5790      class-or-namespace-name :: template nested-name-specifier [opt]
5791
5792    nested-name-specifier: [C++0x]
5793      type-name ::
5794      namespace-name ::
5795      nested-name-specifier identifier ::
5796      nested-name-specifier template [opt] simple-template-id ::
5797
5798    PARSER->SCOPE should be set appropriately before this function is
5799    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5800    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
5801    in name lookups.
5802
5803    Sets PARSER->SCOPE to the class (TYPE) or namespace
5804    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5805    it unchanged if there is no nested-name-specifier.  Returns the new
5806    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5807
5808    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5809    part of a declaration and/or decl-specifier.  */
5810
5811 static tree
5812 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5813                                      bool typename_keyword_p,
5814                                      bool check_dependency_p,
5815                                      bool type_p,
5816                                      bool is_declaration)
5817 {
5818   bool success = false;
5819   cp_token_position start = 0;
5820   cp_token *token;
5821
5822   /* Remember where the nested-name-specifier starts.  */
5823   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5824     {
5825       start = cp_lexer_token_position (parser->lexer, false);
5826       push_deferring_access_checks (dk_deferred);
5827     }
5828
5829   while (true)
5830     {
5831       tree new_scope;
5832       tree old_scope;
5833       tree saved_qualifying_scope;
5834       bool template_keyword_p;
5835
5836       /* Spot cases that cannot be the beginning of a
5837          nested-name-specifier.  */
5838       token = cp_lexer_peek_token (parser->lexer);
5839
5840       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5841          the already parsed nested-name-specifier.  */
5842       if (token->type == CPP_NESTED_NAME_SPECIFIER)
5843         {
5844           /* Grab the nested-name-specifier and continue the loop.  */
5845           cp_parser_pre_parsed_nested_name_specifier (parser);
5846           /* If we originally encountered this nested-name-specifier
5847              with IS_DECLARATION set to false, we will not have
5848              resolved TYPENAME_TYPEs, so we must do so here.  */
5849           if (is_declaration
5850               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5851             {
5852               new_scope = resolve_typename_type (parser->scope,
5853                                                  /*only_current_p=*/false);
5854               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5855                 parser->scope = new_scope;
5856             }
5857           success = true;
5858           continue;
5859         }
5860
5861       /* Spot cases that cannot be the beginning of a
5862          nested-name-specifier.  On the second and subsequent times
5863          through the loop, we look for the `template' keyword.  */
5864       if (success && token->keyword == RID_TEMPLATE)
5865         ;
5866       /* A template-id can start a nested-name-specifier.  */
5867       else if (token->type == CPP_TEMPLATE_ID)
5868         ;
5869       /* DR 743: decltype can be used in a nested-name-specifier.  */
5870       else if (token_is_decltype (token))
5871         ;
5872       else
5873         {
5874           /* If the next token is not an identifier, then it is
5875              definitely not a type-name or namespace-name.  */
5876           if (token->type != CPP_NAME)
5877             break;
5878           /* If the following token is neither a `<' (to begin a
5879              template-id), nor a `::', then we are not looking at a
5880              nested-name-specifier.  */
5881           token = cp_lexer_peek_nth_token (parser->lexer, 2);
5882
5883           if (token->type == CPP_COLON
5884               && parser->colon_corrects_to_scope_p
5885               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5886             {
5887               error_at (token->location,
5888                         "found %<:%> in nested-name-specifier, expected %<::%>");
5889               token->type = CPP_SCOPE;
5890             }
5891
5892           if (token->type != CPP_SCOPE
5893               && !cp_parser_nth_token_starts_template_argument_list_p
5894                   (parser, 2))
5895             break;
5896         }
5897
5898       /* The nested-name-specifier is optional, so we parse
5899          tentatively.  */
5900       cp_parser_parse_tentatively (parser);
5901
5902       /* Look for the optional `template' keyword, if this isn't the
5903          first time through the loop.  */
5904       if (success)
5905         template_keyword_p = cp_parser_optional_template_keyword (parser);
5906       else
5907         template_keyword_p = false;
5908
5909       /* Save the old scope since the name lookup we are about to do
5910          might destroy it.  */
5911       old_scope = parser->scope;
5912       saved_qualifying_scope = parser->qualifying_scope;
5913       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5914          look up names in "X<T>::I" in order to determine that "Y" is
5915          a template.  So, if we have a typename at this point, we make
5916          an effort to look through it.  */
5917       if (is_declaration
5918           && !typename_keyword_p
5919           && parser->scope
5920           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5921         parser->scope = resolve_typename_type (parser->scope,
5922                                                /*only_current_p=*/false);
5923       /* Parse the qualifying entity.  */
5924       new_scope
5925         = cp_parser_qualifying_entity (parser,
5926                                        typename_keyword_p,
5927                                        template_keyword_p,
5928                                        check_dependency_p,
5929                                        type_p,
5930                                        is_declaration);
5931       /* Look for the `::' token.  */
5932       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5933
5934       /* If we found what we wanted, we keep going; otherwise, we're
5935          done.  */
5936       if (!cp_parser_parse_definitely (parser))
5937         {
5938           bool error_p = false;
5939
5940           /* Restore the OLD_SCOPE since it was valid before the
5941              failed attempt at finding the last
5942              class-or-namespace-name.  */
5943           parser->scope = old_scope;
5944           parser->qualifying_scope = saved_qualifying_scope;
5945
5946           /* If the next token is a decltype, and the one after that is a
5947              `::', then the decltype has failed to resolve to a class or
5948              enumeration type.  Give this error even when parsing
5949              tentatively since it can't possibly be valid--and we're going
5950              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5951              won't get another chance.*/
5952           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5953               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5954                   == CPP_SCOPE))
5955             {
5956               token = cp_lexer_consume_token (parser->lexer);
5957               error_at (token->location, "decltype evaluates to %qT, "
5958                         "which is not a class or enumeration type",
5959                         token->u.tree_check_value->value);
5960               parser->scope = error_mark_node;
5961               error_p = true;
5962               /* As below.  */
5963               success = true;
5964               cp_lexer_consume_token (parser->lexer);
5965             }
5966
5967           if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5968               && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5969             {
5970               /* If we have a non-type template-id followed by ::, it can't
5971                  possibly be valid.  */
5972               token = cp_lexer_peek_token (parser->lexer);
5973               tree tid = token->u.tree_check_value->value;
5974               if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5975                   && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5976                 {
5977                   tree tmpl = NULL_TREE;
5978                   if (is_overloaded_fn (tid))
5979                     {
5980                       tree fns = get_fns (tid);
5981                       if (!OVL_CHAIN (fns))
5982                         tmpl = OVL_CURRENT (fns);
5983                       error_at (token->location, "function template-id %qD "
5984                                 "in nested-name-specifier", tid);
5985                     }
5986                   else
5987                     {
5988                       /* Variable template.  */
5989                       tmpl = TREE_OPERAND (tid, 0);
5990                       gcc_assert (variable_template_p (tmpl));
5991                       error_at (token->location, "variable template-id %qD "
5992                                 "in nested-name-specifier", tid);
5993                     }
5994                   if (tmpl)
5995                     inform (DECL_SOURCE_LOCATION (tmpl),
5996                             "%qD declared here", tmpl);
5997
5998                   parser->scope = error_mark_node;
5999                   error_p = true;
6000                   /* As below.  */
6001                   success = true;
6002                   cp_lexer_consume_token (parser->lexer);
6003                   cp_lexer_consume_token (parser->lexer);
6004                 }
6005             }
6006
6007           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6008             break;
6009           /* If the next token is an identifier, and the one after
6010              that is a `::', then any valid interpretation would have
6011              found a class-or-namespace-name.  */
6012           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6013                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6014                      == CPP_SCOPE)
6015                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6016                      != CPP_COMPL))
6017             {
6018               token = cp_lexer_consume_token (parser->lexer);
6019               if (!error_p)
6020                 {
6021                   if (!token->error_reported)
6022                     {
6023                       tree decl;
6024                       tree ambiguous_decls;
6025
6026                       decl = cp_parser_lookup_name (parser, token->u.value,
6027                                                     none_type,
6028                                                     /*is_template=*/false,
6029                                                     /*is_namespace=*/false,
6030                                                     /*check_dependency=*/true,
6031                                                     &ambiguous_decls,
6032                                                     token->location);
6033                       if (TREE_CODE (decl) == TEMPLATE_DECL)
6034                         error_at (token->location,
6035                                   "%qD used without template parameters",
6036                                   decl);
6037                       else if (ambiguous_decls)
6038                         {
6039                           // cp_parser_lookup_name has the same diagnostic,
6040                           // thus make sure to emit it at most once.
6041                           if (cp_parser_uncommitted_to_tentative_parse_p
6042                               (parser))
6043                             {
6044                               error_at (token->location,
6045                                         "reference to %qD is ambiguous",
6046                                         token->u.value);
6047                               print_candidates (ambiguous_decls);
6048                             }
6049                           decl = error_mark_node;
6050                         }
6051                       else
6052                         {
6053                           if (cxx_dialect != cxx98)
6054                             cp_parser_name_lookup_error
6055                             (parser, token->u.value, decl, NLE_NOT_CXX98,
6056                              token->location);
6057                           else
6058                             cp_parser_name_lookup_error
6059                             (parser, token->u.value, decl, NLE_CXX98,
6060                              token->location);
6061                         }
6062                     }
6063                   parser->scope = error_mark_node;
6064                   error_p = true;
6065                   /* Treat this as a successful nested-name-specifier
6066                      due to:
6067
6068                      [basic.lookup.qual]
6069
6070                      If the name found is not a class-name (clause
6071                      _class_) or namespace-name (_namespace.def_), the
6072                      program is ill-formed.  */
6073                   success = true;
6074                 }
6075               cp_lexer_consume_token (parser->lexer);
6076             }
6077           break;
6078         }
6079       /* We've found one valid nested-name-specifier.  */
6080       success = true;
6081       /* Name lookup always gives us a DECL.  */
6082       if (TREE_CODE (new_scope) == TYPE_DECL)
6083         new_scope = TREE_TYPE (new_scope);
6084       /* Uses of "template" must be followed by actual templates.  */
6085       if (template_keyword_p
6086           && !(CLASS_TYPE_P (new_scope)
6087                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6088                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6089                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
6090           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6091                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6092                    == TEMPLATE_ID_EXPR)))
6093         permerror (input_location, TYPE_P (new_scope)
6094                    ? G_("%qT is not a template")
6095                    : G_("%qD is not a template"),
6096                    new_scope);
6097       /* If it is a class scope, try to complete it; we are about to
6098          be looking up names inside the class.  */
6099       if (TYPE_P (new_scope)
6100           /* Since checking types for dependency can be expensive,
6101              avoid doing it if the type is already complete.  */
6102           && !COMPLETE_TYPE_P (new_scope)
6103           /* Do not try to complete dependent types.  */
6104           && !dependent_type_p (new_scope))
6105         {
6106           new_scope = complete_type (new_scope);
6107           /* If it is a typedef to current class, use the current
6108              class instead, as the typedef won't have any names inside
6109              it yet.  */
6110           if (!COMPLETE_TYPE_P (new_scope)
6111               && currently_open_class (new_scope))
6112             new_scope = TYPE_MAIN_VARIANT (new_scope);
6113         }
6114       /* Make sure we look in the right scope the next time through
6115          the loop.  */
6116       parser->scope = new_scope;
6117     }
6118
6119   /* If parsing tentatively, replace the sequence of tokens that makes
6120      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6121      token.  That way, should we re-parse the token stream, we will
6122      not have to repeat the effort required to do the parse, nor will
6123      we issue duplicate error messages.  */
6124   if (success && start)
6125     {
6126       cp_token *token;
6127
6128       token = cp_lexer_token_at (parser->lexer, start);
6129       /* Reset the contents of the START token.  */
6130       token->type = CPP_NESTED_NAME_SPECIFIER;
6131       /* Retrieve any deferred checks.  Do not pop this access checks yet
6132          so the memory will not be reclaimed during token replacing below.  */
6133       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6134       token->u.tree_check_value->value = parser->scope;
6135       token->u.tree_check_value->checks = get_deferred_access_checks ();
6136       token->u.tree_check_value->qualifying_scope =
6137         parser->qualifying_scope;
6138       token->keyword = RID_MAX;
6139
6140       /* Purge all subsequent tokens.  */
6141       cp_lexer_purge_tokens_after (parser->lexer, start);
6142     }
6143
6144   if (start)
6145     pop_to_parent_deferring_access_checks ();
6146
6147   return success ? parser->scope : NULL_TREE;
6148 }
6149
6150 /* Parse a nested-name-specifier.  See
6151    cp_parser_nested_name_specifier_opt for details.  This function
6152    behaves identically, except that it will an issue an error if no
6153    nested-name-specifier is present.  */
6154
6155 static tree
6156 cp_parser_nested_name_specifier (cp_parser *parser,
6157                                  bool typename_keyword_p,
6158                                  bool check_dependency_p,
6159                                  bool type_p,
6160                                  bool is_declaration)
6161 {
6162   tree scope;
6163
6164   /* Look for the nested-name-specifier.  */
6165   scope = cp_parser_nested_name_specifier_opt (parser,
6166                                                typename_keyword_p,
6167                                                check_dependency_p,
6168                                                type_p,
6169                                                is_declaration);
6170   /* If it was not present, issue an error message.  */
6171   if (!scope)
6172     {
6173       cp_parser_error (parser, "expected nested-name-specifier");
6174       parser->scope = NULL_TREE;
6175     }
6176
6177   return scope;
6178 }
6179
6180 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6181    this is either a class-name or a namespace-name (which corresponds
6182    to the class-or-namespace-name production in the grammar). For
6183    C++0x, it can also be a type-name that refers to an enumeration
6184    type or a simple-template-id.
6185
6186    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6187    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6188    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6189    TYPE_P is TRUE iff the next name should be taken as a class-name,
6190    even the same name is declared to be another entity in the same
6191    scope.
6192
6193    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6194    specified by the class-or-namespace-name.  If neither is found the
6195    ERROR_MARK_NODE is returned.  */
6196
6197 static tree
6198 cp_parser_qualifying_entity (cp_parser *parser,
6199                              bool typename_keyword_p,
6200                              bool template_keyword_p,
6201                              bool check_dependency_p,
6202                              bool type_p,
6203                              bool is_declaration)
6204 {
6205   tree saved_scope;
6206   tree saved_qualifying_scope;
6207   tree saved_object_scope;
6208   tree scope;
6209   bool only_class_p;
6210   bool successful_parse_p;
6211
6212   /* DR 743: decltype can appear in a nested-name-specifier.  */
6213   if (cp_lexer_next_token_is_decltype (parser->lexer))
6214     {
6215       scope = cp_parser_decltype (parser);
6216       if (TREE_CODE (scope) != ENUMERAL_TYPE
6217           && !MAYBE_CLASS_TYPE_P (scope))
6218         {
6219           cp_parser_simulate_error (parser);
6220           return error_mark_node;
6221         }
6222       if (TYPE_NAME (scope))
6223         scope = TYPE_NAME (scope);
6224       return scope;
6225     }
6226
6227   /* Before we try to parse the class-name, we must save away the
6228      current PARSER->SCOPE since cp_parser_class_name will destroy
6229      it.  */
6230   saved_scope = parser->scope;
6231   saved_qualifying_scope = parser->qualifying_scope;
6232   saved_object_scope = parser->object_scope;
6233   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
6234      there is no need to look for a namespace-name.  */
6235   only_class_p = template_keyword_p 
6236     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6237   if (!only_class_p)
6238     cp_parser_parse_tentatively (parser);
6239   scope = cp_parser_class_name (parser,
6240                                 typename_keyword_p,
6241                                 template_keyword_p,
6242                                 type_p ? class_type : none_type,
6243                                 check_dependency_p,
6244                                 /*class_head_p=*/false,
6245                                 is_declaration,
6246                                 /*enum_ok=*/cxx_dialect > cxx98);
6247   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6248   /* If that didn't work, try for a namespace-name.  */
6249   if (!only_class_p && !successful_parse_p)
6250     {
6251       /* Restore the saved scope.  */
6252       parser->scope = saved_scope;
6253       parser->qualifying_scope = saved_qualifying_scope;
6254       parser->object_scope = saved_object_scope;
6255       /* If we are not looking at an identifier followed by the scope
6256          resolution operator, then this is not part of a
6257          nested-name-specifier.  (Note that this function is only used
6258          to parse the components of a nested-name-specifier.)  */
6259       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6260           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6261         return error_mark_node;
6262       scope = cp_parser_namespace_name (parser);
6263     }
6264
6265   return scope;
6266 }
6267
6268 /* Return true if we are looking at a compound-literal, false otherwise.  */
6269
6270 static bool
6271 cp_parser_compound_literal_p (cp_parser *parser)
6272 {
6273   /* Consume the `('.  */
6274   cp_lexer_consume_token (parser->lexer);
6275
6276   cp_lexer_save_tokens (parser->lexer);
6277
6278   /* Skip tokens until the next token is a closing parenthesis.
6279      If we find the closing `)', and the next token is a `{', then
6280      we are looking at a compound-literal.  */
6281   bool compound_literal_p
6282     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6283                                               /*consume_paren=*/true)
6284        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6285   
6286   /* Roll back the tokens we skipped.  */
6287   cp_lexer_rollback_tokens (parser->lexer);
6288
6289   return compound_literal_p;
6290 }
6291
6292 /* Parse a postfix-expression.
6293
6294    postfix-expression:
6295      primary-expression
6296      postfix-expression [ expression ]
6297      postfix-expression ( expression-list [opt] )
6298      simple-type-specifier ( expression-list [opt] )
6299      typename :: [opt] nested-name-specifier identifier
6300        ( expression-list [opt] )
6301      typename :: [opt] nested-name-specifier template [opt] template-id
6302        ( expression-list [opt] )
6303      postfix-expression . template [opt] id-expression
6304      postfix-expression -> template [opt] id-expression
6305      postfix-expression . pseudo-destructor-name
6306      postfix-expression -> pseudo-destructor-name
6307      postfix-expression ++
6308      postfix-expression --
6309      dynamic_cast < type-id > ( expression )
6310      static_cast < type-id > ( expression )
6311      reinterpret_cast < type-id > ( expression )
6312      const_cast < type-id > ( expression )
6313      typeid ( expression )
6314      typeid ( type-id )
6315
6316    GNU Extension:
6317
6318    postfix-expression:
6319      ( type-id ) { initializer-list , [opt] }
6320
6321    This extension is a GNU version of the C99 compound-literal
6322    construct.  (The C99 grammar uses `type-name' instead of `type-id',
6323    but they are essentially the same concept.)
6324
6325    If ADDRESS_P is true, the postfix expression is the operand of the
6326    `&' operator.  CAST_P is true if this expression is the target of a
6327    cast.
6328
6329    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6330    class member access expressions [expr.ref].
6331
6332    Returns a representation of the expression.  */
6333
6334 static cp_expr
6335 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6336                               bool member_access_only_p, bool decltype_p,
6337                               cp_id_kind * pidk_return)
6338 {
6339   cp_token *token;
6340   location_t loc;
6341   enum rid keyword;
6342   cp_id_kind idk = CP_ID_KIND_NONE;
6343   cp_expr postfix_expression = NULL_TREE;
6344   bool is_member_access = false;
6345   int saved_in_statement = -1;
6346
6347   /* Peek at the next token.  */
6348   token = cp_lexer_peek_token (parser->lexer);
6349   loc = token->location;
6350   location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6351
6352   /* Some of the productions are determined by keywords.  */
6353   keyword = token->keyword;
6354   switch (keyword)
6355     {
6356     case RID_DYNCAST:
6357     case RID_STATCAST:
6358     case RID_REINTCAST:
6359     case RID_CONSTCAST:
6360       {
6361         tree type;
6362         cp_expr expression;
6363         const char *saved_message;
6364         bool saved_in_type_id_in_expr_p;
6365
6366         /* All of these can be handled in the same way from the point
6367            of view of parsing.  Begin by consuming the token
6368            identifying the cast.  */
6369         cp_lexer_consume_token (parser->lexer);
6370
6371         /* New types cannot be defined in the cast.  */
6372         saved_message = parser->type_definition_forbidden_message;
6373         parser->type_definition_forbidden_message
6374           = G_("types may not be defined in casts");
6375
6376         /* Look for the opening `<'.  */
6377         cp_parser_require (parser, CPP_LESS, RT_LESS);
6378         /* Parse the type to which we are casting.  */
6379         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6380         parser->in_type_id_in_expr_p = true;
6381         type = cp_parser_type_id (parser);
6382         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6383         /* Look for the closing `>'.  */
6384         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6385         /* Restore the old message.  */
6386         parser->type_definition_forbidden_message = saved_message;
6387
6388         bool saved_greater_than_is_operator_p
6389           = parser->greater_than_is_operator_p;
6390         parser->greater_than_is_operator_p = true;
6391
6392         /* And the expression which is being cast.  */
6393         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6394         expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6395         cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6396                                                    RT_CLOSE_PAREN);
6397         location_t end_loc = close_paren ?
6398           close_paren->location : UNKNOWN_LOCATION;
6399
6400         parser->greater_than_is_operator_p
6401           = saved_greater_than_is_operator_p;
6402
6403         /* Only type conversions to integral or enumeration types
6404            can be used in constant-expressions.  */
6405         if (!cast_valid_in_integral_constant_expression_p (type)
6406             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6407           {
6408             postfix_expression = error_mark_node;
6409             break;
6410           }
6411
6412         switch (keyword)
6413           {
6414           case RID_DYNCAST:
6415             postfix_expression
6416               = build_dynamic_cast (type, expression, tf_warning_or_error);
6417             break;
6418           case RID_STATCAST:
6419             postfix_expression
6420               = build_static_cast (type, expression, tf_warning_or_error);
6421             break;
6422           case RID_REINTCAST:
6423             postfix_expression
6424               = build_reinterpret_cast (type, expression, 
6425                                         tf_warning_or_error);
6426             break;
6427           case RID_CONSTCAST:
6428             postfix_expression
6429               = build_const_cast (type, expression, tf_warning_or_error);
6430             break;
6431           default:
6432             gcc_unreachable ();
6433           }
6434
6435         /* Construct a location e.g. :
6436              reinterpret_cast <int *> (expr)
6437              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6438            ranging from the start of the "*_cast" token to the final closing
6439            paren, with the caret at the start.  */
6440         location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6441         postfix_expression.set_location (cp_cast_loc);
6442       }
6443       break;
6444
6445     case RID_TYPEID:
6446       {
6447         tree type;
6448         const char *saved_message;
6449         bool saved_in_type_id_in_expr_p;
6450
6451         /* Consume the `typeid' token.  */
6452         cp_lexer_consume_token (parser->lexer);
6453         /* Look for the `(' token.  */
6454         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6455         /* Types cannot be defined in a `typeid' expression.  */
6456         saved_message = parser->type_definition_forbidden_message;
6457         parser->type_definition_forbidden_message
6458           = G_("types may not be defined in a %<typeid%> expression");
6459         /* We can't be sure yet whether we're looking at a type-id or an
6460            expression.  */
6461         cp_parser_parse_tentatively (parser);
6462         /* Try a type-id first.  */
6463         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6464         parser->in_type_id_in_expr_p = true;
6465         type = cp_parser_type_id (parser);
6466         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6467         /* Look for the `)' token.  Otherwise, we can't be sure that
6468            we're not looking at an expression: consider `typeid (int
6469            (3))', for example.  */
6470         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6471         /* If all went well, simply lookup the type-id.  */
6472         if (cp_parser_parse_definitely (parser))
6473           postfix_expression = get_typeid (type, tf_warning_or_error);
6474         /* Otherwise, fall back to the expression variant.  */
6475         else
6476           {
6477             tree expression;
6478
6479             /* Look for an expression.  */
6480             expression = cp_parser_expression (parser, & idk);
6481             /* Compute its typeid.  */
6482             postfix_expression = build_typeid (expression, tf_warning_or_error);
6483             /* Look for the `)' token.  */
6484             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6485           }
6486         /* Restore the saved message.  */
6487         parser->type_definition_forbidden_message = saved_message;
6488         /* `typeid' may not appear in an integral constant expression.  */
6489         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6490           postfix_expression = error_mark_node;
6491       }
6492       break;
6493
6494     case RID_TYPENAME:
6495       {
6496         tree type;
6497         /* The syntax permitted here is the same permitted for an
6498            elaborated-type-specifier.  */
6499         ++parser->prevent_constrained_type_specifiers;
6500         type = cp_parser_elaborated_type_specifier (parser,
6501                                                     /*is_friend=*/false,
6502                                                     /*is_declaration=*/false);
6503         --parser->prevent_constrained_type_specifiers;
6504         postfix_expression = cp_parser_functional_cast (parser, type);
6505       }
6506       break;
6507
6508     case RID_CILK_SPAWN:
6509       {
6510         location_t cilk_spawn_loc
6511           = cp_lexer_peek_token (parser->lexer)->location;
6512         cp_lexer_consume_token (parser->lexer);
6513         token = cp_lexer_peek_token (parser->lexer);
6514         if (token->type == CPP_SEMICOLON)
6515           {
6516             error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6517                       "an expression");
6518             postfix_expression = error_mark_node;
6519             break;
6520           }
6521         else if (!current_function_decl)
6522           {
6523             error_at (token->location, "%<_Cilk_spawn%> may only be used "
6524                       "inside a function");
6525             postfix_expression = error_mark_node;
6526             break;
6527           }
6528         else
6529           {
6530             /* Consecutive _Cilk_spawns are not allowed in a statement.  */
6531             saved_in_statement = parser->in_statement;
6532             parser->in_statement |= IN_CILK_SPAWN;
6533           }
6534         cfun->calls_cilk_spawn = 1;
6535         postfix_expression = 
6536           cp_parser_postfix_expression (parser, false, false, 
6537                                         false, false, &idk);
6538         if (!flag_cilkplus)
6539           {
6540             error_at (token->location, "-fcilkplus must be enabled to use"
6541                       " %<_Cilk_spawn%>");
6542             cfun->calls_cilk_spawn = 0;
6543           }
6544         else if (saved_in_statement & IN_CILK_SPAWN)
6545           {
6546             error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6547                       "are not permitted");
6548             postfix_expression = error_mark_node;
6549             cfun->calls_cilk_spawn = 0; 
6550           }
6551         else
6552           {
6553             location_t loc = postfix_expression.get_location ();
6554             postfix_expression = build_cilk_spawn (token->location, 
6555                                                    postfix_expression);
6556             /* Build a location of the form:
6557                  _Cilk_spawn expr
6558                  ~~~~~~~~~~~~^~~~
6559                with caret at the expr, ranging from the start of the
6560                _Cilk_spawn token to the end of the expression.  */
6561             location_t combined_loc =
6562               make_location (loc, cilk_spawn_loc, get_finish (loc));
6563             postfix_expression.set_location (combined_loc);
6564             if (postfix_expression != error_mark_node) 
6565               SET_EXPR_LOCATION (postfix_expression, input_location);
6566             parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6567           }
6568         break;
6569       }
6570
6571     case RID_BUILTIN_SHUFFLE:
6572       {
6573         vec<tree, va_gc> *vec;
6574         unsigned int i;
6575         tree p;
6576
6577         cp_lexer_consume_token (parser->lexer);
6578         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6579                     /*cast_p=*/false, /*allow_expansion_p=*/true,
6580                     /*non_constant_p=*/NULL);
6581         if (vec == NULL)
6582           {
6583             postfix_expression = error_mark_node;
6584             break;
6585           }
6586
6587         FOR_EACH_VEC_ELT (*vec, i, p)
6588           mark_exp_read (p);
6589
6590         if (vec->length () == 2)
6591           postfix_expression
6592             = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6593                                      tf_warning_or_error);
6594         else if (vec->length () == 3)
6595           postfix_expression
6596             = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6597                                      tf_warning_or_error);
6598         else
6599           {
6600             error_at (loc, "wrong number of arguments to "
6601                            "%<__builtin_shuffle%>");
6602             postfix_expression = error_mark_node;
6603           }
6604         break;
6605       }
6606
6607     default:
6608       {
6609         tree type;
6610
6611         /* If the next thing is a simple-type-specifier, we may be
6612            looking at a functional cast.  We could also be looking at
6613            an id-expression.  So, we try the functional cast, and if
6614            that doesn't work we fall back to the primary-expression.  */
6615         cp_parser_parse_tentatively (parser);
6616         /* Look for the simple-type-specifier.  */
6617         ++parser->prevent_constrained_type_specifiers;
6618         type = cp_parser_simple_type_specifier (parser,
6619                                                 /*decl_specs=*/NULL,
6620                                                 CP_PARSER_FLAGS_NONE);
6621         --parser->prevent_constrained_type_specifiers;
6622         /* Parse the cast itself.  */
6623         if (!cp_parser_error_occurred (parser))
6624           postfix_expression
6625             = cp_parser_functional_cast (parser, type);
6626         /* If that worked, we're done.  */
6627         if (cp_parser_parse_definitely (parser))
6628           break;
6629
6630         /* If the functional-cast didn't work out, try a
6631            compound-literal.  */
6632         if (cp_parser_allow_gnu_extensions_p (parser)
6633             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6634           {
6635             cp_expr initializer = NULL_TREE;
6636
6637             cp_parser_parse_tentatively (parser);
6638
6639             /* Avoid calling cp_parser_type_id pointlessly, see comment
6640                in cp_parser_cast_expression about c++/29234.  */
6641             if (!cp_parser_compound_literal_p (parser))
6642               cp_parser_simulate_error (parser);
6643             else
6644               {
6645                 /* Parse the type.  */
6646                 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6647                 parser->in_type_id_in_expr_p = true;
6648                 type = cp_parser_type_id (parser);
6649                 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6650                 /* Look for the `)'.  */
6651                 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6652               }
6653
6654             /* If things aren't going well, there's no need to
6655                keep going.  */
6656             if (!cp_parser_error_occurred (parser))
6657               {
6658                 bool non_constant_p;
6659                 /* Parse the brace-enclosed initializer list.  */
6660                 initializer = cp_parser_braced_list (parser,
6661                                                      &non_constant_p);
6662               }
6663             /* If that worked, we're definitely looking at a
6664                compound-literal expression.  */
6665             if (cp_parser_parse_definitely (parser))
6666               {
6667                 /* Warn the user that a compound literal is not
6668                    allowed in standard C++.  */
6669                 pedwarn (input_location, OPT_Wpedantic,
6670                          "ISO C++ forbids compound-literals");
6671                 /* For simplicity, we disallow compound literals in
6672                    constant-expressions.  We could
6673                    allow compound literals of integer type, whose
6674                    initializer was a constant, in constant
6675                    expressions.  Permitting that usage, as a further
6676                    extension, would not change the meaning of any
6677                    currently accepted programs.  (Of course, as
6678                    compound literals are not part of ISO C++, the
6679                    standard has nothing to say.)  */
6680                 if (cp_parser_non_integral_constant_expression (parser,
6681                                                                 NIC_NCC))
6682                   {
6683                     postfix_expression = error_mark_node;
6684                     break;
6685                   }
6686                 /* Form the representation of the compound-literal.  */
6687                 postfix_expression
6688                   = finish_compound_literal (type, initializer,
6689                                              tf_warning_or_error);
6690                 postfix_expression.set_location (initializer.get_location ());
6691                 break;
6692               }
6693           }
6694
6695         /* It must be a primary-expression.  */
6696         postfix_expression
6697           = cp_parser_primary_expression (parser, address_p, cast_p,
6698                                           /*template_arg_p=*/false,
6699                                           decltype_p,
6700                                           &idk);
6701       }
6702       break;
6703     }
6704
6705   /* Note that we don't need to worry about calling build_cplus_new on a
6706      class-valued CALL_EXPR in decltype when it isn't the end of the
6707      postfix-expression; unary_complex_lvalue will take care of that for
6708      all these cases.  */
6709
6710   /* Keep looping until the postfix-expression is complete.  */
6711   while (true)
6712     {
6713       if (idk == CP_ID_KIND_UNQUALIFIED
6714           && identifier_p (postfix_expression)
6715           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6716         /* It is not a Koenig lookup function call.  */
6717         postfix_expression
6718           = unqualified_name_lookup_error (postfix_expression);
6719
6720       /* Peek at the next token.  */
6721       token = cp_lexer_peek_token (parser->lexer);
6722
6723       switch (token->type)
6724         {
6725         case CPP_OPEN_SQUARE:
6726           if (cp_next_tokens_can_be_std_attribute_p (parser))
6727             {
6728               cp_parser_error (parser,
6729                                "two consecutive %<[%> shall "
6730                                "only introduce an attribute");
6731               return error_mark_node;
6732             }
6733           postfix_expression
6734             = cp_parser_postfix_open_square_expression (parser,
6735                                                         postfix_expression,
6736                                                         false,
6737                                                         decltype_p);
6738           postfix_expression.set_range (start_loc,
6739                                         postfix_expression.get_location ());
6740
6741           idk = CP_ID_KIND_NONE;
6742           is_member_access = false;
6743           break;
6744
6745         case CPP_OPEN_PAREN:
6746           /* postfix-expression ( expression-list [opt] ) */
6747           {
6748             bool koenig_p;
6749             bool is_builtin_constant_p;
6750             bool saved_integral_constant_expression_p = false;
6751             bool saved_non_integral_constant_expression_p = false;
6752             tsubst_flags_t complain = complain_flags (decltype_p);
6753             vec<tree, va_gc> *args;
6754             location_t close_paren_loc = UNKNOWN_LOCATION;
6755
6756             is_member_access = false;
6757
6758             is_builtin_constant_p
6759               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6760             if (is_builtin_constant_p)
6761               {
6762                 /* The whole point of __builtin_constant_p is to allow
6763                    non-constant expressions to appear as arguments.  */
6764                 saved_integral_constant_expression_p
6765                   = parser->integral_constant_expression_p;
6766                 saved_non_integral_constant_expression_p
6767                   = parser->non_integral_constant_expression_p;
6768                 parser->integral_constant_expression_p = false;
6769               }
6770             args = (cp_parser_parenthesized_expression_list
6771                     (parser, non_attr,
6772                      /*cast_p=*/false, /*allow_expansion_p=*/true,
6773                      /*non_constant_p=*/NULL,
6774                      /*close_paren_loc=*/&close_paren_loc));
6775             if (is_builtin_constant_p)
6776               {
6777                 parser->integral_constant_expression_p
6778                   = saved_integral_constant_expression_p;
6779                 parser->non_integral_constant_expression_p
6780                   = saved_non_integral_constant_expression_p;
6781               }
6782
6783             if (args == NULL)
6784               {
6785                 postfix_expression = error_mark_node;
6786                 break;
6787               }
6788
6789             /* Function calls are not permitted in
6790                constant-expressions.  */
6791             if (! builtin_valid_in_constant_expr_p (postfix_expression)
6792                 && cp_parser_non_integral_constant_expression (parser,
6793                                                                NIC_FUNC_CALL))
6794               {
6795                 postfix_expression = error_mark_node;
6796                 release_tree_vector (args);
6797                 break;
6798               }
6799
6800             koenig_p = false;
6801             if (idk == CP_ID_KIND_UNQUALIFIED
6802                 || idk == CP_ID_KIND_TEMPLATE_ID)
6803               {
6804                 if (identifier_p (postfix_expression))
6805                   {
6806                     if (!args->is_empty ())
6807                       {
6808                         koenig_p = true;
6809                         if (!any_type_dependent_arguments_p (args))
6810                           postfix_expression
6811                             = perform_koenig_lookup (postfix_expression, args,
6812                                                      complain);
6813                       }
6814                     else
6815                       postfix_expression
6816                         = unqualified_fn_lookup_error (postfix_expression);
6817                   }
6818                 /* We do not perform argument-dependent lookup if
6819                    normal lookup finds a non-function, in accordance
6820                    with the expected resolution of DR 218.  */
6821                 else if (!args->is_empty ()
6822                          && is_overloaded_fn (postfix_expression))
6823                   {
6824                     tree fn = get_first_fn (postfix_expression);
6825                     fn = STRIP_TEMPLATE (fn);
6826
6827                     /* Do not do argument dependent lookup if regular
6828                        lookup finds a member function or a block-scope
6829                        function declaration.  [basic.lookup.argdep]/3  */
6830                     if (!DECL_FUNCTION_MEMBER_P (fn)
6831                         && !DECL_LOCAL_FUNCTION_P (fn))
6832                       {
6833                         koenig_p = true;
6834                         if (!any_type_dependent_arguments_p (args))
6835                           postfix_expression
6836                             = perform_koenig_lookup (postfix_expression, args,
6837                                                      complain);
6838                       }
6839                   }
6840               }
6841
6842             if (warn_memset_transposed_args)
6843               {
6844                 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6845                     && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6846                     && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6847                     && vec_safe_length (args) == 3
6848                     && TREE_CODE ((*args)[2]) == INTEGER_CST
6849                     && integer_zerop ((*args)[2])
6850                     && !(TREE_CODE ((*args)[1]) == INTEGER_CST
6851                          && integer_zerop ((*args)[1])))
6852                   warning (OPT_Wmemset_transposed_args,
6853                            "%<memset%> used with constant zero length "
6854                            "parameter; this could be due to transposed "
6855                            "parameters");
6856               }
6857
6858             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6859               {
6860                 tree instance = TREE_OPERAND (postfix_expression, 0);
6861                 tree fn = TREE_OPERAND (postfix_expression, 1);
6862
6863                 if (processing_template_decl
6864                     && (type_dependent_expression_p (instance)
6865                         || (!BASELINK_P (fn)
6866                             && TREE_CODE (fn) != FIELD_DECL)
6867                         || type_dependent_expression_p (fn)
6868                         || any_type_dependent_arguments_p (args)))
6869                   {
6870                     postfix_expression
6871                       = build_nt_call_vec (postfix_expression, args);
6872                     release_tree_vector (args);
6873                     break;
6874                   }
6875
6876                 if (BASELINK_P (fn))
6877                   {
6878                   postfix_expression
6879                     = (build_new_method_call
6880                        (instance, fn, &args, NULL_TREE,
6881                         (idk == CP_ID_KIND_QUALIFIED
6882                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6883                          : LOOKUP_NORMAL),
6884                         /*fn_p=*/NULL,
6885                         complain));
6886                   }
6887                 else
6888                   postfix_expression
6889                     = finish_call_expr (postfix_expression, &args,
6890                                         /*disallow_virtual=*/false,
6891                                         /*koenig_p=*/false,
6892                                         complain);
6893               }
6894             else if (TREE_CODE (postfix_expression) == OFFSET_REF
6895                      || TREE_CODE (postfix_expression) == MEMBER_REF
6896                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6897               postfix_expression = (build_offset_ref_call_from_tree
6898                                     (postfix_expression, &args,
6899                                      complain));
6900             else if (idk == CP_ID_KIND_QUALIFIED)
6901               /* A call to a static class member, or a namespace-scope
6902                  function.  */
6903               postfix_expression
6904                 = finish_call_expr (postfix_expression, &args,
6905                                     /*disallow_virtual=*/true,
6906                                     koenig_p,
6907                                     complain);
6908             else
6909               /* All other function calls.  */
6910               postfix_expression
6911                 = finish_call_expr (postfix_expression, &args,
6912                                     /*disallow_virtual=*/false,
6913                                     koenig_p,
6914                                     complain);
6915
6916             if (close_paren_loc != UNKNOWN_LOCATION)
6917               {
6918                 location_t combined_loc = make_location (token->location,
6919                                                          start_loc,
6920                                                          close_paren_loc);
6921                 postfix_expression.set_location (combined_loc);
6922               }
6923
6924             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
6925             idk = CP_ID_KIND_NONE;
6926
6927             release_tree_vector (args);
6928           }
6929           break;
6930
6931         case CPP_DOT:
6932         case CPP_DEREF:
6933           /* postfix-expression . template [opt] id-expression
6934              postfix-expression . pseudo-destructor-name
6935              postfix-expression -> template [opt] id-expression
6936              postfix-expression -> pseudo-destructor-name */
6937
6938           /* Consume the `.' or `->' operator.  */
6939           cp_lexer_consume_token (parser->lexer);
6940
6941           postfix_expression
6942             = cp_parser_postfix_dot_deref_expression (parser, token->type,
6943                                                       postfix_expression,
6944                                                       false, &idk, loc);
6945
6946           is_member_access = true;
6947           break;
6948
6949         case CPP_PLUS_PLUS:
6950           /* postfix-expression ++  */
6951           /* Consume the `++' token.  */
6952           cp_lexer_consume_token (parser->lexer);
6953           /* Generate a representation for the complete expression.  */
6954           postfix_expression
6955             = finish_increment_expr (postfix_expression,
6956                                      POSTINCREMENT_EXPR);
6957           /* Increments may not appear in constant-expressions.  */
6958           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6959             postfix_expression = error_mark_node;
6960           idk = CP_ID_KIND_NONE;
6961           is_member_access = false;
6962           break;
6963
6964         case CPP_MINUS_MINUS:
6965           /* postfix-expression -- */
6966           /* Consume the `--' token.  */
6967           cp_lexer_consume_token (parser->lexer);
6968           /* Generate a representation for the complete expression.  */
6969           postfix_expression
6970             = finish_increment_expr (postfix_expression,
6971                                      POSTDECREMENT_EXPR);
6972           /* Decrements may not appear in constant-expressions.  */
6973           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6974             postfix_expression = error_mark_node;
6975           idk = CP_ID_KIND_NONE;
6976           is_member_access = false;
6977           break;
6978
6979         default:
6980           if (pidk_return != NULL)
6981             * pidk_return = idk;
6982           if (member_access_only_p)
6983             return is_member_access
6984               ? postfix_expression
6985               : cp_expr (error_mark_node);
6986           else
6987             return postfix_expression;
6988         }
6989     }
6990
6991   /* We should never get here.  */
6992   gcc_unreachable ();
6993   return error_mark_node;
6994 }
6995
6996 /* This function parses Cilk Plus array notations.  If a normal array expr. is
6997    parsed then the array index is passed back to the caller through *INIT_INDEX 
6998    and the function returns a NULL_TREE.  If array notation expr. is parsed, 
6999    then *INIT_INDEX is ignored by the caller and the function returns 
7000    a tree of type ARRAY_NOTATION_REF.  If some error occurred it returns 
7001    error_mark_node.  */
7002
7003 static tree
7004 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7005                           tree array_value)
7006 {
7007   cp_token *token = NULL;
7008   tree length_index, stride = NULL_TREE, value_tree, array_type;
7009   if (!array_value || array_value == error_mark_node)
7010     {
7011       cp_parser_skip_to_end_of_statement (parser);
7012       return error_mark_node;
7013     }
7014
7015   array_type = TREE_TYPE (array_value);
7016   
7017   bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7018   parser->colon_corrects_to_scope_p = false;
7019   token = cp_lexer_peek_token (parser->lexer);
7020   
7021   if (!token)
7022     {
7023       cp_parser_error (parser, "expected %<:%> or numeral");
7024       return error_mark_node;
7025     }
7026   else if (token->type == CPP_COLON)
7027     {
7028       /* Consume the ':'.  */
7029       cp_lexer_consume_token (parser->lexer);
7030       
7031       /* If we are here, then we have a case like this A[:].  */
7032       if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7033         {
7034           cp_parser_error (parser, "expected %<]%>");
7035           cp_parser_skip_to_end_of_statement (parser);
7036           return error_mark_node;
7037         }
7038       *init_index = NULL_TREE;
7039       stride = NULL_TREE;
7040       length_index = NULL_TREE;
7041     }
7042   else
7043     {
7044       /* If we are here, then there are three valid possibilities:
7045          1. ARRAY [ EXP ]
7046          2. ARRAY [ EXP : EXP ]
7047          3. ARRAY [ EXP : EXP : EXP ]  */
7048
7049       *init_index = cp_parser_expression (parser);
7050       if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7051         {  
7052           /* This indicates that we have a normal array expression.  */
7053           parser->colon_corrects_to_scope_p = saved_colon_corrects;
7054           return NULL_TREE;
7055         }
7056       
7057       /* Consume the ':'.  */
7058       cp_lexer_consume_token (parser->lexer);
7059       length_index = cp_parser_expression (parser);
7060       if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7061         {
7062           cp_lexer_consume_token (parser->lexer);
7063           stride = cp_parser_expression (parser);
7064         }
7065     }
7066   parser->colon_corrects_to_scope_p = saved_colon_corrects;
7067
7068   if (*init_index == error_mark_node || length_index == error_mark_node
7069       || stride == error_mark_node || array_type == error_mark_node)
7070     {
7071       if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7072         cp_lexer_consume_token (parser->lexer);
7073       return error_mark_node;
7074     }
7075   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7076
7077   value_tree = build_array_notation_ref (loc, array_value, *init_index, 
7078                                          length_index, stride, array_type);
7079   return value_tree;
7080 }
7081
7082 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7083    by cp_parser_builtin_offsetof.  We're looking for
7084
7085      postfix-expression [ expression ]
7086      postfix-expression [ braced-init-list ] (C++11)
7087
7088    FOR_OFFSETOF is set if we're being called in that context, which
7089    changes how we deal with integer constant expressions.  */
7090
7091 static tree
7092 cp_parser_postfix_open_square_expression (cp_parser *parser,
7093                                           tree postfix_expression,
7094                                           bool for_offsetof,
7095                                           bool decltype_p)
7096 {
7097   tree index = NULL_TREE;
7098   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7099   bool saved_greater_than_is_operator_p;
7100
7101   /* Consume the `[' token.  */
7102   cp_lexer_consume_token (parser->lexer);
7103
7104   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7105   parser->greater_than_is_operator_p = true;
7106
7107   /* Parse the index expression.  */
7108   /* ??? For offsetof, there is a question of what to allow here.  If
7109      offsetof is not being used in an integral constant expression context,
7110      then we *could* get the right answer by computing the value at runtime.
7111      If we are in an integral constant expression context, then we might
7112      could accept any constant expression; hard to say without analysis.
7113      Rather than open the barn door too wide right away, allow only integer
7114      constant expressions here.  */
7115   if (for_offsetof)
7116     index = cp_parser_constant_expression (parser);
7117   else
7118     {
7119       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7120         {
7121           bool expr_nonconst_p;
7122           cp_lexer_set_source_position (parser->lexer);
7123           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7124           index = cp_parser_braced_list (parser, &expr_nonconst_p);
7125           if (flag_cilkplus
7126               && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7127             {
7128               error_at (cp_lexer_peek_token (parser->lexer)->location,
7129                         "braced list index is not allowed with array "
7130                         "notation");
7131               cp_parser_skip_to_end_of_statement (parser);
7132               return error_mark_node;
7133             }
7134         }
7135       else if (flag_cilkplus)
7136         {
7137           /* Here are have these two options:
7138              ARRAY[EXP : EXP]        - Array notation expr with default
7139              stride of 1.
7140              ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7141              stride.  */
7142           tree an_exp = cp_parser_array_notation (loc, parser, &index, 
7143                                                   postfix_expression);
7144           if (an_exp)
7145             return an_exp;
7146         }
7147       else
7148         index = cp_parser_expression (parser);
7149     }
7150
7151   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7152
7153   /* Look for the closing `]'.  */
7154   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7155
7156   /* Build the ARRAY_REF.  */
7157   postfix_expression = grok_array_decl (loc, postfix_expression,
7158                                         index, decltype_p);
7159
7160   /* When not doing offsetof, array references are not permitted in
7161      constant-expressions.  */
7162   if (!for_offsetof
7163       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7164     postfix_expression = error_mark_node;
7165
7166   return postfix_expression;
7167 }
7168
7169 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7170    by cp_parser_builtin_offsetof.  We're looking for
7171
7172      postfix-expression . template [opt] id-expression
7173      postfix-expression . pseudo-destructor-name
7174      postfix-expression -> template [opt] id-expression
7175      postfix-expression -> pseudo-destructor-name
7176
7177    FOR_OFFSETOF is set if we're being called in that context.  That sorta
7178    limits what of the above we'll actually accept, but nevermind.
7179    TOKEN_TYPE is the "." or "->" token, which will already have been
7180    removed from the stream.  */
7181
7182 static tree
7183 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7184                                         enum cpp_ttype token_type,
7185                                         cp_expr postfix_expression,
7186                                         bool for_offsetof, cp_id_kind *idk,
7187                                         location_t location)
7188 {
7189   tree name;
7190   bool dependent_p;
7191   bool pseudo_destructor_p;
7192   tree scope = NULL_TREE;
7193   location_t start_loc = postfix_expression.get_start ();
7194
7195   /* If this is a `->' operator, dereference the pointer.  */
7196   if (token_type == CPP_DEREF)
7197     postfix_expression = build_x_arrow (location, postfix_expression,
7198                                         tf_warning_or_error);
7199   /* Check to see whether or not the expression is type-dependent.  */
7200   dependent_p = type_dependent_expression_p (postfix_expression);
7201   /* The identifier following the `->' or `.' is not qualified.  */
7202   parser->scope = NULL_TREE;
7203   parser->qualifying_scope = NULL_TREE;
7204   parser->object_scope = NULL_TREE;
7205   *idk = CP_ID_KIND_NONE;
7206
7207   /* Enter the scope corresponding to the type of the object
7208      given by the POSTFIX_EXPRESSION.  */
7209   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7210     {
7211       scope = TREE_TYPE (postfix_expression);
7212       /* According to the standard, no expression should ever have
7213          reference type.  Unfortunately, we do not currently match
7214          the standard in this respect in that our internal representation
7215          of an expression may have reference type even when the standard
7216          says it does not.  Therefore, we have to manually obtain the
7217          underlying type here.  */
7218       scope = non_reference (scope);
7219       /* The type of the POSTFIX_EXPRESSION must be complete.  */
7220       if (scope == unknown_type_node)
7221         {
7222           error_at (location, "%qE does not have class type",
7223                     postfix_expression.get_value ());
7224           scope = NULL_TREE;
7225         }
7226       /* Unlike the object expression in other contexts, *this is not
7227          required to be of complete type for purposes of class member
7228          access (5.2.5) outside the member function body.  */
7229       else if (postfix_expression != current_class_ref
7230                && !(processing_template_decl && scope == current_class_type))
7231         scope = complete_type_or_else (scope, NULL_TREE);
7232       /* Let the name lookup machinery know that we are processing a
7233          class member access expression.  */
7234       parser->context->object_type = scope;
7235       /* If something went wrong, we want to be able to discern that case,
7236          as opposed to the case where there was no SCOPE due to the type
7237          of expression being dependent.  */
7238       if (!scope)
7239         scope = error_mark_node;
7240       /* If the SCOPE was erroneous, make the various semantic analysis
7241          functions exit quickly -- and without issuing additional error
7242          messages.  */
7243       if (scope == error_mark_node)
7244         postfix_expression = error_mark_node;
7245     }
7246   else
7247     /* Tell cp_parser_lookup_name that there was an object, even though it's
7248        type-dependent.  */
7249     parser->context->object_type = unknown_type_node;
7250
7251   /* Assume this expression is not a pseudo-destructor access.  */
7252   pseudo_destructor_p = false;
7253
7254   /* If the SCOPE is a scalar type, then, if this is a valid program,
7255      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
7256      is type dependent, it can be pseudo-destructor-name or something else.
7257      Try to parse it as pseudo-destructor-name first.  */
7258   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7259     {
7260       tree s;
7261       tree type;
7262
7263       cp_parser_parse_tentatively (parser);
7264       /* Parse the pseudo-destructor-name.  */
7265       s = NULL_TREE;
7266       cp_parser_pseudo_destructor_name (parser, postfix_expression,
7267                                         &s, &type);
7268       if (dependent_p
7269           && (cp_parser_error_occurred (parser)
7270               || !SCALAR_TYPE_P (type)))
7271         cp_parser_abort_tentative_parse (parser);
7272       else if (cp_parser_parse_definitely (parser))
7273         {
7274           pseudo_destructor_p = true;
7275           postfix_expression
7276             = finish_pseudo_destructor_expr (postfix_expression,
7277                                              s, type, location);
7278         }
7279     }
7280
7281   if (!pseudo_destructor_p)
7282     {
7283       /* If the SCOPE is not a scalar type, we are looking at an
7284          ordinary class member access expression, rather than a
7285          pseudo-destructor-name.  */
7286       bool template_p;
7287       cp_token *token = cp_lexer_peek_token (parser->lexer);
7288       /* Parse the id-expression.  */
7289       name = (cp_parser_id_expression
7290               (parser,
7291                cp_parser_optional_template_keyword (parser),
7292                /*check_dependency_p=*/true,
7293                &template_p,
7294                /*declarator_p=*/false,
7295                /*optional_p=*/false));
7296       /* In general, build a SCOPE_REF if the member name is qualified.
7297          However, if the name was not dependent and has already been
7298          resolved; there is no need to build the SCOPE_REF.  For example;
7299
7300              struct X { void f(); };
7301              template <typename T> void f(T* t) { t->X::f(); }
7302
7303          Even though "t" is dependent, "X::f" is not and has been resolved
7304          to a BASELINK; there is no need to include scope information.  */
7305
7306       /* But we do need to remember that there was an explicit scope for
7307          virtual function calls.  */
7308       if (parser->scope)
7309         *idk = CP_ID_KIND_QUALIFIED;
7310
7311       /* If the name is a template-id that names a type, we will get a
7312          TYPE_DECL here.  That is invalid code.  */
7313       if (TREE_CODE (name) == TYPE_DECL)
7314         {
7315           error_at (token->location, "invalid use of %qD", name);
7316           postfix_expression = error_mark_node;
7317         }
7318       else
7319         {
7320           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7321             {
7322               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7323                 {
7324                   error_at (token->location, "%<%D::%D%> is not a class member",
7325                             parser->scope, name);
7326                   postfix_expression = error_mark_node;
7327                 }
7328               else
7329                 name = build_qualified_name (/*type=*/NULL_TREE,
7330                                              parser->scope,
7331                                              name,
7332                                              template_p);
7333               parser->scope = NULL_TREE;
7334               parser->qualifying_scope = NULL_TREE;
7335               parser->object_scope = NULL_TREE;
7336             }
7337           if (parser->scope && name && BASELINK_P (name))
7338             adjust_result_of_qualified_name_lookup
7339               (name, parser->scope, scope);
7340           postfix_expression
7341             = finish_class_member_access_expr (postfix_expression, name,
7342                                                template_p, 
7343                                                tf_warning_or_error);
7344           /* Build a location e.g.:
7345                ptr->access_expr
7346                ~~~^~~~~~~~~~~~~
7347              where the caret is at the deref token, ranging from
7348              the start of postfix_expression to the end of the access expr.  */
7349           location_t end_loc
7350             = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7351           location_t combined_loc
7352             = make_location (input_location, start_loc, end_loc);
7353           protected_set_expr_location (postfix_expression, combined_loc);
7354         }
7355     }
7356
7357   /* We no longer need to look up names in the scope of the object on
7358      the left-hand side of the `.' or `->' operator.  */
7359   parser->context->object_type = NULL_TREE;
7360
7361   /* Outside of offsetof, these operators may not appear in
7362      constant-expressions.  */
7363   if (!for_offsetof
7364       && (cp_parser_non_integral_constant_expression
7365           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7366     postfix_expression = error_mark_node;
7367
7368   return postfix_expression;
7369 }
7370
7371 /* Parse a parenthesized expression-list.
7372
7373    expression-list:
7374      assignment-expression
7375      expression-list, assignment-expression
7376
7377    attribute-list:
7378      expression-list
7379      identifier
7380      identifier, expression-list
7381
7382    CAST_P is true if this expression is the target of a cast.
7383
7384    ALLOW_EXPANSION_P is true if this expression allows expansion of an
7385    argument pack.
7386
7387    Returns a vector of trees.  Each element is a representation of an
7388    assignment-expression.  NULL is returned if the ( and or ) are
7389    missing.  An empty, but allocated, vector is returned on no
7390    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
7391    if we are parsing an attribute list for an attribute that wants a
7392    plain identifier argument, normal_attr for an attribute that wants
7393    an expression, or non_attr if we aren't parsing an attribute list.  If
7394    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7395    not all of the expressions in the list were constant.
7396    If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7397    will be written to with the location of the closing parenthesis.  If
7398    an error occurs, it may or may not be written to.  */
7399
7400 static vec<tree, va_gc> *
7401 cp_parser_parenthesized_expression_list (cp_parser* parser,
7402                                          int is_attribute_list,
7403                                          bool cast_p,
7404                                          bool allow_expansion_p,
7405                                          bool *non_constant_p,
7406                                          location_t *close_paren_loc)
7407 {
7408   vec<tree, va_gc> *expression_list;
7409   bool fold_expr_p = is_attribute_list != non_attr;
7410   tree identifier = NULL_TREE;
7411   bool saved_greater_than_is_operator_p;
7412
7413   /* Assume all the expressions will be constant.  */
7414   if (non_constant_p)
7415     *non_constant_p = false;
7416
7417   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7418     return NULL;
7419
7420   expression_list = make_tree_vector ();
7421
7422   /* Within a parenthesized expression, a `>' token is always
7423      the greater-than operator.  */
7424   saved_greater_than_is_operator_p
7425     = parser->greater_than_is_operator_p;
7426   parser->greater_than_is_operator_p = true;
7427
7428   /* Consume expressions until there are no more.  */
7429   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7430     while (true)
7431       {
7432         tree expr;
7433
7434         /* At the beginning of attribute lists, check to see if the
7435            next token is an identifier.  */
7436         if (is_attribute_list == id_attr
7437             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7438           {
7439             cp_token *token;
7440
7441             /* Consume the identifier.  */
7442             token = cp_lexer_consume_token (parser->lexer);
7443             /* Save the identifier.  */
7444             identifier = token->u.value;
7445           }
7446         else
7447           {
7448             bool expr_non_constant_p;
7449
7450             /* Parse the next assignment-expression.  */
7451             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7452               {
7453                 /* A braced-init-list.  */
7454                 cp_lexer_set_source_position (parser->lexer);
7455                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7456                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7457                 if (non_constant_p && expr_non_constant_p)
7458                   *non_constant_p = true;
7459               }
7460             else if (non_constant_p)
7461               {
7462                 expr = (cp_parser_constant_expression
7463                         (parser, /*allow_non_constant_p=*/true,
7464                          &expr_non_constant_p));
7465                 if (expr_non_constant_p)
7466                   *non_constant_p = true;
7467               }
7468             else
7469               expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7470                                                       cast_p);
7471
7472             if (fold_expr_p)
7473               expr = instantiate_non_dependent_expr (expr);
7474
7475             /* If we have an ellipsis, then this is an expression
7476                expansion.  */
7477             if (allow_expansion_p
7478                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7479               {
7480                 /* Consume the `...'.  */
7481                 cp_lexer_consume_token (parser->lexer);
7482
7483                 /* Build the argument pack.  */
7484                 expr = make_pack_expansion (expr);
7485               }
7486
7487              /* Add it to the list.  We add error_mark_node
7488                 expressions to the list, so that we can still tell if
7489                 the correct form for a parenthesized expression-list
7490                 is found. That gives better errors.  */
7491             vec_safe_push (expression_list, expr);
7492
7493             if (expr == error_mark_node)
7494               goto skip_comma;
7495           }
7496
7497         /* After the first item, attribute lists look the same as
7498            expression lists.  */
7499         is_attribute_list = non_attr;
7500
7501       get_comma:;
7502         /* If the next token isn't a `,', then we are done.  */
7503         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7504           break;
7505
7506         /* Otherwise, consume the `,' and keep going.  */
7507         cp_lexer_consume_token (parser->lexer);
7508       }
7509
7510   if (close_paren_loc)
7511     *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7512
7513   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7514     {
7515       int ending;
7516
7517     skip_comma:;
7518       /* We try and resync to an unnested comma, as that will give the
7519          user better diagnostics.  */
7520       ending = cp_parser_skip_to_closing_parenthesis (parser,
7521                                                       /*recovering=*/true,
7522                                                       /*or_comma=*/true,
7523                                                       /*consume_paren=*/true);
7524       if (ending < 0)
7525         goto get_comma;
7526       if (!ending)
7527         {
7528           parser->greater_than_is_operator_p
7529             = saved_greater_than_is_operator_p;
7530           return NULL;
7531         }
7532     }
7533
7534   parser->greater_than_is_operator_p
7535     = saved_greater_than_is_operator_p;
7536
7537   if (identifier)
7538     vec_safe_insert (expression_list, 0, identifier);
7539
7540   return expression_list;
7541 }
7542
7543 /* Parse a pseudo-destructor-name.
7544
7545    pseudo-destructor-name:
7546      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7547      :: [opt] nested-name-specifier template template-id :: ~ type-name
7548      :: [opt] nested-name-specifier [opt] ~ type-name
7549
7550    If either of the first two productions is used, sets *SCOPE to the
7551    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7552    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7553    or ERROR_MARK_NODE if the parse fails.  */
7554
7555 static void
7556 cp_parser_pseudo_destructor_name (cp_parser* parser,
7557                                   tree object,
7558                                   tree* scope,
7559                                   tree* type)
7560 {
7561   bool nested_name_specifier_p;
7562
7563   /* Handle ~auto.  */
7564   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7565       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7566       && !type_dependent_expression_p (object))
7567     {
7568       if (cxx_dialect < cxx14)
7569         pedwarn (input_location, 0,
7570                  "%<~auto%> only available with "
7571                  "-std=c++14 or -std=gnu++14");
7572       cp_lexer_consume_token (parser->lexer);
7573       cp_lexer_consume_token (parser->lexer);
7574       *scope = NULL_TREE;
7575       *type = TREE_TYPE (object);
7576       return;
7577     }
7578
7579   /* Assume that things will not work out.  */
7580   *type = error_mark_node;
7581
7582   /* Look for the optional `::' operator.  */
7583   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7584   /* Look for the optional nested-name-specifier.  */
7585   nested_name_specifier_p
7586     = (cp_parser_nested_name_specifier_opt (parser,
7587                                             /*typename_keyword_p=*/false,
7588                                             /*check_dependency_p=*/true,
7589                                             /*type_p=*/false,
7590                                             /*is_declaration=*/false)
7591        != NULL_TREE);
7592   /* Now, if we saw a nested-name-specifier, we might be doing the
7593      second production.  */
7594   if (nested_name_specifier_p
7595       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7596     {
7597       /* Consume the `template' keyword.  */
7598       cp_lexer_consume_token (parser->lexer);
7599       /* Parse the template-id.  */
7600       cp_parser_template_id (parser,
7601                              /*template_keyword_p=*/true,
7602                              /*check_dependency_p=*/false,
7603                              class_type,
7604                              /*is_declaration=*/true);
7605       /* Look for the `::' token.  */
7606       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7607     }
7608   /* If the next token is not a `~', then there might be some
7609      additional qualification.  */
7610   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7611     {
7612       /* At this point, we're looking for "type-name :: ~".  The type-name
7613          must not be a class-name, since this is a pseudo-destructor.  So,
7614          it must be either an enum-name, or a typedef-name -- both of which
7615          are just identifiers.  So, we peek ahead to check that the "::"
7616          and "~" tokens are present; if they are not, then we can avoid
7617          calling type_name.  */
7618       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7619           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7620           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7621         {
7622           cp_parser_error (parser, "non-scalar type");
7623           return;
7624         }
7625
7626       /* Look for the type-name.  */
7627       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7628       if (*scope == error_mark_node)
7629         return;
7630
7631       /* Look for the `::' token.  */
7632       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7633     }
7634   else
7635     *scope = NULL_TREE;
7636
7637   /* Look for the `~'.  */
7638   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7639
7640   /* Once we see the ~, this has to be a pseudo-destructor.  */
7641   if (!processing_template_decl && !cp_parser_error_occurred (parser))
7642     cp_parser_commit_to_topmost_tentative_parse (parser);
7643
7644   /* Look for the type-name again.  We are not responsible for
7645      checking that it matches the first type-name.  */
7646   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7647 }
7648
7649 /* Parse a unary-expression.
7650
7651    unary-expression:
7652      postfix-expression
7653      ++ cast-expression
7654      -- cast-expression
7655      unary-operator cast-expression
7656      sizeof unary-expression
7657      sizeof ( type-id )
7658      alignof ( type-id )  [C++0x]
7659      new-expression
7660      delete-expression
7661
7662    GNU Extensions:
7663
7664    unary-expression:
7665      __extension__ cast-expression
7666      __alignof__ unary-expression
7667      __alignof__ ( type-id )
7668      alignof unary-expression  [C++0x]
7669      __real__ cast-expression
7670      __imag__ cast-expression
7671      && identifier
7672      sizeof ( type-id ) { initializer-list , [opt] }
7673      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7674      __alignof__ ( type-id ) { initializer-list , [opt] }
7675
7676    ADDRESS_P is true iff the unary-expression is appearing as the
7677    operand of the `&' operator.   CAST_P is true if this expression is
7678    the target of a cast.
7679
7680    Returns a representation of the expression.  */
7681
7682 static cp_expr
7683 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7684                             bool address_p, bool cast_p, bool decltype_p)
7685 {
7686   cp_token *token;
7687   enum tree_code unary_operator;
7688
7689   /* Peek at the next token.  */
7690   token = cp_lexer_peek_token (parser->lexer);
7691   /* Some keywords give away the kind of expression.  */
7692   if (token->type == CPP_KEYWORD)
7693     {
7694       enum rid keyword = token->keyword;
7695
7696       switch (keyword)
7697         {
7698         case RID_ALIGNOF:
7699         case RID_SIZEOF:
7700           {
7701             tree operand, ret;
7702             enum tree_code op;
7703             location_t first_loc;
7704
7705             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7706             /* Consume the token.  */
7707             cp_lexer_consume_token (parser->lexer);
7708             first_loc = cp_lexer_peek_token (parser->lexer)->location;
7709             /* Parse the operand.  */
7710             operand = cp_parser_sizeof_operand (parser, keyword);
7711
7712             if (TYPE_P (operand))
7713               ret = cxx_sizeof_or_alignof_type (operand, op, true);
7714             else
7715               {
7716                 /* ISO C++ defines alignof only with types, not with
7717                    expressions. So pedwarn if alignof is used with a non-
7718                    type expression. However, __alignof__ is ok.  */
7719                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7720                   pedwarn (token->location, OPT_Wpedantic,
7721                            "ISO C++ does not allow %<alignof%> "
7722                            "with a non-type");
7723
7724                 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7725               }
7726             /* For SIZEOF_EXPR, just issue diagnostics, but keep
7727                SIZEOF_EXPR with the original operand.  */
7728             if (op == SIZEOF_EXPR && ret != error_mark_node)
7729               {
7730                 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7731                   {
7732                     if (!processing_template_decl && TYPE_P (operand))
7733                       {
7734                         ret = build_min (SIZEOF_EXPR, size_type_node,
7735                                          build1 (NOP_EXPR, operand,
7736                                                  error_mark_node));
7737                         SIZEOF_EXPR_TYPE_P (ret) = 1;
7738                       }
7739                     else
7740                       ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7741                     TREE_SIDE_EFFECTS (ret) = 0;
7742                     TREE_READONLY (ret) = 1;
7743                   }
7744                 SET_EXPR_LOCATION (ret, first_loc);
7745               }
7746             return ret;
7747           }
7748
7749         case RID_NEW:
7750           return cp_parser_new_expression (parser);
7751
7752         case RID_DELETE:
7753           return cp_parser_delete_expression (parser);
7754
7755         case RID_EXTENSION:
7756           {
7757             /* The saved value of the PEDANTIC flag.  */
7758             int saved_pedantic;
7759             tree expr;
7760
7761             /* Save away the PEDANTIC flag.  */
7762             cp_parser_extension_opt (parser, &saved_pedantic);
7763             /* Parse the cast-expression.  */
7764             expr = cp_parser_simple_cast_expression (parser);
7765             /* Restore the PEDANTIC flag.  */
7766             pedantic = saved_pedantic;
7767
7768             return expr;
7769           }
7770
7771         case RID_REALPART:
7772         case RID_IMAGPART:
7773           {
7774             tree expression;
7775
7776             /* Consume the `__real__' or `__imag__' token.  */
7777             cp_lexer_consume_token (parser->lexer);
7778             /* Parse the cast-expression.  */
7779             expression = cp_parser_simple_cast_expression (parser);
7780             /* Create the complete representation.  */
7781             return build_x_unary_op (token->location,
7782                                      (keyword == RID_REALPART
7783                                       ? REALPART_EXPR : IMAGPART_EXPR),
7784                                      expression,
7785                                      tf_warning_or_error);
7786           }
7787           break;
7788
7789         case RID_TRANSACTION_ATOMIC:
7790         case RID_TRANSACTION_RELAXED:
7791           return cp_parser_transaction_expression (parser, keyword);
7792
7793         case RID_NOEXCEPT:
7794           {
7795             tree expr;
7796             const char *saved_message;
7797             bool saved_integral_constant_expression_p;
7798             bool saved_non_integral_constant_expression_p;
7799             bool saved_greater_than_is_operator_p;
7800
7801             cp_lexer_consume_token (parser->lexer);
7802             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7803
7804             saved_message = parser->type_definition_forbidden_message;
7805             parser->type_definition_forbidden_message
7806               = G_("types may not be defined in %<noexcept%> expressions");
7807
7808             saved_integral_constant_expression_p
7809               = parser->integral_constant_expression_p;
7810             saved_non_integral_constant_expression_p
7811               = parser->non_integral_constant_expression_p;
7812             parser->integral_constant_expression_p = false;
7813
7814             saved_greater_than_is_operator_p
7815               = parser->greater_than_is_operator_p;
7816             parser->greater_than_is_operator_p = true;
7817
7818             ++cp_unevaluated_operand;
7819             ++c_inhibit_evaluation_warnings;
7820             ++cp_noexcept_operand;
7821             expr = cp_parser_expression (parser);
7822             --cp_noexcept_operand;
7823             --c_inhibit_evaluation_warnings;
7824             --cp_unevaluated_operand;
7825
7826             parser->greater_than_is_operator_p
7827               = saved_greater_than_is_operator_p;
7828
7829             parser->integral_constant_expression_p
7830               = saved_integral_constant_expression_p;
7831             parser->non_integral_constant_expression_p
7832               = saved_non_integral_constant_expression_p;
7833
7834             parser->type_definition_forbidden_message = saved_message;
7835
7836             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7837             return finish_noexcept_expr (expr, tf_warning_or_error);
7838           }
7839
7840         default:
7841           break;
7842         }
7843     }
7844
7845   /* Look for the `:: new' and `:: delete', which also signal the
7846      beginning of a new-expression, or delete-expression,
7847      respectively.  If the next token is `::', then it might be one of
7848      these.  */
7849   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7850     {
7851       enum rid keyword;
7852
7853       /* See if the token after the `::' is one of the keywords in
7854          which we're interested.  */
7855       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7856       /* If it's `new', we have a new-expression.  */
7857       if (keyword == RID_NEW)
7858         return cp_parser_new_expression (parser);
7859       /* Similarly, for `delete'.  */
7860       else if (keyword == RID_DELETE)
7861         return cp_parser_delete_expression (parser);
7862     }
7863
7864   /* Look for a unary operator.  */
7865   unary_operator = cp_parser_unary_operator (token);
7866   /* The `++' and `--' operators can be handled similarly, even though
7867      they are not technically unary-operators in the grammar.  */
7868   if (unary_operator == ERROR_MARK)
7869     {
7870       if (token->type == CPP_PLUS_PLUS)
7871         unary_operator = PREINCREMENT_EXPR;
7872       else if (token->type == CPP_MINUS_MINUS)
7873         unary_operator = PREDECREMENT_EXPR;
7874       /* Handle the GNU address-of-label extension.  */
7875       else if (cp_parser_allow_gnu_extensions_p (parser)
7876                && token->type == CPP_AND_AND)
7877         {
7878           tree identifier;
7879           tree expression;
7880           location_t start_loc = token->location;
7881
7882           /* Consume the '&&' token.  */
7883           cp_lexer_consume_token (parser->lexer);
7884           /* Look for the identifier.  */
7885           location_t finish_loc
7886             = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7887           identifier = cp_parser_identifier (parser);
7888           /* Construct a location of the form:
7889                &&label
7890                ^~~~~~~
7891              with caret==start at the "&&", finish at the end of the label.  */
7892           location_t combined_loc
7893             = make_location (start_loc, start_loc, finish_loc);
7894           /* Create an expression representing the address.  */
7895           expression = finish_label_address_expr (identifier, combined_loc);
7896           if (cp_parser_non_integral_constant_expression (parser,
7897                                                           NIC_ADDR_LABEL))
7898             expression = error_mark_node;
7899           return expression;
7900         }
7901     }
7902   if (unary_operator != ERROR_MARK)
7903     {
7904       cp_expr cast_expression;
7905       cp_expr expression = error_mark_node;
7906       non_integral_constant non_constant_p = NIC_NONE;
7907       location_t loc = token->location;
7908       tsubst_flags_t complain = complain_flags (decltype_p);
7909
7910       /* Consume the operator token.  */
7911       token = cp_lexer_consume_token (parser->lexer);
7912       enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7913
7914       /* Parse the cast-expression.  */
7915       cast_expression
7916         = cp_parser_cast_expression (parser,
7917                                      unary_operator == ADDR_EXPR,
7918                                      /*cast_p=*/false,
7919                                      /*decltype*/false,
7920                                      pidk);
7921
7922       /* Make a location:
7923             OP_TOKEN  CAST_EXPRESSION
7924             ^~~~~~~~~~~~~~~~~~~~~~~~~
7925          with start==caret at the operator token, and
7926          extending to the end of the cast_expression.  */
7927       loc = make_location (loc, loc, cast_expression.get_finish ());
7928
7929       /* Now, build an appropriate representation.  */
7930       switch (unary_operator)
7931         {
7932         case INDIRECT_REF:
7933           non_constant_p = NIC_STAR;
7934           expression = build_x_indirect_ref (loc, cast_expression,
7935                                              RO_UNARY_STAR,
7936                                              complain);
7937           /* TODO: build_x_indirect_ref does not always honor the
7938              location, so ensure it is set.  */
7939           expression.set_location (loc);
7940           break;
7941
7942         case ADDR_EXPR:
7943            non_constant_p = NIC_ADDR;
7944           /* Fall through.  */
7945         case BIT_NOT_EXPR:
7946           expression = build_x_unary_op (loc, unary_operator,
7947                                          cast_expression,
7948                                          complain);
7949           /* TODO: build_x_unary_op does not always honor the location,
7950              so ensure it is set.  */
7951           expression.set_location (loc);
7952           break;
7953
7954         case PREINCREMENT_EXPR:
7955         case PREDECREMENT_EXPR:
7956           non_constant_p = unary_operator == PREINCREMENT_EXPR
7957                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7958           /* Fall through.  */
7959         case NEGATE_EXPR:
7960           /* Immediately fold negation of a constant, unless the constant is 0
7961              (since -0 == 0) or it would overflow.  */
7962           if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7963               && CONSTANT_CLASS_P (cast_expression)
7964               && !integer_zerop (cast_expression)
7965               && !TREE_OVERFLOW (cast_expression))
7966             {
7967               tree folded = fold_build1 (unary_operator,
7968                                          TREE_TYPE (cast_expression),
7969                                          cast_expression);
7970               if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7971                 {
7972                   expression = cp_expr (folded, loc);
7973                   break;
7974                 }
7975             }
7976           /* Fall through.  */
7977         case UNARY_PLUS_EXPR:
7978         case TRUTH_NOT_EXPR:
7979           expression = finish_unary_op_expr (loc, unary_operator,
7980                                              cast_expression, complain);
7981           break;
7982
7983         default:
7984           gcc_unreachable ();
7985         }
7986
7987       if (non_constant_p != NIC_NONE
7988           && cp_parser_non_integral_constant_expression (parser,
7989                                                          non_constant_p))
7990         expression = error_mark_node;
7991
7992       return expression;
7993     }
7994
7995   return cp_parser_postfix_expression (parser, address_p, cast_p,
7996                                        /*member_access_only_p=*/false,
7997                                        decltype_p,
7998                                        pidk);
7999 }
8000
8001 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
8002    unary-operator, the corresponding tree code is returned.  */
8003
8004 static enum tree_code
8005 cp_parser_unary_operator (cp_token* token)
8006 {
8007   switch (token->type)
8008     {
8009     case CPP_MULT:
8010       return INDIRECT_REF;
8011
8012     case CPP_AND:
8013       return ADDR_EXPR;
8014
8015     case CPP_PLUS:
8016       return UNARY_PLUS_EXPR;
8017
8018     case CPP_MINUS:
8019       return NEGATE_EXPR;
8020
8021     case CPP_NOT:
8022       return TRUTH_NOT_EXPR;
8023
8024     case CPP_COMPL:
8025       return BIT_NOT_EXPR;
8026
8027     default:
8028       return ERROR_MARK;
8029     }
8030 }
8031
8032 /* Parse a new-expression.
8033
8034    new-expression:
8035      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8036      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8037
8038    Returns a representation of the expression.  */
8039
8040 static tree
8041 cp_parser_new_expression (cp_parser* parser)
8042 {
8043   bool global_scope_p;
8044   vec<tree, va_gc> *placement;
8045   tree type;
8046   vec<tree, va_gc> *initializer;
8047   tree nelts = NULL_TREE;
8048   tree ret;
8049
8050   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8051
8052   /* Look for the optional `::' operator.  */
8053   global_scope_p
8054     = (cp_parser_global_scope_opt (parser,
8055                                    /*current_scope_valid_p=*/false)
8056        != NULL_TREE);
8057   /* Look for the `new' operator.  */
8058   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8059   /* There's no easy way to tell a new-placement from the
8060      `( type-id )' construct.  */
8061   cp_parser_parse_tentatively (parser);
8062   /* Look for a new-placement.  */
8063   placement = cp_parser_new_placement (parser);
8064   /* If that didn't work out, there's no new-placement.  */
8065   if (!cp_parser_parse_definitely (parser))
8066     {
8067       if (placement != NULL)
8068         release_tree_vector (placement);
8069       placement = NULL;
8070     }
8071
8072   /* If the next token is a `(', then we have a parenthesized
8073      type-id.  */
8074   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8075     {
8076       cp_token *token;
8077       const char *saved_message = parser->type_definition_forbidden_message;
8078
8079       /* Consume the `('.  */
8080       cp_lexer_consume_token (parser->lexer);
8081
8082       /* Parse the type-id.  */
8083       parser->type_definition_forbidden_message
8084         = G_("types may not be defined in a new-expression");
8085       {
8086         type_id_in_expr_sentinel s (parser);
8087         type = cp_parser_type_id (parser);
8088       }
8089       parser->type_definition_forbidden_message = saved_message;
8090
8091       /* Look for the closing `)'.  */
8092       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8093       token = cp_lexer_peek_token (parser->lexer);
8094       /* There should not be a direct-new-declarator in this production,
8095          but GCC used to allowed this, so we check and emit a sensible error
8096          message for this case.  */
8097       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8098         {
8099           error_at (token->location,
8100                     "array bound forbidden after parenthesized type-id");
8101           inform (token->location, 
8102                   "try removing the parentheses around the type-id");
8103           cp_parser_direct_new_declarator (parser);
8104         }
8105     }
8106   /* Otherwise, there must be a new-type-id.  */
8107   else
8108     type = cp_parser_new_type_id (parser, &nelts);
8109
8110   /* If the next token is a `(' or '{', then we have a new-initializer.  */
8111   cp_token *token = cp_lexer_peek_token (parser->lexer);
8112   if (token->type == CPP_OPEN_PAREN
8113       || token->type == CPP_OPEN_BRACE)
8114     initializer = cp_parser_new_initializer (parser);
8115   else
8116     initializer = NULL;
8117
8118   /* A new-expression may not appear in an integral constant
8119      expression.  */
8120   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8121     ret = error_mark_node;
8122   /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8123      of a new-type-id or type-id of a new-expression, the new-expression shall
8124      contain a new-initializer of the form ( assignment-expression )".
8125      Additionally, consistently with the spirit of DR 1467, we want to accept
8126      'new auto { 2 }' too.  */
8127   else if (type_uses_auto (type)
8128            && (vec_safe_length (initializer) != 1
8129                || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8130                    && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8131     {
8132       error_at (token->location,
8133                 "initialization of new-expression for type %<auto%> "
8134                 "requires exactly one element");
8135       ret = error_mark_node;
8136     }
8137   else
8138     {
8139       /* Construct a location e.g.:
8140            ptr = new int[100]
8141                  ^~~~~~~~~~~~
8142          with caret == start at the start of the "new" token, and the end
8143          at the end of the final token we consumed.  */
8144       cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8145       location_t end_loc = get_finish (end_tok->location);
8146       location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8147
8148       /* Create a representation of the new-expression.  */
8149       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8150                        tf_warning_or_error);
8151       protected_set_expr_location (ret, combined_loc);
8152     }
8153
8154   if (placement != NULL)
8155     release_tree_vector (placement);
8156   if (initializer != NULL)
8157     release_tree_vector (initializer);
8158
8159   return ret;
8160 }
8161
8162 /* Parse a new-placement.
8163
8164    new-placement:
8165      ( expression-list )
8166
8167    Returns the same representation as for an expression-list.  */
8168
8169 static vec<tree, va_gc> *
8170 cp_parser_new_placement (cp_parser* parser)
8171 {
8172   vec<tree, va_gc> *expression_list;
8173
8174   /* Parse the expression-list.  */
8175   expression_list = (cp_parser_parenthesized_expression_list
8176                      (parser, non_attr, /*cast_p=*/false,
8177                       /*allow_expansion_p=*/true,
8178                       /*non_constant_p=*/NULL));
8179
8180   if (expression_list && expression_list->is_empty ())
8181     error ("expected expression-list or type-id");
8182
8183   return expression_list;
8184 }
8185
8186 /* Parse a new-type-id.
8187
8188    new-type-id:
8189      type-specifier-seq new-declarator [opt]
8190
8191    Returns the TYPE allocated.  If the new-type-id indicates an array
8192    type, *NELTS is set to the number of elements in the last array
8193    bound; the TYPE will not include the last array bound.  */
8194
8195 static tree
8196 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8197 {
8198   cp_decl_specifier_seq type_specifier_seq;
8199   cp_declarator *new_declarator;
8200   cp_declarator *declarator;
8201   cp_declarator *outer_declarator;
8202   const char *saved_message;
8203
8204   /* The type-specifier sequence must not contain type definitions.
8205      (It cannot contain declarations of new types either, but if they
8206      are not definitions we will catch that because they are not
8207      complete.)  */
8208   saved_message = parser->type_definition_forbidden_message;
8209   parser->type_definition_forbidden_message
8210     = G_("types may not be defined in a new-type-id");
8211   /* Parse the type-specifier-seq.  */
8212   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8213                                 /*is_trailing_return=*/false,
8214                                 &type_specifier_seq);
8215   /* Restore the old message.  */
8216   parser->type_definition_forbidden_message = saved_message;
8217
8218   if (type_specifier_seq.type == error_mark_node)
8219     return error_mark_node;
8220
8221   /* Parse the new-declarator.  */
8222   new_declarator = cp_parser_new_declarator_opt (parser);
8223
8224   /* Determine the number of elements in the last array dimension, if
8225      any.  */
8226   *nelts = NULL_TREE;
8227   /* Skip down to the last array dimension.  */
8228   declarator = new_declarator;
8229   outer_declarator = NULL;
8230   while (declarator && (declarator->kind == cdk_pointer
8231                         || declarator->kind == cdk_ptrmem))
8232     {
8233       outer_declarator = declarator;
8234       declarator = declarator->declarator;
8235     }
8236   while (declarator
8237          && declarator->kind == cdk_array
8238          && declarator->declarator
8239          && declarator->declarator->kind == cdk_array)
8240     {
8241       outer_declarator = declarator;
8242       declarator = declarator->declarator;
8243     }
8244
8245   if (declarator && declarator->kind == cdk_array)
8246     {
8247       *nelts = declarator->u.array.bounds;
8248       if (*nelts == error_mark_node)
8249         *nelts = integer_one_node;
8250
8251       if (outer_declarator)
8252         outer_declarator->declarator = declarator->declarator;
8253       else
8254         new_declarator = NULL;
8255     }
8256
8257   return groktypename (&type_specifier_seq, new_declarator, false);
8258 }
8259
8260 /* Parse an (optional) new-declarator.
8261
8262    new-declarator:
8263      ptr-operator new-declarator [opt]
8264      direct-new-declarator
8265
8266    Returns the declarator.  */
8267
8268 static cp_declarator *
8269 cp_parser_new_declarator_opt (cp_parser* parser)
8270 {
8271   enum tree_code code;
8272   tree type, std_attributes = NULL_TREE;
8273   cp_cv_quals cv_quals;  
8274
8275   /* We don't know if there's a ptr-operator next, or not.  */
8276   cp_parser_parse_tentatively (parser);
8277   /* Look for a ptr-operator.  */
8278   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8279   /* If that worked, look for more new-declarators.  */
8280   if (cp_parser_parse_definitely (parser))
8281     {
8282       cp_declarator *declarator;
8283
8284       /* Parse another optional declarator.  */
8285       declarator = cp_parser_new_declarator_opt (parser);
8286
8287       declarator = cp_parser_make_indirect_declarator
8288         (code, type, cv_quals, declarator, std_attributes);
8289
8290       return declarator;
8291     }
8292
8293   /* If the next token is a `[', there is a direct-new-declarator.  */
8294   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8295     return cp_parser_direct_new_declarator (parser);
8296
8297   return NULL;
8298 }
8299
8300 /* Parse a direct-new-declarator.
8301
8302    direct-new-declarator:
8303      [ expression ]
8304      direct-new-declarator [constant-expression]
8305
8306    */
8307
8308 static cp_declarator *
8309 cp_parser_direct_new_declarator (cp_parser* parser)
8310 {
8311   cp_declarator *declarator = NULL;
8312
8313   while (true)
8314     {
8315       tree expression;
8316       cp_token *token;
8317
8318       /* Look for the opening `['.  */
8319       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8320
8321       token = cp_lexer_peek_token (parser->lexer);
8322       expression = cp_parser_expression (parser);
8323       /* The standard requires that the expression have integral
8324          type.  DR 74 adds enumeration types.  We believe that the
8325          real intent is that these expressions be handled like the
8326          expression in a `switch' condition, which also allows
8327          classes with a single conversion to integral or
8328          enumeration type.  */
8329       if (!processing_template_decl)
8330         {
8331           expression
8332             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8333                                           expression,
8334                                           /*complain=*/true);
8335           if (!expression)
8336             {
8337               error_at (token->location,
8338                         "expression in new-declarator must have integral "
8339                         "or enumeration type");
8340               expression = error_mark_node;
8341             }
8342         }
8343
8344       /* Look for the closing `]'.  */
8345       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8346
8347       /* Add this bound to the declarator.  */
8348       declarator = make_array_declarator (declarator, expression);
8349
8350       /* If the next token is not a `[', then there are no more
8351          bounds.  */
8352       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8353         break;
8354     }
8355
8356   return declarator;
8357 }
8358
8359 /* Parse a new-initializer.
8360
8361    new-initializer:
8362      ( expression-list [opt] )
8363      braced-init-list
8364
8365    Returns a representation of the expression-list.  */
8366
8367 static vec<tree, va_gc> *
8368 cp_parser_new_initializer (cp_parser* parser)
8369 {
8370   vec<tree, va_gc> *expression_list;
8371
8372   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8373     {
8374       tree t;
8375       bool expr_non_constant_p;
8376       cp_lexer_set_source_position (parser->lexer);
8377       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8378       t = cp_parser_braced_list (parser, &expr_non_constant_p);
8379       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8380       expression_list = make_tree_vector_single (t);
8381     }
8382   else
8383     expression_list = (cp_parser_parenthesized_expression_list
8384                        (parser, non_attr, /*cast_p=*/false,
8385                         /*allow_expansion_p=*/true,
8386                         /*non_constant_p=*/NULL));
8387
8388   return expression_list;
8389 }
8390
8391 /* Parse a delete-expression.
8392
8393    delete-expression:
8394      :: [opt] delete cast-expression
8395      :: [opt] delete [ ] cast-expression
8396
8397    Returns a representation of the expression.  */
8398
8399 static tree
8400 cp_parser_delete_expression (cp_parser* parser)
8401 {
8402   bool global_scope_p;
8403   bool array_p;
8404   tree expression;
8405
8406   /* Look for the optional `::' operator.  */
8407   global_scope_p
8408     = (cp_parser_global_scope_opt (parser,
8409                                    /*current_scope_valid_p=*/false)
8410        != NULL_TREE);
8411   /* Look for the `delete' keyword.  */
8412   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8413   /* See if the array syntax is in use.  */
8414   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8415     {
8416       /* Consume the `[' token.  */
8417       cp_lexer_consume_token (parser->lexer);
8418       /* Look for the `]' token.  */
8419       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8420       /* Remember that this is the `[]' construct.  */
8421       array_p = true;
8422     }
8423   else
8424     array_p = false;
8425
8426   /* Parse the cast-expression.  */
8427   expression = cp_parser_simple_cast_expression (parser);
8428
8429   /* A delete-expression may not appear in an integral constant
8430      expression.  */
8431   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8432     return error_mark_node;
8433
8434   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8435                         tf_warning_or_error);
8436 }
8437
8438 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8439    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8440    0 otherwise.  */
8441
8442 static int
8443 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8444 {
8445   cp_token *token = cp_lexer_peek_token (parser->lexer);
8446   switch (token->type)
8447     {
8448     case CPP_COMMA:
8449     case CPP_SEMICOLON:
8450     case CPP_QUERY:
8451     case CPP_COLON:
8452     case CPP_CLOSE_SQUARE:
8453     case CPP_CLOSE_PAREN:
8454     case CPP_CLOSE_BRACE:
8455     case CPP_OPEN_BRACE:
8456     case CPP_DOT:
8457     case CPP_DOT_STAR:
8458     case CPP_DEREF:
8459     case CPP_DEREF_STAR:
8460     case CPP_DIV:
8461     case CPP_MOD:
8462     case CPP_LSHIFT:
8463     case CPP_RSHIFT:
8464     case CPP_LESS:
8465     case CPP_GREATER:
8466     case CPP_LESS_EQ:
8467     case CPP_GREATER_EQ:
8468     case CPP_EQ_EQ:
8469     case CPP_NOT_EQ:
8470     case CPP_EQ:
8471     case CPP_MULT_EQ:
8472     case CPP_DIV_EQ:
8473     case CPP_MOD_EQ:
8474     case CPP_PLUS_EQ:
8475     case CPP_MINUS_EQ:
8476     case CPP_RSHIFT_EQ:
8477     case CPP_LSHIFT_EQ:
8478     case CPP_AND_EQ:
8479     case CPP_XOR_EQ:
8480     case CPP_OR_EQ:
8481     case CPP_XOR:
8482     case CPP_OR:
8483     case CPP_OR_OR:
8484     case CPP_EOF:
8485     case CPP_ELLIPSIS:
8486       return 0;
8487
8488     case CPP_OPEN_PAREN:
8489       /* In ((type ()) () the last () isn't a valid cast-expression,
8490          so the whole must be parsed as postfix-expression.  */
8491       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8492              != CPP_CLOSE_PAREN;
8493
8494     case CPP_OPEN_SQUARE:
8495       /* '[' may start a primary-expression in obj-c++ and in C++11,
8496          as a lambda-expression, eg, '(void)[]{}'.  */
8497       if (cxx_dialect >= cxx11)
8498         return -1;
8499       return c_dialect_objc ();
8500
8501     case CPP_PLUS_PLUS:
8502     case CPP_MINUS_MINUS:
8503       /* '++' and '--' may or may not start a cast-expression:
8504
8505          struct T { void operator++(int); };
8506          void f() { (T())++; }
8507
8508          vs
8509
8510          int a;
8511          (int)++a;  */
8512       return -1;
8513
8514     default:
8515       return 1;
8516     }
8517 }
8518
8519 /* Parse a cast-expression.
8520
8521    cast-expression:
8522      unary-expression
8523      ( type-id ) cast-expression
8524
8525    ADDRESS_P is true iff the unary-expression is appearing as the
8526    operand of the `&' operator.   CAST_P is true if this expression is
8527    the target of a cast.
8528
8529    Returns a representation of the expression.  */
8530
8531 static cp_expr
8532 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8533                            bool decltype_p, cp_id_kind * pidk)
8534 {
8535   /* If it's a `(', then we might be looking at a cast.  */
8536   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8537     {
8538       tree type = NULL_TREE;
8539       cp_expr expr (NULL_TREE);
8540       int cast_expression = 0;
8541       const char *saved_message;
8542
8543       /* There's no way to know yet whether or not this is a cast.
8544          For example, `(int (3))' is a unary-expression, while `(int)
8545          3' is a cast.  So, we resort to parsing tentatively.  */
8546       cp_parser_parse_tentatively (parser);
8547       /* Types may not be defined in a cast.  */
8548       saved_message = parser->type_definition_forbidden_message;
8549       parser->type_definition_forbidden_message
8550         = G_("types may not be defined in casts");
8551       /* Consume the `('.  */
8552       cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8553       location_t open_paren_loc = open_paren->location;
8554
8555       /* A very tricky bit is that `(struct S) { 3 }' is a
8556          compound-literal (which we permit in C++ as an extension).
8557          But, that construct is not a cast-expression -- it is a
8558          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
8559          is legal; if the compound-literal were a cast-expression,
8560          you'd need an extra set of parentheses.)  But, if we parse
8561          the type-id, and it happens to be a class-specifier, then we
8562          will commit to the parse at that point, because we cannot
8563          undo the action that is done when creating a new class.  So,
8564          then we cannot back up and do a postfix-expression.
8565
8566          Another tricky case is the following (c++/29234):
8567
8568          struct S { void operator () (); };
8569
8570          void foo ()
8571          {
8572            ( S()() );
8573          }
8574
8575          As a type-id we parse the parenthesized S()() as a function
8576          returning a function, groktypename complains and we cannot
8577          back up in this case either.
8578
8579          Therefore, we scan ahead to the closing `)', and check to see
8580          if the tokens after the `)' can start a cast-expression.  Otherwise
8581          we are dealing with an unary-expression, a postfix-expression
8582          or something else.
8583
8584          Yet another tricky case, in C++11, is the following (c++/54891):
8585
8586          (void)[]{};
8587
8588          The issue is that usually, besides the case of lambda-expressions,
8589          the parenthesized type-id cannot be followed by '[', and, eg, we
8590          want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8591          Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8592          we don't commit, we try a cast-expression, then an unary-expression.
8593
8594          Save tokens so that we can put them back.  */
8595       cp_lexer_save_tokens (parser->lexer);
8596
8597       /* We may be looking at a cast-expression.  */
8598       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8599                                                  /*consume_paren=*/true))
8600         cast_expression
8601           = cp_parser_tokens_start_cast_expression (parser);
8602
8603       /* Roll back the tokens we skipped.  */
8604       cp_lexer_rollback_tokens (parser->lexer);
8605       /* If we aren't looking at a cast-expression, simulate an error so
8606          that the call to cp_parser_error_occurred below returns true.  */
8607       if (!cast_expression)
8608         cp_parser_simulate_error (parser);
8609       else
8610         {
8611           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8612           parser->in_type_id_in_expr_p = true;
8613           /* Look for the type-id.  */
8614           type = cp_parser_type_id (parser);
8615           /* Look for the closing `)'.  */
8616           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8617           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8618         }
8619
8620       /* Restore the saved message.  */
8621       parser->type_definition_forbidden_message = saved_message;
8622
8623       /* At this point this can only be either a cast or a
8624          parenthesized ctor such as `(T ())' that looks like a cast to
8625          function returning T.  */
8626       if (!cp_parser_error_occurred (parser))
8627         {
8628           /* Only commit if the cast-expression doesn't start with
8629              '++', '--', or '[' in C++11.  */
8630           if (cast_expression > 0)
8631             cp_parser_commit_to_topmost_tentative_parse (parser);
8632
8633           expr = cp_parser_cast_expression (parser,
8634                                             /*address_p=*/false,
8635                                             /*cast_p=*/true,
8636                                             /*decltype_p=*/false,
8637                                             pidk);
8638
8639           if (cp_parser_parse_definitely (parser))
8640             {
8641               /* Warn about old-style casts, if so requested.  */
8642               if (warn_old_style_cast
8643                   && !in_system_header_at (input_location)
8644                   && !VOID_TYPE_P (type)
8645                   && current_lang_name != lang_name_c)
8646                 warning (OPT_Wold_style_cast, "use of old-style cast");
8647
8648               /* Only type conversions to integral or enumeration types
8649                  can be used in constant-expressions.  */
8650               if (!cast_valid_in_integral_constant_expression_p (type)
8651                   && cp_parser_non_integral_constant_expression (parser,
8652                                                                  NIC_CAST))
8653                 return error_mark_node;
8654
8655               /* Perform the cast.  */
8656               /* Make a location:
8657                    (TYPE) EXPR
8658                    ^~~~~~~~~~~
8659                  with start==caret at the open paren, extending to the
8660                  end of "expr".  */
8661               location_t cast_loc = make_location (open_paren_loc,
8662                                                    open_paren_loc,
8663                                                    expr.get_finish ());
8664               expr = build_c_cast (cast_loc, type, expr);
8665               return expr;
8666             }
8667         }
8668       else 
8669         cp_parser_abort_tentative_parse (parser);
8670     }
8671
8672   /* If we get here, then it's not a cast, so it must be a
8673      unary-expression.  */
8674   return cp_parser_unary_expression (parser, pidk, address_p,
8675                                      cast_p, decltype_p);
8676 }
8677
8678 /* Parse a binary expression of the general form:
8679
8680    pm-expression:
8681      cast-expression
8682      pm-expression .* cast-expression
8683      pm-expression ->* cast-expression
8684
8685    multiplicative-expression:
8686      pm-expression
8687      multiplicative-expression * pm-expression
8688      multiplicative-expression / pm-expression
8689      multiplicative-expression % pm-expression
8690
8691    additive-expression:
8692      multiplicative-expression
8693      additive-expression + multiplicative-expression
8694      additive-expression - multiplicative-expression
8695
8696    shift-expression:
8697      additive-expression
8698      shift-expression << additive-expression
8699      shift-expression >> additive-expression
8700
8701    relational-expression:
8702      shift-expression
8703      relational-expression < shift-expression
8704      relational-expression > shift-expression
8705      relational-expression <= shift-expression
8706      relational-expression >= shift-expression
8707
8708   GNU Extension:
8709
8710    relational-expression:
8711      relational-expression <? shift-expression
8712      relational-expression >? shift-expression
8713
8714    equality-expression:
8715      relational-expression
8716      equality-expression == relational-expression
8717      equality-expression != relational-expression
8718
8719    and-expression:
8720      equality-expression
8721      and-expression & equality-expression
8722
8723    exclusive-or-expression:
8724      and-expression
8725      exclusive-or-expression ^ and-expression
8726
8727    inclusive-or-expression:
8728      exclusive-or-expression
8729      inclusive-or-expression | exclusive-or-expression
8730
8731    logical-and-expression:
8732      inclusive-or-expression
8733      logical-and-expression && inclusive-or-expression
8734
8735    logical-or-expression:
8736      logical-and-expression
8737      logical-or-expression || logical-and-expression
8738
8739    All these are implemented with a single function like:
8740
8741    binary-expression:
8742      simple-cast-expression
8743      binary-expression <token> binary-expression
8744
8745    CAST_P is true if this expression is the target of a cast.
8746
8747    The binops_by_token map is used to get the tree codes for each <token> type.
8748    binary-expressions are associated according to a precedence table.  */
8749
8750 #define TOKEN_PRECEDENCE(token)                              \
8751 (((token->type == CPP_GREATER                                \
8752    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8753   && !parser->greater_than_is_operator_p)                    \
8754  ? PREC_NOT_OPERATOR                                         \
8755  : binops_by_token[token->type].prec)
8756
8757 static cp_expr
8758 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8759                              bool no_toplevel_fold_p,
8760                              bool decltype_p,
8761                              enum cp_parser_prec prec,
8762                              cp_id_kind * pidk)
8763 {
8764   cp_parser_expression_stack stack;
8765   cp_parser_expression_stack_entry *sp = &stack[0];
8766   cp_parser_expression_stack_entry current;
8767   cp_expr rhs;
8768   cp_token *token;
8769   enum tree_code rhs_type;
8770   enum cp_parser_prec new_prec, lookahead_prec;
8771   tree overload;
8772
8773   /* Parse the first expression.  */
8774   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8775                       ? TRUTH_NOT_EXPR : ERROR_MARK);
8776   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8777                                            cast_p, decltype_p, pidk);
8778   current.prec = prec;
8779
8780   if (cp_parser_error_occurred (parser))
8781     return error_mark_node;
8782
8783   for (;;)
8784     {
8785       /* Get an operator token.  */
8786       token = cp_lexer_peek_token (parser->lexer);
8787
8788       if (warn_cxx11_compat
8789           && token->type == CPP_RSHIFT
8790           && !parser->greater_than_is_operator_p)
8791         {
8792           if (warning_at (token->location, OPT_Wc__11_compat,
8793                           "%<>>%> operator is treated"
8794                           " as two right angle brackets in C++11"))
8795             inform (token->location,
8796                     "suggest parentheses around %<>>%> expression");
8797         }
8798
8799       new_prec = TOKEN_PRECEDENCE (token);
8800       if (new_prec != PREC_NOT_OPERATOR
8801           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8802         /* This is a fold-expression; handle it later.  */
8803         new_prec = PREC_NOT_OPERATOR;
8804
8805       /* Popping an entry off the stack means we completed a subexpression:
8806          - either we found a token which is not an operator (`>' where it is not
8807            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8808            will happen repeatedly;
8809          - or, we found an operator which has lower priority.  This is the case
8810            where the recursive descent *ascends*, as in `3 * 4 + 5' after
8811            parsing `3 * 4'.  */
8812       if (new_prec <= current.prec)
8813         {
8814           if (sp == stack)
8815             break;
8816           else
8817             goto pop;
8818         }
8819
8820      get_rhs:
8821       current.tree_type = binops_by_token[token->type].tree_type;
8822       current.loc = token->location;
8823
8824       /* We used the operator token.  */
8825       cp_lexer_consume_token (parser->lexer);
8826
8827       /* For "false && x" or "true || x", x will never be executed;
8828          disable warnings while evaluating it.  */
8829       if (current.tree_type == TRUTH_ANDIF_EXPR)
8830         c_inhibit_evaluation_warnings +=
8831           cp_fully_fold (current.lhs) == truthvalue_false_node;
8832       else if (current.tree_type == TRUTH_ORIF_EXPR)
8833         c_inhibit_evaluation_warnings +=
8834           cp_fully_fold (current.lhs) == truthvalue_true_node;
8835
8836       /* Extract another operand.  It may be the RHS of this expression
8837          or the LHS of a new, higher priority expression.  */
8838       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8839                   ? TRUTH_NOT_EXPR : ERROR_MARK);
8840       rhs = cp_parser_simple_cast_expression (parser);
8841
8842       /* Get another operator token.  Look up its precedence to avoid
8843          building a useless (immediately popped) stack entry for common
8844          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
8845       token = cp_lexer_peek_token (parser->lexer);
8846       lookahead_prec = TOKEN_PRECEDENCE (token);
8847       if (lookahead_prec != PREC_NOT_OPERATOR
8848           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8849         lookahead_prec = PREC_NOT_OPERATOR;
8850       if (lookahead_prec > new_prec)
8851         {
8852           /* ... and prepare to parse the RHS of the new, higher priority
8853              expression.  Since precedence levels on the stack are
8854              monotonically increasing, we do not have to care about
8855              stack overflows.  */
8856           *sp = current;
8857           ++sp;
8858           current.lhs = rhs;
8859           current.lhs_type = rhs_type;
8860           current.prec = new_prec;
8861           new_prec = lookahead_prec;
8862           goto get_rhs;
8863
8864          pop:
8865           lookahead_prec = new_prec;
8866           /* If the stack is not empty, we have parsed into LHS the right side
8867              (`4' in the example above) of an expression we had suspended.
8868              We can use the information on the stack to recover the LHS (`3')
8869              from the stack together with the tree code (`MULT_EXPR'), and
8870              the precedence of the higher level subexpression
8871              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
8872              which will be used to actually build the additive expression.  */
8873           rhs = current.lhs;
8874           rhs_type = current.lhs_type;
8875           --sp;
8876           current = *sp;
8877         }
8878
8879       /* Undo the disabling of warnings done above.  */
8880       if (current.tree_type == TRUTH_ANDIF_EXPR)
8881         c_inhibit_evaluation_warnings -=
8882           cp_fully_fold (current.lhs) == truthvalue_false_node;
8883       else if (current.tree_type == TRUTH_ORIF_EXPR)
8884         c_inhibit_evaluation_warnings -=
8885           cp_fully_fold (current.lhs) == truthvalue_true_node;
8886
8887       if (warn_logical_not_paren
8888           && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8889           && current.lhs_type == TRUTH_NOT_EXPR
8890           /* Avoid warning for !!x == y.  */
8891           && (TREE_CODE (current.lhs) != NE_EXPR
8892               || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8893           && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8894               || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8895                   /* Avoid warning for !b == y where b is boolean.  */
8896                   && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8897                       || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8898                           != BOOLEAN_TYPE))))
8899           /* Avoid warning for !!b == y where b is boolean.  */
8900           && (!DECL_P (current.lhs)
8901               || TREE_TYPE (current.lhs) == NULL_TREE
8902               || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8903         warn_logical_not_parentheses (current.loc, current.tree_type,
8904                                       maybe_constant_value (rhs));
8905
8906       overload = NULL;
8907
8908       location_t combined_loc = make_location (current.loc,
8909                                                current.lhs.get_start (),
8910                                                rhs.get_finish ());
8911
8912       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8913          ERROR_MARK for everything that is not a binary expression.
8914          This makes warn_about_parentheses miss some warnings that
8915          involve unary operators.  For unary expressions we should
8916          pass the correct tree_code unless the unary expression was
8917          surrounded by parentheses.
8918       */
8919       if (no_toplevel_fold_p
8920           && lookahead_prec <= current.prec
8921           && sp == stack)
8922         current.lhs = build2_loc (combined_loc,
8923                                   current.tree_type,
8924                                   TREE_CODE_CLASS (current.tree_type)
8925                                   == tcc_comparison
8926                                   ? boolean_type_node : TREE_TYPE (current.lhs),
8927                                   current.lhs, rhs);
8928       else
8929         {
8930           current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8931                                            current.lhs, current.lhs_type,
8932                                            rhs, rhs_type, &overload,
8933                                            complain_flags (decltype_p));
8934           /* TODO: build_x_binary_op doesn't always honor the location.  */
8935           current.lhs.set_location (combined_loc);
8936         }
8937       current.lhs_type = current.tree_type;
8938
8939       /* If the binary operator required the use of an overloaded operator,
8940          then this expression cannot be an integral constant-expression.
8941          An overloaded operator can be used even if both operands are
8942          otherwise permissible in an integral constant-expression if at
8943          least one of the operands is of enumeration type.  */
8944
8945       if (overload
8946           && cp_parser_non_integral_constant_expression (parser,
8947                                                          NIC_OVERLOADED))
8948         return error_mark_node;
8949     }
8950
8951   return current.lhs;
8952 }
8953
8954 static cp_expr
8955 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8956                              bool no_toplevel_fold_p,
8957                              enum cp_parser_prec prec,
8958                              cp_id_kind * pidk)
8959 {
8960   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8961                                       /*decltype*/false, prec, pidk);
8962 }
8963
8964 /* Parse the `? expression : assignment-expression' part of a
8965    conditional-expression.  The LOGICAL_OR_EXPR is the
8966    logical-or-expression that started the conditional-expression.
8967    Returns a representation of the entire conditional-expression.
8968
8969    This routine is used by cp_parser_assignment_expression.
8970
8971      ? expression : assignment-expression
8972
8973    GNU Extensions:
8974
8975      ? : assignment-expression */
8976
8977 static tree
8978 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8979 {
8980   tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8981   cp_expr assignment_expr;
8982   struct cp_token *token;
8983   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8984
8985   /* Consume the `?' token.  */
8986   cp_lexer_consume_token (parser->lexer);
8987   token = cp_lexer_peek_token (parser->lexer);
8988   if (cp_parser_allow_gnu_extensions_p (parser)
8989       && token->type == CPP_COLON)
8990     {
8991       pedwarn (token->location, OPT_Wpedantic, 
8992                "ISO C++ does not allow ?: with omitted middle operand");
8993       /* Implicit true clause.  */
8994       expr = NULL_TREE;
8995       c_inhibit_evaluation_warnings +=
8996         folded_logical_or_expr == truthvalue_true_node;
8997       warn_for_omitted_condop (token->location, logical_or_expr);
8998     }
8999   else
9000     {
9001       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9002       parser->colon_corrects_to_scope_p = false;
9003       /* Parse the expression.  */
9004       c_inhibit_evaluation_warnings +=
9005         folded_logical_or_expr == truthvalue_false_node;
9006       expr = cp_parser_expression (parser);
9007       c_inhibit_evaluation_warnings +=
9008         ((folded_logical_or_expr == truthvalue_true_node)
9009          - (folded_logical_or_expr == truthvalue_false_node));
9010       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9011     }
9012
9013   /* The next token should be a `:'.  */
9014   cp_parser_require (parser, CPP_COLON, RT_COLON);
9015   /* Parse the assignment-expression.  */
9016   assignment_expr = cp_parser_assignment_expression (parser);
9017   c_inhibit_evaluation_warnings -=
9018     folded_logical_or_expr == truthvalue_true_node;
9019
9020   /* Make a location:
9021        LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9022        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9023      with the caret at the "?", ranging from the start of
9024      the logical_or_expr to the end of the assignment_expr.  */
9025   loc = make_location (loc,
9026                        logical_or_expr.get_start (),
9027                        assignment_expr.get_finish ());
9028
9029   /* Build the conditional-expression.  */
9030   return build_x_conditional_expr (loc, logical_or_expr,
9031                                    expr,
9032                                    assignment_expr,
9033                                    tf_warning_or_error);
9034 }
9035
9036 /* Parse an assignment-expression.
9037
9038    assignment-expression:
9039      conditional-expression
9040      logical-or-expression assignment-operator assignment_expression
9041      throw-expression
9042
9043    CAST_P is true if this expression is the target of a cast.
9044    DECLTYPE_P is true if this expression is the operand of decltype.
9045
9046    Returns a representation for the expression.  */
9047
9048 static cp_expr
9049 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9050                                  bool cast_p, bool decltype_p)
9051 {
9052   cp_expr expr;
9053
9054   /* If the next token is the `throw' keyword, then we're looking at
9055      a throw-expression.  */
9056   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9057     expr = cp_parser_throw_expression (parser);
9058   /* Otherwise, it must be that we are looking at a
9059      logical-or-expression.  */
9060   else
9061     {
9062       /* Parse the binary expressions (logical-or-expression).  */
9063       expr = cp_parser_binary_expression (parser, cast_p, false,
9064                                           decltype_p,
9065                                           PREC_NOT_OPERATOR, pidk);
9066       /* If the next token is a `?' then we're actually looking at a
9067          conditional-expression.  */
9068       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9069         return cp_parser_question_colon_clause (parser, expr);
9070       else
9071         {
9072           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9073
9074           /* If it's an assignment-operator, we're using the second
9075              production.  */
9076           enum tree_code assignment_operator
9077             = cp_parser_assignment_operator_opt (parser);
9078           if (assignment_operator != ERROR_MARK)
9079             {
9080               bool non_constant_p;
9081
9082               /* Parse the right-hand side of the assignment.  */
9083               cp_expr rhs = cp_parser_initializer_clause (parser,
9084                                                           &non_constant_p);
9085
9086               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9087                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9088
9089               /* An assignment may not appear in a
9090                  constant-expression.  */
9091               if (cp_parser_non_integral_constant_expression (parser,
9092                                                               NIC_ASSIGNMENT))
9093                 return error_mark_node;
9094               /* Build the assignment expression.  Its default
9095                  location:
9096                    LHS = RHS
9097                    ~~~~^~~~~
9098                  is the location of the '=' token as the
9099                  caret, ranging from the start of the lhs to the
9100                  end of the rhs.  */
9101               loc = make_location (loc,
9102                                    expr.get_start (),
9103                                    rhs.get_finish ());
9104               expr = build_x_modify_expr (loc, expr,
9105                                           assignment_operator,
9106                                           rhs,
9107                                           complain_flags (decltype_p));
9108               /* TODO: build_x_modify_expr doesn't honor the location,
9109                  so we must set it here.  */
9110               expr.set_location (loc);
9111             }
9112         }
9113     }
9114
9115   return expr;
9116 }
9117
9118 /* Parse an (optional) assignment-operator.
9119
9120    assignment-operator: one of
9121      = *= /= %= += -= >>= <<= &= ^= |=
9122
9123    GNU Extension:
9124
9125    assignment-operator: one of
9126      <?= >?=
9127
9128    If the next token is an assignment operator, the corresponding tree
9129    code is returned, and the token is consumed.  For example, for
9130    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
9131    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
9132    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
9133    operator, ERROR_MARK is returned.  */
9134
9135 static enum tree_code
9136 cp_parser_assignment_operator_opt (cp_parser* parser)
9137 {
9138   enum tree_code op;
9139   cp_token *token;
9140
9141   /* Peek at the next token.  */
9142   token = cp_lexer_peek_token (parser->lexer);
9143
9144   switch (token->type)
9145     {
9146     case CPP_EQ:
9147       op = NOP_EXPR;
9148       break;
9149
9150     case CPP_MULT_EQ:
9151       op = MULT_EXPR;
9152       break;
9153
9154     case CPP_DIV_EQ:
9155       op = TRUNC_DIV_EXPR;
9156       break;
9157
9158     case CPP_MOD_EQ:
9159       op = TRUNC_MOD_EXPR;
9160       break;
9161
9162     case CPP_PLUS_EQ:
9163       op = PLUS_EXPR;
9164       break;
9165
9166     case CPP_MINUS_EQ:
9167       op = MINUS_EXPR;
9168       break;
9169
9170     case CPP_RSHIFT_EQ:
9171       op = RSHIFT_EXPR;
9172       break;
9173
9174     case CPP_LSHIFT_EQ:
9175       op = LSHIFT_EXPR;
9176       break;
9177
9178     case CPP_AND_EQ:
9179       op = BIT_AND_EXPR;
9180       break;
9181
9182     case CPP_XOR_EQ:
9183       op = BIT_XOR_EXPR;
9184       break;
9185
9186     case CPP_OR_EQ:
9187       op = BIT_IOR_EXPR;
9188       break;
9189
9190     default:
9191       /* Nothing else is an assignment operator.  */
9192       op = ERROR_MARK;
9193     }
9194
9195   /* An operator followed by ... is a fold-expression, handled elsewhere.  */
9196   if (op != ERROR_MARK
9197       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9198     op = ERROR_MARK;
9199
9200   /* If it was an assignment operator, consume it.  */
9201   if (op != ERROR_MARK)
9202     cp_lexer_consume_token (parser->lexer);
9203
9204   return op;
9205 }
9206
9207 /* Parse an expression.
9208
9209    expression:
9210      assignment-expression
9211      expression , assignment-expression
9212
9213    CAST_P is true if this expression is the target of a cast.
9214    DECLTYPE_P is true if this expression is the immediate operand of decltype,
9215      except possibly parenthesized or on the RHS of a comma (N3276).
9216
9217    Returns a representation of the expression.  */
9218
9219 static cp_expr
9220 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9221                       bool cast_p, bool decltype_p)
9222 {
9223   cp_expr expression = NULL_TREE;
9224   location_t loc = UNKNOWN_LOCATION;
9225
9226   while (true)
9227     {
9228       cp_expr assignment_expression;
9229
9230       /* Parse the next assignment-expression.  */
9231       assignment_expression
9232         = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9233
9234       /* We don't create a temporary for a call that is the immediate operand
9235          of decltype or on the RHS of a comma.  But when we see a comma, we
9236          need to create a temporary for a call on the LHS.  */
9237       if (decltype_p && !processing_template_decl
9238           && TREE_CODE (assignment_expression) == CALL_EXPR
9239           && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9240           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9241         assignment_expression
9242           = build_cplus_new (TREE_TYPE (assignment_expression),
9243                              assignment_expression, tf_warning_or_error);
9244
9245       /* If this is the first assignment-expression, we can just
9246          save it away.  */
9247       if (!expression)
9248         expression = assignment_expression;
9249       else
9250         {
9251           /* Create a location with caret at the comma, ranging
9252              from the start of the LHS to the end of the RHS.  */
9253           loc = make_location (loc,
9254                                expression.get_start (),
9255                                assignment_expression.get_finish ());
9256           expression = build_x_compound_expr (loc, expression,
9257                                               assignment_expression,
9258                                               complain_flags (decltype_p));
9259           expression.set_location (loc);
9260         }
9261       /* If the next token is not a comma, or we're in a fold-expression, then
9262          we are done with the expression.  */
9263       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9264           || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9265         break;
9266       /* Consume the `,'.  */
9267       loc = cp_lexer_peek_token (parser->lexer)->location;
9268       cp_lexer_consume_token (parser->lexer);
9269       /* A comma operator cannot appear in a constant-expression.  */
9270       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9271         expression = error_mark_node;
9272     }
9273
9274   return expression;
9275 }
9276
9277 /* Parse a constant-expression.
9278
9279    constant-expression:
9280      conditional-expression
9281
9282   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9283   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
9284   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
9285   is false, NON_CONSTANT_P should be NULL.  */
9286
9287 static cp_expr
9288 cp_parser_constant_expression (cp_parser* parser,
9289                                bool allow_non_constant_p,
9290                                bool *non_constant_p)
9291 {
9292   bool saved_integral_constant_expression_p;
9293   bool saved_allow_non_integral_constant_expression_p;
9294   bool saved_non_integral_constant_expression_p;
9295   cp_expr expression;
9296
9297   /* It might seem that we could simply parse the
9298      conditional-expression, and then check to see if it were
9299      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
9300      one that the compiler can figure out is constant, possibly after
9301      doing some simplifications or optimizations.  The standard has a
9302      precise definition of constant-expression, and we must honor
9303      that, even though it is somewhat more restrictive.
9304
9305      For example:
9306
9307        int i[(2, 3)];
9308
9309      is not a legal declaration, because `(2, 3)' is not a
9310      constant-expression.  The `,' operator is forbidden in a
9311      constant-expression.  However, GCC's constant-folding machinery
9312      will fold this operation to an INTEGER_CST for `3'.  */
9313
9314   /* Save the old settings.  */
9315   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9316   saved_allow_non_integral_constant_expression_p
9317     = parser->allow_non_integral_constant_expression_p;
9318   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9319   /* We are now parsing a constant-expression.  */
9320   parser->integral_constant_expression_p = true;
9321   parser->allow_non_integral_constant_expression_p
9322     = (allow_non_constant_p || cxx_dialect >= cxx11);
9323   parser->non_integral_constant_expression_p = false;
9324   /* Although the grammar says "conditional-expression", we parse an
9325      "assignment-expression", which also permits "throw-expression"
9326      and the use of assignment operators.  In the case that
9327      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9328      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
9329      actually essential that we look for an assignment-expression.
9330      For example, cp_parser_initializer_clauses uses this function to
9331      determine whether a particular assignment-expression is in fact
9332      constant.  */
9333   expression = cp_parser_assignment_expression (parser);
9334   /* Restore the old settings.  */
9335   parser->integral_constant_expression_p
9336     = saved_integral_constant_expression_p;
9337   parser->allow_non_integral_constant_expression_p
9338     = saved_allow_non_integral_constant_expression_p;
9339   if (cxx_dialect >= cxx11)
9340     {
9341       /* Require an rvalue constant expression here; that's what our
9342          callers expect.  Reference constant expressions are handled
9343          separately in e.g. cp_parser_template_argument.  */
9344       bool is_const = potential_rvalue_constant_expression (expression);
9345       parser->non_integral_constant_expression_p = !is_const;
9346       if (!is_const && !allow_non_constant_p)
9347         require_potential_rvalue_constant_expression (expression);
9348     }
9349   if (allow_non_constant_p)
9350     *non_constant_p = parser->non_integral_constant_expression_p;
9351   parser->non_integral_constant_expression_p
9352     = saved_non_integral_constant_expression_p;
9353
9354   return expression;
9355 }
9356
9357 /* Parse __builtin_offsetof.
9358
9359    offsetof-expression:
9360      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9361
9362    offsetof-member-designator:
9363      id-expression
9364      | offsetof-member-designator "." id-expression
9365      | offsetof-member-designator "[" expression "]"
9366      | offsetof-member-designator "->" id-expression  */
9367
9368 static cp_expr
9369 cp_parser_builtin_offsetof (cp_parser *parser)
9370 {
9371   int save_ice_p, save_non_ice_p;
9372   tree type;
9373   cp_expr expr;
9374   cp_id_kind dummy;
9375   cp_token *token;
9376   location_t finish_loc;
9377
9378   /* We're about to accept non-integral-constant things, but will
9379      definitely yield an integral constant expression.  Save and
9380      restore these values around our local parsing.  */
9381   save_ice_p = parser->integral_constant_expression_p;
9382   save_non_ice_p = parser->non_integral_constant_expression_p;
9383
9384   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9385
9386   /* Consume the "__builtin_offsetof" token.  */
9387   cp_lexer_consume_token (parser->lexer);
9388   /* Consume the opening `('.  */
9389   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9390   /* Parse the type-id.  */
9391   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9392   type = cp_parser_type_id (parser);
9393   /* Look for the `,'.  */
9394   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9395   token = cp_lexer_peek_token (parser->lexer);
9396
9397   /* Build the (type *)null that begins the traditional offsetof macro.  */
9398   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9399                             tf_warning_or_error);
9400
9401   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
9402   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9403                                                  true, &dummy, token->location);
9404   while (true)
9405     {
9406       token = cp_lexer_peek_token (parser->lexer);
9407       switch (token->type)
9408         {
9409         case CPP_OPEN_SQUARE:
9410           /* offsetof-member-designator "[" expression "]" */
9411           expr = cp_parser_postfix_open_square_expression (parser, expr,
9412                                                            true, false);
9413           break;
9414
9415         case CPP_DEREF:
9416           /* offsetof-member-designator "->" identifier */
9417           expr = grok_array_decl (token->location, expr,
9418                                   integer_zero_node, false);
9419           /* FALLTHRU */
9420
9421         case CPP_DOT:
9422           /* offsetof-member-designator "." identifier */
9423           cp_lexer_consume_token (parser->lexer);
9424           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9425                                                          expr, true, &dummy,
9426                                                          token->location);
9427           break;
9428
9429         case CPP_CLOSE_PAREN:
9430           /* Consume the ")" token.  */
9431           finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9432           cp_lexer_consume_token (parser->lexer);
9433           goto success;
9434
9435         default:
9436           /* Error.  We know the following require will fail, but
9437              that gives the proper error message.  */
9438           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9439           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9440           expr = error_mark_node;
9441           goto failure;
9442         }
9443     }
9444
9445  success:
9446   /* Make a location of the form:
9447        __builtin_offsetof (struct s, f)
9448        ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9449      with caret at the type-id, ranging from the start of the
9450      "_builtin_offsetof" token to the close paren.  */
9451   loc = make_location (loc, start_loc, finish_loc);
9452   /* The result will be an INTEGER_CST, so we need to explicitly
9453      preserve the location.  */
9454   expr = cp_expr (finish_offsetof (expr, loc), loc);
9455
9456  failure:
9457   parser->integral_constant_expression_p = save_ice_p;
9458   parser->non_integral_constant_expression_p = save_non_ice_p;
9459
9460   return expr;
9461 }
9462
9463 /* Parse a trait expression.
9464
9465    Returns a representation of the expression, the underlying type
9466    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
9467
9468 static tree
9469 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9470 {
9471   cp_trait_kind kind;
9472   tree type1, type2 = NULL_TREE;
9473   bool binary = false;
9474   bool variadic = false;
9475
9476   switch (keyword)
9477     {
9478     case RID_HAS_NOTHROW_ASSIGN:
9479       kind = CPTK_HAS_NOTHROW_ASSIGN;
9480       break;
9481     case RID_HAS_NOTHROW_CONSTRUCTOR:
9482       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9483       break;
9484     case RID_HAS_NOTHROW_COPY:
9485       kind = CPTK_HAS_NOTHROW_COPY;
9486       break;
9487     case RID_HAS_TRIVIAL_ASSIGN:
9488       kind = CPTK_HAS_TRIVIAL_ASSIGN;
9489       break;
9490     case RID_HAS_TRIVIAL_CONSTRUCTOR:
9491       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9492       break;
9493     case RID_HAS_TRIVIAL_COPY:
9494       kind = CPTK_HAS_TRIVIAL_COPY;
9495       break;
9496     case RID_HAS_TRIVIAL_DESTRUCTOR:
9497       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9498       break;
9499     case RID_HAS_VIRTUAL_DESTRUCTOR:
9500       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9501       break;
9502     case RID_IS_ABSTRACT:
9503       kind = CPTK_IS_ABSTRACT;
9504       break;
9505     case RID_IS_BASE_OF:
9506       kind = CPTK_IS_BASE_OF;
9507       binary = true;
9508       break;
9509     case RID_IS_CLASS:
9510       kind = CPTK_IS_CLASS;
9511       break;
9512     case RID_IS_EMPTY:
9513       kind = CPTK_IS_EMPTY;
9514       break;
9515     case RID_IS_ENUM:
9516       kind = CPTK_IS_ENUM;
9517       break;
9518     case RID_IS_FINAL:
9519       kind = CPTK_IS_FINAL;
9520       break;
9521     case RID_IS_LITERAL_TYPE:
9522       kind = CPTK_IS_LITERAL_TYPE;
9523       break;
9524     case RID_IS_POD:
9525       kind = CPTK_IS_POD;
9526       break;
9527     case RID_IS_POLYMORPHIC:
9528       kind = CPTK_IS_POLYMORPHIC;
9529       break;
9530     case RID_IS_SAME_AS:
9531       kind = CPTK_IS_SAME_AS;
9532       binary = true;
9533       break;
9534     case RID_IS_STD_LAYOUT:
9535       kind = CPTK_IS_STD_LAYOUT;
9536       break;
9537     case RID_IS_TRIVIAL:
9538       kind = CPTK_IS_TRIVIAL;
9539       break;
9540     case RID_IS_TRIVIALLY_ASSIGNABLE:
9541       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9542       binary = true;
9543       break;
9544     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9545       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9546       variadic = true;
9547       break;
9548     case RID_IS_TRIVIALLY_COPYABLE:
9549       kind = CPTK_IS_TRIVIALLY_COPYABLE;
9550       break;
9551     case RID_IS_UNION:
9552       kind = CPTK_IS_UNION;
9553       break;
9554     case RID_UNDERLYING_TYPE:
9555       kind = CPTK_UNDERLYING_TYPE;
9556       break;
9557     case RID_BASES:
9558       kind = CPTK_BASES;
9559       break;
9560     case RID_DIRECT_BASES:
9561       kind = CPTK_DIRECT_BASES;
9562       break;
9563     default:
9564       gcc_unreachable ();
9565     }
9566
9567   /* Consume the token.  */
9568   cp_lexer_consume_token (parser->lexer);
9569
9570   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9571
9572   {
9573     type_id_in_expr_sentinel s (parser);
9574     type1 = cp_parser_type_id (parser);
9575   }
9576
9577   if (type1 == error_mark_node)
9578     return error_mark_node;
9579
9580   if (binary)
9581     {
9582       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9583  
9584       {
9585         type_id_in_expr_sentinel s (parser);
9586         type2 = cp_parser_type_id (parser);
9587       }
9588
9589       if (type2 == error_mark_node)
9590         return error_mark_node;
9591     }
9592   else if (variadic)
9593     {
9594       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9595         {
9596           cp_lexer_consume_token (parser->lexer);
9597           tree elt = cp_parser_type_id (parser);
9598           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9599             {
9600               cp_lexer_consume_token (parser->lexer);
9601               elt = make_pack_expansion (elt);
9602             }
9603           if (elt == error_mark_node)
9604             return error_mark_node;
9605           type2 = tree_cons (NULL_TREE, elt, type2);
9606         }
9607     }
9608
9609   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9610
9611   /* Complete the trait expression, which may mean either processing
9612      the trait expr now or saving it for template instantiation.  */
9613   switch(kind)
9614     {
9615     case CPTK_UNDERLYING_TYPE:
9616       return finish_underlying_type (type1);
9617     case CPTK_BASES:
9618       return finish_bases (type1, false);
9619     case CPTK_DIRECT_BASES:
9620       return finish_bases (type1, true);
9621     default:
9622       return finish_trait_expr (kind, type1, type2);
9623     }
9624 }
9625
9626 /* Lambdas that appear in variable initializer or default argument scope
9627    get that in their mangling, so we need to record it.  We might as well
9628    use the count for function and namespace scopes as well.  */
9629 static GTY(()) tree lambda_scope;
9630 static GTY(()) int lambda_count;
9631 struct GTY(()) tree_int
9632 {
9633   tree t;
9634   int i;
9635 };
9636 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9637
9638 static void
9639 start_lambda_scope (tree decl)
9640 {
9641   tree_int ti;
9642   gcc_assert (decl);
9643   /* Once we're inside a function, we ignore other scopes and just push
9644      the function again so that popping works properly.  */
9645   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9646     decl = current_function_decl;
9647   ti.t = lambda_scope;
9648   ti.i = lambda_count;
9649   vec_safe_push (lambda_scope_stack, ti);
9650   if (lambda_scope != decl)
9651     {
9652       /* Don't reset the count if we're still in the same function.  */
9653       lambda_scope = decl;
9654       lambda_count = 0;
9655     }
9656 }
9657
9658 static void
9659 record_lambda_scope (tree lambda)
9660 {
9661   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9662   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9663 }
9664
9665 static void
9666 finish_lambda_scope (void)
9667 {
9668   tree_int *p = &lambda_scope_stack->last ();
9669   if (lambda_scope != p->t)
9670     {
9671       lambda_scope = p->t;
9672       lambda_count = p->i;
9673     }
9674   lambda_scope_stack->pop ();
9675 }
9676
9677 /* Parse a lambda expression.
9678
9679    lambda-expression:
9680      lambda-introducer lambda-declarator [opt] compound-statement
9681
9682    Returns a representation of the expression.  */
9683
9684 static cp_expr
9685 cp_parser_lambda_expression (cp_parser* parser)
9686 {
9687   tree lambda_expr = build_lambda_expr ();
9688   tree type;
9689   bool ok = true;
9690   cp_token *token = cp_lexer_peek_token (parser->lexer);
9691   cp_token_position start = 0;
9692
9693   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9694
9695   if (cp_unevaluated_operand)
9696     {
9697       if (!token->error_reported)
9698         {
9699           error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9700                     "lambda-expression in unevaluated context");
9701           token->error_reported = true;
9702         }
9703       ok = false;
9704     }
9705   else if (parser->in_template_argument_list_p)
9706     {
9707       if (!token->error_reported)
9708         {
9709           error_at (token->location, "lambda-expression in template-argument");
9710           token->error_reported = true;
9711         }
9712       ok = false;
9713     }
9714
9715   /* We may be in the middle of deferred access check.  Disable
9716      it now.  */
9717   push_deferring_access_checks (dk_no_deferred);
9718
9719   cp_parser_lambda_introducer (parser, lambda_expr);
9720
9721   type = begin_lambda_type (lambda_expr);
9722   if (type == error_mark_node)
9723     return error_mark_node;
9724
9725   record_lambda_scope (lambda_expr);
9726
9727   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
9728   determine_visibility (TYPE_NAME (type));
9729
9730   /* Now that we've started the type, add the capture fields for any
9731      explicit captures.  */
9732   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9733
9734   {
9735     /* Inside the class, surrounding template-parameter-lists do not apply.  */
9736     unsigned int saved_num_template_parameter_lists
9737         = parser->num_template_parameter_lists;
9738     unsigned char in_statement = parser->in_statement;
9739     bool in_switch_statement_p = parser->in_switch_statement_p;
9740     bool fully_implicit_function_template_p
9741         = parser->fully_implicit_function_template_p;
9742     tree implicit_template_parms = parser->implicit_template_parms;
9743     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9744     bool auto_is_implicit_function_template_parm_p
9745         = parser->auto_is_implicit_function_template_parm_p;
9746
9747     parser->num_template_parameter_lists = 0;
9748     parser->in_statement = 0;
9749     parser->in_switch_statement_p = false;
9750     parser->fully_implicit_function_template_p = false;
9751     parser->implicit_template_parms = 0;
9752     parser->implicit_template_scope = 0;
9753     parser->auto_is_implicit_function_template_parm_p = false;
9754
9755     /* By virtue of defining a local class, a lambda expression has access to
9756        the private variables of enclosing classes.  */
9757
9758     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9759
9760     if (ok && cp_parser_error_occurred (parser))
9761       ok = false;
9762
9763     if (ok)
9764       {
9765         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9766             && cp_parser_start_tentative_firewall (parser))
9767           start = token;
9768         cp_parser_lambda_body (parser, lambda_expr);
9769       }
9770     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9771       {
9772         if (cp_parser_skip_to_closing_brace (parser))
9773           cp_lexer_consume_token (parser->lexer);
9774       }
9775
9776     /* The capture list was built up in reverse order; fix that now.  */
9777     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9778       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9779
9780     if (ok)
9781       maybe_add_lambda_conv_op (type);
9782
9783     type = finish_struct (type, /*attributes=*/NULL_TREE);
9784
9785     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9786     parser->in_statement = in_statement;
9787     parser->in_switch_statement_p = in_switch_statement_p;
9788     parser->fully_implicit_function_template_p
9789         = fully_implicit_function_template_p;
9790     parser->implicit_template_parms = implicit_template_parms;
9791     parser->implicit_template_scope = implicit_template_scope;
9792     parser->auto_is_implicit_function_template_parm_p
9793         = auto_is_implicit_function_template_parm_p;
9794   }
9795
9796   /* This field is only used during parsing of the lambda.  */
9797   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9798
9799   /* This lambda shouldn't have any proxies left at this point.  */
9800   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9801   /* And now that we're done, push proxies for an enclosing lambda.  */
9802   insert_pending_capture_proxies ();
9803
9804   if (ok)
9805     lambda_expr = build_lambda_object (lambda_expr);
9806   else
9807     lambda_expr = error_mark_node;
9808
9809   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9810
9811   pop_deferring_access_checks ();
9812
9813   return lambda_expr;
9814 }
9815
9816 /* Parse the beginning of a lambda expression.
9817
9818    lambda-introducer:
9819      [ lambda-capture [opt] ]
9820
9821    LAMBDA_EXPR is the current representation of the lambda expression.  */
9822
9823 static void
9824 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9825 {
9826   /* Need commas after the first capture.  */
9827   bool first = true;
9828
9829   /* Eat the leading `['.  */
9830   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9831
9832   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
9833   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9834       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9835     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9836   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9837     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9838
9839   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9840     {
9841       cp_lexer_consume_token (parser->lexer);
9842       first = false;
9843     }
9844
9845   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9846     {
9847       cp_token* capture_token;
9848       tree capture_id;
9849       tree capture_init_expr;
9850       cp_id_kind idk = CP_ID_KIND_NONE;
9851       bool explicit_init_p = false;
9852
9853       enum capture_kind_type
9854       {
9855         BY_COPY,
9856         BY_REFERENCE
9857       };
9858       enum capture_kind_type capture_kind = BY_COPY;
9859
9860       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9861         {
9862           error ("expected end of capture-list");
9863           return;
9864         }
9865
9866       if (first)
9867         first = false;
9868       else
9869         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9870
9871       /* Possibly capture `this'.  */
9872       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9873         {
9874           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9875           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9876             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9877                      "with by-copy capture default");
9878           cp_lexer_consume_token (parser->lexer);
9879           add_capture (lambda_expr,
9880                        /*id=*/this_identifier,
9881                        /*initializer=*/finish_this_expr(),
9882                        /*by_reference_p=*/false,
9883                        explicit_init_p);
9884           continue;
9885         }
9886
9887       /* Remember whether we want to capture as a reference or not.  */
9888       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9889         {
9890           capture_kind = BY_REFERENCE;
9891           cp_lexer_consume_token (parser->lexer);
9892         }
9893
9894       /* Get the identifier.  */
9895       capture_token = cp_lexer_peek_token (parser->lexer);
9896       capture_id = cp_parser_identifier (parser);
9897
9898       if (capture_id == error_mark_node)
9899         /* Would be nice to have a cp_parser_skip_to_closing_x for general
9900            delimiters, but I modified this to stop on unnested ']' as well.  It
9901            was already changed to stop on unnested '}', so the
9902            "closing_parenthesis" name is no more misleading with my change.  */
9903         {
9904           cp_parser_skip_to_closing_parenthesis (parser,
9905                                                  /*recovering=*/true,
9906                                                  /*or_comma=*/true,
9907                                                  /*consume_paren=*/true);
9908           break;
9909         }
9910
9911       /* Find the initializer for this capture.  */
9912       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9913           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9914           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9915         {
9916           bool direct, non_constant;
9917           /* An explicit initializer exists.  */
9918           if (cxx_dialect < cxx14)
9919             pedwarn (input_location, 0,
9920                      "lambda capture initializers "
9921                      "only available with -std=c++14 or -std=gnu++14");
9922           capture_init_expr = cp_parser_initializer (parser, &direct,
9923                                                      &non_constant);
9924           explicit_init_p = true;
9925           if (capture_init_expr == NULL_TREE)
9926             {
9927               error ("empty initializer for lambda init-capture");
9928               capture_init_expr = error_mark_node;
9929             }
9930         }
9931       else
9932         {
9933           const char* error_msg;
9934
9935           /* Turn the identifier into an id-expression.  */
9936           capture_init_expr
9937             = cp_parser_lookup_name_simple (parser, capture_id,
9938                                             capture_token->location);
9939
9940           if (capture_init_expr == error_mark_node)
9941             {
9942               unqualified_name_lookup_error (capture_id);
9943               continue;
9944             }
9945           else if (DECL_P (capture_init_expr)
9946                    && (!VAR_P (capture_init_expr)
9947                        && TREE_CODE (capture_init_expr) != PARM_DECL))
9948             {
9949               error_at (capture_token->location,
9950                         "capture of non-variable %qD ",
9951                         capture_init_expr);
9952               inform (DECL_SOURCE_LOCATION (capture_init_expr),
9953                       "%q#D declared here", capture_init_expr);
9954               continue;
9955             }
9956           if (VAR_P (capture_init_expr)
9957               && decl_storage_duration (capture_init_expr) != dk_auto)
9958             {
9959               if (pedwarn (capture_token->location, 0, "capture of variable "
9960                            "%qD with non-automatic storage duration",
9961                            capture_init_expr))
9962                 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9963                         "%q#D declared here", capture_init_expr);
9964               continue;
9965             }
9966
9967           capture_init_expr
9968             = finish_id_expression
9969                 (capture_id,
9970                  capture_init_expr,
9971                  parser->scope,
9972                  &idk,
9973                  /*integral_constant_expression_p=*/false,
9974                  /*allow_non_integral_constant_expression_p=*/false,
9975                  /*non_integral_constant_expression_p=*/NULL,
9976                  /*template_p=*/false,
9977                  /*done=*/true,
9978                  /*address_p=*/false,
9979                  /*template_arg_p=*/false,
9980                  &error_msg,
9981                  capture_token->location);
9982
9983           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9984             {
9985               cp_lexer_consume_token (parser->lexer);
9986               capture_init_expr = make_pack_expansion (capture_init_expr);
9987             }
9988           else
9989             check_for_bare_parameter_packs (capture_init_expr);
9990         }
9991
9992       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9993           && !explicit_init_p)
9994         {
9995           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9996               && capture_kind == BY_COPY)
9997             pedwarn (capture_token->location, 0, "explicit by-copy capture "
9998                      "of %qD redundant with by-copy capture default",
9999                      capture_id);
10000           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10001               && capture_kind == BY_REFERENCE)
10002             pedwarn (capture_token->location, 0, "explicit by-reference "
10003                      "capture of %qD redundant with by-reference capture "
10004                      "default", capture_id);
10005         }
10006
10007       add_capture (lambda_expr,
10008                    capture_id,
10009                    capture_init_expr,
10010                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
10011                    explicit_init_p);
10012     }
10013
10014   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10015 }
10016
10017 /* Parse the (optional) middle of a lambda expression.
10018
10019    lambda-declarator:
10020      < template-parameter-list [opt] >
10021      ( parameter-declaration-clause [opt] )
10022        attribute-specifier [opt]
10023        mutable [opt]
10024        exception-specification [opt]
10025        lambda-return-type-clause [opt]
10026
10027    LAMBDA_EXPR is the current representation of the lambda expression.  */
10028
10029 static bool
10030 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10031 {
10032   /* 5.1.1.4 of the standard says:
10033        If a lambda-expression does not include a lambda-declarator, it is as if
10034        the lambda-declarator were ().
10035      This means an empty parameter list, no attributes, and no exception
10036      specification.  */
10037   tree param_list = void_list_node;
10038   tree attributes = NULL_TREE;
10039   tree exception_spec = NULL_TREE;
10040   tree template_param_list = NULL_TREE;
10041   tree tx_qual = NULL_TREE;
10042
10043   /* The template-parameter-list is optional, but must begin with
10044      an opening angle if present.  */
10045   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10046     {
10047       if (cxx_dialect < cxx14)
10048         pedwarn (parser->lexer->next_token->location, 0,
10049                  "lambda templates are only available with "
10050                  "-std=c++14 or -std=gnu++14");
10051
10052       cp_lexer_consume_token (parser->lexer);
10053
10054       template_param_list = cp_parser_template_parameter_list (parser);
10055
10056       cp_parser_skip_to_end_of_template_parameter_list (parser);
10057
10058       /* We just processed one more parameter list.  */
10059       ++parser->num_template_parameter_lists;
10060     }
10061
10062   /* The parameter-declaration-clause is optional (unless
10063      template-parameter-list was given), but must begin with an
10064      opening parenthesis if present.  */
10065   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10066     {
10067       cp_lexer_consume_token (parser->lexer);
10068
10069       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10070
10071       /* Parse parameters.  */
10072       param_list = cp_parser_parameter_declaration_clause (parser);
10073
10074       /* Default arguments shall not be specified in the
10075          parameter-declaration-clause of a lambda-declarator.  */
10076       for (tree t = param_list; t; t = TREE_CHAIN (t))
10077         if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10078           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10079                    "default argument specified for lambda parameter");
10080
10081       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10082
10083       attributes = cp_parser_attributes_opt (parser);
10084
10085       /* Parse optional `mutable' keyword.  */
10086       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10087         {
10088           cp_lexer_consume_token (parser->lexer);
10089           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10090         }
10091
10092       tx_qual = cp_parser_tx_qualifier_opt (parser);
10093
10094       /* Parse optional exception specification.  */
10095       exception_spec = cp_parser_exception_specification_opt (parser);
10096
10097       /* Parse optional trailing return type.  */
10098       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10099         {
10100           cp_lexer_consume_token (parser->lexer);
10101           LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10102             = cp_parser_trailing_type_id (parser);
10103         }
10104
10105       /* The function parameters must be in scope all the way until after the
10106          trailing-return-type in case of decltype.  */
10107       pop_bindings_and_leave_scope ();
10108     }
10109   else if (template_param_list != NULL_TREE) // generate diagnostic
10110     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10111
10112   /* Create the function call operator.
10113
10114      Messing with declarators like this is no uglier than building up the
10115      FUNCTION_DECL by hand, and this is less likely to get out of sync with
10116      other code.  */
10117   {
10118     cp_decl_specifier_seq return_type_specs;
10119     cp_declarator* declarator;
10120     tree fco;
10121     int quals;
10122     void *p;
10123
10124     clear_decl_specs (&return_type_specs);
10125     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10126       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10127     else
10128       /* Maybe we will deduce the return type later.  */
10129       return_type_specs.type = make_auto ();
10130
10131     p = obstack_alloc (&declarator_obstack, 0);
10132
10133     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10134                                      sfk_none);
10135
10136     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10137              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10138     declarator = make_call_declarator (declarator, param_list, quals,
10139                                        VIRT_SPEC_UNSPECIFIED,
10140                                        REF_QUAL_NONE,
10141                                        tx_qual,
10142                                        exception_spec,
10143                                        /*late_return_type=*/NULL_TREE,
10144                                        /*requires_clause*/NULL_TREE);
10145     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10146
10147     fco = grokmethod (&return_type_specs,
10148                       declarator,
10149                       attributes);
10150     if (fco != error_mark_node)
10151       {
10152         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10153         DECL_ARTIFICIAL (fco) = 1;
10154         /* Give the object parameter a different name.  */
10155         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10156         if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10157           TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10158       }
10159     if (template_param_list)
10160       {
10161         fco = finish_member_template_decl (fco);
10162         finish_template_decl (template_param_list);
10163         --parser->num_template_parameter_lists;
10164       }
10165     else if (parser->fully_implicit_function_template_p)
10166       fco = finish_fully_implicit_template (parser, fco);
10167
10168     finish_member_declaration (fco);
10169
10170     obstack_free (&declarator_obstack, p);
10171
10172     return (fco != error_mark_node);
10173   }
10174 }
10175
10176 /* Parse the body of a lambda expression, which is simply
10177
10178    compound-statement
10179
10180    but which requires special handling.
10181    LAMBDA_EXPR is the current representation of the lambda expression.  */
10182
10183 static void
10184 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10185 {
10186   bool nested = (current_function_decl != NULL_TREE);
10187   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10188   if (nested)
10189     push_function_context ();
10190   else
10191     /* Still increment function_depth so that we don't GC in the
10192        middle of an expression.  */
10193     ++function_depth;
10194   vec<tree> omp_privatization_save;
10195   save_omp_privatization_clauses (omp_privatization_save);
10196   /* Clear this in case we're in the middle of a default argument.  */
10197   parser->local_variables_forbidden_p = false;
10198
10199   /* Finish the function call operator
10200      - class_specifier
10201      + late_parsing_for_member
10202      + function_definition_after_declarator
10203      + ctor_initializer_opt_and_function_body  */
10204   {
10205     tree fco = lambda_function (lambda_expr);
10206     tree body;
10207     bool done = false;
10208     tree compound_stmt;
10209     tree cap;
10210
10211     /* Let the front end know that we are going to be defining this
10212        function.  */
10213     start_preparsed_function (fco,
10214                               NULL_TREE,
10215                               SF_PRE_PARSED | SF_INCLASS_INLINE);
10216
10217     start_lambda_scope (fco);
10218     body = begin_function_body ();
10219
10220     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10221       goto out;
10222
10223     /* Push the proxies for any explicit captures.  */
10224     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10225          cap = TREE_CHAIN (cap))
10226       build_capture_proxy (TREE_PURPOSE (cap));
10227
10228     compound_stmt = begin_compound_stmt (0);
10229
10230     /* 5.1.1.4 of the standard says:
10231          If a lambda-expression does not include a trailing-return-type, it
10232          is as if the trailing-return-type denotes the following type:
10233           * if the compound-statement is of the form
10234                { return attribute-specifier [opt] expression ; }
10235              the type of the returned expression after lvalue-to-rvalue
10236              conversion (_conv.lval_ 4.1), array-to-pointer conversion
10237              (_conv.array_ 4.2), and function-to-pointer conversion
10238              (_conv.func_ 4.3);
10239           * otherwise, void.  */
10240
10241     /* In a lambda that has neither a lambda-return-type-clause
10242        nor a deducible form, errors should be reported for return statements
10243        in the body.  Since we used void as the placeholder return type, parsing
10244        the body as usual will give such desired behavior.  */
10245     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10246         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10247         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10248       {
10249         tree expr = NULL_TREE;
10250         cp_id_kind idk = CP_ID_KIND_NONE;
10251
10252         /* Parse tentatively in case there's more after the initial return
10253            statement.  */
10254         cp_parser_parse_tentatively (parser);
10255
10256         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10257
10258         expr = cp_parser_expression (parser, &idk);
10259
10260         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10261         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10262
10263         if (cp_parser_parse_definitely (parser))
10264           {
10265             if (!processing_template_decl)
10266               {
10267                 tree type = lambda_return_type (expr);
10268                 apply_deduced_return_type (fco, type);
10269                 if (type == error_mark_node)
10270                   expr = error_mark_node;
10271               }
10272
10273             /* Will get error here if type not deduced yet.  */
10274             finish_return_stmt (expr);
10275
10276             done = true;
10277           }
10278       }
10279
10280     if (!done)
10281       {
10282         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10283           cp_parser_label_declaration (parser);
10284         cp_parser_statement_seq_opt (parser, NULL_TREE);
10285         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10286       }
10287
10288     finish_compound_stmt (compound_stmt);
10289
10290   out:
10291     finish_function_body (body);
10292     finish_lambda_scope ();
10293
10294     /* Finish the function and generate code for it if necessary.  */
10295     tree fn = finish_function (/*inline*/2);
10296
10297     /* Only expand if the call op is not a template.  */
10298     if (!DECL_TEMPLATE_INFO (fco))
10299       expand_or_defer_fn (fn);
10300   }
10301
10302   restore_omp_privatization_clauses (omp_privatization_save);
10303   parser->local_variables_forbidden_p = local_variables_forbidden_p;
10304   if (nested)
10305     pop_function_context();
10306   else
10307     --function_depth;
10308 }
10309
10310 /* Statements [gram.stmt.stmt]  */
10311
10312 /* Parse a statement.
10313
10314    statement:
10315      labeled-statement
10316      expression-statement
10317      compound-statement
10318      selection-statement
10319      iteration-statement
10320      jump-statement
10321      declaration-statement
10322      try-block
10323
10324   C++11:
10325
10326   statement:
10327     labeled-statement
10328     attribute-specifier-seq (opt) expression-statement
10329     attribute-specifier-seq (opt) compound-statement
10330     attribute-specifier-seq (opt) selection-statement
10331     attribute-specifier-seq (opt) iteration-statement
10332     attribute-specifier-seq (opt) jump-statement
10333     declaration-statement
10334     attribute-specifier-seq (opt) try-block
10335
10336   TM Extension:
10337
10338    statement:
10339      atomic-statement
10340
10341   IN_COMPOUND is true when the statement is nested inside a
10342   cp_parser_compound_statement; this matters for certain pragmas.
10343
10344   If IF_P is not NULL, *IF_P is set to indicate whether the statement
10345   is a (possibly labeled) if statement which is not enclosed in braces
10346   and has an else clause.  This is used to implement -Wparentheses.
10347
10348   CHAIN is a vector of if-else-if conditions.  */
10349
10350 static void
10351 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10352                      bool in_compound, bool *if_p, vec<tree> *chain)
10353 {
10354   tree statement, std_attrs = NULL_TREE;
10355   cp_token *token;
10356   location_t statement_location, attrs_location;
10357
10358  restart:
10359   if (if_p != NULL)
10360     *if_p = false;
10361   /* There is no statement yet.  */
10362   statement = NULL_TREE;
10363
10364   saved_token_sentinel saved_tokens (parser->lexer);
10365   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10366   if (c_dialect_objc ())
10367     /* In obj-c++, seeing '[[' might be the either the beginning of
10368        c++11 attributes, or a nested objc-message-expression.  So
10369        let's parse the c++11 attributes tentatively.  */
10370     cp_parser_parse_tentatively (parser);
10371   std_attrs = cp_parser_std_attribute_spec_seq (parser);
10372   if (c_dialect_objc ())
10373     {
10374       if (!cp_parser_parse_definitely (parser))
10375         std_attrs = NULL_TREE;
10376     }
10377
10378   /* Peek at the next token.  */
10379   token = cp_lexer_peek_token (parser->lexer);
10380   /* Remember the location of the first token in the statement.  */
10381   statement_location = token->location;
10382   /* If this is a keyword, then that will often determine what kind of
10383      statement we have.  */
10384   if (token->type == CPP_KEYWORD)
10385     {
10386       enum rid keyword = token->keyword;
10387
10388       switch (keyword)
10389         {
10390         case RID_CASE:
10391         case RID_DEFAULT:
10392           /* Looks like a labeled-statement with a case label.
10393              Parse the label, and then use tail recursion to parse
10394              the statement.  */
10395           cp_parser_label_for_labeled_statement (parser, std_attrs);
10396           in_compound = false;
10397           goto restart;
10398
10399         case RID_IF:
10400         case RID_SWITCH:
10401           statement = cp_parser_selection_statement (parser, if_p, chain);
10402           break;
10403
10404         case RID_WHILE:
10405         case RID_DO:
10406         case RID_FOR:
10407           statement = cp_parser_iteration_statement (parser, if_p, false);
10408           break;
10409
10410         case RID_CILK_FOR:
10411           if (!flag_cilkplus)
10412             {
10413               error_at (cp_lexer_peek_token (parser->lexer)->location,
10414                         "-fcilkplus must be enabled to use %<_Cilk_for%>");
10415               cp_lexer_consume_token (parser->lexer);
10416               statement = error_mark_node;
10417             }
10418           else
10419             statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10420           break;
10421
10422         case RID_BREAK:
10423         case RID_CONTINUE:
10424         case RID_RETURN:
10425         case RID_GOTO:
10426           statement = cp_parser_jump_statement (parser);
10427           break;
10428
10429         case RID_CILK_SYNC:
10430           cp_lexer_consume_token (parser->lexer);
10431           if (flag_cilkplus)
10432             {
10433               tree sync_expr = build_cilk_sync ();
10434               SET_EXPR_LOCATION (sync_expr,
10435                                  token->location);
10436               statement = finish_expr_stmt (sync_expr);
10437             }
10438           else
10439             {
10440               error_at (token->location, "-fcilkplus must be enabled to use"
10441                         " %<_Cilk_sync%>");
10442               statement = error_mark_node;
10443             }
10444           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10445           break;
10446
10447           /* Objective-C++ exception-handling constructs.  */
10448         case RID_AT_TRY:
10449         case RID_AT_CATCH:
10450         case RID_AT_FINALLY:
10451         case RID_AT_SYNCHRONIZED:
10452         case RID_AT_THROW:
10453           statement = cp_parser_objc_statement (parser);
10454           break;
10455
10456         case RID_TRY:
10457           statement = cp_parser_try_block (parser);
10458           break;
10459
10460         case RID_NAMESPACE:
10461           /* This must be a namespace alias definition.  */
10462           cp_parser_declaration_statement (parser);
10463           return;
10464           
10465         case RID_TRANSACTION_ATOMIC:
10466         case RID_TRANSACTION_RELAXED:
10467         case RID_SYNCHRONIZED:
10468         case RID_ATOMIC_NOEXCEPT:
10469         case RID_ATOMIC_CANCEL:
10470           statement = cp_parser_transaction (parser, token);
10471           break;
10472         case RID_TRANSACTION_CANCEL:
10473           statement = cp_parser_transaction_cancel (parser);
10474           break;
10475
10476         default:
10477           /* It might be a keyword like `int' that can start a
10478              declaration-statement.  */
10479           break;
10480         }
10481     }
10482   else if (token->type == CPP_NAME)
10483     {
10484       /* If the next token is a `:', then we are looking at a
10485          labeled-statement.  */
10486       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10487       if (token->type == CPP_COLON)
10488         {
10489           /* Looks like a labeled-statement with an ordinary label.
10490              Parse the label, and then use tail recursion to parse
10491              the statement.  */
10492
10493           cp_parser_label_for_labeled_statement (parser, std_attrs);
10494           in_compound = false;
10495           goto restart;
10496         }
10497     }
10498   /* Anything that starts with a `{' must be a compound-statement.  */
10499   else if (token->type == CPP_OPEN_BRACE)
10500     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10501   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10502      a statement all its own.  */
10503   else if (token->type == CPP_PRAGMA)
10504     {
10505       /* Only certain OpenMP pragmas are attached to statements, and thus
10506          are considered statements themselves.  All others are not.  In
10507          the context of a compound, accept the pragma as a "statement" and
10508          return so that we can check for a close brace.  Otherwise we
10509          require a real statement and must go back and read one.  */
10510       if (in_compound)
10511         cp_parser_pragma (parser, pragma_compound, if_p);
10512       else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10513         goto restart;
10514       return;
10515     }
10516   else if (token->type == CPP_EOF)
10517     {
10518       cp_parser_error (parser, "expected statement");
10519       return;
10520     }
10521
10522   /* Everything else must be a declaration-statement or an
10523      expression-statement.  Try for the declaration-statement
10524      first, unless we are looking at a `;', in which case we know that
10525      we have an expression-statement.  */
10526   if (!statement)
10527     {
10528       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10529         {
10530           if (std_attrs != NULL_TREE)
10531             {
10532               /*  Attributes should be parsed as part of the the
10533                   declaration, so let's un-parse them.  */
10534               saved_tokens.rollback();
10535               std_attrs = NULL_TREE;
10536             }
10537
10538           cp_parser_parse_tentatively (parser);
10539           /* Try to parse the declaration-statement.  */
10540           cp_parser_declaration_statement (parser);
10541           /* If that worked, we're done.  */
10542           if (cp_parser_parse_definitely (parser))
10543             return;
10544         }
10545       /* Look for an expression-statement instead.  */
10546       statement = cp_parser_expression_statement (parser, in_statement_expr);
10547     }
10548
10549   /* Set the line number for the statement.  */
10550   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10551     SET_EXPR_LOCATION (statement, statement_location);
10552
10553   /* Note that for now, we don't do anything with c++11 statements
10554      parsed at this level.  */
10555   if (std_attrs != NULL_TREE)
10556     warning_at (attrs_location,
10557                 OPT_Wattributes,
10558                 "attributes at the beginning of statement are ignored");
10559 }
10560
10561 /* Parse the label for a labeled-statement, i.e.
10562
10563    identifier :
10564    case constant-expression :
10565    default :
10566
10567    GNU Extension:
10568    case constant-expression ... constant-expression : statement
10569
10570    When a label is parsed without errors, the label is added to the
10571    parse tree by the finish_* functions, so this function doesn't
10572    have to return the label.  */
10573
10574 static void
10575 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10576 {
10577   cp_token *token;
10578   tree label = NULL_TREE;
10579   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10580
10581   /* The next token should be an identifier.  */
10582   token = cp_lexer_peek_token (parser->lexer);
10583   if (token->type != CPP_NAME
10584       && token->type != CPP_KEYWORD)
10585     {
10586       cp_parser_error (parser, "expected labeled-statement");
10587       return;
10588     }
10589
10590   parser->colon_corrects_to_scope_p = false;
10591   switch (token->keyword)
10592     {
10593     case RID_CASE:
10594       {
10595         tree expr, expr_hi;
10596         cp_token *ellipsis;
10597
10598         /* Consume the `case' token.  */
10599         cp_lexer_consume_token (parser->lexer);
10600         /* Parse the constant-expression.  */
10601         expr = cp_parser_constant_expression (parser);
10602         if (check_for_bare_parameter_packs (expr))
10603           expr = error_mark_node;
10604
10605         ellipsis = cp_lexer_peek_token (parser->lexer);
10606         if (ellipsis->type == CPP_ELLIPSIS)
10607           {
10608             /* Consume the `...' token.  */
10609             cp_lexer_consume_token (parser->lexer);
10610             expr_hi = cp_parser_constant_expression (parser);
10611             if (check_for_bare_parameter_packs (expr_hi))
10612               expr_hi = error_mark_node;
10613
10614             /* We don't need to emit warnings here, as the common code
10615                will do this for us.  */
10616           }
10617         else
10618           expr_hi = NULL_TREE;
10619
10620         if (parser->in_switch_statement_p)
10621           finish_case_label (token->location, expr, expr_hi);
10622         else
10623           error_at (token->location,
10624                     "case label %qE not within a switch statement",
10625                     expr);
10626       }
10627       break;
10628
10629     case RID_DEFAULT:
10630       /* Consume the `default' token.  */
10631       cp_lexer_consume_token (parser->lexer);
10632
10633       if (parser->in_switch_statement_p)
10634         finish_case_label (token->location, NULL_TREE, NULL_TREE);
10635       else
10636         error_at (token->location, "case label not within a switch statement");
10637       break;
10638
10639     default:
10640       /* Anything else must be an ordinary label.  */
10641       label = finish_label_stmt (cp_parser_identifier (parser));
10642       break;
10643     }
10644
10645   /* Require the `:' token.  */
10646   cp_parser_require (parser, CPP_COLON, RT_COLON);
10647
10648   /* An ordinary label may optionally be followed by attributes.
10649      However, this is only permitted if the attributes are then
10650      followed by a semicolon.  This is because, for backward
10651      compatibility, when parsing
10652        lab: __attribute__ ((unused)) int i;
10653      we want the attribute to attach to "i", not "lab".  */
10654   if (label != NULL_TREE
10655       && cp_next_tokens_can_be_gnu_attribute_p (parser))
10656     {
10657       tree attrs;
10658       cp_parser_parse_tentatively (parser);
10659       attrs = cp_parser_gnu_attributes_opt (parser);
10660       if (attrs == NULL_TREE
10661           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10662         cp_parser_abort_tentative_parse (parser);
10663       else if (!cp_parser_parse_definitely (parser))
10664         ;
10665       else
10666         attributes = chainon (attributes, attrs);
10667     }
10668
10669   if (attributes != NULL_TREE)
10670     cplus_decl_attributes (&label, attributes, 0);
10671
10672   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10673 }
10674
10675 /* Parse an expression-statement.
10676
10677    expression-statement:
10678      expression [opt] ;
10679
10680    Returns the new EXPR_STMT -- or NULL_TREE if the expression
10681    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10682    indicates whether this expression-statement is part of an
10683    expression statement.  */
10684
10685 static tree
10686 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10687 {
10688   tree statement = NULL_TREE;
10689   cp_token *token = cp_lexer_peek_token (parser->lexer);
10690
10691   /* If the next token is a ';', then there is no expression
10692      statement.  */
10693   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10694     {
10695       statement = cp_parser_expression (parser);
10696       if (statement == error_mark_node
10697           && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10698         {
10699           cp_parser_skip_to_end_of_block_or_statement (parser);
10700           return error_mark_node;
10701         }
10702     }
10703
10704   /* Give a helpful message for "A<T>::type t;" and the like.  */
10705   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10706       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10707     {
10708       if (TREE_CODE (statement) == SCOPE_REF)
10709         error_at (token->location, "need %<typename%> before %qE because "
10710                   "%qT is a dependent scope",
10711                   statement, TREE_OPERAND (statement, 0));
10712       else if (is_overloaded_fn (statement)
10713                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10714         {
10715           /* A::A a; */
10716           tree fn = get_first_fn (statement);
10717           error_at (token->location,
10718                     "%<%T::%D%> names the constructor, not the type",
10719                     DECL_CONTEXT (fn), DECL_NAME (fn));
10720         }
10721     }
10722
10723   /* Consume the final `;'.  */
10724   cp_parser_consume_semicolon_at_end_of_statement (parser);
10725
10726   if (in_statement_expr
10727       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10728     /* This is the final expression statement of a statement
10729        expression.  */
10730     statement = finish_stmt_expr_expr (statement, in_statement_expr);
10731   else if (statement)
10732     statement = finish_expr_stmt (statement);
10733
10734   return statement;
10735 }
10736
10737 /* Parse a compound-statement.
10738
10739    compound-statement:
10740      { statement-seq [opt] }
10741
10742    GNU extension:
10743
10744    compound-statement:
10745      { label-declaration-seq [opt] statement-seq [opt] }
10746
10747    label-declaration-seq:
10748      label-declaration
10749      label-declaration-seq label-declaration
10750
10751    Returns a tree representing the statement.  */
10752
10753 static tree
10754 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10755                               int bcs_flags, bool function_body)
10756 {
10757   tree compound_stmt;
10758
10759   /* Consume the `{'.  */
10760   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10761     return error_mark_node;
10762   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10763       && !function_body && cxx_dialect < cxx14)
10764     pedwarn (input_location, OPT_Wpedantic,
10765              "compound-statement in constexpr function");
10766   /* Begin the compound-statement.  */
10767   compound_stmt = begin_compound_stmt (bcs_flags);
10768   /* If the next keyword is `__label__' we have a label declaration.  */
10769   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10770     cp_parser_label_declaration (parser);
10771   /* Parse an (optional) statement-seq.  */
10772   cp_parser_statement_seq_opt (parser, in_statement_expr);
10773   /* Finish the compound-statement.  */
10774   finish_compound_stmt (compound_stmt);
10775   /* Consume the `}'.  */
10776   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10777
10778   return compound_stmt;
10779 }
10780
10781 /* Parse an (optional) statement-seq.
10782
10783    statement-seq:
10784      statement
10785      statement-seq [opt] statement  */
10786
10787 static void
10788 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10789 {
10790   /* Scan statements until there aren't any more.  */
10791   while (true)
10792     {
10793       cp_token *token = cp_lexer_peek_token (parser->lexer);
10794
10795       /* If we are looking at a `}', then we have run out of
10796          statements; the same is true if we have reached the end
10797          of file, or have stumbled upon a stray '@end'.  */
10798       if (token->type == CPP_CLOSE_BRACE
10799           || token->type == CPP_EOF
10800           || token->type == CPP_PRAGMA_EOL
10801           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10802         break;
10803       
10804       /* If we are in a compound statement and find 'else' then
10805          something went wrong.  */
10806       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10807         {
10808           if (parser->in_statement & IN_IF_STMT) 
10809             break;
10810           else
10811             {
10812               token = cp_lexer_consume_token (parser->lexer);
10813               error_at (token->location, "%<else%> without a previous %<if%>");
10814             }
10815         }
10816
10817       /* Parse the statement.  */
10818       cp_parser_statement (parser, in_statement_expr, true, NULL);
10819     }
10820 }
10821
10822 /* Parse a selection-statement.
10823
10824    selection-statement:
10825      if ( condition ) statement
10826      if ( condition ) statement else statement
10827      switch ( condition ) statement
10828
10829    Returns the new IF_STMT or SWITCH_STMT.
10830
10831    If IF_P is not NULL, *IF_P is set to indicate whether the statement
10832    is a (possibly labeled) if statement which is not enclosed in
10833    braces and has an else clause.  This is used to implement
10834    -Wparentheses.
10835
10836    CHAIN is a vector of if-else-if conditions.  This is used to implement
10837    -Wduplicated-cond.  */
10838
10839 static tree
10840 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10841                                vec<tree> *chain)
10842 {
10843   cp_token *token;
10844   enum rid keyword;
10845   token_indent_info guard_tinfo;
10846
10847   if (if_p != NULL)
10848     *if_p = false;
10849
10850   /* Peek at the next token.  */
10851   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10852   guard_tinfo = get_token_indent_info (token);
10853
10854   /* See what kind of keyword it is.  */
10855   keyword = token->keyword;
10856   switch (keyword)
10857     {
10858     case RID_IF:
10859     case RID_SWITCH:
10860       {
10861         tree statement;
10862         tree condition;
10863
10864         /* Look for the `('.  */
10865         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10866           {
10867             cp_parser_skip_to_end_of_statement (parser);
10868             return error_mark_node;
10869           }
10870
10871         /* Begin the selection-statement.  */
10872         if (keyword == RID_IF)
10873           statement = begin_if_stmt ();
10874         else
10875           statement = begin_switch_stmt ();
10876
10877         /* Parse the condition.  */
10878         condition = cp_parser_condition (parser);
10879         /* Look for the `)'.  */
10880         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10881           cp_parser_skip_to_closing_parenthesis (parser, true, false,
10882                                                  /*consume_paren=*/true);
10883
10884         if (keyword == RID_IF)
10885           {
10886             bool nested_if;
10887             unsigned char in_statement;
10888
10889             /* Add the condition.  */
10890             finish_if_stmt_cond (condition, statement);
10891
10892             if (warn_duplicated_cond)
10893               warn_duplicated_cond_add_or_warn (token->location, condition,
10894                                                 &chain);
10895
10896             /* Parse the then-clause.  */
10897             in_statement = parser->in_statement;
10898             parser->in_statement |= IN_IF_STMT;
10899             cp_parser_implicitly_scoped_statement (parser, &nested_if,
10900                                                    guard_tinfo);
10901             parser->in_statement = in_statement;
10902
10903             finish_then_clause (statement);
10904
10905             /* If the next token is `else', parse the else-clause.  */
10906             if (cp_lexer_next_token_is_keyword (parser->lexer,
10907                                                 RID_ELSE))
10908               {
10909                 guard_tinfo
10910                   = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10911                 /* Consume the `else' keyword.  */
10912                 cp_lexer_consume_token (parser->lexer);
10913                 if (warn_duplicated_cond)
10914                   {
10915                     if (cp_lexer_next_token_is_keyword (parser->lexer,
10916                                                         RID_IF)
10917                         && chain == NULL)
10918                       {
10919                         /* We've got "if (COND) else if (COND2)".  Start
10920                            the condition chain and add COND as the first
10921                            element.  */
10922                         chain = new vec<tree> ();
10923                         if (!CONSTANT_CLASS_P (condition)
10924                             && !TREE_SIDE_EFFECTS (condition))
10925                         {
10926                           /* Wrap it in a NOP_EXPR so that we can set the
10927                              location of the condition.  */
10928                           tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10929                                            condition);
10930                           SET_EXPR_LOCATION (e, token->location);
10931                           chain->safe_push (e);
10932                         }
10933                       }
10934                     else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10935                                                               RID_IF))
10936                       {
10937                         /* This is if-else without subsequent if.  Zap the
10938                            condition chain; we would have already warned at
10939                            this point.  */
10940                         delete chain;
10941                         chain = NULL;
10942                       }
10943                   }
10944                 begin_else_clause (statement);
10945                 /* Parse the else-clause.  */
10946                 cp_parser_implicitly_scoped_statement (parser, NULL,
10947                                                        guard_tinfo, chain);
10948
10949                 finish_else_clause (statement);
10950
10951                 /* If we are currently parsing a then-clause, then
10952                    IF_P will not be NULL.  We set it to true to
10953                    indicate that this if statement has an else clause.
10954                    This may trigger the Wparentheses warning below
10955                    when we get back up to the parent if statement.  */
10956                 if (if_p != NULL)
10957                   *if_p = true;
10958               }
10959             else
10960               {
10961                 /* This if statement does not have an else clause.  If
10962                    NESTED_IF is true, then the then-clause has an if
10963                    statement which does have an else clause.  We warn
10964                    about the potential ambiguity.  */
10965                 if (nested_if)
10966                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10967                               "suggest explicit braces to avoid ambiguous"
10968                               " %<else%>");
10969                 if (warn_duplicated_cond)
10970                   {
10971                     /* We don't need the condition chain anymore.  */
10972                     delete chain;
10973                     chain = NULL;
10974                   }
10975               }
10976
10977             /* Now we're all done with the if-statement.  */
10978             finish_if_stmt (statement);
10979           }
10980         else
10981           {
10982             bool in_switch_statement_p;
10983             unsigned char in_statement;
10984
10985             /* Add the condition.  */
10986             finish_switch_cond (condition, statement);
10987
10988             /* Parse the body of the switch-statement.  */
10989             in_switch_statement_p = parser->in_switch_statement_p;
10990             in_statement = parser->in_statement;
10991             parser->in_switch_statement_p = true;
10992             parser->in_statement |= IN_SWITCH_STMT;
10993             cp_parser_implicitly_scoped_statement (parser, NULL,
10994                                                    guard_tinfo);
10995             parser->in_switch_statement_p = in_switch_statement_p;
10996             parser->in_statement = in_statement;
10997
10998             /* Now we're all done with the switch-statement.  */
10999             finish_switch_stmt (statement);
11000           }
11001
11002         return statement;
11003       }
11004       break;
11005
11006     default:
11007       cp_parser_error (parser, "expected selection-statement");
11008       return error_mark_node;
11009     }
11010 }
11011
11012 /* Parse a condition.
11013
11014    condition:
11015      expression
11016      type-specifier-seq declarator = initializer-clause
11017      type-specifier-seq declarator braced-init-list
11018
11019    GNU Extension:
11020
11021    condition:
11022      type-specifier-seq declarator asm-specification [opt]
11023        attributes [opt] = assignment-expression
11024
11025    Returns the expression that should be tested.  */
11026
11027 static tree
11028 cp_parser_condition (cp_parser* parser)
11029 {
11030   cp_decl_specifier_seq type_specifiers;
11031   const char *saved_message;
11032   int declares_class_or_enum;
11033
11034   /* Try the declaration first.  */
11035   cp_parser_parse_tentatively (parser);
11036   /* New types are not allowed in the type-specifier-seq for a
11037      condition.  */
11038   saved_message = parser->type_definition_forbidden_message;
11039   parser->type_definition_forbidden_message
11040     = G_("types may not be defined in conditions");
11041   /* Parse the type-specifier-seq.  */
11042   cp_parser_decl_specifier_seq (parser,
11043                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11044                                 &type_specifiers,
11045                                 &declares_class_or_enum);
11046   /* Restore the saved message.  */
11047   parser->type_definition_forbidden_message = saved_message;
11048   /* If all is well, we might be looking at a declaration.  */
11049   if (!cp_parser_error_occurred (parser))
11050     {
11051       tree decl;
11052       tree asm_specification;
11053       tree attributes;
11054       cp_declarator *declarator;
11055       tree initializer = NULL_TREE;
11056
11057       /* Parse the declarator.  */
11058       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11059                                          /*ctor_dtor_or_conv_p=*/NULL,
11060                                          /*parenthesized_p=*/NULL,
11061                                          /*member_p=*/false,
11062                                          /*friend_p=*/false);
11063       /* Parse the attributes.  */
11064       attributes = cp_parser_attributes_opt (parser);
11065       /* Parse the asm-specification.  */
11066       asm_specification = cp_parser_asm_specification_opt (parser);
11067       /* If the next token is not an `=' or '{', then we might still be
11068          looking at an expression.  For example:
11069
11070            if (A(a).x)
11071
11072          looks like a decl-specifier-seq and a declarator -- but then
11073          there is no `=', so this is an expression.  */
11074       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11075           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11076         cp_parser_simulate_error (parser);
11077         
11078       /* If we did see an `=' or '{', then we are looking at a declaration
11079          for sure.  */
11080       if (cp_parser_parse_definitely (parser))
11081         {
11082           tree pushed_scope;
11083           bool non_constant_p;
11084           bool flags = LOOKUP_ONLYCONVERTING;
11085
11086           /* Create the declaration.  */
11087           decl = start_decl (declarator, &type_specifiers,
11088                              /*initialized_p=*/true,
11089                              attributes, /*prefix_attributes=*/NULL_TREE,
11090                              &pushed_scope);
11091
11092           /* Parse the initializer.  */
11093           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11094             {
11095               initializer = cp_parser_braced_list (parser, &non_constant_p);
11096               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11097               flags = 0;
11098             }
11099           else
11100             {
11101               /* Consume the `='.  */
11102               cp_parser_require (parser, CPP_EQ, RT_EQ);
11103               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11104             }
11105           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11106             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11107
11108           /* Process the initializer.  */
11109           cp_finish_decl (decl,
11110                           initializer, !non_constant_p,
11111                           asm_specification,
11112                           flags);
11113
11114           if (pushed_scope)
11115             pop_scope (pushed_scope);
11116
11117           return convert_from_reference (decl);
11118         }
11119     }
11120   /* If we didn't even get past the declarator successfully, we are
11121      definitely not looking at a declaration.  */
11122   else
11123     cp_parser_abort_tentative_parse (parser);
11124
11125   /* Otherwise, we are looking at an expression.  */
11126   return cp_parser_expression (parser);
11127 }
11128
11129 /* Parses a for-statement or range-for-statement until the closing ')',
11130    not included. */
11131
11132 static tree
11133 cp_parser_for (cp_parser *parser, bool ivdep)
11134 {
11135   tree init, scope, decl;
11136   bool is_range_for;
11137
11138   /* Begin the for-statement.  */
11139   scope = begin_for_scope (&init);
11140
11141   /* Parse the initialization.  */
11142   is_range_for = cp_parser_for_init_statement (parser, &decl);
11143
11144   if (is_range_for)
11145     return cp_parser_range_for (parser, scope, init, decl, ivdep);
11146   else
11147     return cp_parser_c_for (parser, scope, init, ivdep);
11148 }
11149
11150 static tree
11151 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11152 {
11153   /* Normal for loop */
11154   tree condition = NULL_TREE;
11155   tree expression = NULL_TREE;
11156   tree stmt;
11157
11158   stmt = begin_for_stmt (scope, init);
11159   /* The for-init-statement has already been parsed in
11160      cp_parser_for_init_statement, so no work is needed here.  */
11161   finish_for_init_stmt (stmt);
11162
11163   /* If there's a condition, process it.  */
11164   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11165     condition = cp_parser_condition (parser);
11166   else if (ivdep)
11167     {
11168       cp_parser_error (parser, "missing loop condition in loop with "
11169                        "%<GCC ivdep%> pragma");
11170       condition = error_mark_node;
11171     }
11172   finish_for_cond (condition, stmt, ivdep);
11173   /* Look for the `;'.  */
11174   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11175
11176   /* If there's an expression, process it.  */
11177   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11178     expression = cp_parser_expression (parser);
11179   finish_for_expr (expression, stmt);
11180
11181   return stmt;
11182 }
11183
11184 /* Tries to parse a range-based for-statement:
11185
11186   range-based-for:
11187     decl-specifier-seq declarator : expression
11188
11189   The decl-specifier-seq declarator and the `:' are already parsed by
11190   cp_parser_for_init_statement. If processing_template_decl it returns a
11191   newly created RANGE_FOR_STMT; if not, it is converted to a
11192   regular FOR_STMT.  */
11193
11194 static tree
11195 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11196                      bool ivdep)
11197 {
11198   tree stmt, range_expr;
11199   cxx_binding *binding = NULL;
11200   tree name = NULL_TREE;
11201
11202   /* Get the range declaration momentarily out of the way so that
11203      the range expression doesn't clash with it. */
11204   if (range_decl != error_mark_node)
11205     {
11206       name = DECL_NAME (range_decl);
11207       binding = IDENTIFIER_BINDING (name);
11208       IDENTIFIER_BINDING (name) = binding->previous;
11209     }
11210
11211   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11212     {
11213       bool expr_non_constant_p;
11214       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11215     }
11216   else
11217     range_expr = cp_parser_expression (parser);
11218
11219   /* Put the range declaration back into scope. */
11220   if (range_decl != error_mark_node)
11221     {
11222       binding->previous = IDENTIFIER_BINDING (name);
11223       IDENTIFIER_BINDING (name) = binding;
11224     }
11225
11226   /* If in template, STMT is converted to a normal for-statement
11227      at instantiation. If not, it is done just ahead. */
11228   if (processing_template_decl)
11229     {
11230       if (check_for_bare_parameter_packs (range_expr))
11231         range_expr = error_mark_node;
11232       stmt = begin_range_for_stmt (scope, init);
11233       if (ivdep)
11234         RANGE_FOR_IVDEP (stmt) = 1;
11235       finish_range_for_decl (stmt, range_decl, range_expr);
11236       if (!type_dependent_expression_p (range_expr)
11237           /* do_auto_deduction doesn't mess with template init-lists.  */
11238           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11239         do_range_for_auto_deduction (range_decl, range_expr);
11240     }
11241   else
11242     {
11243       stmt = begin_for_stmt (scope, init);
11244       stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11245     }
11246   return stmt;
11247 }
11248
11249 /* Subroutine of cp_convert_range_for: given the initializer expression,
11250    builds up the range temporary.  */
11251
11252 static tree
11253 build_range_temp (tree range_expr)
11254 {
11255   tree range_type, range_temp;
11256
11257   /* Find out the type deduced by the declaration
11258      `auto &&__range = range_expr'.  */
11259   range_type = cp_build_reference_type (make_auto (), true);
11260   range_type = do_auto_deduction (range_type, range_expr,
11261                                   type_uses_auto (range_type));
11262
11263   /* Create the __range variable.  */
11264   range_temp = build_decl (input_location, VAR_DECL,
11265                            get_identifier ("__for_range"), range_type);
11266   TREE_USED (range_temp) = 1;
11267   DECL_ARTIFICIAL (range_temp) = 1;
11268
11269   return range_temp;
11270 }
11271
11272 /* Used by cp_parser_range_for in template context: we aren't going to
11273    do a full conversion yet, but we still need to resolve auto in the
11274    type of the for-range-declaration if present.  This is basically
11275    a shortcut version of cp_convert_range_for.  */
11276
11277 static void
11278 do_range_for_auto_deduction (tree decl, tree range_expr)
11279 {
11280   tree auto_node = type_uses_auto (TREE_TYPE (decl));
11281   if (auto_node)
11282     {
11283       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11284       range_temp = convert_from_reference (build_range_temp (range_expr));
11285       iter_type = (cp_parser_perform_range_for_lookup
11286                    (range_temp, &begin_dummy, &end_dummy));
11287       if (iter_type)
11288         {
11289           iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11290                                   iter_type);
11291           iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11292                                             tf_warning_or_error);
11293           TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11294                                                 iter_decl, auto_node);
11295         }
11296     }
11297 }
11298
11299 /* Converts a range-based for-statement into a normal
11300    for-statement, as per the definition.
11301
11302       for (RANGE_DECL : RANGE_EXPR)
11303         BLOCK
11304
11305    should be equivalent to:
11306
11307       {
11308         auto &&__range = RANGE_EXPR;
11309         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11310               __begin != __end;
11311               ++__begin)
11312           {
11313               RANGE_DECL = *__begin;
11314               BLOCK
11315           }
11316       }
11317
11318    If RANGE_EXPR is an array:
11319         BEGIN_EXPR = __range
11320         END_EXPR = __range + ARRAY_SIZE(__range)
11321    Else if RANGE_EXPR has a member 'begin' or 'end':
11322         BEGIN_EXPR = __range.begin()
11323         END_EXPR = __range.end()
11324    Else:
11325         BEGIN_EXPR = begin(__range)
11326         END_EXPR = end(__range);
11327
11328    If __range has a member 'begin' but not 'end', or vice versa, we must
11329    still use the second alternative (it will surely fail, however).
11330    When calling begin()/end() in the third alternative we must use
11331    argument dependent lookup, but always considering 'std' as an associated
11332    namespace.  */
11333
11334 tree
11335 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11336                       bool ivdep)
11337 {
11338   tree begin, end;
11339   tree iter_type, begin_expr, end_expr;
11340   tree condition, expression;
11341
11342   if (range_decl == error_mark_node || range_expr == error_mark_node)
11343     /* If an error happened previously do nothing or else a lot of
11344        unhelpful errors would be issued.  */
11345     begin_expr = end_expr = iter_type = error_mark_node;
11346   else
11347     {
11348       tree range_temp;
11349
11350       if (VAR_P (range_expr)
11351           && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11352         /* Can't bind a reference to an array of runtime bound.  */
11353         range_temp = range_expr;
11354       else
11355         {
11356           range_temp = build_range_temp (range_expr);
11357           pushdecl (range_temp);
11358           cp_finish_decl (range_temp, range_expr,
11359                           /*is_constant_init*/false, NULL_TREE,
11360                           LOOKUP_ONLYCONVERTING);
11361           range_temp = convert_from_reference (range_temp);
11362         }
11363       iter_type = cp_parser_perform_range_for_lookup (range_temp,
11364                                                       &begin_expr, &end_expr);
11365     }
11366
11367   /* The new for initialization statement.  */
11368   begin = build_decl (input_location, VAR_DECL,
11369                       get_identifier ("__for_begin"), iter_type);
11370   TREE_USED (begin) = 1;
11371   DECL_ARTIFICIAL (begin) = 1;
11372   pushdecl (begin);
11373   cp_finish_decl (begin, begin_expr,
11374                   /*is_constant_init*/false, NULL_TREE,
11375                   LOOKUP_ONLYCONVERTING);
11376
11377   if (cxx_dialect >= cxx1z)
11378     iter_type = cv_unqualified (TREE_TYPE (end_expr));
11379   end = build_decl (input_location, VAR_DECL,
11380                     get_identifier ("__for_end"), iter_type);
11381   TREE_USED (end) = 1;
11382   DECL_ARTIFICIAL (end) = 1;
11383   pushdecl (end);
11384   cp_finish_decl (end, end_expr,
11385                   /*is_constant_init*/false, NULL_TREE,
11386                   LOOKUP_ONLYCONVERTING);
11387
11388   finish_for_init_stmt (statement);
11389
11390   /* The new for condition.  */
11391   condition = build_x_binary_op (input_location, NE_EXPR,
11392                                  begin, ERROR_MARK,
11393                                  end, ERROR_MARK,
11394                                  NULL, tf_warning_or_error);
11395   finish_for_cond (condition, statement, ivdep);
11396
11397   /* The new increment expression.  */
11398   expression = finish_unary_op_expr (input_location,
11399                                      PREINCREMENT_EXPR, begin,
11400                                      tf_warning_or_error);
11401   finish_for_expr (expression, statement);
11402
11403   /* The declaration is initialized with *__begin inside the loop body.  */
11404   cp_finish_decl (range_decl,
11405                   build_x_indirect_ref (input_location, begin, RO_NULL,
11406                                         tf_warning_or_error),
11407                   /*is_constant_init*/false, NULL_TREE,
11408                   LOOKUP_ONLYCONVERTING);
11409
11410   return statement;
11411 }
11412
11413 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11414    We need to solve both at the same time because the method used
11415    depends on the existence of members begin or end.
11416    Returns the type deduced for the iterator expression.  */
11417
11418 static tree
11419 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11420 {
11421   if (error_operand_p (range))
11422     {
11423       *begin = *end = error_mark_node;
11424       return error_mark_node;
11425     }
11426
11427   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11428     {
11429       error ("range-based %<for%> expression of type %qT "
11430              "has incomplete type", TREE_TYPE (range));
11431       *begin = *end = error_mark_node;
11432       return error_mark_node;
11433     }
11434   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11435     {
11436       /* If RANGE is an array, we will use pointer arithmetic.  */
11437       *begin = range;
11438       *end = build_binary_op (input_location, PLUS_EXPR,
11439                               range,
11440                               array_type_nelts_top (TREE_TYPE (range)),
11441                               0);
11442       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11443     }
11444   else
11445     {
11446       /* If it is not an array, we must do a bit of magic.  */
11447       tree id_begin, id_end;
11448       tree member_begin, member_end;
11449
11450       *begin = *end = error_mark_node;
11451
11452       id_begin = get_identifier ("begin");
11453       id_end = get_identifier ("end");
11454       member_begin = lookup_member (TREE_TYPE (range), id_begin,
11455                                     /*protect=*/2, /*want_type=*/false,
11456                                     tf_warning_or_error);
11457       member_end = lookup_member (TREE_TYPE (range), id_end,
11458                                   /*protect=*/2, /*want_type=*/false,
11459                                   tf_warning_or_error);
11460
11461       if (member_begin != NULL_TREE || member_end != NULL_TREE)
11462         {
11463           /* Use the member functions.  */
11464           if (member_begin != NULL_TREE)
11465             *begin = cp_parser_range_for_member_function (range, id_begin);
11466           else
11467             error ("range-based %<for%> expression of type %qT has an "
11468                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11469
11470           if (member_end != NULL_TREE)
11471             *end = cp_parser_range_for_member_function (range, id_end);
11472           else
11473             error ("range-based %<for%> expression of type %qT has a "
11474                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11475         }
11476       else
11477         {
11478           /* Use global functions with ADL.  */
11479           vec<tree, va_gc> *vec;
11480           vec = make_tree_vector ();
11481
11482           vec_safe_push (vec, range);
11483
11484           member_begin = perform_koenig_lookup (id_begin, vec,
11485                                                 tf_warning_or_error);
11486           *begin = finish_call_expr (member_begin, &vec, false, true,
11487                                      tf_warning_or_error);
11488           member_end = perform_koenig_lookup (id_end, vec,
11489                                               tf_warning_or_error);
11490           *end = finish_call_expr (member_end, &vec, false, true,
11491                                    tf_warning_or_error);
11492
11493           release_tree_vector (vec);
11494         }
11495
11496       /* Last common checks.  */
11497       if (*begin == error_mark_node || *end == error_mark_node)
11498         {
11499           /* If one of the expressions is an error do no more checks.  */
11500           *begin = *end = error_mark_node;
11501           return error_mark_node;
11502         }
11503       else if (type_dependent_expression_p (*begin)
11504                || type_dependent_expression_p (*end))
11505         /* Can happen, when, eg, in a template context, Koenig lookup
11506            can't resolve begin/end (c++/58503).  */
11507         return NULL_TREE;
11508       else
11509         {
11510           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11511           /* The unqualified type of the __begin and __end temporaries should
11512              be the same, as required by the multiple auto declaration.  */
11513           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11514             {
11515               if (cxx_dialect >= cxx1z
11516                   && (build_x_binary_op (input_location, NE_EXPR,
11517                                          *begin, ERROR_MARK,
11518                                          *end, ERROR_MARK,
11519                                          NULL, tf_none)
11520                       != error_mark_node))
11521                 /* P0184R0 allows __begin and __end to have different types,
11522                    but make sure they are comparable so we can give a better
11523                    diagnostic.  */;
11524               else
11525                 error ("inconsistent begin/end types in range-based %<for%> "
11526                        "statement: %qT and %qT",
11527                        TREE_TYPE (*begin), TREE_TYPE (*end));
11528             }
11529           return iter_type;
11530         }
11531     }
11532 }
11533
11534 /* Helper function for cp_parser_perform_range_for_lookup.
11535    Builds a tree for RANGE.IDENTIFIER().  */
11536
11537 static tree
11538 cp_parser_range_for_member_function (tree range, tree identifier)
11539 {
11540   tree member, res;
11541   vec<tree, va_gc> *vec;
11542
11543   member = finish_class_member_access_expr (range, identifier,
11544                                             false, tf_warning_or_error);
11545   if (member == error_mark_node)
11546     return error_mark_node;
11547
11548   vec = make_tree_vector ();
11549   res = finish_call_expr (member, &vec,
11550                           /*disallow_virtual=*/false,
11551                           /*koenig_p=*/false,
11552                           tf_warning_or_error);
11553   release_tree_vector (vec);
11554   return res;
11555 }
11556
11557 /* Parse an iteration-statement.
11558
11559    iteration-statement:
11560      while ( condition ) statement
11561      do statement while ( expression ) ;
11562      for ( for-init-statement condition [opt] ; expression [opt] )
11563        statement
11564
11565    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
11566
11567 static tree
11568 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11569 {
11570   cp_token *token;
11571   enum rid keyword;
11572   tree statement;
11573   unsigned char in_statement;
11574   token_indent_info guard_tinfo;
11575
11576   /* Peek at the next token.  */
11577   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11578   if (!token)
11579     return error_mark_node;
11580
11581   guard_tinfo = get_token_indent_info (token);
11582
11583   /* Remember whether or not we are already within an iteration
11584      statement.  */
11585   in_statement = parser->in_statement;
11586
11587   /* See what kind of keyword it is.  */
11588   keyword = token->keyword;
11589   switch (keyword)
11590     {
11591     case RID_WHILE:
11592       {
11593         tree condition;
11594
11595         /* Begin the while-statement.  */
11596         statement = begin_while_stmt ();
11597         /* Look for the `('.  */
11598         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11599         /* Parse the condition.  */
11600         condition = cp_parser_condition (parser);
11601         finish_while_stmt_cond (condition, statement, ivdep);
11602         /* Look for the `)'.  */
11603         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11604         /* Parse the dependent statement.  */
11605         parser->in_statement = IN_ITERATION_STMT;
11606         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11607         parser->in_statement = in_statement;
11608         /* We're done with the while-statement.  */
11609         finish_while_stmt (statement);
11610       }
11611       break;
11612
11613     case RID_DO:
11614       {
11615         tree expression;
11616
11617         /* Begin the do-statement.  */
11618         statement = begin_do_stmt ();
11619         /* Parse the body of the do-statement.  */
11620         parser->in_statement = IN_ITERATION_STMT;
11621         cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11622         parser->in_statement = in_statement;
11623         finish_do_body (statement);
11624         /* Look for the `while' keyword.  */
11625         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11626         /* Look for the `('.  */
11627         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11628         /* Parse the expression.  */
11629         expression = cp_parser_expression (parser);
11630         /* We're done with the do-statement.  */
11631         finish_do_stmt (expression, statement, ivdep);
11632         /* Look for the `)'.  */
11633         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11634         /* Look for the `;'.  */
11635         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11636       }
11637       break;
11638
11639     case RID_FOR:
11640       {
11641         /* Look for the `('.  */
11642         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11643
11644         statement = cp_parser_for (parser, ivdep);
11645
11646         /* Look for the `)'.  */
11647         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11648
11649         /* Parse the body of the for-statement.  */
11650         parser->in_statement = IN_ITERATION_STMT;
11651         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11652         parser->in_statement = in_statement;
11653
11654         /* We're done with the for-statement.  */
11655         finish_for_stmt (statement);
11656       }
11657       break;
11658
11659     default:
11660       cp_parser_error (parser, "expected iteration-statement");
11661       statement = error_mark_node;
11662       break;
11663     }
11664
11665   return statement;
11666 }
11667
11668 /* Parse a for-init-statement or the declarator of a range-based-for.
11669    Returns true if a range-based-for declaration is seen.
11670
11671    for-init-statement:
11672      expression-statement
11673      simple-declaration  */
11674
11675 static bool
11676 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11677 {
11678   /* If the next token is a `;', then we have an empty
11679      expression-statement.  Grammatically, this is also a
11680      simple-declaration, but an invalid one, because it does not
11681      declare anything.  Therefore, if we did not handle this case
11682      specially, we would issue an error message about an invalid
11683      declaration.  */
11684   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11685     {
11686       bool is_range_for = false;
11687       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11688
11689       /* A colon is used in range-based for.  */
11690       parser->colon_corrects_to_scope_p = false;
11691
11692       /* We're going to speculatively look for a declaration, falling back
11693          to an expression, if necessary.  */
11694       cp_parser_parse_tentatively (parser);
11695       /* Parse the declaration.  */
11696       cp_parser_simple_declaration (parser,
11697                                     /*function_definition_allowed_p=*/false,
11698                                     decl);
11699       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11700       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11701         {
11702           /* It is a range-for, consume the ':' */
11703           cp_lexer_consume_token (parser->lexer);
11704           is_range_for = true;
11705           if (cxx_dialect < cxx11)
11706             {
11707               pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11708                        "range-based %<for%> loops only available with "
11709                        "-std=c++11 or -std=gnu++11");
11710               *decl = error_mark_node;
11711             }
11712         }
11713       else
11714           /* The ';' is not consumed yet because we told
11715              cp_parser_simple_declaration not to.  */
11716           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11717
11718       if (cp_parser_parse_definitely (parser))
11719         return is_range_for;
11720       /* If the tentative parse failed, then we shall need to look for an
11721          expression-statement.  */
11722     }
11723   /* If we are here, it is an expression-statement.  */
11724   cp_parser_expression_statement (parser, NULL_TREE);
11725   return false;
11726 }
11727
11728 /* Parse a jump-statement.
11729
11730    jump-statement:
11731      break ;
11732      continue ;
11733      return expression [opt] ;
11734      return braced-init-list ;
11735      goto identifier ;
11736
11737    GNU extension:
11738
11739    jump-statement:
11740      goto * expression ;
11741
11742    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
11743
11744 static tree
11745 cp_parser_jump_statement (cp_parser* parser)
11746 {
11747   tree statement = error_mark_node;
11748   cp_token *token;
11749   enum rid keyword;
11750   unsigned char in_statement;
11751
11752   /* Peek at the next token.  */
11753   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11754   if (!token)
11755     return error_mark_node;
11756
11757   /* See what kind of keyword it is.  */
11758   keyword = token->keyword;
11759   switch (keyword)
11760     {
11761     case RID_BREAK:
11762       in_statement = parser->in_statement & ~IN_IF_STMT;      
11763       switch (in_statement)
11764         {
11765         case 0:
11766           error_at (token->location, "break statement not within loop or switch");
11767           break;
11768         default:
11769           gcc_assert ((in_statement & IN_SWITCH_STMT)
11770                       || in_statement == IN_ITERATION_STMT);
11771           statement = finish_break_stmt ();
11772           if (in_statement == IN_ITERATION_STMT)
11773             break_maybe_infinite_loop ();
11774           break;
11775         case IN_OMP_BLOCK:
11776           error_at (token->location, "invalid exit from OpenMP structured block");
11777           break;
11778         case IN_OMP_FOR:
11779           error_at (token->location, "break statement used with OpenMP for loop");
11780           break;
11781         case IN_CILK_SIMD_FOR:
11782           error_at (token->location, "break statement used with Cilk Plus for loop");
11783           break;
11784         }
11785       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11786       break;
11787
11788     case RID_CONTINUE:
11789       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11790         {
11791         case 0:
11792           error_at (token->location, "continue statement not within a loop");
11793           break;
11794         case IN_CILK_SIMD_FOR:
11795           error_at (token->location,
11796                     "continue statement within %<#pragma simd%> loop body");
11797           /* Fall through.  */
11798         case IN_ITERATION_STMT:
11799         case IN_OMP_FOR:
11800           statement = finish_continue_stmt ();
11801           break;
11802         case IN_OMP_BLOCK:
11803           error_at (token->location, "invalid exit from OpenMP structured block");
11804           break;
11805         default:
11806           gcc_unreachable ();
11807         }
11808       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11809       break;
11810
11811     case RID_RETURN:
11812       {
11813         tree expr;
11814         bool expr_non_constant_p;
11815
11816         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11817           {
11818             cp_lexer_set_source_position (parser->lexer);
11819             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11820             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11821           }
11822         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11823           expr = cp_parser_expression (parser);
11824         else
11825           /* If the next token is a `;', then there is no
11826              expression.  */
11827           expr = NULL_TREE;
11828         /* Build the return-statement.  */
11829         statement = finish_return_stmt (expr);
11830         /* Look for the final `;'.  */
11831         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11832       }
11833       break;
11834
11835     case RID_GOTO:
11836       if (parser->in_function_body
11837           && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11838         {
11839           error ("%<goto%> in %<constexpr%> function");
11840           cp_function_chain->invalid_constexpr = true;
11841         }
11842
11843       /* Create the goto-statement.  */
11844       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11845         {
11846           /* Issue a warning about this use of a GNU extension.  */
11847           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11848           /* Consume the '*' token.  */
11849           cp_lexer_consume_token (parser->lexer);
11850           /* Parse the dependent expression.  */
11851           finish_goto_stmt (cp_parser_expression (parser));
11852         }
11853       else
11854         finish_goto_stmt (cp_parser_identifier (parser));
11855       /* Look for the final `;'.  */
11856       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11857       break;
11858
11859     default:
11860       cp_parser_error (parser, "expected jump-statement");
11861       break;
11862     }
11863
11864   return statement;
11865 }
11866
11867 /* Parse a declaration-statement.
11868
11869    declaration-statement:
11870      block-declaration  */
11871
11872 static void
11873 cp_parser_declaration_statement (cp_parser* parser)
11874 {
11875   void *p;
11876
11877   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11878   p = obstack_alloc (&declarator_obstack, 0);
11879
11880  /* Parse the block-declaration.  */
11881   cp_parser_block_declaration (parser, /*statement_p=*/true);
11882
11883   /* Free any declarators allocated.  */
11884   obstack_free (&declarator_obstack, p);
11885 }
11886
11887 /* Some dependent statements (like `if (cond) statement'), are
11888    implicitly in their own scope.  In other words, if the statement is
11889    a single statement (as opposed to a compound-statement), it is
11890    none-the-less treated as if it were enclosed in braces.  Any
11891    declarations appearing in the dependent statement are out of scope
11892    after control passes that point.  This function parses a statement,
11893    but ensures that is in its own scope, even if it is not a
11894    compound-statement.
11895
11896    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11897    is a (possibly labeled) if statement which is not enclosed in
11898    braces and has an else clause.  This is used to implement
11899    -Wparentheses.
11900
11901    CHAIN is a vector of if-else-if conditions.  This is used to implement
11902    -Wduplicated-cond.
11903
11904    Returns the new statement.  */
11905
11906 static tree
11907 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11908                                        const token_indent_info &guard_tinfo,
11909                                        vec<tree> *chain)
11910 {
11911   tree statement;
11912   location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11913   token_indent_info body_tinfo
11914     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11915
11916   if (if_p != NULL)
11917     *if_p = false;
11918
11919   /* Mark if () ; with a special NOP_EXPR.  */
11920   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11921     {
11922       cp_lexer_consume_token (parser->lexer);
11923       statement = add_stmt (build_empty_stmt (body_loc));
11924
11925       if (guard_tinfo.keyword == RID_IF
11926           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11927         warning_at (body_loc, OPT_Wempty_body,
11928                     "suggest braces around empty body in an %<if%> statement");
11929       else if (guard_tinfo.keyword == RID_ELSE)
11930         warning_at (body_loc, OPT_Wempty_body,
11931                     "suggest braces around empty body in an %<else%> statement");
11932     }
11933   /* if a compound is opened, we simply parse the statement directly.  */
11934   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11935     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11936   /* If the token is not a `{', then we must take special action.  */
11937   else
11938     {
11939       /* Create a compound-statement.  */
11940       statement = begin_compound_stmt (0);
11941       /* Parse the dependent-statement.  */
11942       cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11943       /* Finish the dummy compound-statement.  */
11944       finish_compound_stmt (statement);
11945     }
11946
11947   token_indent_info next_tinfo
11948     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11949   warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11950
11951   /* Return the statement.  */
11952   return statement;
11953 }
11954
11955 /* For some dependent statements (like `while (cond) statement'), we
11956    have already created a scope.  Therefore, even if the dependent
11957    statement is a compound-statement, we do not want to create another
11958    scope.  */
11959
11960 static void
11961 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11962                                     const token_indent_info &guard_tinfo)
11963 {
11964   /* If the token is a `{', then we must take special action.  */
11965   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11966     {
11967       token_indent_info body_tinfo
11968         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11969
11970       cp_parser_statement (parser, NULL_TREE, false, if_p);
11971       token_indent_info next_tinfo
11972         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11973       warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11974     }
11975   else
11976     {
11977       /* Avoid calling cp_parser_compound_statement, so that we
11978          don't create a new scope.  Do everything else by hand.  */
11979       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11980       /* If the next keyword is `__label__' we have a label declaration.  */
11981       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11982         cp_parser_label_declaration (parser);
11983       /* Parse an (optional) statement-seq.  */
11984       cp_parser_statement_seq_opt (parser, NULL_TREE);
11985       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11986     }
11987 }
11988
11989 /* Declarations [gram.dcl.dcl] */
11990
11991 /* Parse an optional declaration-sequence.
11992
11993    declaration-seq:
11994      declaration
11995      declaration-seq declaration  */
11996
11997 static void
11998 cp_parser_declaration_seq_opt (cp_parser* parser)
11999 {
12000   while (true)
12001     {
12002       cp_token *token;
12003
12004       token = cp_lexer_peek_token (parser->lexer);
12005
12006       if (token->type == CPP_CLOSE_BRACE
12007           || token->type == CPP_EOF
12008           || token->type == CPP_PRAGMA_EOL)
12009         break;
12010
12011       if (token->type == CPP_SEMICOLON)
12012         {
12013           /* A declaration consisting of a single semicolon is
12014              invalid.  Allow it unless we're being pedantic.  */
12015           cp_lexer_consume_token (parser->lexer);
12016           if (!in_system_header_at (input_location))
12017             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12018           continue;
12019         }
12020
12021       /* If we're entering or exiting a region that's implicitly
12022          extern "C", modify the lang context appropriately.  */
12023       if (!parser->implicit_extern_c && token->implicit_extern_c)
12024         {
12025           push_lang_context (lang_name_c);
12026           parser->implicit_extern_c = true;
12027         }
12028       else if (parser->implicit_extern_c && !token->implicit_extern_c)
12029         {
12030           pop_lang_context ();
12031           parser->implicit_extern_c = false;
12032         }
12033
12034       if (token->type == CPP_PRAGMA)
12035         {
12036           /* A top-level declaration can consist solely of a #pragma.
12037              A nested declaration cannot, so this is done here and not
12038              in cp_parser_declaration.  (A #pragma at block scope is
12039              handled in cp_parser_statement.)  */
12040           cp_parser_pragma (parser, pragma_external, NULL);
12041           continue;
12042         }
12043
12044       /* Parse the declaration itself.  */
12045       cp_parser_declaration (parser);
12046     }
12047 }
12048
12049 /* Parse a declaration.
12050
12051    declaration:
12052      block-declaration
12053      function-definition
12054      template-declaration
12055      explicit-instantiation
12056      explicit-specialization
12057      linkage-specification
12058      namespace-definition
12059
12060    GNU extension:
12061
12062    declaration:
12063       __extension__ declaration */
12064
12065 static void
12066 cp_parser_declaration (cp_parser* parser)
12067 {
12068   cp_token token1;
12069   cp_token token2;
12070   int saved_pedantic;
12071   void *p;
12072   tree attributes = NULL_TREE;
12073
12074   /* Check for the `__extension__' keyword.  */
12075   if (cp_parser_extension_opt (parser, &saved_pedantic))
12076     {
12077       /* Parse the qualified declaration.  */
12078       cp_parser_declaration (parser);
12079       /* Restore the PEDANTIC flag.  */
12080       pedantic = saved_pedantic;
12081
12082       return;
12083     }
12084
12085   /* Try to figure out what kind of declaration is present.  */
12086   token1 = *cp_lexer_peek_token (parser->lexer);
12087
12088   if (token1.type != CPP_EOF)
12089     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12090   else
12091     {
12092       token2.type = CPP_EOF;
12093       token2.keyword = RID_MAX;
12094     }
12095
12096   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12097   p = obstack_alloc (&declarator_obstack, 0);
12098
12099   /* If the next token is `extern' and the following token is a string
12100      literal, then we have a linkage specification.  */
12101   if (token1.keyword == RID_EXTERN
12102       && cp_parser_is_pure_string_literal (&token2))
12103     cp_parser_linkage_specification (parser);
12104   /* If the next token is `template', then we have either a template
12105      declaration, an explicit instantiation, or an explicit
12106      specialization.  */
12107   else if (token1.keyword == RID_TEMPLATE)
12108     {
12109       /* `template <>' indicates a template specialization.  */
12110       if (token2.type == CPP_LESS
12111           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12112         cp_parser_explicit_specialization (parser);
12113       /* `template <' indicates a template declaration.  */
12114       else if (token2.type == CPP_LESS)
12115         cp_parser_template_declaration (parser, /*member_p=*/false);
12116       /* Anything else must be an explicit instantiation.  */
12117       else
12118         cp_parser_explicit_instantiation (parser);
12119     }
12120   /* If the next token is `export', then we have a template
12121      declaration.  */
12122   else if (token1.keyword == RID_EXPORT)
12123     cp_parser_template_declaration (parser, /*member_p=*/false);
12124   /* If the next token is `extern', 'static' or 'inline' and the one
12125      after that is `template', we have a GNU extended explicit
12126      instantiation directive.  */
12127   else if (cp_parser_allow_gnu_extensions_p (parser)
12128            && (token1.keyword == RID_EXTERN
12129                || token1.keyword == RID_STATIC
12130                || token1.keyword == RID_INLINE)
12131            && token2.keyword == RID_TEMPLATE)
12132     cp_parser_explicit_instantiation (parser);
12133   /* If the next token is `namespace', check for a named or unnamed
12134      namespace definition.  */
12135   else if (token1.keyword == RID_NAMESPACE
12136            && (/* A named namespace definition.  */
12137                (token2.type == CPP_NAME
12138                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12139                     != CPP_EQ))
12140                || (token2.type == CPP_OPEN_SQUARE
12141                    && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12142                    == CPP_OPEN_SQUARE)
12143                /* An unnamed namespace definition.  */
12144                || token2.type == CPP_OPEN_BRACE
12145                || token2.keyword == RID_ATTRIBUTE))
12146     cp_parser_namespace_definition (parser);
12147   /* An inline (associated) namespace definition.  */
12148   else if (token1.keyword == RID_INLINE
12149            && token2.keyword == RID_NAMESPACE)
12150     cp_parser_namespace_definition (parser);
12151   /* Objective-C++ declaration/definition.  */
12152   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12153     cp_parser_objc_declaration (parser, NULL_TREE);
12154   else if (c_dialect_objc ()
12155            && token1.keyword == RID_ATTRIBUTE
12156            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12157     cp_parser_objc_declaration (parser, attributes);
12158   /* At this point we may have a template declared by a concept
12159      introduction.  */
12160   else if (flag_concepts
12161            && cp_parser_template_declaration_after_export (parser,
12162                                                            /*member_p=*/false))
12163     /* We did.  */;
12164   else
12165     /* Try to parse a block-declaration, or a function-definition.  */
12166     cp_parser_block_declaration (parser, /*statement_p=*/false);
12167
12168   /* Free any declarators allocated.  */
12169   obstack_free (&declarator_obstack, p);
12170 }
12171
12172 /* Parse a block-declaration.
12173
12174    block-declaration:
12175      simple-declaration
12176      asm-definition
12177      namespace-alias-definition
12178      using-declaration
12179      using-directive
12180
12181    GNU Extension:
12182
12183    block-declaration:
12184      __extension__ block-declaration
12185
12186    C++0x Extension:
12187
12188    block-declaration:
12189      static_assert-declaration
12190
12191    If STATEMENT_P is TRUE, then this block-declaration is occurring as
12192    part of a declaration-statement.  */
12193
12194 static void
12195 cp_parser_block_declaration (cp_parser *parser,
12196                              bool      statement_p)
12197 {
12198   cp_token *token1;
12199   int saved_pedantic;
12200
12201   /* Check for the `__extension__' keyword.  */
12202   if (cp_parser_extension_opt (parser, &saved_pedantic))
12203     {
12204       /* Parse the qualified declaration.  */
12205       cp_parser_block_declaration (parser, statement_p);
12206       /* Restore the PEDANTIC flag.  */
12207       pedantic = saved_pedantic;
12208
12209       return;
12210     }
12211
12212   /* Peek at the next token to figure out which kind of declaration is
12213      present.  */
12214   token1 = cp_lexer_peek_token (parser->lexer);
12215
12216   /* If the next keyword is `asm', we have an asm-definition.  */
12217   if (token1->keyword == RID_ASM)
12218     {
12219       if (statement_p)
12220         cp_parser_commit_to_tentative_parse (parser);
12221       cp_parser_asm_definition (parser);
12222     }
12223   /* If the next keyword is `namespace', we have a
12224      namespace-alias-definition.  */
12225   else if (token1->keyword == RID_NAMESPACE)
12226     cp_parser_namespace_alias_definition (parser);
12227   /* If the next keyword is `using', we have a
12228      using-declaration, a using-directive, or an alias-declaration.  */
12229   else if (token1->keyword == RID_USING)
12230     {
12231       cp_token *token2;
12232
12233       if (statement_p)
12234         cp_parser_commit_to_tentative_parse (parser);
12235       /* If the token after `using' is `namespace', then we have a
12236          using-directive.  */
12237       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12238       if (token2->keyword == RID_NAMESPACE)
12239         cp_parser_using_directive (parser);
12240       /* If the second token after 'using' is '=', then we have an
12241          alias-declaration.  */
12242       else if (cxx_dialect >= cxx11
12243                && token2->type == CPP_NAME
12244                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12245                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12246         cp_parser_alias_declaration (parser);
12247       /* Otherwise, it's a using-declaration.  */
12248       else
12249         cp_parser_using_declaration (parser,
12250                                      /*access_declaration_p=*/false);
12251     }
12252   /* If the next keyword is `__label__' we have a misplaced label
12253      declaration.  */
12254   else if (token1->keyword == RID_LABEL)
12255     {
12256       cp_lexer_consume_token (parser->lexer);
12257       error_at (token1->location, "%<__label__%> not at the beginning of a block");
12258       cp_parser_skip_to_end_of_statement (parser);
12259       /* If the next token is now a `;', consume it.  */
12260       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12261         cp_lexer_consume_token (parser->lexer);
12262     }
12263   /* If the next token is `static_assert' we have a static assertion.  */
12264   else if (token1->keyword == RID_STATIC_ASSERT)
12265     cp_parser_static_assert (parser, /*member_p=*/false);
12266   /* Anything else must be a simple-declaration.  */
12267   else
12268     cp_parser_simple_declaration (parser, !statement_p,
12269                                   /*maybe_range_for_decl*/NULL);
12270 }
12271
12272 /* Parse a simple-declaration.
12273
12274    simple-declaration:
12275      decl-specifier-seq [opt] init-declarator-list [opt] ;
12276
12277    init-declarator-list:
12278      init-declarator
12279      init-declarator-list , init-declarator
12280
12281    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12282    function-definition as a simple-declaration.
12283
12284    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12285    parsed declaration if it is an uninitialized single declarator not followed
12286    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12287    if present, will not be consumed.  */
12288
12289 static void
12290 cp_parser_simple_declaration (cp_parser* parser,
12291                               bool function_definition_allowed_p,
12292                               tree *maybe_range_for_decl)
12293 {
12294   cp_decl_specifier_seq decl_specifiers;
12295   int declares_class_or_enum;
12296   bool saw_declarator;
12297   location_t comma_loc = UNKNOWN_LOCATION;
12298   location_t init_loc = UNKNOWN_LOCATION;
12299
12300   if (maybe_range_for_decl)
12301     *maybe_range_for_decl = NULL_TREE;
12302
12303   /* Defer access checks until we know what is being declared; the
12304      checks for names appearing in the decl-specifier-seq should be
12305      done as if we were in the scope of the thing being declared.  */
12306   push_deferring_access_checks (dk_deferred);
12307
12308   /* Parse the decl-specifier-seq.  We have to keep track of whether
12309      or not the decl-specifier-seq declares a named class or
12310      enumeration type, since that is the only case in which the
12311      init-declarator-list is allowed to be empty.
12312
12313      [dcl.dcl]
12314
12315      In a simple-declaration, the optional init-declarator-list can be
12316      omitted only when declaring a class or enumeration, that is when
12317      the decl-specifier-seq contains either a class-specifier, an
12318      elaborated-type-specifier, or an enum-specifier.  */
12319   cp_parser_decl_specifier_seq (parser,
12320                                 CP_PARSER_FLAGS_OPTIONAL,
12321                                 &decl_specifiers,
12322                                 &declares_class_or_enum);
12323   /* We no longer need to defer access checks.  */
12324   stop_deferring_access_checks ();
12325
12326   /* In a block scope, a valid declaration must always have a
12327      decl-specifier-seq.  By not trying to parse declarators, we can
12328      resolve the declaration/expression ambiguity more quickly.  */
12329   if (!function_definition_allowed_p
12330       && !decl_specifiers.any_specifiers_p)
12331     {
12332       cp_parser_error (parser, "expected declaration");
12333       goto done;
12334     }
12335
12336   /* If the next two tokens are both identifiers, the code is
12337      erroneous. The usual cause of this situation is code like:
12338
12339        T t;
12340
12341      where "T" should name a type -- but does not.  */
12342   if (!decl_specifiers.any_type_specifiers_p
12343       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12344     {
12345       /* If parsing tentatively, we should commit; we really are
12346          looking at a declaration.  */
12347       cp_parser_commit_to_tentative_parse (parser);
12348       /* Give up.  */
12349       goto done;
12350     }
12351
12352   /* If we have seen at least one decl-specifier, and the next token
12353      is not a parenthesis, then we must be looking at a declaration.
12354      (After "int (" we might be looking at a functional cast.)  */
12355   if (decl_specifiers.any_specifiers_p
12356       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12357       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12358       && !cp_parser_error_occurred (parser))
12359     cp_parser_commit_to_tentative_parse (parser);
12360
12361   tree last_type;
12362
12363   last_type = NULL_TREE;
12364
12365   /* Keep going until we hit the `;' at the end of the simple
12366      declaration.  */
12367   saw_declarator = false;
12368   while (cp_lexer_next_token_is_not (parser->lexer,
12369                                      CPP_SEMICOLON))
12370     {
12371       cp_token *token;
12372       bool function_definition_p;
12373       tree decl;
12374       tree auto_result = NULL_TREE;
12375
12376       if (saw_declarator)
12377         {
12378           /* If we are processing next declarator, comma is expected */
12379           token = cp_lexer_peek_token (parser->lexer);
12380           gcc_assert (token->type == CPP_COMMA);
12381           cp_lexer_consume_token (parser->lexer);
12382           if (maybe_range_for_decl)
12383             {
12384               *maybe_range_for_decl = error_mark_node;
12385               if (comma_loc == UNKNOWN_LOCATION)
12386                 comma_loc = token->location;
12387             }
12388         }
12389       else
12390         saw_declarator = true;
12391
12392       /* Parse the init-declarator.  */
12393       decl = cp_parser_init_declarator (parser, &decl_specifiers,
12394                                         /*checks=*/NULL,
12395                                         function_definition_allowed_p,
12396                                         /*member_p=*/false,
12397                                         declares_class_or_enum,
12398                                         &function_definition_p,
12399                                         maybe_range_for_decl,
12400                                         &init_loc,
12401                                         &auto_result);
12402       /* If an error occurred while parsing tentatively, exit quickly.
12403          (That usually happens when in the body of a function; each
12404          statement is treated as a declaration-statement until proven
12405          otherwise.)  */
12406       if (cp_parser_error_occurred (parser))
12407         goto done;
12408
12409       if (auto_result
12410           && (!processing_template_decl || !type_uses_auto (auto_result)))
12411         {
12412           if (last_type
12413               && last_type != error_mark_node
12414               && !same_type_p (auto_result, last_type))
12415             {
12416               /* If the list of declarators contains more than one declarator,
12417                  the type of each declared variable is determined as described
12418                  above. If the type deduced for the template parameter U is not
12419                  the same in each deduction, the program is ill-formed.  */
12420               error_at (decl_specifiers.locations[ds_type_spec],
12421                         "inconsistent deduction for %qT: %qT and then %qT",
12422                         decl_specifiers.type, last_type, auto_result);
12423               last_type = error_mark_node;
12424             }
12425           else
12426             last_type = auto_result;
12427         }
12428
12429       /* Handle function definitions specially.  */
12430       if (function_definition_p)
12431         {
12432           /* If the next token is a `,', then we are probably
12433              processing something like:
12434
12435                void f() {}, *p;
12436
12437              which is erroneous.  */
12438           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12439             {
12440               cp_token *token = cp_lexer_peek_token (parser->lexer);
12441               error_at (token->location,
12442                         "mixing"
12443                         " declarations and function-definitions is forbidden");
12444             }
12445           /* Otherwise, we're done with the list of declarators.  */
12446           else
12447             {
12448               pop_deferring_access_checks ();
12449               return;
12450             }
12451         }
12452       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12453         *maybe_range_for_decl = decl;
12454       /* The next token should be either a `,' or a `;'.  */
12455       token = cp_lexer_peek_token (parser->lexer);
12456       /* If it's a `,', there are more declarators to come.  */
12457       if (token->type == CPP_COMMA)
12458         /* will be consumed next time around */;
12459       /* If it's a `;', we are done.  */
12460       else if (token->type == CPP_SEMICOLON)
12461         break;
12462       else if (maybe_range_for_decl)
12463         {
12464           if (declares_class_or_enum && token->type == CPP_COLON)
12465             pedwarn (decl_specifiers.locations[ds_type_spec], 0,
12466                      "types may not be defined in a for-range-declaration");
12467           break;
12468         }
12469       /* Anything else is an error.  */
12470       else
12471         {
12472           /* If we have already issued an error message we don't need
12473              to issue another one.  */
12474           if ((decl != error_mark_node
12475                && DECL_INITIAL (decl) != error_mark_node)
12476               || cp_parser_uncommitted_to_tentative_parse_p (parser))
12477             cp_parser_error (parser, "expected %<,%> or %<;%>");
12478           /* Skip tokens until we reach the end of the statement.  */
12479           cp_parser_skip_to_end_of_statement (parser);
12480           /* If the next token is now a `;', consume it.  */
12481           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12482             cp_lexer_consume_token (parser->lexer);
12483           goto done;
12484         }
12485       /* After the first time around, a function-definition is not
12486          allowed -- even if it was OK at first.  For example:
12487
12488            int i, f() {}
12489
12490          is not valid.  */
12491       function_definition_allowed_p = false;
12492     }
12493
12494   /* Issue an error message if no declarators are present, and the
12495      decl-specifier-seq does not itself declare a class or
12496      enumeration: [dcl.dcl]/3.  */
12497   if (!saw_declarator)
12498     {
12499       if (cp_parser_declares_only_class_p (parser))
12500         {
12501           if (!declares_class_or_enum
12502               && decl_specifiers.type
12503               && OVERLOAD_TYPE_P (decl_specifiers.type))
12504             /* Ensure an error is issued anyway when finish_decltype_type,
12505                called via cp_parser_decl_specifier_seq, returns a class or
12506                an enumeration (c++/51786).  */
12507             decl_specifiers.type = NULL_TREE;
12508           shadow_tag (&decl_specifiers);
12509         }
12510       /* Perform any deferred access checks.  */
12511       perform_deferred_access_checks (tf_warning_or_error);
12512     }
12513
12514   /* Consume the `;'.  */
12515   if (!maybe_range_for_decl)
12516     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12517   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12518     {
12519       if (init_loc != UNKNOWN_LOCATION)
12520         error_at (init_loc, "initializer in range-based %<for%> loop");
12521       if (comma_loc != UNKNOWN_LOCATION)
12522         error_at (comma_loc,
12523                   "multiple declarations in range-based %<for%> loop");
12524     }
12525
12526  done:
12527   pop_deferring_access_checks ();
12528 }
12529
12530 /* Parse a decl-specifier-seq.
12531
12532    decl-specifier-seq:
12533      decl-specifier-seq [opt] decl-specifier
12534      decl-specifier attribute-specifier-seq [opt] (C++11)
12535
12536    decl-specifier:
12537      storage-class-specifier
12538      type-specifier
12539      function-specifier
12540      friend
12541      typedef
12542
12543    GNU Extension:
12544
12545    decl-specifier:
12546      attributes
12547
12548    Concepts Extension:
12549
12550    decl-specifier:
12551      concept
12552
12553    Set *DECL_SPECS to a representation of the decl-specifier-seq.
12554
12555    The parser flags FLAGS is used to control type-specifier parsing.
12556
12557    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12558    flags:
12559
12560      1: one of the decl-specifiers is an elaborated-type-specifier
12561         (i.e., a type declaration)
12562      2: one of the decl-specifiers is an enum-specifier or a
12563         class-specifier (i.e., a type definition)
12564
12565    */
12566
12567 static void
12568 cp_parser_decl_specifier_seq (cp_parser* parser,
12569                               cp_parser_flags flags,
12570                               cp_decl_specifier_seq *decl_specs,
12571                               int* declares_class_or_enum)
12572 {
12573   bool constructor_possible_p = !parser->in_declarator_p;
12574   bool found_decl_spec = false;
12575   cp_token *start_token = NULL;
12576   cp_decl_spec ds;
12577
12578   /* Clear DECL_SPECS.  */
12579   clear_decl_specs (decl_specs);
12580
12581   /* Assume no class or enumeration type is declared.  */
12582   *declares_class_or_enum = 0;
12583
12584   /* Keep reading specifiers until there are no more to read.  */
12585   while (true)
12586     {
12587       bool constructor_p;
12588       cp_token *token;
12589       ds = ds_last;
12590
12591       /* Peek at the next token.  */
12592       token = cp_lexer_peek_token (parser->lexer);
12593
12594       /* Save the first token of the decl spec list for error
12595          reporting.  */
12596       if (!start_token)
12597         start_token = token;
12598       /* Handle attributes.  */
12599       if (cp_next_tokens_can_be_attribute_p (parser))
12600         {
12601           /* Parse the attributes.  */
12602           tree attrs = cp_parser_attributes_opt (parser);
12603
12604           /* In a sequence of declaration specifiers, c++11 attributes
12605              appertain to the type that precede them. In that case
12606              [dcl.spec]/1 says:
12607
12608                  The attribute-specifier-seq affects the type only for
12609                  the declaration it appears in, not other declarations
12610                  involving the same type.
12611
12612              But for now let's force the user to position the
12613              attribute either at the beginning of the declaration or
12614              after the declarator-id, which would clearly mean that it
12615              applies to the declarator.  */
12616           if (cxx11_attribute_p (attrs))
12617             {
12618               if (!found_decl_spec)
12619                 /* The c++11 attribute is at the beginning of the
12620                    declaration.  It appertains to the entity being
12621                    declared.  */;
12622               else
12623                 {
12624                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12625                     {
12626                       /*  This is an attribute following a
12627                           class-specifier.  */
12628                       if (decl_specs->type_definition_p)
12629                         warn_misplaced_attr_for_class_type (token->location,
12630                                                             decl_specs->type);
12631                       attrs = NULL_TREE;
12632                     }
12633                   else
12634                     {
12635                       decl_specs->std_attributes
12636                         = chainon (decl_specs->std_attributes,
12637                                    attrs);
12638                       if (decl_specs->locations[ds_std_attribute] == 0)
12639                         decl_specs->locations[ds_std_attribute] = token->location;
12640                     }
12641                   continue;
12642                 }
12643             }
12644
12645             decl_specs->attributes
12646               = chainon (decl_specs->attributes,
12647                          attrs);
12648           if (decl_specs->locations[ds_attribute] == 0)
12649             decl_specs->locations[ds_attribute] = token->location;
12650           continue;
12651         }
12652       /* Assume we will find a decl-specifier keyword.  */
12653       found_decl_spec = true;
12654       /* If the next token is an appropriate keyword, we can simply
12655          add it to the list.  */
12656       switch (token->keyword)
12657         {
12658           /* decl-specifier:
12659                friend
12660                constexpr */
12661         case RID_FRIEND:
12662           if (!at_class_scope_p ())
12663             {
12664               error_at (token->location, "%<friend%> used outside of class");
12665               cp_lexer_purge_token (parser->lexer);
12666             }
12667           else
12668             {
12669               ds = ds_friend;
12670               /* Consume the token.  */
12671               cp_lexer_consume_token (parser->lexer);
12672             }
12673           break;
12674
12675         case RID_CONSTEXPR:
12676           ds = ds_constexpr;
12677           cp_lexer_consume_token (parser->lexer);
12678           break;
12679
12680         case RID_CONCEPT:
12681           ds = ds_concept;
12682           cp_lexer_consume_token (parser->lexer);
12683           break;
12684
12685           /* function-specifier:
12686                inline
12687                virtual
12688                explicit  */
12689         case RID_INLINE:
12690         case RID_VIRTUAL:
12691         case RID_EXPLICIT:
12692           cp_parser_function_specifier_opt (parser, decl_specs);
12693           break;
12694
12695           /* decl-specifier:
12696                typedef  */
12697         case RID_TYPEDEF:
12698           ds = ds_typedef;
12699           /* Consume the token.  */
12700           cp_lexer_consume_token (parser->lexer);
12701           /* A constructor declarator cannot appear in a typedef.  */
12702           constructor_possible_p = false;
12703           /* The "typedef" keyword can only occur in a declaration; we
12704              may as well commit at this point.  */
12705           cp_parser_commit_to_tentative_parse (parser);
12706
12707           if (decl_specs->storage_class != sc_none)
12708             decl_specs->conflicting_specifiers_p = true;
12709           break;
12710
12711           /* storage-class-specifier:
12712                auto
12713                register
12714                static
12715                extern
12716                mutable
12717
12718              GNU Extension:
12719                thread  */
12720         case RID_AUTO:
12721           if (cxx_dialect == cxx98) 
12722             {
12723               /* Consume the token.  */
12724               cp_lexer_consume_token (parser->lexer);
12725
12726               /* Complain about `auto' as a storage specifier, if
12727                  we're complaining about C++0x compatibility.  */
12728               warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12729                           " changes meaning in C++11; please remove it");
12730
12731               /* Set the storage class anyway.  */
12732               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12733                                            token);
12734             }
12735           else
12736             /* C++0x auto type-specifier.  */
12737             found_decl_spec = false;
12738           break;
12739
12740         case RID_REGISTER:
12741         case RID_STATIC:
12742         case RID_EXTERN:
12743         case RID_MUTABLE:
12744           /* Consume the token.  */
12745           cp_lexer_consume_token (parser->lexer);
12746           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12747                                        token);
12748           break;
12749         case RID_THREAD:
12750           /* Consume the token.  */
12751           ds = ds_thread;
12752           cp_lexer_consume_token (parser->lexer);
12753           break;
12754
12755         default:
12756           /* We did not yet find a decl-specifier yet.  */
12757           found_decl_spec = false;
12758           break;
12759         }
12760
12761       if (found_decl_spec
12762           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12763           && token->keyword != RID_CONSTEXPR)
12764         error ("decl-specifier invalid in condition");
12765
12766       if (ds != ds_last)
12767         set_and_check_decl_spec_loc (decl_specs, ds, token);
12768
12769       /* Constructors are a special case.  The `S' in `S()' is not a
12770          decl-specifier; it is the beginning of the declarator.  */
12771       constructor_p
12772         = (!found_decl_spec
12773            && constructor_possible_p
12774            && (cp_parser_constructor_declarator_p
12775                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12776
12777       /* If we don't have a DECL_SPEC yet, then we must be looking at
12778          a type-specifier.  */
12779       if (!found_decl_spec && !constructor_p)
12780         {
12781           int decl_spec_declares_class_or_enum;
12782           bool is_cv_qualifier;
12783           tree type_spec;
12784
12785           type_spec
12786             = cp_parser_type_specifier (parser, flags,
12787                                         decl_specs,
12788                                         /*is_declaration=*/true,
12789                                         &decl_spec_declares_class_or_enum,
12790                                         &is_cv_qualifier);
12791           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12792
12793           /* If this type-specifier referenced a user-defined type
12794              (a typedef, class-name, etc.), then we can't allow any
12795              more such type-specifiers henceforth.
12796
12797              [dcl.spec]
12798
12799              The longest sequence of decl-specifiers that could
12800              possibly be a type name is taken as the
12801              decl-specifier-seq of a declaration.  The sequence shall
12802              be self-consistent as described below.
12803
12804              [dcl.type]
12805
12806              As a general rule, at most one type-specifier is allowed
12807              in the complete decl-specifier-seq of a declaration.  The
12808              only exceptions are the following:
12809
12810              -- const or volatile can be combined with any other
12811                 type-specifier.
12812
12813              -- signed or unsigned can be combined with char, long,
12814                 short, or int.
12815
12816              -- ..
12817
12818              Example:
12819
12820                typedef char* Pc;
12821                void g (const int Pc);
12822
12823              Here, Pc is *not* part of the decl-specifier seq; it's
12824              the declarator.  Therefore, once we see a type-specifier
12825              (other than a cv-qualifier), we forbid any additional
12826              user-defined types.  We *do* still allow things like `int
12827              int' to be considered a decl-specifier-seq, and issue the
12828              error message later.  */
12829           if (type_spec && !is_cv_qualifier)
12830             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12831           /* A constructor declarator cannot follow a type-specifier.  */
12832           if (type_spec)
12833             {
12834               constructor_possible_p = false;
12835               found_decl_spec = true;
12836               if (!is_cv_qualifier)
12837                 decl_specs->any_type_specifiers_p = true;
12838             }
12839         }
12840
12841       /* If we still do not have a DECL_SPEC, then there are no more
12842          decl-specifiers.  */
12843       if (!found_decl_spec)
12844         break;
12845
12846       decl_specs->any_specifiers_p = true;
12847       /* After we see one decl-specifier, further decl-specifiers are
12848          always optional.  */
12849       flags |= CP_PARSER_FLAGS_OPTIONAL;
12850     }
12851
12852   /* Don't allow a friend specifier with a class definition.  */
12853   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12854       && (*declares_class_or_enum & 2))
12855     error_at (decl_specs->locations[ds_friend],
12856               "class definition may not be declared a friend");
12857 }
12858
12859 /* Parse an (optional) storage-class-specifier.
12860
12861    storage-class-specifier:
12862      auto
12863      register
12864      static
12865      extern
12866      mutable
12867
12868    GNU Extension:
12869
12870    storage-class-specifier:
12871      thread
12872
12873    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
12874
12875 static tree
12876 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12877 {
12878   switch (cp_lexer_peek_token (parser->lexer)->keyword)
12879     {
12880     case RID_AUTO:
12881       if (cxx_dialect != cxx98)
12882         return NULL_TREE;
12883       /* Fall through for C++98.  */
12884
12885     case RID_REGISTER:
12886     case RID_STATIC:
12887     case RID_EXTERN:
12888     case RID_MUTABLE:
12889     case RID_THREAD:
12890       /* Consume the token.  */
12891       return cp_lexer_consume_token (parser->lexer)->u.value;
12892
12893     default:
12894       return NULL_TREE;
12895     }
12896 }
12897
12898 /* Parse an (optional) function-specifier.
12899
12900    function-specifier:
12901      inline
12902      virtual
12903      explicit
12904
12905    Returns an IDENTIFIER_NODE corresponding to the keyword used.
12906    Updates DECL_SPECS, if it is non-NULL.  */
12907
12908 static tree
12909 cp_parser_function_specifier_opt (cp_parser* parser,
12910                                   cp_decl_specifier_seq *decl_specs)
12911 {
12912   cp_token *token = cp_lexer_peek_token (parser->lexer);
12913   switch (token->keyword)
12914     {
12915     case RID_INLINE:
12916       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12917       break;
12918
12919     case RID_VIRTUAL:
12920       /* 14.5.2.3 [temp.mem]
12921
12922          A member function template shall not be virtual.  */
12923       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12924         error_at (token->location, "templates may not be %<virtual%>");
12925       else
12926         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12927       break;
12928
12929     case RID_EXPLICIT:
12930       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12931       break;
12932
12933     default:
12934       return NULL_TREE;
12935     }
12936
12937   /* Consume the token.  */
12938   return cp_lexer_consume_token (parser->lexer)->u.value;
12939 }
12940
12941 /* Parse a linkage-specification.
12942
12943    linkage-specification:
12944      extern string-literal { declaration-seq [opt] }
12945      extern string-literal declaration  */
12946
12947 static void
12948 cp_parser_linkage_specification (cp_parser* parser)
12949 {
12950   tree linkage;
12951
12952   /* Look for the `extern' keyword.  */
12953   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12954
12955   /* Look for the string-literal.  */
12956   linkage = cp_parser_string_literal (parser, false, false);
12957
12958   /* Transform the literal into an identifier.  If the literal is a
12959      wide-character string, or contains embedded NULs, then we can't
12960      handle it as the user wants.  */
12961   if (strlen (TREE_STRING_POINTER (linkage))
12962       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12963     {
12964       cp_parser_error (parser, "invalid linkage-specification");
12965       /* Assume C++ linkage.  */
12966       linkage = lang_name_cplusplus;
12967     }
12968   else
12969     linkage = get_identifier (TREE_STRING_POINTER (linkage));
12970
12971   /* We're now using the new linkage.  */
12972   push_lang_context (linkage);
12973
12974   /* If the next token is a `{', then we're using the first
12975      production.  */
12976   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12977     {
12978       cp_ensure_no_omp_declare_simd (parser);
12979       cp_ensure_no_oacc_routine (parser);
12980
12981       /* Consume the `{' token.  */
12982       cp_lexer_consume_token (parser->lexer);
12983       /* Parse the declarations.  */
12984       cp_parser_declaration_seq_opt (parser);
12985       /* Look for the closing `}'.  */
12986       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12987     }
12988   /* Otherwise, there's just one declaration.  */
12989   else
12990     {
12991       bool saved_in_unbraced_linkage_specification_p;
12992
12993       saved_in_unbraced_linkage_specification_p
12994         = parser->in_unbraced_linkage_specification_p;
12995       parser->in_unbraced_linkage_specification_p = true;
12996       cp_parser_declaration (parser);
12997       parser->in_unbraced_linkage_specification_p
12998         = saved_in_unbraced_linkage_specification_p;
12999     }
13000
13001   /* We're done with the linkage-specification.  */
13002   pop_lang_context ();
13003 }
13004
13005 /* Parse a static_assert-declaration.
13006
13007    static_assert-declaration:
13008      static_assert ( constant-expression , string-literal ) ; 
13009      static_assert ( constant-expression ) ; (C++1Z)
13010
13011    If MEMBER_P, this static_assert is a class member.  */
13012
13013 static void 
13014 cp_parser_static_assert(cp_parser *parser, bool member_p)
13015 {
13016   tree condition;
13017   tree message;
13018   cp_token *token;
13019   location_t saved_loc;
13020   bool dummy;
13021
13022   /* Peek at the `static_assert' token so we can keep track of exactly
13023      where the static assertion started.  */
13024   token = cp_lexer_peek_token (parser->lexer);
13025   saved_loc = token->location;
13026
13027   /* Look for the `static_assert' keyword.  */
13028   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
13029                                   RT_STATIC_ASSERT))
13030     return;
13031
13032   /*  We know we are in a static assertion; commit to any tentative
13033       parse.  */
13034   if (cp_parser_parsing_tentatively (parser))
13035     cp_parser_commit_to_tentative_parse (parser);
13036
13037   /* Parse the `(' starting the static assertion condition.  */
13038   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13039
13040   /* Parse the constant-expression.  Allow a non-constant expression
13041      here in order to give better diagnostics in finish_static_assert.  */
13042   condition = 
13043     cp_parser_constant_expression (parser,
13044                                    /*allow_non_constant_p=*/true,
13045                                    /*non_constant_p=*/&dummy);
13046
13047   if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13048     {
13049       if (cxx_dialect < cxx1z)
13050         pedwarn (input_location, OPT_Wpedantic,
13051                  "static_assert without a message "
13052                  "only available with -std=c++1z or -std=gnu++1z");
13053       /* Eat the ')'  */
13054       cp_lexer_consume_token (parser->lexer);
13055       message = build_string (1, "");
13056       TREE_TYPE (message) = char_array_type_node;
13057       fix_string_type (message);
13058     }
13059   else
13060     {
13061       /* Parse the separating `,'.  */
13062       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13063
13064       /* Parse the string-literal message.  */
13065       message = cp_parser_string_literal (parser, 
13066                                           /*translate=*/false,
13067                                           /*wide_ok=*/true);
13068
13069       /* A `)' completes the static assertion.  */
13070       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13071         cp_parser_skip_to_closing_parenthesis (parser, 
13072                                                /*recovering=*/true, 
13073                                                /*or_comma=*/false,
13074                                                /*consume_paren=*/true);
13075     }
13076
13077   /* A semicolon terminates the declaration.  */
13078   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13079
13080   /* Complete the static assertion, which may mean either processing 
13081      the static assert now or saving it for template instantiation.  */
13082   finish_static_assert (condition, message, saved_loc, member_p);
13083 }
13084
13085 /* Parse the expression in decltype ( expression ).  */
13086
13087 static tree
13088 cp_parser_decltype_expr (cp_parser *parser,
13089                          bool &id_expression_or_member_access_p)
13090 {
13091   cp_token *id_expr_start_token;
13092   tree expr;
13093
13094   /* Since we're going to preserve any side-effects from this parse, set up a
13095      firewall to protect our callers from cp_parser_commit_to_tentative_parse
13096      in the expression.  */
13097   tentative_firewall firewall (parser);
13098
13099   /* First, try parsing an id-expression.  */
13100   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13101   cp_parser_parse_tentatively (parser);
13102   expr = cp_parser_id_expression (parser,
13103                                   /*template_keyword_p=*/false,
13104                                   /*check_dependency_p=*/true,
13105                                   /*template_p=*/NULL,
13106                                   /*declarator_p=*/false,
13107                                   /*optional_p=*/false);
13108
13109   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13110     {
13111       bool non_integral_constant_expression_p = false;
13112       tree id_expression = expr;
13113       cp_id_kind idk;
13114       const char *error_msg;
13115
13116       if (identifier_p (expr))
13117         /* Lookup the name we got back from the id-expression.  */
13118         expr = cp_parser_lookup_name_simple (parser, expr,
13119                                              id_expr_start_token->location);
13120
13121       if (expr
13122           && expr != error_mark_node
13123           && TREE_CODE (expr) != TYPE_DECL
13124           && (TREE_CODE (expr) != BIT_NOT_EXPR
13125               || !TYPE_P (TREE_OPERAND (expr, 0)))
13126           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13127         {
13128           /* Complete lookup of the id-expression.  */
13129           expr = (finish_id_expression
13130                   (id_expression, expr, parser->scope, &idk,
13131                    /*integral_constant_expression_p=*/false,
13132                    /*allow_non_integral_constant_expression_p=*/true,
13133                    &non_integral_constant_expression_p,
13134                    /*template_p=*/false,
13135                    /*done=*/true,
13136                    /*address_p=*/false,
13137                    /*template_arg_p=*/false,
13138                    &error_msg,
13139                    id_expr_start_token->location));
13140
13141           if (expr == error_mark_node)
13142             /* We found an id-expression, but it was something that we
13143                should not have found. This is an error, not something
13144                we can recover from, so note that we found an
13145                id-expression and we'll recover as gracefully as
13146                possible.  */
13147             id_expression_or_member_access_p = true;
13148         }
13149
13150       if (expr 
13151           && expr != error_mark_node
13152           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13153         /* We have an id-expression.  */
13154         id_expression_or_member_access_p = true;
13155     }
13156
13157   if (!id_expression_or_member_access_p)
13158     {
13159       /* Abort the id-expression parse.  */
13160       cp_parser_abort_tentative_parse (parser);
13161
13162       /* Parsing tentatively, again.  */
13163       cp_parser_parse_tentatively (parser);
13164
13165       /* Parse a class member access.  */
13166       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13167                                            /*cast_p=*/false, /*decltype*/true,
13168                                            /*member_access_only_p=*/true, NULL);
13169
13170       if (expr 
13171           && expr != error_mark_node
13172           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13173         /* We have an id-expression.  */
13174         id_expression_or_member_access_p = true;
13175     }
13176
13177   if (id_expression_or_member_access_p)
13178     /* We have parsed the complete id-expression or member access.  */
13179     cp_parser_parse_definitely (parser);
13180   else
13181     {
13182       /* Abort our attempt to parse an id-expression or member access
13183          expression.  */
13184       cp_parser_abort_tentative_parse (parser);
13185
13186       /* Parse a full expression.  */
13187       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13188                                    /*decltype_p=*/true);
13189     }
13190
13191   return expr;
13192 }
13193
13194 /* Parse a `decltype' type. Returns the type.
13195
13196    simple-type-specifier:
13197      decltype ( expression )
13198    C++14 proposal:
13199      decltype ( auto )  */
13200
13201 static tree
13202 cp_parser_decltype (cp_parser *parser)
13203 {
13204   tree expr;
13205   bool id_expression_or_member_access_p = false;
13206   const char *saved_message;
13207   bool saved_integral_constant_expression_p;
13208   bool saved_non_integral_constant_expression_p;
13209   bool saved_greater_than_is_operator_p;
13210   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13211
13212   if (start_token->type == CPP_DECLTYPE)
13213     {
13214       /* Already parsed.  */
13215       cp_lexer_consume_token (parser->lexer);
13216       return saved_checks_value (start_token->u.tree_check_value);
13217     }
13218
13219   /* Look for the `decltype' token.  */
13220   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13221     return error_mark_node;
13222
13223   /* Parse the opening `('.  */
13224   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13225     return error_mark_node;
13226
13227   /* decltype (auto) */
13228   if (cxx_dialect >= cxx14
13229       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13230     {
13231       cp_lexer_consume_token (parser->lexer);
13232       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13233         return error_mark_node;
13234       expr = make_decltype_auto ();
13235       AUTO_IS_DECLTYPE (expr) = true;
13236       goto rewrite;
13237     }
13238
13239   /* Types cannot be defined in a `decltype' expression.  Save away the
13240      old message.  */
13241   saved_message = parser->type_definition_forbidden_message;
13242
13243   /* And create the new one.  */
13244   parser->type_definition_forbidden_message
13245     = G_("types may not be defined in %<decltype%> expressions");
13246
13247   /* The restrictions on constant-expressions do not apply inside
13248      decltype expressions.  */
13249   saved_integral_constant_expression_p
13250     = parser->integral_constant_expression_p;
13251   saved_non_integral_constant_expression_p
13252     = parser->non_integral_constant_expression_p;
13253   parser->integral_constant_expression_p = false;
13254
13255   /* Within a parenthesized expression, a `>' token is always
13256      the greater-than operator.  */
13257   saved_greater_than_is_operator_p
13258     = parser->greater_than_is_operator_p;
13259   parser->greater_than_is_operator_p = true;
13260
13261   /* Do not actually evaluate the expression.  */
13262   ++cp_unevaluated_operand;
13263
13264   /* Do not warn about problems with the expression.  */
13265   ++c_inhibit_evaluation_warnings;
13266
13267   expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13268
13269   /* Go back to evaluating expressions.  */
13270   --cp_unevaluated_operand;
13271   --c_inhibit_evaluation_warnings;
13272
13273   /* The `>' token might be the end of a template-id or
13274      template-parameter-list now.  */
13275   parser->greater_than_is_operator_p
13276     = saved_greater_than_is_operator_p;
13277
13278   /* Restore the old message and the integral constant expression
13279      flags.  */
13280   parser->type_definition_forbidden_message = saved_message;
13281   parser->integral_constant_expression_p
13282     = saved_integral_constant_expression_p;
13283   parser->non_integral_constant_expression_p
13284     = saved_non_integral_constant_expression_p;
13285
13286   /* Parse to the closing `)'.  */
13287   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13288     {
13289       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13290                                              /*consume_paren=*/true);
13291       return error_mark_node;
13292     }
13293
13294   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13295                                tf_warning_or_error);
13296
13297  rewrite:
13298   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13299      it again.  */
13300   start_token->type = CPP_DECLTYPE;
13301   start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13302   start_token->u.tree_check_value->value = expr;
13303   start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13304   start_token->keyword = RID_MAX;
13305   cp_lexer_purge_tokens_after (parser->lexer, start_token);
13306
13307   return expr;
13308 }
13309
13310 /* Special member functions [gram.special] */
13311
13312 /* Parse a conversion-function-id.
13313
13314    conversion-function-id:
13315      operator conversion-type-id
13316
13317    Returns an IDENTIFIER_NODE representing the operator.  */
13318
13319 static tree
13320 cp_parser_conversion_function_id (cp_parser* parser)
13321 {
13322   tree type;
13323   tree saved_scope;
13324   tree saved_qualifying_scope;
13325   tree saved_object_scope;
13326   tree pushed_scope = NULL_TREE;
13327
13328   /* Look for the `operator' token.  */
13329   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13330     return error_mark_node;
13331   /* When we parse the conversion-type-id, the current scope will be
13332      reset.  However, we need that information in able to look up the
13333      conversion function later, so we save it here.  */
13334   saved_scope = parser->scope;
13335   saved_qualifying_scope = parser->qualifying_scope;
13336   saved_object_scope = parser->object_scope;
13337   /* We must enter the scope of the class so that the names of
13338      entities declared within the class are available in the
13339      conversion-type-id.  For example, consider:
13340
13341        struct S {
13342          typedef int I;
13343          operator I();
13344        };
13345
13346        S::operator I() { ... }
13347
13348      In order to see that `I' is a type-name in the definition, we
13349      must be in the scope of `S'.  */
13350   if (saved_scope)
13351     pushed_scope = push_scope (saved_scope);
13352   /* Parse the conversion-type-id.  */
13353   type = cp_parser_conversion_type_id (parser);
13354   /* Leave the scope of the class, if any.  */
13355   if (pushed_scope)
13356     pop_scope (pushed_scope);
13357   /* Restore the saved scope.  */
13358   parser->scope = saved_scope;
13359   parser->qualifying_scope = saved_qualifying_scope;
13360   parser->object_scope = saved_object_scope;
13361   /* If the TYPE is invalid, indicate failure.  */
13362   if (type == error_mark_node)
13363     return error_mark_node;
13364   return mangle_conv_op_name_for_type (type);
13365 }
13366
13367 /* Parse a conversion-type-id:
13368
13369    conversion-type-id:
13370      type-specifier-seq conversion-declarator [opt]
13371
13372    Returns the TYPE specified.  */
13373
13374 static tree
13375 cp_parser_conversion_type_id (cp_parser* parser)
13376 {
13377   tree attributes;
13378   cp_decl_specifier_seq type_specifiers;
13379   cp_declarator *declarator;
13380   tree type_specified;
13381   const char *saved_message;
13382
13383   /* Parse the attributes.  */
13384   attributes = cp_parser_attributes_opt (parser);
13385
13386   saved_message = parser->type_definition_forbidden_message;
13387   parser->type_definition_forbidden_message
13388     = G_("types may not be defined in a conversion-type-id");
13389
13390   /* Parse the type-specifiers.  */
13391   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13392                                 /*is_trailing_return=*/false,
13393                                 &type_specifiers);
13394
13395   parser->type_definition_forbidden_message = saved_message;
13396
13397   /* If that didn't work, stop.  */
13398   if (type_specifiers.type == error_mark_node)
13399     return error_mark_node;
13400   /* Parse the conversion-declarator.  */
13401   declarator = cp_parser_conversion_declarator_opt (parser);
13402
13403   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
13404                                     /*initialized=*/0, &attributes);
13405   if (attributes)
13406     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13407
13408   /* Don't give this error when parsing tentatively.  This happens to
13409      work because we always parse this definitively once.  */
13410   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13411       && type_uses_auto (type_specified))
13412     {
13413       if (cxx_dialect < cxx14)
13414         {
13415           error ("invalid use of %<auto%> in conversion operator");
13416           return error_mark_node;
13417         }
13418       else if (template_parm_scope_p ())
13419         warning (0, "use of %<auto%> in member template "
13420                  "conversion operator can never be deduced");
13421     }
13422
13423   return type_specified;
13424 }
13425
13426 /* Parse an (optional) conversion-declarator.
13427
13428    conversion-declarator:
13429      ptr-operator conversion-declarator [opt]
13430
13431    */
13432
13433 static cp_declarator *
13434 cp_parser_conversion_declarator_opt (cp_parser* parser)
13435 {
13436   enum tree_code code;
13437   tree class_type, std_attributes = NULL_TREE;
13438   cp_cv_quals cv_quals;
13439
13440   /* We don't know if there's a ptr-operator next, or not.  */
13441   cp_parser_parse_tentatively (parser);
13442   /* Try the ptr-operator.  */
13443   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13444                                  &std_attributes);
13445   /* If it worked, look for more conversion-declarators.  */
13446   if (cp_parser_parse_definitely (parser))
13447     {
13448       cp_declarator *declarator;
13449
13450       /* Parse another optional declarator.  */
13451       declarator = cp_parser_conversion_declarator_opt (parser);
13452
13453       declarator = cp_parser_make_indirect_declarator
13454         (code, class_type, cv_quals, declarator, std_attributes);
13455
13456       return declarator;
13457    }
13458
13459   return NULL;
13460 }
13461
13462 /* Parse an (optional) ctor-initializer.
13463
13464    ctor-initializer:
13465      : mem-initializer-list
13466
13467    Returns TRUE iff the ctor-initializer was actually present.  */
13468
13469 static bool
13470 cp_parser_ctor_initializer_opt (cp_parser* parser)
13471 {
13472   /* If the next token is not a `:', then there is no
13473      ctor-initializer.  */
13474   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13475     {
13476       /* Do default initialization of any bases and members.  */
13477       if (DECL_CONSTRUCTOR_P (current_function_decl))
13478         finish_mem_initializers (NULL_TREE);
13479
13480       return false;
13481     }
13482
13483   /* Consume the `:' token.  */
13484   cp_lexer_consume_token (parser->lexer);
13485   /* And the mem-initializer-list.  */
13486   cp_parser_mem_initializer_list (parser);
13487
13488   return true;
13489 }
13490
13491 /* Parse a mem-initializer-list.
13492
13493    mem-initializer-list:
13494      mem-initializer ... [opt]
13495      mem-initializer ... [opt] , mem-initializer-list  */
13496
13497 static void
13498 cp_parser_mem_initializer_list (cp_parser* parser)
13499 {
13500   tree mem_initializer_list = NULL_TREE;
13501   tree target_ctor = error_mark_node;
13502   cp_token *token = cp_lexer_peek_token (parser->lexer);
13503
13504   /* Let the semantic analysis code know that we are starting the
13505      mem-initializer-list.  */
13506   if (!DECL_CONSTRUCTOR_P (current_function_decl))
13507     error_at (token->location,
13508               "only constructors take member initializers");
13509
13510   /* Loop through the list.  */
13511   while (true)
13512     {
13513       tree mem_initializer;
13514
13515       token = cp_lexer_peek_token (parser->lexer);
13516       /* Parse the mem-initializer.  */
13517       mem_initializer = cp_parser_mem_initializer (parser);
13518       /* If the next token is a `...', we're expanding member initializers. */
13519       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13520         {
13521           /* Consume the `...'. */
13522           cp_lexer_consume_token (parser->lexer);
13523
13524           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13525              can be expanded but members cannot. */
13526           if (mem_initializer != error_mark_node
13527               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13528             {
13529               error_at (token->location,
13530                         "cannot expand initializer for member %<%D%>",
13531                         TREE_PURPOSE (mem_initializer));
13532               mem_initializer = error_mark_node;
13533             }
13534
13535           /* Construct the pack expansion type. */
13536           if (mem_initializer != error_mark_node)
13537             mem_initializer = make_pack_expansion (mem_initializer);
13538         }
13539       if (target_ctor != error_mark_node
13540           && mem_initializer != error_mark_node)
13541         {
13542           error ("mem-initializer for %qD follows constructor delegation",
13543                  TREE_PURPOSE (mem_initializer));
13544           mem_initializer = error_mark_node;
13545         }
13546       /* Look for a target constructor. */
13547       if (mem_initializer != error_mark_node
13548           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13549           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13550         {
13551           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13552           if (mem_initializer_list)
13553             {
13554               error ("constructor delegation follows mem-initializer for %qD",
13555                      TREE_PURPOSE (mem_initializer_list));
13556               mem_initializer = error_mark_node;
13557             }
13558           target_ctor = mem_initializer;
13559         }
13560       /* Add it to the list, unless it was erroneous.  */
13561       if (mem_initializer != error_mark_node)
13562         {
13563           TREE_CHAIN (mem_initializer) = mem_initializer_list;
13564           mem_initializer_list = mem_initializer;
13565         }
13566       /* If the next token is not a `,', we're done.  */
13567       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13568         break;
13569       /* Consume the `,' token.  */
13570       cp_lexer_consume_token (parser->lexer);
13571     }
13572
13573   /* Perform semantic analysis.  */
13574   if (DECL_CONSTRUCTOR_P (current_function_decl))
13575     finish_mem_initializers (mem_initializer_list);
13576 }
13577
13578 /* Parse a mem-initializer.
13579
13580    mem-initializer:
13581      mem-initializer-id ( expression-list [opt] )
13582      mem-initializer-id braced-init-list
13583
13584    GNU extension:
13585
13586    mem-initializer:
13587      ( expression-list [opt] )
13588
13589    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
13590    class) or FIELD_DECL (for a non-static data member) to initialize;
13591    the TREE_VALUE is the expression-list.  An empty initialization
13592    list is represented by void_list_node.  */
13593
13594 static tree
13595 cp_parser_mem_initializer (cp_parser* parser)
13596 {
13597   tree mem_initializer_id;
13598   tree expression_list;
13599   tree member;
13600   cp_token *token = cp_lexer_peek_token (parser->lexer);
13601
13602   /* Find out what is being initialized.  */
13603   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13604     {
13605       permerror (token->location,
13606                  "anachronistic old-style base class initializer");
13607       mem_initializer_id = NULL_TREE;
13608     }
13609   else
13610     {
13611       mem_initializer_id = cp_parser_mem_initializer_id (parser);
13612       if (mem_initializer_id == error_mark_node)
13613         return mem_initializer_id;
13614     }
13615   member = expand_member_init (mem_initializer_id);
13616   if (member && !DECL_P (member))
13617     in_base_initializer = 1;
13618
13619   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13620     {
13621       bool expr_non_constant_p;
13622       cp_lexer_set_source_position (parser->lexer);
13623       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13624       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13625       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13626       expression_list = build_tree_list (NULL_TREE, expression_list);
13627     }
13628   else
13629     {
13630       vec<tree, va_gc> *vec;
13631       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13632                                                      /*cast_p=*/false,
13633                                                      /*allow_expansion_p=*/true,
13634                                                      /*non_constant_p=*/NULL);
13635       if (vec == NULL)
13636         return error_mark_node;
13637       expression_list = build_tree_list_vec (vec);
13638       release_tree_vector (vec);
13639     }
13640
13641   if (expression_list == error_mark_node)
13642     return error_mark_node;
13643   if (!expression_list)
13644     expression_list = void_type_node;
13645
13646   in_base_initializer = 0;
13647
13648   return member ? build_tree_list (member, expression_list) : error_mark_node;
13649 }
13650
13651 /* Parse a mem-initializer-id.
13652
13653    mem-initializer-id:
13654      :: [opt] nested-name-specifier [opt] class-name
13655      decltype-specifier (C++11)
13656      identifier
13657
13658    Returns a TYPE indicating the class to be initialized for the first
13659    production (and the second in C++11).  Returns an IDENTIFIER_NODE
13660    indicating the data member to be initialized for the last production.  */
13661
13662 static tree
13663 cp_parser_mem_initializer_id (cp_parser* parser)
13664 {
13665   bool global_scope_p;
13666   bool nested_name_specifier_p;
13667   bool template_p = false;
13668   tree id;
13669
13670   cp_token *token = cp_lexer_peek_token (parser->lexer);
13671
13672   /* `typename' is not allowed in this context ([temp.res]).  */
13673   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13674     {
13675       error_at (token->location, 
13676                 "keyword %<typename%> not allowed in this context (a qualified "
13677                 "member initializer is implicitly a type)");
13678       cp_lexer_consume_token (parser->lexer);
13679     }
13680   /* Look for the optional `::' operator.  */
13681   global_scope_p
13682     = (cp_parser_global_scope_opt (parser,
13683                                    /*current_scope_valid_p=*/false)
13684        != NULL_TREE);
13685   /* Look for the optional nested-name-specifier.  The simplest way to
13686      implement:
13687
13688        [temp.res]
13689
13690        The keyword `typename' is not permitted in a base-specifier or
13691        mem-initializer; in these contexts a qualified name that
13692        depends on a template-parameter is implicitly assumed to be a
13693        type name.
13694
13695      is to assume that we have seen the `typename' keyword at this
13696      point.  */
13697   nested_name_specifier_p
13698     = (cp_parser_nested_name_specifier_opt (parser,
13699                                             /*typename_keyword_p=*/true,
13700                                             /*check_dependency_p=*/true,
13701                                             /*type_p=*/true,
13702                                             /*is_declaration=*/true)
13703        != NULL_TREE);
13704   if (nested_name_specifier_p)
13705     template_p = cp_parser_optional_template_keyword (parser);
13706   /* If there is a `::' operator or a nested-name-specifier, then we
13707      are definitely looking for a class-name.  */
13708   if (global_scope_p || nested_name_specifier_p)
13709     return cp_parser_class_name (parser,
13710                                  /*typename_keyword_p=*/true,
13711                                  /*template_keyword_p=*/template_p,
13712                                  typename_type,
13713                                  /*check_dependency_p=*/true,
13714                                  /*class_head_p=*/false,
13715                                  /*is_declaration=*/true);
13716   /* Otherwise, we could also be looking for an ordinary identifier.  */
13717   cp_parser_parse_tentatively (parser);
13718   if (cp_lexer_next_token_is_decltype (parser->lexer))
13719     /* Try a decltype-specifier.  */
13720     id = cp_parser_decltype (parser);
13721   else
13722     /* Otherwise, try a class-name.  */
13723     id = cp_parser_class_name (parser,
13724                                /*typename_keyword_p=*/true,
13725                                /*template_keyword_p=*/false,
13726                                none_type,
13727                                /*check_dependency_p=*/true,
13728                                /*class_head_p=*/false,
13729                                /*is_declaration=*/true);
13730   /* If we found one, we're done.  */
13731   if (cp_parser_parse_definitely (parser))
13732     return id;
13733   /* Otherwise, look for an ordinary identifier.  */
13734   return cp_parser_identifier (parser);
13735 }
13736
13737 /* Overloading [gram.over] */
13738
13739 /* Parse an operator-function-id.
13740
13741    operator-function-id:
13742      operator operator
13743
13744    Returns an IDENTIFIER_NODE for the operator which is a
13745    human-readable spelling of the identifier, e.g., `operator +'.  */
13746
13747 static cp_expr
13748 cp_parser_operator_function_id (cp_parser* parser)
13749 {
13750   /* Look for the `operator' keyword.  */
13751   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13752     return error_mark_node;
13753   /* And then the name of the operator itself.  */
13754   return cp_parser_operator (parser);
13755 }
13756
13757 /* Return an identifier node for a user-defined literal operator.
13758    The suffix identifier is chained to the operator name identifier.  */
13759
13760 static tree
13761 cp_literal_operator_id (const char* name)
13762 {
13763   tree identifier;
13764   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13765                               + strlen (name) + 10);
13766   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13767   identifier = get_identifier (buffer);
13768
13769   return identifier;
13770 }
13771
13772 /* Parse an operator.
13773
13774    operator:
13775      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13776      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13777      || ++ -- , ->* -> () []
13778
13779    GNU Extensions:
13780
13781    operator:
13782      <? >? <?= >?=
13783
13784    Returns an IDENTIFIER_NODE for the operator which is a
13785    human-readable spelling of the identifier, e.g., `operator +'.  */
13786
13787 static cp_expr
13788 cp_parser_operator (cp_parser* parser)
13789 {
13790   tree id = NULL_TREE;
13791   cp_token *token;
13792   bool utf8 = false;
13793
13794   /* Peek at the next token.  */
13795   token = cp_lexer_peek_token (parser->lexer);
13796
13797   location_t start_loc = token->location;
13798
13799   /* Figure out which operator we have.  */
13800   switch (token->type)
13801     {
13802     case CPP_KEYWORD:
13803       {
13804         enum tree_code op;
13805
13806         /* The keyword should be either `new' or `delete'.  */
13807         if (token->keyword == RID_NEW)
13808           op = NEW_EXPR;
13809         else if (token->keyword == RID_DELETE)
13810           op = DELETE_EXPR;
13811         else
13812           break;
13813
13814         /* Consume the `new' or `delete' token.  */
13815         location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13816
13817         /* Peek at the next token.  */
13818         token = cp_lexer_peek_token (parser->lexer);
13819         /* If it's a `[' token then this is the array variant of the
13820            operator.  */
13821         if (token->type == CPP_OPEN_SQUARE)
13822           {
13823             /* Consume the `[' token.  */
13824             cp_lexer_consume_token (parser->lexer);
13825             /* Look for the `]' token.  */
13826             if (cp_token *close_token
13827                 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13828               end_loc = close_token->location;
13829             id = ansi_opname (op == NEW_EXPR
13830                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13831           }
13832         /* Otherwise, we have the non-array variant.  */
13833         else
13834           id = ansi_opname (op);
13835
13836         location_t loc = make_location (start_loc, start_loc, end_loc);
13837
13838         return cp_expr (id, loc);
13839       }
13840
13841     case CPP_PLUS:
13842       id = ansi_opname (PLUS_EXPR);
13843       break;
13844
13845     case CPP_MINUS:
13846       id = ansi_opname (MINUS_EXPR);
13847       break;
13848
13849     case CPP_MULT:
13850       id = ansi_opname (MULT_EXPR);
13851       break;
13852
13853     case CPP_DIV:
13854       id = ansi_opname (TRUNC_DIV_EXPR);
13855       break;
13856
13857     case CPP_MOD:
13858       id = ansi_opname (TRUNC_MOD_EXPR);
13859       break;
13860
13861     case CPP_XOR:
13862       id = ansi_opname (BIT_XOR_EXPR);
13863       break;
13864
13865     case CPP_AND:
13866       id = ansi_opname (BIT_AND_EXPR);
13867       break;
13868
13869     case CPP_OR:
13870       id = ansi_opname (BIT_IOR_EXPR);
13871       break;
13872
13873     case CPP_COMPL:
13874       id = ansi_opname (BIT_NOT_EXPR);
13875       break;
13876
13877     case CPP_NOT:
13878       id = ansi_opname (TRUTH_NOT_EXPR);
13879       break;
13880
13881     case CPP_EQ:
13882       id = ansi_assopname (NOP_EXPR);
13883       break;
13884
13885     case CPP_LESS:
13886       id = ansi_opname (LT_EXPR);
13887       break;
13888
13889     case CPP_GREATER:
13890       id = ansi_opname (GT_EXPR);
13891       break;
13892
13893     case CPP_PLUS_EQ:
13894       id = ansi_assopname (PLUS_EXPR);
13895       break;
13896
13897     case CPP_MINUS_EQ:
13898       id = ansi_assopname (MINUS_EXPR);
13899       break;
13900
13901     case CPP_MULT_EQ:
13902       id = ansi_assopname (MULT_EXPR);
13903       break;
13904
13905     case CPP_DIV_EQ:
13906       id = ansi_assopname (TRUNC_DIV_EXPR);
13907       break;
13908
13909     case CPP_MOD_EQ:
13910       id = ansi_assopname (TRUNC_MOD_EXPR);
13911       break;
13912
13913     case CPP_XOR_EQ:
13914       id = ansi_assopname (BIT_XOR_EXPR);
13915       break;
13916
13917     case CPP_AND_EQ:
13918       id = ansi_assopname (BIT_AND_EXPR);
13919       break;
13920
13921     case CPP_OR_EQ:
13922       id = ansi_assopname (BIT_IOR_EXPR);
13923       break;
13924
13925     case CPP_LSHIFT:
13926       id = ansi_opname (LSHIFT_EXPR);
13927       break;
13928
13929     case CPP_RSHIFT:
13930       id = ansi_opname (RSHIFT_EXPR);
13931       break;
13932
13933     case CPP_LSHIFT_EQ:
13934       id = ansi_assopname (LSHIFT_EXPR);
13935       break;
13936
13937     case CPP_RSHIFT_EQ:
13938       id = ansi_assopname (RSHIFT_EXPR);
13939       break;
13940
13941     case CPP_EQ_EQ:
13942       id = ansi_opname (EQ_EXPR);
13943       break;
13944
13945     case CPP_NOT_EQ:
13946       id = ansi_opname (NE_EXPR);
13947       break;
13948
13949     case CPP_LESS_EQ:
13950       id = ansi_opname (LE_EXPR);
13951       break;
13952
13953     case CPP_GREATER_EQ:
13954       id = ansi_opname (GE_EXPR);
13955       break;
13956
13957     case CPP_AND_AND:
13958       id = ansi_opname (TRUTH_ANDIF_EXPR);
13959       break;
13960
13961     case CPP_OR_OR:
13962       id = ansi_opname (TRUTH_ORIF_EXPR);
13963       break;
13964
13965     case CPP_PLUS_PLUS:
13966       id = ansi_opname (POSTINCREMENT_EXPR);
13967       break;
13968
13969     case CPP_MINUS_MINUS:
13970       id = ansi_opname (PREDECREMENT_EXPR);
13971       break;
13972
13973     case CPP_COMMA:
13974       id = ansi_opname (COMPOUND_EXPR);
13975       break;
13976
13977     case CPP_DEREF_STAR:
13978       id = ansi_opname (MEMBER_REF);
13979       break;
13980
13981     case CPP_DEREF:
13982       id = ansi_opname (COMPONENT_REF);
13983       break;
13984
13985     case CPP_OPEN_PAREN:
13986       /* Consume the `('.  */
13987       cp_lexer_consume_token (parser->lexer);
13988       /* Look for the matching `)'.  */
13989       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13990       return ansi_opname (CALL_EXPR);
13991
13992     case CPP_OPEN_SQUARE:
13993       /* Consume the `['.  */
13994       cp_lexer_consume_token (parser->lexer);
13995       /* Look for the matching `]'.  */
13996       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13997       return ansi_opname (ARRAY_REF);
13998
13999     case CPP_UTF8STRING:
14000     case CPP_UTF8STRING_USERDEF:
14001       utf8 = true;
14002     case CPP_STRING:
14003     case CPP_WSTRING:
14004     case CPP_STRING16:
14005     case CPP_STRING32:
14006     case CPP_STRING_USERDEF:
14007     case CPP_WSTRING_USERDEF:
14008     case CPP_STRING16_USERDEF:
14009     case CPP_STRING32_USERDEF:
14010       {
14011         tree str, string_tree;
14012         int sz, len;
14013
14014         if (cxx_dialect == cxx98)
14015           maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14016
14017         /* Consume the string.  */
14018         str = cp_parser_string_literal (parser, /*translate=*/true,
14019                                       /*wide_ok=*/true, /*lookup_udlit=*/false);
14020         if (str == error_mark_node)
14021           return error_mark_node;
14022         else if (TREE_CODE (str) == USERDEF_LITERAL)
14023           {
14024             string_tree = USERDEF_LITERAL_VALUE (str);
14025             id = USERDEF_LITERAL_SUFFIX_ID (str);
14026           }
14027         else
14028           {
14029             string_tree = str;
14030             /* Look for the suffix identifier.  */
14031             token = cp_lexer_peek_token (parser->lexer);
14032             if (token->type == CPP_NAME)
14033               id = cp_parser_identifier (parser);
14034             else if (token->type == CPP_KEYWORD)
14035               {
14036                 error ("unexpected keyword;"
14037                        " remove space between quotes and suffix identifier");
14038                 return error_mark_node;
14039               }
14040             else
14041               {
14042                 error ("expected suffix identifier");
14043                 return error_mark_node;
14044               }
14045           }
14046         sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14047                                (TREE_TYPE (TREE_TYPE (string_tree))));
14048         len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14049         if (len != 0)
14050           {
14051             error ("expected empty string after %<operator%> keyword");
14052             return error_mark_node;
14053           }
14054         if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14055             != char_type_node)
14056           {
14057             error ("invalid encoding prefix in literal operator");
14058             return error_mark_node;
14059           }
14060         if (id != error_mark_node)
14061           {
14062             const char *name = IDENTIFIER_POINTER (id);
14063             id = cp_literal_operator_id (name);
14064           }
14065         return id;
14066       }
14067
14068     default:
14069       /* Anything else is an error.  */
14070       break;
14071     }
14072
14073   /* If we have selected an identifier, we need to consume the
14074      operator token.  */
14075   if (id)
14076     cp_lexer_consume_token (parser->lexer);
14077   /* Otherwise, no valid operator name was present.  */
14078   else
14079     {
14080       cp_parser_error (parser, "expected operator");
14081       id = error_mark_node;
14082     }
14083
14084   return cp_expr (id, start_loc);
14085 }
14086
14087 /* Parse a template-declaration.
14088
14089    template-declaration:
14090      export [opt] template < template-parameter-list > declaration
14091
14092    If MEMBER_P is TRUE, this template-declaration occurs within a
14093    class-specifier.
14094
14095    The grammar rule given by the standard isn't correct.  What
14096    is really meant is:
14097
14098    template-declaration:
14099      export [opt] template-parameter-list-seq
14100        decl-specifier-seq [opt] init-declarator [opt] ;
14101      export [opt] template-parameter-list-seq
14102        function-definition
14103
14104    template-parameter-list-seq:
14105      template-parameter-list-seq [opt]
14106      template < template-parameter-list >
14107
14108    Concept Extensions:
14109
14110    template-parameter-list-seq:
14111      template < template-parameter-list > requires-clause [opt]
14112
14113    requires-clause:
14114      requires logical-or-expression  */
14115
14116 static void
14117 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14118 {
14119   /* Check for `export'.  */
14120   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14121     {
14122       /* Consume the `export' token.  */
14123       cp_lexer_consume_token (parser->lexer);
14124       /* Warn that we do not support `export'.  */
14125       warning (0, "keyword %<export%> not implemented, and will be ignored");
14126     }
14127
14128   cp_parser_template_declaration_after_export (parser, member_p);
14129 }
14130
14131 /* Parse a template-parameter-list.
14132
14133    template-parameter-list:
14134      template-parameter
14135      template-parameter-list , template-parameter
14136
14137    Returns a TREE_LIST.  Each node represents a template parameter.
14138    The nodes are connected via their TREE_CHAINs.  */
14139
14140 static tree
14141 cp_parser_template_parameter_list (cp_parser* parser)
14142 {
14143   tree parameter_list = NULL_TREE;
14144
14145   begin_template_parm_list ();
14146
14147   /* The loop below parses the template parms.  We first need to know
14148      the total number of template parms to be able to compute proper
14149      canonical types of each dependent type. So after the loop, when
14150      we know the total number of template parms,
14151      end_template_parm_list computes the proper canonical types and
14152      fixes up the dependent types accordingly.  */
14153   while (true)
14154     {
14155       tree parameter;
14156       bool is_non_type;
14157       bool is_parameter_pack;
14158       location_t parm_loc;
14159
14160       /* Parse the template-parameter.  */
14161       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14162       parameter = cp_parser_template_parameter (parser, 
14163                                                 &is_non_type,
14164                                                 &is_parameter_pack);
14165       /* Add it to the list.  */
14166       if (parameter != error_mark_node)
14167         parameter_list = process_template_parm (parameter_list,
14168                                                 parm_loc,
14169                                                 parameter,
14170                                                 is_non_type,
14171                                                 is_parameter_pack);
14172       else
14173        {
14174          tree err_parm = build_tree_list (parameter, parameter);
14175          parameter_list = chainon (parameter_list, err_parm);
14176        }
14177
14178       /* If the next token is not a `,', we're done.  */
14179       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14180         break;
14181       /* Otherwise, consume the `,' token.  */
14182       cp_lexer_consume_token (parser->lexer);
14183     }
14184
14185   return end_template_parm_list (parameter_list);
14186 }
14187
14188 /* Parse a introduction-list.
14189
14190    introduction-list:
14191      introduced-parameter
14192      introduction-list , introduced-parameter
14193
14194    introduced-parameter:
14195      ...[opt] identifier
14196
14197    Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
14198    then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
14199    WILDCARD_DECL will also have DECL_NAME set and token location in
14200    DECL_SOURCE_LOCATION.  */
14201
14202 static tree
14203 cp_parser_introduction_list (cp_parser *parser)
14204 {
14205   vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14206
14207   while (true)
14208     {
14209       bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14210       if (is_pack)
14211         cp_lexer_consume_token (parser->lexer);
14212
14213       /* Build placeholder. */
14214       tree parm = build_nt (WILDCARD_DECL);
14215       DECL_SOURCE_LOCATION (parm)
14216         = cp_lexer_peek_token (parser->lexer)->location;
14217       DECL_NAME (parm) = cp_parser_identifier (parser);
14218       WILDCARD_PACK_P (parm) = is_pack;
14219       vec_safe_push (introduction_vec, parm);
14220
14221       /* If the next token is not a `,', we're done.  */
14222       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14223         break;
14224       /* Otherwise, consume the `,' token.  */
14225       cp_lexer_consume_token (parser->lexer);
14226     }
14227
14228   /* Convert the vec into a TREE_VEC.  */
14229   tree introduction_list = make_tree_vec (introduction_vec->length ());
14230   unsigned int n;
14231   tree parm;
14232   FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14233     TREE_VEC_ELT (introduction_list, n) = parm;
14234
14235   release_tree_vector (introduction_vec);
14236   return introduction_list;
14237 }
14238
14239 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14240    is an abstract declarator. */
14241
14242 static inline cp_declarator*
14243 get_id_declarator (cp_declarator *declarator)
14244 {
14245   cp_declarator *d = declarator;
14246   while (d && d->kind != cdk_id)
14247     d = d->declarator;
14248   return d;
14249 }
14250
14251 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14252    is an abstract declarator. */
14253
14254 static inline tree
14255 get_unqualified_id (cp_declarator *declarator)
14256 {
14257   declarator = get_id_declarator (declarator);
14258   if (declarator)
14259     return declarator->u.id.unqualified_name;
14260   else
14261     return NULL_TREE;
14262 }
14263
14264 /* Returns true if DECL represents a constrained-parameter.  */
14265
14266 static inline bool
14267 is_constrained_parameter (tree decl)
14268 {
14269   return (decl
14270           && TREE_CODE (decl) == TYPE_DECL
14271           && CONSTRAINED_PARM_CONCEPT (decl)
14272           && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14273 }
14274
14275 /* Returns true if PARM declares a constrained-parameter. */
14276
14277 static inline bool
14278 is_constrained_parameter (cp_parameter_declarator *parm)
14279 {
14280   return is_constrained_parameter (parm->decl_specifiers.type);
14281 }
14282
14283 /* Check that the type parameter is only a declarator-id, and that its
14284    type is not cv-qualified. */
14285
14286 bool
14287 cp_parser_check_constrained_type_parm (cp_parser *parser,
14288                                        cp_parameter_declarator *parm)
14289 {
14290   if (!parm->declarator)
14291     return true;
14292
14293   if (parm->declarator->kind != cdk_id)
14294     {
14295       cp_parser_error (parser, "invalid constrained type parameter");
14296       return false;
14297     }
14298
14299   /* Don't allow cv-qualified type parameters.  */
14300   if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14301       || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14302     {
14303       cp_parser_error (parser, "cv-qualified type parameter");
14304       return false;
14305     }
14306
14307   return true;
14308 }
14309
14310 /* Finish parsing/processing a template type parameter and checking
14311    various restrictions. */
14312
14313 static inline tree
14314 cp_parser_constrained_type_template_parm (cp_parser *parser,
14315                                           tree id,
14316                                           cp_parameter_declarator* parmdecl)
14317 {
14318   if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14319     return finish_template_type_parm (class_type_node, id);
14320   else
14321     return error_mark_node;
14322 }
14323
14324 static tree
14325 finish_constrained_template_template_parm (tree proto, tree id)
14326 {
14327   /* FIXME: This should probably be copied, and we may need to adjust
14328      the template parameter depths.  */
14329   tree saved_parms = current_template_parms;
14330   begin_template_parm_list ();
14331   current_template_parms = DECL_TEMPLATE_PARMS (proto);
14332   end_template_parm_list ();
14333
14334   tree parm = finish_template_template_parm (class_type_node, id);
14335   current_template_parms = saved_parms;
14336
14337   return parm;
14338 }
14339
14340 /* Finish parsing/processing a template template parameter by borrowing
14341    the template parameter list from the prototype parameter.  */
14342
14343 static tree
14344 cp_parser_constrained_template_template_parm (cp_parser *parser,
14345                                               tree proto,
14346                                               tree id,
14347                                               cp_parameter_declarator *parmdecl)
14348 {
14349   if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14350     return error_mark_node;
14351   return finish_constrained_template_template_parm (proto, id);
14352 }
14353
14354 /* Create a new non-type template parameter from the given PARM
14355    declarator.  */
14356
14357 static tree
14358 constrained_non_type_template_parm (bool *is_non_type,
14359                                     cp_parameter_declarator *parm)
14360 {
14361   *is_non_type = true;
14362   cp_declarator *decl = parm->declarator;
14363   cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14364   specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14365   return grokdeclarator (decl, specs, TPARM, 0, NULL);
14366 }
14367
14368 /* Build a constrained template parameter based on the PARMDECL
14369    declarator. The type of PARMDECL is the constrained type, which
14370    refers to the prototype template parameter that ultimately
14371    specifies the type of the declared parameter. */
14372
14373 static tree
14374 finish_constrained_parameter (cp_parser *parser,
14375                               cp_parameter_declarator *parmdecl,
14376                               bool *is_non_type,
14377                               bool *is_parameter_pack)
14378 {
14379   tree decl = parmdecl->decl_specifiers.type;
14380   tree id = get_unqualified_id (parmdecl->declarator);
14381   tree def = parmdecl->default_argument;
14382   tree proto = DECL_INITIAL (decl);
14383
14384   /* A template parameter constrained by a variadic concept shall also
14385      be declared as a template parameter pack.  */
14386   bool is_variadic = template_parameter_pack_p (proto);
14387   if (is_variadic && !*is_parameter_pack)
14388     cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14389
14390   /* Build the parameter. Return an error if the declarator was invalid. */
14391   tree parm;
14392   if (TREE_CODE (proto) == TYPE_DECL)
14393     parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14394   else if (TREE_CODE (proto) == TEMPLATE_DECL)
14395     parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14396                                                          parmdecl);
14397   else
14398     parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14399   if (parm == error_mark_node)
14400     return error_mark_node;
14401
14402   /* Finish the parameter decl and create a node attaching the
14403      default argument and constraint.  */
14404   parm = build_tree_list (def, parm);
14405   TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14406
14407   return parm;
14408 }
14409
14410 /* Returns true if the parsed type actually represents the declaration
14411    of a type template-parameter.  */
14412
14413 static inline bool
14414 declares_constrained_type_template_parameter (tree type)
14415 {
14416   return (is_constrained_parameter (type)
14417           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14418 }
14419
14420
14421 /* Returns true if the parsed type actually represents the declaration of
14422    a template template-parameter.  */
14423
14424 static bool
14425 declares_constrained_template_template_parameter (tree type)
14426 {
14427   return (is_constrained_parameter (type)
14428           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14429 }
14430
14431 /* Parse a default argument for a type template-parameter.
14432    Note that diagnostics are handled in cp_parser_template_parameter.  */
14433
14434 static tree
14435 cp_parser_default_type_template_argument (cp_parser *parser)
14436 {
14437   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14438
14439   /* Consume the `=' token.  */
14440   cp_lexer_consume_token (parser->lexer);
14441
14442   cp_token *token = cp_lexer_peek_token (parser->lexer);
14443
14444   /* Parse the default-argument.  */
14445   push_deferring_access_checks (dk_no_deferred);
14446   tree default_argument = cp_parser_type_id (parser);
14447   pop_deferring_access_checks ();
14448
14449   if (flag_concepts && type_uses_auto (default_argument))
14450     {
14451       error_at (token->location,
14452                 "invalid use of %<auto%> in default template argument");
14453       return error_mark_node;
14454     }
14455
14456   return default_argument;
14457 }
14458
14459 /* Parse a default argument for a template template-parameter.  */
14460
14461 static tree
14462 cp_parser_default_template_template_argument (cp_parser *parser)
14463 {
14464   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14465
14466   bool is_template;
14467
14468   /* Consume the `='.  */
14469   cp_lexer_consume_token (parser->lexer);
14470   /* Parse the id-expression.  */
14471   push_deferring_access_checks (dk_no_deferred);
14472   /* save token before parsing the id-expression, for error
14473      reporting */
14474   const cp_token* token = cp_lexer_peek_token (parser->lexer);
14475   tree default_argument
14476     = cp_parser_id_expression (parser,
14477                                /*template_keyword_p=*/false,
14478                                /*check_dependency_p=*/true,
14479                                /*template_p=*/&is_template,
14480                                /*declarator_p=*/false,
14481                                /*optional_p=*/false);
14482   if (TREE_CODE (default_argument) == TYPE_DECL)
14483     /* If the id-expression was a template-id that refers to
14484        a template-class, we already have the declaration here,
14485        so no further lookup is needed.  */
14486     ;
14487   else
14488     /* Look up the name.  */
14489     default_argument
14490       = cp_parser_lookup_name (parser, default_argument,
14491                                none_type,
14492                                /*is_template=*/is_template,
14493                                /*is_namespace=*/false,
14494                                /*check_dependency=*/true,
14495                                /*ambiguous_decls=*/NULL,
14496                                token->location);
14497   /* See if the default argument is valid.  */
14498   default_argument = check_template_template_default_arg (default_argument);
14499   pop_deferring_access_checks ();
14500   return default_argument;
14501 }
14502
14503 /* Parse a template-parameter.
14504
14505    template-parameter:
14506      type-parameter
14507      parameter-declaration
14508
14509    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
14510    the parameter.  The TREE_PURPOSE is the default value, if any.
14511    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
14512    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
14513    set to true iff this parameter is a parameter pack. */
14514
14515 static tree
14516 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14517                               bool *is_parameter_pack)
14518 {
14519   cp_token *token;
14520   cp_parameter_declarator *parameter_declarator;
14521   tree parm;
14522
14523   /* Assume it is a type parameter or a template parameter.  */
14524   *is_non_type = false;
14525   /* Assume it not a parameter pack. */
14526   *is_parameter_pack = false;
14527   /* Peek at the next token.  */
14528   token = cp_lexer_peek_token (parser->lexer);
14529   /* If it is `class' or `template', we have a type-parameter.  */
14530   if (token->keyword == RID_TEMPLATE)
14531     return cp_parser_type_parameter (parser, is_parameter_pack);
14532   /* If it is `class' or `typename' we do not know yet whether it is a
14533      type parameter or a non-type parameter.  Consider:
14534
14535        template <typename T, typename T::X X> ...
14536
14537      or:
14538
14539        template <class C, class D*> ...
14540
14541      Here, the first parameter is a type parameter, and the second is
14542      a non-type parameter.  We can tell by looking at the token after
14543      the identifier -- if it is a `,', `=', or `>' then we have a type
14544      parameter.  */
14545   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14546     {
14547       /* Peek at the token after `class' or `typename'.  */
14548       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14549       /* If it's an ellipsis, we have a template type parameter
14550          pack. */
14551       if (token->type == CPP_ELLIPSIS)
14552         return cp_parser_type_parameter (parser, is_parameter_pack);
14553       /* If it's an identifier, skip it.  */
14554       if (token->type == CPP_NAME)
14555         token = cp_lexer_peek_nth_token (parser->lexer, 3);
14556       /* Now, see if the token looks like the end of a template
14557          parameter.  */
14558       if (token->type == CPP_COMMA
14559           || token->type == CPP_EQ
14560           || token->type == CPP_GREATER)
14561         return cp_parser_type_parameter (parser, is_parameter_pack);
14562     }
14563
14564   /* Otherwise, it is a non-type parameter or a constrained parameter.
14565
14566      [temp.param]
14567
14568      When parsing a default template-argument for a non-type
14569      template-parameter, the first non-nested `>' is taken as the end
14570      of the template parameter-list rather than a greater-than
14571      operator.  */
14572   parameter_declarator
14573      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14574                                         /*parenthesized_p=*/NULL);
14575
14576   if (!parameter_declarator)
14577     return error_mark_node;
14578
14579   /* If the parameter declaration is marked as a parameter pack, set
14580    *IS_PARAMETER_PACK to notify the caller.  */
14581   if (parameter_declarator->template_parameter_pack_p)
14582     *is_parameter_pack = true;
14583
14584   if (parameter_declarator->default_argument)
14585     {
14586       /* Can happen in some cases of erroneous input (c++/34892).  */
14587       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14588         /* Consume the `...' for better error recovery.  */
14589         cp_lexer_consume_token (parser->lexer);
14590     }
14591
14592   // The parameter may have been constrained.
14593   if (is_constrained_parameter (parameter_declarator))
14594     return finish_constrained_parameter (parser,
14595                                          parameter_declarator,
14596                                          is_non_type,
14597                                          is_parameter_pack);
14598
14599   // Now we're sure that the parameter is a non-type parameter.
14600   *is_non_type = true;
14601
14602   parm = grokdeclarator (parameter_declarator->declarator,
14603                          &parameter_declarator->decl_specifiers,
14604                          TPARM, /*initialized=*/0,
14605                          /*attrlist=*/NULL);
14606   if (parm == error_mark_node)
14607     return error_mark_node;
14608
14609   return build_tree_list (parameter_declarator->default_argument, parm);
14610 }
14611
14612 /* Parse a type-parameter.
14613
14614    type-parameter:
14615      class identifier [opt]
14616      class identifier [opt] = type-id
14617      typename identifier [opt]
14618      typename identifier [opt] = type-id
14619      template < template-parameter-list > class identifier [opt]
14620      template < template-parameter-list > class identifier [opt]
14621        = id-expression
14622
14623    GNU Extension (variadic templates):
14624
14625    type-parameter:
14626      class ... identifier [opt]
14627      typename ... identifier [opt]
14628
14629    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
14630    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
14631    the declaration of the parameter.
14632
14633    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14634
14635 static tree
14636 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14637 {
14638   cp_token *token;
14639   tree parameter;
14640
14641   /* Look for a keyword to tell us what kind of parameter this is.  */
14642   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14643   if (!token)
14644     return error_mark_node;
14645
14646   switch (token->keyword)
14647     {
14648     case RID_CLASS:
14649     case RID_TYPENAME:
14650       {
14651         tree identifier;
14652         tree default_argument;
14653
14654         /* If the next token is an ellipsis, we have a template
14655            argument pack. */
14656         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14657           {
14658             /* Consume the `...' token. */
14659             cp_lexer_consume_token (parser->lexer);
14660             maybe_warn_variadic_templates ();
14661
14662             *is_parameter_pack = true;
14663           }
14664
14665         /* If the next token is an identifier, then it names the
14666            parameter.  */
14667         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14668           identifier = cp_parser_identifier (parser);
14669         else
14670           identifier = NULL_TREE;
14671
14672         /* Create the parameter.  */
14673         parameter = finish_template_type_parm (class_type_node, identifier);
14674
14675         /* If the next token is an `=', we have a default argument.  */
14676         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14677           {
14678             default_argument
14679               = cp_parser_default_type_template_argument (parser);
14680
14681             /* Template parameter packs cannot have default
14682                arguments. */
14683             if (*is_parameter_pack)
14684               {
14685                 if (identifier)
14686                   error_at (token->location,
14687                             "template parameter pack %qD cannot have a "
14688                             "default argument", identifier);
14689                 else
14690                   error_at (token->location,
14691                             "template parameter packs cannot have "
14692                             "default arguments");
14693                 default_argument = NULL_TREE;
14694               }
14695             else if (check_for_bare_parameter_packs (default_argument))
14696               default_argument = error_mark_node;
14697           }
14698         else
14699           default_argument = NULL_TREE;
14700
14701         /* Create the combined representation of the parameter and the
14702            default argument.  */
14703         parameter = build_tree_list (default_argument, parameter);
14704       }
14705       break;
14706
14707     case RID_TEMPLATE:
14708       {
14709         tree identifier;
14710         tree default_argument;
14711
14712         /* Look for the `<'.  */
14713         cp_parser_require (parser, CPP_LESS, RT_LESS);
14714         /* Parse the template-parameter-list.  */
14715         cp_parser_template_parameter_list (parser);
14716         /* Look for the `>'.  */
14717         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14718
14719         // If template requirements are present, parse them.
14720         if (flag_concepts)
14721           {
14722             tree reqs = get_shorthand_constraints (current_template_parms);
14723             if (tree r = cp_parser_requires_clause_opt (parser))
14724               reqs = conjoin_constraints (reqs, normalize_expression (r));
14725             TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14726           }
14727
14728         /* Look for the `class' or 'typename' keywords.  */
14729         cp_parser_type_parameter_key (parser);
14730         /* If the next token is an ellipsis, we have a template
14731            argument pack. */
14732         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14733           {
14734             /* Consume the `...' token. */
14735             cp_lexer_consume_token (parser->lexer);
14736             maybe_warn_variadic_templates ();
14737
14738             *is_parameter_pack = true;
14739           }
14740         /* If the next token is an `=', then there is a
14741            default-argument.  If the next token is a `>', we are at
14742            the end of the parameter-list.  If the next token is a `,',
14743            then we are at the end of this parameter.  */
14744         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14745             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14746             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14747           {
14748             identifier = cp_parser_identifier (parser);
14749             /* Treat invalid names as if the parameter were nameless.  */
14750             if (identifier == error_mark_node)
14751               identifier = NULL_TREE;
14752           }
14753         else
14754           identifier = NULL_TREE;
14755
14756         /* Create the template parameter.  */
14757         parameter = finish_template_template_parm (class_type_node,
14758                                                    identifier);
14759
14760         /* If the next token is an `=', then there is a
14761            default-argument.  */
14762         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14763           {
14764             default_argument
14765               = cp_parser_default_template_template_argument (parser);
14766
14767             /* Template parameter packs cannot have default
14768                arguments. */
14769             if (*is_parameter_pack)
14770               {
14771                 if (identifier)
14772                   error_at (token->location,
14773                             "template parameter pack %qD cannot "
14774                             "have a default argument",
14775                             identifier);
14776                 else
14777                   error_at (token->location, "template parameter packs cannot "
14778                             "have default arguments");
14779                 default_argument = NULL_TREE;
14780               }
14781           }
14782         else
14783           default_argument = NULL_TREE;
14784
14785         /* Create the combined representation of the parameter and the
14786            default argument.  */
14787         parameter = build_tree_list (default_argument, parameter);
14788       }
14789       break;
14790
14791     default:
14792       gcc_unreachable ();
14793       break;
14794     }
14795
14796   return parameter;
14797 }
14798
14799 /* Parse a template-id.
14800
14801    template-id:
14802      template-name < template-argument-list [opt] >
14803
14804    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14805    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
14806    returned.  Otherwise, if the template-name names a function, or set
14807    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
14808    names a class, returns a TYPE_DECL for the specialization.
14809
14810    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14811    uninstantiated templates.  */
14812
14813 static tree
14814 cp_parser_template_id (cp_parser *parser,
14815                        bool template_keyword_p,
14816                        bool check_dependency_p,
14817                        enum tag_types tag_type,
14818                        bool is_declaration)
14819 {
14820   tree templ;
14821   tree arguments;
14822   tree template_id;
14823   cp_token_position start_of_id = 0;
14824   cp_token *next_token = NULL, *next_token_2 = NULL;
14825   bool is_identifier;
14826
14827   /* If the next token corresponds to a template-id, there is no need
14828      to reparse it.  */
14829   next_token = cp_lexer_peek_token (parser->lexer);
14830   if (next_token->type == CPP_TEMPLATE_ID)
14831     {
14832       cp_lexer_consume_token (parser->lexer);
14833       return saved_checks_value (next_token->u.tree_check_value);
14834     }
14835
14836   /* Avoid performing name lookup if there is no possibility of
14837      finding a template-id.  */
14838   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14839       || (next_token->type == CPP_NAME
14840           && !cp_parser_nth_token_starts_template_argument_list_p
14841                (parser, 2)))
14842     {
14843       cp_parser_error (parser, "expected template-id");
14844       return error_mark_node;
14845     }
14846
14847   /* Remember where the template-id starts.  */
14848   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14849     start_of_id = cp_lexer_token_position (parser->lexer, false);
14850
14851   push_deferring_access_checks (dk_deferred);
14852
14853   /* Parse the template-name.  */
14854   is_identifier = false;
14855   templ = cp_parser_template_name (parser, template_keyword_p,
14856                                    check_dependency_p,
14857                                    is_declaration,
14858                                    tag_type,
14859                                    &is_identifier);
14860   if (templ == error_mark_node || is_identifier)
14861     {
14862       pop_deferring_access_checks ();
14863       return templ;
14864     }
14865
14866   /* Since we're going to preserve any side-effects from this parse, set up a
14867      firewall to protect our callers from cp_parser_commit_to_tentative_parse
14868      in the template arguments.  */
14869   tentative_firewall firewall (parser);
14870
14871   /* If we find the sequence `[:' after a template-name, it's probably
14872      a digraph-typo for `< ::'. Substitute the tokens and check if we can
14873      parse correctly the argument list.  */
14874   next_token = cp_lexer_peek_token (parser->lexer);
14875   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14876   if (next_token->type == CPP_OPEN_SQUARE
14877       && next_token->flags & DIGRAPH
14878       && next_token_2->type == CPP_COLON
14879       && !(next_token_2->flags & PREV_WHITE))
14880     {
14881       cp_parser_parse_tentatively (parser);
14882       /* Change `:' into `::'.  */
14883       next_token_2->type = CPP_SCOPE;
14884       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14885          CPP_LESS.  */
14886       cp_lexer_consume_token (parser->lexer);
14887
14888       /* Parse the arguments.  */
14889       arguments = cp_parser_enclosed_template_argument_list (parser);
14890       if (!cp_parser_parse_definitely (parser))
14891         {
14892           /* If we couldn't parse an argument list, then we revert our changes
14893              and return simply an error. Maybe this is not a template-id
14894              after all.  */
14895           next_token_2->type = CPP_COLON;
14896           cp_parser_error (parser, "expected %<<%>");
14897           pop_deferring_access_checks ();
14898           return error_mark_node;
14899         }
14900       /* Otherwise, emit an error about the invalid digraph, but continue
14901          parsing because we got our argument list.  */
14902       if (permerror (next_token->location,
14903                      "%<<::%> cannot begin a template-argument list"))
14904         {
14905           static bool hint = false;
14906           inform (next_token->location,
14907                   "%<<:%> is an alternate spelling for %<[%>."
14908                   " Insert whitespace between %<<%> and %<::%>");
14909           if (!hint && !flag_permissive)
14910             {
14911               inform (next_token->location, "(if you use %<-fpermissive%> "
14912                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14913                       "accept your code)");
14914               hint = true;
14915             }
14916         }
14917     }
14918   else
14919     {
14920       /* Look for the `<' that starts the template-argument-list.  */
14921       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14922         {
14923           pop_deferring_access_checks ();
14924           return error_mark_node;
14925         }
14926       /* Parse the arguments.  */
14927       arguments = cp_parser_enclosed_template_argument_list (parser);
14928     }
14929
14930   /* Build a representation of the specialization.  */
14931   if (identifier_p (templ))
14932     template_id = build_min_nt_loc (next_token->location,
14933                                     TEMPLATE_ID_EXPR,
14934                                     templ, arguments);
14935   else if (DECL_TYPE_TEMPLATE_P (templ)
14936            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14937     {
14938       bool entering_scope;
14939       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14940          template (rather than some instantiation thereof) only if
14941          is not nested within some other construct.  For example, in
14942          "template <typename T> void f(T) { A<T>::", A<T> is just an
14943          instantiation of A.  */
14944       entering_scope = (template_parm_scope_p ()
14945                         && cp_lexer_next_token_is (parser->lexer,
14946                                                    CPP_SCOPE));
14947       template_id
14948         = finish_template_type (templ, arguments, entering_scope);
14949     }
14950   /* A template-like identifier may be a partial concept id. */
14951   else if (flag_concepts
14952            && (template_id = (cp_parser_maybe_partial_concept_id
14953                               (parser, templ, arguments))))
14954     return template_id;
14955   else if (variable_template_p (templ))
14956     {
14957       template_id = lookup_template_variable (templ, arguments);
14958       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14959         SET_EXPR_LOCATION (template_id, next_token->location);
14960     }
14961   else
14962     {
14963       /* If it's not a class-template or a template-template, it should be
14964          a function-template.  */
14965       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14966                    || TREE_CODE (templ) == OVERLOAD
14967                    || BASELINK_P (templ)));
14968
14969       template_id = lookup_template_function (templ, arguments);
14970       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14971         SET_EXPR_LOCATION (template_id, next_token->location);
14972     }
14973
14974   /* If parsing tentatively, replace the sequence of tokens that makes
14975      up the template-id with a CPP_TEMPLATE_ID token.  That way,
14976      should we re-parse the token stream, we will not have to repeat
14977      the effort required to do the parse, nor will we issue duplicate
14978      error messages about problems during instantiation of the
14979      template.  */
14980   if (start_of_id
14981       /* Don't do this if we had a parse error in a declarator; re-parsing
14982          might succeed if a name changes meaning (60361).  */
14983       && !(cp_parser_error_occurred (parser)
14984            && cp_parser_parsing_tentatively (parser)
14985            && parser->in_declarator_p))
14986     {
14987       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14988
14989       /* Reset the contents of the START_OF_ID token.  */
14990       token->type = CPP_TEMPLATE_ID;
14991
14992       /* Update the location to be of the form:
14993            template-name < template-argument-list [opt] >
14994            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14995          with caret == start at the start of the template-name,
14996          ranging until the closing '>'.  */
14997       location_t finish_loc
14998         = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14999       location_t combined_loc
15000         = make_location (token->location, token->location, finish_loc);
15001       token->location = combined_loc;
15002
15003       /* Retrieve any deferred checks.  Do not pop this access checks yet
15004          so the memory will not be reclaimed during token replacing below.  */
15005       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15006       token->u.tree_check_value->value = template_id;
15007       token->u.tree_check_value->checks = get_deferred_access_checks ();
15008       token->keyword = RID_MAX;
15009
15010       /* Purge all subsequent tokens.  */
15011       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15012
15013       /* ??? Can we actually assume that, if template_id ==
15014          error_mark_node, we will have issued a diagnostic to the
15015          user, as opposed to simply marking the tentative parse as
15016          failed?  */
15017       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15018         error_at (token->location, "parse error in template argument list");
15019     }
15020
15021   pop_to_parent_deferring_access_checks ();
15022   return template_id;
15023 }
15024
15025 /* Parse a template-name.
15026
15027    template-name:
15028      identifier
15029
15030    The standard should actually say:
15031
15032    template-name:
15033      identifier
15034      operator-function-id
15035
15036    A defect report has been filed about this issue.
15037
15038    A conversion-function-id cannot be a template name because they cannot
15039    be part of a template-id. In fact, looking at this code:
15040
15041    a.operator K<int>()
15042
15043    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15044    It is impossible to call a templated conversion-function-id with an
15045    explicit argument list, since the only allowed template parameter is
15046    the type to which it is converting.
15047
15048    If TEMPLATE_KEYWORD_P is true, then we have just seen the
15049    `template' keyword, in a construction like:
15050
15051      T::template f<3>()
15052
15053    In that case `f' is taken to be a template-name, even though there
15054    is no way of knowing for sure.
15055
15056    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15057    name refers to a set of overloaded functions, at least one of which
15058    is a template, or an IDENTIFIER_NODE with the name of the template,
15059    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
15060    names are looked up inside uninstantiated templates.  */
15061
15062 static tree
15063 cp_parser_template_name (cp_parser* parser,
15064                          bool template_keyword_p,
15065                          bool check_dependency_p,
15066                          bool is_declaration,
15067                          enum tag_types tag_type,
15068                          bool *is_identifier)
15069 {
15070   tree identifier;
15071   tree decl;
15072   tree fns;
15073   cp_token *token = cp_lexer_peek_token (parser->lexer);
15074
15075   /* If the next token is `operator', then we have either an
15076      operator-function-id or a conversion-function-id.  */
15077   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15078     {
15079       /* We don't know whether we're looking at an
15080          operator-function-id or a conversion-function-id.  */
15081       cp_parser_parse_tentatively (parser);
15082       /* Try an operator-function-id.  */
15083       identifier = cp_parser_operator_function_id (parser);
15084       /* If that didn't work, try a conversion-function-id.  */
15085       if (!cp_parser_parse_definitely (parser))
15086         {
15087           cp_parser_error (parser, "expected template-name");
15088           return error_mark_node;
15089         }
15090     }
15091   /* Look for the identifier.  */
15092   else
15093     identifier = cp_parser_identifier (parser);
15094
15095   /* If we didn't find an identifier, we don't have a template-id.  */
15096   if (identifier == error_mark_node)
15097     return error_mark_node;
15098
15099   /* If the name immediately followed the `template' keyword, then it
15100      is a template-name.  However, if the next token is not `<', then
15101      we do not treat it as a template-name, since it is not being used
15102      as part of a template-id.  This enables us to handle constructs
15103      like:
15104
15105        template <typename T> struct S { S(); };
15106        template <typename T> S<T>::S();
15107
15108      correctly.  We would treat `S' as a template -- if it were `S<T>'
15109      -- but we do not if there is no `<'.  */
15110
15111   if (processing_template_decl
15112       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15113     {
15114       /* In a declaration, in a dependent context, we pretend that the
15115          "template" keyword was present in order to improve error
15116          recovery.  For example, given:
15117
15118            template <typename T> void f(T::X<int>);
15119
15120          we want to treat "X<int>" as a template-id.  */
15121       if (is_declaration
15122           && !template_keyword_p
15123           && parser->scope && TYPE_P (parser->scope)
15124           && check_dependency_p
15125           && dependent_scope_p (parser->scope)
15126           /* Do not do this for dtors (or ctors), since they never
15127              need the template keyword before their name.  */
15128           && !constructor_name_p (identifier, parser->scope))
15129         {
15130           cp_token_position start = 0;
15131
15132           /* Explain what went wrong.  */
15133           error_at (token->location, "non-template %qD used as template",
15134                     identifier);
15135           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15136                   parser->scope, identifier);
15137           /* If parsing tentatively, find the location of the "<" token.  */
15138           if (cp_parser_simulate_error (parser))
15139             start = cp_lexer_token_position (parser->lexer, true);
15140           /* Parse the template arguments so that we can issue error
15141              messages about them.  */
15142           cp_lexer_consume_token (parser->lexer);
15143           cp_parser_enclosed_template_argument_list (parser);
15144           /* Skip tokens until we find a good place from which to
15145              continue parsing.  */
15146           cp_parser_skip_to_closing_parenthesis (parser,
15147                                                  /*recovering=*/true,
15148                                                  /*or_comma=*/true,
15149                                                  /*consume_paren=*/false);
15150           /* If parsing tentatively, permanently remove the
15151              template argument list.  That will prevent duplicate
15152              error messages from being issued about the missing
15153              "template" keyword.  */
15154           if (start)
15155             cp_lexer_purge_tokens_after (parser->lexer, start);
15156           if (is_identifier)
15157             *is_identifier = true;
15158           return identifier;
15159         }
15160
15161       /* If the "template" keyword is present, then there is generally
15162          no point in doing name-lookup, so we just return IDENTIFIER.
15163          But, if the qualifying scope is non-dependent then we can
15164          (and must) do name-lookup normally.  */
15165       if (template_keyword_p
15166           && (!parser->scope
15167               || (TYPE_P (parser->scope)
15168                   && dependent_type_p (parser->scope))))
15169         return identifier;
15170     }
15171
15172   /* Look up the name.  */
15173   decl = cp_parser_lookup_name (parser, identifier,
15174                                 tag_type,
15175                                 /*is_template=*/true,
15176                                 /*is_namespace=*/false,
15177                                 check_dependency_p,
15178                                 /*ambiguous_decls=*/NULL,
15179                                 token->location);
15180
15181   decl = strip_using_decl (decl);
15182
15183   /* If DECL is a template, then the name was a template-name.  */
15184   if (TREE_CODE (decl) == TEMPLATE_DECL)
15185     {
15186       if (TREE_DEPRECATED (decl)
15187           && deprecated_state != DEPRECATED_SUPPRESS)
15188         warn_deprecated_use (decl, NULL_TREE);
15189     }
15190   else
15191     {
15192       tree fn = NULL_TREE;
15193
15194       /* The standard does not explicitly indicate whether a name that
15195          names a set of overloaded declarations, some of which are
15196          templates, is a template-name.  However, such a name should
15197          be a template-name; otherwise, there is no way to form a
15198          template-id for the overloaded templates.  */
15199       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15200       if (TREE_CODE (fns) == OVERLOAD)
15201         for (fn = fns; fn; fn = OVL_NEXT (fn))
15202           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15203             break;
15204
15205       if (!fn)
15206         {
15207           /* The name does not name a template.  */
15208           cp_parser_error (parser, "expected template-name");
15209           return error_mark_node;
15210         }
15211     }
15212
15213   /* If DECL is dependent, and refers to a function, then just return
15214      its name; we will look it up again during template instantiation.  */
15215   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15216     {
15217       tree scope = ovl_scope (decl);
15218       if (TYPE_P (scope) && dependent_type_p (scope))
15219         return identifier;
15220     }
15221
15222   return decl;
15223 }
15224
15225 /* Parse a template-argument-list.
15226
15227    template-argument-list:
15228      template-argument ... [opt]
15229      template-argument-list , template-argument ... [opt]
15230
15231    Returns a TREE_VEC containing the arguments.  */
15232
15233 static tree
15234 cp_parser_template_argument_list (cp_parser* parser)
15235 {
15236   tree fixed_args[10];
15237   unsigned n_args = 0;
15238   unsigned alloced = 10;
15239   tree *arg_ary = fixed_args;
15240   tree vec;
15241   bool saved_in_template_argument_list_p;
15242   bool saved_ice_p;
15243   bool saved_non_ice_p;
15244
15245   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15246   parser->in_template_argument_list_p = true;
15247   /* Even if the template-id appears in an integral
15248      constant-expression, the contents of the argument list do
15249      not.  */
15250   saved_ice_p = parser->integral_constant_expression_p;
15251   parser->integral_constant_expression_p = false;
15252   saved_non_ice_p = parser->non_integral_constant_expression_p;
15253   parser->non_integral_constant_expression_p = false;
15254
15255   /* Parse the arguments.  */
15256   do
15257     {
15258       tree argument;
15259
15260       if (n_args)
15261         /* Consume the comma.  */
15262         cp_lexer_consume_token (parser->lexer);
15263
15264       /* Parse the template-argument.  */
15265       argument = cp_parser_template_argument (parser);
15266
15267       /* If the next token is an ellipsis, we're expanding a template
15268          argument pack. */
15269       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15270         {
15271           if (argument == error_mark_node)
15272             {
15273               cp_token *token = cp_lexer_peek_token (parser->lexer);
15274               error_at (token->location,
15275                         "expected parameter pack before %<...%>");
15276             }
15277           /* Consume the `...' token. */
15278           cp_lexer_consume_token (parser->lexer);
15279
15280           /* Make the argument into a TYPE_PACK_EXPANSION or
15281              EXPR_PACK_EXPANSION. */
15282           argument = make_pack_expansion (argument);
15283         }
15284
15285       if (n_args == alloced)
15286         {
15287           alloced *= 2;
15288
15289           if (arg_ary == fixed_args)
15290             {
15291               arg_ary = XNEWVEC (tree, alloced);
15292               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15293             }
15294           else
15295             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15296         }
15297       arg_ary[n_args++] = argument;
15298     }
15299   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15300
15301   vec = make_tree_vec (n_args);
15302
15303   while (n_args--)
15304     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15305
15306   if (arg_ary != fixed_args)
15307     free (arg_ary);
15308   parser->non_integral_constant_expression_p = saved_non_ice_p;
15309   parser->integral_constant_expression_p = saved_ice_p;
15310   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15311   if (CHECKING_P)
15312     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15313   return vec;
15314 }
15315
15316 /* Parse a template-argument.
15317
15318    template-argument:
15319      assignment-expression
15320      type-id
15321      id-expression
15322
15323    The representation is that of an assignment-expression, type-id, or
15324    id-expression -- except that the qualified id-expression is
15325    evaluated, so that the value returned is either a DECL or an
15326    OVERLOAD.
15327
15328    Although the standard says "assignment-expression", it forbids
15329    throw-expressions or assignments in the template argument.
15330    Therefore, we use "conditional-expression" instead.  */
15331
15332 static tree
15333 cp_parser_template_argument (cp_parser* parser)
15334 {
15335   tree argument;
15336   bool template_p;
15337   bool address_p;
15338   bool maybe_type_id = false;
15339   cp_token *token = NULL, *argument_start_token = NULL;
15340   location_t loc = 0;
15341   cp_id_kind idk;
15342
15343   /* There's really no way to know what we're looking at, so we just
15344      try each alternative in order.
15345
15346        [temp.arg]
15347
15348        In a template-argument, an ambiguity between a type-id and an
15349        expression is resolved to a type-id, regardless of the form of
15350        the corresponding template-parameter.
15351
15352      Therefore, we try a type-id first.  */
15353   cp_parser_parse_tentatively (parser);
15354   argument = cp_parser_template_type_arg (parser);
15355   /* If there was no error parsing the type-id but the next token is a
15356      '>>', our behavior depends on which dialect of C++ we're
15357      parsing. In C++98, we probably found a typo for '> >'. But there
15358      are type-id which are also valid expressions. For instance:
15359
15360      struct X { int operator >> (int); };
15361      template <int V> struct Foo {};
15362      Foo<X () >> 5> r;
15363
15364      Here 'X()' is a valid type-id of a function type, but the user just
15365      wanted to write the expression "X() >> 5". Thus, we remember that we
15366      found a valid type-id, but we still try to parse the argument as an
15367      expression to see what happens. 
15368
15369      In C++0x, the '>>' will be considered two separate '>'
15370      tokens.  */
15371   if (!cp_parser_error_occurred (parser)
15372       && cxx_dialect == cxx98
15373       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15374     {
15375       maybe_type_id = true;
15376       cp_parser_abort_tentative_parse (parser);
15377     }
15378   else
15379     {
15380       /* If the next token isn't a `,' or a `>', then this argument wasn't
15381       really finished. This means that the argument is not a valid
15382       type-id.  */
15383       if (!cp_parser_next_token_ends_template_argument_p (parser))
15384         cp_parser_error (parser, "expected template-argument");
15385       /* If that worked, we're done.  */
15386       if (cp_parser_parse_definitely (parser))
15387         return argument;
15388     }
15389   /* We're still not sure what the argument will be.  */
15390   cp_parser_parse_tentatively (parser);
15391   /* Try a template.  */
15392   argument_start_token = cp_lexer_peek_token (parser->lexer);
15393   argument = cp_parser_id_expression (parser,
15394                                       /*template_keyword_p=*/false,
15395                                       /*check_dependency_p=*/true,
15396                                       &template_p,
15397                                       /*declarator_p=*/false,
15398                                       /*optional_p=*/false);
15399   /* If the next token isn't a `,' or a `>', then this argument wasn't
15400      really finished.  */
15401   if (!cp_parser_next_token_ends_template_argument_p (parser))
15402     cp_parser_error (parser, "expected template-argument");
15403   if (!cp_parser_error_occurred (parser))
15404     {
15405       /* Figure out what is being referred to.  If the id-expression
15406          was for a class template specialization, then we will have a
15407          TYPE_DECL at this point.  There is no need to do name lookup
15408          at this point in that case.  */
15409       if (TREE_CODE (argument) != TYPE_DECL)
15410         argument = cp_parser_lookup_name (parser, argument,
15411                                           none_type,
15412                                           /*is_template=*/template_p,
15413                                           /*is_namespace=*/false,
15414                                           /*check_dependency=*/true,
15415                                           /*ambiguous_decls=*/NULL,
15416                                           argument_start_token->location);
15417       /* Handle a constrained-type-specifier for a non-type template
15418          parameter.  */
15419       if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15420         argument = decl;
15421       else if (TREE_CODE (argument) != TEMPLATE_DECL
15422                && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15423         cp_parser_error (parser, "expected template-name");
15424     }
15425   if (cp_parser_parse_definitely (parser))
15426     {
15427       if (TREE_DEPRECATED (argument))
15428         warn_deprecated_use (argument, NULL_TREE);
15429       return argument;
15430     }
15431   /* It must be a non-type argument.  In C++17 any constant-expression is
15432      allowed.  */
15433   if (cxx_dialect > cxx14)
15434     goto general_expr;
15435
15436   /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15437
15438      -- an integral constant-expression of integral or enumeration
15439         type; or
15440
15441      -- the name of a non-type template-parameter; or
15442
15443      -- the name of an object or function with external linkage...
15444
15445      -- the address of an object or function with external linkage...
15446
15447      -- a pointer to member...  */
15448   /* Look for a non-type template parameter.  */
15449   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15450     {
15451       cp_parser_parse_tentatively (parser);
15452       argument = cp_parser_primary_expression (parser,
15453                                                /*address_p=*/false,
15454                                                /*cast_p=*/false,
15455                                                /*template_arg_p=*/true,
15456                                                &idk);
15457       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15458           || !cp_parser_next_token_ends_template_argument_p (parser))
15459         cp_parser_simulate_error (parser);
15460       if (cp_parser_parse_definitely (parser))
15461         return argument;
15462     }
15463
15464   /* If the next token is "&", the argument must be the address of an
15465      object or function with external linkage.  */
15466   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15467   if (address_p)
15468     {
15469       loc = cp_lexer_peek_token (parser->lexer)->location;
15470       cp_lexer_consume_token (parser->lexer);
15471     }
15472   /* See if we might have an id-expression.  */
15473   token = cp_lexer_peek_token (parser->lexer);
15474   if (token->type == CPP_NAME
15475       || token->keyword == RID_OPERATOR
15476       || token->type == CPP_SCOPE
15477       || token->type == CPP_TEMPLATE_ID
15478       || token->type == CPP_NESTED_NAME_SPECIFIER)
15479     {
15480       cp_parser_parse_tentatively (parser);
15481       argument = cp_parser_primary_expression (parser,
15482                                                address_p,
15483                                                /*cast_p=*/false,
15484                                                /*template_arg_p=*/true,
15485                                                &idk);
15486       if (cp_parser_error_occurred (parser)
15487           || !cp_parser_next_token_ends_template_argument_p (parser))
15488         cp_parser_abort_tentative_parse (parser);
15489       else
15490         {
15491           tree probe;
15492
15493           if (INDIRECT_REF_P (argument))
15494             {
15495               /* Strip the dereference temporarily.  */
15496               gcc_assert (REFERENCE_REF_P (argument));
15497               argument = TREE_OPERAND (argument, 0);
15498             }
15499
15500           /* If we're in a template, we represent a qualified-id referring
15501              to a static data member as a SCOPE_REF even if the scope isn't
15502              dependent so that we can check access control later.  */
15503           probe = argument;
15504           if (TREE_CODE (probe) == SCOPE_REF)
15505             probe = TREE_OPERAND (probe, 1);
15506           if (VAR_P (probe))
15507             {
15508               /* A variable without external linkage might still be a
15509                  valid constant-expression, so no error is issued here
15510                  if the external-linkage check fails.  */
15511               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15512                 cp_parser_simulate_error (parser);
15513             }
15514           else if (is_overloaded_fn (argument))
15515             /* All overloaded functions are allowed; if the external
15516                linkage test does not pass, an error will be issued
15517                later.  */
15518             ;
15519           else if (address_p
15520                    && (TREE_CODE (argument) == OFFSET_REF
15521                        || TREE_CODE (argument) == SCOPE_REF))
15522             /* A pointer-to-member.  */
15523             ;
15524           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15525             ;
15526           else
15527             cp_parser_simulate_error (parser);
15528
15529           if (cp_parser_parse_definitely (parser))
15530             {
15531               if (address_p)
15532                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15533                                              tf_warning_or_error);
15534               else
15535                 argument = convert_from_reference (argument);
15536               return argument;
15537             }
15538         }
15539     }
15540   /* If the argument started with "&", there are no other valid
15541      alternatives at this point.  */
15542   if (address_p)
15543     {
15544       cp_parser_error (parser, "invalid non-type template argument");
15545       return error_mark_node;
15546     }
15547
15548  general_expr:
15549   /* If the argument wasn't successfully parsed as a type-id followed
15550      by '>>', the argument can only be a constant expression now.
15551      Otherwise, we try parsing the constant-expression tentatively,
15552      because the argument could really be a type-id.  */
15553   if (maybe_type_id)
15554     cp_parser_parse_tentatively (parser);
15555
15556   if (cxx_dialect <= cxx14)
15557     argument = cp_parser_constant_expression (parser);
15558   else
15559     {
15560       /* With C++17 generalized non-type template arguments we need to handle
15561          lvalue constant expressions, too.  */
15562       argument = cp_parser_assignment_expression (parser);
15563       require_potential_constant_expression (argument);
15564     }
15565
15566   if (!maybe_type_id)
15567     return argument;
15568   if (!cp_parser_next_token_ends_template_argument_p (parser))
15569     cp_parser_error (parser, "expected template-argument");
15570   if (cp_parser_parse_definitely (parser))
15571     return argument;
15572   /* We did our best to parse the argument as a non type-id, but that
15573      was the only alternative that matched (albeit with a '>' after
15574      it). We can assume it's just a typo from the user, and a
15575      diagnostic will then be issued.  */
15576   return cp_parser_template_type_arg (parser);
15577 }
15578
15579 /* Parse an explicit-instantiation.
15580
15581    explicit-instantiation:
15582      template declaration
15583
15584    Although the standard says `declaration', what it really means is:
15585
15586    explicit-instantiation:
15587      template decl-specifier-seq [opt] declarator [opt] ;
15588
15589    Things like `template int S<int>::i = 5, int S<double>::j;' are not
15590    supposed to be allowed.  A defect report has been filed about this
15591    issue.
15592
15593    GNU Extension:
15594
15595    explicit-instantiation:
15596      storage-class-specifier template
15597        decl-specifier-seq [opt] declarator [opt] ;
15598      function-specifier template
15599        decl-specifier-seq [opt] declarator [opt] ;  */
15600
15601 static void
15602 cp_parser_explicit_instantiation (cp_parser* parser)
15603 {
15604   int declares_class_or_enum;
15605   cp_decl_specifier_seq decl_specifiers;
15606   tree extension_specifier = NULL_TREE;
15607
15608   timevar_push (TV_TEMPLATE_INST);
15609
15610   /* Look for an (optional) storage-class-specifier or
15611      function-specifier.  */
15612   if (cp_parser_allow_gnu_extensions_p (parser))
15613     {
15614       extension_specifier
15615         = cp_parser_storage_class_specifier_opt (parser);
15616       if (!extension_specifier)
15617         extension_specifier
15618           = cp_parser_function_specifier_opt (parser,
15619                                               /*decl_specs=*/NULL);
15620     }
15621
15622   /* Look for the `template' keyword.  */
15623   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15624   /* Let the front end know that we are processing an explicit
15625      instantiation.  */
15626   begin_explicit_instantiation ();
15627   /* [temp.explicit] says that we are supposed to ignore access
15628      control while processing explicit instantiation directives.  */
15629   push_deferring_access_checks (dk_no_check);
15630   /* Parse a decl-specifier-seq.  */
15631   cp_parser_decl_specifier_seq (parser,
15632                                 CP_PARSER_FLAGS_OPTIONAL,
15633                                 &decl_specifiers,
15634                                 &declares_class_or_enum);
15635   /* If there was exactly one decl-specifier, and it declared a class,
15636      and there's no declarator, then we have an explicit type
15637      instantiation.  */
15638   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15639     {
15640       tree type;
15641
15642       type = check_tag_decl (&decl_specifiers,
15643                              /*explicit_type_instantiation_p=*/true);
15644       /* Turn access control back on for names used during
15645          template instantiation.  */
15646       pop_deferring_access_checks ();
15647       if (type)
15648         do_type_instantiation (type, extension_specifier,
15649                                /*complain=*/tf_error);
15650     }
15651   else
15652     {
15653       cp_declarator *declarator;
15654       tree decl;
15655
15656       /* Parse the declarator.  */
15657       declarator
15658         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15659                                 /*ctor_dtor_or_conv_p=*/NULL,
15660                                 /*parenthesized_p=*/NULL,
15661                                 /*member_p=*/false,
15662                                 /*friend_p=*/false);
15663       if (declares_class_or_enum & 2)
15664         cp_parser_check_for_definition_in_return_type (declarator,
15665                                                        decl_specifiers.type,
15666                                                        decl_specifiers.locations[ds_type_spec]);
15667       if (declarator != cp_error_declarator)
15668         {
15669           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15670             permerror (decl_specifiers.locations[ds_inline],
15671                        "explicit instantiation shall not use"
15672                        " %<inline%> specifier");
15673           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15674             permerror (decl_specifiers.locations[ds_constexpr],
15675                        "explicit instantiation shall not use"
15676                        " %<constexpr%> specifier");
15677
15678           decl = grokdeclarator (declarator, &decl_specifiers,
15679                                  NORMAL, 0, &decl_specifiers.attributes);
15680           /* Turn access control back on for names used during
15681              template instantiation.  */
15682           pop_deferring_access_checks ();
15683           /* Do the explicit instantiation.  */
15684           do_decl_instantiation (decl, extension_specifier);
15685         }
15686       else
15687         {
15688           pop_deferring_access_checks ();
15689           /* Skip the body of the explicit instantiation.  */
15690           cp_parser_skip_to_end_of_statement (parser);
15691         }
15692     }
15693   /* We're done with the instantiation.  */
15694   end_explicit_instantiation ();
15695
15696   cp_parser_consume_semicolon_at_end_of_statement (parser);
15697
15698   timevar_pop (TV_TEMPLATE_INST);
15699 }
15700
15701 /* Parse an explicit-specialization.
15702
15703    explicit-specialization:
15704      template < > declaration
15705
15706    Although the standard says `declaration', what it really means is:
15707
15708    explicit-specialization:
15709      template <> decl-specifier [opt] init-declarator [opt] ;
15710      template <> function-definition
15711      template <> explicit-specialization
15712      template <> template-declaration  */
15713
15714 static void
15715 cp_parser_explicit_specialization (cp_parser* parser)
15716 {
15717   bool need_lang_pop;
15718   cp_token *token = cp_lexer_peek_token (parser->lexer);
15719
15720   /* Look for the `template' keyword.  */
15721   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15722   /* Look for the `<'.  */
15723   cp_parser_require (parser, CPP_LESS, RT_LESS);
15724   /* Look for the `>'.  */
15725   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15726   /* We have processed another parameter list.  */
15727   ++parser->num_template_parameter_lists;
15728   /* [temp]
15729
15730      A template ... explicit specialization ... shall not have C
15731      linkage.  */
15732   if (current_lang_name == lang_name_c)
15733     {
15734       error_at (token->location, "template specialization with C linkage");
15735       /* Give it C++ linkage to avoid confusing other parts of the
15736          front end.  */
15737       push_lang_context (lang_name_cplusplus);
15738       need_lang_pop = true;
15739     }
15740   else
15741     need_lang_pop = false;
15742   /* Let the front end know that we are beginning a specialization.  */
15743   if (!begin_specialization ())
15744     {
15745       end_specialization ();
15746       return;
15747     }
15748
15749   /* If the next keyword is `template', we need to figure out whether
15750      or not we're looking a template-declaration.  */
15751   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15752     {
15753       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15754           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15755         cp_parser_template_declaration_after_export (parser,
15756                                                      /*member_p=*/false);
15757       else
15758         cp_parser_explicit_specialization (parser);
15759     }
15760   else
15761     /* Parse the dependent declaration.  */
15762     cp_parser_single_declaration (parser,
15763                                   /*checks=*/NULL,
15764                                   /*member_p=*/false,
15765                                   /*explicit_specialization_p=*/true,
15766                                   /*friend_p=*/NULL);
15767   /* We're done with the specialization.  */
15768   end_specialization ();
15769   /* For the erroneous case of a template with C linkage, we pushed an
15770      implicit C++ linkage scope; exit that scope now.  */
15771   if (need_lang_pop)
15772     pop_lang_context ();
15773   /* We're done with this parameter list.  */
15774   --parser->num_template_parameter_lists;
15775 }
15776
15777 /* Parse a type-specifier.
15778
15779    type-specifier:
15780      simple-type-specifier
15781      class-specifier
15782      enum-specifier
15783      elaborated-type-specifier
15784      cv-qualifier
15785
15786    GNU Extension:
15787
15788    type-specifier:
15789      __complex__
15790
15791    Returns a representation of the type-specifier.  For a
15792    class-specifier, enum-specifier, or elaborated-type-specifier, a
15793    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15794
15795    The parser flags FLAGS is used to control type-specifier parsing.
15796
15797    If IS_DECLARATION is TRUE, then this type-specifier is appearing
15798    in a decl-specifier-seq.
15799
15800    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15801    class-specifier, enum-specifier, or elaborated-type-specifier, then
15802    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
15803    if a type is declared; 2 if it is defined.  Otherwise, it is set to
15804    zero.
15805
15806    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15807    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
15808    is set to FALSE.  */
15809
15810 static tree
15811 cp_parser_type_specifier (cp_parser* parser,
15812                           cp_parser_flags flags,
15813                           cp_decl_specifier_seq *decl_specs,
15814                           bool is_declaration,
15815                           int* declares_class_or_enum,
15816                           bool* is_cv_qualifier)
15817 {
15818   tree type_spec = NULL_TREE;
15819   cp_token *token;
15820   enum rid keyword;
15821   cp_decl_spec ds = ds_last;
15822
15823   /* Assume this type-specifier does not declare a new type.  */
15824   if (declares_class_or_enum)
15825     *declares_class_or_enum = 0;
15826   /* And that it does not specify a cv-qualifier.  */
15827   if (is_cv_qualifier)
15828     *is_cv_qualifier = false;
15829   /* Peek at the next token.  */
15830   token = cp_lexer_peek_token (parser->lexer);
15831
15832   /* If we're looking at a keyword, we can use that to guide the
15833      production we choose.  */
15834   keyword = token->keyword;
15835   switch (keyword)
15836     {
15837     case RID_ENUM:
15838       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15839         goto elaborated_type_specifier;
15840
15841       /* Look for the enum-specifier.  */
15842       type_spec = cp_parser_enum_specifier (parser);
15843       /* If that worked, we're done.  */
15844       if (type_spec)
15845         {
15846           if (declares_class_or_enum)
15847             *declares_class_or_enum = 2;
15848           if (decl_specs)
15849             cp_parser_set_decl_spec_type (decl_specs,
15850                                           type_spec,
15851                                           token,
15852                                           /*type_definition_p=*/true);
15853           return type_spec;
15854         }
15855       else
15856         goto elaborated_type_specifier;
15857
15858       /* Any of these indicate either a class-specifier, or an
15859          elaborated-type-specifier.  */
15860     case RID_CLASS:
15861     case RID_STRUCT:
15862     case RID_UNION:
15863       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15864         goto elaborated_type_specifier;
15865
15866       /* Parse tentatively so that we can back up if we don't find a
15867          class-specifier.  */
15868       cp_parser_parse_tentatively (parser);
15869       /* Look for the class-specifier.  */
15870       type_spec = cp_parser_class_specifier (parser);
15871       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15872       /* If that worked, we're done.  */
15873       if (cp_parser_parse_definitely (parser))
15874         {
15875           if (declares_class_or_enum)
15876             *declares_class_or_enum = 2;
15877           if (decl_specs)
15878             cp_parser_set_decl_spec_type (decl_specs,
15879                                           type_spec,
15880                                           token,
15881                                           /*type_definition_p=*/true);
15882           return type_spec;
15883         }
15884
15885       /* Fall through.  */
15886     elaborated_type_specifier:
15887       /* We're declaring (not defining) a class or enum.  */
15888       if (declares_class_or_enum)
15889         *declares_class_or_enum = 1;
15890
15891       /* Fall through.  */
15892     case RID_TYPENAME:
15893       /* Look for an elaborated-type-specifier.  */
15894       type_spec
15895         = (cp_parser_elaborated_type_specifier
15896            (parser,
15897             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15898             is_declaration));
15899       if (decl_specs)
15900         cp_parser_set_decl_spec_type (decl_specs,
15901                                       type_spec,
15902                                       token,
15903                                       /*type_definition_p=*/false);
15904       return type_spec;
15905
15906     case RID_CONST:
15907       ds = ds_const;
15908       if (is_cv_qualifier)
15909         *is_cv_qualifier = true;
15910       break;
15911
15912     case RID_VOLATILE:
15913       ds = ds_volatile;
15914       if (is_cv_qualifier)
15915         *is_cv_qualifier = true;
15916       break;
15917
15918     case RID_RESTRICT:
15919       ds = ds_restrict;
15920       if (is_cv_qualifier)
15921         *is_cv_qualifier = true;
15922       break;
15923
15924     case RID_COMPLEX:
15925       /* The `__complex__' keyword is a GNU extension.  */
15926       ds = ds_complex;
15927       break;
15928
15929     default:
15930       break;
15931     }
15932
15933   /* Handle simple keywords.  */
15934   if (ds != ds_last)
15935     {
15936       if (decl_specs)
15937         {
15938           set_and_check_decl_spec_loc (decl_specs, ds, token);
15939           decl_specs->any_specifiers_p = true;
15940         }
15941       return cp_lexer_consume_token (parser->lexer)->u.value;
15942     }
15943
15944   /* If we do not already have a type-specifier, assume we are looking
15945      at a simple-type-specifier.  */
15946   type_spec = cp_parser_simple_type_specifier (parser,
15947                                                decl_specs,
15948                                                flags);
15949
15950   /* If we didn't find a type-specifier, and a type-specifier was not
15951      optional in this context, issue an error message.  */
15952   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15953     {
15954       cp_parser_error (parser, "expected type specifier");
15955       return error_mark_node;
15956     }
15957
15958   return type_spec;
15959 }
15960
15961 /* Parse a simple-type-specifier.
15962
15963    simple-type-specifier:
15964      :: [opt] nested-name-specifier [opt] type-name
15965      :: [opt] nested-name-specifier template template-id
15966      char
15967      wchar_t
15968      bool
15969      short
15970      int
15971      long
15972      signed
15973      unsigned
15974      float
15975      double
15976      void
15977
15978    C++0x Extension:
15979
15980    simple-type-specifier:
15981      auto
15982      decltype ( expression )   
15983      char16_t
15984      char32_t
15985      __underlying_type ( type-id )
15986
15987    GNU Extension:
15988
15989    simple-type-specifier:
15990      __int128
15991      __typeof__ unary-expression
15992      __typeof__ ( type-id )
15993      __typeof__ ( type-id ) { initializer-list , [opt] }
15994
15995    Concepts Extension:
15996
15997    simple-type-specifier:
15998      constrained-type-specifier
15999
16000    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
16001    appropriately updated.  */
16002
16003 static tree
16004 cp_parser_simple_type_specifier (cp_parser* parser,
16005                                  cp_decl_specifier_seq *decl_specs,
16006                                  cp_parser_flags flags)
16007 {
16008   tree type = NULL_TREE;
16009   cp_token *token;
16010   int idx;
16011
16012   /* Peek at the next token.  */
16013   token = cp_lexer_peek_token (parser->lexer);
16014
16015   /* If we're looking at a keyword, things are easy.  */
16016   switch (token->keyword)
16017     {
16018     case RID_CHAR:
16019       if (decl_specs)
16020         decl_specs->explicit_char_p = true;
16021       type = char_type_node;
16022       break;
16023     case RID_CHAR16:
16024       type = char16_type_node;
16025       break;
16026     case RID_CHAR32:
16027       type = char32_type_node;
16028       break;
16029     case RID_WCHAR:
16030       type = wchar_type_node;
16031       break;
16032     case RID_BOOL:
16033       type = boolean_type_node;
16034       break;
16035     case RID_SHORT:
16036       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16037       type = short_integer_type_node;
16038       break;
16039     case RID_INT:
16040       if (decl_specs)
16041         decl_specs->explicit_int_p = true;
16042       type = integer_type_node;
16043       break;
16044     case RID_INT_N_0:
16045     case RID_INT_N_1:
16046     case RID_INT_N_2:
16047     case RID_INT_N_3:
16048       idx = token->keyword - RID_INT_N_0;
16049       if (! int_n_enabled_p [idx])
16050         break;
16051       if (decl_specs)
16052         {
16053           decl_specs->explicit_intN_p = true;
16054           decl_specs->int_n_idx = idx;
16055         }
16056       type = int_n_trees [idx].signed_type;
16057       break;
16058     case RID_LONG:
16059       if (decl_specs)
16060         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16061       type = long_integer_type_node;
16062       break;
16063     case RID_SIGNED:
16064       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16065       type = integer_type_node;
16066       break;
16067     case RID_UNSIGNED:
16068       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16069       type = unsigned_type_node;
16070       break;
16071     case RID_FLOAT:
16072       type = float_type_node;
16073       break;
16074     case RID_DOUBLE:
16075       type = double_type_node;
16076       break;
16077     case RID_VOID:
16078       type = void_type_node;
16079       break;
16080
16081     case RID_AUTO:
16082       maybe_warn_cpp0x (CPP0X_AUTO);
16083       if (parser->auto_is_implicit_function_template_parm_p)
16084         {
16085           /* The 'auto' might be the placeholder return type for a function decl
16086              with trailing return type.  */
16087           bool have_trailing_return_fn_decl = false;
16088
16089           cp_parser_parse_tentatively (parser);
16090           cp_lexer_consume_token (parser->lexer);
16091           while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16092                  && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16093                  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16094                  && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16095             {
16096               if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16097                 {
16098                   cp_lexer_consume_token (parser->lexer);
16099                   cp_parser_skip_to_closing_parenthesis (parser,
16100                                                          /*recovering*/false,
16101                                                          /*or_comma*/false,
16102                                                          /*consume_paren*/true);
16103                   continue;
16104                 }
16105
16106               if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16107                 {
16108                   have_trailing_return_fn_decl = true;
16109                   break;
16110                 }
16111
16112               cp_lexer_consume_token (parser->lexer);
16113             }
16114           cp_parser_abort_tentative_parse (parser);
16115
16116           if (have_trailing_return_fn_decl)
16117             {
16118               type = make_auto ();
16119               break;
16120             }
16121
16122           if (cxx_dialect >= cxx14)
16123             {
16124               type = synthesize_implicit_template_parm (parser, NULL_TREE);
16125               type = TREE_TYPE (type);
16126             }
16127           else
16128             type = error_mark_node;
16129
16130           if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16131             {
16132               if (cxx_dialect < cxx14)
16133                 error_at (token->location,
16134                          "use of %<auto%> in lambda parameter declaration "
16135                          "only available with "
16136                          "-std=c++14 or -std=gnu++14");
16137             }
16138           else if (cxx_dialect < cxx14)
16139             error_at (token->location,
16140                      "use of %<auto%> in parameter declaration "
16141                      "only available with "
16142                      "-std=c++14 or -std=gnu++14");
16143           else if (!flag_concepts)
16144             pedwarn (token->location, OPT_Wpedantic,
16145                      "ISO C++ forbids use of %<auto%> in parameter "
16146                      "declaration");
16147         }
16148       else
16149         type = make_auto ();
16150       break;
16151
16152     case RID_DECLTYPE:
16153       /* Since DR 743, decltype can either be a simple-type-specifier by
16154          itself or begin a nested-name-specifier.  Parsing it will replace
16155          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16156          handling below decide what to do.  */
16157       cp_parser_decltype (parser);
16158       cp_lexer_set_token_position (parser->lexer, token);
16159       break;
16160
16161     case RID_TYPEOF:
16162       /* Consume the `typeof' token.  */
16163       cp_lexer_consume_token (parser->lexer);
16164       /* Parse the operand to `typeof'.  */
16165       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16166       /* If it is not already a TYPE, take its type.  */
16167       if (!TYPE_P (type))
16168         type = finish_typeof (type);
16169
16170       if (decl_specs)
16171         cp_parser_set_decl_spec_type (decl_specs, type,
16172                                       token,
16173                                       /*type_definition_p=*/false);
16174
16175       return type;
16176
16177     case RID_UNDERLYING_TYPE:
16178       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16179       if (decl_specs)
16180         cp_parser_set_decl_spec_type (decl_specs, type,
16181                                       token,
16182                                       /*type_definition_p=*/false);
16183
16184       return type;
16185
16186     case RID_BASES:
16187     case RID_DIRECT_BASES:
16188       type = cp_parser_trait_expr (parser, token->keyword);
16189       if (decl_specs)
16190        cp_parser_set_decl_spec_type (decl_specs, type,
16191                                      token,
16192                                      /*type_definition_p=*/false);
16193       return type;
16194     default:
16195       break;
16196     }
16197
16198   /* If token is an already-parsed decltype not followed by ::,
16199      it's a simple-type-specifier.  */
16200   if (token->type == CPP_DECLTYPE
16201       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16202     {
16203       type = saved_checks_value (token->u.tree_check_value);
16204       if (decl_specs)
16205         {
16206           cp_parser_set_decl_spec_type (decl_specs, type,
16207                                         token,
16208                                         /*type_definition_p=*/false);
16209           /* Remember that we are handling a decltype in order to
16210              implement the resolution of DR 1510 when the argument
16211              isn't instantiation dependent.  */
16212           decl_specs->decltype_p = true;
16213         }
16214       cp_lexer_consume_token (parser->lexer);
16215       return type;
16216     }
16217
16218   /* If the type-specifier was for a built-in type, we're done.  */
16219   if (type)
16220     {
16221       /* Record the type.  */
16222       if (decl_specs
16223           && (token->keyword != RID_SIGNED
16224               && token->keyword != RID_UNSIGNED
16225               && token->keyword != RID_SHORT
16226               && token->keyword != RID_LONG))
16227         cp_parser_set_decl_spec_type (decl_specs,
16228                                       type,
16229                                       token,
16230                                       /*type_definition_p=*/false);
16231       if (decl_specs)
16232         decl_specs->any_specifiers_p = true;
16233
16234       /* Consume the token.  */
16235       cp_lexer_consume_token (parser->lexer);
16236
16237       if (type == error_mark_node)
16238         return error_mark_node;
16239
16240       /* There is no valid C++ program where a non-template type is
16241          followed by a "<".  That usually indicates that the user thought
16242          that the type was a template.  */
16243       cp_parser_check_for_invalid_template_id (parser, type, none_type,
16244                                                token->location);
16245
16246       return TYPE_NAME (type);
16247     }
16248
16249   /* The type-specifier must be a user-defined type.  */
16250   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16251     {
16252       bool qualified_p;
16253       bool global_p;
16254
16255       /* Don't gobble tokens or issue error messages if this is an
16256          optional type-specifier.  */
16257       if (flags & CP_PARSER_FLAGS_OPTIONAL)
16258         cp_parser_parse_tentatively (parser);
16259
16260       /* Look for the optional `::' operator.  */
16261       global_p
16262         = (cp_parser_global_scope_opt (parser,
16263                                        /*current_scope_valid_p=*/false)
16264            != NULL_TREE);
16265       /* Look for the nested-name specifier.  */
16266       qualified_p
16267         = (cp_parser_nested_name_specifier_opt (parser,
16268                                                 /*typename_keyword_p=*/false,
16269                                                 /*check_dependency_p=*/true,
16270                                                 /*type_p=*/false,
16271                                                 /*is_declaration=*/false)
16272            != NULL_TREE);
16273       token = cp_lexer_peek_token (parser->lexer);
16274       /* If we have seen a nested-name-specifier, and the next token
16275          is `template', then we are using the template-id production.  */
16276       if (parser->scope
16277           && cp_parser_optional_template_keyword (parser))
16278         {
16279           /* Look for the template-id.  */
16280           type = cp_parser_template_id (parser,
16281                                         /*template_keyword_p=*/true,
16282                                         /*check_dependency_p=*/true,
16283                                         none_type,
16284                                         /*is_declaration=*/false);
16285           /* If the template-id did not name a type, we are out of
16286              luck.  */
16287           if (TREE_CODE (type) != TYPE_DECL)
16288             {
16289               cp_parser_error (parser, "expected template-id for type");
16290               type = NULL_TREE;
16291             }
16292         }
16293       /* Otherwise, look for a type-name.  */
16294       else
16295         type = cp_parser_type_name (parser);
16296       /* Keep track of all name-lookups performed in class scopes.  */
16297       if (type
16298           && !global_p
16299           && !qualified_p
16300           && TREE_CODE (type) == TYPE_DECL
16301           && identifier_p (DECL_NAME (type)))
16302         maybe_note_name_used_in_class (DECL_NAME (type), type);
16303       /* If it didn't work out, we don't have a TYPE.  */
16304       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16305           && !cp_parser_parse_definitely (parser))
16306         type = NULL_TREE;
16307       if (type && decl_specs)
16308         cp_parser_set_decl_spec_type (decl_specs, type,
16309                                       token,
16310                                       /*type_definition_p=*/false);
16311     }
16312
16313   /* If we didn't get a type-name, issue an error message.  */
16314   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16315     {
16316       cp_parser_error (parser, "expected type-name");
16317       return error_mark_node;
16318     }
16319
16320   if (type && type != error_mark_node)
16321     {
16322       /* See if TYPE is an Objective-C type, and if so, parse and
16323          accept any protocol references following it.  Do this before
16324          the cp_parser_check_for_invalid_template_id() call, because
16325          Objective-C types can be followed by '<...>' which would
16326          enclose protocol names rather than template arguments, and so
16327          everything is fine.  */
16328       if (c_dialect_objc () && !parser->scope
16329           && (objc_is_id (type) || objc_is_class_name (type)))
16330         {
16331           tree protos = cp_parser_objc_protocol_refs_opt (parser);
16332           tree qual_type = objc_get_protocol_qualified_type (type, protos);
16333
16334           /* Clobber the "unqualified" type previously entered into
16335              DECL_SPECS with the new, improved protocol-qualified version.  */
16336           if (decl_specs)
16337             decl_specs->type = qual_type;
16338
16339           return qual_type;
16340         }
16341
16342       /* There is no valid C++ program where a non-template type is
16343          followed by a "<".  That usually indicates that the user
16344          thought that the type was a template.  */
16345       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16346                                                none_type,
16347                                                token->location);
16348     }
16349
16350   return type;
16351 }
16352
16353 /* Parse a type-name.
16354
16355    type-name:
16356      class-name
16357      enum-name
16358      typedef-name
16359      simple-template-id [in c++0x]
16360
16361    enum-name:
16362      identifier
16363
16364    typedef-name:
16365      identifier
16366
16367   Concepts:
16368
16369    type-name:
16370      concept-name
16371      partial-concept-id
16372
16373    concept-name:
16374      identifier
16375
16376    Returns a TYPE_DECL for the type.  */
16377
16378 static tree
16379 cp_parser_type_name (cp_parser* parser)
16380 {
16381   return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16382 }
16383
16384 /* See above. */
16385 static tree
16386 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16387 {
16388   tree type_decl;
16389
16390   /* We can't know yet whether it is a class-name or not.  */
16391   cp_parser_parse_tentatively (parser);
16392   /* Try a class-name.  */
16393   type_decl = cp_parser_class_name (parser,
16394                                     typename_keyword_p,
16395                                     /*template_keyword_p=*/false,
16396                                     none_type,
16397                                     /*check_dependency_p=*/true,
16398                                     /*class_head_p=*/false,
16399                                     /*is_declaration=*/false);
16400   /* If it's not a class-name, keep looking.  */
16401   if (!cp_parser_parse_definitely (parser))
16402     {
16403       if (cxx_dialect < cxx11)
16404         /* It must be a typedef-name or an enum-name.  */
16405         return cp_parser_nonclass_name (parser);
16406
16407       cp_parser_parse_tentatively (parser);
16408       /* It is either a simple-template-id representing an
16409          instantiation of an alias template...  */
16410       type_decl = cp_parser_template_id (parser,
16411                                          /*template_keyword_p=*/false,
16412                                          /*check_dependency_p=*/true,
16413                                          none_type,
16414                                          /*is_declaration=*/false);
16415       /* Note that this must be an instantiation of an alias template
16416          because [temp.names]/6 says:
16417          
16418              A template-id that names an alias template specialization
16419              is a type-name.
16420
16421          Whereas [temp.names]/7 says:
16422          
16423              A simple-template-id that names a class template
16424              specialization is a class-name.
16425
16426          With concepts, this could also be a partial-concept-id that
16427          declares a non-type template parameter. */
16428       if (type_decl != NULL_TREE
16429           && TREE_CODE (type_decl) == TYPE_DECL
16430           && TYPE_DECL_ALIAS_P (type_decl))
16431         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16432       else if (is_constrained_parameter (type_decl))
16433         /* Don't do anything. */ ;
16434       else
16435         cp_parser_simulate_error (parser);
16436
16437       if (!cp_parser_parse_definitely (parser))
16438         /* ... Or a typedef-name or an enum-name.  */
16439         return cp_parser_nonclass_name (parser);
16440     }
16441
16442   return type_decl;
16443 }
16444
16445 /*  Check if DECL and ARGS can form a constrained-type-specifier.
16446     If ARGS is non-null, we try to form a concept check of the
16447     form DECL<?, ARGS> where ? is a wildcard that matches any
16448     kind of template argument. If ARGS is NULL, then we try to
16449     form a concept check of the form DECL<?>. */
16450
16451 static tree
16452 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16453                                             tree decl, tree args)
16454 {
16455   gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16456
16457   /* If we a constrained-type-specifier cannot be deduced. */
16458   if (parser->prevent_constrained_type_specifiers)
16459     return NULL_TREE;
16460
16461   /* A constrained type specifier can only be found in an
16462      overload set or as a reference to a template declaration.
16463
16464      FIXME: This might be masking a bug.  It's possible that
16465      that the deduction below is causing template specializations
16466      to be formed with the wildcard as an argument.  */
16467   if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16468     return NULL_TREE;
16469
16470   /* Try to build a call expression that evaluates the
16471      concept. This can fail if the overload set refers
16472      only to non-templates. */
16473   tree placeholder = build_nt (WILDCARD_DECL);
16474   tree check = build_concept_check (decl, placeholder, args);
16475   if (check == error_mark_node)
16476     return NULL_TREE;
16477
16478   /* Deduce the checked constraint and the prototype parameter.
16479
16480      FIXME: In certain cases, failure to deduce should be a
16481      diagnosable error.  */
16482   tree conc;
16483   tree proto;
16484   if (!deduce_constrained_parameter (check, conc, proto))
16485     return NULL_TREE;
16486
16487   /* In template parameter scope, this results in a constrained
16488      parameter. Return a descriptor of that parm. */
16489   if (processing_template_parmlist)
16490     return build_constrained_parameter (conc, proto, args);
16491
16492   /* In a parameter-declaration-clause, constrained-type
16493      specifiers result in invented template parameters.  */
16494   if (parser->auto_is_implicit_function_template_parm_p)
16495     {
16496       tree x = build_constrained_parameter (conc, proto, args);
16497       return synthesize_implicit_template_parm (parser, x);
16498     }
16499   else
16500     {
16501      /* Otherwise, we're in a context where the constrained
16502         type name is deduced and the constraint applies
16503         after deduction. */
16504       return make_constrained_auto (conc, args);
16505     }
16506
16507   return NULL_TREE;
16508 }
16509
16510 /* If DECL refers to a concept, return a TYPE_DECL representing
16511    the result of using the constrained type specifier in the
16512    current context.  DECL refers to a concept if
16513
16514   - it is an overload set containing a function concept taking a single
16515     type argument, or
16516
16517   - it is a variable concept taking a single type argument.  */
16518
16519 static tree
16520 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16521 {
16522   if (flag_concepts
16523       && (TREE_CODE (decl) == OVERLOAD
16524           || BASELINK_P (decl)
16525           || variable_concept_p (decl)))
16526     return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16527   else
16528     return NULL_TREE;
16529 }
16530
16531 /* Check if DECL and ARGS form a partial-concept-id.  If so,
16532    assign ID to the resulting constrained placeholder.
16533
16534    Returns true if the partial-concept-id designates a placeholder
16535    and false otherwise. Note that *id is set to NULL_TREE in
16536    this case. */
16537
16538 static tree
16539 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16540 {
16541   return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16542 }
16543
16544 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16545    or a concept-name.
16546
16547    enum-name:
16548      identifier
16549
16550    typedef-name:
16551      identifier
16552
16553    concept-name:
16554      identifier
16555
16556    Returns a TYPE_DECL for the type.  */
16557
16558 static tree
16559 cp_parser_nonclass_name (cp_parser* parser)
16560 {
16561   tree type_decl;
16562   tree identifier;
16563
16564   cp_token *token = cp_lexer_peek_token (parser->lexer);
16565   identifier = cp_parser_identifier (parser);
16566   if (identifier == error_mark_node)
16567     return error_mark_node;
16568
16569   /* Look up the type-name.  */
16570   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16571
16572   type_decl = strip_using_decl (type_decl);
16573   
16574   /* If we found an overload set, then it may refer to a concept-name. */
16575   if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16576     type_decl = decl;
16577
16578   if (TREE_CODE (type_decl) != TYPE_DECL
16579       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16580     {
16581       /* See if this is an Objective-C type.  */
16582       tree protos = cp_parser_objc_protocol_refs_opt (parser);
16583       tree type = objc_get_protocol_qualified_type (identifier, protos);
16584       if (type)
16585         type_decl = TYPE_NAME (type);
16586     }
16587
16588   /* Issue an error if we did not find a type-name.  */
16589   if (TREE_CODE (type_decl) != TYPE_DECL
16590       /* In Objective-C, we have the complication that class names are
16591          normally type names and start declarations (eg, the
16592          "NSObject" in "NSObject *object;"), but can be used in an
16593          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16594          is an expression.  So, a classname followed by a dot is not a
16595          valid type-name.  */
16596       || (objc_is_class_name (TREE_TYPE (type_decl))
16597           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16598     {
16599       if (!cp_parser_simulate_error (parser))
16600         cp_parser_name_lookup_error (parser, identifier, type_decl,
16601                                      NLE_TYPE, token->location);
16602       return error_mark_node;
16603     }
16604   /* Remember that the name was used in the definition of the
16605      current class so that we can check later to see if the
16606      meaning would have been different after the class was
16607      entirely defined.  */
16608   else if (type_decl != error_mark_node
16609            && !parser->scope)
16610     maybe_note_name_used_in_class (identifier, type_decl);
16611   
16612   return type_decl;
16613 }
16614
16615 /* Parse an elaborated-type-specifier.  Note that the grammar given
16616    here incorporates the resolution to DR68.
16617
16618    elaborated-type-specifier:
16619      class-key :: [opt] nested-name-specifier [opt] identifier
16620      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16621      enum-key :: [opt] nested-name-specifier [opt] identifier
16622      typename :: [opt] nested-name-specifier identifier
16623      typename :: [opt] nested-name-specifier template [opt]
16624        template-id
16625
16626    GNU extension:
16627
16628    elaborated-type-specifier:
16629      class-key attributes :: [opt] nested-name-specifier [opt] identifier
16630      class-key attributes :: [opt] nested-name-specifier [opt]
16631                template [opt] template-id
16632      enum attributes :: [opt] nested-name-specifier [opt] identifier
16633
16634    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16635    declared `friend'.  If IS_DECLARATION is TRUE, then this
16636    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16637    something is being declared.
16638
16639    Returns the TYPE specified.  */
16640
16641 static tree
16642 cp_parser_elaborated_type_specifier (cp_parser* parser,
16643                                      bool is_friend,
16644                                      bool is_declaration)
16645 {
16646   enum tag_types tag_type;
16647   tree identifier;
16648   tree type = NULL_TREE;
16649   tree attributes = NULL_TREE;
16650   tree globalscope;
16651   cp_token *token = NULL;
16652
16653   /* See if we're looking at the `enum' keyword.  */
16654   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16655     {
16656       /* Consume the `enum' token.  */
16657       cp_lexer_consume_token (parser->lexer);
16658       /* Remember that it's an enumeration type.  */
16659       tag_type = enum_type;
16660       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16661          enums) is used here.  */
16662       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16663           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16664         {
16665             pedwarn (input_location, 0, "elaborated-type-specifier "
16666                       "for a scoped enum must not use the %<%D%> keyword",
16667                       cp_lexer_peek_token (parser->lexer)->u.value);
16668           /* Consume the `struct' or `class' and parse it anyway.  */
16669           cp_lexer_consume_token (parser->lexer);
16670         }
16671       /* Parse the attributes.  */
16672       attributes = cp_parser_attributes_opt (parser);
16673     }
16674   /* Or, it might be `typename'.  */
16675   else if (cp_lexer_next_token_is_keyword (parser->lexer,
16676                                            RID_TYPENAME))
16677     {
16678       /* Consume the `typename' token.  */
16679       cp_lexer_consume_token (parser->lexer);
16680       /* Remember that it's a `typename' type.  */
16681       tag_type = typename_type;
16682     }
16683   /* Otherwise it must be a class-key.  */
16684   else
16685     {
16686       tag_type = cp_parser_class_key (parser);
16687       if (tag_type == none_type)
16688         return error_mark_node;
16689       /* Parse the attributes.  */
16690       attributes = cp_parser_attributes_opt (parser);
16691     }
16692
16693   /* Look for the `::' operator.  */
16694   globalscope =  cp_parser_global_scope_opt (parser,
16695                                              /*current_scope_valid_p=*/false);
16696   /* Look for the nested-name-specifier.  */
16697   if (tag_type == typename_type && !globalscope)
16698     {
16699       if (!cp_parser_nested_name_specifier (parser,
16700                                            /*typename_keyword_p=*/true,
16701                                            /*check_dependency_p=*/true,
16702                                            /*type_p=*/true,
16703                                             is_declaration))
16704         return error_mark_node;
16705     }
16706   else
16707     /* Even though `typename' is not present, the proposed resolution
16708        to Core Issue 180 says that in `class A<T>::B', `B' should be
16709        considered a type-name, even if `A<T>' is dependent.  */
16710     cp_parser_nested_name_specifier_opt (parser,
16711                                          /*typename_keyword_p=*/true,
16712                                          /*check_dependency_p=*/true,
16713                                          /*type_p=*/true,
16714                                          is_declaration);
16715  /* For everything but enumeration types, consider a template-id.
16716     For an enumeration type, consider only a plain identifier.  */
16717   if (tag_type != enum_type)
16718     {
16719       bool template_p = false;
16720       tree decl;
16721
16722       /* Allow the `template' keyword.  */
16723       template_p = cp_parser_optional_template_keyword (parser);
16724       /* If we didn't see `template', we don't know if there's a
16725          template-id or not.  */
16726       if (!template_p)
16727         cp_parser_parse_tentatively (parser);
16728       /* Parse the template-id.  */
16729       token = cp_lexer_peek_token (parser->lexer);
16730       decl = cp_parser_template_id (parser, template_p,
16731                                     /*check_dependency_p=*/true,
16732                                     tag_type,
16733                                     is_declaration);
16734       /* If we didn't find a template-id, look for an ordinary
16735          identifier.  */
16736       if (!template_p && !cp_parser_parse_definitely (parser))
16737         ;
16738       /* We can get here when cp_parser_template_id, called by
16739          cp_parser_class_name with tag_type == none_type, succeeds
16740          and caches a BASELINK.  Then, when called again here,
16741          instead of failing and returning an error_mark_node
16742          returns it (see template/typename17.C in C++11).
16743          ??? Could we diagnose this earlier?  */
16744       else if (tag_type == typename_type && BASELINK_P (decl))
16745         {
16746           cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16747           type = error_mark_node;
16748         }
16749       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16750          in effect, then we must assume that, upon instantiation, the
16751          template will correspond to a class.  */
16752       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16753                && tag_type == typename_type)
16754         type = make_typename_type (parser->scope, decl,
16755                                    typename_type,
16756                                    /*complain=*/tf_error);
16757       /* If the `typename' keyword is in effect and DECL is not a type
16758          decl, then type is non existent.   */
16759       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16760         ; 
16761       else if (TREE_CODE (decl) == TYPE_DECL)
16762         type = check_elaborated_type_specifier (tag_type, decl,
16763                                                 /*allow_template_p=*/true);
16764       else if (decl == error_mark_node)
16765         type = error_mark_node; 
16766     }
16767
16768   if (!type)
16769     {
16770       token = cp_lexer_peek_token (parser->lexer);
16771       identifier = cp_parser_identifier (parser);
16772
16773       if (identifier == error_mark_node)
16774         {
16775           parser->scope = NULL_TREE;
16776           return error_mark_node;
16777         }
16778
16779       /* For a `typename', we needn't call xref_tag.  */
16780       if (tag_type == typename_type
16781           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16782         return cp_parser_make_typename_type (parser, identifier,
16783                                              token->location);
16784
16785       /* Template parameter lists apply only if we are not within a
16786          function parameter list.  */
16787       bool template_parm_lists_apply
16788           = parser->num_template_parameter_lists;
16789       if (template_parm_lists_apply)
16790         for (cp_binding_level *s = current_binding_level;
16791              s && s->kind != sk_template_parms;
16792              s = s->level_chain)
16793           if (s->kind == sk_function_parms)
16794             template_parm_lists_apply = false;
16795
16796       /* Look up a qualified name in the usual way.  */
16797       if (parser->scope)
16798         {
16799           tree decl;
16800           tree ambiguous_decls;
16801
16802           decl = cp_parser_lookup_name (parser, identifier,
16803                                         tag_type,
16804                                         /*is_template=*/false,
16805                                         /*is_namespace=*/false,
16806                                         /*check_dependency=*/true,
16807                                         &ambiguous_decls,
16808                                         token->location);
16809
16810           /* If the lookup was ambiguous, an error will already have been
16811              issued.  */
16812           if (ambiguous_decls)
16813             return error_mark_node;
16814
16815           /* If we are parsing friend declaration, DECL may be a
16816              TEMPLATE_DECL tree node here.  However, we need to check
16817              whether this TEMPLATE_DECL results in valid code.  Consider
16818              the following example:
16819
16820                namespace N {
16821                  template <class T> class C {};
16822                }
16823                class X {
16824                  template <class T> friend class N::C; // #1, valid code
16825                };
16826                template <class T> class Y {
16827                  friend class N::C;                    // #2, invalid code
16828                };
16829
16830              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16831              name lookup of `N::C'.  We see that friend declaration must
16832              be template for the code to be valid.  Note that
16833              processing_template_decl does not work here since it is
16834              always 1 for the above two cases.  */
16835
16836           decl = (cp_parser_maybe_treat_template_as_class
16837                   (decl, /*tag_name_p=*/is_friend
16838                          && template_parm_lists_apply));
16839
16840           if (TREE_CODE (decl) != TYPE_DECL)
16841             {
16842               cp_parser_diagnose_invalid_type_name (parser,
16843                                                     identifier,
16844                                                     token->location);
16845               return error_mark_node;
16846             }
16847
16848           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16849             {
16850               bool allow_template = (template_parm_lists_apply
16851                                      || DECL_SELF_REFERENCE_P (decl));
16852               type = check_elaborated_type_specifier (tag_type, decl,
16853                                                       allow_template);
16854
16855               if (type == error_mark_node)
16856                 return error_mark_node;
16857             }
16858
16859           /* Forward declarations of nested types, such as
16860
16861                class C1::C2;
16862                class C1::C2::C3;
16863
16864              are invalid unless all components preceding the final '::'
16865              are complete.  If all enclosing types are complete, these
16866              declarations become merely pointless.
16867
16868              Invalid forward declarations of nested types are errors
16869              caught elsewhere in parsing.  Those that are pointless arrive
16870              here.  */
16871
16872           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16873               && !is_friend && !processing_explicit_instantiation)
16874             warning (0, "declaration %qD does not declare anything", decl);
16875
16876           type = TREE_TYPE (decl);
16877         }
16878       else
16879         {
16880           /* An elaborated-type-specifier sometimes introduces a new type and
16881              sometimes names an existing type.  Normally, the rule is that it
16882              introduces a new type only if there is not an existing type of
16883              the same name already in scope.  For example, given:
16884
16885                struct S {};
16886                void f() { struct S s; }
16887
16888              the `struct S' in the body of `f' is the same `struct S' as in
16889              the global scope; the existing definition is used.  However, if
16890              there were no global declaration, this would introduce a new
16891              local class named `S'.
16892
16893              An exception to this rule applies to the following code:
16894
16895                namespace N { struct S; }
16896
16897              Here, the elaborated-type-specifier names a new type
16898              unconditionally; even if there is already an `S' in the
16899              containing scope this declaration names a new type.
16900              This exception only applies if the elaborated-type-specifier
16901              forms the complete declaration:
16902
16903                [class.name]
16904
16905                A declaration consisting solely of `class-key identifier ;' is
16906                either a redeclaration of the name in the current scope or a
16907                forward declaration of the identifier as a class name.  It
16908                introduces the name into the current scope.
16909
16910              We are in this situation precisely when the next token is a `;'.
16911
16912              An exception to the exception is that a `friend' declaration does
16913              *not* name a new type; i.e., given:
16914
16915                struct S { friend struct T; };
16916
16917              `T' is not a new type in the scope of `S'.
16918
16919              Also, `new struct S' or `sizeof (struct S)' never results in the
16920              definition of a new type; a new type can only be declared in a
16921              declaration context.  */
16922
16923           tag_scope ts;
16924           bool template_p;
16925
16926           if (is_friend)
16927             /* Friends have special name lookup rules.  */
16928             ts = ts_within_enclosing_non_class;
16929           else if (is_declaration
16930                    && cp_lexer_next_token_is (parser->lexer,
16931                                               CPP_SEMICOLON))
16932             /* This is a `class-key identifier ;' */
16933             ts = ts_current;
16934           else
16935             ts = ts_global;
16936
16937           template_p =
16938             (template_parm_lists_apply
16939              && (cp_parser_next_token_starts_class_definition_p (parser)
16940                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16941           /* An unqualified name was used to reference this type, so
16942              there were no qualifying templates.  */
16943           if (template_parm_lists_apply
16944               && !cp_parser_check_template_parameters (parser,
16945                                                        /*num_templates=*/0,
16946                                                        token->location,
16947                                                        /*declarator=*/NULL))
16948             return error_mark_node;
16949           type = xref_tag (tag_type, identifier, ts, template_p);
16950         }
16951     }
16952
16953   if (type == error_mark_node)
16954     return error_mark_node;
16955
16956   /* Allow attributes on forward declarations of classes.  */
16957   if (attributes)
16958     {
16959       if (TREE_CODE (type) == TYPENAME_TYPE)
16960         warning (OPT_Wattributes,
16961                  "attributes ignored on uninstantiated type");
16962       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16963                && ! processing_explicit_instantiation)
16964         warning (OPT_Wattributes,
16965                  "attributes ignored on template instantiation");
16966       else if (is_declaration && cp_parser_declares_only_class_p (parser))
16967         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16968       else
16969         warning (OPT_Wattributes,
16970                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16971     }
16972
16973   if (tag_type != enum_type)
16974     {
16975       /* Indicate whether this class was declared as a `class' or as a
16976          `struct'.  */
16977       if (CLASS_TYPE_P (type))
16978         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16979       cp_parser_check_class_key (tag_type, type);
16980     }
16981
16982   /* A "<" cannot follow an elaborated type specifier.  If that
16983      happens, the user was probably trying to form a template-id.  */
16984   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16985                                            token->location);
16986
16987   return type;
16988 }
16989
16990 /* Parse an enum-specifier.
16991
16992    enum-specifier:
16993      enum-head { enumerator-list [opt] }
16994      enum-head { enumerator-list , } [C++0x]
16995
16996    enum-head:
16997      enum-key identifier [opt] enum-base [opt]
16998      enum-key nested-name-specifier identifier enum-base [opt]
16999
17000    enum-key:
17001      enum
17002      enum class   [C++0x]
17003      enum struct  [C++0x]
17004
17005    enum-base:   [C++0x]
17006      : type-specifier-seq
17007
17008    opaque-enum-specifier:
17009      enum-key identifier enum-base [opt] ;
17010
17011    GNU Extensions:
17012      enum-key attributes[opt] identifier [opt] enum-base [opt] 
17013        { enumerator-list [opt] }attributes[opt]
17014      enum-key attributes[opt] identifier [opt] enum-base [opt]
17015        { enumerator-list, }attributes[opt] [C++0x]
17016
17017    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17018    if the token stream isn't an enum-specifier after all.  */
17019
17020 static tree
17021 cp_parser_enum_specifier (cp_parser* parser)
17022 {
17023   tree identifier;
17024   tree type = NULL_TREE;
17025   tree prev_scope;
17026   tree nested_name_specifier = NULL_TREE;
17027   tree attributes;
17028   bool scoped_enum_p = false;
17029   bool has_underlying_type = false;
17030   bool nested_being_defined = false;
17031   bool new_value_list = false;
17032   bool is_new_type = false;
17033   bool is_anonymous = false;
17034   tree underlying_type = NULL_TREE;
17035   cp_token *type_start_token = NULL;
17036   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17037
17038   parser->colon_corrects_to_scope_p = false;
17039
17040   /* Parse tentatively so that we can back up if we don't find a
17041      enum-specifier.  */
17042   cp_parser_parse_tentatively (parser);
17043
17044   /* Caller guarantees that the current token is 'enum', an identifier
17045      possibly follows, and the token after that is an opening brace.
17046      If we don't have an identifier, fabricate an anonymous name for
17047      the enumeration being defined.  */
17048   cp_lexer_consume_token (parser->lexer);
17049
17050   /* Parse the "class" or "struct", which indicates a scoped
17051      enumeration type in C++0x.  */
17052   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17053       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17054     {
17055       if (cxx_dialect < cxx11)
17056         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17057
17058       /* Consume the `struct' or `class' token.  */
17059       cp_lexer_consume_token (parser->lexer);
17060
17061       scoped_enum_p = true;
17062     }
17063
17064   attributes = cp_parser_attributes_opt (parser);
17065
17066   /* Clear the qualification.  */
17067   parser->scope = NULL_TREE;
17068   parser->qualifying_scope = NULL_TREE;
17069   parser->object_scope = NULL_TREE;
17070
17071   /* Figure out in what scope the declaration is being placed.  */
17072   prev_scope = current_scope ();
17073
17074   type_start_token = cp_lexer_peek_token (parser->lexer);
17075
17076   push_deferring_access_checks (dk_no_check);
17077   nested_name_specifier
17078       = cp_parser_nested_name_specifier_opt (parser,
17079                                              /*typename_keyword_p=*/true,
17080                                              /*check_dependency_p=*/false,
17081                                              /*type_p=*/false,
17082                                              /*is_declaration=*/false);
17083
17084   if (nested_name_specifier)
17085     {
17086       tree name;
17087
17088       identifier = cp_parser_identifier (parser);
17089       name =  cp_parser_lookup_name (parser, identifier,
17090                                      enum_type,
17091                                      /*is_template=*/false,
17092                                      /*is_namespace=*/false,
17093                                      /*check_dependency=*/true,
17094                                      /*ambiguous_decls=*/NULL,
17095                                      input_location);
17096       if (name && name != error_mark_node)
17097         {
17098           type = TREE_TYPE (name);
17099           if (TREE_CODE (type) == TYPENAME_TYPE)
17100             {
17101               /* Are template enums allowed in ISO? */
17102               if (template_parm_scope_p ())
17103                 pedwarn (type_start_token->location, OPT_Wpedantic,
17104                          "%qD is an enumeration template", name);
17105               /* ignore a typename reference, for it will be solved by name
17106                  in start_enum.  */
17107               type = NULL_TREE;
17108             }
17109         }
17110       else if (nested_name_specifier == error_mark_node)
17111         /* We already issued an error.  */;
17112       else
17113         {
17114           error_at (type_start_token->location,
17115                     "%qD does not name an enumeration in %qT",
17116                     identifier, nested_name_specifier);
17117           nested_name_specifier = error_mark_node;
17118         }
17119     }
17120   else
17121     {
17122       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17123         identifier = cp_parser_identifier (parser);
17124       else
17125         {
17126           identifier = make_anon_name ();
17127           is_anonymous = true;
17128           if (scoped_enum_p)
17129             error_at (type_start_token->location,
17130                       "anonymous scoped enum is not allowed");
17131         }
17132     }
17133   pop_deferring_access_checks ();
17134
17135   /* Check for the `:' that denotes a specified underlying type in C++0x.
17136      Note that a ':' could also indicate a bitfield width, however.  */
17137   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17138     {
17139       cp_decl_specifier_seq type_specifiers;
17140
17141       /* Consume the `:'.  */
17142       cp_lexer_consume_token (parser->lexer);
17143
17144       /* Parse the type-specifier-seq.  */
17145       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17146                                     /*is_trailing_return=*/false,
17147                                     &type_specifiers);
17148
17149       /* At this point this is surely not elaborated type specifier.  */
17150       if (!cp_parser_parse_definitely (parser))
17151         return NULL_TREE;
17152
17153       if (cxx_dialect < cxx11)
17154         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17155
17156       has_underlying_type = true;
17157
17158       /* If that didn't work, stop.  */
17159       if (type_specifiers.type != error_mark_node)
17160         {
17161           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17162                                             /*initialized=*/0, NULL);
17163           if (underlying_type == error_mark_node
17164               || check_for_bare_parameter_packs (underlying_type))
17165             underlying_type = NULL_TREE;
17166         }
17167     }
17168
17169   /* Look for the `{' but don't consume it yet.  */
17170   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17171     {
17172       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17173         {
17174           cp_parser_error (parser, "expected %<{%>");
17175           if (has_underlying_type)
17176             {
17177               type = NULL_TREE;
17178               goto out;
17179             }
17180         }
17181       /* An opaque-enum-specifier must have a ';' here.  */
17182       if ((scoped_enum_p || underlying_type)
17183           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17184         {
17185           cp_parser_error (parser, "expected %<;%> or %<{%>");
17186           if (has_underlying_type)
17187             {
17188               type = NULL_TREE;
17189               goto out;
17190             }
17191         }
17192     }
17193
17194   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17195     return NULL_TREE;
17196
17197   if (nested_name_specifier)
17198     {
17199       if (CLASS_TYPE_P (nested_name_specifier))
17200         {
17201           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17202           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17203           push_scope (nested_name_specifier);
17204         }
17205       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17206         {
17207           push_nested_namespace (nested_name_specifier);
17208         }
17209     }
17210
17211   /* Issue an error message if type-definitions are forbidden here.  */
17212   if (!cp_parser_check_type_definition (parser))
17213     type = error_mark_node;
17214   else
17215     /* Create the new type.  We do this before consuming the opening
17216        brace so the enum will be recorded as being on the line of its
17217        tag (or the 'enum' keyword, if there is no tag).  */
17218     type = start_enum (identifier, type, underlying_type,
17219                        attributes, scoped_enum_p, &is_new_type);
17220
17221   /* If the next token is not '{' it is an opaque-enum-specifier or an
17222      elaborated-type-specifier.  */
17223   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17224     {
17225       timevar_push (TV_PARSE_ENUM);
17226       if (nested_name_specifier
17227           && nested_name_specifier != error_mark_node)
17228         {
17229           /* The following catches invalid code such as:
17230              enum class S<int>::E { A, B, C }; */
17231           if (!processing_specialization
17232               && CLASS_TYPE_P (nested_name_specifier)
17233               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17234             error_at (type_start_token->location, "cannot add an enumerator "
17235                       "list to a template instantiation");
17236
17237           if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17238             {
17239               error_at (type_start_token->location,
17240                         "%<%T::%E%> has not been declared",
17241                         TYPE_CONTEXT (nested_name_specifier),
17242                         nested_name_specifier);
17243               type = error_mark_node;
17244             }
17245           else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17246                    && !CLASS_TYPE_P (nested_name_specifier))
17247             {
17248               error_at (type_start_token->location, "nested name specifier "
17249                         "%qT for enum declaration does not name a class "
17250                         "or namespace", nested_name_specifier);
17251               type = error_mark_node;
17252             }
17253           /* If that scope does not contain the scope in which the
17254              class was originally declared, the program is invalid.  */
17255           else if (prev_scope && !is_ancestor (prev_scope,
17256                                                nested_name_specifier))
17257             {
17258               if (at_namespace_scope_p ())
17259                 error_at (type_start_token->location,
17260                           "declaration of %qD in namespace %qD which does not "
17261                           "enclose %qD",
17262                           type, prev_scope, nested_name_specifier);
17263               else
17264                 error_at (type_start_token->location,
17265                           "declaration of %qD in %qD which does not "
17266                           "enclose %qD",
17267                           type, prev_scope, nested_name_specifier);
17268               type = error_mark_node;
17269             }
17270         }
17271
17272       if (scoped_enum_p)
17273         begin_scope (sk_scoped_enum, type);
17274
17275       /* Consume the opening brace.  */
17276       cp_lexer_consume_token (parser->lexer);
17277
17278       if (type == error_mark_node)
17279         ; /* Nothing to add */
17280       else if (OPAQUE_ENUM_P (type)
17281                || (cxx_dialect > cxx98 && processing_specialization))
17282         {
17283           new_value_list = true;
17284           SET_OPAQUE_ENUM_P (type, false);
17285           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17286         }
17287       else
17288         {
17289           error_at (type_start_token->location,
17290                     "multiple definition of %q#T", type);
17291           inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17292                   "previous definition here");
17293           type = error_mark_node;
17294         }
17295
17296       if (type == error_mark_node)
17297         cp_parser_skip_to_end_of_block_or_statement (parser);
17298       /* If the next token is not '}', then there are some enumerators.  */
17299       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17300         {
17301           if (is_anonymous && !scoped_enum_p)
17302             pedwarn (type_start_token->location, OPT_Wpedantic,
17303                      "ISO C++ forbids empty anonymous enum");
17304         }
17305       else
17306         cp_parser_enumerator_list (parser, type);
17307
17308       /* Consume the final '}'.  */
17309       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17310
17311       if (scoped_enum_p)
17312         finish_scope ();
17313       timevar_pop (TV_PARSE_ENUM);
17314     }
17315   else
17316     {
17317       /* If a ';' follows, then it is an opaque-enum-specifier
17318         and additional restrictions apply.  */
17319       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17320         {
17321           if (is_anonymous)
17322             error_at (type_start_token->location,
17323                       "opaque-enum-specifier without name");
17324           else if (nested_name_specifier)
17325             error_at (type_start_token->location,
17326                       "opaque-enum-specifier must use a simple identifier");
17327         }
17328     }
17329
17330   /* Look for trailing attributes to apply to this enumeration, and
17331      apply them if appropriate.  */
17332   if (cp_parser_allow_gnu_extensions_p (parser))
17333     {
17334       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17335       cplus_decl_attributes (&type,
17336                              trailing_attr,
17337                              (int) ATTR_FLAG_TYPE_IN_PLACE);
17338     }
17339
17340   /* Finish up the enumeration.  */
17341   if (type != error_mark_node)
17342     {
17343       if (new_value_list)
17344         finish_enum_value_list (type);
17345       if (is_new_type)
17346         finish_enum (type);
17347     }
17348
17349   if (nested_name_specifier)
17350     {
17351       if (CLASS_TYPE_P (nested_name_specifier))
17352         {
17353           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17354           pop_scope (nested_name_specifier);
17355         }
17356       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17357         {
17358           pop_nested_namespace (nested_name_specifier);
17359         }
17360     }
17361  out:
17362   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17363   return type;
17364 }
17365
17366 /* Parse an enumerator-list.  The enumerators all have the indicated
17367    TYPE.
17368
17369    enumerator-list:
17370      enumerator-definition
17371      enumerator-list , enumerator-definition  */
17372
17373 static void
17374 cp_parser_enumerator_list (cp_parser* parser, tree type)
17375 {
17376   while (true)
17377     {
17378       /* Parse an enumerator-definition.  */
17379       cp_parser_enumerator_definition (parser, type);
17380
17381       /* If the next token is not a ',', we've reached the end of
17382          the list.  */
17383       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17384         break;
17385       /* Otherwise, consume the `,' and keep going.  */
17386       cp_lexer_consume_token (parser->lexer);
17387       /* If the next token is a `}', there is a trailing comma.  */
17388       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17389         {
17390           if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17391             pedwarn (input_location, OPT_Wpedantic,
17392                      "comma at end of enumerator list");
17393           break;
17394         }
17395     }
17396 }
17397
17398 /* Parse an enumerator-definition.  The enumerator has the indicated
17399    TYPE.
17400
17401    enumerator-definition:
17402      enumerator
17403      enumerator = constant-expression
17404
17405    enumerator:
17406      identifier
17407
17408    GNU Extensions:
17409
17410    enumerator-definition:
17411      enumerator attributes [opt]
17412      enumerator attributes [opt] = constant-expression  */
17413
17414 static void
17415 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17416 {
17417   tree identifier;
17418   tree value;
17419   location_t loc;
17420
17421   /* Save the input location because we are interested in the location
17422      of the identifier and not the location of the explicit value.  */
17423   loc = cp_lexer_peek_token (parser->lexer)->location;
17424
17425   /* Look for the identifier.  */
17426   identifier = cp_parser_identifier (parser);
17427   if (identifier == error_mark_node)
17428     return;
17429
17430   /* Parse any specified attributes.  */
17431   tree attrs = cp_parser_attributes_opt (parser);
17432
17433   /* If the next token is an '=', then there is an explicit value.  */
17434   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17435     {
17436       /* Consume the `=' token.  */
17437       cp_lexer_consume_token (parser->lexer);
17438       /* Parse the value.  */
17439       value = cp_parser_constant_expression (parser);
17440     }
17441   else
17442     value = NULL_TREE;
17443
17444   /* If we are processing a template, make sure the initializer of the
17445      enumerator doesn't contain any bare template parameter pack.  */
17446   if (check_for_bare_parameter_packs (value))
17447     value = error_mark_node;
17448
17449   /* Create the enumerator.  */
17450   build_enumerator (identifier, value, type, attrs, loc);
17451 }
17452
17453 /* Parse a namespace-name.
17454
17455    namespace-name:
17456      original-namespace-name
17457      namespace-alias
17458
17459    Returns the NAMESPACE_DECL for the namespace.  */
17460
17461 static tree
17462 cp_parser_namespace_name (cp_parser* parser)
17463 {
17464   tree identifier;
17465   tree namespace_decl;
17466
17467   cp_token *token = cp_lexer_peek_token (parser->lexer);
17468
17469   /* Get the name of the namespace.  */
17470   identifier = cp_parser_identifier (parser);
17471   if (identifier == error_mark_node)
17472     return error_mark_node;
17473
17474   /* Look up the identifier in the currently active scope.  Look only
17475      for namespaces, due to:
17476
17477        [basic.lookup.udir]
17478
17479        When looking up a namespace-name in a using-directive or alias
17480        definition, only namespace names are considered.
17481
17482      And:
17483
17484        [basic.lookup.qual]
17485
17486        During the lookup of a name preceding the :: scope resolution
17487        operator, object, function, and enumerator names are ignored.
17488
17489      (Note that cp_parser_qualifying_entity only calls this
17490      function if the token after the name is the scope resolution
17491      operator.)  */
17492   namespace_decl = cp_parser_lookup_name (parser, identifier,
17493                                           none_type,
17494                                           /*is_template=*/false,
17495                                           /*is_namespace=*/true,
17496                                           /*check_dependency=*/true,
17497                                           /*ambiguous_decls=*/NULL,
17498                                           token->location);
17499   /* If it's not a namespace, issue an error.  */
17500   if (namespace_decl == error_mark_node
17501       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17502     {
17503       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17504         error_at (token->location, "%qD is not a namespace-name", identifier);
17505       cp_parser_error (parser, "expected namespace-name");
17506       namespace_decl = error_mark_node;
17507     }
17508
17509   return namespace_decl;
17510 }
17511
17512 /* Parse a namespace-definition.
17513
17514    namespace-definition:
17515      named-namespace-definition
17516      unnamed-namespace-definition
17517
17518    named-namespace-definition:
17519      original-namespace-definition
17520      extension-namespace-definition
17521
17522    original-namespace-definition:
17523      namespace identifier { namespace-body }
17524
17525    extension-namespace-definition:
17526      namespace original-namespace-name { namespace-body }
17527
17528    unnamed-namespace-definition:
17529      namespace { namespace-body } */
17530
17531 static void
17532 cp_parser_namespace_definition (cp_parser* parser)
17533 {
17534   tree identifier, attribs;
17535   bool has_visibility;
17536   bool is_inline;
17537   cp_token* token;
17538   int nested_definition_count = 0;
17539
17540   cp_ensure_no_omp_declare_simd (parser);
17541   cp_ensure_no_oacc_routine (parser);
17542   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17543     {
17544       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17545       is_inline = true;
17546       cp_lexer_consume_token (parser->lexer);
17547     }
17548   else
17549     is_inline = false;
17550
17551   /* Look for the `namespace' keyword.  */
17552   token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17553
17554   /* Parse any specified attributes before the identifier.  */
17555   attribs = cp_parser_attributes_opt (parser);
17556
17557   /* Get the name of the namespace.  We do not attempt to distinguish
17558      between an original-namespace-definition and an
17559      extension-namespace-definition at this point.  The semantic
17560      analysis routines are responsible for that.  */
17561   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17562     identifier = cp_parser_identifier (parser);
17563   else
17564     identifier = NULL_TREE;
17565
17566   /* Parse any specified attributes after the identifier.  */
17567   tree post_ident_attribs = cp_parser_attributes_opt (parser);
17568   if (post_ident_attribs)
17569     {
17570       if (attribs)
17571         attribs = chainon (attribs, post_ident_attribs);
17572       else
17573         attribs = post_ident_attribs;
17574     }
17575
17576   /* Start the namespace.  */
17577   push_namespace (identifier);
17578
17579   /* Parse any nested namespace definition. */
17580   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17581     {
17582       if (attribs)
17583         error_at (token->location, "a nested namespace definition cannot have attributes");
17584       if (cxx_dialect < cxx1z)
17585         pedwarn (input_location, OPT_Wpedantic,
17586                  "nested namespace definitions only available with "
17587                  "-std=c++1z or -std=gnu++1z");
17588       if (is_inline)
17589         error_at (token->location, "a nested namespace definition cannot be inline");
17590       while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17591         {
17592           cp_lexer_consume_token (parser->lexer);
17593           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17594             identifier = cp_parser_identifier (parser);
17595           else
17596             {
17597               cp_parser_error (parser, "nested identifier required");
17598               break;
17599             }
17600           ++nested_definition_count;
17601           push_namespace (identifier);
17602         }
17603     }
17604
17605   /* Look for the `{' to validate starting the namespace.  */
17606   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17607
17608   /* "inline namespace" is equivalent to a stub namespace definition
17609      followed by a strong using directive.  */
17610   if (is_inline)
17611     {
17612       tree name_space = current_namespace;
17613       /* Set up namespace association.  */
17614       DECL_NAMESPACE_ASSOCIATIONS (name_space)
17615         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17616                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
17617       /* Import the contents of the inline namespace.  */
17618       pop_namespace ();
17619       do_using_directive (name_space);
17620       push_namespace (identifier);
17621     }
17622
17623   has_visibility = handle_namespace_attrs (current_namespace, attribs);
17624
17625   warning  (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17626
17627   /* Parse the body of the namespace.  */
17628   cp_parser_namespace_body (parser);
17629
17630   if (has_visibility)
17631     pop_visibility (1);
17632
17633   /* Finish the nested namespace definitions.  */
17634   while (nested_definition_count--)
17635     pop_namespace ();
17636
17637   /* Finish the namespace.  */
17638   pop_namespace ();
17639   /* Look for the final `}'.  */
17640   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17641 }
17642
17643 /* Parse a namespace-body.
17644
17645    namespace-body:
17646      declaration-seq [opt]  */
17647
17648 static void
17649 cp_parser_namespace_body (cp_parser* parser)
17650 {
17651   cp_parser_declaration_seq_opt (parser);
17652 }
17653
17654 /* Parse a namespace-alias-definition.
17655
17656    namespace-alias-definition:
17657      namespace identifier = qualified-namespace-specifier ;  */
17658
17659 static void
17660 cp_parser_namespace_alias_definition (cp_parser* parser)
17661 {
17662   tree identifier;
17663   tree namespace_specifier;
17664
17665   cp_token *token = cp_lexer_peek_token (parser->lexer);
17666
17667   /* Look for the `namespace' keyword.  */
17668   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17669   /* Look for the identifier.  */
17670   identifier = cp_parser_identifier (parser);
17671   if (identifier == error_mark_node)
17672     return;
17673   /* Look for the `=' token.  */
17674   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17675       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
17676     {
17677       error_at (token->location, "%<namespace%> definition is not allowed here");
17678       /* Skip the definition.  */
17679       cp_lexer_consume_token (parser->lexer);
17680       if (cp_parser_skip_to_closing_brace (parser))
17681         cp_lexer_consume_token (parser->lexer);
17682       return;
17683     }
17684   cp_parser_require (parser, CPP_EQ, RT_EQ);
17685   /* Look for the qualified-namespace-specifier.  */
17686   namespace_specifier
17687     = cp_parser_qualified_namespace_specifier (parser);
17688   /* Look for the `;' token.  */
17689   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17690
17691   /* Register the alias in the symbol table.  */
17692   do_namespace_alias (identifier, namespace_specifier);
17693 }
17694
17695 /* Parse a qualified-namespace-specifier.
17696
17697    qualified-namespace-specifier:
17698      :: [opt] nested-name-specifier [opt] namespace-name
17699
17700    Returns a NAMESPACE_DECL corresponding to the specified
17701    namespace.  */
17702
17703 static tree
17704 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17705 {
17706   /* Look for the optional `::'.  */
17707   cp_parser_global_scope_opt (parser,
17708                               /*current_scope_valid_p=*/false);
17709
17710   /* Look for the optional nested-name-specifier.  */
17711   cp_parser_nested_name_specifier_opt (parser,
17712                                        /*typename_keyword_p=*/false,
17713                                        /*check_dependency_p=*/true,
17714                                        /*type_p=*/false,
17715                                        /*is_declaration=*/true);
17716
17717   return cp_parser_namespace_name (parser);
17718 }
17719
17720 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17721    access declaration.
17722
17723    using-declaration:
17724      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17725      using :: unqualified-id ;  
17726
17727    access-declaration:
17728      qualified-id ;  
17729
17730    */
17731
17732 static bool
17733 cp_parser_using_declaration (cp_parser* parser, 
17734                              bool access_declaration_p)
17735 {
17736   cp_token *token;
17737   bool typename_p = false;
17738   bool global_scope_p;
17739   tree decl;
17740   tree identifier;
17741   tree qscope;
17742   int oldcount = errorcount;
17743   cp_token *diag_token = NULL;
17744
17745   if (access_declaration_p)
17746     {
17747       diag_token = cp_lexer_peek_token (parser->lexer);
17748       cp_parser_parse_tentatively (parser);
17749     }
17750   else
17751     {
17752       /* Look for the `using' keyword.  */
17753       cp_parser_require_keyword (parser, RID_USING, RT_USING);
17754       
17755       /* Peek at the next token.  */
17756       token = cp_lexer_peek_token (parser->lexer);
17757       /* See if it's `typename'.  */
17758       if (token->keyword == RID_TYPENAME)
17759         {
17760           /* Remember that we've seen it.  */
17761           typename_p = true;
17762           /* Consume the `typename' token.  */
17763           cp_lexer_consume_token (parser->lexer);
17764         }
17765     }
17766
17767   /* Look for the optional global scope qualification.  */
17768   global_scope_p
17769     = (cp_parser_global_scope_opt (parser,
17770                                    /*current_scope_valid_p=*/false)
17771        != NULL_TREE);
17772
17773   /* If we saw `typename', or didn't see `::', then there must be a
17774      nested-name-specifier present.  */
17775   if (typename_p || !global_scope_p)
17776     {
17777       qscope = cp_parser_nested_name_specifier (parser, typename_p,
17778                                                 /*check_dependency_p=*/true,
17779                                                 /*type_p=*/false,
17780                                                 /*is_declaration=*/true);
17781       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17782         {
17783           cp_parser_skip_to_end_of_block_or_statement (parser);
17784           return false;
17785         }
17786     }
17787   /* Otherwise, we could be in either of the two productions.  In that
17788      case, treat the nested-name-specifier as optional.  */
17789   else
17790     qscope = cp_parser_nested_name_specifier_opt (parser,
17791                                                   /*typename_keyword_p=*/false,
17792                                                   /*check_dependency_p=*/true,
17793                                                   /*type_p=*/false,
17794                                                   /*is_declaration=*/true);
17795   if (!qscope)
17796     qscope = global_namespace;
17797   else if (UNSCOPED_ENUM_P (qscope))
17798     qscope = CP_TYPE_CONTEXT (qscope);
17799
17800   if (access_declaration_p && cp_parser_error_occurred (parser))
17801     /* Something has already gone wrong; there's no need to parse
17802        further.  Since an error has occurred, the return value of
17803        cp_parser_parse_definitely will be false, as required.  */
17804     return cp_parser_parse_definitely (parser);
17805
17806   token = cp_lexer_peek_token (parser->lexer);
17807   /* Parse the unqualified-id.  */
17808   identifier = cp_parser_unqualified_id (parser,
17809                                          /*template_keyword_p=*/false,
17810                                          /*check_dependency_p=*/true,
17811                                          /*declarator_p=*/true,
17812                                          /*optional_p=*/false);
17813
17814   if (access_declaration_p)
17815     {
17816       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17817         cp_parser_simulate_error (parser);
17818       if (!cp_parser_parse_definitely (parser))
17819         return false;
17820     }
17821
17822   /* The function we call to handle a using-declaration is different
17823      depending on what scope we are in.  */
17824   if (qscope == error_mark_node || identifier == error_mark_node)
17825     ;
17826   else if (!identifier_p (identifier)
17827            && TREE_CODE (identifier) != BIT_NOT_EXPR)
17828     /* [namespace.udecl]
17829
17830        A using declaration shall not name a template-id.  */
17831     error_at (token->location,
17832               "a template-id may not appear in a using-declaration");
17833   else
17834     {
17835       if (at_class_scope_p ())
17836         {
17837           /* Create the USING_DECL.  */
17838           decl = do_class_using_decl (parser->scope, identifier);
17839
17840           if (decl && typename_p)
17841             USING_DECL_TYPENAME_P (decl) = 1;
17842
17843           if (check_for_bare_parameter_packs (decl))
17844             {
17845               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17846               return false;
17847             }
17848           else
17849             /* Add it to the list of members in this class.  */
17850             finish_member_declaration (decl);
17851         }
17852       else
17853         {
17854           decl = cp_parser_lookup_name_simple (parser,
17855                                                identifier,
17856                                                token->location);
17857           if (decl == error_mark_node)
17858             cp_parser_name_lookup_error (parser, identifier,
17859                                          decl, NLE_NULL,
17860                                          token->location);
17861           else if (check_for_bare_parameter_packs (decl))
17862             {
17863               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17864               return false;
17865             }
17866           else if (!at_namespace_scope_p ())
17867             do_local_using_decl (decl, qscope, identifier);
17868           else
17869             do_toplevel_using_decl (decl, qscope, identifier);
17870         }
17871     }
17872
17873   /* Look for the final `;'.  */
17874   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17875
17876   if (access_declaration_p && errorcount == oldcount)
17877     warning_at (diag_token->location, OPT_Wdeprecated,
17878                 "access declarations are deprecated "
17879                 "in favour of using-declarations; "
17880                 "suggestion: add the %<using%> keyword");
17881
17882   return true;
17883 }
17884
17885 /* Parse an alias-declaration.
17886
17887    alias-declaration:
17888      using identifier attribute-specifier-seq [opt] = type-id  */
17889
17890 static tree
17891 cp_parser_alias_declaration (cp_parser* parser)
17892 {
17893   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17894   location_t id_location;
17895   cp_declarator *declarator;
17896   cp_decl_specifier_seq decl_specs;
17897   bool member_p;
17898   const char *saved_message = NULL;
17899
17900   /* Look for the `using' keyword.  */
17901   cp_token *using_token
17902     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17903   if (using_token == NULL)
17904     return error_mark_node;
17905
17906   id_location = cp_lexer_peek_token (parser->lexer)->location;
17907   id = cp_parser_identifier (parser);
17908   if (id == error_mark_node)
17909     return error_mark_node;
17910
17911   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17912   attributes = cp_parser_attributes_opt (parser);
17913   if (attributes == error_mark_node)
17914     return error_mark_node;
17915
17916   cp_parser_require (parser, CPP_EQ, RT_EQ);
17917
17918   if (cp_parser_error_occurred (parser))
17919     return error_mark_node;
17920
17921   cp_parser_commit_to_tentative_parse (parser);
17922
17923   /* Now we are going to parse the type-id of the declaration.  */
17924
17925   /*
17926     [dcl.type]/3 says:
17927
17928         "A type-specifier-seq shall not define a class or enumeration
17929          unless it appears in the type-id of an alias-declaration (7.1.3) that
17930          is not the declaration of a template-declaration."
17931
17932     In other words, if we currently are in an alias template, the
17933     type-id should not define a type.
17934
17935     So let's set parser->type_definition_forbidden_message in that
17936     case; cp_parser_check_type_definition (called by
17937     cp_parser_class_specifier) will then emit an error if a type is
17938     defined in the type-id.  */
17939   if (parser->num_template_parameter_lists)
17940     {
17941       saved_message = parser->type_definition_forbidden_message;
17942       parser->type_definition_forbidden_message =
17943         G_("types may not be defined in alias template declarations");
17944     }
17945
17946   type = cp_parser_type_id (parser);
17947
17948   /* Restore the error message if need be.  */
17949   if (parser->num_template_parameter_lists)
17950     parser->type_definition_forbidden_message = saved_message;
17951
17952   if (type == error_mark_node
17953       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17954     {
17955       cp_parser_skip_to_end_of_block_or_statement (parser);
17956       return error_mark_node;
17957     }
17958
17959   /* A typedef-name can also be introduced by an alias-declaration. The
17960      identifier following the using keyword becomes a typedef-name. It has
17961      the same semantics as if it were introduced by the typedef
17962      specifier. In particular, it does not define a new type and it shall
17963      not appear in the type-id.  */
17964
17965   clear_decl_specs (&decl_specs);
17966   decl_specs.type = type;
17967   if (attributes != NULL_TREE)
17968     {
17969       decl_specs.attributes = attributes;
17970       set_and_check_decl_spec_loc (&decl_specs,
17971                                    ds_attribute,
17972                                    attrs_token);
17973     }
17974   set_and_check_decl_spec_loc (&decl_specs,
17975                                ds_typedef,
17976                                using_token);
17977   set_and_check_decl_spec_loc (&decl_specs,
17978                                ds_alias,
17979                                using_token);
17980
17981   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17982   declarator->id_loc = id_location;
17983
17984   member_p = at_class_scope_p ();
17985   if (member_p)
17986     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17987                       NULL_TREE, attributes);
17988   else
17989     decl = start_decl (declarator, &decl_specs, 0,
17990                        attributes, NULL_TREE, &pushed_scope);
17991   if (decl == error_mark_node)
17992     return decl;
17993
17994   // Attach constraints to the alias declaration.
17995   if (flag_concepts && current_template_parms)
17996     {
17997       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17998       tree constr = build_constraints (reqs, NULL_TREE);
17999       set_constraints (decl, constr);
18000     }
18001
18002   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18003
18004   if (pushed_scope)
18005     pop_scope (pushed_scope);
18006
18007   /* If decl is a template, return its TEMPLATE_DECL so that it gets
18008      added into the symbol table; otherwise, return the TYPE_DECL.  */
18009   if (DECL_LANG_SPECIFIC (decl)
18010       && DECL_TEMPLATE_INFO (decl)
18011       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18012     {
18013       decl = DECL_TI_TEMPLATE (decl);
18014       if (member_p)
18015         check_member_template (decl);
18016     }
18017
18018   return decl;
18019 }
18020
18021 /* Parse a using-directive.
18022
18023    using-directive:
18024      using namespace :: [opt] nested-name-specifier [opt]
18025        namespace-name ;  */
18026
18027 static void
18028 cp_parser_using_directive (cp_parser* parser)
18029 {
18030   tree namespace_decl;
18031   tree attribs;
18032
18033   /* Look for the `using' keyword.  */
18034   cp_parser_require_keyword (parser, RID_USING, RT_USING);
18035   /* And the `namespace' keyword.  */
18036   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18037   /* Look for the optional `::' operator.  */
18038   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18039   /* And the optional nested-name-specifier.  */
18040   cp_parser_nested_name_specifier_opt (parser,
18041                                        /*typename_keyword_p=*/false,
18042                                        /*check_dependency_p=*/true,
18043                                        /*type_p=*/false,
18044                                        /*is_declaration=*/true);
18045   /* Get the namespace being used.  */
18046   namespace_decl = cp_parser_namespace_name (parser);
18047   /* And any specified attributes.  */
18048   attribs = cp_parser_attributes_opt (parser);
18049   /* Update the symbol table.  */
18050   parse_using_directive (namespace_decl, attribs);
18051   /* Look for the final `;'.  */
18052   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18053 }
18054
18055 /* Parse an asm-definition.
18056
18057    asm-definition:
18058      asm ( string-literal ) ;
18059
18060    GNU Extension:
18061
18062    asm-definition:
18063      asm volatile [opt] ( string-literal ) ;
18064      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18065      asm volatile [opt] ( string-literal : asm-operand-list [opt]
18066                           : asm-operand-list [opt] ) ;
18067      asm volatile [opt] ( string-literal : asm-operand-list [opt]
18068                           : asm-operand-list [opt]
18069                           : asm-clobber-list [opt] ) ;
18070      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18071                                : asm-clobber-list [opt]
18072                                : asm-goto-list ) ;  */
18073
18074 static void
18075 cp_parser_asm_definition (cp_parser* parser)
18076 {
18077   tree string;
18078   tree outputs = NULL_TREE;
18079   tree inputs = NULL_TREE;
18080   tree clobbers = NULL_TREE;
18081   tree labels = NULL_TREE;
18082   tree asm_stmt;
18083   bool volatile_p = false;
18084   bool extended_p = false;
18085   bool invalid_inputs_p = false;
18086   bool invalid_outputs_p = false;
18087   bool goto_p = false;
18088   required_token missing = RT_NONE;
18089
18090   /* Look for the `asm' keyword.  */
18091   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18092
18093   if (parser->in_function_body
18094       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18095     {
18096       error ("%<asm%> in %<constexpr%> function");
18097       cp_function_chain->invalid_constexpr = true;
18098     }
18099
18100   /* See if the next token is `volatile'.  */
18101   if (cp_parser_allow_gnu_extensions_p (parser)
18102       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18103     {
18104       /* Remember that we saw the `volatile' keyword.  */
18105       volatile_p = true;
18106       /* Consume the token.  */
18107       cp_lexer_consume_token (parser->lexer);
18108     }
18109   if (cp_parser_allow_gnu_extensions_p (parser)
18110       && parser->in_function_body
18111       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18112     {
18113       /* Remember that we saw the `goto' keyword.  */
18114       goto_p = true;
18115       /* Consume the token.  */
18116       cp_lexer_consume_token (parser->lexer);
18117     }
18118   /* Look for the opening `('.  */
18119   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18120     return;
18121   /* Look for the string.  */
18122   string = cp_parser_string_literal (parser, false, false);
18123   if (string == error_mark_node)
18124     {
18125       cp_parser_skip_to_closing_parenthesis (parser, true, false,
18126                                              /*consume_paren=*/true);
18127       return;
18128     }
18129
18130   /* If we're allowing GNU extensions, check for the extended assembly
18131      syntax.  Unfortunately, the `:' tokens need not be separated by
18132      a space in C, and so, for compatibility, we tolerate that here
18133      too.  Doing that means that we have to treat the `::' operator as
18134      two `:' tokens.  */
18135   if (cp_parser_allow_gnu_extensions_p (parser)
18136       && parser->in_function_body
18137       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18138           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18139     {
18140       bool inputs_p = false;
18141       bool clobbers_p = false;
18142       bool labels_p = false;
18143
18144       /* The extended syntax was used.  */
18145       extended_p = true;
18146
18147       /* Look for outputs.  */
18148       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18149         {
18150           /* Consume the `:'.  */
18151           cp_lexer_consume_token (parser->lexer);
18152           /* Parse the output-operands.  */
18153           if (cp_lexer_next_token_is_not (parser->lexer,
18154                                           CPP_COLON)
18155               && cp_lexer_next_token_is_not (parser->lexer,
18156                                              CPP_SCOPE)
18157               && cp_lexer_next_token_is_not (parser->lexer,
18158                                              CPP_CLOSE_PAREN)
18159               && !goto_p)
18160             {
18161               outputs = cp_parser_asm_operand_list (parser);
18162               if (outputs == error_mark_node)
18163                 invalid_outputs_p = true;
18164             }
18165         }
18166       /* If the next token is `::', there are no outputs, and the
18167          next token is the beginning of the inputs.  */
18168       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18169         /* The inputs are coming next.  */
18170         inputs_p = true;
18171
18172       /* Look for inputs.  */
18173       if (inputs_p
18174           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18175         {
18176           /* Consume the `:' or `::'.  */
18177           cp_lexer_consume_token (parser->lexer);
18178           /* Parse the output-operands.  */
18179           if (cp_lexer_next_token_is_not (parser->lexer,
18180                                           CPP_COLON)
18181               && cp_lexer_next_token_is_not (parser->lexer,
18182                                              CPP_SCOPE)
18183               && cp_lexer_next_token_is_not (parser->lexer,
18184                                              CPP_CLOSE_PAREN))
18185             {
18186               inputs = cp_parser_asm_operand_list (parser);
18187               if (inputs == error_mark_node)
18188                 invalid_inputs_p = true;
18189             }
18190         }
18191       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18192         /* The clobbers are coming next.  */
18193         clobbers_p = true;
18194
18195       /* Look for clobbers.  */
18196       if (clobbers_p
18197           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18198         {
18199           clobbers_p = true;
18200           /* Consume the `:' or `::'.  */
18201           cp_lexer_consume_token (parser->lexer);
18202           /* Parse the clobbers.  */
18203           if (cp_lexer_next_token_is_not (parser->lexer,
18204                                           CPP_COLON)
18205               && cp_lexer_next_token_is_not (parser->lexer,
18206                                              CPP_CLOSE_PAREN))
18207             clobbers = cp_parser_asm_clobber_list (parser);
18208         }
18209       else if (goto_p
18210                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18211         /* The labels are coming next.  */
18212         labels_p = true;
18213
18214       /* Look for labels.  */
18215       if (labels_p
18216           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18217         {
18218           labels_p = true;
18219           /* Consume the `:' or `::'.  */
18220           cp_lexer_consume_token (parser->lexer);
18221           /* Parse the labels.  */
18222           labels = cp_parser_asm_label_list (parser);
18223         }
18224
18225       if (goto_p && !labels_p)
18226         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18227     }
18228   else if (goto_p)
18229     missing = RT_COLON_SCOPE;
18230
18231   /* Look for the closing `)'.  */
18232   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18233                           missing ? missing : RT_CLOSE_PAREN))
18234     cp_parser_skip_to_closing_parenthesis (parser, true, false,
18235                                            /*consume_paren=*/true);
18236   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18237
18238   if (!invalid_inputs_p && !invalid_outputs_p)
18239     {
18240       /* Create the ASM_EXPR.  */
18241       if (parser->in_function_body)
18242         {
18243           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18244                                       inputs, clobbers, labels);
18245           /* If the extended syntax was not used, mark the ASM_EXPR.  */
18246           if (!extended_p)
18247             {
18248               tree temp = asm_stmt;
18249               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18250                 temp = TREE_OPERAND (temp, 0);
18251
18252               ASM_INPUT_P (temp) = 1;
18253             }
18254         }
18255       else
18256         symtab->finalize_toplevel_asm (string);
18257     }
18258 }
18259
18260 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18261    type that comes from the decl-specifier-seq.  */
18262
18263 static tree
18264 strip_declarator_types (tree type, cp_declarator *declarator)
18265 {
18266   for (cp_declarator *d = declarator; d;)
18267     switch (d->kind)
18268       {
18269       case cdk_id:
18270       case cdk_error:
18271         d = NULL;
18272         break;
18273
18274       default:
18275         if (TYPE_PTRMEMFUNC_P (type))
18276           type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18277         type = TREE_TYPE (type);
18278         d = d->declarator;
18279         break;
18280       }
18281
18282   return type;
18283 }
18284
18285 /* Declarators [gram.dcl.decl] */
18286
18287 /* Parse an init-declarator.
18288
18289    init-declarator:
18290      declarator initializer [opt]
18291
18292    GNU Extension:
18293
18294    init-declarator:
18295      declarator asm-specification [opt] attributes [opt] initializer [opt]
18296
18297    function-definition:
18298      decl-specifier-seq [opt] declarator ctor-initializer [opt]
18299        function-body
18300      decl-specifier-seq [opt] declarator function-try-block
18301
18302    GNU Extension:
18303
18304    function-definition:
18305      __extension__ function-definition
18306
18307    TM Extension:
18308
18309    function-definition:
18310      decl-specifier-seq [opt] declarator function-transaction-block
18311
18312    The DECL_SPECIFIERS apply to this declarator.  Returns a
18313    representation of the entity declared.  If MEMBER_P is TRUE, then
18314    this declarator appears in a class scope.  The new DECL created by
18315    this declarator is returned.
18316
18317    The CHECKS are access checks that should be performed once we know
18318    what entity is being declared (and, therefore, what classes have
18319    befriended it).
18320
18321    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18322    for a function-definition here as well.  If the declarator is a
18323    declarator for a function-definition, *FUNCTION_DEFINITION_P will
18324    be TRUE upon return.  By that point, the function-definition will
18325    have been completely parsed.
18326
18327    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18328    is FALSE.
18329
18330    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18331    parsed declaration if it is an uninitialized single declarator not followed
18332    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18333    if present, will not be consumed.  If returned, this declarator will be
18334    created with SD_INITIALIZED but will not call cp_finish_decl.
18335
18336    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18337    and there is an initializer, the pointed location_t is set to the
18338    location of the '=' or `(', or '{' in C++11 token introducing the
18339    initializer.  */
18340
18341 static tree
18342 cp_parser_init_declarator (cp_parser* parser,
18343                            cp_decl_specifier_seq *decl_specifiers,
18344                            vec<deferred_access_check, va_gc> *checks,
18345                            bool function_definition_allowed_p,
18346                            bool member_p,
18347                            int declares_class_or_enum,
18348                            bool* function_definition_p,
18349                            tree* maybe_range_for_decl,
18350                            location_t* init_loc,
18351                            tree* auto_result)
18352 {
18353   cp_token *token = NULL, *asm_spec_start_token = NULL,
18354            *attributes_start_token = NULL;
18355   cp_declarator *declarator;
18356   tree prefix_attributes;
18357   tree attributes = NULL;
18358   tree asm_specification;
18359   tree initializer;
18360   tree decl = NULL_TREE;
18361   tree scope;
18362   int is_initialized;
18363   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
18364      initialized with "= ..", CPP_OPEN_PAREN if initialized with
18365      "(...)".  */
18366   enum cpp_ttype initialization_kind;
18367   bool is_direct_init = false;
18368   bool is_non_constant_init;
18369   int ctor_dtor_or_conv_p;
18370   bool friend_p = cp_parser_friend_p (decl_specifiers);
18371   tree pushed_scope = NULL_TREE;
18372   bool range_for_decl_p = false;
18373   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18374   location_t tmp_init_loc = UNKNOWN_LOCATION;
18375
18376   /* Gather the attributes that were provided with the
18377      decl-specifiers.  */
18378   prefix_attributes = decl_specifiers->attributes;
18379
18380   /* Assume that this is not the declarator for a function
18381      definition.  */
18382   if (function_definition_p)
18383     *function_definition_p = false;
18384
18385   /* Default arguments are only permitted for function parameters.  */
18386   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18387     parser->default_arg_ok_p = false;
18388
18389   /* Defer access checks while parsing the declarator; we cannot know
18390      what names are accessible until we know what is being
18391      declared.  */
18392   resume_deferring_access_checks ();
18393
18394   /* Parse the declarator.  */
18395   token = cp_lexer_peek_token (parser->lexer);
18396   declarator
18397     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18398                             &ctor_dtor_or_conv_p,
18399                             /*parenthesized_p=*/NULL,
18400                             member_p, friend_p);
18401   /* Gather up the deferred checks.  */
18402   stop_deferring_access_checks ();
18403
18404   parser->default_arg_ok_p = saved_default_arg_ok_p;
18405
18406   /* If the DECLARATOR was erroneous, there's no need to go
18407      further.  */
18408   if (declarator == cp_error_declarator)
18409     return error_mark_node;
18410
18411   /* Check that the number of template-parameter-lists is OK.  */
18412   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18413                                                        token->location))
18414     return error_mark_node;
18415
18416   if (declares_class_or_enum & 2)
18417     cp_parser_check_for_definition_in_return_type (declarator,
18418                                                    decl_specifiers->type,
18419                                                    decl_specifiers->locations[ds_type_spec]);
18420
18421   /* Figure out what scope the entity declared by the DECLARATOR is
18422      located in.  `grokdeclarator' sometimes changes the scope, so
18423      we compute it now.  */
18424   scope = get_scope_of_declarator (declarator);
18425
18426   /* Perform any lookups in the declared type which were thought to be
18427      dependent, but are not in the scope of the declarator.  */
18428   decl_specifiers->type
18429     = maybe_update_decl_type (decl_specifiers->type, scope);
18430
18431   /* If we're allowing GNU extensions, look for an
18432      asm-specification.  */
18433   if (cp_parser_allow_gnu_extensions_p (parser))
18434     {
18435       /* Look for an asm-specification.  */
18436       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18437       asm_specification = cp_parser_asm_specification_opt (parser);
18438     }
18439   else
18440     asm_specification = NULL_TREE;
18441
18442   /* Look for attributes.  */
18443   attributes_start_token = cp_lexer_peek_token (parser->lexer);
18444   attributes = cp_parser_attributes_opt (parser);
18445
18446   /* Peek at the next token.  */
18447   token = cp_lexer_peek_token (parser->lexer);
18448
18449   bool bogus_implicit_tmpl = false;
18450
18451   if (function_declarator_p (declarator))
18452     {
18453       /* Check to see if the token indicates the start of a
18454          function-definition.  */
18455       if (cp_parser_token_starts_function_definition_p (token))
18456         {
18457           if (!function_definition_allowed_p)
18458             {
18459               /* If a function-definition should not appear here, issue an
18460                  error message.  */
18461               cp_parser_error (parser,
18462                                "a function-definition is not allowed here");
18463               return error_mark_node;
18464             }
18465
18466           location_t func_brace_location
18467             = cp_lexer_peek_token (parser->lexer)->location;
18468
18469           /* Neither attributes nor an asm-specification are allowed
18470              on a function-definition.  */
18471           if (asm_specification)
18472             error_at (asm_spec_start_token->location,
18473                       "an asm-specification is not allowed "
18474                       "on a function-definition");
18475           if (attributes)
18476             error_at (attributes_start_token->location,
18477                       "attributes are not allowed "
18478                       "on a function-definition");
18479           /* This is a function-definition.  */
18480           *function_definition_p = true;
18481
18482           /* Parse the function definition.  */
18483           if (member_p)
18484             decl = cp_parser_save_member_function_body (parser,
18485                                                         decl_specifiers,
18486                                                         declarator,
18487                                                         prefix_attributes);
18488           else
18489             decl =
18490               (cp_parser_function_definition_from_specifiers_and_declarator
18491                (parser, decl_specifiers, prefix_attributes, declarator));
18492
18493           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18494             {
18495               /* This is where the prologue starts...  */
18496               DECL_STRUCT_FUNCTION (decl)->function_start_locus
18497                 = func_brace_location;
18498             }
18499
18500           return decl;
18501         }
18502     }
18503   else if (parser->fully_implicit_function_template_p)
18504     {
18505       /* A non-template declaration involving a function parameter list
18506          containing an implicit template parameter will be made into a
18507          template.  If the resulting declaration is not going to be an
18508          actual function then finish the template scope here to prevent it.
18509          An error message will be issued once we have a decl to talk about.
18510
18511          FIXME probably we should do type deduction rather than create an
18512          implicit template, but the standard currently doesn't allow it. */
18513       bogus_implicit_tmpl = true;
18514       finish_fully_implicit_template (parser, NULL_TREE);
18515     }
18516
18517   /* [dcl.dcl]
18518
18519      Only in function declarations for constructors, destructors, and
18520      type conversions can the decl-specifier-seq be omitted.
18521
18522      We explicitly postpone this check past the point where we handle
18523      function-definitions because we tolerate function-definitions
18524      that are missing their return types in some modes.  */
18525   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18526     {
18527       cp_parser_error (parser,
18528                        "expected constructor, destructor, or type conversion");
18529       return error_mark_node;
18530     }
18531
18532   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
18533   if (token->type == CPP_EQ
18534       || token->type == CPP_OPEN_PAREN
18535       || token->type == CPP_OPEN_BRACE)
18536     {
18537       is_initialized = SD_INITIALIZED;
18538       initialization_kind = token->type;
18539       if (maybe_range_for_decl)
18540         *maybe_range_for_decl = error_mark_node;
18541       tmp_init_loc = token->location;
18542       if (init_loc && *init_loc == UNKNOWN_LOCATION)
18543         *init_loc = tmp_init_loc;
18544
18545       if (token->type == CPP_EQ
18546           && function_declarator_p (declarator))
18547         {
18548           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18549           if (t2->keyword == RID_DEFAULT)
18550             is_initialized = SD_DEFAULTED;
18551           else if (t2->keyword == RID_DELETE)
18552             is_initialized = SD_DELETED;
18553         }
18554     }
18555   else
18556     {
18557       /* If the init-declarator isn't initialized and isn't followed by a
18558          `,' or `;', it's not a valid init-declarator.  */
18559       if (token->type != CPP_COMMA
18560           && token->type != CPP_SEMICOLON)
18561         {
18562           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18563             range_for_decl_p = true;
18564           else
18565             {
18566               if (!maybe_range_for_decl)
18567                 cp_parser_error (parser, "expected initializer");
18568               return error_mark_node;
18569             }
18570         }
18571       is_initialized = SD_UNINITIALIZED;
18572       initialization_kind = CPP_EOF;
18573     }
18574
18575   /* Because start_decl has side-effects, we should only call it if we
18576      know we're going ahead.  By this point, we know that we cannot
18577      possibly be looking at any other construct.  */
18578   cp_parser_commit_to_tentative_parse (parser);
18579
18580   /* Enter the newly declared entry in the symbol table.  If we're
18581      processing a declaration in a class-specifier, we wait until
18582      after processing the initializer.  */
18583   if (!member_p)
18584     {
18585       if (parser->in_unbraced_linkage_specification_p)
18586         decl_specifiers->storage_class = sc_extern;
18587       decl = start_decl (declarator, decl_specifiers,
18588                          range_for_decl_p? SD_INITIALIZED : is_initialized,
18589                          attributes, prefix_attributes, &pushed_scope);
18590       cp_finalize_omp_declare_simd (parser, decl);
18591       cp_finalize_oacc_routine (parser, decl, false);
18592       /* Adjust location of decl if declarator->id_loc is more appropriate:
18593          set, and decl wasn't merged with another decl, in which case its
18594          location would be different from input_location, and more accurate.  */
18595       if (DECL_P (decl)
18596           && declarator->id_loc != UNKNOWN_LOCATION
18597           && DECL_SOURCE_LOCATION (decl) == input_location)
18598         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18599     }
18600   else if (scope)
18601     /* Enter the SCOPE.  That way unqualified names appearing in the
18602        initializer will be looked up in SCOPE.  */
18603     pushed_scope = push_scope (scope);
18604
18605   /* Perform deferred access control checks, now that we know in which
18606      SCOPE the declared entity resides.  */
18607   if (!member_p && decl)
18608     {
18609       tree saved_current_function_decl = NULL_TREE;
18610
18611       /* If the entity being declared is a function, pretend that we
18612          are in its scope.  If it is a `friend', it may have access to
18613          things that would not otherwise be accessible.  */
18614       if (TREE_CODE (decl) == FUNCTION_DECL)
18615         {
18616           saved_current_function_decl = current_function_decl;
18617           current_function_decl = decl;
18618         }
18619
18620       /* Perform access checks for template parameters.  */
18621       cp_parser_perform_template_parameter_access_checks (checks);
18622
18623       /* Perform the access control checks for the declarator and the
18624          decl-specifiers.  */
18625       perform_deferred_access_checks (tf_warning_or_error);
18626
18627       /* Restore the saved value.  */
18628       if (TREE_CODE (decl) == FUNCTION_DECL)
18629         current_function_decl = saved_current_function_decl;
18630     }
18631
18632   /* Parse the initializer.  */
18633   initializer = NULL_TREE;
18634   is_direct_init = false;
18635   is_non_constant_init = true;
18636   if (is_initialized)
18637     {
18638       if (function_declarator_p (declarator))
18639         {
18640            if (initialization_kind == CPP_EQ)
18641              initializer = cp_parser_pure_specifier (parser);
18642            else
18643              {
18644                /* If the declaration was erroneous, we don't really
18645                   know what the user intended, so just silently
18646                   consume the initializer.  */
18647                if (decl != error_mark_node)
18648                  error_at (tmp_init_loc, "initializer provided for function");
18649                cp_parser_skip_to_closing_parenthesis (parser,
18650                                                       /*recovering=*/true,
18651                                                       /*or_comma=*/false,
18652                                                       /*consume_paren=*/true);
18653              }
18654         }
18655       else
18656         {
18657           /* We want to record the extra mangling scope for in-class
18658              initializers of class members and initializers of static data
18659              member templates.  The former involves deferring
18660              parsing of the initializer until end of class as with default
18661              arguments.  So right here we only handle the latter.  */
18662           if (!member_p && processing_template_decl)
18663             start_lambda_scope (decl);
18664           initializer = cp_parser_initializer (parser,
18665                                                &is_direct_init,
18666                                                &is_non_constant_init);
18667           if (!member_p && processing_template_decl)
18668             finish_lambda_scope ();
18669           if (initializer == error_mark_node)
18670             cp_parser_skip_to_end_of_statement (parser);
18671         }
18672     }
18673
18674   /* The old parser allows attributes to appear after a parenthesized
18675      initializer.  Mark Mitchell proposed removing this functionality
18676      on the GCC mailing lists on 2002-08-13.  This parser accepts the
18677      attributes -- but ignores them.  */
18678   if (cp_parser_allow_gnu_extensions_p (parser)
18679       && initialization_kind == CPP_OPEN_PAREN)
18680     if (cp_parser_attributes_opt (parser))
18681       warning (OPT_Wattributes,
18682                "attributes after parenthesized initializer ignored");
18683
18684   /* And now complain about a non-function implicit template.  */
18685   if (bogus_implicit_tmpl && decl != error_mark_node)
18686     error_at (DECL_SOURCE_LOCATION (decl),
18687               "non-function %qD declared as implicit template", decl);
18688
18689   /* For an in-class declaration, use `grokfield' to create the
18690      declaration.  */
18691   if (member_p)
18692     {
18693       if (pushed_scope)
18694         {
18695           pop_scope (pushed_scope);
18696           pushed_scope = NULL_TREE;
18697         }
18698       decl = grokfield (declarator, decl_specifiers,
18699                         initializer, !is_non_constant_init,
18700                         /*asmspec=*/NULL_TREE,
18701                         chainon (attributes, prefix_attributes));
18702       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18703         cp_parser_save_default_args (parser, decl);
18704       cp_finalize_omp_declare_simd (parser, decl);
18705       cp_finalize_oacc_routine (parser, decl, false);
18706     }
18707
18708   /* Finish processing the declaration.  But, skip member
18709      declarations.  */
18710   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18711     {
18712       cp_finish_decl (decl,
18713                       initializer, !is_non_constant_init,
18714                       asm_specification,
18715                       /* If the initializer is in parentheses, then this is
18716                          a direct-initialization, which means that an
18717                          `explicit' constructor is OK.  Otherwise, an
18718                          `explicit' constructor cannot be used.  */
18719                       ((is_direct_init || !is_initialized)
18720                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18721     }
18722   else if ((cxx_dialect != cxx98) && friend_p
18723            && decl && TREE_CODE (decl) == FUNCTION_DECL)
18724     /* Core issue #226 (C++0x only): A default template-argument
18725        shall not be specified in a friend class template
18726        declaration. */
18727     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
18728                              /*is_partial=*/false, /*is_friend_decl=*/1);
18729
18730   if (!friend_p && pushed_scope)
18731     pop_scope (pushed_scope);
18732
18733   if (function_declarator_p (declarator)
18734       && parser->fully_implicit_function_template_p)
18735     {
18736       if (member_p)
18737         decl = finish_fully_implicit_template (parser, decl);
18738       else
18739         finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18740     }
18741
18742   if (auto_result && is_initialized && decl_specifiers->type
18743       && type_uses_auto (decl_specifiers->type))
18744     *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18745
18746   return decl;
18747 }
18748
18749 /* Parse a declarator.
18750
18751    declarator:
18752      direct-declarator
18753      ptr-operator declarator
18754
18755    abstract-declarator:
18756      ptr-operator abstract-declarator [opt]
18757      direct-abstract-declarator
18758
18759    GNU Extensions:
18760
18761    declarator:
18762      attributes [opt] direct-declarator
18763      attributes [opt] ptr-operator declarator
18764
18765    abstract-declarator:
18766      attributes [opt] ptr-operator abstract-declarator [opt]
18767      attributes [opt] direct-abstract-declarator
18768
18769    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18770    detect constructor, destructor or conversion operators. It is set
18771    to -1 if the declarator is a name, and +1 if it is a
18772    function. Otherwise it is set to zero. Usually you just want to
18773    test for >0, but internally the negative value is used.
18774
18775    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18776    a decl-specifier-seq unless it declares a constructor, destructor,
18777    or conversion.  It might seem that we could check this condition in
18778    semantic analysis, rather than parsing, but that makes it difficult
18779    to handle something like `f()'.  We want to notice that there are
18780    no decl-specifiers, and therefore realize that this is an
18781    expression, not a declaration.)
18782
18783    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18784    the declarator is a direct-declarator of the form "(...)".
18785
18786    MEMBER_P is true iff this declarator is a member-declarator.
18787
18788    FRIEND_P is true iff this declarator is a friend.  */
18789
18790 static cp_declarator *
18791 cp_parser_declarator (cp_parser* parser,
18792                       cp_parser_declarator_kind dcl_kind,
18793                       int* ctor_dtor_or_conv_p,
18794                       bool* parenthesized_p,
18795                       bool member_p, bool friend_p)
18796 {
18797   cp_declarator *declarator;
18798   enum tree_code code;
18799   cp_cv_quals cv_quals;
18800   tree class_type;
18801   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18802
18803   /* Assume this is not a constructor, destructor, or type-conversion
18804      operator.  */
18805   if (ctor_dtor_or_conv_p)
18806     *ctor_dtor_or_conv_p = 0;
18807
18808   if (cp_parser_allow_gnu_extensions_p (parser))
18809     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18810
18811   /* Check for the ptr-operator production.  */
18812   cp_parser_parse_tentatively (parser);
18813   /* Parse the ptr-operator.  */
18814   code = cp_parser_ptr_operator (parser,
18815                                  &class_type,
18816                                  &cv_quals,
18817                                  &std_attributes);
18818
18819   /* If that worked, then we have a ptr-operator.  */
18820   if (cp_parser_parse_definitely (parser))
18821     {
18822       /* If a ptr-operator was found, then this declarator was not
18823          parenthesized.  */
18824       if (parenthesized_p)
18825         *parenthesized_p = true;
18826       /* The dependent declarator is optional if we are parsing an
18827          abstract-declarator.  */
18828       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18829         cp_parser_parse_tentatively (parser);
18830
18831       /* Parse the dependent declarator.  */
18832       declarator = cp_parser_declarator (parser, dcl_kind,
18833                                          /*ctor_dtor_or_conv_p=*/NULL,
18834                                          /*parenthesized_p=*/NULL,
18835                                          /*member_p=*/false,
18836                                          friend_p);
18837
18838       /* If we are parsing an abstract-declarator, we must handle the
18839          case where the dependent declarator is absent.  */
18840       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18841           && !cp_parser_parse_definitely (parser))
18842         declarator = NULL;
18843
18844       declarator = cp_parser_make_indirect_declarator
18845         (code, class_type, cv_quals, declarator, std_attributes);
18846     }
18847   /* Everything else is a direct-declarator.  */
18848   else
18849     {
18850       if (parenthesized_p)
18851         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18852                                                    CPP_OPEN_PAREN);
18853       declarator = cp_parser_direct_declarator (parser, dcl_kind,
18854                                                 ctor_dtor_or_conv_p,
18855                                                 member_p, friend_p);
18856     }
18857
18858   if (gnu_attributes && declarator && declarator != cp_error_declarator)
18859     declarator->attributes = gnu_attributes;
18860   return declarator;
18861 }
18862
18863 /* Parse a direct-declarator or direct-abstract-declarator.
18864
18865    direct-declarator:
18866      declarator-id
18867      direct-declarator ( parameter-declaration-clause )
18868        cv-qualifier-seq [opt]
18869        ref-qualifier [opt]
18870        exception-specification [opt]
18871      direct-declarator [ constant-expression [opt] ]
18872      ( declarator )
18873
18874    direct-abstract-declarator:
18875      direct-abstract-declarator [opt]
18876        ( parameter-declaration-clause )
18877        cv-qualifier-seq [opt]
18878        ref-qualifier [opt]
18879        exception-specification [opt]
18880      direct-abstract-declarator [opt] [ constant-expression [opt] ]
18881      ( abstract-declarator )
18882
18883    Returns a representation of the declarator.  DCL_KIND is
18884    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18885    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
18886    we are parsing a direct-declarator.  It is
18887    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18888    of ambiguity we prefer an abstract declarator, as per
18889    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18890    as for cp_parser_declarator.  */
18891
18892 static cp_declarator *
18893 cp_parser_direct_declarator (cp_parser* parser,
18894                              cp_parser_declarator_kind dcl_kind,
18895                              int* ctor_dtor_or_conv_p,
18896                              bool member_p, bool friend_p)
18897 {
18898   cp_token *token;
18899   cp_declarator *declarator = NULL;
18900   tree scope = NULL_TREE;
18901   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18902   bool saved_in_declarator_p = parser->in_declarator_p;
18903   bool first = true;
18904   tree pushed_scope = NULL_TREE;
18905
18906   while (true)
18907     {
18908       /* Peek at the next token.  */
18909       token = cp_lexer_peek_token (parser->lexer);
18910       if (token->type == CPP_OPEN_PAREN)
18911         {
18912           /* This is either a parameter-declaration-clause, or a
18913              parenthesized declarator. When we know we are parsing a
18914              named declarator, it must be a parenthesized declarator
18915              if FIRST is true. For instance, `(int)' is a
18916              parameter-declaration-clause, with an omitted
18917              direct-abstract-declarator. But `((*))', is a
18918              parenthesized abstract declarator. Finally, when T is a
18919              template parameter `(T)' is a
18920              parameter-declaration-clause, and not a parenthesized
18921              named declarator.
18922
18923              We first try and parse a parameter-declaration-clause,
18924              and then try a nested declarator (if FIRST is true).
18925
18926              It is not an error for it not to be a
18927              parameter-declaration-clause, even when FIRST is
18928              false. Consider,
18929
18930                int i (int);
18931                int i (3);
18932
18933              The first is the declaration of a function while the
18934              second is the definition of a variable, including its
18935              initializer.
18936
18937              Having seen only the parenthesis, we cannot know which of
18938              these two alternatives should be selected.  Even more
18939              complex are examples like:
18940
18941                int i (int (a));
18942                int i (int (3));
18943
18944              The former is a function-declaration; the latter is a
18945              variable initialization.
18946
18947              Thus again, we try a parameter-declaration-clause, and if
18948              that fails, we back out and return.  */
18949
18950           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18951             {
18952               tree params;
18953               bool is_declarator = false;
18954
18955               /* In a member-declarator, the only valid interpretation
18956                  of a parenthesis is the start of a
18957                  parameter-declaration-clause.  (It is invalid to
18958                  initialize a static data member with a parenthesized
18959                  initializer; only the "=" form of initialization is
18960                  permitted.)  */
18961               if (!member_p)
18962                 cp_parser_parse_tentatively (parser);
18963
18964               /* Consume the `('.  */
18965               cp_lexer_consume_token (parser->lexer);
18966               if (first)
18967                 {
18968                   /* If this is going to be an abstract declarator, we're
18969                      in a declarator and we can't have default args.  */
18970                   parser->default_arg_ok_p = false;
18971                   parser->in_declarator_p = true;
18972                 }
18973
18974               begin_scope (sk_function_parms, NULL_TREE);
18975
18976               /* Parse the parameter-declaration-clause.  */
18977               params = cp_parser_parameter_declaration_clause (parser);
18978
18979               /* Consume the `)'.  */
18980               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18981
18982               /* If all went well, parse the cv-qualifier-seq,
18983                  ref-qualifier and the exception-specification.  */
18984               if (member_p || cp_parser_parse_definitely (parser))
18985                 {
18986                   cp_cv_quals cv_quals;
18987                   cp_virt_specifiers virt_specifiers;
18988                   cp_ref_qualifier ref_qual;
18989                   tree exception_specification;
18990                   tree late_return;
18991                   tree attrs;
18992                   bool memfn = (member_p || (pushed_scope
18993                                              && CLASS_TYPE_P (pushed_scope)));
18994
18995                   is_declarator = true;
18996
18997                   if (ctor_dtor_or_conv_p)
18998                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18999                   first = false;
19000
19001                   /* Parse the cv-qualifier-seq.  */
19002                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19003                   /* Parse the ref-qualifier. */
19004                   ref_qual = cp_parser_ref_qualifier_opt (parser);
19005                   /* Parse the tx-qualifier.  */
19006                   tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19007                   /* And the exception-specification.  */
19008                   exception_specification
19009                     = cp_parser_exception_specification_opt (parser);
19010
19011                   attrs = cp_parser_std_attribute_spec_seq (parser);
19012
19013                   /* In here, we handle cases where attribute is used after
19014                      the function declaration.  For example:
19015                      void func (int x) __attribute__((vector(..)));  */
19016                   tree gnu_attrs = NULL_TREE;
19017                   if (flag_cilkplus
19018                       && cp_next_tokens_can_be_gnu_attribute_p (parser))
19019                     {
19020                       cp_parser_parse_tentatively (parser);
19021                       tree attr = cp_parser_gnu_attributes_opt (parser);
19022                       if (cp_lexer_next_token_is_not (parser->lexer,
19023                                                       CPP_SEMICOLON)
19024                           && cp_lexer_next_token_is_not (parser->lexer,
19025                                                          CPP_OPEN_BRACE))
19026                         cp_parser_abort_tentative_parse (parser);
19027                       else if (!cp_parser_parse_definitely (parser))
19028                         ;
19029                       else
19030                         gnu_attrs = attr;
19031                     }
19032                   tree requires_clause = NULL_TREE;
19033                   late_return = (cp_parser_late_return_type_opt
19034                                  (parser, declarator, requires_clause,
19035                                   memfn ? cv_quals : -1));
19036
19037                   /* Parse the virt-specifier-seq.  */
19038                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19039
19040                   /* Create the function-declarator.  */
19041                   declarator = make_call_declarator (declarator,
19042                                                      params,
19043                                                      cv_quals,
19044                                                      virt_specifiers,
19045                                                      ref_qual,
19046                                                      tx_qual,
19047                                                      exception_specification,
19048                                                      late_return,
19049                                                      requires_clause);
19050                   declarator->std_attributes = attrs;
19051                   declarator->attributes = gnu_attrs;
19052                   /* Any subsequent parameter lists are to do with
19053                      return type, so are not those of the declared
19054                      function.  */
19055                   parser->default_arg_ok_p = false;
19056                 }
19057
19058               /* Remove the function parms from scope.  */
19059               pop_bindings_and_leave_scope ();
19060
19061               if (is_declarator)
19062                 /* Repeat the main loop.  */
19063                 continue;
19064             }
19065
19066           /* If this is the first, we can try a parenthesized
19067              declarator.  */
19068           if (first)
19069             {
19070               bool saved_in_type_id_in_expr_p;
19071
19072               parser->default_arg_ok_p = saved_default_arg_ok_p;
19073               parser->in_declarator_p = saved_in_declarator_p;
19074
19075               /* Consume the `('.  */
19076               cp_lexer_consume_token (parser->lexer);
19077               /* Parse the nested declarator.  */
19078               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19079               parser->in_type_id_in_expr_p = true;
19080               declarator
19081                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19082                                         /*parenthesized_p=*/NULL,
19083                                         member_p, friend_p);
19084               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19085               first = false;
19086               /* Expect a `)'.  */
19087               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19088                 declarator = cp_error_declarator;
19089               if (declarator == cp_error_declarator)
19090                 break;
19091
19092               goto handle_declarator;
19093             }
19094           /* Otherwise, we must be done.  */
19095           else
19096             break;
19097         }
19098       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19099                && token->type == CPP_OPEN_SQUARE
19100                && !cp_next_tokens_can_be_attribute_p (parser))
19101         {
19102           /* Parse an array-declarator.  */
19103           tree bounds, attrs;
19104
19105           if (ctor_dtor_or_conv_p)
19106             *ctor_dtor_or_conv_p = 0;
19107
19108           first = false;
19109           parser->default_arg_ok_p = false;
19110           parser->in_declarator_p = true;
19111           /* Consume the `['.  */
19112           cp_lexer_consume_token (parser->lexer);
19113           /* Peek at the next token.  */
19114           token = cp_lexer_peek_token (parser->lexer);
19115           /* If the next token is `]', then there is no
19116              constant-expression.  */
19117           if (token->type != CPP_CLOSE_SQUARE)
19118             {
19119               bool non_constant_p;
19120               bounds
19121                 = cp_parser_constant_expression (parser,
19122                                                  /*allow_non_constant=*/true,
19123                                                  &non_constant_p);
19124               if (!non_constant_p)
19125                 /* OK */;
19126               else if (error_operand_p (bounds))
19127                 /* Already gave an error.  */;
19128               else if (!parser->in_function_body
19129                        || current_binding_level->kind == sk_function_parms)
19130                 {
19131                   /* Normally, the array bound must be an integral constant
19132                      expression.  However, as an extension, we allow VLAs
19133                      in function scopes as long as they aren't part of a
19134                      parameter declaration.  */
19135                   cp_parser_error (parser,
19136                                    "array bound is not an integer constant");
19137                   bounds = error_mark_node;
19138                 }
19139               else if (processing_template_decl
19140                        && !type_dependent_expression_p (bounds))
19141                 {
19142                   /* Remember this wasn't a constant-expression.  */
19143                   bounds = build_nop (TREE_TYPE (bounds), bounds);
19144                   TREE_SIDE_EFFECTS (bounds) = 1;
19145                 }
19146             }
19147           else
19148             bounds = NULL_TREE;
19149           /* Look for the closing `]'.  */
19150           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19151             {
19152               declarator = cp_error_declarator;
19153               break;
19154             }
19155
19156           attrs = cp_parser_std_attribute_spec_seq (parser);
19157           declarator = make_array_declarator (declarator, bounds);
19158           declarator->std_attributes = attrs;
19159         }
19160       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19161         {
19162           {
19163             tree qualifying_scope;
19164             tree unqualified_name;
19165             tree attrs;
19166             special_function_kind sfk;
19167             bool abstract_ok;
19168             bool pack_expansion_p = false;
19169             cp_token *declarator_id_start_token;
19170
19171             /* Parse a declarator-id */
19172             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19173             if (abstract_ok)
19174               {
19175                 cp_parser_parse_tentatively (parser);
19176
19177                 /* If we see an ellipsis, we should be looking at a
19178                    parameter pack. */
19179                 if (token->type == CPP_ELLIPSIS)
19180                   {
19181                     /* Consume the `...' */
19182                     cp_lexer_consume_token (parser->lexer);
19183
19184                     pack_expansion_p = true;
19185                   }
19186               }
19187
19188             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19189             unqualified_name
19190               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19191             qualifying_scope = parser->scope;
19192             if (abstract_ok)
19193               {
19194                 bool okay = false;
19195
19196                 if (!unqualified_name && pack_expansion_p)
19197                   {
19198                     /* Check whether an error occurred. */
19199                     okay = !cp_parser_error_occurred (parser);
19200
19201                     /* We already consumed the ellipsis to mark a
19202                        parameter pack, but we have no way to report it,
19203                        so abort the tentative parse. We will be exiting
19204                        immediately anyway. */
19205                     cp_parser_abort_tentative_parse (parser);
19206                   }
19207                 else
19208                   okay = cp_parser_parse_definitely (parser);
19209
19210                 if (!okay)
19211                   unqualified_name = error_mark_node;
19212                 else if (unqualified_name
19213                          && (qualifying_scope
19214                              || (!identifier_p (unqualified_name))))
19215                   {
19216                     cp_parser_error (parser, "expected unqualified-id");
19217                     unqualified_name = error_mark_node;
19218                   }
19219               }
19220
19221             if (!unqualified_name)
19222               return NULL;
19223             if (unqualified_name == error_mark_node)
19224               {
19225                 declarator = cp_error_declarator;
19226                 pack_expansion_p = false;
19227                 declarator->parameter_pack_p = false;
19228                 break;
19229               }
19230
19231             attrs = cp_parser_std_attribute_spec_seq (parser);
19232
19233             if (qualifying_scope && at_namespace_scope_p ()
19234                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19235               {
19236                 /* In the declaration of a member of a template class
19237                    outside of the class itself, the SCOPE will sometimes
19238                    be a TYPENAME_TYPE.  For example, given:
19239
19240                    template <typename T>
19241                    int S<T>::R::i = 3;
19242
19243                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
19244                    this context, we must resolve S<T>::R to an ordinary
19245                    type, rather than a typename type.
19246
19247                    The reason we normally avoid resolving TYPENAME_TYPEs
19248                    is that a specialization of `S' might render
19249                    `S<T>::R' not a type.  However, if `S' is
19250                    specialized, then this `i' will not be used, so there
19251                    is no harm in resolving the types here.  */
19252                 tree type;
19253
19254                 /* Resolve the TYPENAME_TYPE.  */
19255                 type = resolve_typename_type (qualifying_scope,
19256                                               /*only_current_p=*/false);
19257                 /* If that failed, the declarator is invalid.  */
19258                 if (TREE_CODE (type) == TYPENAME_TYPE)
19259                   {
19260                     if (typedef_variant_p (type))
19261                       error_at (declarator_id_start_token->location,
19262                                 "cannot define member of dependent typedef "
19263                                 "%qT", type);
19264                     else
19265                       error_at (declarator_id_start_token->location,
19266                                 "%<%T::%E%> is not a type",
19267                                 TYPE_CONTEXT (qualifying_scope),
19268                                 TYPE_IDENTIFIER (qualifying_scope));
19269                   }
19270                 qualifying_scope = type;
19271               }
19272
19273             sfk = sfk_none;
19274
19275             if (unqualified_name)
19276               {
19277                 tree class_type;
19278
19279                 if (qualifying_scope
19280                     && CLASS_TYPE_P (qualifying_scope))
19281                   class_type = qualifying_scope;
19282                 else
19283                   class_type = current_class_type;
19284
19285                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19286                   {
19287                     tree name_type = TREE_TYPE (unqualified_name);
19288                     if (class_type && same_type_p (name_type, class_type))
19289                       {
19290                         if (qualifying_scope
19291                             && CLASSTYPE_USE_TEMPLATE (name_type))
19292                           {
19293                             error_at (declarator_id_start_token->location,
19294                                       "invalid use of constructor as a template");
19295                             inform (declarator_id_start_token->location,
19296                                     "use %<%T::%D%> instead of %<%T::%D%> to "
19297                                     "name the constructor in a qualified name",
19298                                     class_type,
19299                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19300                                     class_type, name_type);
19301                             declarator = cp_error_declarator;
19302                             break;
19303                           }
19304                         else
19305                           unqualified_name = constructor_name (class_type);
19306                       }
19307                     else
19308                       {
19309                         /* We do not attempt to print the declarator
19310                            here because we do not have enough
19311                            information about its original syntactic
19312                            form.  */
19313                         cp_parser_error (parser, "invalid declarator");
19314                         declarator = cp_error_declarator;
19315                         break;
19316                       }
19317                   }
19318
19319                 if (class_type)
19320                   {
19321                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19322                       sfk = sfk_destructor;
19323                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19324                       sfk = sfk_conversion;
19325                     else if (/* There's no way to declare a constructor
19326                                 for an anonymous type, even if the type
19327                                 got a name for linkage purposes.  */
19328                              !TYPE_WAS_ANONYMOUS (class_type)
19329                              /* Handle correctly (c++/19200):
19330
19331                                 struct S {
19332                                   struct T{};
19333                                   friend void S(T);
19334                                 };
19335
19336                                 and also:
19337
19338                                 namespace N {
19339                                   void S();
19340                                 }
19341
19342                                 struct S {
19343                                   friend void N::S();
19344                                 };  */
19345                              && !(friend_p
19346                                   && class_type != qualifying_scope)
19347                              && constructor_name_p (unqualified_name,
19348                                                     class_type))
19349                       {
19350                         unqualified_name = constructor_name (class_type);
19351                         sfk = sfk_constructor;
19352                       }
19353                     else if (is_overloaded_fn (unqualified_name)
19354                              && DECL_CONSTRUCTOR_P (get_first_fn
19355                                                     (unqualified_name)))
19356                       sfk = sfk_constructor;
19357
19358                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
19359                       *ctor_dtor_or_conv_p = -1;
19360                   }
19361               }
19362             declarator = make_id_declarator (qualifying_scope,
19363                                              unqualified_name,
19364                                              sfk);
19365             declarator->std_attributes = attrs;
19366             declarator->id_loc = token->location;
19367             declarator->parameter_pack_p = pack_expansion_p;
19368
19369             if (pack_expansion_p)
19370               maybe_warn_variadic_templates ();
19371           }
19372
19373         handle_declarator:;
19374           scope = get_scope_of_declarator (declarator);
19375           if (scope)
19376             {
19377               /* Any names that appear after the declarator-id for a
19378                  member are looked up in the containing scope.  */
19379               if (at_function_scope_p ())
19380                 {
19381                   /* But declarations with qualified-ids can't appear in a
19382                      function.  */
19383                   cp_parser_error (parser, "qualified-id in declaration");
19384                   declarator = cp_error_declarator;
19385                   break;
19386                 }
19387               pushed_scope = push_scope (scope);
19388             }
19389           parser->in_declarator_p = true;
19390           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19391               || (declarator && declarator->kind == cdk_id))
19392             /* Default args are only allowed on function
19393                declarations.  */
19394             parser->default_arg_ok_p = saved_default_arg_ok_p;
19395           else
19396             parser->default_arg_ok_p = false;
19397
19398           first = false;
19399         }
19400       /* We're done.  */
19401       else
19402         break;
19403     }
19404
19405   /* For an abstract declarator, we might wind up with nothing at this
19406      point.  That's an error; the declarator is not optional.  */
19407   if (!declarator)
19408     cp_parser_error (parser, "expected declarator");
19409
19410   /* If we entered a scope, we must exit it now.  */
19411   if (pushed_scope)
19412     pop_scope (pushed_scope);
19413
19414   parser->default_arg_ok_p = saved_default_arg_ok_p;
19415   parser->in_declarator_p = saved_in_declarator_p;
19416
19417   return declarator;
19418 }
19419
19420 /* Parse a ptr-operator.
19421
19422    ptr-operator:
19423      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19424      * cv-qualifier-seq [opt]
19425      &
19426      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19427      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19428
19429    GNU Extension:
19430
19431    ptr-operator:
19432      & cv-qualifier-seq [opt]
19433
19434    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19435    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19436    an rvalue reference. In the case of a pointer-to-member, *TYPE is
19437    filled in with the TYPE containing the member.  *CV_QUALS is
19438    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19439    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
19440    Note that the tree codes returned by this function have nothing
19441    to do with the types of trees that will be eventually be created
19442    to represent the pointer or reference type being parsed. They are
19443    just constants with suggestive names. */
19444 static enum tree_code
19445 cp_parser_ptr_operator (cp_parser* parser,
19446                         tree* type,
19447                         cp_cv_quals *cv_quals,
19448                         tree *attributes)
19449 {
19450   enum tree_code code = ERROR_MARK;
19451   cp_token *token;
19452   tree attrs = NULL_TREE;
19453
19454   /* Assume that it's not a pointer-to-member.  */
19455   *type = NULL_TREE;
19456   /* And that there are no cv-qualifiers.  */
19457   *cv_quals = TYPE_UNQUALIFIED;
19458
19459   /* Peek at the next token.  */
19460   token = cp_lexer_peek_token (parser->lexer);
19461
19462   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
19463   if (token->type == CPP_MULT)
19464     code = INDIRECT_REF;
19465   else if (token->type == CPP_AND)
19466     code = ADDR_EXPR;
19467   else if ((cxx_dialect != cxx98) &&
19468            token->type == CPP_AND_AND) /* C++0x only */
19469     code = NON_LVALUE_EXPR;
19470
19471   if (code != ERROR_MARK)
19472     {
19473       /* Consume the `*', `&' or `&&'.  */
19474       cp_lexer_consume_token (parser->lexer);
19475
19476       /* A `*' can be followed by a cv-qualifier-seq, and so can a
19477          `&', if we are allowing GNU extensions.  (The only qualifier
19478          that can legally appear after `&' is `restrict', but that is
19479          enforced during semantic analysis.  */
19480       if (code == INDIRECT_REF
19481           || cp_parser_allow_gnu_extensions_p (parser))
19482         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19483
19484       attrs = cp_parser_std_attribute_spec_seq (parser);
19485       if (attributes != NULL)
19486         *attributes = attrs;
19487     }
19488   else
19489     {
19490       /* Try the pointer-to-member case.  */
19491       cp_parser_parse_tentatively (parser);
19492       /* Look for the optional `::' operator.  */
19493       cp_parser_global_scope_opt (parser,
19494                                   /*current_scope_valid_p=*/false);
19495       /* Look for the nested-name specifier.  */
19496       token = cp_lexer_peek_token (parser->lexer);
19497       cp_parser_nested_name_specifier (parser,
19498                                        /*typename_keyword_p=*/false,
19499                                        /*check_dependency_p=*/true,
19500                                        /*type_p=*/false,
19501                                        /*is_declaration=*/false);
19502       /* If we found it, and the next token is a `*', then we are
19503          indeed looking at a pointer-to-member operator.  */
19504       if (!cp_parser_error_occurred (parser)
19505           && cp_parser_require (parser, CPP_MULT, RT_MULT))
19506         {
19507           /* Indicate that the `*' operator was used.  */
19508           code = INDIRECT_REF;
19509
19510           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19511             error_at (token->location, "%qD is a namespace", parser->scope);
19512           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19513             error_at (token->location, "cannot form pointer to member of "
19514                       "non-class %q#T", parser->scope);
19515           else
19516             {
19517               /* The type of which the member is a member is given by the
19518                  current SCOPE.  */
19519               *type = parser->scope;
19520               /* The next name will not be qualified.  */
19521               parser->scope = NULL_TREE;
19522               parser->qualifying_scope = NULL_TREE;
19523               parser->object_scope = NULL_TREE;
19524               /* Look for optional c++11 attributes.  */
19525               attrs = cp_parser_std_attribute_spec_seq (parser);
19526               if (attributes != NULL)
19527                 *attributes = attrs;
19528               /* Look for the optional cv-qualifier-seq.  */
19529               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19530             }
19531         }
19532       /* If that didn't work we don't have a ptr-operator.  */
19533       if (!cp_parser_parse_definitely (parser))
19534         cp_parser_error (parser, "expected ptr-operator");
19535     }
19536
19537   return code;
19538 }
19539
19540 /* Parse an (optional) cv-qualifier-seq.
19541
19542    cv-qualifier-seq:
19543      cv-qualifier cv-qualifier-seq [opt]
19544
19545    cv-qualifier:
19546      const
19547      volatile
19548
19549    GNU Extension:
19550
19551    cv-qualifier:
19552      __restrict__
19553
19554    Returns a bitmask representing the cv-qualifiers.  */
19555
19556 static cp_cv_quals
19557 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19558 {
19559   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19560
19561   while (true)
19562     {
19563       cp_token *token;
19564       cp_cv_quals cv_qualifier;
19565
19566       /* Peek at the next token.  */
19567       token = cp_lexer_peek_token (parser->lexer);
19568       /* See if it's a cv-qualifier.  */
19569       switch (token->keyword)
19570         {
19571         case RID_CONST:
19572           cv_qualifier = TYPE_QUAL_CONST;
19573           break;
19574
19575         case RID_VOLATILE:
19576           cv_qualifier = TYPE_QUAL_VOLATILE;
19577           break;
19578
19579         case RID_RESTRICT:
19580           cv_qualifier = TYPE_QUAL_RESTRICT;
19581           break;
19582
19583         default:
19584           cv_qualifier = TYPE_UNQUALIFIED;
19585           break;
19586         }
19587
19588       if (!cv_qualifier)
19589         break;
19590
19591       if (cv_quals & cv_qualifier)
19592         {
19593           error_at (token->location, "duplicate cv-qualifier");
19594           cp_lexer_purge_token (parser->lexer);
19595         }
19596       else
19597         {
19598           cp_lexer_consume_token (parser->lexer);
19599           cv_quals |= cv_qualifier;
19600         }
19601     }
19602
19603   return cv_quals;
19604 }
19605
19606 /* Parse an (optional) ref-qualifier
19607
19608    ref-qualifier:
19609      &
19610      &&
19611
19612    Returns cp_ref_qualifier representing ref-qualifier. */
19613
19614 static cp_ref_qualifier
19615 cp_parser_ref_qualifier_opt (cp_parser* parser)
19616 {
19617   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19618
19619   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
19620   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19621     return ref_qual;
19622
19623   while (true)
19624     {
19625       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19626       cp_token *token = cp_lexer_peek_token (parser->lexer);
19627
19628       switch (token->type)
19629         {
19630         case CPP_AND:
19631           curr_ref_qual = REF_QUAL_LVALUE;
19632           break;
19633
19634         case CPP_AND_AND:
19635           curr_ref_qual = REF_QUAL_RVALUE;
19636           break;
19637
19638         default:
19639           curr_ref_qual = REF_QUAL_NONE;
19640           break;
19641         }
19642
19643       if (!curr_ref_qual)
19644         break;
19645       else if (ref_qual)
19646         {
19647           error_at (token->location, "multiple ref-qualifiers");
19648           cp_lexer_purge_token (parser->lexer);
19649         }
19650       else
19651         {
19652           ref_qual = curr_ref_qual;
19653           cp_lexer_consume_token (parser->lexer);
19654         }
19655     }
19656
19657   return ref_qual;
19658 }
19659
19660 /* Parse an optional tx-qualifier.
19661
19662    tx-qualifier:
19663      transaction_safe
19664      transaction_safe_dynamic  */
19665
19666 static tree
19667 cp_parser_tx_qualifier_opt (cp_parser *parser)
19668 {
19669   cp_token *token = cp_lexer_peek_token (parser->lexer);
19670   if (token->type == CPP_NAME)
19671     {
19672       tree name = token->u.value;
19673       const char *p = IDENTIFIER_POINTER (name);
19674       const int len = strlen ("transaction_safe");
19675       if (!strncmp (p, "transaction_safe", len))
19676         {
19677           p += len;
19678           if (*p == '\0'
19679               || !strcmp (p, "_dynamic"))
19680             {
19681               cp_lexer_consume_token (parser->lexer);
19682               if (!flag_tm)
19683                 {
19684                   error ("%E requires %<-fgnu-tm%>", name);
19685                   return NULL_TREE;
19686                 }
19687               else
19688                 return name;
19689             }
19690         }
19691     }
19692   return NULL_TREE;
19693 }
19694
19695 /* Parse an (optional) virt-specifier-seq.
19696
19697    virt-specifier-seq:
19698      virt-specifier virt-specifier-seq [opt]
19699
19700    virt-specifier:
19701      override
19702      final
19703
19704    Returns a bitmask representing the virt-specifiers.  */
19705
19706 static cp_virt_specifiers
19707 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19708 {
19709   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19710
19711   while (true)
19712     {
19713       cp_token *token;
19714       cp_virt_specifiers virt_specifier;
19715
19716       /* Peek at the next token.  */
19717       token = cp_lexer_peek_token (parser->lexer);
19718       /* See if it's a virt-specifier-qualifier.  */
19719       if (token->type != CPP_NAME)
19720         break;
19721       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19722         {
19723           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19724           virt_specifier = VIRT_SPEC_OVERRIDE;
19725         }
19726       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19727         {
19728           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19729           virt_specifier = VIRT_SPEC_FINAL;
19730         }
19731       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19732         {
19733           virt_specifier = VIRT_SPEC_FINAL;
19734         }
19735       else
19736         break;
19737
19738       if (virt_specifiers & virt_specifier)
19739         {
19740           error_at (token->location, "duplicate virt-specifier");
19741           cp_lexer_purge_token (parser->lexer);
19742         }
19743       else
19744         {
19745           cp_lexer_consume_token (parser->lexer);
19746           virt_specifiers |= virt_specifier;
19747         }
19748     }
19749   return virt_specifiers;
19750 }
19751
19752 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19753    is in scope even though it isn't real.  */
19754
19755 void
19756 inject_this_parameter (tree ctype, cp_cv_quals quals)
19757 {
19758   tree this_parm;
19759
19760   if (current_class_ptr)
19761     {
19762       /* We don't clear this between NSDMIs.  Is it already what we want?  */
19763       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19764       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19765           && cp_type_quals (type) == quals)
19766         return;
19767     }
19768
19769   this_parm = build_this_parm (ctype, quals);
19770   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
19771   current_class_ptr = NULL_TREE;
19772   current_class_ref
19773     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19774   current_class_ptr = this_parm;
19775 }
19776
19777 /* Return true iff our current scope is a non-static data member
19778    initializer.  */
19779
19780 bool
19781 parsing_nsdmi (void)
19782 {
19783   /* We recognize NSDMI context by the context-less 'this' pointer set up
19784      by the function above.  */
19785   if (current_class_ptr
19786       && TREE_CODE (current_class_ptr) == PARM_DECL
19787       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19788     return true;
19789   return false;
19790 }
19791
19792 /* Parse a late-specified return type, if any.  This is not a separate
19793    non-terminal, but part of a function declarator, which looks like
19794
19795    -> trailing-type-specifier-seq abstract-declarator(opt)
19796
19797    Returns the type indicated by the type-id.
19798
19799    In addition to this, parse any queued up omp declare simd
19800    clauses and Cilk Plus SIMD-enabled function's vector attributes.
19801
19802    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19803    function.  */
19804
19805 static tree
19806 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19807                                 tree& requires_clause, cp_cv_quals quals)
19808 {
19809   cp_token *token;
19810   tree type = NULL_TREE;
19811   bool declare_simd_p = (parser->omp_declare_simd
19812                          && declarator
19813                          && declarator->kind == cdk_id);
19814
19815   bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info 
19816                                 && declarator && declarator->kind == cdk_id);
19817
19818   bool oacc_routine_p = (parser->oacc_routine
19819                          && declarator
19820                          && declarator->kind == cdk_id);
19821
19822   /* Peek at the next token.  */
19823   token = cp_lexer_peek_token (parser->lexer);
19824   /* A late-specified return type is indicated by an initial '->'. */
19825   if (token->type != CPP_DEREF
19826       && token->keyword != RID_REQUIRES
19827       && !(token->type == CPP_NAME
19828            && token->u.value == ridpointers[RID_REQUIRES])
19829       && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19830     return NULL_TREE;
19831
19832   tree save_ccp = current_class_ptr;
19833   tree save_ccr = current_class_ref;
19834   if (quals >= 0)
19835     {
19836       /* DR 1207: 'this' is in scope in the trailing return type.  */
19837       inject_this_parameter (current_class_type, quals);
19838     }
19839
19840   if (token->type == CPP_DEREF)
19841     {
19842       /* Consume the ->.  */
19843       cp_lexer_consume_token (parser->lexer);
19844
19845       type = cp_parser_trailing_type_id (parser);
19846     }
19847
19848   /* Function declarations may be followed by a trailing
19849      requires-clause.  */
19850   requires_clause = cp_parser_requires_clause_opt (parser);
19851
19852   if (cilk_simd_fn_vector_p)
19853     declarator->attributes
19854       = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19855                                                   declarator->attributes);
19856   if (declare_simd_p)
19857     declarator->attributes
19858       = cp_parser_late_parsing_omp_declare_simd (parser,
19859                                                  declarator->attributes);
19860   if (oacc_routine_p)
19861     declarator->attributes
19862       = cp_parser_late_parsing_oacc_routine (parser,
19863                                              declarator->attributes);
19864
19865   if (quals >= 0)
19866     {
19867       current_class_ptr = save_ccp;
19868       current_class_ref = save_ccr;
19869     }
19870
19871   return type;
19872 }
19873
19874 /* Parse a declarator-id.
19875
19876    declarator-id:
19877      id-expression
19878      :: [opt] nested-name-specifier [opt] type-name
19879
19880    In the `id-expression' case, the value returned is as for
19881    cp_parser_id_expression if the id-expression was an unqualified-id.
19882    If the id-expression was a qualified-id, then a SCOPE_REF is
19883    returned.  The first operand is the scope (either a NAMESPACE_DECL
19884    or TREE_TYPE), but the second is still just a representation of an
19885    unqualified-id.  */
19886
19887 static tree
19888 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19889 {
19890   tree id;
19891   /* The expression must be an id-expression.  Assume that qualified
19892      names are the names of types so that:
19893
19894        template <class T>
19895        int S<T>::R::i = 3;
19896
19897      will work; we must treat `S<T>::R' as the name of a type.
19898      Similarly, assume that qualified names are templates, where
19899      required, so that:
19900
19901        template <class T>
19902        int S<T>::R<T>::i = 3;
19903
19904      will work, too.  */
19905   id = cp_parser_id_expression (parser,
19906                                 /*template_keyword_p=*/false,
19907                                 /*check_dependency_p=*/false,
19908                                 /*template_p=*/NULL,
19909                                 /*declarator_p=*/true,
19910                                 optional_p);
19911   if (id && BASELINK_P (id))
19912     id = BASELINK_FUNCTIONS (id);
19913   return id;
19914 }
19915
19916 /* Parse a type-id.
19917
19918    type-id:
19919      type-specifier-seq abstract-declarator [opt]
19920
19921    Returns the TYPE specified.  */
19922
19923 static tree
19924 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19925                      bool is_trailing_return)
19926 {
19927   cp_decl_specifier_seq type_specifier_seq;
19928   cp_declarator *abstract_declarator;
19929
19930   /* Parse the type-specifier-seq.  */
19931   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19932                                 is_trailing_return,
19933                                 &type_specifier_seq);
19934   if (type_specifier_seq.type == error_mark_node)
19935     return error_mark_node;
19936
19937   /* There might or might not be an abstract declarator.  */
19938   cp_parser_parse_tentatively (parser);
19939   /* Look for the declarator.  */
19940   abstract_declarator
19941     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19942                             /*parenthesized_p=*/NULL,
19943                             /*member_p=*/false,
19944                             /*friend_p=*/false);
19945   /* Check to see if there really was a declarator.  */
19946   if (!cp_parser_parse_definitely (parser))
19947     abstract_declarator = NULL;
19948
19949   if (type_specifier_seq.type
19950       /* The concepts TS allows 'auto' as a type-id.  */
19951       && (!flag_concepts || parser->in_type_id_in_expr_p)
19952       /* None of the valid uses of 'auto' in C++14 involve the type-id
19953          nonterminal, but it is valid in a trailing-return-type.  */
19954       && !(cxx_dialect >= cxx14 && is_trailing_return)
19955       && type_uses_auto (type_specifier_seq.type))
19956     {
19957       /* A type-id with type 'auto' is only ok if the abstract declarator
19958          is a function declarator with a late-specified return type.
19959
19960          A type-id with 'auto' is also valid in a trailing-return-type
19961          in a compound-requirement. */
19962       if (abstract_declarator
19963           && abstract_declarator->kind == cdk_function
19964           && abstract_declarator->u.function.late_return_type)
19965         /* OK */;
19966       else if (parser->in_result_type_constraint_p)
19967         /* OK */;
19968       else
19969         {
19970           error ("invalid use of %<auto%>");
19971           return error_mark_node;
19972         }
19973     }
19974   
19975   return groktypename (&type_specifier_seq, abstract_declarator,
19976                        is_template_arg);
19977 }
19978
19979 static tree
19980 cp_parser_type_id (cp_parser *parser)
19981 {
19982   return cp_parser_type_id_1 (parser, false, false);
19983 }
19984
19985 static tree
19986 cp_parser_template_type_arg (cp_parser *parser)
19987 {
19988   tree r;
19989   const char *saved_message = parser->type_definition_forbidden_message;
19990   parser->type_definition_forbidden_message
19991     = G_("types may not be defined in template arguments");
19992   r = cp_parser_type_id_1 (parser, true, false);
19993   parser->type_definition_forbidden_message = saved_message;
19994   if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19995     {
19996       error ("invalid use of %<auto%> in template argument");
19997       r = error_mark_node;
19998     }
19999   return r;
20000 }
20001
20002 static tree
20003 cp_parser_trailing_type_id (cp_parser *parser)
20004 {
20005   return cp_parser_type_id_1 (parser, false, true);
20006 }
20007
20008 /* Parse a type-specifier-seq.
20009
20010    type-specifier-seq:
20011      type-specifier type-specifier-seq [opt]
20012
20013    GNU extension:
20014
20015    type-specifier-seq:
20016      attributes type-specifier-seq [opt]
20017
20018    If IS_DECLARATION is true, we are at the start of a "condition" or
20019    exception-declaration, so we might be followed by a declarator-id.
20020
20021    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20022    i.e. we've just seen "->".
20023
20024    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
20025
20026 static void
20027 cp_parser_type_specifier_seq (cp_parser* parser,
20028                               bool is_declaration,
20029                               bool is_trailing_return,
20030                               cp_decl_specifier_seq *type_specifier_seq)
20031 {
20032   bool seen_type_specifier = false;
20033   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20034   cp_token *start_token = NULL;
20035
20036   /* Clear the TYPE_SPECIFIER_SEQ.  */
20037   clear_decl_specs (type_specifier_seq);
20038
20039   /* In the context of a trailing return type, enum E { } is an
20040      elaborated-type-specifier followed by a function-body, not an
20041      enum-specifier.  */
20042   if (is_trailing_return)
20043     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20044
20045   /* Parse the type-specifiers and attributes.  */
20046   while (true)
20047     {
20048       tree type_specifier;
20049       bool is_cv_qualifier;
20050
20051       /* Check for attributes first.  */
20052       if (cp_next_tokens_can_be_attribute_p (parser))
20053         {
20054           type_specifier_seq->attributes =
20055             chainon (type_specifier_seq->attributes,
20056                      cp_parser_attributes_opt (parser));
20057           continue;
20058         }
20059
20060       /* record the token of the beginning of the type specifier seq,
20061          for error reporting purposes*/
20062      if (!start_token)
20063        start_token = cp_lexer_peek_token (parser->lexer);
20064
20065       /* Look for the type-specifier.  */
20066       type_specifier = cp_parser_type_specifier (parser,
20067                                                  flags,
20068                                                  type_specifier_seq,
20069                                                  /*is_declaration=*/false,
20070                                                  NULL,
20071                                                  &is_cv_qualifier);
20072       if (!type_specifier)
20073         {
20074           /* If the first type-specifier could not be found, this is not a
20075              type-specifier-seq at all.  */
20076           if (!seen_type_specifier)
20077             {
20078               /* Set in_declarator_p to avoid skipping to the semicolon.  */
20079               int in_decl = parser->in_declarator_p;
20080               parser->in_declarator_p = true;
20081
20082               if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20083                   || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20084                 cp_parser_error (parser, "expected type-specifier");
20085
20086               parser->in_declarator_p = in_decl;
20087
20088               type_specifier_seq->type = error_mark_node;
20089               return;
20090             }
20091           /* If subsequent type-specifiers could not be found, the
20092              type-specifier-seq is complete.  */
20093           break;
20094         }
20095
20096       seen_type_specifier = true;
20097       /* The standard says that a condition can be:
20098
20099             type-specifier-seq declarator = assignment-expression
20100
20101          However, given:
20102
20103            struct S {};
20104            if (int S = ...)
20105
20106          we should treat the "S" as a declarator, not as a
20107          type-specifier.  The standard doesn't say that explicitly for
20108          type-specifier-seq, but it does say that for
20109          decl-specifier-seq in an ordinary declaration.  Perhaps it
20110          would be clearer just to allow a decl-specifier-seq here, and
20111          then add a semantic restriction that if any decl-specifiers
20112          that are not type-specifiers appear, the program is invalid.  */
20113       if (is_declaration && !is_cv_qualifier)
20114         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20115     }
20116 }
20117
20118 /* Return whether the function currently being declared has an associated
20119    template parameter list.  */
20120
20121 static bool
20122 function_being_declared_is_template_p (cp_parser* parser)
20123 {
20124   if (!current_template_parms || processing_template_parmlist)
20125     return false;
20126
20127   if (parser->implicit_template_scope)
20128     return true;
20129
20130   if (at_class_scope_p ()
20131       && TYPE_BEING_DEFINED (current_class_type))
20132     return parser->num_template_parameter_lists != 0;
20133
20134   return ((int) parser->num_template_parameter_lists > template_class_depth
20135           (current_class_type));
20136 }
20137
20138 /* Parse a parameter-declaration-clause.
20139
20140    parameter-declaration-clause:
20141      parameter-declaration-list [opt] ... [opt]
20142      parameter-declaration-list , ...
20143
20144    Returns a representation for the parameter declarations.  A return
20145    value of NULL indicates a parameter-declaration-clause consisting
20146    only of an ellipsis.  */
20147
20148 static tree
20149 cp_parser_parameter_declaration_clause (cp_parser* parser)
20150 {
20151   tree parameters;
20152   cp_token *token;
20153   bool ellipsis_p;
20154   bool is_error;
20155
20156   struct cleanup {
20157     cp_parser* parser;
20158     int auto_is_implicit_function_template_parm_p;
20159     ~cleanup() {
20160       parser->auto_is_implicit_function_template_parm_p
20161         = auto_is_implicit_function_template_parm_p;
20162     }
20163   } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20164
20165   (void) cleanup;
20166
20167   if (!processing_specialization
20168       && !processing_template_parmlist
20169       && !processing_explicit_instantiation)
20170     if (!current_function_decl
20171         || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20172       parser->auto_is_implicit_function_template_parm_p = true;
20173
20174   /* Peek at the next token.  */
20175   token = cp_lexer_peek_token (parser->lexer);
20176   /* Check for trivial parameter-declaration-clauses.  */
20177   if (token->type == CPP_ELLIPSIS)
20178     {
20179       /* Consume the `...' token.  */
20180       cp_lexer_consume_token (parser->lexer);
20181       return NULL_TREE;
20182     }
20183   else if (token->type == CPP_CLOSE_PAREN)
20184     /* There are no parameters.  */
20185     {
20186 #ifndef NO_IMPLICIT_EXTERN_C
20187       if (in_system_header_at (input_location)
20188           && current_class_type == NULL
20189           && current_lang_name == lang_name_c)
20190         return NULL_TREE;
20191       else
20192 #endif
20193         return void_list_node;
20194     }
20195   /* Check for `(void)', too, which is a special case.  */
20196   else if (token->keyword == RID_VOID
20197            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20198                == CPP_CLOSE_PAREN))
20199     {
20200       /* Consume the `void' token.  */
20201       cp_lexer_consume_token (parser->lexer);
20202       /* There are no parameters.  */
20203       return void_list_node;
20204     }
20205
20206   /* Parse the parameter-declaration-list.  */
20207   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20208   /* If a parse error occurred while parsing the
20209      parameter-declaration-list, then the entire
20210      parameter-declaration-clause is erroneous.  */
20211   if (is_error)
20212     return NULL;
20213
20214   /* Peek at the next token.  */
20215   token = cp_lexer_peek_token (parser->lexer);
20216   /* If it's a `,', the clause should terminate with an ellipsis.  */
20217   if (token->type == CPP_COMMA)
20218     {
20219       /* Consume the `,'.  */
20220       cp_lexer_consume_token (parser->lexer);
20221       /* Expect an ellipsis.  */
20222       ellipsis_p
20223         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20224     }
20225   /* It might also be `...' if the optional trailing `,' was
20226      omitted.  */
20227   else if (token->type == CPP_ELLIPSIS)
20228     {
20229       /* Consume the `...' token.  */
20230       cp_lexer_consume_token (parser->lexer);
20231       /* And remember that we saw it.  */
20232       ellipsis_p = true;
20233     }
20234   else
20235     ellipsis_p = false;
20236
20237   /* Finish the parameter list.  */
20238   if (!ellipsis_p)
20239     parameters = chainon (parameters, void_list_node);
20240
20241   return parameters;
20242 }
20243
20244 /* Parse a parameter-declaration-list.
20245
20246    parameter-declaration-list:
20247      parameter-declaration
20248      parameter-declaration-list , parameter-declaration
20249
20250    Returns a representation of the parameter-declaration-list, as for
20251    cp_parser_parameter_declaration_clause.  However, the
20252    `void_list_node' is never appended to the list.  Upon return,
20253    *IS_ERROR will be true iff an error occurred.  */
20254
20255 static tree
20256 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20257 {
20258   tree parameters = NULL_TREE;
20259   tree *tail = &parameters;
20260   bool saved_in_unbraced_linkage_specification_p;
20261   int index = 0;
20262
20263   /* Assume all will go well.  */
20264   *is_error = false;
20265   /* The special considerations that apply to a function within an
20266      unbraced linkage specifications do not apply to the parameters
20267      to the function.  */
20268   saved_in_unbraced_linkage_specification_p
20269     = parser->in_unbraced_linkage_specification_p;
20270   parser->in_unbraced_linkage_specification_p = false;
20271
20272   /* Look for more parameters.  */
20273   while (true)
20274     {
20275       cp_parameter_declarator *parameter;
20276       tree decl = error_mark_node;
20277       bool parenthesized_p = false;
20278       int template_parm_idx = (function_being_declared_is_template_p (parser)?
20279                                TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20280                                                 (current_template_parms)) : 0);
20281
20282       /* Parse the parameter.  */
20283       parameter
20284         = cp_parser_parameter_declaration (parser,
20285                                            /*template_parm_p=*/false,
20286                                            &parenthesized_p);
20287
20288       /* We don't know yet if the enclosing context is deprecated, so wait
20289          and warn in grokparms if appropriate.  */
20290       deprecated_state = DEPRECATED_SUPPRESS;
20291
20292       if (parameter)
20293         {
20294           /* If a function parameter pack was specified and an implicit template
20295              parameter was introduced during cp_parser_parameter_declaration,
20296              change any implicit parameters introduced into packs.  */
20297           if (parser->implicit_template_parms
20298               && parameter->declarator
20299               && parameter->declarator->parameter_pack_p)
20300             {
20301               int latest_template_parm_idx = TREE_VEC_LENGTH
20302                 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20303
20304               if (latest_template_parm_idx != template_parm_idx)
20305                 parameter->decl_specifiers.type = convert_generic_types_to_packs
20306                   (parameter->decl_specifiers.type,
20307                    template_parm_idx, latest_template_parm_idx);
20308             }
20309
20310           decl = grokdeclarator (parameter->declarator,
20311                                  &parameter->decl_specifiers,
20312                                  PARM,
20313                                  parameter->default_argument != NULL_TREE,
20314                                  &parameter->decl_specifiers.attributes);
20315         }
20316
20317       deprecated_state = DEPRECATED_NORMAL;
20318
20319       /* If a parse error occurred parsing the parameter declaration,
20320          then the entire parameter-declaration-list is erroneous.  */
20321       if (decl == error_mark_node)
20322         {
20323           *is_error = true;
20324           parameters = error_mark_node;
20325           break;
20326         }
20327
20328       if (parameter->decl_specifiers.attributes)
20329         cplus_decl_attributes (&decl,
20330                                parameter->decl_specifiers.attributes,
20331                                0);
20332       if (DECL_NAME (decl))
20333         decl = pushdecl (decl);
20334
20335       if (decl != error_mark_node)
20336         {
20337           retrofit_lang_decl (decl);
20338           DECL_PARM_INDEX (decl) = ++index;
20339           DECL_PARM_LEVEL (decl) = function_parm_depth ();
20340         }
20341
20342       /* Add the new parameter to the list.  */
20343       *tail = build_tree_list (parameter->default_argument, decl);
20344       tail = &TREE_CHAIN (*tail);
20345
20346       /* Peek at the next token.  */
20347       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20348           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20349           /* These are for Objective-C++ */
20350           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20351           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20352         /* The parameter-declaration-list is complete.  */
20353         break;
20354       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20355         {
20356           cp_token *token;
20357
20358           /* Peek at the next token.  */
20359           token = cp_lexer_peek_nth_token (parser->lexer, 2);
20360           /* If it's an ellipsis, then the list is complete.  */
20361           if (token->type == CPP_ELLIPSIS)
20362             break;
20363           /* Otherwise, there must be more parameters.  Consume the
20364              `,'.  */
20365           cp_lexer_consume_token (parser->lexer);
20366           /* When parsing something like:
20367
20368                 int i(float f, double d)
20369
20370              we can tell after seeing the declaration for "f" that we
20371              are not looking at an initialization of a variable "i",
20372              but rather at the declaration of a function "i".
20373
20374              Due to the fact that the parsing of template arguments
20375              (as specified to a template-id) requires backtracking we
20376              cannot use this technique when inside a template argument
20377              list.  */
20378           if (!parser->in_template_argument_list_p
20379               && !parser->in_type_id_in_expr_p
20380               && cp_parser_uncommitted_to_tentative_parse_p (parser)
20381               /* However, a parameter-declaration of the form
20382                  "float(f)" (which is a valid declaration of a
20383                  parameter "f") can also be interpreted as an
20384                  expression (the conversion of "f" to "float").  */
20385               && !parenthesized_p)
20386             cp_parser_commit_to_tentative_parse (parser);
20387         }
20388       else
20389         {
20390           cp_parser_error (parser, "expected %<,%> or %<...%>");
20391           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20392             cp_parser_skip_to_closing_parenthesis (parser,
20393                                                    /*recovering=*/true,
20394                                                    /*or_comma=*/false,
20395                                                    /*consume_paren=*/false);
20396           break;
20397         }
20398     }
20399
20400   parser->in_unbraced_linkage_specification_p
20401     = saved_in_unbraced_linkage_specification_p;
20402
20403   /* Reset implicit_template_scope if we are about to leave the function
20404      parameter list that introduced it.  Note that for out-of-line member
20405      definitions, there will be one or more class scopes before we get to
20406      the template parameter scope.  */
20407
20408   if (cp_binding_level *its = parser->implicit_template_scope)
20409     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20410       {
20411         while (maybe_its->kind == sk_class)
20412           maybe_its = maybe_its->level_chain;
20413         if (maybe_its == its)
20414           {
20415             parser->implicit_template_parms = 0;
20416             parser->implicit_template_scope = 0;
20417           }
20418       }
20419
20420   return parameters;
20421 }
20422
20423 /* Parse a parameter declaration.
20424
20425    parameter-declaration:
20426      decl-specifier-seq ... [opt] declarator
20427      decl-specifier-seq declarator = assignment-expression
20428      decl-specifier-seq ... [opt] abstract-declarator [opt]
20429      decl-specifier-seq abstract-declarator [opt] = assignment-expression
20430
20431    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20432    declares a template parameter.  (In that case, a non-nested `>'
20433    token encountered during the parsing of the assignment-expression
20434    is not interpreted as a greater-than operator.)
20435
20436    Returns a representation of the parameter, or NULL if an error
20437    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20438    true iff the declarator is of the form "(p)".  */
20439
20440 static cp_parameter_declarator *
20441 cp_parser_parameter_declaration (cp_parser *parser,
20442                                  bool template_parm_p,
20443                                  bool *parenthesized_p)
20444 {
20445   int declares_class_or_enum;
20446   cp_decl_specifier_seq decl_specifiers;
20447   cp_declarator *declarator;
20448   tree default_argument;
20449   cp_token *token = NULL, *declarator_token_start = NULL;
20450   const char *saved_message;
20451   bool template_parameter_pack_p = false;
20452
20453   /* In a template parameter, `>' is not an operator.
20454
20455      [temp.param]
20456
20457      When parsing a default template-argument for a non-type
20458      template-parameter, the first non-nested `>' is taken as the end
20459      of the template parameter-list rather than a greater-than
20460      operator.  */
20461
20462   /* Type definitions may not appear in parameter types.  */
20463   saved_message = parser->type_definition_forbidden_message;
20464   parser->type_definition_forbidden_message
20465     = G_("types may not be defined in parameter types");
20466
20467   /* Parse the declaration-specifiers.  */
20468   cp_parser_decl_specifier_seq (parser,
20469                                 CP_PARSER_FLAGS_NONE,
20470                                 &decl_specifiers,
20471                                 &declares_class_or_enum);
20472
20473   /* Complain about missing 'typename' or other invalid type names.  */
20474   if (!decl_specifiers.any_type_specifiers_p
20475       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20476     decl_specifiers.type = error_mark_node;
20477
20478   /* If an error occurred, there's no reason to attempt to parse the
20479      rest of the declaration.  */
20480   if (cp_parser_error_occurred (parser))
20481     {
20482       parser->type_definition_forbidden_message = saved_message;
20483       return NULL;
20484     }
20485
20486   /* Peek at the next token.  */
20487   token = cp_lexer_peek_token (parser->lexer);
20488
20489   /* If the next token is a `)', `,', `=', `>', or `...', then there
20490      is no declarator. However, when variadic templates are enabled,
20491      there may be a declarator following `...'.  */
20492   if (token->type == CPP_CLOSE_PAREN
20493       || token->type == CPP_COMMA
20494       || token->type == CPP_EQ
20495       || token->type == CPP_GREATER)
20496     {
20497       declarator = NULL;
20498       if (parenthesized_p)
20499         *parenthesized_p = false;
20500     }
20501   /* Otherwise, there should be a declarator.  */
20502   else
20503     {
20504       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20505       parser->default_arg_ok_p = false;
20506
20507       /* After seeing a decl-specifier-seq, if the next token is not a
20508          "(", there is no possibility that the code is a valid
20509          expression.  Therefore, if parsing tentatively, we commit at
20510          this point.  */
20511       if (!parser->in_template_argument_list_p
20512           /* In an expression context, having seen:
20513
20514                (int((char ...
20515
20516              we cannot be sure whether we are looking at a
20517              function-type (taking a "char" as a parameter) or a cast
20518              of some object of type "char" to "int".  */
20519           && !parser->in_type_id_in_expr_p
20520           && cp_parser_uncommitted_to_tentative_parse_p (parser)
20521           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20522           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20523         cp_parser_commit_to_tentative_parse (parser);
20524       /* Parse the declarator.  */
20525       declarator_token_start = token;
20526       declarator = cp_parser_declarator (parser,
20527                                          CP_PARSER_DECLARATOR_EITHER,
20528                                          /*ctor_dtor_or_conv_p=*/NULL,
20529                                          parenthesized_p,
20530                                          /*member_p=*/false,
20531                                          /*friend_p=*/false);
20532       parser->default_arg_ok_p = saved_default_arg_ok_p;
20533       /* After the declarator, allow more attributes.  */
20534       decl_specifiers.attributes
20535         = chainon (decl_specifiers.attributes,
20536                    cp_parser_attributes_opt (parser));
20537
20538       /* If the declarator is a template parameter pack, remember that and
20539          clear the flag in the declarator itself so we don't get errors
20540          from grokdeclarator.  */
20541       if (template_parm_p && declarator && declarator->parameter_pack_p)
20542         {
20543           declarator->parameter_pack_p = false;
20544           template_parameter_pack_p = true;
20545         }
20546     }
20547
20548   /* If the next token is an ellipsis, and we have not seen a declarator
20549      name, and if either the type of the declarator contains parameter
20550      packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20551      for, eg, abbreviated integral type names), then we actually have a
20552      parameter pack expansion expression. Otherwise, leave the ellipsis
20553      for a C-style variadic function. */
20554   token = cp_lexer_peek_token (parser->lexer);
20555   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20556     {
20557       tree type = decl_specifiers.type;
20558
20559       if (type && DECL_P (type))
20560         type = TREE_TYPE (type);
20561
20562       if (((type
20563             && TREE_CODE (type) != TYPE_PACK_EXPANSION
20564             && (template_parm_p || uses_parameter_packs (type)))
20565            || (!type && template_parm_p))
20566           && declarator_can_be_parameter_pack (declarator))
20567         {
20568           /* Consume the `...'. */
20569           cp_lexer_consume_token (parser->lexer);
20570           maybe_warn_variadic_templates ();
20571           
20572           /* Build a pack expansion type */
20573           if (template_parm_p)
20574             template_parameter_pack_p = true;
20575           else if (declarator)
20576             declarator->parameter_pack_p = true;
20577           else
20578             decl_specifiers.type = make_pack_expansion (type);
20579         }
20580     }
20581
20582   /* The restriction on defining new types applies only to the type
20583      of the parameter, not to the default argument.  */
20584   parser->type_definition_forbidden_message = saved_message;
20585
20586   /* If the next token is `=', then process a default argument.  */
20587   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20588     {
20589       tree type = decl_specifiers.type;
20590       token = cp_lexer_peek_token (parser->lexer);
20591       /* If we are defining a class, then the tokens that make up the
20592          default argument must be saved and processed later.  */
20593       if (!template_parm_p && at_class_scope_p ()
20594           && TYPE_BEING_DEFINED (current_class_type)
20595           && !LAMBDA_TYPE_P (current_class_type))
20596         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20597
20598       // A constrained-type-specifier may declare a type template-parameter.
20599       else if (declares_constrained_type_template_parameter (type))
20600         default_argument
20601           = cp_parser_default_type_template_argument (parser);
20602
20603       // A constrained-type-specifier may declare a template-template-parameter.
20604       else if (declares_constrained_template_template_parameter (type))
20605         default_argument
20606           = cp_parser_default_template_template_argument (parser);
20607
20608       /* Outside of a class definition, we can just parse the
20609          assignment-expression.  */
20610       else
20611         default_argument
20612           = cp_parser_default_argument (parser, template_parm_p);
20613
20614       if (!parser->default_arg_ok_p)
20615         {
20616           permerror (token->location,
20617                      "default arguments are only "
20618                      "permitted for function parameters");
20619         }
20620       else if ((declarator && declarator->parameter_pack_p)
20621                || template_parameter_pack_p
20622                || (decl_specifiers.type
20623                    && PACK_EXPANSION_P (decl_specifiers.type)))
20624         {
20625           /* Find the name of the parameter pack.  */     
20626           cp_declarator *id_declarator = declarator;
20627           while (id_declarator && id_declarator->kind != cdk_id)
20628             id_declarator = id_declarator->declarator;
20629           
20630           if (id_declarator && id_declarator->kind == cdk_id)
20631             error_at (declarator_token_start->location,
20632                       template_parm_p
20633                       ? G_("template parameter pack %qD "
20634                            "cannot have a default argument")
20635                       : G_("parameter pack %qD cannot have "
20636                            "a default argument"),
20637                       id_declarator->u.id.unqualified_name);
20638           else
20639             error_at (declarator_token_start->location,
20640                       template_parm_p
20641                       ? G_("template parameter pack cannot have "
20642                            "a default argument")
20643                       : G_("parameter pack cannot have a "
20644                            "default argument"));
20645
20646           default_argument = NULL_TREE;
20647         }
20648     }
20649   else
20650     default_argument = NULL_TREE;
20651
20652   return make_parameter_declarator (&decl_specifiers,
20653                                     declarator,
20654                                     default_argument,
20655                                     template_parameter_pack_p);
20656 }
20657
20658 /* Parse a default argument and return it.
20659
20660    TEMPLATE_PARM_P is true if this is a default argument for a
20661    non-type template parameter.  */
20662 static tree
20663 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20664 {
20665   tree default_argument = NULL_TREE;
20666   bool saved_greater_than_is_operator_p;
20667   bool saved_local_variables_forbidden_p;
20668   bool non_constant_p, is_direct_init;
20669
20670   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20671      set correctly.  */
20672   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20673   parser->greater_than_is_operator_p = !template_parm_p;
20674   /* Local variable names (and the `this' keyword) may not
20675      appear in a default argument.  */
20676   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20677   parser->local_variables_forbidden_p = true;
20678   /* Parse the assignment-expression.  */
20679   if (template_parm_p)
20680     push_deferring_access_checks (dk_no_deferred);
20681   tree saved_class_ptr = NULL_TREE;
20682   tree saved_class_ref = NULL_TREE;
20683   /* The "this" pointer is not valid in a default argument.  */
20684   if (cfun)
20685     {
20686       saved_class_ptr = current_class_ptr;
20687       cp_function_chain->x_current_class_ptr = NULL_TREE;
20688       saved_class_ref = current_class_ref;
20689       cp_function_chain->x_current_class_ref = NULL_TREE;
20690     }
20691   default_argument
20692     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20693   /* Restore the "this" pointer.  */
20694   if (cfun)
20695     {
20696       cp_function_chain->x_current_class_ptr = saved_class_ptr;
20697       cp_function_chain->x_current_class_ref = saved_class_ref;
20698     }
20699   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20700     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20701   if (template_parm_p)
20702     pop_deferring_access_checks ();
20703   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20704   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20705
20706   return default_argument;
20707 }
20708
20709 /* Parse a function-body.
20710
20711    function-body:
20712      compound_statement  */
20713
20714 static void
20715 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20716 {
20717   cp_parser_compound_statement (parser, NULL, (in_function_try_block
20718                                                ? BCS_TRY_BLOCK : BCS_NORMAL),
20719                                 true);
20720 }
20721
20722 /* Parse a ctor-initializer-opt followed by a function-body.  Return
20723    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
20724    is true we are parsing a function-try-block.  */
20725
20726 static bool
20727 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20728                                                   bool in_function_try_block)
20729 {
20730   tree body, list;
20731   bool ctor_initializer_p;
20732   const bool check_body_p =
20733      DECL_CONSTRUCTOR_P (current_function_decl)
20734      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20735   tree last = NULL;
20736
20737   /* Begin the function body.  */
20738   body = begin_function_body ();
20739   /* Parse the optional ctor-initializer.  */
20740   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20741
20742   /* If we're parsing a constexpr constructor definition, we need
20743      to check that the constructor body is indeed empty.  However,
20744      before we get to cp_parser_function_body lot of junk has been
20745      generated, so we can't just check that we have an empty block.
20746      Rather we take a snapshot of the outermost block, and check whether
20747      cp_parser_function_body changed its state.  */
20748   if (check_body_p)
20749     {
20750       list = cur_stmt_list;
20751       if (STATEMENT_LIST_TAIL (list))
20752         last = STATEMENT_LIST_TAIL (list)->stmt;
20753     }
20754   /* Parse the function-body.  */
20755   cp_parser_function_body (parser, in_function_try_block);
20756   if (check_body_p)
20757     check_constexpr_ctor_body (last, list, /*complain=*/true);
20758   /* Finish the function body.  */
20759   finish_function_body (body);
20760
20761   return ctor_initializer_p;
20762 }
20763
20764 /* Parse an initializer.
20765
20766    initializer:
20767      = initializer-clause
20768      ( expression-list )
20769
20770    Returns an expression representing the initializer.  If no
20771    initializer is present, NULL_TREE is returned.
20772
20773    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20774    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
20775    set to TRUE if there is no initializer present.  If there is an
20776    initializer, and it is not a constant-expression, *NON_CONSTANT_P
20777    is set to true; otherwise it is set to false.  */
20778
20779 static tree
20780 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20781                        bool* non_constant_p)
20782 {
20783   cp_token *token;
20784   tree init;
20785
20786   /* Peek at the next token.  */
20787   token = cp_lexer_peek_token (parser->lexer);
20788
20789   /* Let our caller know whether or not this initializer was
20790      parenthesized.  */
20791   *is_direct_init = (token->type != CPP_EQ);
20792   /* Assume that the initializer is constant.  */
20793   *non_constant_p = false;
20794
20795   if (token->type == CPP_EQ)
20796     {
20797       /* Consume the `='.  */
20798       cp_lexer_consume_token (parser->lexer);
20799       /* Parse the initializer-clause.  */
20800       init = cp_parser_initializer_clause (parser, non_constant_p);
20801     }
20802   else if (token->type == CPP_OPEN_PAREN)
20803     {
20804       vec<tree, va_gc> *vec;
20805       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20806                                                      /*cast_p=*/false,
20807                                                      /*allow_expansion_p=*/true,
20808                                                      non_constant_p);
20809       if (vec == NULL)
20810         return error_mark_node;
20811       init = build_tree_list_vec (vec);
20812       release_tree_vector (vec);
20813     }
20814   else if (token->type == CPP_OPEN_BRACE)
20815     {
20816       cp_lexer_set_source_position (parser->lexer);
20817       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20818       init = cp_parser_braced_list (parser, non_constant_p);
20819       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20820     }
20821   else
20822     {
20823       /* Anything else is an error.  */
20824       cp_parser_error (parser, "expected initializer");
20825       init = error_mark_node;
20826     }
20827
20828   return init;
20829 }
20830
20831 /* Parse an initializer-clause.
20832
20833    initializer-clause:
20834      assignment-expression
20835      braced-init-list
20836
20837    Returns an expression representing the initializer.
20838
20839    If the `assignment-expression' production is used the value
20840    returned is simply a representation for the expression.
20841
20842    Otherwise, calls cp_parser_braced_list.  */
20843
20844 static cp_expr
20845 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20846 {
20847   cp_expr initializer;
20848
20849   /* Assume the expression is constant.  */
20850   *non_constant_p = false;
20851
20852   /* If it is not a `{', then we are looking at an
20853      assignment-expression.  */
20854   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20855     {
20856       initializer
20857         = cp_parser_constant_expression (parser,
20858                                         /*allow_non_constant_p=*/true,
20859                                         non_constant_p);
20860     }
20861   else
20862     initializer = cp_parser_braced_list (parser, non_constant_p);
20863
20864   return initializer;
20865 }
20866
20867 /* Parse a brace-enclosed initializer list.
20868
20869    braced-init-list:
20870      { initializer-list , [opt] }
20871      { }
20872
20873    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
20874    the elements of the initializer-list (or NULL, if the last
20875    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
20876    NULL_TREE.  There is no way to detect whether or not the optional
20877    trailing `,' was provided.  NON_CONSTANT_P is as for
20878    cp_parser_initializer.  */     
20879
20880 static cp_expr
20881 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20882 {
20883   tree initializer;
20884   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20885
20886   /* Consume the `{' token.  */
20887   cp_lexer_consume_token (parser->lexer);
20888   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
20889   initializer = make_node (CONSTRUCTOR);
20890   /* If it's not a `}', then there is a non-trivial initializer.  */
20891   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20892     {
20893       /* Parse the initializer list.  */
20894       CONSTRUCTOR_ELTS (initializer)
20895         = cp_parser_initializer_list (parser, non_constant_p);
20896       /* A trailing `,' token is allowed.  */
20897       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20898         cp_lexer_consume_token (parser->lexer);
20899     }
20900   else
20901     *non_constant_p = false;
20902   /* Now, there should be a trailing `}'.  */
20903   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20904   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20905   TREE_TYPE (initializer) = init_list_type_node;
20906
20907   cp_expr result (initializer);
20908   /* Build a location of the form:
20909        { ... }
20910        ^~~~~~~
20911      with caret==start at the open brace, finish at the close brace.  */
20912   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20913   result.set_location (combined_loc);
20914   return result;
20915 }
20916
20917 /* Consume tokens up to, and including, the next non-nested closing `]'.
20918    Returns true iff we found a closing `]'.  */
20919
20920 static bool
20921 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20922 {
20923   unsigned square_depth = 0;
20924
20925   while (true)
20926     {
20927       cp_token * token = cp_lexer_peek_token (parser->lexer);
20928
20929       switch (token->type)
20930         {
20931         case CPP_EOF:
20932         case CPP_PRAGMA_EOL:
20933           /* If we've run out of tokens, then there is no closing `]'.  */
20934           return false;
20935
20936         case CPP_OPEN_SQUARE:
20937           ++square_depth;
20938           break;
20939
20940         case CPP_CLOSE_SQUARE:
20941           if (!square_depth--)
20942             {
20943               cp_lexer_consume_token (parser->lexer);
20944               return true;
20945             }
20946           break;
20947
20948         default:
20949           break;
20950         }
20951
20952       /* Consume the token.  */
20953       cp_lexer_consume_token (parser->lexer);
20954     }
20955 }
20956
20957 /* Return true if we are looking at an array-designator, false otherwise.  */
20958
20959 static bool
20960 cp_parser_array_designator_p (cp_parser *parser)
20961 {
20962   /* Consume the `['.  */
20963   cp_lexer_consume_token (parser->lexer);
20964
20965   cp_lexer_save_tokens (parser->lexer);
20966
20967   /* Skip tokens until the next token is a closing square bracket.
20968      If we find the closing `]', and the next token is a `=', then
20969      we are looking at an array designator.  */
20970   bool array_designator_p
20971     = (cp_parser_skip_to_closing_square_bracket (parser)
20972        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20973   
20974   /* Roll back the tokens we skipped.  */
20975   cp_lexer_rollback_tokens (parser->lexer);
20976
20977   return array_designator_p;
20978 }
20979
20980 /* Parse an initializer-list.
20981
20982    initializer-list:
20983      initializer-clause ... [opt]
20984      initializer-list , initializer-clause ... [opt]
20985
20986    GNU Extension:
20987
20988    initializer-list:
20989      designation initializer-clause ...[opt]
20990      initializer-list , designation initializer-clause ...[opt]
20991
20992    designation:
20993      . identifier =
20994      identifier :
20995      [ constant-expression ] =
20996
20997    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
20998    for the initializer.  If the INDEX of the elt is non-NULL, it is the
20999    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
21000    as for cp_parser_initializer.  */
21001
21002 static vec<constructor_elt, va_gc> *
21003 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21004 {
21005   vec<constructor_elt, va_gc> *v = NULL;
21006
21007   /* Assume all of the expressions are constant.  */
21008   *non_constant_p = false;
21009
21010   /* Parse the rest of the list.  */
21011   while (true)
21012     {
21013       cp_token *token;
21014       tree designator;
21015       tree initializer;
21016       bool clause_non_constant_p;
21017
21018       /* If the next token is an identifier and the following one is a
21019          colon, we are looking at the GNU designated-initializer
21020          syntax.  */
21021       if (cp_parser_allow_gnu_extensions_p (parser)
21022           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21023           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21024         {
21025           /* Warn the user that they are using an extension.  */
21026           pedwarn (input_location, OPT_Wpedantic, 
21027                    "ISO C++ does not allow designated initializers");
21028           /* Consume the identifier.  */
21029           designator = cp_lexer_consume_token (parser->lexer)->u.value;
21030           /* Consume the `:'.  */
21031           cp_lexer_consume_token (parser->lexer);
21032         }
21033       /* Also handle the C99 syntax, '. id ='.  */
21034       else if (cp_parser_allow_gnu_extensions_p (parser)
21035                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21036                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21037                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21038         {
21039           /* Warn the user that they are using an extension.  */
21040           pedwarn (input_location, OPT_Wpedantic,
21041                    "ISO C++ does not allow C99 designated initializers");
21042           /* Consume the `.'.  */
21043           cp_lexer_consume_token (parser->lexer);
21044           /* Consume the identifier.  */
21045           designator = cp_lexer_consume_token (parser->lexer)->u.value;
21046           /* Consume the `='.  */
21047           cp_lexer_consume_token (parser->lexer);
21048         }
21049       /* Also handle C99 array designators, '[ const ] ='.  */
21050       else if (cp_parser_allow_gnu_extensions_p (parser)
21051                && !c_dialect_objc ()
21052                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21053         {
21054           /* In C++11, [ could start a lambda-introducer.  */
21055           bool non_const = false;
21056
21057           cp_parser_parse_tentatively (parser);
21058
21059           if (!cp_parser_array_designator_p (parser))
21060             {
21061               cp_parser_simulate_error (parser);
21062               designator = NULL_TREE;
21063             }
21064           else
21065             {
21066               designator = cp_parser_constant_expression (parser, true,
21067                                                           &non_const);
21068               cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21069               cp_parser_require (parser, CPP_EQ, RT_EQ);
21070             }
21071
21072           if (!cp_parser_parse_definitely (parser))
21073             designator = NULL_TREE;
21074           else if (non_const)
21075             require_potential_rvalue_constant_expression (designator);
21076         }
21077       else
21078         designator = NULL_TREE;
21079
21080       /* Parse the initializer.  */
21081       initializer = cp_parser_initializer_clause (parser,
21082                                                   &clause_non_constant_p);
21083       /* If any clause is non-constant, so is the entire initializer.  */
21084       if (clause_non_constant_p)
21085         *non_constant_p = true;
21086
21087       /* If we have an ellipsis, this is an initializer pack
21088          expansion.  */
21089       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21090         {
21091           /* Consume the `...'.  */
21092           cp_lexer_consume_token (parser->lexer);
21093
21094           /* Turn the initializer into an initializer expansion.  */
21095           initializer = make_pack_expansion (initializer);
21096         }
21097
21098       /* Add it to the vector.  */
21099       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21100
21101       /* If the next token is not a comma, we have reached the end of
21102          the list.  */
21103       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21104         break;
21105
21106       /* Peek at the next token.  */
21107       token = cp_lexer_peek_nth_token (parser->lexer, 2);
21108       /* If the next token is a `}', then we're still done.  An
21109          initializer-clause can have a trailing `,' after the
21110          initializer-list and before the closing `}'.  */
21111       if (token->type == CPP_CLOSE_BRACE)
21112         break;
21113
21114       /* Consume the `,' token.  */
21115       cp_lexer_consume_token (parser->lexer);
21116     }
21117
21118   return v;
21119 }
21120
21121 /* Classes [gram.class] */
21122
21123 /* Parse a class-name.
21124
21125    class-name:
21126      identifier
21127      template-id
21128
21129    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21130    to indicate that names looked up in dependent types should be
21131    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
21132    keyword has been used to indicate that the name that appears next
21133    is a template.  TAG_TYPE indicates the explicit tag given before
21134    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
21135    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
21136    is the class being defined in a class-head.  If ENUM_OK is TRUE,
21137    enum-names are also accepted.
21138
21139    Returns the TYPE_DECL representing the class.  */
21140
21141 static tree
21142 cp_parser_class_name (cp_parser *parser,
21143                       bool typename_keyword_p,
21144                       bool template_keyword_p,
21145                       enum tag_types tag_type,
21146                       bool check_dependency_p,
21147                       bool class_head_p,
21148                       bool is_declaration,
21149                       bool enum_ok)
21150 {
21151   tree decl;
21152   tree scope;
21153   bool typename_p;
21154   cp_token *token;
21155   tree identifier = NULL_TREE;
21156
21157   /* All class-names start with an identifier.  */
21158   token = cp_lexer_peek_token (parser->lexer);
21159   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21160     {
21161       cp_parser_error (parser, "expected class-name");
21162       return error_mark_node;
21163     }
21164
21165   /* PARSER->SCOPE can be cleared when parsing the template-arguments
21166      to a template-id, so we save it here.  */
21167   scope = parser->scope;
21168   if (scope == error_mark_node)
21169     return error_mark_node;
21170
21171   /* Any name names a type if we're following the `typename' keyword
21172      in a qualified name where the enclosing scope is type-dependent.  */
21173   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21174                 && dependent_type_p (scope));
21175   /* Handle the common case (an identifier, but not a template-id)
21176      efficiently.  */
21177   if (token->type == CPP_NAME
21178       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21179     {
21180       cp_token *identifier_token;
21181       bool ambiguous_p;
21182
21183       /* Look for the identifier.  */
21184       identifier_token = cp_lexer_peek_token (parser->lexer);
21185       ambiguous_p = identifier_token->error_reported;
21186       identifier = cp_parser_identifier (parser);
21187       /* If the next token isn't an identifier, we are certainly not
21188          looking at a class-name.  */
21189       if (identifier == error_mark_node)
21190         decl = error_mark_node;
21191       /* If we know this is a type-name, there's no need to look it
21192          up.  */
21193       else if (typename_p)
21194         decl = identifier;
21195       else
21196         {
21197           tree ambiguous_decls;
21198           /* If we already know that this lookup is ambiguous, then
21199              we've already issued an error message; there's no reason
21200              to check again.  */
21201           if (ambiguous_p)
21202             {
21203               cp_parser_simulate_error (parser);
21204               return error_mark_node;
21205             }
21206           /* If the next token is a `::', then the name must be a type
21207              name.
21208
21209              [basic.lookup.qual]
21210
21211              During the lookup for a name preceding the :: scope
21212              resolution operator, object, function, and enumerator
21213              names are ignored.  */
21214           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21215             tag_type = scope_type;
21216           /* Look up the name.  */
21217           decl = cp_parser_lookup_name (parser, identifier,
21218                                         tag_type,
21219                                         /*is_template=*/false,
21220                                         /*is_namespace=*/false,
21221                                         check_dependency_p,
21222                                         &ambiguous_decls,
21223                                         identifier_token->location);
21224           if (ambiguous_decls)
21225             {
21226               if (cp_parser_parsing_tentatively (parser))
21227                 cp_parser_simulate_error (parser);
21228               return error_mark_node;
21229             }
21230         }
21231     }
21232   else
21233     {
21234       /* Try a template-id.  */
21235       decl = cp_parser_template_id (parser, template_keyword_p,
21236                                     check_dependency_p,
21237                                     tag_type,
21238                                     is_declaration);
21239       if (decl == error_mark_node)
21240         return error_mark_node;
21241     }
21242
21243   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21244
21245   /* If this is a typename, create a TYPENAME_TYPE.  */
21246   if (typename_p && decl != error_mark_node)
21247     {
21248       decl = make_typename_type (scope, decl, typename_type,
21249                                  /*complain=*/tf_error);
21250       if (decl != error_mark_node)
21251         decl = TYPE_NAME (decl);
21252     }
21253
21254   decl = strip_using_decl (decl);
21255
21256   /* Check to see that it is really the name of a class.  */
21257   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21258       && identifier_p (TREE_OPERAND (decl, 0))
21259       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21260     /* Situations like this:
21261
21262          template <typename T> struct A {
21263            typename T::template X<int>::I i;
21264          };
21265
21266        are problematic.  Is `T::template X<int>' a class-name?  The
21267        standard does not seem to be definitive, but there is no other
21268        valid interpretation of the following `::'.  Therefore, those
21269        names are considered class-names.  */
21270     {
21271       decl = make_typename_type (scope, decl, tag_type, tf_error);
21272       if (decl != error_mark_node)
21273         decl = TYPE_NAME (decl);
21274     }
21275   else if (TREE_CODE (decl) != TYPE_DECL
21276            || TREE_TYPE (decl) == error_mark_node
21277            || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21278                 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21279            /* In Objective-C 2.0, a classname followed by '.' starts a
21280               dot-syntax expression, and it's not a type-name.  */
21281            || (c_dialect_objc ()
21282                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
21283                && objc_is_class_name (decl)))
21284     decl = error_mark_node;
21285
21286   if (decl == error_mark_node)
21287     cp_parser_error (parser, "expected class-name");
21288   else if (identifier && !parser->scope)
21289     maybe_note_name_used_in_class (identifier, decl);
21290
21291   return decl;
21292 }
21293
21294 /* Parse a class-specifier.
21295
21296    class-specifier:
21297      class-head { member-specification [opt] }
21298
21299    Returns the TREE_TYPE representing the class.  */
21300
21301 static tree
21302 cp_parser_class_specifier_1 (cp_parser* parser)
21303 {
21304   tree type;
21305   tree attributes = NULL_TREE;
21306   bool nested_name_specifier_p;
21307   unsigned saved_num_template_parameter_lists;
21308   bool saved_in_function_body;
21309   unsigned char in_statement;
21310   bool in_switch_statement_p;
21311   bool saved_in_unbraced_linkage_specification_p;
21312   tree old_scope = NULL_TREE;
21313   tree scope = NULL_TREE;
21314   cp_token *closing_brace;
21315
21316   push_deferring_access_checks (dk_no_deferred);
21317
21318   /* Parse the class-head.  */
21319   type = cp_parser_class_head (parser,
21320                                &nested_name_specifier_p);
21321   /* If the class-head was a semantic disaster, skip the entire body
21322      of the class.  */
21323   if (!type)
21324     {
21325       cp_parser_skip_to_end_of_block_or_statement (parser);
21326       pop_deferring_access_checks ();
21327       return error_mark_node;
21328     }
21329
21330   /* Look for the `{'.  */
21331   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21332     {
21333       pop_deferring_access_checks ();
21334       return error_mark_node;
21335     }
21336
21337   cp_ensure_no_omp_declare_simd (parser);
21338   cp_ensure_no_oacc_routine (parser);
21339
21340   /* Issue an error message if type-definitions are forbidden here.  */
21341   cp_parser_check_type_definition (parser);
21342   /* Remember that we are defining one more class.  */
21343   ++parser->num_classes_being_defined;
21344   /* Inside the class, surrounding template-parameter-lists do not
21345      apply.  */
21346   saved_num_template_parameter_lists
21347     = parser->num_template_parameter_lists;
21348   parser->num_template_parameter_lists = 0;
21349   /* We are not in a function body.  */
21350   saved_in_function_body = parser->in_function_body;
21351   parser->in_function_body = false;
21352   /* Or in a loop.  */
21353   in_statement = parser->in_statement;
21354   parser->in_statement = 0;
21355   /* Or in a switch.  */
21356   in_switch_statement_p = parser->in_switch_statement_p;
21357   parser->in_switch_statement_p = false;
21358   /* We are not immediately inside an extern "lang" block.  */
21359   saved_in_unbraced_linkage_specification_p
21360     = parser->in_unbraced_linkage_specification_p;
21361   parser->in_unbraced_linkage_specification_p = false;
21362
21363   // Associate constraints with the type.
21364   if (flag_concepts)
21365     type = associate_classtype_constraints (type);
21366
21367   /* Start the class.  */
21368   if (nested_name_specifier_p)
21369     {
21370       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21371       old_scope = push_inner_scope (scope);
21372     }
21373   type = begin_class_definition (type);
21374
21375   if (type == error_mark_node)
21376     /* If the type is erroneous, skip the entire body of the class.  */
21377     cp_parser_skip_to_closing_brace (parser);
21378   else
21379     /* Parse the member-specification.  */
21380     cp_parser_member_specification_opt (parser);
21381
21382   /* Look for the trailing `}'.  */
21383   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21384   /* Look for trailing attributes to apply to this class.  */
21385   if (cp_parser_allow_gnu_extensions_p (parser))
21386     attributes = cp_parser_gnu_attributes_opt (parser);
21387   if (type != error_mark_node)
21388     type = finish_struct (type, attributes);
21389   if (nested_name_specifier_p)
21390     pop_inner_scope (old_scope, scope);
21391
21392   /* We've finished a type definition.  Check for the common syntax
21393      error of forgetting a semicolon after the definition.  We need to
21394      be careful, as we can't just check for not-a-semicolon and be done
21395      with it; the user might have typed:
21396
21397      class X { } c = ...;
21398      class X { } *p = ...;
21399
21400      and so forth.  Instead, enumerate all the possible tokens that
21401      might follow this production; if we don't see one of them, then
21402      complain and silently insert the semicolon.  */
21403   {
21404     cp_token *token = cp_lexer_peek_token (parser->lexer);
21405     bool want_semicolon = true;
21406
21407     if (cp_next_tokens_can_be_std_attribute_p (parser))
21408       /* Don't try to parse c++11 attributes here.  As per the
21409          grammar, that should be a task for
21410          cp_parser_decl_specifier_seq.  */
21411       want_semicolon = false;
21412
21413     switch (token->type)
21414       {
21415       case CPP_NAME:
21416       case CPP_SEMICOLON:
21417       case CPP_MULT:
21418       case CPP_AND:
21419       case CPP_OPEN_PAREN:
21420       case CPP_CLOSE_PAREN:
21421       case CPP_COMMA:
21422         want_semicolon = false;
21423         break;
21424
21425         /* While it's legal for type qualifiers and storage class
21426            specifiers to follow type definitions in the grammar, only
21427            compiler testsuites contain code like that.  Assume that if
21428            we see such code, then what we're really seeing is a case
21429            like:
21430
21431            class X { }
21432            const <type> var = ...;
21433
21434            or
21435
21436            class Y { }
21437            static <type> func (...) ...
21438
21439            i.e. the qualifier or specifier applies to the next
21440            declaration.  To do so, however, we need to look ahead one
21441            more token to see if *that* token is a type specifier.
21442
21443            This code could be improved to handle:
21444
21445            class Z { }
21446            static const <type> var = ...;  */
21447       case CPP_KEYWORD:
21448         if (keyword_is_decl_specifier (token->keyword))
21449           {
21450             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21451
21452             /* Handling user-defined types here would be nice, but very
21453                tricky.  */
21454             want_semicolon
21455               = (lookahead->type == CPP_KEYWORD
21456                  && keyword_begins_type_specifier (lookahead->keyword));
21457           }
21458         break;
21459       default:
21460         break;
21461       }
21462
21463     /* If we don't have a type, then something is very wrong and we
21464        shouldn't try to do anything clever.  Likewise for not seeing the
21465        closing brace.  */
21466     if (closing_brace && TYPE_P (type) && want_semicolon)
21467       {
21468         cp_token_position prev
21469           = cp_lexer_previous_token_position (parser->lexer);
21470         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21471         location_t loc = prev_token->location;
21472
21473         if (CLASSTYPE_DECLARED_CLASS (type))
21474           error_at (loc, "expected %<;%> after class definition");
21475         else if (TREE_CODE (type) == RECORD_TYPE)
21476           error_at (loc, "expected %<;%> after struct definition");
21477         else if (TREE_CODE (type) == UNION_TYPE)
21478           error_at (loc, "expected %<;%> after union definition");
21479         else
21480           gcc_unreachable ();
21481
21482         /* Unget one token and smash it to look as though we encountered
21483            a semicolon in the input stream.  */
21484         cp_lexer_set_token_position (parser->lexer, prev);
21485         token = cp_lexer_peek_token (parser->lexer);
21486         token->type = CPP_SEMICOLON;
21487         token->keyword = RID_MAX;
21488       }
21489   }
21490
21491   /* If this class is not itself within the scope of another class,
21492      then we need to parse the bodies of all of the queued function
21493      definitions.  Note that the queued functions defined in a class
21494      are not always processed immediately following the
21495      class-specifier for that class.  Consider:
21496
21497        struct A {
21498          struct B { void f() { sizeof (A); } };
21499        };
21500
21501      If `f' were processed before the processing of `A' were
21502      completed, there would be no way to compute the size of `A'.
21503      Note that the nesting we are interested in here is lexical --
21504      not the semantic nesting given by TYPE_CONTEXT.  In particular,
21505      for:
21506
21507        struct A { struct B; };
21508        struct A::B { void f() { } };
21509
21510      there is no need to delay the parsing of `A::B::f'.  */
21511   if (--parser->num_classes_being_defined == 0)
21512     {
21513       tree decl;
21514       tree class_type = NULL_TREE;
21515       tree pushed_scope = NULL_TREE;
21516       unsigned ix;
21517       cp_default_arg_entry *e;
21518       tree save_ccp, save_ccr;
21519
21520       /* In a first pass, parse default arguments to the functions.
21521          Then, in a second pass, parse the bodies of the functions.
21522          This two-phased approach handles cases like:
21523
21524             struct S {
21525               void f() { g(); }
21526               void g(int i = 3);
21527             };
21528
21529          */
21530       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21531         {
21532           decl = e->decl;
21533           /* If there are default arguments that have not yet been processed,
21534              take care of them now.  */
21535           if (class_type != e->class_type)
21536             {
21537               if (pushed_scope)
21538                 pop_scope (pushed_scope);
21539               class_type = e->class_type;
21540               pushed_scope = push_scope (class_type);
21541             }
21542           /* Make sure that any template parameters are in scope.  */
21543           maybe_begin_member_template_processing (decl);
21544           /* Parse the default argument expressions.  */
21545           cp_parser_late_parsing_default_args (parser, decl);
21546           /* Remove any template parameters from the symbol table.  */
21547           maybe_end_member_template_processing ();
21548         }
21549       vec_safe_truncate (unparsed_funs_with_default_args, 0);
21550       /* Now parse any NSDMIs.  */
21551       save_ccp = current_class_ptr;
21552       save_ccr = current_class_ref;
21553       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21554         {
21555           if (class_type != DECL_CONTEXT (decl))
21556             {
21557               if (pushed_scope)
21558                 pop_scope (pushed_scope);
21559               class_type = DECL_CONTEXT (decl);
21560               pushed_scope = push_scope (class_type);
21561             }
21562           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21563           cp_parser_late_parsing_nsdmi (parser, decl);
21564         }
21565       vec_safe_truncate (unparsed_nsdmis, 0);
21566       current_class_ptr = save_ccp;
21567       current_class_ref = save_ccr;
21568       if (pushed_scope)
21569         pop_scope (pushed_scope);
21570
21571       /* Now do some post-NSDMI bookkeeping.  */
21572       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21573         after_nsdmi_defaulted_late_checks (class_type);
21574       vec_safe_truncate (unparsed_classes, 0);
21575       after_nsdmi_defaulted_late_checks (type);
21576
21577       /* Now parse the body of the functions.  */
21578       if (flag_openmp)
21579         {
21580           /* OpenMP UDRs need to be parsed before all other functions.  */
21581           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21582             if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21583               cp_parser_late_parsing_for_member (parser, decl);
21584           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21585             if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21586               cp_parser_late_parsing_for_member (parser, decl);
21587         }
21588       else
21589         FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21590           cp_parser_late_parsing_for_member (parser, decl);
21591       vec_safe_truncate (unparsed_funs_with_definitions, 0);
21592     }
21593   else
21594     vec_safe_push (unparsed_classes, type);
21595
21596   /* Put back any saved access checks.  */
21597   pop_deferring_access_checks ();
21598
21599   /* Restore saved state.  */
21600   parser->in_switch_statement_p = in_switch_statement_p;
21601   parser->in_statement = in_statement;
21602   parser->in_function_body = saved_in_function_body;
21603   parser->num_template_parameter_lists
21604     = saved_num_template_parameter_lists;
21605   parser->in_unbraced_linkage_specification_p
21606     = saved_in_unbraced_linkage_specification_p;
21607
21608   return type;
21609 }
21610
21611 static tree
21612 cp_parser_class_specifier (cp_parser* parser)
21613 {
21614   tree ret;
21615   timevar_push (TV_PARSE_STRUCT);
21616   ret = cp_parser_class_specifier_1 (parser);
21617   timevar_pop (TV_PARSE_STRUCT);
21618   return ret;
21619 }
21620
21621 /* Parse a class-head.
21622
21623    class-head:
21624      class-key identifier [opt] base-clause [opt]
21625      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21626      class-key nested-name-specifier [opt] template-id
21627        base-clause [opt]
21628
21629    class-virt-specifier:
21630      final
21631
21632    GNU Extensions:
21633      class-key attributes identifier [opt] base-clause [opt]
21634      class-key attributes nested-name-specifier identifier base-clause [opt]
21635      class-key attributes nested-name-specifier [opt] template-id
21636        base-clause [opt]
21637
21638    Upon return BASES is initialized to the list of base classes (or
21639    NULL, if there are none) in the same form returned by
21640    cp_parser_base_clause.
21641
21642    Returns the TYPE of the indicated class.  Sets
21643    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21644    involving a nested-name-specifier was used, and FALSE otherwise.
21645
21646    Returns error_mark_node if this is not a class-head.
21647
21648    Returns NULL_TREE if the class-head is syntactically valid, but
21649    semantically invalid in a way that means we should skip the entire
21650    body of the class.  */
21651
21652 static tree
21653 cp_parser_class_head (cp_parser* parser,
21654                       bool* nested_name_specifier_p)
21655 {
21656   tree nested_name_specifier;
21657   enum tag_types class_key;
21658   tree id = NULL_TREE;
21659   tree type = NULL_TREE;
21660   tree attributes;
21661   tree bases;
21662   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21663   bool template_id_p = false;
21664   bool qualified_p = false;
21665   bool invalid_nested_name_p = false;
21666   bool invalid_explicit_specialization_p = false;
21667   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21668   tree pushed_scope = NULL_TREE;
21669   unsigned num_templates;
21670   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21671   /* Assume no nested-name-specifier will be present.  */
21672   *nested_name_specifier_p = false;
21673   /* Assume no template parameter lists will be used in defining the
21674      type.  */
21675   num_templates = 0;
21676   parser->colon_corrects_to_scope_p = false;
21677
21678   /* Look for the class-key.  */
21679   class_key = cp_parser_class_key (parser);
21680   if (class_key == none_type)
21681     return error_mark_node;
21682
21683   /* Parse the attributes.  */
21684   attributes = cp_parser_attributes_opt (parser);
21685
21686   /* If the next token is `::', that is invalid -- but sometimes
21687      people do try to write:
21688
21689        struct ::S {};
21690
21691      Handle this gracefully by accepting the extra qualifier, and then
21692      issuing an error about it later if this really is a
21693      class-head.  If it turns out just to be an elaborated type
21694      specifier, remain silent.  */
21695   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21696     qualified_p = true;
21697
21698   push_deferring_access_checks (dk_no_check);
21699
21700   /* Determine the name of the class.  Begin by looking for an
21701      optional nested-name-specifier.  */
21702   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21703   nested_name_specifier
21704     = cp_parser_nested_name_specifier_opt (parser,
21705                                            /*typename_keyword_p=*/false,
21706                                            /*check_dependency_p=*/false,
21707                                            /*type_p=*/true,
21708                                            /*is_declaration=*/false);
21709   /* If there was a nested-name-specifier, then there *must* be an
21710      identifier.  */
21711   if (nested_name_specifier)
21712     {
21713       type_start_token = cp_lexer_peek_token (parser->lexer);
21714       /* Although the grammar says `identifier', it really means
21715          `class-name' or `template-name'.  You are only allowed to
21716          define a class that has already been declared with this
21717          syntax.
21718
21719          The proposed resolution for Core Issue 180 says that wherever
21720          you see `class T::X' you should treat `X' as a type-name.
21721
21722          It is OK to define an inaccessible class; for example:
21723
21724            class A { class B; };
21725            class A::B {};
21726
21727          We do not know if we will see a class-name, or a
21728          template-name.  We look for a class-name first, in case the
21729          class-name is a template-id; if we looked for the
21730          template-name first we would stop after the template-name.  */
21731       cp_parser_parse_tentatively (parser);
21732       type = cp_parser_class_name (parser,
21733                                    /*typename_keyword_p=*/false,
21734                                    /*template_keyword_p=*/false,
21735                                    class_type,
21736                                    /*check_dependency_p=*/false,
21737                                    /*class_head_p=*/true,
21738                                    /*is_declaration=*/false);
21739       /* If that didn't work, ignore the nested-name-specifier.  */
21740       if (!cp_parser_parse_definitely (parser))
21741         {
21742           invalid_nested_name_p = true;
21743           type_start_token = cp_lexer_peek_token (parser->lexer);
21744           id = cp_parser_identifier (parser);
21745           if (id == error_mark_node)
21746             id = NULL_TREE;
21747         }
21748       /* If we could not find a corresponding TYPE, treat this
21749          declaration like an unqualified declaration.  */
21750       if (type == error_mark_node)
21751         nested_name_specifier = NULL_TREE;
21752       /* Otherwise, count the number of templates used in TYPE and its
21753          containing scopes.  */
21754       else
21755         {
21756           tree scope;
21757
21758           for (scope = TREE_TYPE (type);
21759                scope && TREE_CODE (scope) != NAMESPACE_DECL;
21760                scope = get_containing_scope (scope))
21761             if (TYPE_P (scope)
21762                 && CLASS_TYPE_P (scope)
21763                 && CLASSTYPE_TEMPLATE_INFO (scope)
21764                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21765                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21766                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21767               ++num_templates;
21768         }
21769     }
21770   /* Otherwise, the identifier is optional.  */
21771   else
21772     {
21773       /* We don't know whether what comes next is a template-id,
21774          an identifier, or nothing at all.  */
21775       cp_parser_parse_tentatively (parser);
21776       /* Check for a template-id.  */
21777       type_start_token = cp_lexer_peek_token (parser->lexer);
21778       id = cp_parser_template_id (parser,
21779                                   /*template_keyword_p=*/false,
21780                                   /*check_dependency_p=*/true,
21781                                   class_key,
21782                                   /*is_declaration=*/true);
21783       /* If that didn't work, it could still be an identifier.  */
21784       if (!cp_parser_parse_definitely (parser))
21785         {
21786           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21787             {
21788               type_start_token = cp_lexer_peek_token (parser->lexer);
21789               id = cp_parser_identifier (parser);
21790             }
21791           else
21792             id = NULL_TREE;
21793         }
21794       else
21795         {
21796           template_id_p = true;
21797           ++num_templates;
21798         }
21799     }
21800
21801   pop_deferring_access_checks ();
21802
21803   if (id)
21804     {
21805       cp_parser_check_for_invalid_template_id (parser, id,
21806                                                class_key,
21807                                                type_start_token->location);
21808     }
21809   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21810
21811   /* If it's not a `:' or a `{' then we can't really be looking at a
21812      class-head, since a class-head only appears as part of a
21813      class-specifier.  We have to detect this situation before calling
21814      xref_tag, since that has irreversible side-effects.  */
21815   if (!cp_parser_next_token_starts_class_definition_p (parser))
21816     {
21817       cp_parser_error (parser, "expected %<{%> or %<:%>");
21818       type = error_mark_node;
21819       goto out;
21820     }
21821
21822   /* At this point, we're going ahead with the class-specifier, even
21823      if some other problem occurs.  */
21824   cp_parser_commit_to_tentative_parse (parser);
21825   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21826     {
21827       cp_parser_error (parser,
21828                        "cannot specify %<override%> for a class");
21829       type = error_mark_node;
21830       goto out;
21831     }
21832   /* Issue the error about the overly-qualified name now.  */
21833   if (qualified_p)
21834     {
21835       cp_parser_error (parser,
21836                        "global qualification of class name is invalid");
21837       type = error_mark_node;
21838       goto out;
21839     }
21840   else if (invalid_nested_name_p)
21841     {
21842       cp_parser_error (parser,
21843                        "qualified name does not name a class");
21844       type = error_mark_node;
21845       goto out;
21846     }
21847   else if (nested_name_specifier)
21848     {
21849       tree scope;
21850
21851       /* Reject typedef-names in class heads.  */
21852       if (!DECL_IMPLICIT_TYPEDEF_P (type))
21853         {
21854           error_at (type_start_token->location,
21855                     "invalid class name in declaration of %qD",
21856                     type);
21857           type = NULL_TREE;
21858           goto done;
21859         }
21860
21861       /* Figure out in what scope the declaration is being placed.  */
21862       scope = current_scope ();
21863       /* If that scope does not contain the scope in which the
21864          class was originally declared, the program is invalid.  */
21865       if (scope && !is_ancestor (scope, nested_name_specifier))
21866         {
21867           if (at_namespace_scope_p ())
21868             error_at (type_start_token->location,
21869                       "declaration of %qD in namespace %qD which does not "
21870                       "enclose %qD",
21871                       type, scope, nested_name_specifier);
21872           else
21873             error_at (type_start_token->location,
21874                       "declaration of %qD in %qD which does not enclose %qD",
21875                       type, scope, nested_name_specifier);
21876           type = NULL_TREE;
21877           goto done;
21878         }
21879       /* [dcl.meaning]
21880
21881          A declarator-id shall not be qualified except for the
21882          definition of a ... nested class outside of its class
21883          ... [or] the definition or explicit instantiation of a
21884          class member of a namespace outside of its namespace.  */
21885       if (scope == nested_name_specifier)
21886         {
21887           permerror (nested_name_specifier_token_start->location,
21888                      "extra qualification not allowed");
21889           nested_name_specifier = NULL_TREE;
21890           num_templates = 0;
21891         }
21892     }
21893   /* An explicit-specialization must be preceded by "template <>".  If
21894      it is not, try to recover gracefully.  */
21895   if (at_namespace_scope_p ()
21896       && parser->num_template_parameter_lists == 0
21897       && template_id_p)
21898     {
21899       error_at (type_start_token->location,
21900                 "an explicit specialization must be preceded by %<template <>%>");
21901       invalid_explicit_specialization_p = true;
21902       /* Take the same action that would have been taken by
21903          cp_parser_explicit_specialization.  */
21904       ++parser->num_template_parameter_lists;
21905       begin_specialization ();
21906     }
21907   /* There must be no "return" statements between this point and the
21908      end of this function; set "type "to the correct return value and
21909      use "goto done;" to return.  */
21910   /* Make sure that the right number of template parameters were
21911      present.  */
21912   if (!cp_parser_check_template_parameters (parser, num_templates,
21913                                             type_start_token->location,
21914                                             /*declarator=*/NULL))
21915     {
21916       /* If something went wrong, there is no point in even trying to
21917          process the class-definition.  */
21918       type = NULL_TREE;
21919       goto done;
21920     }
21921
21922   /* Look up the type.  */
21923   if (template_id_p)
21924     {
21925       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21926           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21927               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21928         {
21929           error_at (type_start_token->location,
21930                     "function template %qD redeclared as a class template", id);
21931           type = error_mark_node;
21932         }
21933       else
21934         {
21935           type = TREE_TYPE (id);
21936           type = maybe_process_partial_specialization (type);
21937         }
21938       if (nested_name_specifier)
21939         pushed_scope = push_scope (nested_name_specifier);
21940     }
21941   else if (nested_name_specifier)
21942     {
21943       tree class_type;
21944
21945       /* Given:
21946
21947             template <typename T> struct S { struct T };
21948             template <typename T> struct S<T>::T { };
21949
21950          we will get a TYPENAME_TYPE when processing the definition of
21951          `S::T'.  We need to resolve it to the actual type before we
21952          try to define it.  */
21953       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21954         {
21955           class_type = resolve_typename_type (TREE_TYPE (type),
21956                                               /*only_current_p=*/false);
21957           if (TREE_CODE (class_type) != TYPENAME_TYPE)
21958             type = TYPE_NAME (class_type);
21959           else
21960             {
21961               cp_parser_error (parser, "could not resolve typename type");
21962               type = error_mark_node;
21963             }
21964         }
21965
21966       if (maybe_process_partial_specialization (TREE_TYPE (type))
21967           == error_mark_node)
21968         {
21969           type = NULL_TREE;
21970           goto done;
21971         }
21972
21973       class_type = current_class_type;
21974       /* Enter the scope indicated by the nested-name-specifier.  */
21975       pushed_scope = push_scope (nested_name_specifier);
21976       /* Get the canonical version of this type.  */
21977       type = TYPE_MAIN_DECL (TREE_TYPE (type));
21978       /* Call push_template_decl if it seems like we should be defining a
21979          template either from the template headers or the type we're
21980          defining, so that we diagnose both extra and missing headers.  */
21981       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21982            || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21983           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21984         {
21985           type = push_template_decl (type);
21986           if (type == error_mark_node)
21987             {
21988               type = NULL_TREE;
21989               goto done;
21990             }
21991         }
21992
21993       type = TREE_TYPE (type);
21994       *nested_name_specifier_p = true;
21995     }
21996   else      /* The name is not a nested name.  */
21997     {
21998       /* If the class was unnamed, create a dummy name.  */
21999       if (!id)
22000         id = make_anon_name ();
22001       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22002                        parser->num_template_parameter_lists);
22003     }
22004
22005   /* Indicate whether this class was declared as a `class' or as a
22006      `struct'.  */
22007   if (TREE_CODE (type) == RECORD_TYPE)
22008     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22009   cp_parser_check_class_key (class_key, type);
22010
22011   /* If this type was already complete, and we see another definition,
22012      that's an error.  */
22013   if (type != error_mark_node && COMPLETE_TYPE_P (type))
22014     {
22015       error_at (type_start_token->location, "redefinition of %q#T",
22016                 type);
22017       error_at (type_start_token->location, "previous definition of %q+#T",
22018                 type);
22019       type = NULL_TREE;
22020       goto done;
22021     }
22022   else if (type == error_mark_node)
22023     type = NULL_TREE;
22024
22025   if (type)
22026     {
22027       /* Apply attributes now, before any use of the class as a template
22028          argument in its base list.  */
22029       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22030       fixup_attribute_variants (type);
22031     }
22032
22033   /* We will have entered the scope containing the class; the names of
22034      base classes should be looked up in that context.  For example:
22035
22036        struct A { struct B {}; struct C; };
22037        struct A::C : B {};
22038
22039      is valid.  */
22040
22041   /* Get the list of base-classes, if there is one.  */
22042   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22043     {
22044       /* PR59482: enter the class scope so that base-specifiers are looked
22045          up correctly.  */
22046       if (type)
22047         pushclass (type);
22048       bases = cp_parser_base_clause (parser);
22049       /* PR59482: get out of the previously pushed class scope so that the
22050          subsequent pops pop the right thing.  */
22051       if (type)
22052         popclass ();
22053     }
22054   else
22055     bases = NULL_TREE;
22056
22057   /* If we're really defining a class, process the base classes.
22058      If they're invalid, fail.  */
22059   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22060       && !xref_basetypes (type, bases))
22061     type = NULL_TREE;
22062
22063  done:
22064   /* Leave the scope given by the nested-name-specifier.  We will
22065      enter the class scope itself while processing the members.  */
22066   if (pushed_scope)
22067     pop_scope (pushed_scope);
22068
22069   if (invalid_explicit_specialization_p)
22070     {
22071       end_specialization ();
22072       --parser->num_template_parameter_lists;
22073     }
22074
22075   if (type)
22076     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22077   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22078     CLASSTYPE_FINAL (type) = 1;
22079  out:
22080   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22081   return type;
22082 }
22083
22084 /* Parse a class-key.
22085
22086    class-key:
22087      class
22088      struct
22089      union
22090
22091    Returns the kind of class-key specified, or none_type to indicate
22092    error.  */
22093
22094 static enum tag_types
22095 cp_parser_class_key (cp_parser* parser)
22096 {
22097   cp_token *token;
22098   enum tag_types tag_type;
22099
22100   /* Look for the class-key.  */
22101   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22102   if (!token)
22103     return none_type;
22104
22105   /* Check to see if the TOKEN is a class-key.  */
22106   tag_type = cp_parser_token_is_class_key (token);
22107   if (!tag_type)
22108     cp_parser_error (parser, "expected class-key");
22109   return tag_type;
22110 }
22111
22112 /* Parse a type-parameter-key.
22113
22114    type-parameter-key:
22115      class
22116      typename
22117  */
22118
22119 static void
22120 cp_parser_type_parameter_key (cp_parser* parser)
22121 {
22122   /* Look for the type-parameter-key.  */
22123   enum tag_types tag_type = none_type;
22124   cp_token *token = cp_lexer_peek_token (parser->lexer);
22125   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22126     {
22127       cp_lexer_consume_token (parser->lexer);
22128       if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22129         /* typename is not allowed in a template template parameter
22130            by the standard until C++1Z.  */
22131         pedwarn (token->location, OPT_Wpedantic, 
22132                  "ISO C++ forbids typename key in template template parameter;"
22133                  " use -std=c++1z or -std=gnu++1z");
22134     }
22135   else
22136     cp_parser_error (parser, "expected %<class%> or %<typename%>");
22137
22138   return;
22139 }
22140
22141 /* Parse an (optional) member-specification.
22142
22143    member-specification:
22144      member-declaration member-specification [opt]
22145      access-specifier : member-specification [opt]  */
22146
22147 static void
22148 cp_parser_member_specification_opt (cp_parser* parser)
22149 {
22150   while (true)
22151     {
22152       cp_token *token;
22153       enum rid keyword;
22154
22155       /* Peek at the next token.  */
22156       token = cp_lexer_peek_token (parser->lexer);
22157       /* If it's a `}', or EOF then we've seen all the members.  */
22158       if (token->type == CPP_CLOSE_BRACE
22159           || token->type == CPP_EOF
22160           || token->type == CPP_PRAGMA_EOL)
22161         break;
22162
22163       /* See if this token is a keyword.  */
22164       keyword = token->keyword;
22165       switch (keyword)
22166         {
22167         case RID_PUBLIC:
22168         case RID_PROTECTED:
22169         case RID_PRIVATE:
22170           /* Consume the access-specifier.  */
22171           cp_lexer_consume_token (parser->lexer);
22172           /* Remember which access-specifier is active.  */
22173           current_access_specifier = token->u.value;
22174           /* Look for the `:'.  */
22175           cp_parser_require (parser, CPP_COLON, RT_COLON);
22176           break;
22177
22178         default:
22179           /* Accept #pragmas at class scope.  */
22180           if (token->type == CPP_PRAGMA)
22181             {
22182               cp_parser_pragma (parser, pragma_member, NULL);
22183               break;
22184             }
22185
22186           /* Otherwise, the next construction must be a
22187              member-declaration.  */
22188           cp_parser_member_declaration (parser);
22189         }
22190     }
22191 }
22192
22193 /* Parse a member-declaration.
22194
22195    member-declaration:
22196      decl-specifier-seq [opt] member-declarator-list [opt] ;
22197      function-definition ; [opt]
22198      :: [opt] nested-name-specifier template [opt] unqualified-id ;
22199      using-declaration
22200      template-declaration
22201      alias-declaration
22202
22203    member-declarator-list:
22204      member-declarator
22205      member-declarator-list , member-declarator
22206
22207    member-declarator:
22208      declarator pure-specifier [opt]
22209      declarator constant-initializer [opt]
22210      identifier [opt] : constant-expression
22211
22212    GNU Extensions:
22213
22214    member-declaration:
22215      __extension__ member-declaration
22216
22217    member-declarator:
22218      declarator attributes [opt] pure-specifier [opt]
22219      declarator attributes [opt] constant-initializer [opt]
22220      identifier [opt] attributes [opt] : constant-expression  
22221
22222    C++0x Extensions:
22223
22224    member-declaration:
22225      static_assert-declaration  */
22226
22227 static void
22228 cp_parser_member_declaration (cp_parser* parser)
22229 {
22230   cp_decl_specifier_seq decl_specifiers;
22231   tree prefix_attributes;
22232   tree decl;
22233   int declares_class_or_enum;
22234   bool friend_p;
22235   cp_token *token = NULL;
22236   cp_token *decl_spec_token_start = NULL;
22237   cp_token *initializer_token_start = NULL;
22238   int saved_pedantic;
22239   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22240
22241   /* Check for the `__extension__' keyword.  */
22242   if (cp_parser_extension_opt (parser, &saved_pedantic))
22243     {
22244       /* Recurse.  */
22245       cp_parser_member_declaration (parser);
22246       /* Restore the old value of the PEDANTIC flag.  */
22247       pedantic = saved_pedantic;
22248
22249       return;
22250     }
22251
22252   /* Check for a template-declaration.  */
22253   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22254     {
22255       /* An explicit specialization here is an error condition, and we
22256          expect the specialization handler to detect and report this.  */
22257       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22258           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22259         cp_parser_explicit_specialization (parser);
22260       else
22261         cp_parser_template_declaration (parser, /*member_p=*/true);
22262
22263       return;
22264     }
22265   /* Check for a template introduction.  */
22266   else if (cp_parser_template_declaration_after_export (parser, true))
22267     return;
22268
22269   /* Check for a using-declaration.  */
22270   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22271     {
22272       if (cxx_dialect < cxx11)
22273         {
22274           /* Parse the using-declaration.  */
22275           cp_parser_using_declaration (parser,
22276                                        /*access_declaration_p=*/false);
22277           return;
22278         }
22279       else
22280         {
22281           tree decl;
22282           bool alias_decl_expected;
22283           cp_parser_parse_tentatively (parser);
22284           decl = cp_parser_alias_declaration (parser);
22285           /* Note that if we actually see the '=' token after the
22286              identifier, cp_parser_alias_declaration commits the
22287              tentative parse.  In that case, we really expect an
22288              alias-declaration.  Otherwise, we expect a using
22289              declaration.  */
22290           alias_decl_expected =
22291             !cp_parser_uncommitted_to_tentative_parse_p (parser);
22292           cp_parser_parse_definitely (parser);
22293
22294           if (alias_decl_expected)
22295             finish_member_declaration (decl);
22296           else
22297             cp_parser_using_declaration (parser,
22298                                          /*access_declaration_p=*/false);
22299           return;
22300         }
22301     }
22302
22303   /* Check for @defs.  */
22304   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22305     {
22306       tree ivar, member;
22307       tree ivar_chains = cp_parser_objc_defs_expression (parser);
22308       ivar = ivar_chains;
22309       while (ivar)
22310         {
22311           member = ivar;
22312           ivar = TREE_CHAIN (member);
22313           TREE_CHAIN (member) = NULL_TREE;
22314           finish_member_declaration (member);
22315         }
22316       return;
22317     }
22318
22319   /* If the next token is `static_assert' we have a static assertion.  */
22320   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22321     {
22322       cp_parser_static_assert (parser, /*member_p=*/true);
22323       return;
22324     }
22325
22326   parser->colon_corrects_to_scope_p = false;
22327
22328   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22329       goto out;
22330
22331   /* Parse the decl-specifier-seq.  */
22332   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22333   cp_parser_decl_specifier_seq (parser,
22334                                 CP_PARSER_FLAGS_OPTIONAL,
22335                                 &decl_specifiers,
22336                                 &declares_class_or_enum);
22337   /* Check for an invalid type-name.  */
22338   if (!decl_specifiers.any_type_specifiers_p
22339       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22340     goto out;
22341   /* If there is no declarator, then the decl-specifier-seq should
22342      specify a type.  */
22343   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22344     {
22345       /* If there was no decl-specifier-seq, and the next token is a
22346          `;', then we have something like:
22347
22348            struct S { ; };
22349
22350          [class.mem]
22351
22352          Each member-declaration shall declare at least one member
22353          name of the class.  */
22354       if (!decl_specifiers.any_specifiers_p)
22355         {
22356           cp_token *token = cp_lexer_peek_token (parser->lexer);
22357           if (!in_system_header_at (token->location))
22358             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22359         }
22360       else
22361         {
22362           tree type;
22363
22364           /* See if this declaration is a friend.  */
22365           friend_p = cp_parser_friend_p (&decl_specifiers);
22366           /* If there were decl-specifiers, check to see if there was
22367              a class-declaration.  */
22368           type = check_tag_decl (&decl_specifiers,
22369                                  /*explicit_type_instantiation_p=*/false);
22370           /* Nested classes have already been added to the class, but
22371              a `friend' needs to be explicitly registered.  */
22372           if (friend_p)
22373             {
22374               /* If the `friend' keyword was present, the friend must
22375                  be introduced with a class-key.  */
22376                if (!declares_class_or_enum && cxx_dialect < cxx11)
22377                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22378                           "in C++03 a class-key must be used "
22379                           "when declaring a friend");
22380                /* In this case:
22381
22382                     template <typename T> struct A {
22383                       friend struct A<T>::B;
22384                     };
22385
22386                   A<T>::B will be represented by a TYPENAME_TYPE, and
22387                   therefore not recognized by check_tag_decl.  */
22388                if (!type)
22389                  {
22390                    type = decl_specifiers.type;
22391                    if (type && TREE_CODE (type) == TYPE_DECL)
22392                      type = TREE_TYPE (type);
22393                  }
22394                if (!type || !TYPE_P (type))
22395                  error_at (decl_spec_token_start->location,
22396                            "friend declaration does not name a class or "
22397                            "function");
22398                else
22399                  make_friend_class (current_class_type, type,
22400                                     /*complain=*/true);
22401             }
22402           /* If there is no TYPE, an error message will already have
22403              been issued.  */
22404           else if (!type || type == error_mark_node)
22405             ;
22406           /* An anonymous aggregate has to be handled specially; such
22407              a declaration really declares a data member (with a
22408              particular type), as opposed to a nested class.  */
22409           else if (ANON_AGGR_TYPE_P (type))
22410             {
22411               /* C++11 9.5/6.  */
22412               if (decl_specifiers.storage_class != sc_none)
22413                 error_at (decl_spec_token_start->location,
22414                           "a storage class on an anonymous aggregate "
22415                           "in class scope is not allowed");
22416
22417               /* Remove constructors and such from TYPE, now that we
22418                  know it is an anonymous aggregate.  */
22419               fixup_anonymous_aggr (type);
22420               /* And make the corresponding data member.  */
22421               decl = build_decl (decl_spec_token_start->location,
22422                                  FIELD_DECL, NULL_TREE, type);
22423               /* Add it to the class.  */
22424               finish_member_declaration (decl);
22425             }
22426           else
22427             cp_parser_check_access_in_redeclaration
22428                                               (TYPE_NAME (type),
22429                                                decl_spec_token_start->location);
22430         }
22431     }
22432   else
22433     {
22434       bool assume_semicolon = false;
22435
22436       /* Clear attributes from the decl_specifiers but keep them
22437          around as prefix attributes that apply them to the entity
22438          being declared.  */
22439       prefix_attributes = decl_specifiers.attributes;
22440       decl_specifiers.attributes = NULL_TREE;
22441
22442       /* See if these declarations will be friends.  */
22443       friend_p = cp_parser_friend_p (&decl_specifiers);
22444
22445       /* Keep going until we hit the `;' at the end of the
22446          declaration.  */
22447       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22448         {
22449           tree attributes = NULL_TREE;
22450           tree first_attribute;
22451
22452           /* Peek at the next token.  */
22453           token = cp_lexer_peek_token (parser->lexer);
22454
22455           /* Check for a bitfield declaration.  */
22456           if (token->type == CPP_COLON
22457               || (token->type == CPP_NAME
22458                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22459                   == CPP_COLON))
22460             {
22461               tree identifier;
22462               tree width;
22463
22464               /* Get the name of the bitfield.  Note that we cannot just
22465                  check TOKEN here because it may have been invalidated by
22466                  the call to cp_lexer_peek_nth_token above.  */
22467               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22468                 identifier = cp_parser_identifier (parser);
22469               else
22470                 identifier = NULL_TREE;
22471
22472               /* Consume the `:' token.  */
22473               cp_lexer_consume_token (parser->lexer);
22474               /* Get the width of the bitfield.  */
22475               width
22476                 = cp_parser_constant_expression (parser);
22477
22478               /* Look for attributes that apply to the bitfield.  */
22479               attributes = cp_parser_attributes_opt (parser);
22480               /* Remember which attributes are prefix attributes and
22481                  which are not.  */
22482               first_attribute = attributes;
22483               /* Combine the attributes.  */
22484               attributes = chainon (prefix_attributes, attributes);
22485
22486               /* Create the bitfield declaration.  */
22487               decl = grokbitfield (identifier
22488                                    ? make_id_declarator (NULL_TREE,
22489                                                          identifier,
22490                                                          sfk_none)
22491                                    : NULL,
22492                                    &decl_specifiers,
22493                                    width,
22494                                    attributes);
22495             }
22496           else
22497             {
22498               cp_declarator *declarator;
22499               tree initializer;
22500               tree asm_specification;
22501               int ctor_dtor_or_conv_p;
22502
22503               /* Parse the declarator.  */
22504               declarator
22505                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22506                                         &ctor_dtor_or_conv_p,
22507                                         /*parenthesized_p=*/NULL,
22508                                         /*member_p=*/true,
22509                                         friend_p);
22510
22511               /* If something went wrong parsing the declarator, make sure
22512                  that we at least consume some tokens.  */
22513               if (declarator == cp_error_declarator)
22514                 {
22515                   /* Skip to the end of the statement.  */
22516                   cp_parser_skip_to_end_of_statement (parser);
22517                   /* If the next token is not a semicolon, that is
22518                      probably because we just skipped over the body of
22519                      a function.  So, we consume a semicolon if
22520                      present, but do not issue an error message if it
22521                      is not present.  */
22522                   if (cp_lexer_next_token_is (parser->lexer,
22523                                               CPP_SEMICOLON))
22524                     cp_lexer_consume_token (parser->lexer);
22525                   goto out;
22526                 }
22527
22528               if (declares_class_or_enum & 2)
22529                 cp_parser_check_for_definition_in_return_type
22530                                             (declarator, decl_specifiers.type,
22531                                              decl_specifiers.locations[ds_type_spec]);
22532
22533               /* Look for an asm-specification.  */
22534               asm_specification = cp_parser_asm_specification_opt (parser);
22535               /* Look for attributes that apply to the declaration.  */
22536               attributes = cp_parser_attributes_opt (parser);
22537               /* Remember which attributes are prefix attributes and
22538                  which are not.  */
22539               first_attribute = attributes;
22540               /* Combine the attributes.  */
22541               attributes = chainon (prefix_attributes, attributes);
22542
22543               /* If it's an `=', then we have a constant-initializer or a
22544                  pure-specifier.  It is not correct to parse the
22545                  initializer before registering the member declaration
22546                  since the member declaration should be in scope while
22547                  its initializer is processed.  However, the rest of the
22548                  front end does not yet provide an interface that allows
22549                  us to handle this correctly.  */
22550               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22551                 {
22552                   /* In [class.mem]:
22553
22554                      A pure-specifier shall be used only in the declaration of
22555                      a virtual function.
22556
22557                      A member-declarator can contain a constant-initializer
22558                      only if it declares a static member of integral or
22559                      enumeration type.
22560
22561                      Therefore, if the DECLARATOR is for a function, we look
22562                      for a pure-specifier; otherwise, we look for a
22563                      constant-initializer.  When we call `grokfield', it will
22564                      perform more stringent semantics checks.  */
22565                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
22566                   if (function_declarator_p (declarator)
22567                       || (decl_specifiers.type
22568                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22569                           && declarator->kind == cdk_id
22570                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22571                               == FUNCTION_TYPE)))
22572                     initializer = cp_parser_pure_specifier (parser);
22573                   else if (decl_specifiers.storage_class != sc_static)
22574                     initializer = cp_parser_save_nsdmi (parser);
22575                   else if (cxx_dialect >= cxx11)
22576                     {
22577                       bool nonconst;
22578                       /* Don't require a constant rvalue in C++11, since we
22579                          might want a reference constant.  We'll enforce
22580                          constancy later.  */
22581                       cp_lexer_consume_token (parser->lexer);
22582                       /* Parse the initializer.  */
22583                       initializer = cp_parser_initializer_clause (parser,
22584                                                                   &nonconst);
22585                     }
22586                   else
22587                     /* Parse the initializer.  */
22588                     initializer = cp_parser_constant_initializer (parser);
22589                 }
22590               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22591                        && !function_declarator_p (declarator))
22592                 {
22593                   bool x;
22594                   if (decl_specifiers.storage_class != sc_static)
22595                     initializer = cp_parser_save_nsdmi (parser);
22596                   else
22597                     initializer = cp_parser_initializer (parser, &x, &x);
22598                 }
22599               /* Otherwise, there is no initializer.  */
22600               else
22601                 initializer = NULL_TREE;
22602
22603               /* See if we are probably looking at a function
22604                  definition.  We are certainly not looking at a
22605                  member-declarator.  Calling `grokfield' has
22606                  side-effects, so we must not do it unless we are sure
22607                  that we are looking at a member-declarator.  */
22608               if (cp_parser_token_starts_function_definition_p
22609                   (cp_lexer_peek_token (parser->lexer)))
22610                 {
22611                   /* The grammar does not allow a pure-specifier to be
22612                      used when a member function is defined.  (It is
22613                      possible that this fact is an oversight in the
22614                      standard, since a pure function may be defined
22615                      outside of the class-specifier.  */
22616                   if (initializer && initializer_token_start)
22617                     error_at (initializer_token_start->location,
22618                               "pure-specifier on function-definition");
22619                   decl = cp_parser_save_member_function_body (parser,
22620                                                               &decl_specifiers,
22621                                                               declarator,
22622                                                               attributes);
22623                   if (parser->fully_implicit_function_template_p)
22624                     decl = finish_fully_implicit_template (parser, decl);
22625                   /* If the member was not a friend, declare it here.  */
22626                   if (!friend_p)
22627                     finish_member_declaration (decl);
22628                   /* Peek at the next token.  */
22629                   token = cp_lexer_peek_token (parser->lexer);
22630                   /* If the next token is a semicolon, consume it.  */
22631                   if (token->type == CPP_SEMICOLON)
22632                     cp_lexer_consume_token (parser->lexer);
22633                   goto out;
22634                 }
22635               else
22636                 if (declarator->kind == cdk_function)
22637                   declarator->id_loc = token->location;
22638               /* Create the declaration.  */
22639               decl = grokfield (declarator, &decl_specifiers,
22640                                 initializer, /*init_const_expr_p=*/true,
22641                                 asm_specification, attributes);
22642               if (parser->fully_implicit_function_template_p)
22643                 {
22644                   if (friend_p)
22645                     finish_fully_implicit_template (parser, 0);
22646                   else
22647                     decl = finish_fully_implicit_template (parser, decl);
22648                 }
22649             }
22650
22651           cp_finalize_omp_declare_simd (parser, decl);
22652           cp_finalize_oacc_routine (parser, decl, false);
22653
22654           /* Reset PREFIX_ATTRIBUTES.  */
22655           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22656             attributes = TREE_CHAIN (attributes);
22657           if (attributes)
22658             TREE_CHAIN (attributes) = NULL_TREE;
22659
22660           /* If there is any qualification still in effect, clear it
22661              now; we will be starting fresh with the next declarator.  */
22662           parser->scope = NULL_TREE;
22663           parser->qualifying_scope = NULL_TREE;
22664           parser->object_scope = NULL_TREE;
22665           /* If it's a `,', then there are more declarators.  */
22666           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22667             {
22668               cp_lexer_consume_token (parser->lexer);
22669               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22670                 {
22671                   cp_token *token = cp_lexer_previous_token (parser->lexer);
22672                   error_at (token->location,
22673                             "stray %<,%> at end of member declaration");
22674                 }
22675             }
22676           /* If the next token isn't a `;', then we have a parse error.  */
22677           else if (cp_lexer_next_token_is_not (parser->lexer,
22678                                                CPP_SEMICOLON))
22679             {
22680               /* The next token might be a ways away from where the
22681                  actual semicolon is missing.  Find the previous token
22682                  and use that for our error position.  */
22683               cp_token *token = cp_lexer_previous_token (parser->lexer);
22684               error_at (token->location,
22685                         "expected %<;%> at end of member declaration");
22686
22687               /* Assume that the user meant to provide a semicolon.  If
22688                  we were to cp_parser_skip_to_end_of_statement, we might
22689                  skip to a semicolon inside a member function definition
22690                  and issue nonsensical error messages.  */
22691               assume_semicolon = true;
22692             }
22693
22694           if (decl)
22695             {
22696               /* Add DECL to the list of members.  */
22697               if (!friend_p
22698                   /* Explicitly include, eg, NSDMIs, for better error
22699                      recovery (c++/58650).  */
22700                   || !DECL_DECLARES_FUNCTION_P (decl))
22701                 finish_member_declaration (decl);
22702
22703               if (TREE_CODE (decl) == FUNCTION_DECL)
22704                 cp_parser_save_default_args (parser, decl);
22705               else if (TREE_CODE (decl) == FIELD_DECL
22706                        && !DECL_C_BIT_FIELD (decl)
22707                        && DECL_INITIAL (decl))
22708                 /* Add DECL to the queue of NSDMI to be parsed later.  */
22709                 vec_safe_push (unparsed_nsdmis, decl);
22710             }
22711
22712           if (assume_semicolon)
22713             goto out;
22714         }
22715     }
22716
22717   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22718  out:
22719   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22720 }
22721
22722 /* Parse a pure-specifier.
22723
22724    pure-specifier:
22725      = 0
22726
22727    Returns INTEGER_ZERO_NODE if a pure specifier is found.
22728    Otherwise, ERROR_MARK_NODE is returned.  */
22729
22730 static tree
22731 cp_parser_pure_specifier (cp_parser* parser)
22732 {
22733   cp_token *token;
22734
22735   /* Look for the `=' token.  */
22736   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22737     return error_mark_node;
22738   /* Look for the `0' token.  */
22739   token = cp_lexer_peek_token (parser->lexer);
22740
22741   if (token->type == CPP_EOF
22742       || token->type == CPP_PRAGMA_EOL)
22743     return error_mark_node;
22744
22745   cp_lexer_consume_token (parser->lexer);
22746
22747   /* Accept = default or = delete in c++0x mode.  */
22748   if (token->keyword == RID_DEFAULT
22749       || token->keyword == RID_DELETE)
22750     {
22751       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22752       return token->u.value;
22753     }
22754
22755   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
22756   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22757     {
22758       cp_parser_error (parser,
22759                        "invalid pure specifier (only %<= 0%> is allowed)");
22760       cp_parser_skip_to_end_of_statement (parser);
22761       return error_mark_node;
22762     }
22763   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22764     {
22765       error_at (token->location, "templates may not be %<virtual%>");
22766       return error_mark_node;
22767     }
22768
22769   return integer_zero_node;
22770 }
22771
22772 /* Parse a constant-initializer.
22773
22774    constant-initializer:
22775      = constant-expression
22776
22777    Returns a representation of the constant-expression.  */
22778
22779 static tree
22780 cp_parser_constant_initializer (cp_parser* parser)
22781 {
22782   /* Look for the `=' token.  */
22783   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22784     return error_mark_node;
22785
22786   /* It is invalid to write:
22787
22788        struct S { static const int i = { 7 }; };
22789
22790      */
22791   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22792     {
22793       cp_parser_error (parser,
22794                        "a brace-enclosed initializer is not allowed here");
22795       /* Consume the opening brace.  */
22796       cp_lexer_consume_token (parser->lexer);
22797       /* Skip the initializer.  */
22798       cp_parser_skip_to_closing_brace (parser);
22799       /* Look for the trailing `}'.  */
22800       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22801
22802       return error_mark_node;
22803     }
22804
22805   return cp_parser_constant_expression (parser);
22806 }
22807
22808 /* Derived classes [gram.class.derived] */
22809
22810 /* Parse a base-clause.
22811
22812    base-clause:
22813      : base-specifier-list
22814
22815    base-specifier-list:
22816      base-specifier ... [opt]
22817      base-specifier-list , base-specifier ... [opt]
22818
22819    Returns a TREE_LIST representing the base-classes, in the order in
22820    which they were declared.  The representation of each node is as
22821    described by cp_parser_base_specifier.
22822
22823    In the case that no bases are specified, this function will return
22824    NULL_TREE, not ERROR_MARK_NODE.  */
22825
22826 static tree
22827 cp_parser_base_clause (cp_parser* parser)
22828 {
22829   tree bases = NULL_TREE;
22830
22831   /* Look for the `:' that begins the list.  */
22832   cp_parser_require (parser, CPP_COLON, RT_COLON);
22833
22834   /* Scan the base-specifier-list.  */
22835   while (true)
22836     {
22837       cp_token *token;
22838       tree base;
22839       bool pack_expansion_p = false;
22840
22841       /* Look for the base-specifier.  */
22842       base = cp_parser_base_specifier (parser);
22843       /* Look for the (optional) ellipsis. */
22844       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22845         {
22846           /* Consume the `...'. */
22847           cp_lexer_consume_token (parser->lexer);
22848
22849           pack_expansion_p = true;
22850         }
22851
22852       /* Add BASE to the front of the list.  */
22853       if (base && base != error_mark_node)
22854         {
22855           if (pack_expansion_p)
22856             /* Make this a pack expansion type. */
22857             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22858
22859           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22860             {
22861               TREE_CHAIN (base) = bases;
22862               bases = base;
22863             }
22864         }
22865       /* Peek at the next token.  */
22866       token = cp_lexer_peek_token (parser->lexer);
22867       /* If it's not a comma, then the list is complete.  */
22868       if (token->type != CPP_COMMA)
22869         break;
22870       /* Consume the `,'.  */
22871       cp_lexer_consume_token (parser->lexer);
22872     }
22873
22874   /* PARSER->SCOPE may still be non-NULL at this point, if the last
22875      base class had a qualified name.  However, the next name that
22876      appears is certainly not qualified.  */
22877   parser->scope = NULL_TREE;
22878   parser->qualifying_scope = NULL_TREE;
22879   parser->object_scope = NULL_TREE;
22880
22881   return nreverse (bases);
22882 }
22883
22884 /* Parse a base-specifier.
22885
22886    base-specifier:
22887      :: [opt] nested-name-specifier [opt] class-name
22888      virtual access-specifier [opt] :: [opt] nested-name-specifier
22889        [opt] class-name
22890      access-specifier virtual [opt] :: [opt] nested-name-specifier
22891        [opt] class-name
22892
22893    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
22894    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22895    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
22896    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
22897
22898 static tree
22899 cp_parser_base_specifier (cp_parser* parser)
22900 {
22901   cp_token *token;
22902   bool done = false;
22903   bool virtual_p = false;
22904   bool duplicate_virtual_error_issued_p = false;
22905   bool duplicate_access_error_issued_p = false;
22906   bool class_scope_p, template_p;
22907   tree access = access_default_node;
22908   tree type;
22909
22910   /* Process the optional `virtual' and `access-specifier'.  */
22911   while (!done)
22912     {
22913       /* Peek at the next token.  */
22914       token = cp_lexer_peek_token (parser->lexer);
22915       /* Process `virtual'.  */
22916       switch (token->keyword)
22917         {
22918         case RID_VIRTUAL:
22919           /* If `virtual' appears more than once, issue an error.  */
22920           if (virtual_p && !duplicate_virtual_error_issued_p)
22921             {
22922               cp_parser_error (parser,
22923                                "%<virtual%> specified more than once in base-specified");
22924               duplicate_virtual_error_issued_p = true;
22925             }
22926
22927           virtual_p = true;
22928
22929           /* Consume the `virtual' token.  */
22930           cp_lexer_consume_token (parser->lexer);
22931
22932           break;
22933
22934         case RID_PUBLIC:
22935         case RID_PROTECTED:
22936         case RID_PRIVATE:
22937           /* If more than one access specifier appears, issue an
22938              error.  */
22939           if (access != access_default_node
22940               && !duplicate_access_error_issued_p)
22941             {
22942               cp_parser_error (parser,
22943                                "more than one access specifier in base-specified");
22944               duplicate_access_error_issued_p = true;
22945             }
22946
22947           access = ridpointers[(int) token->keyword];
22948
22949           /* Consume the access-specifier.  */
22950           cp_lexer_consume_token (parser->lexer);
22951
22952           break;
22953
22954         default:
22955           done = true;
22956           break;
22957         }
22958     }
22959   /* It is not uncommon to see programs mechanically, erroneously, use
22960      the 'typename' keyword to denote (dependent) qualified types
22961      as base classes.  */
22962   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22963     {
22964       token = cp_lexer_peek_token (parser->lexer);
22965       if (!processing_template_decl)
22966         error_at (token->location,
22967                   "keyword %<typename%> not allowed outside of templates");
22968       else
22969         error_at (token->location,
22970                   "keyword %<typename%> not allowed in this context "
22971                   "(the base class is implicitly a type)");
22972       cp_lexer_consume_token (parser->lexer);
22973     }
22974
22975   /* Look for the optional `::' operator.  */
22976   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22977   /* Look for the nested-name-specifier.  The simplest way to
22978      implement:
22979
22980        [temp.res]
22981
22982        The keyword `typename' is not permitted in a base-specifier or
22983        mem-initializer; in these contexts a qualified name that
22984        depends on a template-parameter is implicitly assumed to be a
22985        type name.
22986
22987      is to pretend that we have seen the `typename' keyword at this
22988      point.  */
22989   cp_parser_nested_name_specifier_opt (parser,
22990                                        /*typename_keyword_p=*/true,
22991                                        /*check_dependency_p=*/true,
22992                                        typename_type,
22993                                        /*is_declaration=*/true);
22994   /* If the base class is given by a qualified name, assume that names
22995      we see are type names or templates, as appropriate.  */
22996   class_scope_p = (parser->scope && TYPE_P (parser->scope));
22997   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22998
22999   if (!parser->scope
23000       && cp_lexer_next_token_is_decltype (parser->lexer))
23001     /* DR 950 allows decltype as a base-specifier.  */
23002     type = cp_parser_decltype (parser);
23003   else
23004     {
23005       /* Otherwise, look for the class-name.  */
23006       type = cp_parser_class_name (parser,
23007                                    class_scope_p,
23008                                    template_p,
23009                                    typename_type,
23010                                    /*check_dependency_p=*/true,
23011                                    /*class_head_p=*/false,
23012                                    /*is_declaration=*/true);
23013       type = TREE_TYPE (type);
23014     }
23015
23016   if (type == error_mark_node)
23017     return error_mark_node;
23018
23019   return finish_base_specifier (type, access, virtual_p);
23020 }
23021
23022 /* Exception handling [gram.exception] */
23023
23024 /* Parse an (optional) noexcept-specification.
23025
23026    noexcept-specification:
23027      noexcept ( constant-expression ) [opt]
23028
23029    If no noexcept-specification is present, returns NULL_TREE.
23030    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23031    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23032    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
23033    Otherwise, returns a noexcept specification unless RETURN_COND is true,
23034    in which case a boolean condition is returned instead.  */
23035
23036 static tree
23037 cp_parser_noexcept_specification_opt (cp_parser* parser,
23038                                       bool require_constexpr,
23039                                       bool* consumed_expr,
23040                                       bool return_cond)
23041 {
23042   cp_token *token;
23043   const char *saved_message;
23044
23045   /* Peek at the next token.  */
23046   token = cp_lexer_peek_token (parser->lexer);
23047
23048   /* Is it a noexcept-specification?  */
23049   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23050     {
23051       tree expr;
23052       cp_lexer_consume_token (parser->lexer);
23053
23054       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23055         {
23056           cp_lexer_consume_token (parser->lexer);
23057
23058           if (require_constexpr)
23059             {
23060               /* Types may not be defined in an exception-specification.  */
23061               saved_message = parser->type_definition_forbidden_message;
23062               parser->type_definition_forbidden_message
23063               = G_("types may not be defined in an exception-specification");
23064
23065               expr = cp_parser_constant_expression (parser);
23066
23067               /* Restore the saved message.  */
23068               parser->type_definition_forbidden_message = saved_message;
23069             }
23070           else
23071             {
23072               expr = cp_parser_expression (parser);
23073               *consumed_expr = true;
23074             }
23075
23076           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23077         }
23078       else
23079         {
23080           expr = boolean_true_node;
23081           if (!require_constexpr)
23082             *consumed_expr = false;
23083         }
23084
23085       /* We cannot build a noexcept-spec right away because this will check
23086          that expr is a constexpr.  */
23087       if (!return_cond)
23088         return build_noexcept_spec (expr, tf_warning_or_error);
23089       else
23090         return expr;
23091     }
23092   else
23093     return NULL_TREE;
23094 }
23095
23096 /* Parse an (optional) exception-specification.
23097
23098    exception-specification:
23099      throw ( type-id-list [opt] )
23100
23101    Returns a TREE_LIST representing the exception-specification.  The
23102    TREE_VALUE of each node is a type.  */
23103
23104 static tree
23105 cp_parser_exception_specification_opt (cp_parser* parser)
23106 {
23107   cp_token *token;
23108   tree type_id_list;
23109   const char *saved_message;
23110
23111   /* Peek at the next token.  */
23112   token = cp_lexer_peek_token (parser->lexer);
23113
23114   /* Is it a noexcept-specification?  */
23115   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23116                                                       false);
23117   if (type_id_list != NULL_TREE)
23118     return type_id_list;
23119
23120   /* If it's not `throw', then there's no exception-specification.  */
23121   if (!cp_parser_is_keyword (token, RID_THROW))
23122     return NULL_TREE;
23123
23124 #if 0
23125   /* Enable this once a lot of code has transitioned to noexcept?  */
23126   if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23127     warning (OPT_Wdeprecated, "dynamic exception specifications are "
23128              "deprecated in C++0x; use %<noexcept%> instead");
23129 #endif
23130
23131   /* Consume the `throw'.  */
23132   cp_lexer_consume_token (parser->lexer);
23133
23134   /* Look for the `('.  */
23135   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23136
23137   /* Peek at the next token.  */
23138   token = cp_lexer_peek_token (parser->lexer);
23139   /* If it's not a `)', then there is a type-id-list.  */
23140   if (token->type != CPP_CLOSE_PAREN)
23141     {
23142       /* Types may not be defined in an exception-specification.  */
23143       saved_message = parser->type_definition_forbidden_message;
23144       parser->type_definition_forbidden_message
23145         = G_("types may not be defined in an exception-specification");
23146       /* Parse the type-id-list.  */
23147       type_id_list = cp_parser_type_id_list (parser);
23148       /* Restore the saved message.  */
23149       parser->type_definition_forbidden_message = saved_message;
23150     }
23151   else
23152     type_id_list = empty_except_spec;
23153
23154   /* Look for the `)'.  */
23155   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23156
23157   return type_id_list;
23158 }
23159
23160 /* Parse an (optional) type-id-list.
23161
23162    type-id-list:
23163      type-id ... [opt]
23164      type-id-list , type-id ... [opt]
23165
23166    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
23167    in the order that the types were presented.  */
23168
23169 static tree
23170 cp_parser_type_id_list (cp_parser* parser)
23171 {
23172   tree types = NULL_TREE;
23173
23174   while (true)
23175     {
23176       cp_token *token;
23177       tree type;
23178
23179       token = cp_lexer_peek_token (parser->lexer);
23180
23181       /* Get the next type-id.  */
23182       type = cp_parser_type_id (parser);
23183       /* Check for invalid 'auto'.  */
23184       if (flag_concepts && type_uses_auto (type))
23185         {
23186           error_at (token->location,
23187                     "invalid use of %<auto%> in exception-specification");
23188           type = error_mark_node;
23189         }
23190       /* Parse the optional ellipsis. */
23191       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23192         {
23193           /* Consume the `...'. */
23194           cp_lexer_consume_token (parser->lexer);
23195
23196           /* Turn the type into a pack expansion expression. */
23197           type = make_pack_expansion (type);
23198         }
23199       /* Add it to the list.  */
23200       types = add_exception_specifier (types, type, /*complain=*/1);
23201       /* Peek at the next token.  */
23202       token = cp_lexer_peek_token (parser->lexer);
23203       /* If it is not a `,', we are done.  */
23204       if (token->type != CPP_COMMA)
23205         break;
23206       /* Consume the `,'.  */
23207       cp_lexer_consume_token (parser->lexer);
23208     }
23209
23210   return nreverse (types);
23211 }
23212
23213 /* Parse a try-block.
23214
23215    try-block:
23216      try compound-statement handler-seq  */
23217
23218 static tree
23219 cp_parser_try_block (cp_parser* parser)
23220 {
23221   tree try_block;
23222
23223   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23224   if (parser->in_function_body
23225       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23226     error ("%<try%> in %<constexpr%> function");
23227
23228   try_block = begin_try_block ();
23229   cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23230   finish_try_block (try_block);
23231   cp_parser_handler_seq (parser);
23232   finish_handler_sequence (try_block);
23233
23234   return try_block;
23235 }
23236
23237 /* Parse a function-try-block.
23238
23239    function-try-block:
23240      try ctor-initializer [opt] function-body handler-seq  */
23241
23242 static bool
23243 cp_parser_function_try_block (cp_parser* parser)
23244 {
23245   tree compound_stmt;
23246   tree try_block;
23247   bool ctor_initializer_p;
23248
23249   /* Look for the `try' keyword.  */
23250   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23251     return false;
23252   /* Let the rest of the front end know where we are.  */
23253   try_block = begin_function_try_block (&compound_stmt);
23254   /* Parse the function-body.  */
23255   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23256     (parser, /*in_function_try_block=*/true);
23257   /* We're done with the `try' part.  */
23258   finish_function_try_block (try_block);
23259   /* Parse the handlers.  */
23260   cp_parser_handler_seq (parser);
23261   /* We're done with the handlers.  */
23262   finish_function_handler_sequence (try_block, compound_stmt);
23263
23264   return ctor_initializer_p;
23265 }
23266
23267 /* Parse a handler-seq.
23268
23269    handler-seq:
23270      handler handler-seq [opt]  */
23271
23272 static void
23273 cp_parser_handler_seq (cp_parser* parser)
23274 {
23275   while (true)
23276     {
23277       cp_token *token;
23278
23279       /* Parse the handler.  */
23280       cp_parser_handler (parser);
23281       /* Peek at the next token.  */
23282       token = cp_lexer_peek_token (parser->lexer);
23283       /* If it's not `catch' then there are no more handlers.  */
23284       if (!cp_parser_is_keyword (token, RID_CATCH))
23285         break;
23286     }
23287 }
23288
23289 /* Parse a handler.
23290
23291    handler:
23292      catch ( exception-declaration ) compound-statement  */
23293
23294 static void
23295 cp_parser_handler (cp_parser* parser)
23296 {
23297   tree handler;
23298   tree declaration;
23299
23300   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23301   handler = begin_handler ();
23302   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23303   declaration = cp_parser_exception_declaration (parser);
23304   finish_handler_parms (declaration, handler);
23305   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23306   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23307   finish_handler (handler);
23308 }
23309
23310 /* Parse an exception-declaration.
23311
23312    exception-declaration:
23313      type-specifier-seq declarator
23314      type-specifier-seq abstract-declarator
23315      type-specifier-seq
23316      ...
23317
23318    Returns a VAR_DECL for the declaration, or NULL_TREE if the
23319    ellipsis variant is used.  */
23320
23321 static tree
23322 cp_parser_exception_declaration (cp_parser* parser)
23323 {
23324   cp_decl_specifier_seq type_specifiers;
23325   cp_declarator *declarator;
23326   const char *saved_message;
23327
23328   /* If it's an ellipsis, it's easy to handle.  */
23329   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23330     {
23331       /* Consume the `...' token.  */
23332       cp_lexer_consume_token (parser->lexer);
23333       return NULL_TREE;
23334     }
23335
23336   /* Types may not be defined in exception-declarations.  */
23337   saved_message = parser->type_definition_forbidden_message;
23338   parser->type_definition_forbidden_message
23339     = G_("types may not be defined in exception-declarations");
23340
23341   /* Parse the type-specifier-seq.  */
23342   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23343                                 /*is_trailing_return=*/false,
23344                                 &type_specifiers);
23345   /* If it's a `)', then there is no declarator.  */
23346   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23347     declarator = NULL;
23348   else
23349     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23350                                        /*ctor_dtor_or_conv_p=*/NULL,
23351                                        /*parenthesized_p=*/NULL,
23352                                        /*member_p=*/false,
23353                                        /*friend_p=*/false);
23354
23355   /* Restore the saved message.  */
23356   parser->type_definition_forbidden_message = saved_message;
23357
23358   if (!type_specifiers.any_specifiers_p)
23359     return error_mark_node;
23360
23361   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23362 }
23363
23364 /* Parse a throw-expression.
23365
23366    throw-expression:
23367      throw assignment-expression [opt]
23368
23369    Returns a THROW_EXPR representing the throw-expression.  */
23370
23371 static tree
23372 cp_parser_throw_expression (cp_parser* parser)
23373 {
23374   tree expression;
23375   cp_token* token;
23376
23377   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23378   token = cp_lexer_peek_token (parser->lexer);
23379   /* Figure out whether or not there is an assignment-expression
23380      following the "throw" keyword.  */
23381   if (token->type == CPP_COMMA
23382       || token->type == CPP_SEMICOLON
23383       || token->type == CPP_CLOSE_PAREN
23384       || token->type == CPP_CLOSE_SQUARE
23385       || token->type == CPP_CLOSE_BRACE
23386       || token->type == CPP_COLON)
23387     expression = NULL_TREE;
23388   else
23389     expression = cp_parser_assignment_expression (parser);
23390
23391   return build_throw (expression);
23392 }
23393
23394 /* GNU Extensions */
23395
23396 /* Parse an (optional) asm-specification.
23397
23398    asm-specification:
23399      asm ( string-literal )
23400
23401    If the asm-specification is present, returns a STRING_CST
23402    corresponding to the string-literal.  Otherwise, returns
23403    NULL_TREE.  */
23404
23405 static tree
23406 cp_parser_asm_specification_opt (cp_parser* parser)
23407 {
23408   cp_token *token;
23409   tree asm_specification;
23410
23411   /* Peek at the next token.  */
23412   token = cp_lexer_peek_token (parser->lexer);
23413   /* If the next token isn't the `asm' keyword, then there's no
23414      asm-specification.  */
23415   if (!cp_parser_is_keyword (token, RID_ASM))
23416     return NULL_TREE;
23417
23418   /* Consume the `asm' token.  */
23419   cp_lexer_consume_token (parser->lexer);
23420   /* Look for the `('.  */
23421   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23422
23423   /* Look for the string-literal.  */
23424   asm_specification = cp_parser_string_literal (parser, false, false);
23425
23426   /* Look for the `)'.  */
23427   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23428
23429   return asm_specification;
23430 }
23431
23432 /* Parse an asm-operand-list.
23433
23434    asm-operand-list:
23435      asm-operand
23436      asm-operand-list , asm-operand
23437
23438    asm-operand:
23439      string-literal ( expression )
23440      [ string-literal ] string-literal ( expression )
23441
23442    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
23443    each node is the expression.  The TREE_PURPOSE is itself a
23444    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23445    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23446    is a STRING_CST for the string literal before the parenthesis. Returns
23447    ERROR_MARK_NODE if any of the operands are invalid.  */
23448
23449 static tree
23450 cp_parser_asm_operand_list (cp_parser* parser)
23451 {
23452   tree asm_operands = NULL_TREE;
23453   bool invalid_operands = false;
23454
23455   while (true)
23456     {
23457       tree string_literal;
23458       tree expression;
23459       tree name;
23460
23461       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23462         {
23463           /* Consume the `[' token.  */
23464           cp_lexer_consume_token (parser->lexer);
23465           /* Read the operand name.  */
23466           name = cp_parser_identifier (parser);
23467           if (name != error_mark_node)
23468             name = build_string (IDENTIFIER_LENGTH (name),
23469                                  IDENTIFIER_POINTER (name));
23470           /* Look for the closing `]'.  */
23471           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23472         }
23473       else
23474         name = NULL_TREE;
23475       /* Look for the string-literal.  */
23476       string_literal = cp_parser_string_literal (parser, false, false);
23477
23478       /* Look for the `('.  */
23479       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23480       /* Parse the expression.  */
23481       expression = cp_parser_expression (parser);
23482       /* Look for the `)'.  */
23483       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23484
23485       if (name == error_mark_node 
23486           || string_literal == error_mark_node 
23487           || expression == error_mark_node)
23488         invalid_operands = true;
23489
23490       /* Add this operand to the list.  */
23491       asm_operands = tree_cons (build_tree_list (name, string_literal),
23492                                 expression,
23493                                 asm_operands);
23494       /* If the next token is not a `,', there are no more
23495          operands.  */
23496       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23497         break;
23498       /* Consume the `,'.  */
23499       cp_lexer_consume_token (parser->lexer);
23500     }
23501
23502   return invalid_operands ? error_mark_node : nreverse (asm_operands);
23503 }
23504
23505 /* Parse an asm-clobber-list.
23506
23507    asm-clobber-list:
23508      string-literal
23509      asm-clobber-list , string-literal
23510
23511    Returns a TREE_LIST, indicating the clobbers in the order that they
23512    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
23513
23514 static tree
23515 cp_parser_asm_clobber_list (cp_parser* parser)
23516 {
23517   tree clobbers = NULL_TREE;
23518
23519   while (true)
23520     {
23521       tree string_literal;
23522
23523       /* Look for the string literal.  */
23524       string_literal = cp_parser_string_literal (parser, false, false);
23525       /* Add it to the list.  */
23526       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23527       /* If the next token is not a `,', then the list is
23528          complete.  */
23529       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23530         break;
23531       /* Consume the `,' token.  */
23532       cp_lexer_consume_token (parser->lexer);
23533     }
23534
23535   return clobbers;
23536 }
23537
23538 /* Parse an asm-label-list.
23539
23540    asm-label-list:
23541      identifier
23542      asm-label-list , identifier
23543
23544    Returns a TREE_LIST, indicating the labels in the order that they
23545    appeared.  The TREE_VALUE of each node is a label.  */
23546
23547 static tree
23548 cp_parser_asm_label_list (cp_parser* parser)
23549 {
23550   tree labels = NULL_TREE;
23551
23552   while (true)
23553     {
23554       tree identifier, label, name;
23555
23556       /* Look for the identifier.  */
23557       identifier = cp_parser_identifier (parser);
23558       if (!error_operand_p (identifier))
23559         {
23560           label = lookup_label (identifier);
23561           if (TREE_CODE (label) == LABEL_DECL)
23562             {
23563               TREE_USED (label) = 1;
23564               check_goto (label);
23565               name = build_string (IDENTIFIER_LENGTH (identifier),
23566                                    IDENTIFIER_POINTER (identifier));
23567               labels = tree_cons (name, label, labels);
23568             }
23569         }
23570       /* If the next token is not a `,', then the list is
23571          complete.  */
23572       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23573         break;
23574       /* Consume the `,' token.  */
23575       cp_lexer_consume_token (parser->lexer);
23576     }
23577
23578   return nreverse (labels);
23579 }
23580
23581 /* Return TRUE iff the next tokens in the stream are possibly the
23582    beginning of a GNU extension attribute. */
23583
23584 static bool
23585 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23586 {
23587   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23588 }
23589
23590 /* Return TRUE iff the next tokens in the stream are possibly the
23591    beginning of a standard C++-11 attribute specifier.  */
23592
23593 static bool
23594 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23595 {
23596   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23597 }
23598
23599 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23600    beginning of a standard C++-11 attribute specifier.  */
23601
23602 static bool
23603 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23604 {
23605   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23606
23607   return (cxx_dialect >= cxx11
23608           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23609               || (token->type == CPP_OPEN_SQUARE
23610                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23611                   && token->type == CPP_OPEN_SQUARE)));
23612 }
23613
23614 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23615    beginning of a GNU extension attribute.  */
23616
23617 static bool
23618 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23619 {
23620   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23621
23622   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23623 }
23624
23625 /* Return true iff the next tokens can be the beginning of either a
23626    GNU attribute list, or a standard C++11 attribute sequence.  */
23627
23628 static bool
23629 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23630 {
23631   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23632           || cp_next_tokens_can_be_std_attribute_p (parser));
23633 }
23634
23635 /* Return true iff the next Nth tokens can be the beginning of either
23636    a GNU attribute list, or a standard C++11 attribute sequence.  */
23637
23638 static bool
23639 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23640 {
23641   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23642           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23643 }
23644
23645 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23646    of GNU attributes, or return NULL.  */
23647
23648 static tree
23649 cp_parser_attributes_opt (cp_parser *parser)
23650 {
23651   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23652       return cp_parser_gnu_attributes_opt (parser);
23653   return cp_parser_std_attribute_spec_seq (parser);
23654 }
23655
23656 #define CILK_SIMD_FN_CLAUSE_MASK                                  \
23657         ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH)   \
23658          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR)       \
23659          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM)      \
23660          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK)         \
23661          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23662
23663 /* Parses the Cilk Plus SIMD-enabled function's attribute.  Syntax:
23664    vector [(<clauses>)]  */
23665
23666 static void
23667 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23668 {  
23669   bool first_p = parser->cilk_simd_fn_info == NULL;
23670   cp_token *token = v_token;
23671   if (first_p)
23672     {
23673       parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23674       parser->cilk_simd_fn_info->error_seen = false;
23675       parser->cilk_simd_fn_info->fndecl_seen = false;
23676       parser->cilk_simd_fn_info->tokens = vNULL;
23677     }
23678   int paren_scope = 0;
23679   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23680     {
23681       cp_lexer_consume_token (parser->lexer);
23682       v_token = cp_lexer_peek_token (parser->lexer);
23683       paren_scope++;
23684     }
23685   while (paren_scope > 0)
23686     {
23687       token = cp_lexer_peek_token (parser->lexer);
23688       if (token->type == CPP_OPEN_PAREN)
23689         paren_scope++;
23690       else if (token->type == CPP_CLOSE_PAREN)
23691         paren_scope--;
23692       /* Do not push the last ')'  */
23693       if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23694         cp_lexer_consume_token (parser->lexer);
23695     }
23696
23697   token->type = CPP_PRAGMA_EOL;
23698   parser->lexer->next_token = token;
23699   cp_lexer_consume_token (parser->lexer);
23700
23701   struct cp_token_cache *cp
23702     = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23703   parser->cilk_simd_fn_info->tokens.safe_push (cp);
23704 }
23705
23706 /* Parse an (optional) series of attributes.
23707
23708    attributes:
23709      attributes attribute
23710
23711    attribute:
23712      __attribute__ (( attribute-list [opt] ))
23713
23714    The return value is as for cp_parser_gnu_attribute_list.  */
23715
23716 static tree
23717 cp_parser_gnu_attributes_opt (cp_parser* parser)
23718 {
23719   tree attributes = NULL_TREE;
23720
23721   while (true)
23722     {
23723       cp_token *token;
23724       tree attribute_list;
23725       bool ok = true;
23726
23727       /* Peek at the next token.  */
23728       token = cp_lexer_peek_token (parser->lexer);
23729       /* If it's not `__attribute__', then we're done.  */
23730       if (token->keyword != RID_ATTRIBUTE)
23731         break;
23732
23733       /* Consume the `__attribute__' keyword.  */
23734       cp_lexer_consume_token (parser->lexer);
23735       /* Look for the two `(' tokens.  */
23736       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23737       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23738
23739       /* Peek at the next token.  */
23740       token = cp_lexer_peek_token (parser->lexer);
23741       if (token->type != CPP_CLOSE_PAREN)
23742         /* Parse the attribute-list.  */
23743         attribute_list = cp_parser_gnu_attribute_list (parser);
23744       else
23745         /* If the next token is a `)', then there is no attribute
23746            list.  */
23747         attribute_list = NULL;
23748
23749       /* Look for the two `)' tokens.  */
23750       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23751         ok = false;
23752       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23753         ok = false;
23754       if (!ok)
23755         cp_parser_skip_to_end_of_statement (parser);
23756
23757       /* Add these new attributes to the list.  */
23758       attributes = chainon (attributes, attribute_list);
23759     }
23760
23761   return attributes;
23762 }
23763
23764 /* Parse a GNU attribute-list.
23765
23766    attribute-list:
23767      attribute
23768      attribute-list , attribute
23769
23770    attribute:
23771      identifier
23772      identifier ( identifier )
23773      identifier ( identifier , expression-list )
23774      identifier ( expression-list )
23775
23776    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
23777    to an attribute.  The TREE_PURPOSE of each node is the identifier
23778    indicating which attribute is in use.  The TREE_VALUE represents
23779    the arguments, if any.  */
23780
23781 static tree
23782 cp_parser_gnu_attribute_list (cp_parser* parser)
23783 {
23784   tree attribute_list = NULL_TREE;
23785   bool save_translate_strings_p = parser->translate_strings_p;
23786
23787   parser->translate_strings_p = false;
23788   while (true)
23789     {
23790       cp_token *token;
23791       tree identifier;
23792       tree attribute;
23793
23794       /* Look for the identifier.  We also allow keywords here; for
23795          example `__attribute__ ((const))' is legal.  */
23796       token = cp_lexer_peek_token (parser->lexer);
23797       if (token->type == CPP_NAME
23798           || token->type == CPP_KEYWORD)
23799         {
23800           tree arguments = NULL_TREE;
23801
23802           /* Consume the token, but save it since we need it for the
23803              SIMD enabled function parsing.  */
23804           cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23805
23806           /* Save away the identifier that indicates which attribute
23807              this is.  */
23808           identifier = (token->type == CPP_KEYWORD) 
23809             /* For keywords, use the canonical spelling, not the
23810                parsed identifier.  */
23811             ? ridpointers[(int) token->keyword]
23812             : id_token->u.value;
23813           
23814           attribute = build_tree_list (identifier, NULL_TREE);
23815
23816           /* Peek at the next token.  */
23817           token = cp_lexer_peek_token (parser->lexer);
23818           /* If it's an `(', then parse the attribute arguments.  */
23819           if (token->type == CPP_OPEN_PAREN)
23820             {
23821               vec<tree, va_gc> *vec;
23822               int attr_flag = (attribute_takes_identifier_p (identifier)
23823                                ? id_attr : normal_attr);
23824               if (is_cilkplus_vector_p (identifier))
23825                 {
23826                   cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23827                   continue;
23828                 }
23829               else
23830                 vec = cp_parser_parenthesized_expression_list 
23831                   (parser, attr_flag, /*cast_p=*/false, 
23832                    /*allow_expansion_p=*/false, 
23833                    /*non_constant_p=*/NULL);
23834               if (vec == NULL)
23835                 arguments = error_mark_node;
23836               else
23837                 {
23838                   arguments = build_tree_list_vec (vec);
23839                   release_tree_vector (vec);
23840                 }
23841               /* Save the arguments away.  */
23842               TREE_VALUE (attribute) = arguments;
23843             }
23844           else if (is_cilkplus_vector_p (identifier))
23845             {
23846               cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23847               continue;
23848             }
23849
23850           if (arguments != error_mark_node)
23851             {
23852               /* Add this attribute to the list.  */
23853               TREE_CHAIN (attribute) = attribute_list;
23854               attribute_list = attribute;
23855             }
23856
23857           token = cp_lexer_peek_token (parser->lexer);
23858         }
23859       /* Now, look for more attributes.  If the next token isn't a
23860          `,', we're done.  */
23861       if (token->type != CPP_COMMA)
23862         break;
23863
23864       /* Consume the comma and keep going.  */
23865       cp_lexer_consume_token (parser->lexer);
23866     }
23867   parser->translate_strings_p = save_translate_strings_p;
23868
23869   /* We built up the list in reverse order.  */
23870   return nreverse (attribute_list);
23871 }
23872
23873 /*  Parse a standard C++11 attribute.
23874
23875     The returned representation is a TREE_LIST which TREE_PURPOSE is
23876     the scoped name of the attribute, and the TREE_VALUE is its
23877     arguments list.
23878
23879     Note that the scoped name of the attribute is itself a TREE_LIST
23880     which TREE_PURPOSE is the namespace of the attribute, and
23881     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
23882     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23883     and which TREE_PURPOSE is directly the attribute name.
23884
23885     Clients of the attribute code should use get_attribute_namespace
23886     and get_attribute_name to get the actual namespace and name of
23887     attributes, regardless of their being GNU or C++11 attributes.
23888
23889     attribute:
23890       attribute-token attribute-argument-clause [opt]
23891
23892     attribute-token:
23893       identifier
23894       attribute-scoped-token
23895
23896     attribute-scoped-token:
23897       attribute-namespace :: identifier
23898
23899     attribute-namespace:
23900       identifier
23901
23902     attribute-argument-clause:
23903       ( balanced-token-seq )
23904
23905     balanced-token-seq:
23906       balanced-token [opt]
23907       balanced-token-seq balanced-token
23908
23909     balanced-token:
23910       ( balanced-token-seq )
23911       [ balanced-token-seq ]
23912       { balanced-token-seq }.  */
23913
23914 static tree
23915 cp_parser_std_attribute (cp_parser *parser)
23916 {
23917   tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23918   cp_token *token;
23919
23920   /* First, parse name of the attribute, a.k.a attribute-token.  */
23921
23922   token = cp_lexer_peek_token (parser->lexer);
23923   if (token->type == CPP_NAME)
23924     attr_id = token->u.value;
23925   else if (token->type == CPP_KEYWORD)
23926     attr_id = ridpointers[(int) token->keyword];
23927   else if (token->flags & NAMED_OP)
23928     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23929
23930   if (attr_id == NULL_TREE)
23931     return NULL_TREE;
23932
23933   cp_lexer_consume_token (parser->lexer);
23934
23935   token = cp_lexer_peek_token (parser->lexer);
23936   if (token->type == CPP_SCOPE)
23937     {
23938       /* We are seeing a scoped attribute token.  */
23939
23940       cp_lexer_consume_token (parser->lexer);
23941       attr_ns = attr_id;
23942
23943       token = cp_lexer_consume_token (parser->lexer);
23944       if (token->type == CPP_NAME)
23945         attr_id = token->u.value;
23946       else if (token->type == CPP_KEYWORD)
23947         attr_id = ridpointers[(int) token->keyword];
23948       else
23949         {
23950           error_at (token->location,
23951                     "expected an identifier for the attribute name");
23952           return error_mark_node;
23953         }
23954       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23955                                    NULL_TREE);
23956       token = cp_lexer_peek_token (parser->lexer);
23957     }
23958   else
23959     {
23960       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23961                                    NULL_TREE);
23962       /* C++11 noreturn attribute is equivalent to GNU's.  */
23963       if (is_attribute_p ("noreturn", attr_id))
23964         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23965       /* C++14 deprecated attribute is equivalent to GNU's.  */
23966       else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23967         {
23968           if (cxx_dialect == cxx11)
23969             pedwarn (token->location, OPT_Wpedantic,
23970                      "%<deprecated%> is a C++14 feature;"
23971                      " use %<gnu::deprecated%>");
23972           TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23973         }
23974       /* Transactional Memory TS optimize_for_synchronized attribute is
23975          equivalent to GNU transaction_callable.  */
23976       else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23977         TREE_PURPOSE (attribute)
23978           = get_identifier ("transaction_callable");
23979       /* Transactional Memory attributes are GNU attributes.  */
23980       else if (tm_attr_to_mask (attr_id))
23981         TREE_PURPOSE (attribute) = attr_id;
23982     }
23983
23984   /* Now parse the optional argument clause of the attribute.  */
23985
23986   if (token->type != CPP_OPEN_PAREN)
23987     return attribute;
23988
23989   {
23990     vec<tree, va_gc> *vec;
23991     int attr_flag = normal_attr;
23992
23993     if (attr_ns == get_identifier ("gnu")
23994         && attribute_takes_identifier_p (attr_id))
23995       /* A GNU attribute that takes an identifier in parameter.  */
23996       attr_flag = id_attr;
23997
23998     vec = cp_parser_parenthesized_expression_list
23999       (parser, attr_flag, /*cast_p=*/false,
24000        /*allow_expansion_p=*/true,
24001        /*non_constant_p=*/NULL);
24002     if (vec == NULL)
24003       arguments = error_mark_node;
24004     else
24005       {
24006         arguments = build_tree_list_vec (vec);
24007         release_tree_vector (vec);
24008       }
24009
24010     if (arguments == error_mark_node)
24011       attribute = error_mark_node;
24012     else
24013       TREE_VALUE (attribute) = arguments;
24014   }
24015
24016   return attribute;
24017 }
24018
24019 /* Check that the attribute ATTRIBUTE appears at most once in the
24020    attribute-list ATTRIBUTES.  This is enforced for noreturn (7.6.3)
24021    and deprecated (7.6.5).  Note that carries_dependency (7.6.4)
24022    isn't implemented yet in GCC.  */
24023
24024 static void
24025 cp_parser_check_std_attribute (tree attributes, tree attribute)
24026 {
24027   if (attributes)
24028     {
24029       tree name = get_attribute_name (attribute);
24030       if (is_attribute_p ("noreturn", name)
24031           && lookup_attribute ("noreturn", attributes))
24032         error ("attribute noreturn can appear at most once "
24033                "in an attribute-list");
24034       else if (is_attribute_p ("deprecated", name)
24035                && lookup_attribute ("deprecated", attributes))
24036         error ("attribute deprecated can appear at most once "
24037                "in an attribute-list");
24038     }
24039 }
24040
24041 /* Parse a list of standard C++-11 attributes.
24042
24043    attribute-list:
24044      attribute [opt]
24045      attribute-list , attribute[opt]
24046      attribute ...
24047      attribute-list , attribute ...
24048 */
24049
24050 static tree
24051 cp_parser_std_attribute_list (cp_parser *parser)
24052 {
24053   tree attributes = NULL_TREE, attribute = NULL_TREE;
24054   cp_token *token = NULL;
24055
24056   while (true)
24057     {
24058       attribute = cp_parser_std_attribute (parser);
24059       if (attribute == error_mark_node)
24060         break;
24061       if (attribute != NULL_TREE)
24062         {
24063           cp_parser_check_std_attribute (attributes, attribute);
24064           TREE_CHAIN (attribute) = attributes;
24065           attributes = attribute;
24066         }
24067       token = cp_lexer_peek_token (parser->lexer);
24068       if (token->type == CPP_ELLIPSIS)
24069         {
24070           cp_lexer_consume_token (parser->lexer);
24071           if (attribute == NULL_TREE)
24072             error_at (token->location,
24073                       "expected attribute before %<...%>");
24074           else
24075             TREE_VALUE (attribute)
24076               = make_pack_expansion (TREE_VALUE (attribute));
24077           token = cp_lexer_peek_token (parser->lexer);
24078         }
24079       if (token->type != CPP_COMMA)
24080         break;
24081       cp_lexer_consume_token (parser->lexer);
24082     }
24083   attributes = nreverse (attributes);
24084   return attributes;
24085 }
24086
24087 /* Parse a standard C++-11 attribute specifier.
24088
24089    attribute-specifier:
24090      [ [ attribute-list ] ]
24091      alignment-specifier
24092
24093    alignment-specifier:
24094      alignas ( type-id ... [opt] )
24095      alignas ( alignment-expression ... [opt] ).  */
24096
24097 static tree
24098 cp_parser_std_attribute_spec (cp_parser *parser)
24099 {
24100   tree attributes = NULL_TREE;
24101   cp_token *token = cp_lexer_peek_token (parser->lexer);
24102
24103   if (token->type == CPP_OPEN_SQUARE
24104       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24105     {
24106       cp_lexer_consume_token (parser->lexer);
24107       cp_lexer_consume_token (parser->lexer);
24108
24109       attributes = cp_parser_std_attribute_list (parser);
24110
24111       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24112           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24113         cp_parser_skip_to_end_of_statement (parser);
24114       else
24115         /* Warn about parsing c++11 attribute in non-c++1 mode, only
24116            when we are sure that we have actually parsed them.  */
24117         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24118     }
24119   else
24120     {
24121       tree alignas_expr;
24122
24123       /* Look for an alignment-specifier.  */
24124
24125       token = cp_lexer_peek_token (parser->lexer);
24126
24127       if (token->type != CPP_KEYWORD
24128           || token->keyword != RID_ALIGNAS)
24129         return NULL_TREE;
24130
24131       cp_lexer_consume_token (parser->lexer);
24132       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24133
24134       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24135         {
24136           cp_parser_error (parser, "expected %<(%>");
24137           return error_mark_node;
24138         }
24139
24140       cp_parser_parse_tentatively (parser);
24141       alignas_expr = cp_parser_type_id (parser);
24142
24143       if (!cp_parser_parse_definitely (parser))
24144         {
24145           alignas_expr = cp_parser_assignment_expression (parser);
24146           if (alignas_expr == error_mark_node)
24147             cp_parser_skip_to_end_of_statement (parser);
24148           if (alignas_expr == NULL_TREE
24149               || alignas_expr == error_mark_node)
24150             return alignas_expr;
24151         }
24152
24153       alignas_expr = cxx_alignas_expr (alignas_expr);
24154       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24155
24156       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24157         {
24158           cp_lexer_consume_token (parser->lexer);
24159           alignas_expr = make_pack_expansion (alignas_expr);
24160         }
24161
24162       if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24163         {
24164           cp_parser_error (parser, "expected %<)%>");
24165           return error_mark_node;
24166         }
24167
24168       /* Build the C++-11 representation of an 'aligned'
24169          attribute.  */
24170       attributes =
24171         build_tree_list (build_tree_list (get_identifier ("gnu"),
24172                                           get_identifier ("aligned")),
24173                          alignas_expr);
24174     }
24175
24176   return attributes;
24177 }
24178
24179 /* Parse a standard C++-11 attribute-specifier-seq.
24180
24181    attribute-specifier-seq:
24182      attribute-specifier-seq [opt] attribute-specifier
24183  */
24184
24185 static tree
24186 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24187 {
24188   tree attr_specs = NULL_TREE;
24189   tree attr_last = NULL_TREE;
24190
24191   while (true)
24192     {
24193       tree attr_spec = cp_parser_std_attribute_spec (parser);
24194       if (attr_spec == NULL_TREE)
24195         break;
24196       if (attr_spec == error_mark_node)
24197         return error_mark_node;
24198
24199       if (attr_last)
24200         TREE_CHAIN (attr_last) = attr_spec;
24201       else
24202         attr_specs = attr_last = attr_spec;
24203       attr_last = tree_last (attr_last);
24204     }
24205
24206   return attr_specs;
24207 }
24208
24209 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
24210    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
24211    current value of the PEDANTIC flag, regardless of whether or not
24212    the `__extension__' keyword is present.  The caller is responsible
24213    for restoring the value of the PEDANTIC flag.  */
24214
24215 static bool
24216 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24217 {
24218   /* Save the old value of the PEDANTIC flag.  */
24219   *saved_pedantic = pedantic;
24220
24221   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24222     {
24223       /* Consume the `__extension__' token.  */
24224       cp_lexer_consume_token (parser->lexer);
24225       /* We're not being pedantic while the `__extension__' keyword is
24226          in effect.  */
24227       pedantic = 0;
24228
24229       return true;
24230     }
24231
24232   return false;
24233 }
24234
24235 /* Parse a label declaration.
24236
24237    label-declaration:
24238      __label__ label-declarator-seq ;
24239
24240    label-declarator-seq:
24241      identifier , label-declarator-seq
24242      identifier  */
24243
24244 static void
24245 cp_parser_label_declaration (cp_parser* parser)
24246 {
24247   /* Look for the `__label__' keyword.  */
24248   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24249
24250   while (true)
24251     {
24252       tree identifier;
24253
24254       /* Look for an identifier.  */
24255       identifier = cp_parser_identifier (parser);
24256       /* If we failed, stop.  */
24257       if (identifier == error_mark_node)
24258         break;
24259       /* Declare it as a label.  */
24260       finish_label_decl (identifier);
24261       /* If the next token is a `;', stop.  */
24262       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24263         break;
24264       /* Look for the `,' separating the label declarations.  */
24265       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24266     }
24267
24268   /* Look for the final `;'.  */
24269   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24270 }
24271
24272 // -------------------------------------------------------------------------- //
24273 // Requires Clause
24274
24275 // Parse a requires clause.
24276 //
24277 //    requires-clause:
24278 //      'requires' logical-or-expression
24279 //
24280 // The required logical-or-expression must be a constant expression. Note
24281 // that we don't check that the expression is constepxr here. We defer until
24282 // we analyze constraints and then, we only check atomic constraints.
24283 static tree
24284 cp_parser_requires_clause (cp_parser *parser)
24285 {
24286   // Parse the requires clause so that it is not automatically folded.
24287   ++processing_template_decl;
24288   tree expr = cp_parser_binary_expression (parser, false, false,
24289                                            PREC_NOT_OPERATOR, NULL);
24290   if (check_for_bare_parameter_packs (expr))
24291     expr = error_mark_node;
24292   --processing_template_decl;
24293   return expr;
24294 }
24295
24296 // Optionally parse a requires clause:
24297 static tree
24298 cp_parser_requires_clause_opt (cp_parser *parser)
24299 {
24300   cp_token *tok = cp_lexer_peek_token (parser->lexer);
24301   if (tok->keyword != RID_REQUIRES)
24302     {
24303       if (!flag_concepts && tok->type == CPP_NAME
24304           && tok->u.value == ridpointers[RID_REQUIRES])
24305         {
24306           error_at (cp_lexer_peek_token (parser->lexer)->location,
24307                     "%<requires%> only available with -fconcepts");
24308           /* Parse and discard the requires-clause.  */
24309           cp_lexer_consume_token (parser->lexer);
24310           cp_parser_requires_clause (parser);
24311         }
24312       return NULL_TREE;
24313     }
24314   cp_lexer_consume_token (parser->lexer);
24315   return cp_parser_requires_clause (parser);
24316 }
24317
24318
24319 /*---------------------------------------------------------------------------
24320                            Requires expressions
24321 ---------------------------------------------------------------------------*/
24322
24323 /* Parse a requires expression
24324
24325    requirement-expression:
24326        'requires' requirement-parameter-list [opt] requirement-body */
24327 static tree
24328 cp_parser_requires_expression (cp_parser *parser)
24329 {
24330   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24331   location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24332
24333   /* A requires-expression shall appear only within a concept
24334      definition or a requires-clause.
24335
24336      TODO: Implement this diagnostic correctly. */
24337   if (!processing_template_decl)
24338     {
24339       error_at (loc, "a requires expression cannot appear outside a template");
24340       cp_parser_skip_to_end_of_statement (parser);
24341       return error_mark_node;
24342     }
24343
24344   tree parms, reqs;
24345   {
24346     /* Local parameters are delared as variables within the scope
24347        of the expression.  They are not visible past the end of
24348        the expression.  Expressions within the requires-expression
24349        are unevaluated.  */
24350     struct scope_sentinel
24351     {
24352       scope_sentinel ()
24353       {
24354         ++cp_unevaluated_operand;
24355         begin_scope (sk_block, NULL_TREE);
24356       }
24357
24358       ~scope_sentinel ()
24359       {
24360         pop_bindings_and_leave_scope ();
24361         --cp_unevaluated_operand;
24362       }
24363     } s;
24364
24365     /* Parse the optional parameter list. */
24366     if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24367       {
24368         parms = cp_parser_requirement_parameter_list (parser);
24369         if (parms == error_mark_node)
24370           return error_mark_node;
24371       }
24372     else
24373       parms = NULL_TREE;
24374
24375     /* Parse the requirement body. */
24376     reqs = cp_parser_requirement_body (parser);
24377     if (reqs == error_mark_node)
24378       return error_mark_node;
24379   }
24380
24381   /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24382      the parm chain.  */
24383   grokparms (parms, &parms);
24384   return finish_requires_expr (parms, reqs);
24385 }
24386
24387 /* Parse a parameterized requirement.
24388
24389    requirement-parameter-list:
24390        '(' parameter-declaration-clause ')' */
24391 static tree
24392 cp_parser_requirement_parameter_list (cp_parser *parser)
24393 {
24394   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24395     return error_mark_node;
24396
24397   tree parms = cp_parser_parameter_declaration_clause (parser);
24398
24399   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24400     return error_mark_node;
24401
24402   return parms;
24403 }
24404
24405 /* Parse the body of a requirement.
24406
24407    requirement-body:
24408        '{' requirement-list '}' */
24409 static tree
24410 cp_parser_requirement_body (cp_parser *parser)
24411 {
24412   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24413     return error_mark_node;
24414
24415   tree reqs = cp_parser_requirement_list (parser);
24416
24417   if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24418     return error_mark_node;
24419
24420   return reqs;
24421 }
24422
24423 /* Parse a list of requirements.
24424
24425    requirement-list:
24426        requirement
24427        requirement-list ';' requirement[opt] */
24428 static tree
24429 cp_parser_requirement_list (cp_parser *parser)
24430 {
24431   tree result = NULL_TREE;
24432   while (true)
24433     {
24434       tree req = cp_parser_requirement (parser);
24435       if (req == error_mark_node)
24436         return error_mark_node;
24437
24438       result = tree_cons (NULL_TREE, req, result);
24439
24440       /* If we see a semi-colon, consume it. */
24441       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24442         cp_lexer_consume_token (parser->lexer);
24443
24444       /* Stop processing at the end of the list. */
24445       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24446         break;
24447     }
24448
24449   /* Reverse the order of requirements so they are analyzed in
24450      declaration order. */
24451   return nreverse (result);
24452 }
24453
24454 /* Parse a syntactic requirement or type requirement.
24455
24456      requirement:
24457        simple-requirement
24458        compound-requirement
24459        type-requirement
24460        nested-requirement */
24461 static tree
24462 cp_parser_requirement (cp_parser *parser)
24463 {
24464   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24465     return cp_parser_compound_requirement (parser);
24466   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24467     return cp_parser_type_requirement (parser);
24468   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24469     return cp_parser_nested_requirement (parser);
24470   else
24471     return cp_parser_simple_requirement (parser);
24472 }
24473
24474 /* Parse a simple requirement.
24475
24476      simple-requirement:
24477        expression ';' */
24478 static tree
24479 cp_parser_simple_requirement (cp_parser *parser)
24480 {
24481   tree expr = cp_parser_expression (parser, NULL, false, false);
24482   if (!expr || expr == error_mark_node)
24483     return error_mark_node;
24484
24485   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24486     return error_mark_node;
24487
24488   return finish_simple_requirement (expr);
24489 }
24490
24491 /* Parse a type requirement
24492
24493      type-requirement
24494          nested-name-specifier [opt] required-type-name ';'
24495
24496      required-type-name:
24497          type-name
24498          'template' [opt] simple-template-id  */
24499 static tree
24500 cp_parser_type_requirement (cp_parser *parser)
24501 {
24502   cp_lexer_consume_token (parser->lexer);
24503
24504   // Save the scope before parsing name specifiers.
24505   tree saved_scope = parser->scope;
24506   tree saved_object_scope = parser->object_scope;
24507   tree saved_qualifying_scope = parser->qualifying_scope;
24508   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24509   cp_parser_nested_name_specifier_opt (parser,
24510                                        /*typename_keyword_p=*/true,
24511                                        /*check_dependency_p=*/false,
24512                                        /*type_p=*/true,
24513                                        /*is_declaration=*/false);
24514
24515   tree type;
24516   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24517     {
24518       cp_lexer_consume_token (parser->lexer);
24519       type = cp_parser_template_id (parser,
24520                                     /*template_keyword_p=*/true,
24521                                     /*check_dependency=*/false,
24522                                     /*tag_type=*/none_type,
24523                                     /*is_declaration=*/false);
24524       type = make_typename_type (parser->scope, type, typename_type,
24525                                  /*complain=*/tf_error);
24526     }
24527   else
24528    type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24529
24530   if (TREE_CODE (type) == TYPE_DECL)
24531     type = TREE_TYPE (type);
24532
24533   parser->scope = saved_scope;
24534   parser->object_scope = saved_object_scope;
24535   parser->qualifying_scope = saved_qualifying_scope;
24536
24537   if (type == error_mark_node)
24538     cp_parser_skip_to_end_of_statement (parser);
24539
24540   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24541     return error_mark_node;
24542   if (type == error_mark_node)
24543     return error_mark_node;
24544
24545   return finish_type_requirement (type);
24546 }
24547
24548 /* Parse a compound requirement
24549
24550      compound-requirement:
24551          '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24552 static tree
24553 cp_parser_compound_requirement (cp_parser *parser)
24554 {
24555   /* Parse an expression enclosed in '{ }'s. */
24556   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24557     return error_mark_node;
24558
24559   tree expr = cp_parser_expression (parser, NULL, false, false);
24560   if (!expr || expr == error_mark_node)
24561     return error_mark_node;
24562
24563   if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24564     return error_mark_node;
24565
24566   /* Parse the optional noexcept. */
24567   bool noexcept_p = false;
24568   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24569     {
24570       cp_lexer_consume_token (parser->lexer);
24571       noexcept_p = true;
24572     }
24573
24574   /* Parse the optional trailing return type. */
24575   tree type = NULL_TREE;
24576   if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24577     {
24578       cp_lexer_consume_token (parser->lexer);
24579       bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24580       parser->in_result_type_constraint_p = true;
24581       type = cp_parser_trailing_type_id (parser);
24582       parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24583       if (type == error_mark_node)
24584         return error_mark_node;
24585     }
24586
24587   return finish_compound_requirement (expr, type, noexcept_p);
24588 }
24589
24590 /* Parse a nested requirement. This is the same as a requires clause.
24591
24592    nested-requirement:
24593      requires-clause */
24594 static tree
24595 cp_parser_nested_requirement (cp_parser *parser)
24596 {
24597   cp_lexer_consume_token (parser->lexer);
24598   tree req = cp_parser_requires_clause (parser);
24599   if (req == error_mark_node)
24600     return error_mark_node;
24601   return finish_nested_requirement (req);
24602 }
24603
24604 /* Support Functions */
24605
24606 /* Return the appropriate prefer_type argument for lookup_name_real based on
24607    tag_type.  */
24608
24609 static inline int
24610 prefer_type_arg (tag_types tag_type)
24611 {
24612   switch (tag_type)
24613     {
24614     case none_type:  return 0;  // No preference.
24615     case scope_type: return 1;  // Type or namespace.
24616     default:         return 2;  // Type only.
24617     }
24618 }
24619
24620 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24621    NAME should have one of the representations used for an
24622    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24623    is returned.  If PARSER->SCOPE is a dependent type, then a
24624    SCOPE_REF is returned.
24625
24626    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24627    returned; the name was already resolved when the TEMPLATE_ID_EXPR
24628    was formed.  Abstractly, such entities should not be passed to this
24629    function, because they do not need to be looked up, but it is
24630    simpler to check for this special case here, rather than at the
24631    call-sites.
24632
24633    In cases not explicitly covered above, this function returns a
24634    DECL, OVERLOAD, or baselink representing the result of the lookup.
24635    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24636    is returned.
24637
24638    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24639    (e.g., "struct") that was used.  In that case bindings that do not
24640    refer to types are ignored.
24641
24642    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24643    ignored.
24644
24645    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24646    are ignored.
24647
24648    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24649    types.
24650
24651    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24652    TREE_LIST of candidates if name-lookup results in an ambiguity, and
24653    NULL_TREE otherwise.  */
24654
24655 static cp_expr
24656 cp_parser_lookup_name (cp_parser *parser, tree name,
24657                        enum tag_types tag_type,
24658                        bool is_template,
24659                        bool is_namespace,
24660                        bool check_dependency,
24661                        tree *ambiguous_decls,
24662                        location_t name_location)
24663 {
24664   tree decl;
24665   tree object_type = parser->context->object_type;
24666
24667   /* Assume that the lookup will be unambiguous.  */
24668   if (ambiguous_decls)
24669     *ambiguous_decls = NULL_TREE;
24670
24671   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24672      no longer valid.  Note that if we are parsing tentatively, and
24673      the parse fails, OBJECT_TYPE will be automatically restored.  */
24674   parser->context->object_type = NULL_TREE;
24675
24676   if (name == error_mark_node)
24677     return error_mark_node;
24678
24679   /* A template-id has already been resolved; there is no lookup to
24680      do.  */
24681   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24682     return name;
24683   if (BASELINK_P (name))
24684     {
24685       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24686                   == TEMPLATE_ID_EXPR);
24687       return name;
24688     }
24689
24690   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
24691      it should already have been checked to make sure that the name
24692      used matches the type being destroyed.  */
24693   if (TREE_CODE (name) == BIT_NOT_EXPR)
24694     {
24695       tree type;
24696
24697       /* Figure out to which type this destructor applies.  */
24698       if (parser->scope)
24699         type = parser->scope;
24700       else if (object_type)
24701         type = object_type;
24702       else
24703         type = current_class_type;
24704       /* If that's not a class type, there is no destructor.  */
24705       if (!type || !CLASS_TYPE_P (type))
24706         return error_mark_node;
24707       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24708         lazily_declare_fn (sfk_destructor, type);
24709       if (!CLASSTYPE_DESTRUCTORS (type))
24710           return error_mark_node;
24711       /* If it was a class type, return the destructor.  */
24712       return CLASSTYPE_DESTRUCTORS (type);
24713     }
24714
24715   /* By this point, the NAME should be an ordinary identifier.  If
24716      the id-expression was a qualified name, the qualifying scope is
24717      stored in PARSER->SCOPE at this point.  */
24718   gcc_assert (identifier_p (name));
24719
24720   /* Perform the lookup.  */
24721   if (parser->scope)
24722     {
24723       bool dependent_p;
24724
24725       if (parser->scope == error_mark_node)
24726         return error_mark_node;
24727
24728       /* If the SCOPE is dependent, the lookup must be deferred until
24729          the template is instantiated -- unless we are explicitly
24730          looking up names in uninstantiated templates.  Even then, we
24731          cannot look up the name if the scope is not a class type; it
24732          might, for example, be a template type parameter.  */
24733       dependent_p = (TYPE_P (parser->scope)
24734                      && dependent_scope_p (parser->scope));
24735       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24736           && dependent_p)
24737         /* Defer lookup.  */
24738         decl = error_mark_node;
24739       else
24740         {
24741           tree pushed_scope = NULL_TREE;
24742
24743           /* If PARSER->SCOPE is a dependent type, then it must be a
24744              class type, and we must not be checking dependencies;
24745              otherwise, we would have processed this lookup above.  So
24746              that PARSER->SCOPE is not considered a dependent base by
24747              lookup_member, we must enter the scope here.  */
24748           if (dependent_p)
24749             pushed_scope = push_scope (parser->scope);
24750
24751           /* If the PARSER->SCOPE is a template specialization, it
24752              may be instantiated during name lookup.  In that case,
24753              errors may be issued.  Even if we rollback the current
24754              tentative parse, those errors are valid.  */
24755           decl = lookup_qualified_name (parser->scope, name,
24756                                         prefer_type_arg (tag_type),
24757                                         /*complain=*/true);
24758
24759           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24760              lookup result and the nested-name-specifier nominates a class C:
24761                * if the name specified after the nested-name-specifier, when
24762                looked up in C, is the injected-class-name of C (Clause 9), or
24763                * if the name specified after the nested-name-specifier is the
24764                same as the identifier or the simple-template-id's template-
24765                name in the last component of the nested-name-specifier,
24766              the name is instead considered to name the constructor of
24767              class C. [ Note: for example, the constructor is not an
24768              acceptable lookup result in an elaborated-type-specifier so
24769              the constructor would not be used in place of the
24770              injected-class-name. --end note ] Such a constructor name
24771              shall be used only in the declarator-id of a declaration that
24772              names a constructor or in a using-declaration.  */
24773           if (tag_type == none_type
24774               && DECL_SELF_REFERENCE_P (decl)
24775               && same_type_p (DECL_CONTEXT (decl), parser->scope))
24776             decl = lookup_qualified_name (parser->scope, ctor_identifier,
24777                                           prefer_type_arg (tag_type),
24778                                           /*complain=*/true);
24779
24780           /* If we have a single function from a using decl, pull it out.  */
24781           if (TREE_CODE (decl) == OVERLOAD
24782               && !really_overloaded_fn (decl))
24783             decl = OVL_FUNCTION (decl);
24784
24785           if (pushed_scope)
24786             pop_scope (pushed_scope);
24787         }
24788
24789       /* If the scope is a dependent type and either we deferred lookup or
24790          we did lookup but didn't find the name, rememeber the name.  */
24791       if (decl == error_mark_node && TYPE_P (parser->scope)
24792           && dependent_type_p (parser->scope))
24793         {
24794           if (tag_type)
24795             {
24796               tree type;
24797
24798               /* The resolution to Core Issue 180 says that `struct
24799                  A::B' should be considered a type-name, even if `A'
24800                  is dependent.  */
24801               type = make_typename_type (parser->scope, name, tag_type,
24802                                          /*complain=*/tf_error);
24803               if (type != error_mark_node)
24804                 decl = TYPE_NAME (type);
24805             }
24806           else if (is_template
24807                    && (cp_parser_next_token_ends_template_argument_p (parser)
24808                        || cp_lexer_next_token_is (parser->lexer,
24809                                                   CPP_CLOSE_PAREN)))
24810             decl = make_unbound_class_template (parser->scope,
24811                                                 name, NULL_TREE,
24812                                                 /*complain=*/tf_error);
24813           else
24814             decl = build_qualified_name (/*type=*/NULL_TREE,
24815                                          parser->scope, name,
24816                                          is_template);
24817         }
24818       parser->qualifying_scope = parser->scope;
24819       parser->object_scope = NULL_TREE;
24820     }
24821   else if (object_type)
24822     {
24823       /* Look up the name in the scope of the OBJECT_TYPE, unless the
24824          OBJECT_TYPE is not a class.  */
24825       if (CLASS_TYPE_P (object_type))
24826         /* If the OBJECT_TYPE is a template specialization, it may
24827            be instantiated during name lookup.  In that case, errors
24828            may be issued.  Even if we rollback the current tentative
24829            parse, those errors are valid.  */
24830         decl = lookup_member (object_type,
24831                               name,
24832                               /*protect=*/0,
24833                               prefer_type_arg (tag_type),
24834                               tf_warning_or_error);
24835       else
24836         decl = NULL_TREE;
24837
24838       if (!decl)
24839         {
24840           /* Look it up in the enclosing context.  */
24841           decl = lookup_name_real (name, prefer_type_arg (tag_type),
24842                                    /*nonclass=*/0,
24843                                    /*block_p=*/true, is_namespace, 0);
24844           /* DR 141 says when looking for a template-name after -> or ., only
24845              consider class templates.  We need to fix our handling of
24846              dependent expressions to implement that properly, but for now
24847              let's ignore namespace-scope function templates.  */
24848           if (decl && is_template && !DECL_TYPE_TEMPLATE_P (decl))
24849             {
24850               tree d = decl;
24851               if (is_overloaded_fn (d))
24852                 d = get_first_fn (d);
24853               if (DECL_P (d) && !DECL_CLASS_SCOPE_P (d))
24854                 decl = NULL_TREE;
24855             }
24856         }
24857       if (object_type == unknown_type_node)
24858         /* The object is type-dependent, so we can't look anything up; we used
24859            this to get the DR 141 behavior.  */
24860         object_type = NULL_TREE;
24861       parser->object_scope = object_type;
24862       parser->qualifying_scope = NULL_TREE;
24863     }
24864   else
24865     {
24866       decl = lookup_name_real (name, prefer_type_arg (tag_type),
24867                                /*nonclass=*/0,
24868                                /*block_p=*/true, is_namespace, 0);
24869       parser->qualifying_scope = NULL_TREE;
24870       parser->object_scope = NULL_TREE;
24871     }
24872
24873   /* If the lookup failed, let our caller know.  */
24874   if (!decl || decl == error_mark_node)
24875     return error_mark_node;
24876
24877   /* Pull out the template from an injected-class-name (or multiple).  */
24878   if (is_template)
24879     decl = maybe_get_template_decl_from_type_decl (decl);
24880
24881   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
24882   if (TREE_CODE (decl) == TREE_LIST)
24883     {
24884       if (ambiguous_decls)
24885         *ambiguous_decls = decl;
24886       /* The error message we have to print is too complicated for
24887          cp_parser_error, so we incorporate its actions directly.  */
24888       if (!cp_parser_simulate_error (parser))
24889         {
24890           error_at (name_location, "reference to %qD is ambiguous",
24891                     name);
24892           print_candidates (decl);
24893         }
24894       return error_mark_node;
24895     }
24896
24897   gcc_assert (DECL_P (decl)
24898               || TREE_CODE (decl) == OVERLOAD
24899               || TREE_CODE (decl) == SCOPE_REF
24900               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24901               || BASELINK_P (decl));
24902
24903   /* If we have resolved the name of a member declaration, check to
24904      see if the declaration is accessible.  When the name resolves to
24905      set of overloaded functions, accessibility is checked when
24906      overload resolution is done.
24907
24908      During an explicit instantiation, access is not checked at all,
24909      as per [temp.explicit].  */
24910   if (DECL_P (decl))
24911     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24912
24913   maybe_record_typedef_use (decl);
24914
24915   return cp_expr (decl, name_location);
24916 }
24917
24918 /* Like cp_parser_lookup_name, but for use in the typical case where
24919    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24920    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
24921
24922 static tree
24923 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24924 {
24925   return cp_parser_lookup_name (parser, name,
24926                                 none_type,
24927                                 /*is_template=*/false,
24928                                 /*is_namespace=*/false,
24929                                 /*check_dependency=*/true,
24930                                 /*ambiguous_decls=*/NULL,
24931                                 location);
24932 }
24933
24934 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24935    the current context, return the TYPE_DECL.  If TAG_NAME_P is
24936    true, the DECL indicates the class being defined in a class-head,
24937    or declared in an elaborated-type-specifier.
24938
24939    Otherwise, return DECL.  */
24940
24941 static tree
24942 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24943 {
24944   /* If the TEMPLATE_DECL is being declared as part of a class-head,
24945      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24946
24947        struct A {
24948          template <typename T> struct B;
24949        };
24950
24951        template <typename T> struct A::B {};
24952
24953      Similarly, in an elaborated-type-specifier:
24954
24955        namespace N { struct X{}; }
24956
24957        struct A {
24958          template <typename T> friend struct N::X;
24959        };
24960
24961      However, if the DECL refers to a class type, and we are in
24962      the scope of the class, then the name lookup automatically
24963      finds the TYPE_DECL created by build_self_reference rather
24964      than a TEMPLATE_DECL.  For example, in:
24965
24966        template <class T> struct S {
24967          S s;
24968        };
24969
24970      there is no need to handle such case.  */
24971
24972   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24973     return DECL_TEMPLATE_RESULT (decl);
24974
24975   return decl;
24976 }
24977
24978 /* If too many, or too few, template-parameter lists apply to the
24979    declarator, issue an error message.  Returns TRUE if all went well,
24980    and FALSE otherwise.  */
24981
24982 static bool
24983 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24984                                                 cp_declarator *declarator,
24985                                                 location_t declarator_location)
24986 {
24987   switch (declarator->kind)
24988     {
24989     case cdk_id:
24990       {
24991         unsigned num_templates = 0;
24992         tree scope = declarator->u.id.qualifying_scope;
24993
24994         if (scope)
24995           num_templates = num_template_headers_for_class (scope);
24996         else if (TREE_CODE (declarator->u.id.unqualified_name)
24997                  == TEMPLATE_ID_EXPR)
24998           /* If the DECLARATOR has the form `X<y>' then it uses one
24999              additional level of template parameters.  */
25000           ++num_templates;
25001
25002         return cp_parser_check_template_parameters 
25003           (parser, num_templates, declarator_location, declarator);
25004       }
25005
25006     case cdk_function:
25007     case cdk_array:
25008     case cdk_pointer:
25009     case cdk_reference:
25010     case cdk_ptrmem:
25011       return (cp_parser_check_declarator_template_parameters
25012               (parser, declarator->declarator, declarator_location));
25013
25014     case cdk_error:
25015       return true;
25016
25017     default:
25018       gcc_unreachable ();
25019     }
25020   return false;
25021 }
25022
25023 /* NUM_TEMPLATES were used in the current declaration.  If that is
25024    invalid, return FALSE and issue an error messages.  Otherwise,
25025    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
25026    declarator and we can print more accurate diagnostics.  */
25027
25028 static bool
25029 cp_parser_check_template_parameters (cp_parser* parser,
25030                                      unsigned num_templates,
25031                                      location_t location,
25032                                      cp_declarator *declarator)
25033 {
25034   /* If there are the same number of template classes and parameter
25035      lists, that's OK.  */
25036   if (parser->num_template_parameter_lists == num_templates)
25037     return true;
25038   /* If there are more, but only one more, then we are referring to a
25039      member template.  That's OK too.  */
25040   if (parser->num_template_parameter_lists == num_templates + 1)
25041     return true;
25042   /* If there are more template classes than parameter lists, we have
25043      something like:
25044
25045        template <class T> void S<T>::R<T>::f ();  */
25046   if (parser->num_template_parameter_lists < num_templates)
25047     {
25048       if (declarator && !current_function_decl)
25049         error_at (location, "specializing member %<%T::%E%> "
25050                   "requires %<template<>%> syntax", 
25051                   declarator->u.id.qualifying_scope,
25052                   declarator->u.id.unqualified_name);
25053       else if (declarator)
25054         error_at (location, "invalid declaration of %<%T::%E%>",
25055                   declarator->u.id.qualifying_scope,
25056                   declarator->u.id.unqualified_name);
25057       else 
25058         error_at (location, "too few template-parameter-lists");
25059       return false;
25060     }
25061   /* Otherwise, there are too many template parameter lists.  We have
25062      something like:
25063
25064      template <class T> template <class U> void S::f();  */
25065   error_at (location, "too many template-parameter-lists");
25066   return false;
25067 }
25068
25069 /* Parse an optional `::' token indicating that the following name is
25070    from the global namespace.  If so, PARSER->SCOPE is set to the
25071    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25072    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25073    Returns the new value of PARSER->SCOPE, if the `::' token is
25074    present, and NULL_TREE otherwise.  */
25075
25076 static tree
25077 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25078 {
25079   cp_token *token;
25080
25081   /* Peek at the next token.  */
25082   token = cp_lexer_peek_token (parser->lexer);
25083   /* If we're looking at a `::' token then we're starting from the
25084      global namespace, not our current location.  */
25085   if (token->type == CPP_SCOPE)
25086     {
25087       /* Consume the `::' token.  */
25088       cp_lexer_consume_token (parser->lexer);
25089       /* Set the SCOPE so that we know where to start the lookup.  */
25090       parser->scope = global_namespace;
25091       parser->qualifying_scope = global_namespace;
25092       parser->object_scope = NULL_TREE;
25093
25094       return parser->scope;
25095     }
25096   else if (!current_scope_valid_p)
25097     {
25098       parser->scope = NULL_TREE;
25099       parser->qualifying_scope = NULL_TREE;
25100       parser->object_scope = NULL_TREE;
25101     }
25102
25103   return NULL_TREE;
25104 }
25105
25106 /* Returns TRUE if the upcoming token sequence is the start of a
25107    constructor declarator.  If FRIEND_P is true, the declarator is
25108    preceded by the `friend' specifier.  */
25109
25110 static bool
25111 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25112 {
25113   bool constructor_p;
25114   bool outside_class_specifier_p;
25115   tree nested_name_specifier;
25116   cp_token *next_token;
25117
25118   /* The common case is that this is not a constructor declarator, so
25119      try to avoid doing lots of work if at all possible.  It's not
25120      valid declare a constructor at function scope.  */
25121   if (parser->in_function_body)
25122     return false;
25123   /* And only certain tokens can begin a constructor declarator.  */
25124   next_token = cp_lexer_peek_token (parser->lexer);
25125   if (next_token->type != CPP_NAME
25126       && next_token->type != CPP_SCOPE
25127       && next_token->type != CPP_NESTED_NAME_SPECIFIER
25128       && next_token->type != CPP_TEMPLATE_ID)
25129     return false;
25130
25131   /* Parse tentatively; we are going to roll back all of the tokens
25132      consumed here.  */
25133   cp_parser_parse_tentatively (parser);
25134   /* Assume that we are looking at a constructor declarator.  */
25135   constructor_p = true;
25136
25137   /* Look for the optional `::' operator.  */
25138   cp_parser_global_scope_opt (parser,
25139                               /*current_scope_valid_p=*/false);
25140   /* Look for the nested-name-specifier.  */
25141   nested_name_specifier
25142     = (cp_parser_nested_name_specifier_opt (parser,
25143                                             /*typename_keyword_p=*/false,
25144                                             /*check_dependency_p=*/false,
25145                                             /*type_p=*/false,
25146                                             /*is_declaration=*/false));
25147
25148   outside_class_specifier_p = (!at_class_scope_p ()
25149                                || !TYPE_BEING_DEFINED (current_class_type)
25150                                || friend_p);
25151
25152   /* Outside of a class-specifier, there must be a
25153      nested-name-specifier.  */
25154   if (!nested_name_specifier && outside_class_specifier_p)
25155     constructor_p = false;
25156   else if (nested_name_specifier == error_mark_node)
25157     constructor_p = false;
25158
25159   /* If we have a class scope, this is easy; DR 147 says that S::S always
25160      names the constructor, and no other qualified name could.  */
25161   if (constructor_p && nested_name_specifier
25162       && CLASS_TYPE_P (nested_name_specifier))
25163     {
25164       tree id = cp_parser_unqualified_id (parser,
25165                                           /*template_keyword_p=*/false,
25166                                           /*check_dependency_p=*/false,
25167                                           /*declarator_p=*/true,
25168                                           /*optional_p=*/false);
25169       if (is_overloaded_fn (id))
25170         id = DECL_NAME (get_first_fn (id));
25171       if (!constructor_name_p (id, nested_name_specifier))
25172         constructor_p = false;
25173     }
25174   /* If we still think that this might be a constructor-declarator,
25175      look for a class-name.  */
25176   else if (constructor_p)
25177     {
25178       /* If we have:
25179
25180            template <typename T> struct S {
25181              S();
25182            };
25183
25184          we must recognize that the nested `S' names a class.  */
25185       tree type_decl;
25186       type_decl = cp_parser_class_name (parser,
25187                                         /*typename_keyword_p=*/false,
25188                                         /*template_keyword_p=*/false,
25189                                         none_type,
25190                                         /*check_dependency_p=*/false,
25191                                         /*class_head_p=*/false,
25192                                         /*is_declaration=*/false);
25193       /* If there was no class-name, then this is not a constructor.
25194          Otherwise, if we are in a class-specifier and we aren't
25195          handling a friend declaration, check that its type matches
25196          current_class_type (c++/38313).  Note: error_mark_node
25197          is left alone for error recovery purposes.  */
25198       constructor_p = (!cp_parser_error_occurred (parser)
25199                        && (outside_class_specifier_p
25200                            || type_decl == error_mark_node
25201                            || same_type_p (current_class_type,
25202                                            TREE_TYPE (type_decl))));
25203
25204       /* If we're still considering a constructor, we have to see a `(',
25205          to begin the parameter-declaration-clause, followed by either a
25206          `)', an `...', or a decl-specifier.  We need to check for a
25207          type-specifier to avoid being fooled into thinking that:
25208
25209            S (f) (int);
25210
25211          is a constructor.  (It is actually a function named `f' that
25212          takes one parameter (of type `int') and returns a value of type
25213          `S'.  */
25214       if (constructor_p
25215           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25216         constructor_p = false;
25217
25218       if (constructor_p
25219           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25220           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25221           /* A parameter declaration begins with a decl-specifier,
25222              which is either the "attribute" keyword, a storage class
25223              specifier, or (usually) a type-specifier.  */
25224           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25225         {
25226           tree type;
25227           tree pushed_scope = NULL_TREE;
25228           unsigned saved_num_template_parameter_lists;
25229
25230           /* Names appearing in the type-specifier should be looked up
25231              in the scope of the class.  */
25232           if (current_class_type)
25233             type = NULL_TREE;
25234           else
25235             {
25236               type = TREE_TYPE (type_decl);
25237               if (TREE_CODE (type) == TYPENAME_TYPE)
25238                 {
25239                   type = resolve_typename_type (type,
25240                                                 /*only_current_p=*/false);
25241                   if (TREE_CODE (type) == TYPENAME_TYPE)
25242                     {
25243                       cp_parser_abort_tentative_parse (parser);
25244                       return false;
25245                     }
25246                 }
25247               pushed_scope = push_scope (type);
25248             }
25249
25250           /* Inside the constructor parameter list, surrounding
25251              template-parameter-lists do not apply.  */
25252           saved_num_template_parameter_lists
25253             = parser->num_template_parameter_lists;
25254           parser->num_template_parameter_lists = 0;
25255
25256           /* Look for the type-specifier.  */
25257           cp_parser_type_specifier (parser,
25258                                     CP_PARSER_FLAGS_NONE,
25259                                     /*decl_specs=*/NULL,
25260                                     /*is_declarator=*/true,
25261                                     /*declares_class_or_enum=*/NULL,
25262                                     /*is_cv_qualifier=*/NULL);
25263
25264           parser->num_template_parameter_lists
25265             = saved_num_template_parameter_lists;
25266
25267           /* Leave the scope of the class.  */
25268           if (pushed_scope)
25269             pop_scope (pushed_scope);
25270
25271           constructor_p = !cp_parser_error_occurred (parser);
25272         }
25273     }
25274
25275   /* We did not really want to consume any tokens.  */
25276   cp_parser_abort_tentative_parse (parser);
25277
25278   return constructor_p;
25279 }
25280
25281 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25282    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
25283    they must be performed once we are in the scope of the function.
25284
25285    Returns the function defined.  */
25286
25287 static tree
25288 cp_parser_function_definition_from_specifiers_and_declarator
25289   (cp_parser* parser,
25290    cp_decl_specifier_seq *decl_specifiers,
25291    tree attributes,
25292    const cp_declarator *declarator)
25293 {
25294   tree fn;
25295   bool success_p;
25296
25297   /* Begin the function-definition.  */
25298   success_p = start_function (decl_specifiers, declarator, attributes);
25299
25300   /* The things we're about to see are not directly qualified by any
25301      template headers we've seen thus far.  */
25302   reset_specialization ();
25303
25304   /* If there were names looked up in the decl-specifier-seq that we
25305      did not check, check them now.  We must wait until we are in the
25306      scope of the function to perform the checks, since the function
25307      might be a friend.  */
25308   perform_deferred_access_checks (tf_warning_or_error);
25309
25310   if (success_p)
25311     {
25312       cp_finalize_omp_declare_simd (parser, current_function_decl);
25313       parser->omp_declare_simd = NULL;
25314       cp_finalize_oacc_routine (parser, current_function_decl, true);
25315       parser->oacc_routine = NULL;
25316     }
25317
25318   if (!success_p)
25319     {
25320       /* Skip the entire function.  */
25321       cp_parser_skip_to_end_of_block_or_statement (parser);
25322       fn = error_mark_node;
25323     }
25324   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25325     {
25326       /* Seen already, skip it.  An error message has already been output.  */
25327       cp_parser_skip_to_end_of_block_or_statement (parser);
25328       fn = current_function_decl;
25329       current_function_decl = NULL_TREE;
25330       /* If this is a function from a class, pop the nested class.  */
25331       if (current_class_name)
25332         pop_nested_class ();
25333     }
25334   else
25335     {
25336       timevar_id_t tv;
25337       if (DECL_DECLARED_INLINE_P (current_function_decl))
25338         tv = TV_PARSE_INLINE;
25339       else
25340         tv = TV_PARSE_FUNC;
25341       timevar_push (tv);
25342       fn = cp_parser_function_definition_after_declarator (parser,
25343                                                          /*inline_p=*/false);
25344       timevar_pop (tv);
25345     }
25346
25347   return fn;
25348 }
25349
25350 /* Parse the part of a function-definition that follows the
25351    declarator.  INLINE_P is TRUE iff this function is an inline
25352    function defined within a class-specifier.
25353
25354    Returns the function defined.  */
25355
25356 static tree
25357 cp_parser_function_definition_after_declarator (cp_parser* parser,
25358                                                 bool inline_p)
25359 {
25360   tree fn;
25361   bool ctor_initializer_p = false;
25362   bool saved_in_unbraced_linkage_specification_p;
25363   bool saved_in_function_body;
25364   unsigned saved_num_template_parameter_lists;
25365   cp_token *token;
25366   bool fully_implicit_function_template_p
25367     = parser->fully_implicit_function_template_p;
25368   parser->fully_implicit_function_template_p = false;
25369   tree implicit_template_parms
25370     = parser->implicit_template_parms;
25371   parser->implicit_template_parms = 0;
25372   cp_binding_level* implicit_template_scope
25373     = parser->implicit_template_scope;
25374   parser->implicit_template_scope = 0;
25375
25376   saved_in_function_body = parser->in_function_body;
25377   parser->in_function_body = true;
25378   /* If the next token is `return', then the code may be trying to
25379      make use of the "named return value" extension that G++ used to
25380      support.  */
25381   token = cp_lexer_peek_token (parser->lexer);
25382   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25383     {
25384       /* Consume the `return' keyword.  */
25385       cp_lexer_consume_token (parser->lexer);
25386       /* Look for the identifier that indicates what value is to be
25387          returned.  */
25388       cp_parser_identifier (parser);
25389       /* Issue an error message.  */
25390       error_at (token->location,
25391                 "named return values are no longer supported");
25392       /* Skip tokens until we reach the start of the function body.  */
25393       while (true)
25394         {
25395           cp_token *token = cp_lexer_peek_token (parser->lexer);
25396           if (token->type == CPP_OPEN_BRACE
25397               || token->type == CPP_EOF
25398               || token->type == CPP_PRAGMA_EOL)
25399             break;
25400           cp_lexer_consume_token (parser->lexer);
25401         }
25402     }
25403   /* The `extern' in `extern "C" void f () { ... }' does not apply to
25404      anything declared inside `f'.  */
25405   saved_in_unbraced_linkage_specification_p
25406     = parser->in_unbraced_linkage_specification_p;
25407   parser->in_unbraced_linkage_specification_p = false;
25408   /* Inside the function, surrounding template-parameter-lists do not
25409      apply.  */
25410   saved_num_template_parameter_lists
25411     = parser->num_template_parameter_lists;
25412   parser->num_template_parameter_lists = 0;
25413
25414   start_lambda_scope (current_function_decl);
25415
25416   /* If the next token is `try', `__transaction_atomic', or
25417      `__transaction_relaxed`, then we are looking at either function-try-block
25418      or function-transaction-block.  Note that all of these include the
25419      function-body.  */
25420   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25421     ctor_initializer_p = cp_parser_function_transaction (parser,
25422         RID_TRANSACTION_ATOMIC);
25423   else if (cp_lexer_next_token_is_keyword (parser->lexer,
25424       RID_TRANSACTION_RELAXED))
25425     ctor_initializer_p = cp_parser_function_transaction (parser,
25426         RID_TRANSACTION_RELAXED);
25427   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25428     ctor_initializer_p = cp_parser_function_try_block (parser);
25429   else
25430     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25431       (parser, /*in_function_try_block=*/false);
25432
25433   finish_lambda_scope ();
25434
25435   /* Finish the function.  */
25436   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25437                         (inline_p ? 2 : 0));
25438   /* Generate code for it, if necessary.  */
25439   expand_or_defer_fn (fn);
25440   /* Restore the saved values.  */
25441   parser->in_unbraced_linkage_specification_p
25442     = saved_in_unbraced_linkage_specification_p;
25443   parser->num_template_parameter_lists
25444     = saved_num_template_parameter_lists;
25445   parser->in_function_body = saved_in_function_body;
25446
25447   parser->fully_implicit_function_template_p
25448     = fully_implicit_function_template_p;
25449   parser->implicit_template_parms
25450     = implicit_template_parms;
25451   parser->implicit_template_scope
25452     = implicit_template_scope;
25453
25454   if (parser->fully_implicit_function_template_p)
25455     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25456
25457   return fn;
25458 }
25459
25460 /* Parse a template-declaration body (following argument list).  */
25461
25462 static void
25463 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25464                                                  tree parameter_list,
25465                                                  bool member_p)
25466 {
25467   tree decl = NULL_TREE;
25468   bool friend_p = false;
25469
25470   /* We just processed one more parameter list.  */
25471   ++parser->num_template_parameter_lists;
25472
25473   /* Get the deferred access checks from the parameter list.  These
25474      will be checked once we know what is being declared, as for a
25475      member template the checks must be performed in the scope of the
25476      class containing the member.  */
25477   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25478
25479   /* Tentatively parse for a new template parameter list, which can either be
25480      the template keyword or a template introduction.  */
25481   if (cp_parser_template_declaration_after_export (parser, member_p))
25482     /* OK */;
25483   else if (cxx_dialect >= cxx11
25484            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25485     decl = cp_parser_alias_declaration (parser);
25486   else
25487     {
25488       /* There are no access checks when parsing a template, as we do not
25489          know if a specialization will be a friend.  */
25490       push_deferring_access_checks (dk_no_check);
25491       cp_token *token = cp_lexer_peek_token (parser->lexer);
25492       decl = cp_parser_single_declaration (parser,
25493                                            checks,
25494                                            member_p,
25495                                            /*explicit_specialization_p=*/false,
25496                                            &friend_p);
25497       pop_deferring_access_checks ();
25498
25499       /* If this is a member template declaration, let the front
25500          end know.  */
25501       if (member_p && !friend_p && decl)
25502         {
25503           if (TREE_CODE (decl) == TYPE_DECL)
25504             cp_parser_check_access_in_redeclaration (decl, token->location);
25505
25506           decl = finish_member_template_decl (decl);
25507         }
25508       else if (friend_p && decl
25509                && DECL_DECLARES_TYPE_P (decl))
25510         make_friend_class (current_class_type, TREE_TYPE (decl),
25511                            /*complain=*/true);
25512     }
25513   /* We are done with the current parameter list.  */
25514   --parser->num_template_parameter_lists;
25515
25516   pop_deferring_access_checks ();
25517
25518   /* Finish up.  */
25519   finish_template_decl (parameter_list);
25520
25521   /* Check the template arguments for a literal operator template.  */
25522   if (decl
25523       && DECL_DECLARES_FUNCTION_P (decl)
25524       && UDLIT_OPER_P (DECL_NAME (decl)))
25525     {
25526       bool ok = true;
25527       if (parameter_list == NULL_TREE)
25528         ok = false;
25529       else
25530         {
25531           int num_parms = TREE_VEC_LENGTH (parameter_list);
25532           if (num_parms == 1)
25533             {
25534               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25535               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25536               if (TREE_TYPE (parm) != char_type_node
25537                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25538                 ok = false;
25539             }
25540           else if (num_parms == 2 && cxx_dialect >= cxx14)
25541             {
25542               tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25543               tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25544               tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25545               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25546               if (parm == error_mark_node
25547                   || TREE_TYPE (parm) != TREE_TYPE (type)
25548                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25549                 ok = false;
25550             }
25551           else
25552             ok = false;
25553         }
25554       if (!ok)
25555         {
25556           if (cxx_dialect >= cxx14)
25557             error ("literal operator template %qD has invalid parameter list."
25558                    "  Expected non-type template argument pack <char...>"
25559                    " or <typename CharT, CharT...>",
25560                    decl);
25561           else
25562             error ("literal operator template %qD has invalid parameter list."
25563                    "  Expected non-type template argument pack <char...>",
25564                    decl);
25565         }
25566     }
25567
25568   /* Register member declarations.  */
25569   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25570     finish_member_declaration (decl);
25571   /* If DECL is a function template, we must return to parse it later.
25572      (Even though there is no definition, there might be default
25573      arguments that need handling.)  */
25574   if (member_p && decl
25575       && DECL_DECLARES_FUNCTION_P (decl))
25576     vec_safe_push (unparsed_funs_with_definitions, decl);
25577 }
25578
25579 /* Parse a template introduction header for a template-declaration.  Returns
25580    false if tentative parse fails.  */
25581
25582 static bool
25583 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25584 {
25585   cp_parser_parse_tentatively (parser);
25586
25587   tree saved_scope = parser->scope;
25588   tree saved_object_scope = parser->object_scope;
25589   tree saved_qualifying_scope = parser->qualifying_scope;
25590
25591   /* Look for the optional `::' operator.  */
25592   cp_parser_global_scope_opt (parser,
25593                               /*current_scope_valid_p=*/false);
25594   /* Look for the nested-name-specifier.  */
25595   cp_parser_nested_name_specifier_opt (parser,
25596                                        /*typename_keyword_p=*/false,
25597                                        /*check_dependency_p=*/true,
25598                                        /*type_p=*/false,
25599                                        /*is_declaration=*/false);
25600
25601   cp_token *token = cp_lexer_peek_token (parser->lexer);
25602   tree concept_name = cp_parser_identifier (parser);
25603
25604   /* Look up the concept for which we will be matching
25605      template parameters.  */
25606   tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25607                                                  token->location);
25608   parser->scope = saved_scope;
25609   parser->object_scope = saved_object_scope;
25610   parser->qualifying_scope = saved_qualifying_scope;
25611
25612   if (concept_name == error_mark_node)
25613     cp_parser_simulate_error (parser);
25614
25615   /* Look for opening brace for introduction.  */
25616   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25617
25618   if (!cp_parser_parse_definitely (parser))
25619     return false;
25620
25621   push_deferring_access_checks (dk_deferred);
25622
25623   /* Build vector of placeholder parameters and grab
25624      matching identifiers.  */
25625   tree introduction_list = cp_parser_introduction_list (parser);
25626
25627   /* The introduction-list shall not be empty.  */
25628   int nargs = TREE_VEC_LENGTH (introduction_list);
25629   if (nargs == 0)
25630     {
25631       error ("empty introduction-list");
25632       return true;
25633     }
25634
25635   /* Look for closing brace for introduction.  */
25636   if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25637     return true;
25638
25639   if (tmpl_decl == error_mark_node)
25640     {
25641       cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25642                                    token->location);
25643       return true;
25644     }
25645
25646   /* Build and associate the constraint.  */
25647   tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25648   if (parms && parms != error_mark_node)
25649     {
25650       cp_parser_template_declaration_after_parameters (parser, parms,
25651                                                        member_p);
25652       return true;
25653     }
25654
25655   error_at (token->location, "no matching concept for template-introduction");
25656   return true;
25657 }
25658
25659 /* Parse a normal template-declaration following the template keyword.  */
25660
25661 static void
25662 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25663 {
25664   tree parameter_list;
25665   bool need_lang_pop;
25666   location_t location = input_location;
25667
25668   /* Look for the `<' token.  */
25669   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25670     return;
25671   if (at_class_scope_p () && current_function_decl)
25672     {
25673       /* 14.5.2.2 [temp.mem]
25674
25675          A local class shall not have member templates.  */
25676       error_at (location,
25677                 "invalid declaration of member template in local class");
25678       cp_parser_skip_to_end_of_block_or_statement (parser);
25679       return;
25680     }
25681   /* [temp]
25682
25683      A template ... shall not have C linkage.  */
25684   if (current_lang_name == lang_name_c)
25685     {
25686       error_at (location, "template with C linkage");
25687       /* Give it C++ linkage to avoid confusing other parts of the
25688          front end.  */
25689       push_lang_context (lang_name_cplusplus);
25690       need_lang_pop = true;
25691     }
25692   else
25693     need_lang_pop = false;
25694
25695   /* We cannot perform access checks on the template parameter
25696      declarations until we know what is being declared, just as we
25697      cannot check the decl-specifier list.  */
25698   push_deferring_access_checks (dk_deferred);
25699
25700   /* If the next token is `>', then we have an invalid
25701      specialization.  Rather than complain about an invalid template
25702      parameter, issue an error message here.  */
25703   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25704     {
25705       cp_parser_error (parser, "invalid explicit specialization");
25706       begin_specialization ();
25707       parameter_list = NULL_TREE;
25708     }
25709   else
25710     {
25711       /* Parse the template parameters.  */
25712       parameter_list = cp_parser_template_parameter_list (parser);
25713     }
25714
25715   /* Look for the `>'.  */
25716   cp_parser_skip_to_end_of_template_parameter_list (parser);
25717
25718   /* Manage template requirements */
25719   if (flag_concepts)
25720   {
25721     tree reqs = get_shorthand_constraints (current_template_parms);
25722     if (tree r = cp_parser_requires_clause_opt (parser))
25723       reqs = conjoin_constraints (reqs, normalize_expression (r));
25724     TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25725   }
25726
25727   cp_parser_template_declaration_after_parameters (parser, parameter_list,
25728                                                    member_p);
25729
25730   /* For the erroneous case of a template with C linkage, we pushed an
25731      implicit C++ linkage scope; exit that scope now.  */
25732   if (need_lang_pop)
25733     pop_lang_context ();
25734 }
25735
25736 /* Parse a template-declaration, assuming that the `export' (and
25737    `extern') keywords, if present, has already been scanned.  MEMBER_P
25738    is as for cp_parser_template_declaration.  */
25739
25740 static bool
25741 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25742 {
25743   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25744     {
25745       cp_lexer_consume_token (parser->lexer);
25746       cp_parser_explicit_template_declaration (parser, member_p);
25747       return true;
25748     }
25749   else if (flag_concepts)
25750     return cp_parser_template_introduction (parser, member_p);
25751
25752   return false;
25753 }
25754
25755 /* Perform the deferred access checks from a template-parameter-list.
25756    CHECKS is a TREE_LIST of access checks, as returned by
25757    get_deferred_access_checks.  */
25758
25759 static void
25760 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25761 {
25762   ++processing_template_parmlist;
25763   perform_access_checks (checks, tf_warning_or_error);
25764   --processing_template_parmlist;
25765 }
25766
25767 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25768    `function-definition' sequence that follows a template header.
25769    If MEMBER_P is true, this declaration appears in a class scope.
25770
25771    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
25772    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
25773
25774 static tree
25775 cp_parser_single_declaration (cp_parser* parser,
25776                               vec<deferred_access_check, va_gc> *checks,
25777                               bool member_p,
25778                               bool explicit_specialization_p,
25779                               bool* friend_p)
25780 {
25781   int declares_class_or_enum;
25782   tree decl = NULL_TREE;
25783   cp_decl_specifier_seq decl_specifiers;
25784   bool function_definition_p = false;
25785   cp_token *decl_spec_token_start;
25786
25787   /* This function is only used when processing a template
25788      declaration.  */
25789   gcc_assert (innermost_scope_kind () == sk_template_parms
25790               || innermost_scope_kind () == sk_template_spec);
25791
25792   /* Defer access checks until we know what is being declared.  */
25793   push_deferring_access_checks (dk_deferred);
25794
25795   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25796      alternative.  */
25797   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25798   cp_parser_decl_specifier_seq (parser,
25799                                 CP_PARSER_FLAGS_OPTIONAL,
25800                                 &decl_specifiers,
25801                                 &declares_class_or_enum);
25802   if (friend_p)
25803     *friend_p = cp_parser_friend_p (&decl_specifiers);
25804
25805   /* There are no template typedefs.  */
25806   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25807     {
25808       error_at (decl_spec_token_start->location,
25809                 "template declaration of %<typedef%>");
25810       decl = error_mark_node;
25811     }
25812
25813   /* Gather up the access checks that occurred the
25814      decl-specifier-seq.  */
25815   stop_deferring_access_checks ();
25816
25817   /* Check for the declaration of a template class.  */
25818   if (declares_class_or_enum)
25819     {
25820       if (cp_parser_declares_only_class_p (parser)
25821           || (declares_class_or_enum & 2))
25822         {
25823           // If this is a declaration, but not a definition, associate
25824           // any constraints with the type declaration. Constraints
25825           // are associated with definitions in cp_parser_class_specifier.
25826           if (declares_class_or_enum == 1)
25827             associate_classtype_constraints (decl_specifiers.type);
25828
25829           decl = shadow_tag (&decl_specifiers);
25830
25831           /* In this case:
25832
25833                struct C {
25834                  friend template <typename T> struct A<T>::B;
25835                };
25836
25837              A<T>::B will be represented by a TYPENAME_TYPE, and
25838              therefore not recognized by shadow_tag.  */
25839           if (friend_p && *friend_p
25840               && !decl
25841               && decl_specifiers.type
25842               && TYPE_P (decl_specifiers.type))
25843             decl = decl_specifiers.type;
25844
25845           if (decl && decl != error_mark_node)
25846             decl = TYPE_NAME (decl);
25847           else
25848             decl = error_mark_node;
25849
25850           /* Perform access checks for template parameters.  */
25851           cp_parser_perform_template_parameter_access_checks (checks);
25852
25853           /* Give a helpful diagnostic for
25854                template <class T> struct A { } a;
25855              if we aren't already recovering from an error.  */
25856           if (!cp_parser_declares_only_class_p (parser)
25857               && !seen_error ())
25858             {
25859               error_at (cp_lexer_peek_token (parser->lexer)->location,
25860                         "a class template declaration must not declare "
25861                         "anything else");
25862               cp_parser_skip_to_end_of_block_or_statement (parser);
25863               goto out;
25864             }
25865         }
25866     }
25867
25868   /* Complain about missing 'typename' or other invalid type names.  */
25869   if (!decl_specifiers.any_type_specifiers_p
25870       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25871     {
25872       /* cp_parser_parse_and_diagnose_invalid_type_name calls
25873          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25874          the rest of this declaration.  */
25875       decl = error_mark_node;
25876       goto out;
25877     }
25878
25879   /* If it's not a template class, try for a template function.  If
25880      the next token is a `;', then this declaration does not declare
25881      anything.  But, if there were errors in the decl-specifiers, then
25882      the error might well have come from an attempted class-specifier.
25883      In that case, there's no need to warn about a missing declarator.  */
25884   if (!decl
25885       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25886           || decl_specifiers.type != error_mark_node))
25887     {
25888       decl = cp_parser_init_declarator (parser,
25889                                         &decl_specifiers,
25890                                         checks,
25891                                         /*function_definition_allowed_p=*/true,
25892                                         member_p,
25893                                         declares_class_or_enum,
25894                                         &function_definition_p,
25895                                         NULL, NULL, NULL);
25896
25897     /* 7.1.1-1 [dcl.stc]
25898
25899        A storage-class-specifier shall not be specified in an explicit
25900        specialization...  */
25901     if (decl
25902         && explicit_specialization_p
25903         && decl_specifiers.storage_class != sc_none)
25904       {
25905         error_at (decl_spec_token_start->location,
25906                   "explicit template specialization cannot have a storage class");
25907         decl = error_mark_node;
25908       }
25909
25910     if (decl && VAR_P (decl))
25911       check_template_variable (decl);
25912     }
25913
25914   /* Look for a trailing `;' after the declaration.  */
25915   if (!function_definition_p
25916       && (decl == error_mark_node
25917           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25918     cp_parser_skip_to_end_of_block_or_statement (parser);
25919
25920  out:
25921   pop_deferring_access_checks ();
25922
25923   /* Clear any current qualification; whatever comes next is the start
25924      of something new.  */
25925   parser->scope = NULL_TREE;
25926   parser->qualifying_scope = NULL_TREE;
25927   parser->object_scope = NULL_TREE;
25928
25929   return decl;
25930 }
25931
25932 /* Parse a cast-expression that is not the operand of a unary "&".  */
25933
25934 static cp_expr
25935 cp_parser_simple_cast_expression (cp_parser *parser)
25936 {
25937   return cp_parser_cast_expression (parser, /*address_p=*/false,
25938                                     /*cast_p=*/false, /*decltype*/false, NULL);
25939 }
25940
25941 /* Parse a functional cast to TYPE.  Returns an expression
25942    representing the cast.  */
25943
25944 static cp_expr
25945 cp_parser_functional_cast (cp_parser* parser, tree type)
25946 {
25947   vec<tree, va_gc> *vec;
25948   tree expression_list;
25949   cp_expr cast;
25950   bool nonconst_p;
25951
25952   location_t start_loc = input_location;
25953
25954   if (!type)
25955     type = error_mark_node;
25956
25957   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25958     {
25959       cp_lexer_set_source_position (parser->lexer);
25960       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25961       expression_list = cp_parser_braced_list (parser, &nonconst_p);
25962       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25963       if (TREE_CODE (type) == TYPE_DECL)
25964         type = TREE_TYPE (type);
25965
25966       cast = finish_compound_literal (type, expression_list,
25967                                       tf_warning_or_error);
25968       /* Create a location of the form:
25969             type_name{i, f}
25970             ^~~~~~~~~~~~~~~
25971          with caret == start at the start of the type name,
25972          finishing at the closing brace.  */
25973       location_t finish_loc
25974         = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25975       location_t combined_loc = make_location (start_loc, start_loc,
25976                                                finish_loc);
25977       cast.set_location (combined_loc);
25978       return cast;
25979    }
25980
25981
25982   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25983                                                  /*cast_p=*/true,
25984                                                  /*allow_expansion_p=*/true,
25985                                                  /*non_constant_p=*/NULL);
25986   if (vec == NULL)
25987     expression_list = error_mark_node;
25988   else
25989     {
25990       expression_list = build_tree_list_vec (vec);
25991       release_tree_vector (vec);
25992     }
25993
25994   cast = build_functional_cast (type, expression_list,
25995                                 tf_warning_or_error);
25996   /* [expr.const]/1: In an integral constant expression "only type
25997      conversions to integral or enumeration type can be used".  */
25998   if (TREE_CODE (type) == TYPE_DECL)
25999     type = TREE_TYPE (type);
26000   if (cast != error_mark_node
26001       && !cast_valid_in_integral_constant_expression_p (type)
26002       && cp_parser_non_integral_constant_expression (parser,
26003                                                      NIC_CONSTRUCTOR))
26004     return error_mark_node;
26005
26006   /* Create a location of the form:
26007        float(i)
26008        ^~~~~~~~
26009      with caret == start at the start of the type name,
26010      finishing at the closing paren.  */
26011   location_t finish_loc
26012     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26013   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26014   cast.set_location (combined_loc);
26015   return cast;
26016 }
26017
26018 /* Save the tokens that make up the body of a member function defined
26019    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
26020    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
26021    specifiers applied to the declaration.  Returns the FUNCTION_DECL
26022    for the member function.  */
26023
26024 static tree
26025 cp_parser_save_member_function_body (cp_parser* parser,
26026                                      cp_decl_specifier_seq *decl_specifiers,
26027                                      cp_declarator *declarator,
26028                                      tree attributes)
26029 {
26030   cp_token *first;
26031   cp_token *last;
26032   tree fn;
26033   bool function_try_block = false;
26034
26035   /* Create the FUNCTION_DECL.  */
26036   fn = grokmethod (decl_specifiers, declarator, attributes);
26037   cp_finalize_omp_declare_simd (parser, fn);
26038   cp_finalize_oacc_routine (parser, fn, true);
26039   /* If something went badly wrong, bail out now.  */
26040   if (fn == error_mark_node)
26041     {
26042       /* If there's a function-body, skip it.  */
26043       if (cp_parser_token_starts_function_definition_p
26044           (cp_lexer_peek_token (parser->lexer)))
26045         cp_parser_skip_to_end_of_block_or_statement (parser);
26046       return error_mark_node;
26047     }
26048
26049   /* Remember it, if there default args to post process.  */
26050   cp_parser_save_default_args (parser, fn);
26051
26052   /* Save away the tokens that make up the body of the
26053      function.  */
26054   first = parser->lexer->next_token;
26055
26056   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26057     cp_lexer_consume_token (parser->lexer);
26058   else if (cp_lexer_next_token_is_keyword (parser->lexer,
26059                                            RID_TRANSACTION_ATOMIC))
26060     {
26061       cp_lexer_consume_token (parser->lexer);
26062       /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
26063       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26064           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26065           && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26066               || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26067           && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26068           && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26069         {
26070           cp_lexer_consume_token (parser->lexer);
26071           cp_lexer_consume_token (parser->lexer);
26072           cp_lexer_consume_token (parser->lexer);
26073           cp_lexer_consume_token (parser->lexer);
26074           cp_lexer_consume_token (parser->lexer);
26075         }
26076       else
26077         while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26078                && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26079           {
26080             cp_lexer_consume_token (parser->lexer);
26081             if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26082               break;
26083           }
26084     }
26085
26086   /* Handle function try blocks.  */
26087   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26088     {
26089       cp_lexer_consume_token (parser->lexer);
26090       function_try_block = true;
26091     }
26092   /* We can have braced-init-list mem-initializers before the fn body.  */
26093   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26094     {
26095       cp_lexer_consume_token (parser->lexer);
26096       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26097         {
26098           /* cache_group will stop after an un-nested { } pair, too.  */
26099           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26100             break;
26101
26102           /* variadic mem-inits have ... after the ')'.  */
26103           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26104             cp_lexer_consume_token (parser->lexer);
26105         }
26106     }
26107   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26108   /* Handle function try blocks.  */
26109   if (function_try_block)
26110     while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26111       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26112   last = parser->lexer->next_token;
26113
26114   /* Save away the inline definition; we will process it when the
26115      class is complete.  */
26116   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26117   DECL_PENDING_INLINE_P (fn) = 1;
26118
26119   /* We need to know that this was defined in the class, so that
26120      friend templates are handled correctly.  */
26121   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26122
26123   /* Add FN to the queue of functions to be parsed later.  */
26124   vec_safe_push (unparsed_funs_with_definitions, fn);
26125
26126   return fn;
26127 }
26128
26129 /* Save the tokens that make up the in-class initializer for a non-static
26130    data member.  Returns a DEFAULT_ARG.  */
26131
26132 static tree
26133 cp_parser_save_nsdmi (cp_parser* parser)
26134 {
26135   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26136 }
26137
26138 /* Parse a template-argument-list, as well as the trailing ">" (but
26139    not the opening "<").  See cp_parser_template_argument_list for the
26140    return value.  */
26141
26142 static tree
26143 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26144 {
26145   tree arguments;
26146   tree saved_scope;
26147   tree saved_qualifying_scope;
26148   tree saved_object_scope;
26149   bool saved_greater_than_is_operator_p;
26150   int saved_unevaluated_operand;
26151   int saved_inhibit_evaluation_warnings;
26152
26153   /* [temp.names]
26154
26155      When parsing a template-id, the first non-nested `>' is taken as
26156      the end of the template-argument-list rather than a greater-than
26157      operator.  */
26158   saved_greater_than_is_operator_p
26159     = parser->greater_than_is_operator_p;
26160   parser->greater_than_is_operator_p = false;
26161   /* Parsing the argument list may modify SCOPE, so we save it
26162      here.  */
26163   saved_scope = parser->scope;
26164   saved_qualifying_scope = parser->qualifying_scope;
26165   saved_object_scope = parser->object_scope;
26166   /* We need to evaluate the template arguments, even though this
26167      template-id may be nested within a "sizeof".  */
26168   saved_unevaluated_operand = cp_unevaluated_operand;
26169   cp_unevaluated_operand = 0;
26170   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26171   c_inhibit_evaluation_warnings = 0;
26172   /* Parse the template-argument-list itself.  */
26173   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26174       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26175     arguments = NULL_TREE;
26176   else
26177     arguments = cp_parser_template_argument_list (parser);
26178   /* Look for the `>' that ends the template-argument-list. If we find
26179      a '>>' instead, it's probably just a typo.  */
26180   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26181     {
26182       if (cxx_dialect != cxx98)
26183         {
26184           /* In C++0x, a `>>' in a template argument list or cast
26185              expression is considered to be two separate `>'
26186              tokens. So, change the current token to a `>', but don't
26187              consume it: it will be consumed later when the outer
26188              template argument list (or cast expression) is parsed.
26189              Note that this replacement of `>' for `>>' is necessary
26190              even if we are parsing tentatively: in the tentative
26191              case, after calling
26192              cp_parser_enclosed_template_argument_list we will always
26193              throw away all of the template arguments and the first
26194              closing `>', either because the template argument list
26195              was erroneous or because we are replacing those tokens
26196              with a CPP_TEMPLATE_ID token.  The second `>' (which will
26197              not have been thrown away) is needed either to close an
26198              outer template argument list or to complete a new-style
26199              cast.  */
26200           cp_token *token = cp_lexer_peek_token (parser->lexer);
26201           token->type = CPP_GREATER;
26202         }
26203       else if (!saved_greater_than_is_operator_p)
26204         {
26205           /* If we're in a nested template argument list, the '>>' has
26206             to be a typo for '> >'. We emit the error message, but we
26207             continue parsing and we push a '>' as next token, so that
26208             the argument list will be parsed correctly.  Note that the
26209             global source location is still on the token before the
26210             '>>', so we need to say explicitly where we want it.  */
26211           cp_token *token = cp_lexer_peek_token (parser->lexer);
26212           error_at (token->location, "%<>>%> should be %<> >%> "
26213                     "within a nested template argument list");
26214
26215           token->type = CPP_GREATER;
26216         }
26217       else
26218         {
26219           /* If this is not a nested template argument list, the '>>'
26220             is a typo for '>'. Emit an error message and continue.
26221             Same deal about the token location, but here we can get it
26222             right by consuming the '>>' before issuing the diagnostic.  */
26223           cp_token *token = cp_lexer_consume_token (parser->lexer);
26224           error_at (token->location,
26225                     "spurious %<>>%>, use %<>%> to terminate "
26226                     "a template argument list");
26227         }
26228     }
26229   else
26230     cp_parser_skip_to_end_of_template_parameter_list (parser);
26231   /* The `>' token might be a greater-than operator again now.  */
26232   parser->greater_than_is_operator_p
26233     = saved_greater_than_is_operator_p;
26234   /* Restore the SAVED_SCOPE.  */
26235   parser->scope = saved_scope;
26236   parser->qualifying_scope = saved_qualifying_scope;
26237   parser->object_scope = saved_object_scope;
26238   cp_unevaluated_operand = saved_unevaluated_operand;
26239   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26240
26241   return arguments;
26242 }
26243
26244 /* MEMBER_FUNCTION is a member function, or a friend.  If default
26245    arguments, or the body of the function have not yet been parsed,
26246    parse them now.  */
26247
26248 static void
26249 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26250 {
26251   timevar_push (TV_PARSE_INMETH);
26252   /* If this member is a template, get the underlying
26253      FUNCTION_DECL.  */
26254   if (DECL_FUNCTION_TEMPLATE_P (member_function))
26255     member_function = DECL_TEMPLATE_RESULT (member_function);
26256
26257   /* There should not be any class definitions in progress at this
26258      point; the bodies of members are only parsed outside of all class
26259      definitions.  */
26260   gcc_assert (parser->num_classes_being_defined == 0);
26261   /* While we're parsing the member functions we might encounter more
26262      classes.  We want to handle them right away, but we don't want
26263      them getting mixed up with functions that are currently in the
26264      queue.  */
26265   push_unparsed_function_queues (parser);
26266
26267   /* Make sure that any template parameters are in scope.  */
26268   maybe_begin_member_template_processing (member_function);
26269
26270   /* If the body of the function has not yet been parsed, parse it
26271      now.  */
26272   if (DECL_PENDING_INLINE_P (member_function))
26273     {
26274       tree function_scope;
26275       cp_token_cache *tokens;
26276
26277       /* The function is no longer pending; we are processing it.  */
26278       tokens = DECL_PENDING_INLINE_INFO (member_function);
26279       DECL_PENDING_INLINE_INFO (member_function) = NULL;
26280       DECL_PENDING_INLINE_P (member_function) = 0;
26281
26282       /* If this is a local class, enter the scope of the containing
26283          function.  */
26284       function_scope = current_function_decl;
26285       if (function_scope)
26286         push_function_context ();
26287
26288       /* Push the body of the function onto the lexer stack.  */
26289       cp_parser_push_lexer_for_tokens (parser, tokens);
26290
26291       /* Let the front end know that we going to be defining this
26292          function.  */
26293       start_preparsed_function (member_function, NULL_TREE,
26294                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
26295
26296       /* Don't do access checking if it is a templated function.  */
26297       if (processing_template_decl)
26298         push_deferring_access_checks (dk_no_check);
26299
26300       /* #pragma omp declare reduction needs special parsing.  */
26301       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26302         {
26303           parser->lexer->in_pragma = true;
26304           cp_parser_omp_declare_reduction_exprs (member_function, parser);
26305           finish_function (/*inline*/2);
26306           cp_check_omp_declare_reduction (member_function);
26307         }
26308       else
26309         /* Now, parse the body of the function.  */
26310         cp_parser_function_definition_after_declarator (parser,
26311                                                         /*inline_p=*/true);
26312
26313       if (processing_template_decl)
26314         pop_deferring_access_checks ();
26315
26316       /* Leave the scope of the containing function.  */
26317       if (function_scope)
26318         pop_function_context ();
26319       cp_parser_pop_lexer (parser);
26320     }
26321
26322   /* Remove any template parameters from the symbol table.  */
26323   maybe_end_member_template_processing ();
26324
26325   /* Restore the queue.  */
26326   pop_unparsed_function_queues (parser);
26327   timevar_pop (TV_PARSE_INMETH);
26328 }
26329
26330 /* If DECL contains any default args, remember it on the unparsed
26331    functions queue.  */
26332
26333 static void
26334 cp_parser_save_default_args (cp_parser* parser, tree decl)
26335 {
26336   tree probe;
26337
26338   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26339        probe;
26340        probe = TREE_CHAIN (probe))
26341     if (TREE_PURPOSE (probe))
26342       {
26343         cp_default_arg_entry entry = {current_class_type, decl};
26344         vec_safe_push (unparsed_funs_with_default_args, entry);
26345         break;
26346       }
26347 }
26348
26349 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26350    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
26351    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
26352    from the parameter-type-list.  */
26353
26354 static tree
26355 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26356                                       tree default_arg, tree parmtype)
26357 {
26358   cp_token_cache *tokens;
26359   tree parsed_arg;
26360   bool dummy;
26361
26362   if (default_arg == error_mark_node)
26363     return error_mark_node;
26364
26365   /* Push the saved tokens for the default argument onto the parser's
26366      lexer stack.  */
26367   tokens = DEFARG_TOKENS (default_arg);
26368   cp_parser_push_lexer_for_tokens (parser, tokens);
26369
26370   start_lambda_scope (decl);
26371
26372   /* Parse the default argument.  */
26373   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26374   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26375     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26376
26377   finish_lambda_scope ();
26378
26379   if (parsed_arg == error_mark_node)
26380     cp_parser_skip_to_end_of_statement (parser);
26381
26382   if (!processing_template_decl)
26383     {
26384       /* In a non-template class, check conversions now.  In a template,
26385          we'll wait and instantiate these as needed.  */
26386       if (TREE_CODE (decl) == PARM_DECL)
26387         parsed_arg = check_default_argument (parmtype, parsed_arg,
26388                                              tf_warning_or_error);
26389       else
26390         parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26391     }
26392
26393   /* If the token stream has not been completely used up, then
26394      there was extra junk after the end of the default
26395      argument.  */
26396   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26397     {
26398       if (TREE_CODE (decl) == PARM_DECL)
26399         cp_parser_error (parser, "expected %<,%>");
26400       else
26401         cp_parser_error (parser, "expected %<;%>");
26402     }
26403
26404   /* Revert to the main lexer.  */
26405   cp_parser_pop_lexer (parser);
26406
26407   return parsed_arg;
26408 }
26409
26410 /* FIELD is a non-static data member with an initializer which we saved for
26411    later; parse it now.  */
26412
26413 static void
26414 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26415 {
26416   tree def;
26417
26418   maybe_begin_member_template_processing (field);
26419
26420   push_unparsed_function_queues (parser);
26421   def = cp_parser_late_parse_one_default_arg (parser, field,
26422                                               DECL_INITIAL (field),
26423                                               NULL_TREE);
26424   pop_unparsed_function_queues (parser);
26425
26426   maybe_end_member_template_processing ();
26427
26428   DECL_INITIAL (field) = def;
26429 }
26430
26431 /* FN is a FUNCTION_DECL which may contains a parameter with an
26432    unparsed DEFAULT_ARG.  Parse the default args now.  This function
26433    assumes that the current scope is the scope in which the default
26434    argument should be processed.  */
26435
26436 static void
26437 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26438 {
26439   bool saved_local_variables_forbidden_p;
26440   tree parm, parmdecl;
26441
26442   /* While we're parsing the default args, we might (due to the
26443      statement expression extension) encounter more classes.  We want
26444      to handle them right away, but we don't want them getting mixed
26445      up with default args that are currently in the queue.  */
26446   push_unparsed_function_queues (parser);
26447
26448   /* Local variable names (and the `this' keyword) may not appear
26449      in a default argument.  */
26450   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26451   parser->local_variables_forbidden_p = true;
26452
26453   push_defarg_context (fn);
26454
26455   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26456          parmdecl = DECL_ARGUMENTS (fn);
26457        parm && parm != void_list_node;
26458        parm = TREE_CHAIN (parm),
26459          parmdecl = DECL_CHAIN (parmdecl))
26460     {
26461       tree default_arg = TREE_PURPOSE (parm);
26462       tree parsed_arg;
26463       vec<tree, va_gc> *insts;
26464       tree copy;
26465       unsigned ix;
26466
26467       if (!default_arg)
26468         continue;
26469
26470       if (TREE_CODE (default_arg) != DEFAULT_ARG)
26471         /* This can happen for a friend declaration for a function
26472            already declared with default arguments.  */
26473         continue;
26474
26475       parsed_arg
26476         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26477                                                 default_arg,
26478                                                 TREE_VALUE (parm));
26479       if (parsed_arg == error_mark_node)
26480         {
26481           continue;
26482         }
26483
26484       TREE_PURPOSE (parm) = parsed_arg;
26485
26486       /* Update any instantiations we've already created.  */
26487       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26488            vec_safe_iterate (insts, ix, &copy); ix++)
26489         TREE_PURPOSE (copy) = parsed_arg;
26490     }
26491
26492   pop_defarg_context ();
26493
26494   /* Make sure no default arg is missing.  */
26495   check_default_args (fn);
26496
26497   /* Restore the state of local_variables_forbidden_p.  */
26498   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26499
26500   /* Restore the queue.  */
26501   pop_unparsed_function_queues (parser);
26502 }
26503
26504 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26505
26506      sizeof ... ( identifier )
26507
26508    where the 'sizeof' token has already been consumed.  */
26509
26510 static tree
26511 cp_parser_sizeof_pack (cp_parser *parser)
26512 {
26513   /* Consume the `...'.  */
26514   cp_lexer_consume_token (parser->lexer);
26515   maybe_warn_variadic_templates ();
26516
26517   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26518   if (paren)
26519     cp_lexer_consume_token (parser->lexer);
26520   else
26521     permerror (cp_lexer_peek_token (parser->lexer)->location,
26522                "%<sizeof...%> argument must be surrounded by parentheses");
26523
26524   cp_token *token = cp_lexer_peek_token (parser->lexer);
26525   tree name = cp_parser_identifier (parser);
26526   if (name == error_mark_node)
26527     return error_mark_node;
26528   /* The name is not qualified.  */
26529   parser->scope = NULL_TREE;
26530   parser->qualifying_scope = NULL_TREE;
26531   parser->object_scope = NULL_TREE;
26532   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26533   if (expr == error_mark_node)
26534     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26535                                  token->location);
26536   if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26537     expr = TREE_TYPE (expr);
26538   else if (TREE_CODE (expr) == CONST_DECL)
26539     expr = DECL_INITIAL (expr);
26540   expr = make_pack_expansion (expr);
26541   PACK_EXPANSION_SIZEOF_P (expr) = true;
26542
26543   if (paren)
26544     cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26545
26546   return expr;
26547 }
26548
26549 /* Parse the operand of `sizeof' (or a similar operator).  Returns
26550    either a TYPE or an expression, depending on the form of the
26551    input.  The KEYWORD indicates which kind of expression we have
26552    encountered.  */
26553
26554 static tree
26555 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26556 {
26557   tree expr = NULL_TREE;
26558   const char *saved_message;
26559   char *tmp;
26560   bool saved_integral_constant_expression_p;
26561   bool saved_non_integral_constant_expression_p;
26562
26563   /* If it's a `...', then we are computing the length of a parameter
26564      pack.  */
26565   if (keyword == RID_SIZEOF
26566       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26567     return cp_parser_sizeof_pack (parser);
26568
26569   /* Types cannot be defined in a `sizeof' expression.  Save away the
26570      old message.  */
26571   saved_message = parser->type_definition_forbidden_message;
26572   /* And create the new one.  */
26573   tmp = concat ("types may not be defined in %<",
26574                 IDENTIFIER_POINTER (ridpointers[keyword]),
26575                 "%> expressions", NULL);
26576   parser->type_definition_forbidden_message = tmp;
26577
26578   /* The restrictions on constant-expressions do not apply inside
26579      sizeof expressions.  */
26580   saved_integral_constant_expression_p
26581     = parser->integral_constant_expression_p;
26582   saved_non_integral_constant_expression_p
26583     = parser->non_integral_constant_expression_p;
26584   parser->integral_constant_expression_p = false;
26585
26586   /* Do not actually evaluate the expression.  */
26587   ++cp_unevaluated_operand;
26588   ++c_inhibit_evaluation_warnings;
26589   /* If it's a `(', then we might be looking at the type-id
26590      construction.  */
26591   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26592     {
26593       tree type = NULL_TREE;
26594
26595       /* We can't be sure yet whether we're looking at a type-id or an
26596          expression.  */
26597       cp_parser_parse_tentatively (parser);
26598       /* Note: as a GNU Extension, compound literals are considered
26599          postfix-expressions as they are in C99, so they are valid
26600          arguments to sizeof.  See comment in cp_parser_cast_expression
26601          for details.  */
26602       if (cp_parser_compound_literal_p (parser))
26603         cp_parser_simulate_error (parser);
26604       else
26605         {
26606           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26607           parser->in_type_id_in_expr_p = true;
26608           /* Look for the type-id.  */
26609           type = cp_parser_type_id (parser);
26610           /* Look for the closing `)'.  */
26611           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26612           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26613         }
26614
26615       /* If all went well, then we're done.  */
26616       if (cp_parser_parse_definitely (parser))
26617         {
26618           cp_decl_specifier_seq decl_specs;
26619
26620           /* Build a trivial decl-specifier-seq.  */
26621           clear_decl_specs (&decl_specs);
26622           decl_specs.type = type;
26623
26624           /* Call grokdeclarator to figure out what type this is.  */
26625           expr = grokdeclarator (NULL,
26626                                  &decl_specs,
26627                                  TYPENAME,
26628                                  /*initialized=*/0,
26629                                  /*attrlist=*/NULL);
26630         }
26631     }
26632
26633   /* If the type-id production did not work out, then we must be
26634      looking at the unary-expression production.  */
26635   if (!expr)
26636     expr = cp_parser_unary_expression (parser);
26637
26638   /* Go back to evaluating expressions.  */
26639   --cp_unevaluated_operand;
26640   --c_inhibit_evaluation_warnings;
26641
26642   /* Free the message we created.  */
26643   free (tmp);
26644   /* And restore the old one.  */
26645   parser->type_definition_forbidden_message = saved_message;
26646   parser->integral_constant_expression_p
26647     = saved_integral_constant_expression_p;
26648   parser->non_integral_constant_expression_p
26649     = saved_non_integral_constant_expression_p;
26650
26651   return expr;
26652 }
26653
26654 /* If the current declaration has no declarator, return true.  */
26655
26656 static bool
26657 cp_parser_declares_only_class_p (cp_parser *parser)
26658 {
26659   /* If the next token is a `;' or a `,' then there is no
26660      declarator.  */
26661   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26662           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26663 }
26664
26665 /* Update the DECL_SPECS to reflect the storage class indicated by
26666    KEYWORD.  */
26667
26668 static void
26669 cp_parser_set_storage_class (cp_parser *parser,
26670                              cp_decl_specifier_seq *decl_specs,
26671                              enum rid keyword,
26672                              cp_token *token)
26673 {
26674   cp_storage_class storage_class;
26675
26676   if (parser->in_unbraced_linkage_specification_p)
26677     {
26678       error_at (token->location, "invalid use of %qD in linkage specification",
26679                 ridpointers[keyword]);
26680       return;
26681     }
26682   else if (decl_specs->storage_class != sc_none)
26683     {
26684       decl_specs->conflicting_specifiers_p = true;
26685       return;
26686     }
26687
26688   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26689       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26690       && decl_specs->gnu_thread_keyword_p)
26691     {
26692       pedwarn (decl_specs->locations[ds_thread], 0,
26693                 "%<__thread%> before %qD", ridpointers[keyword]);
26694     }
26695
26696   switch (keyword)
26697     {
26698     case RID_AUTO:
26699       storage_class = sc_auto;
26700       break;
26701     case RID_REGISTER:
26702       storage_class = sc_register;
26703       break;
26704     case RID_STATIC:
26705       storage_class = sc_static;
26706       break;
26707     case RID_EXTERN:
26708       storage_class = sc_extern;
26709       break;
26710     case RID_MUTABLE:
26711       storage_class = sc_mutable;
26712       break;
26713     default:
26714       gcc_unreachable ();
26715     }
26716   decl_specs->storage_class = storage_class;
26717   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26718
26719   /* A storage class specifier cannot be applied alongside a typedef 
26720      specifier. If there is a typedef specifier present then set 
26721      conflicting_specifiers_p which will trigger an error later
26722      on in grokdeclarator. */
26723   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26724     decl_specs->conflicting_specifiers_p = true;
26725 }
26726
26727 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
26728    is true, the type is a class or enum definition.  */
26729
26730 static void
26731 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26732                               tree type_spec,
26733                               cp_token *token,
26734                               bool type_definition_p)
26735 {
26736   decl_specs->any_specifiers_p = true;
26737
26738   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26739      (with, for example, in "typedef int wchar_t;") we remember that
26740      this is what happened.  In system headers, we ignore these
26741      declarations so that G++ can work with system headers that are not
26742      C++-safe.  */
26743   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26744       && !type_definition_p
26745       && (type_spec == boolean_type_node
26746           || type_spec == char16_type_node
26747           || type_spec == char32_type_node
26748           || type_spec == wchar_type_node)
26749       && (decl_specs->type
26750           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26751           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26752           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26753           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26754     {
26755       decl_specs->redefined_builtin_type = type_spec;
26756       set_and_check_decl_spec_loc (decl_specs,
26757                                    ds_redefined_builtin_type_spec,
26758                                    token);
26759       if (!decl_specs->type)
26760         {
26761           decl_specs->type = type_spec;
26762           decl_specs->type_definition_p = false;
26763           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26764         }
26765     }
26766   else if (decl_specs->type)
26767     decl_specs->multiple_types_p = true;
26768   else
26769     {
26770       decl_specs->type = type_spec;
26771       decl_specs->type_definition_p = type_definition_p;
26772       decl_specs->redefined_builtin_type = NULL_TREE;
26773       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26774     }
26775 }
26776
26777 /* True iff TOKEN is the GNU keyword __thread.  */
26778
26779 static bool
26780 token_is__thread (cp_token *token)
26781 {
26782   gcc_assert (token->keyword == RID_THREAD);
26783   return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26784 }
26785
26786 /* Set the location for a declarator specifier and check if it is
26787    duplicated.
26788
26789    DECL_SPECS is the sequence of declarator specifiers onto which to
26790    set the location.
26791
26792    DS is the single declarator specifier to set which location  is to
26793    be set onto the existing sequence of declarators.
26794
26795    LOCATION is the location for the declarator specifier to
26796    consider.  */
26797
26798 static void
26799 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26800                              cp_decl_spec ds, cp_token *token)
26801 {
26802   gcc_assert (ds < ds_last);
26803
26804   if (decl_specs == NULL)
26805     return;
26806
26807   source_location location = token->location;
26808
26809   if (decl_specs->locations[ds] == 0)
26810     {
26811       decl_specs->locations[ds] = location;
26812       if (ds == ds_thread)
26813         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26814     }
26815   else
26816     {
26817       if (ds == ds_long)
26818         {
26819           if (decl_specs->locations[ds_long_long] != 0)
26820             error_at (location,
26821                       "%<long long long%> is too long for GCC");
26822           else
26823             {
26824               decl_specs->locations[ds_long_long] = location;
26825               pedwarn_cxx98 (location,
26826                              OPT_Wlong_long, 
26827                              "ISO C++ 1998 does not support %<long long%>");
26828             }
26829         }
26830       else if (ds == ds_thread)
26831         {
26832           bool gnu = token_is__thread (token);
26833           if (gnu != decl_specs->gnu_thread_keyword_p)
26834             error_at (location,
26835                       "both %<__thread%> and %<thread_local%> specified");
26836           else
26837             error_at (location, "duplicate %qD", token->u.value);
26838         }
26839       else
26840         {
26841           static const char *const decl_spec_names[] = {
26842             "signed",
26843             "unsigned",
26844             "short",
26845             "long",
26846             "const",
26847             "volatile",
26848             "restrict",
26849             "inline",
26850             "virtual",
26851             "explicit",
26852             "friend",
26853             "typedef",
26854             "using",
26855             "constexpr",
26856             "__complex"
26857           };
26858           error_at (location,
26859                     "duplicate %qs", decl_spec_names[ds]);
26860         }
26861     }
26862 }
26863
26864 /* Return true iff the declarator specifier DS is present in the
26865    sequence of declarator specifiers DECL_SPECS.  */
26866
26867 bool
26868 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26869                           cp_decl_spec ds)
26870 {
26871   gcc_assert (ds < ds_last);
26872
26873   if (decl_specs == NULL)
26874     return false;
26875
26876   return decl_specs->locations[ds] != 0;
26877 }
26878
26879 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26880    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
26881
26882 static bool
26883 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26884 {
26885   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26886 }
26887
26888 /* Issue an error message indicating that TOKEN_DESC was expected.
26889    If KEYWORD is true, it indicated this function is called by
26890    cp_parser_require_keword and the required token can only be
26891    a indicated keyword. */
26892
26893 static void
26894 cp_parser_required_error (cp_parser *parser,
26895                           required_token token_desc,
26896                           bool keyword)
26897 {
26898   switch (token_desc)
26899     {
26900       case RT_NEW:
26901         cp_parser_error (parser, "expected %<new%>");
26902         return;
26903       case RT_DELETE:
26904         cp_parser_error (parser, "expected %<delete%>");
26905         return;
26906       case RT_RETURN:
26907         cp_parser_error (parser, "expected %<return%>");
26908         return;
26909       case RT_WHILE:
26910         cp_parser_error (parser, "expected %<while%>");
26911         return;
26912       case RT_EXTERN:
26913         cp_parser_error (parser, "expected %<extern%>");
26914         return;
26915       case RT_STATIC_ASSERT:
26916         cp_parser_error (parser, "expected %<static_assert%>");
26917         return;
26918       case RT_DECLTYPE:
26919         cp_parser_error (parser, "expected %<decltype%>");
26920         return;
26921       case RT_OPERATOR:
26922         cp_parser_error (parser, "expected %<operator%>");
26923         return;
26924       case RT_CLASS:
26925         cp_parser_error (parser, "expected %<class%>");
26926         return;
26927       case RT_TEMPLATE:
26928         cp_parser_error (parser, "expected %<template%>");
26929         return;
26930       case RT_NAMESPACE:
26931         cp_parser_error (parser, "expected %<namespace%>");
26932         return;
26933       case RT_USING:
26934         cp_parser_error (parser, "expected %<using%>");
26935         return;
26936       case RT_ASM:
26937         cp_parser_error (parser, "expected %<asm%>");
26938         return;
26939       case RT_TRY:
26940         cp_parser_error (parser, "expected %<try%>");
26941         return;
26942       case RT_CATCH:
26943         cp_parser_error (parser, "expected %<catch%>");
26944         return;
26945       case RT_THROW:
26946         cp_parser_error (parser, "expected %<throw%>");
26947         return;
26948       case RT_LABEL:
26949         cp_parser_error (parser, "expected %<__label__%>");
26950         return;
26951       case RT_AT_TRY:
26952         cp_parser_error (parser, "expected %<@try%>");
26953         return;
26954       case RT_AT_SYNCHRONIZED:
26955         cp_parser_error (parser, "expected %<@synchronized%>");
26956         return;
26957       case RT_AT_THROW:
26958         cp_parser_error (parser, "expected %<@throw%>");
26959         return;
26960       case RT_TRANSACTION_ATOMIC:
26961         cp_parser_error (parser, "expected %<__transaction_atomic%>");
26962         return;
26963       case RT_TRANSACTION_RELAXED:
26964         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26965         return;
26966       default:
26967         break;
26968     }
26969   if (!keyword)
26970     {
26971       switch (token_desc)
26972         {
26973           case RT_SEMICOLON:
26974             cp_parser_error (parser, "expected %<;%>");
26975             return;
26976           case RT_OPEN_PAREN:
26977             cp_parser_error (parser, "expected %<(%>");
26978             return;
26979           case RT_CLOSE_BRACE:
26980             cp_parser_error (parser, "expected %<}%>");
26981             return;
26982           case RT_OPEN_BRACE:
26983             cp_parser_error (parser, "expected %<{%>");
26984             return;
26985           case RT_CLOSE_SQUARE:
26986             cp_parser_error (parser, "expected %<]%>");
26987             return;
26988           case RT_OPEN_SQUARE:
26989             cp_parser_error (parser, "expected %<[%>");
26990             return;
26991           case RT_COMMA:
26992             cp_parser_error (parser, "expected %<,%>");
26993             return;
26994           case RT_SCOPE:
26995             cp_parser_error (parser, "expected %<::%>");
26996             return;
26997           case RT_LESS:
26998             cp_parser_error (parser, "expected %<<%>");
26999             return;
27000           case RT_GREATER:
27001             cp_parser_error (parser, "expected %<>%>");
27002             return;
27003           case RT_EQ:
27004             cp_parser_error (parser, "expected %<=%>");
27005             return;
27006           case RT_ELLIPSIS:
27007             cp_parser_error (parser, "expected %<...%>");
27008             return;
27009           case RT_MULT:
27010             cp_parser_error (parser, "expected %<*%>");
27011             return;
27012           case RT_COMPL:
27013             cp_parser_error (parser, "expected %<~%>");
27014             return;
27015           case RT_COLON:
27016             cp_parser_error (parser, "expected %<:%>");
27017             return;
27018           case RT_COLON_SCOPE:
27019             cp_parser_error (parser, "expected %<:%> or %<::%>");
27020             return;
27021           case RT_CLOSE_PAREN:
27022             cp_parser_error (parser, "expected %<)%>");
27023             return;
27024           case RT_COMMA_CLOSE_PAREN:
27025             cp_parser_error (parser, "expected %<,%> or %<)%>");
27026             return;
27027           case RT_PRAGMA_EOL:
27028             cp_parser_error (parser, "expected end of line");
27029             return;
27030           case RT_NAME:
27031             cp_parser_error (parser, "expected identifier");
27032             return;
27033           case RT_SELECT:
27034             cp_parser_error (parser, "expected selection-statement");
27035             return;
27036           case RT_INTERATION:
27037             cp_parser_error (parser, "expected iteration-statement");
27038             return;
27039           case RT_JUMP:
27040             cp_parser_error (parser, "expected jump-statement");
27041             return;
27042           case RT_CLASS_KEY:
27043             cp_parser_error (parser, "expected class-key");
27044             return;
27045           case RT_CLASS_TYPENAME_TEMPLATE:
27046             cp_parser_error (parser,
27047                  "expected %<class%>, %<typename%>, or %<template%>");
27048             return;
27049           default:
27050             gcc_unreachable ();
27051         }
27052     }
27053   else
27054     gcc_unreachable ();
27055 }
27056
27057
27058
27059 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
27060    issue an error message indicating that TOKEN_DESC was expected.
27061
27062    Returns the token consumed, if the token had the appropriate type.
27063    Otherwise, returns NULL.  */
27064
27065 static cp_token *
27066 cp_parser_require (cp_parser* parser,
27067                    enum cpp_ttype type,
27068                    required_token token_desc)
27069 {
27070   if (cp_lexer_next_token_is (parser->lexer, type))
27071     return cp_lexer_consume_token (parser->lexer);
27072   else
27073     {
27074       /* Output the MESSAGE -- unless we're parsing tentatively.  */
27075       if (!cp_parser_simulate_error (parser))
27076         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27077       return NULL;
27078     }
27079 }
27080
27081 /* An error message is produced if the next token is not '>'.
27082    All further tokens are skipped until the desired token is
27083    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
27084
27085 static void
27086 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27087 {
27088   /* Current level of '< ... >'.  */
27089   unsigned level = 0;
27090   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
27091   unsigned nesting_depth = 0;
27092
27093   /* Are we ready, yet?  If not, issue error message.  */
27094   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27095     return;
27096
27097   /* Skip tokens until the desired token is found.  */
27098   while (true)
27099     {
27100       /* Peek at the next token.  */
27101       switch (cp_lexer_peek_token (parser->lexer)->type)
27102         {
27103         case CPP_LESS:
27104           if (!nesting_depth)
27105             ++level;
27106           break;
27107
27108         case CPP_RSHIFT:
27109           if (cxx_dialect == cxx98)
27110             /* C++0x views the `>>' operator as two `>' tokens, but
27111                C++98 does not. */
27112             break;
27113           else if (!nesting_depth && level-- == 0)
27114             {
27115               /* We've hit a `>>' where the first `>' closes the
27116                  template argument list, and the second `>' is
27117                  spurious.  Just consume the `>>' and stop; we've
27118                  already produced at least one error.  */
27119               cp_lexer_consume_token (parser->lexer);
27120               return;
27121             }
27122           /* Fall through for C++0x, so we handle the second `>' in
27123              the `>>'.  */
27124
27125         case CPP_GREATER:
27126           if (!nesting_depth && level-- == 0)
27127             {
27128               /* We've reached the token we want, consume it and stop.  */
27129               cp_lexer_consume_token (parser->lexer);
27130               return;
27131             }
27132           break;
27133
27134         case CPP_OPEN_PAREN:
27135         case CPP_OPEN_SQUARE:
27136           ++nesting_depth;
27137           break;
27138
27139         case CPP_CLOSE_PAREN:
27140         case CPP_CLOSE_SQUARE:
27141           if (nesting_depth-- == 0)
27142             return;
27143           break;
27144
27145         case CPP_EOF:
27146         case CPP_PRAGMA_EOL:
27147         case CPP_SEMICOLON:
27148         case CPP_OPEN_BRACE:
27149         case CPP_CLOSE_BRACE:
27150           /* The '>' was probably forgotten, don't look further.  */
27151           return;
27152
27153         default:
27154           break;
27155         }
27156
27157       /* Consume this token.  */
27158       cp_lexer_consume_token (parser->lexer);
27159     }
27160 }
27161
27162 /* If the next token is the indicated keyword, consume it.  Otherwise,
27163    issue an error message indicating that TOKEN_DESC was expected.
27164
27165    Returns the token consumed, if the token had the appropriate type.
27166    Otherwise, returns NULL.  */
27167
27168 static cp_token *
27169 cp_parser_require_keyword (cp_parser* parser,
27170                            enum rid keyword,
27171                            required_token token_desc)
27172 {
27173   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27174
27175   if (token && token->keyword != keyword)
27176     {
27177       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
27178       return NULL;
27179     }
27180
27181   return token;
27182 }
27183
27184 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27185    function-definition.  */
27186
27187 static bool
27188 cp_parser_token_starts_function_definition_p (cp_token* token)
27189 {
27190   return (/* An ordinary function-body begins with an `{'.  */
27191           token->type == CPP_OPEN_BRACE
27192           /* A ctor-initializer begins with a `:'.  */
27193           || token->type == CPP_COLON
27194           /* A function-try-block begins with `try'.  */
27195           || token->keyword == RID_TRY
27196           /* A function-transaction-block begins with `__transaction_atomic'
27197              or `__transaction_relaxed'.  */
27198           || token->keyword == RID_TRANSACTION_ATOMIC
27199           || token->keyword == RID_TRANSACTION_RELAXED
27200           /* The named return value extension begins with `return'.  */
27201           || token->keyword == RID_RETURN);
27202 }
27203
27204 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27205    definition.  */
27206
27207 static bool
27208 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27209 {
27210   cp_token *token;
27211
27212   token = cp_lexer_peek_token (parser->lexer);
27213   return (token->type == CPP_OPEN_BRACE
27214           || (token->type == CPP_COLON
27215               && !parser->colon_doesnt_start_class_def_p));
27216 }
27217
27218 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27219    C++0x) ending a template-argument.  */
27220
27221 static bool
27222 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27223 {
27224   cp_token *token;
27225
27226   token = cp_lexer_peek_token (parser->lexer);
27227   return (token->type == CPP_COMMA 
27228           || token->type == CPP_GREATER
27229           || token->type == CPP_ELLIPSIS
27230           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27231 }
27232
27233 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27234    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
27235
27236 static bool
27237 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27238                                                      size_t n)
27239 {
27240   cp_token *token;
27241
27242   token = cp_lexer_peek_nth_token (parser->lexer, n);
27243   if (token->type == CPP_LESS)
27244     return true;
27245   /* Check for the sequence `<::' in the original code. It would be lexed as
27246      `[:', where `[' is a digraph, and there is no whitespace before
27247      `:'.  */
27248   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27249     {
27250       cp_token *token2;
27251       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27252       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27253         return true;
27254     }
27255   return false;
27256 }
27257
27258 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27259    or none_type otherwise.  */
27260
27261 static enum tag_types
27262 cp_parser_token_is_class_key (cp_token* token)
27263 {
27264   switch (token->keyword)
27265     {
27266     case RID_CLASS:
27267       return class_type;
27268     case RID_STRUCT:
27269       return record_type;
27270     case RID_UNION:
27271       return union_type;
27272
27273     default:
27274       return none_type;
27275     }
27276 }
27277
27278 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27279    or none_type otherwise or if the token is null.  */
27280
27281 static enum tag_types
27282 cp_parser_token_is_type_parameter_key (cp_token* token)
27283 {
27284   if (!token)
27285     return none_type;
27286
27287   switch (token->keyword)
27288     {
27289     case RID_CLASS:
27290       return class_type;
27291     case RID_TYPENAME:
27292       return typename_type;
27293
27294     default:
27295       return none_type;
27296     }
27297 }
27298
27299 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
27300
27301 static void
27302 cp_parser_check_class_key (enum tag_types class_key, tree type)
27303 {
27304   if (type == error_mark_node)
27305     return;
27306   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27307     {
27308       if (permerror (input_location, "%qs tag used in naming %q#T",
27309                      class_key == union_type ? "union"
27310                      : class_key == record_type ? "struct" : "class",
27311                      type))
27312         inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27313                 "%q#T was previously declared here", type);
27314     }
27315 }
27316
27317 /* Issue an error message if DECL is redeclared with different
27318    access than its original declaration [class.access.spec/3].
27319    This applies to nested classes and nested class templates.
27320    [class.mem/1].  */
27321
27322 static void
27323 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27324 {
27325   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
27326     return;
27327
27328   if ((TREE_PRIVATE (decl)
27329        != (current_access_specifier == access_private_node))
27330       || (TREE_PROTECTED (decl)
27331           != (current_access_specifier == access_protected_node)))
27332     error_at (location, "%qD redeclared with different access", decl);
27333 }
27334
27335 /* Look for the `template' keyword, as a syntactic disambiguator.
27336    Return TRUE iff it is present, in which case it will be
27337    consumed.  */
27338
27339 static bool
27340 cp_parser_optional_template_keyword (cp_parser *parser)
27341 {
27342   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27343     {
27344       /* In C++98 the `template' keyword can only be used within templates;
27345          outside templates the parser can always figure out what is a
27346          template and what is not.  In C++11,  per the resolution of DR 468,
27347          `template' is allowed in cases where it is not strictly necessary.  */
27348       if (!processing_template_decl
27349           && pedantic && cxx_dialect == cxx98)
27350         {
27351           cp_token *token = cp_lexer_peek_token (parser->lexer);
27352           pedwarn (token->location, OPT_Wpedantic,
27353                    "in C++98 %<template%> (as a disambiguator) is only "
27354                    "allowed within templates");
27355           /* If this part of the token stream is rescanned, the same
27356              error message would be generated.  So, we purge the token
27357              from the stream.  */
27358           cp_lexer_purge_token (parser->lexer);
27359           return false;
27360         }
27361       else
27362         {
27363           /* Consume the `template' keyword.  */
27364           cp_lexer_consume_token (parser->lexer);
27365           return true;
27366         }
27367     }
27368   return false;
27369 }
27370
27371 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
27372    set PARSER->SCOPE, and perform other related actions.  */
27373
27374 static void
27375 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27376 {
27377   struct tree_check *check_value;
27378
27379   /* Get the stored value.  */
27380   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27381   /* Set the scope from the stored value.  */
27382   parser->scope = saved_checks_value (check_value);
27383   parser->qualifying_scope = check_value->qualifying_scope;
27384   parser->object_scope = NULL_TREE;
27385 }
27386
27387 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
27388    encounter the end of a block before what we were looking for.  */
27389
27390 static bool
27391 cp_parser_cache_group (cp_parser *parser,
27392                        enum cpp_ttype end,
27393                        unsigned depth)
27394 {
27395   while (true)
27396     {
27397       cp_token *token = cp_lexer_peek_token (parser->lexer);
27398
27399       /* Abort a parenthesized expression if we encounter a semicolon.  */
27400       if ((end == CPP_CLOSE_PAREN || depth == 0)
27401           && token->type == CPP_SEMICOLON)
27402         return true;
27403       /* If we've reached the end of the file, stop.  */
27404       if (token->type == CPP_EOF
27405           || (end != CPP_PRAGMA_EOL
27406               && token->type == CPP_PRAGMA_EOL))
27407         return true;
27408       if (token->type == CPP_CLOSE_BRACE && depth == 0)
27409         /* We've hit the end of an enclosing block, so there's been some
27410            kind of syntax error.  */
27411         return true;
27412
27413       /* Consume the token.  */
27414       cp_lexer_consume_token (parser->lexer);
27415       /* See if it starts a new group.  */
27416       if (token->type == CPP_OPEN_BRACE)
27417         {
27418           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27419           /* In theory this should probably check end == '}', but
27420              cp_parser_save_member_function_body needs it to exit
27421              after either '}' or ')' when called with ')'.  */
27422           if (depth == 0)
27423             return false;
27424         }
27425       else if (token->type == CPP_OPEN_PAREN)
27426         {
27427           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27428           if (depth == 0 && end == CPP_CLOSE_PAREN)
27429             return false;
27430         }
27431       else if (token->type == CPP_PRAGMA)
27432         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27433       else if (token->type == end)
27434         return false;
27435     }
27436 }
27437
27438 /* Like above, for caching a default argument or NSDMI.  Both of these are
27439    terminated by a non-nested comma, but it can be unclear whether or not a
27440    comma is nested in a template argument list unless we do more parsing.
27441    In order to handle this ambiguity, when we encounter a ',' after a '<'
27442    we try to parse what follows as a parameter-declaration-list (in the
27443    case of a default argument) or a member-declarator (in the case of an
27444    NSDMI).  If that succeeds, then we stop caching.  */
27445
27446 static tree
27447 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27448 {
27449   unsigned depth = 0;
27450   int maybe_template_id = 0;
27451   cp_token *first_token;
27452   cp_token *token;
27453   tree default_argument;
27454
27455   /* Add tokens until we have processed the entire default
27456      argument.  We add the range [first_token, token).  */
27457   first_token = cp_lexer_peek_token (parser->lexer);
27458   if (first_token->type == CPP_OPEN_BRACE)
27459     {
27460       /* For list-initialization, this is straightforward.  */
27461       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27462       token = cp_lexer_peek_token (parser->lexer);
27463     }
27464   else while (true)
27465     {
27466       bool done = false;
27467
27468       /* Peek at the next token.  */
27469       token = cp_lexer_peek_token (parser->lexer);
27470       /* What we do depends on what token we have.  */
27471       switch (token->type)
27472         {
27473           /* In valid code, a default argument must be
27474              immediately followed by a `,' `)', or `...'.  */
27475         case CPP_COMMA:
27476           if (depth == 0 && maybe_template_id)
27477             {
27478               /* If we've seen a '<', we might be in a
27479                  template-argument-list.  Until Core issue 325 is
27480                  resolved, we don't know how this situation ought
27481                  to be handled, so try to DTRT.  We check whether
27482                  what comes after the comma is a valid parameter
27483                  declaration list.  If it is, then the comma ends
27484                  the default argument; otherwise the default
27485                  argument continues.  */
27486               bool error = false;
27487               cp_token *peek;
27488
27489               /* Set ITALP so cp_parser_parameter_declaration_list
27490                  doesn't decide to commit to this parse.  */
27491               bool saved_italp = parser->in_template_argument_list_p;
27492               parser->in_template_argument_list_p = true;
27493
27494               cp_parser_parse_tentatively (parser);
27495
27496               if (nsdmi)
27497                 {
27498                   /* Parse declarators until we reach a non-comma or
27499                      somthing that cannot be an initializer.
27500                      Just checking whether we're looking at a single
27501                      declarator is insufficient.  Consider:
27502                        int var = tuple<T,U>::x;
27503                      The template parameter 'U' looks exactly like a
27504                      declarator.  */
27505                   do
27506                     {
27507                       int ctor_dtor_or_conv_p;
27508                       cp_lexer_consume_token (parser->lexer);
27509                       cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27510                                             &ctor_dtor_or_conv_p,
27511                                             /*parenthesized_p=*/NULL,
27512                                             /*member_p=*/true,
27513                                             /*friend_p=*/false);
27514                       peek = cp_lexer_peek_token (parser->lexer);
27515                       if (cp_parser_error_occurred (parser))
27516                         break;
27517                     }
27518                   while (peek->type == CPP_COMMA);
27519                   /* If we met an '=' or ';' then the original comma
27520                      was the end of the NSDMI.  Otherwise assume
27521                      we're still in the NSDMI.  */
27522                   error = (peek->type != CPP_EQ
27523                            && peek->type != CPP_SEMICOLON);
27524                 }
27525               else
27526                 {
27527                   cp_lexer_consume_token (parser->lexer);
27528                   begin_scope (sk_function_parms, NULL_TREE);
27529                   cp_parser_parameter_declaration_list (parser, &error);
27530                   pop_bindings_and_leave_scope ();
27531                 }
27532               if (!cp_parser_error_occurred (parser) && !error)
27533                 done = true;
27534               cp_parser_abort_tentative_parse (parser);
27535
27536               parser->in_template_argument_list_p = saved_italp;
27537               break;
27538             }
27539         case CPP_CLOSE_PAREN:
27540         case CPP_ELLIPSIS:
27541           /* If we run into a non-nested `;', `}', or `]',
27542              then the code is invalid -- but the default
27543              argument is certainly over.  */
27544         case CPP_SEMICOLON:
27545         case CPP_CLOSE_BRACE:
27546         case CPP_CLOSE_SQUARE:
27547           if (depth == 0
27548               /* Handle correctly int n = sizeof ... ( p );  */
27549               && token->type != CPP_ELLIPSIS)
27550             done = true;
27551           /* Update DEPTH, if necessary.  */
27552           else if (token->type == CPP_CLOSE_PAREN
27553                    || token->type == CPP_CLOSE_BRACE
27554                    || token->type == CPP_CLOSE_SQUARE)
27555             --depth;
27556           break;
27557
27558         case CPP_OPEN_PAREN:
27559         case CPP_OPEN_SQUARE:
27560         case CPP_OPEN_BRACE:
27561           ++depth;
27562           break;
27563
27564         case CPP_LESS:
27565           if (depth == 0)
27566             /* This might be the comparison operator, or it might
27567                start a template argument list.  */
27568             ++maybe_template_id;
27569           break;
27570
27571         case CPP_RSHIFT:
27572           if (cxx_dialect == cxx98)
27573             break;
27574           /* Fall through for C++0x, which treats the `>>'
27575              operator like two `>' tokens in certain
27576              cases.  */
27577
27578         case CPP_GREATER:
27579           if (depth == 0)
27580             {
27581               /* This might be an operator, or it might close a
27582                  template argument list.  But if a previous '<'
27583                  started a template argument list, this will have
27584                  closed it, so we can't be in one anymore.  */
27585               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27586               if (maybe_template_id < 0)
27587                 maybe_template_id = 0;
27588             }
27589           break;
27590
27591           /* If we run out of tokens, issue an error message.  */
27592         case CPP_EOF:
27593         case CPP_PRAGMA_EOL:
27594           error_at (token->location, "file ends in default argument");
27595           return error_mark_node;
27596
27597         case CPP_NAME:
27598         case CPP_SCOPE:
27599           /* In these cases, we should look for template-ids.
27600              For example, if the default argument is
27601              `X<int, double>()', we need to do name lookup to
27602              figure out whether or not `X' is a template; if
27603              so, the `,' does not end the default argument.
27604
27605              That is not yet done.  */
27606           break;
27607
27608         default:
27609           break;
27610         }
27611
27612       /* If we've reached the end, stop.  */
27613       if (done)
27614         break;
27615
27616       /* Add the token to the token block.  */
27617       token = cp_lexer_consume_token (parser->lexer);
27618     }
27619
27620   /* Create a DEFAULT_ARG to represent the unparsed default
27621      argument.  */
27622   default_argument = make_node (DEFAULT_ARG);
27623   DEFARG_TOKENS (default_argument)
27624     = cp_token_cache_new (first_token, token);
27625   DEFARG_INSTANTIATIONS (default_argument) = NULL;
27626
27627   return default_argument;
27628 }
27629
27630 /* Begin parsing tentatively.  We always save tokens while parsing
27631    tentatively so that if the tentative parsing fails we can restore the
27632    tokens.  */
27633
27634 static void
27635 cp_parser_parse_tentatively (cp_parser* parser)
27636 {
27637   /* Enter a new parsing context.  */
27638   parser->context = cp_parser_context_new (parser->context);
27639   /* Begin saving tokens.  */
27640   cp_lexer_save_tokens (parser->lexer);
27641   /* In order to avoid repetitive access control error messages,
27642      access checks are queued up until we are no longer parsing
27643      tentatively.  */
27644   push_deferring_access_checks (dk_deferred);
27645 }
27646
27647 /* Commit to the currently active tentative parse.  */
27648
27649 static void
27650 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27651 {
27652   cp_parser_context *context;
27653   cp_lexer *lexer;
27654
27655   /* Mark all of the levels as committed.  */
27656   lexer = parser->lexer;
27657   for (context = parser->context; context->next; context = context->next)
27658     {
27659       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27660         break;
27661       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27662       while (!cp_lexer_saving_tokens (lexer))
27663         lexer = lexer->next;
27664       cp_lexer_commit_tokens (lexer);
27665     }
27666 }
27667
27668 /* Commit to the topmost currently active tentative parse.
27669
27670    Note that this function shouldn't be called when there are
27671    irreversible side-effects while in a tentative state.  For
27672    example, we shouldn't create a permanent entry in the symbol
27673    table, or issue an error message that might not apply if the
27674    tentative parse is aborted.  */
27675
27676 static void
27677 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27678 {
27679   cp_parser_context *context = parser->context;
27680   cp_lexer *lexer = parser->lexer;
27681
27682   if (context)
27683     {
27684       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27685         return;
27686       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27687
27688       while (!cp_lexer_saving_tokens (lexer))
27689         lexer = lexer->next;
27690       cp_lexer_commit_tokens (lexer);
27691     }
27692 }
27693
27694 /* Abort the currently active tentative parse.  All consumed tokens
27695    will be rolled back, and no diagnostics will be issued.  */
27696
27697 static void
27698 cp_parser_abort_tentative_parse (cp_parser* parser)
27699 {
27700   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27701               || errorcount > 0);
27702   cp_parser_simulate_error (parser);
27703   /* Now, pretend that we want to see if the construct was
27704      successfully parsed.  */
27705   cp_parser_parse_definitely (parser);
27706 }
27707
27708 /* Stop parsing tentatively.  If a parse error has occurred, restore the
27709    token stream.  Otherwise, commit to the tokens we have consumed.
27710    Returns true if no error occurred; false otherwise.  */
27711
27712 static bool
27713 cp_parser_parse_definitely (cp_parser* parser)
27714 {
27715   bool error_occurred;
27716   cp_parser_context *context;
27717
27718   /* Remember whether or not an error occurred, since we are about to
27719      destroy that information.  */
27720   error_occurred = cp_parser_error_occurred (parser);
27721   /* Remove the topmost context from the stack.  */
27722   context = parser->context;
27723   parser->context = context->next;
27724   /* If no parse errors occurred, commit to the tentative parse.  */
27725   if (!error_occurred)
27726     {
27727       /* Commit to the tokens read tentatively, unless that was
27728          already done.  */
27729       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27730         cp_lexer_commit_tokens (parser->lexer);
27731
27732       pop_to_parent_deferring_access_checks ();
27733     }
27734   /* Otherwise, if errors occurred, roll back our state so that things
27735      are just as they were before we began the tentative parse.  */
27736   else
27737     {
27738       cp_lexer_rollback_tokens (parser->lexer);
27739       pop_deferring_access_checks ();
27740     }
27741   /* Add the context to the front of the free list.  */
27742   context->next = cp_parser_context_free_list;
27743   cp_parser_context_free_list = context;
27744
27745   return !error_occurred;
27746 }
27747
27748 /* Returns true if we are parsing tentatively and are not committed to
27749    this tentative parse.  */
27750
27751 static bool
27752 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27753 {
27754   return (cp_parser_parsing_tentatively (parser)
27755           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27756 }
27757
27758 /* Returns nonzero iff an error has occurred during the most recent
27759    tentative parse.  */
27760
27761 static bool
27762 cp_parser_error_occurred (cp_parser* parser)
27763 {
27764   return (cp_parser_parsing_tentatively (parser)
27765           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27766 }
27767
27768 /* Returns nonzero if GNU extensions are allowed.  */
27769
27770 static bool
27771 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27772 {
27773   return parser->allow_gnu_extensions_p;
27774 }
27775 \f
27776 /* Objective-C++ Productions */
27777
27778
27779 /* Parse an Objective-C expression, which feeds into a primary-expression
27780    above.
27781
27782    objc-expression:
27783      objc-message-expression
27784      objc-string-literal
27785      objc-encode-expression
27786      objc-protocol-expression
27787      objc-selector-expression
27788
27789   Returns a tree representation of the expression.  */
27790
27791 static cp_expr
27792 cp_parser_objc_expression (cp_parser* parser)
27793 {
27794   /* Try to figure out what kind of declaration is present.  */
27795   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27796
27797   switch (kwd->type)
27798     {
27799     case CPP_OPEN_SQUARE:
27800       return cp_parser_objc_message_expression (parser);
27801
27802     case CPP_OBJC_STRING:
27803       kwd = cp_lexer_consume_token (parser->lexer);
27804       return objc_build_string_object (kwd->u.value);
27805
27806     case CPP_KEYWORD:
27807       switch (kwd->keyword)
27808         {
27809         case RID_AT_ENCODE:
27810           return cp_parser_objc_encode_expression (parser);
27811
27812         case RID_AT_PROTOCOL:
27813           return cp_parser_objc_protocol_expression (parser);
27814
27815         case RID_AT_SELECTOR:
27816           return cp_parser_objc_selector_expression (parser);
27817
27818         default:
27819           break;
27820         }
27821     default:
27822       error_at (kwd->location,
27823                 "misplaced %<@%D%> Objective-C++ construct",
27824                 kwd->u.value);
27825       cp_parser_skip_to_end_of_block_or_statement (parser);
27826     }
27827
27828   return error_mark_node;
27829 }
27830
27831 /* Parse an Objective-C message expression.
27832
27833    objc-message-expression:
27834      [ objc-message-receiver objc-message-args ]
27835
27836    Returns a representation of an Objective-C message.  */
27837
27838 static tree
27839 cp_parser_objc_message_expression (cp_parser* parser)
27840 {
27841   tree receiver, messageargs;
27842
27843   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27844   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
27845   receiver = cp_parser_objc_message_receiver (parser);
27846   messageargs = cp_parser_objc_message_args (parser);
27847   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27848   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27849
27850   tree result = objc_build_message_expr (receiver, messageargs);
27851
27852   /* Construct a location e.g.
27853        [self func1:5]
27854        ^~~~~~~~~~~~~~
27855      ranging from the '[' to the ']', with the caret at the start.  */
27856   location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27857   protected_set_expr_location (result, combined_loc);
27858
27859   return result;
27860 }
27861
27862 /* Parse an objc-message-receiver.
27863
27864    objc-message-receiver:
27865      expression
27866      simple-type-specifier
27867
27868   Returns a representation of the type or expression.  */
27869
27870 static tree
27871 cp_parser_objc_message_receiver (cp_parser* parser)
27872 {
27873   tree rcv;
27874
27875   /* An Objective-C message receiver may be either (1) a type
27876      or (2) an expression.  */
27877   cp_parser_parse_tentatively (parser);
27878   rcv = cp_parser_expression (parser);
27879
27880   /* If that worked out, fine.  */
27881   if (cp_parser_parse_definitely (parser))
27882     return rcv;
27883
27884   cp_parser_parse_tentatively (parser);
27885   rcv = cp_parser_simple_type_specifier (parser,
27886                                          /*decl_specs=*/NULL,
27887                                          CP_PARSER_FLAGS_NONE);
27888
27889   if (cp_parser_parse_definitely (parser))
27890     return objc_get_class_reference (rcv);
27891   
27892   cp_parser_error (parser, "objective-c++ message receiver expected");
27893   return error_mark_node;
27894 }
27895
27896 /* Parse the arguments and selectors comprising an Objective-C message.
27897
27898    objc-message-args:
27899      objc-selector
27900      objc-selector-args
27901      objc-selector-args , objc-comma-args
27902
27903    objc-selector-args:
27904      objc-selector [opt] : assignment-expression
27905      objc-selector-args objc-selector [opt] : assignment-expression
27906
27907    objc-comma-args:
27908      assignment-expression
27909      objc-comma-args , assignment-expression
27910
27911    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27912    selector arguments and TREE_VALUE containing a list of comma
27913    arguments.  */
27914
27915 static tree
27916 cp_parser_objc_message_args (cp_parser* parser)
27917 {
27918   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27919   bool maybe_unary_selector_p = true;
27920   cp_token *token = cp_lexer_peek_token (parser->lexer);
27921
27922   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27923     {
27924       tree selector = NULL_TREE, arg;
27925
27926       if (token->type != CPP_COLON)
27927         selector = cp_parser_objc_selector (parser);
27928
27929       /* Detect if we have a unary selector.  */
27930       if (maybe_unary_selector_p
27931           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27932         return build_tree_list (selector, NULL_TREE);
27933
27934       maybe_unary_selector_p = false;
27935       cp_parser_require (parser, CPP_COLON, RT_COLON);
27936       arg = cp_parser_assignment_expression (parser);
27937
27938       sel_args
27939         = chainon (sel_args,
27940                    build_tree_list (selector, arg));
27941
27942       token = cp_lexer_peek_token (parser->lexer);
27943     }
27944
27945   /* Handle non-selector arguments, if any. */
27946   while (token->type == CPP_COMMA)
27947     {
27948       tree arg;
27949
27950       cp_lexer_consume_token (parser->lexer);
27951       arg = cp_parser_assignment_expression (parser);
27952
27953       addl_args
27954         = chainon (addl_args,
27955                    build_tree_list (NULL_TREE, arg));
27956
27957       token = cp_lexer_peek_token (parser->lexer);
27958     }
27959
27960   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27961     {
27962       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27963       return build_tree_list (error_mark_node, error_mark_node);
27964     }
27965
27966   return build_tree_list (sel_args, addl_args);
27967 }
27968
27969 /* Parse an Objective-C encode expression.
27970
27971    objc-encode-expression:
27972      @encode objc-typename
27973
27974    Returns an encoded representation of the type argument.  */
27975
27976 static cp_expr
27977 cp_parser_objc_encode_expression (cp_parser* parser)
27978 {
27979   tree type;
27980   cp_token *token;
27981   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27982
27983   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
27984   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27985   token = cp_lexer_peek_token (parser->lexer);
27986   type = complete_type (cp_parser_type_id (parser));
27987   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27988
27989   if (!type)
27990     {
27991       error_at (token->location, 
27992                 "%<@encode%> must specify a type as an argument");
27993       return error_mark_node;
27994     }
27995
27996   /* This happens if we find @encode(T) (where T is a template
27997      typename or something dependent on a template typename) when
27998      parsing a template.  In that case, we can't compile it
27999      immediately, but we rather create an AT_ENCODE_EXPR which will
28000      need to be instantiated when the template is used.
28001   */
28002   if (dependent_type_p (type))
28003     {
28004       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28005       TREE_READONLY (value) = 1;
28006       return value;
28007     }
28008
28009
28010   /* Build a location of the form:
28011        @encode(int)
28012        ^~~~~~~~~~~~
28013      with caret==start at the @ token, finishing at the close paren.  */
28014   location_t combined_loc
28015     = make_location (start_loc, start_loc,
28016                      cp_lexer_previous_token (parser->lexer)->location);
28017
28018   return cp_expr (objc_build_encode_expr (type), combined_loc);
28019 }
28020
28021 /* Parse an Objective-C @defs expression.  */
28022
28023 static tree
28024 cp_parser_objc_defs_expression (cp_parser *parser)
28025 {
28026   tree name;
28027
28028   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
28029   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28030   name = cp_parser_identifier (parser);
28031   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28032
28033   return objc_get_class_ivars (name);
28034 }
28035
28036 /* Parse an Objective-C protocol expression.
28037
28038   objc-protocol-expression:
28039     @protocol ( identifier )
28040
28041   Returns a representation of the protocol expression.  */
28042
28043 static tree
28044 cp_parser_objc_protocol_expression (cp_parser* parser)
28045 {
28046   tree proto;
28047   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28048
28049   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
28050   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28051   proto = cp_parser_identifier (parser);
28052   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28053
28054   /* Build a location of the form:
28055        @protocol(prot)
28056        ^~~~~~~~~~~~~~~
28057      with caret==start at the @ token, finishing at the close paren.  */
28058   location_t combined_loc
28059     = make_location (start_loc, start_loc,
28060                      cp_lexer_previous_token (parser->lexer)->location);
28061   tree result = objc_build_protocol_expr (proto);
28062   protected_set_expr_location (result, combined_loc);
28063   return result;
28064 }
28065
28066 /* Parse an Objective-C selector expression.
28067
28068    objc-selector-expression:
28069      @selector ( objc-method-signature )
28070
28071    objc-method-signature:
28072      objc-selector
28073      objc-selector-seq
28074
28075    objc-selector-seq:
28076      objc-selector :
28077      objc-selector-seq objc-selector :
28078
28079   Returns a representation of the method selector.  */
28080
28081 static tree
28082 cp_parser_objc_selector_expression (cp_parser* parser)
28083 {
28084   tree sel_seq = NULL_TREE;
28085   bool maybe_unary_selector_p = true;
28086   cp_token *token;
28087   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28088
28089   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
28090   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28091   token = cp_lexer_peek_token (parser->lexer);
28092
28093   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28094          || token->type == CPP_SCOPE)
28095     {
28096       tree selector = NULL_TREE;
28097
28098       if (token->type != CPP_COLON
28099           || token->type == CPP_SCOPE)
28100         selector = cp_parser_objc_selector (parser);
28101
28102       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28103           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28104         {
28105           /* Detect if we have a unary selector.  */
28106           if (maybe_unary_selector_p)
28107             {
28108               sel_seq = selector;
28109               goto finish_selector;
28110             }
28111           else
28112             {
28113               cp_parser_error (parser, "expected %<:%>");
28114             }
28115         }
28116       maybe_unary_selector_p = false;
28117       token = cp_lexer_consume_token (parser->lexer);
28118
28119       if (token->type == CPP_SCOPE)
28120         {
28121           sel_seq
28122             = chainon (sel_seq,
28123                        build_tree_list (selector, NULL_TREE));
28124           sel_seq
28125             = chainon (sel_seq,
28126                        build_tree_list (NULL_TREE, NULL_TREE));
28127         }
28128       else
28129         sel_seq
28130           = chainon (sel_seq,
28131                      build_tree_list (selector, NULL_TREE));
28132
28133       token = cp_lexer_peek_token (parser->lexer);
28134     }
28135
28136  finish_selector:
28137   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28138
28139
28140   /* Build a location of the form:
28141        @selector(func)
28142        ^~~~~~~~~~~~~~~
28143      with caret==start at the @ token, finishing at the close paren.  */
28144   location_t combined_loc
28145     = make_location (loc, loc,
28146                      cp_lexer_previous_token (parser->lexer)->location);
28147   tree result = objc_build_selector_expr (combined_loc, sel_seq);
28148   /* TODO: objc_build_selector_expr doesn't always honor the location.  */
28149   protected_set_expr_location (result, combined_loc);
28150   return result;
28151 }
28152
28153 /* Parse a list of identifiers.
28154
28155    objc-identifier-list:
28156      identifier
28157      objc-identifier-list , identifier
28158
28159    Returns a TREE_LIST of identifier nodes.  */
28160
28161 static tree
28162 cp_parser_objc_identifier_list (cp_parser* parser)
28163 {
28164   tree identifier;
28165   tree list;
28166   cp_token *sep;
28167
28168   identifier = cp_parser_identifier (parser);
28169   if (identifier == error_mark_node)
28170     return error_mark_node;      
28171
28172   list = build_tree_list (NULL_TREE, identifier);
28173   sep = cp_lexer_peek_token (parser->lexer);
28174
28175   while (sep->type == CPP_COMMA)
28176     {
28177       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
28178       identifier = cp_parser_identifier (parser);
28179       if (identifier == error_mark_node)
28180         return list;
28181
28182       list = chainon (list, build_tree_list (NULL_TREE,
28183                                              identifier));
28184       sep = cp_lexer_peek_token (parser->lexer);
28185     }
28186   
28187   return list;
28188 }
28189
28190 /* Parse an Objective-C alias declaration.
28191
28192    objc-alias-declaration:
28193      @compatibility_alias identifier identifier ;
28194
28195    This function registers the alias mapping with the Objective-C front end.
28196    It returns nothing.  */
28197
28198 static void
28199 cp_parser_objc_alias_declaration (cp_parser* parser)
28200 {
28201   tree alias, orig;
28202
28203   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
28204   alias = cp_parser_identifier (parser);
28205   orig = cp_parser_identifier (parser);
28206   objc_declare_alias (alias, orig);
28207   cp_parser_consume_semicolon_at_end_of_statement (parser);
28208 }
28209
28210 /* Parse an Objective-C class forward-declaration.
28211
28212    objc-class-declaration:
28213      @class objc-identifier-list ;
28214
28215    The function registers the forward declarations with the Objective-C
28216    front end.  It returns nothing.  */
28217
28218 static void
28219 cp_parser_objc_class_declaration (cp_parser* parser)
28220 {
28221   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
28222   while (true)
28223     {
28224       tree id;
28225       
28226       id = cp_parser_identifier (parser);
28227       if (id == error_mark_node)
28228         break;
28229       
28230       objc_declare_class (id);
28231
28232       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28233         cp_lexer_consume_token (parser->lexer);
28234       else
28235         break;
28236     }
28237   cp_parser_consume_semicolon_at_end_of_statement (parser);
28238 }
28239
28240 /* Parse a list of Objective-C protocol references.
28241
28242    objc-protocol-refs-opt:
28243      objc-protocol-refs [opt]
28244
28245    objc-protocol-refs:
28246      < objc-identifier-list >
28247
28248    Returns a TREE_LIST of identifiers, if any.  */
28249
28250 static tree
28251 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28252 {
28253   tree protorefs = NULL_TREE;
28254
28255   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28256     {
28257       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
28258       protorefs = cp_parser_objc_identifier_list (parser);
28259       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28260     }
28261
28262   return protorefs;
28263 }
28264
28265 /* Parse a Objective-C visibility specification.  */
28266
28267 static void
28268 cp_parser_objc_visibility_spec (cp_parser* parser)
28269 {
28270   cp_token *vis = cp_lexer_peek_token (parser->lexer);
28271
28272   switch (vis->keyword)
28273     {
28274     case RID_AT_PRIVATE:
28275       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28276       break;
28277     case RID_AT_PROTECTED:
28278       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28279       break;
28280     case RID_AT_PUBLIC:
28281       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28282       break;
28283     case RID_AT_PACKAGE:
28284       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28285       break;
28286     default:
28287       return;
28288     }
28289
28290   /* Eat '@private'/'@protected'/'@public'.  */
28291   cp_lexer_consume_token (parser->lexer);
28292 }
28293
28294 /* Parse an Objective-C method type.  Return 'true' if it is a class
28295    (+) method, and 'false' if it is an instance (-) method.  */
28296
28297 static inline bool
28298 cp_parser_objc_method_type (cp_parser* parser)
28299 {
28300   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28301     return true;
28302   else
28303     return false;
28304 }
28305
28306 /* Parse an Objective-C protocol qualifier.  */
28307
28308 static tree
28309 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28310 {
28311   tree quals = NULL_TREE, node;
28312   cp_token *token = cp_lexer_peek_token (parser->lexer);
28313
28314   node = token->u.value;
28315
28316   while (node && identifier_p (node)
28317          && (node == ridpointers [(int) RID_IN]
28318              || node == ridpointers [(int) RID_OUT]
28319              || node == ridpointers [(int) RID_INOUT]
28320              || node == ridpointers [(int) RID_BYCOPY]
28321              || node == ridpointers [(int) RID_BYREF]
28322              || node == ridpointers [(int) RID_ONEWAY]))
28323     {
28324       quals = tree_cons (NULL_TREE, node, quals);
28325       cp_lexer_consume_token (parser->lexer);
28326       token = cp_lexer_peek_token (parser->lexer);
28327       node = token->u.value;
28328     }
28329
28330   return quals;
28331 }
28332
28333 /* Parse an Objective-C typename.  */
28334
28335 static tree
28336 cp_parser_objc_typename (cp_parser* parser)
28337 {
28338   tree type_name = NULL_TREE;
28339
28340   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28341     {
28342       tree proto_quals, cp_type = NULL_TREE;
28343
28344       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
28345       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28346
28347       /* An ObjC type name may consist of just protocol qualifiers, in which
28348          case the type shall default to 'id'.  */
28349       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28350         {
28351           cp_type = cp_parser_type_id (parser);
28352           
28353           /* If the type could not be parsed, an error has already
28354              been produced.  For error recovery, behave as if it had
28355              not been specified, which will use the default type
28356              'id'.  */
28357           if (cp_type == error_mark_node)
28358             {
28359               cp_type = NULL_TREE;
28360               /* We need to skip to the closing parenthesis as
28361                  cp_parser_type_id() does not seem to do it for
28362                  us.  */
28363               cp_parser_skip_to_closing_parenthesis (parser,
28364                                                      /*recovering=*/true,
28365                                                      /*or_comma=*/false,
28366                                                      /*consume_paren=*/false);
28367             }
28368         }
28369
28370       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28371       type_name = build_tree_list (proto_quals, cp_type);
28372     }
28373
28374   return type_name;
28375 }
28376
28377 /* Check to see if TYPE refers to an Objective-C selector name.  */
28378
28379 static bool
28380 cp_parser_objc_selector_p (enum cpp_ttype type)
28381 {
28382   return (type == CPP_NAME || type == CPP_KEYWORD
28383           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28384           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28385           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28386           || type == CPP_XOR || type == CPP_XOR_EQ);
28387 }
28388
28389 /* Parse an Objective-C selector.  */
28390
28391 static tree
28392 cp_parser_objc_selector (cp_parser* parser)
28393 {
28394   cp_token *token = cp_lexer_consume_token (parser->lexer);
28395
28396   if (!cp_parser_objc_selector_p (token->type))
28397     {
28398       error_at (token->location, "invalid Objective-C++ selector name");
28399       return error_mark_node;
28400     }
28401
28402   /* C++ operator names are allowed to appear in ObjC selectors.  */
28403   switch (token->type)
28404     {
28405     case CPP_AND_AND: return get_identifier ("and");
28406     case CPP_AND_EQ: return get_identifier ("and_eq");
28407     case CPP_AND: return get_identifier ("bitand");
28408     case CPP_OR: return get_identifier ("bitor");
28409     case CPP_COMPL: return get_identifier ("compl");
28410     case CPP_NOT: return get_identifier ("not");
28411     case CPP_NOT_EQ: return get_identifier ("not_eq");
28412     case CPP_OR_OR: return get_identifier ("or");
28413     case CPP_OR_EQ: return get_identifier ("or_eq");
28414     case CPP_XOR: return get_identifier ("xor");
28415     case CPP_XOR_EQ: return get_identifier ("xor_eq");
28416     default: return token->u.value;
28417     }
28418 }
28419
28420 /* Parse an Objective-C params list.  */
28421
28422 static tree
28423 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28424 {
28425   tree params = NULL_TREE;
28426   bool maybe_unary_selector_p = true;
28427   cp_token *token = cp_lexer_peek_token (parser->lexer);
28428
28429   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28430     {
28431       tree selector = NULL_TREE, type_name, identifier;
28432       tree parm_attr = NULL_TREE;
28433
28434       if (token->keyword == RID_ATTRIBUTE)
28435         break;
28436
28437       if (token->type != CPP_COLON)
28438         selector = cp_parser_objc_selector (parser);
28439
28440       /* Detect if we have a unary selector.  */
28441       if (maybe_unary_selector_p
28442           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28443         {
28444           params = selector; /* Might be followed by attributes.  */
28445           break;
28446         }
28447
28448       maybe_unary_selector_p = false;
28449       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28450         {
28451           /* Something went quite wrong.  There should be a colon
28452              here, but there is not.  Stop parsing parameters.  */
28453           break;
28454         }
28455       type_name = cp_parser_objc_typename (parser);
28456       /* New ObjC allows attributes on parameters too.  */
28457       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28458         parm_attr = cp_parser_attributes_opt (parser);
28459       identifier = cp_parser_identifier (parser);
28460
28461       params
28462         = chainon (params,
28463                    objc_build_keyword_decl (selector,
28464                                             type_name,
28465                                             identifier,
28466                                             parm_attr));
28467
28468       token = cp_lexer_peek_token (parser->lexer);
28469     }
28470
28471   if (params == NULL_TREE)
28472     {
28473       cp_parser_error (parser, "objective-c++ method declaration is expected");
28474       return error_mark_node;
28475     }
28476
28477   /* We allow tail attributes for the method.  */
28478   if (token->keyword == RID_ATTRIBUTE)
28479     {
28480       *attributes = cp_parser_attributes_opt (parser);
28481       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28482           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28483         return params;
28484       cp_parser_error (parser, 
28485                        "method attributes must be specified at the end");
28486       return error_mark_node;
28487     }
28488
28489   if (params == NULL_TREE)
28490     {
28491       cp_parser_error (parser, "objective-c++ method declaration is expected");
28492       return error_mark_node;
28493     }
28494   return params;
28495 }
28496
28497 /* Parse the non-keyword Objective-C params.  */
28498
28499 static tree
28500 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
28501                                        tree* attributes)
28502 {
28503   tree params = make_node (TREE_LIST);
28504   cp_token *token = cp_lexer_peek_token (parser->lexer);
28505   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
28506
28507   while (token->type == CPP_COMMA)
28508     {
28509       cp_parameter_declarator *parmdecl;
28510       tree parm;
28511
28512       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
28513       token = cp_lexer_peek_token (parser->lexer);
28514
28515       if (token->type == CPP_ELLIPSIS)
28516         {
28517           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
28518           *ellipsisp = true;
28519           token = cp_lexer_peek_token (parser->lexer);
28520           break;
28521         }
28522
28523       /* TODO: parse attributes for tail parameters.  */
28524       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28525       parm = grokdeclarator (parmdecl->declarator,
28526                              &parmdecl->decl_specifiers,
28527                              PARM, /*initialized=*/0,
28528                              /*attrlist=*/NULL);
28529
28530       chainon (params, build_tree_list (NULL_TREE, parm));
28531       token = cp_lexer_peek_token (parser->lexer);
28532     }
28533
28534   /* We allow tail attributes for the method.  */
28535   if (token->keyword == RID_ATTRIBUTE)
28536     {
28537       if (*attributes == NULL_TREE)
28538         {
28539           *attributes = cp_parser_attributes_opt (parser);
28540           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28541               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28542             return params;
28543         }
28544       else        
28545         /* We have an error, but parse the attributes, so that we can 
28546            carry on.  */
28547         *attributes = cp_parser_attributes_opt (parser);
28548
28549       cp_parser_error (parser, 
28550                        "method attributes must be specified at the end");
28551       return error_mark_node;
28552     }
28553
28554   return params;
28555 }
28556
28557 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
28558
28559 static void
28560 cp_parser_objc_interstitial_code (cp_parser* parser)
28561 {
28562   cp_token *token = cp_lexer_peek_token (parser->lexer);
28563
28564   /* If the next token is `extern' and the following token is a string
28565      literal, then we have a linkage specification.  */
28566   if (token->keyword == RID_EXTERN
28567       && cp_parser_is_pure_string_literal
28568          (cp_lexer_peek_nth_token (parser->lexer, 2)))
28569     cp_parser_linkage_specification (parser);
28570   /* Handle #pragma, if any.  */
28571   else if (token->type == CPP_PRAGMA)
28572     cp_parser_pragma (parser, pragma_objc_icode, NULL);
28573   /* Allow stray semicolons.  */
28574   else if (token->type == CPP_SEMICOLON)
28575     cp_lexer_consume_token (parser->lexer);
28576   /* Mark methods as optional or required, when building protocols.  */
28577   else if (token->keyword == RID_AT_OPTIONAL)
28578     {
28579       cp_lexer_consume_token (parser->lexer);
28580       objc_set_method_opt (true);
28581     }
28582   else if (token->keyword == RID_AT_REQUIRED)
28583     {
28584       cp_lexer_consume_token (parser->lexer);
28585       objc_set_method_opt (false);
28586     }
28587   else if (token->keyword == RID_NAMESPACE)
28588     cp_parser_namespace_definition (parser);
28589   /* Other stray characters must generate errors.  */
28590   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28591     {
28592       cp_lexer_consume_token (parser->lexer);
28593       error ("stray %qs between Objective-C++ methods",
28594              token->type == CPP_OPEN_BRACE ? "{" : "}");
28595     }
28596   /* Finally, try to parse a block-declaration, or a function-definition.  */
28597   else
28598     cp_parser_block_declaration (parser, /*statement_p=*/false);
28599 }
28600
28601 /* Parse a method signature.  */
28602
28603 static tree
28604 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28605 {
28606   tree rettype, kwdparms, optparms;
28607   bool ellipsis = false;
28608   bool is_class_method;
28609
28610   is_class_method = cp_parser_objc_method_type (parser);
28611   rettype = cp_parser_objc_typename (parser);
28612   *attributes = NULL_TREE;
28613   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28614   if (kwdparms == error_mark_node)
28615     return error_mark_node;
28616   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28617   if (optparms == error_mark_node)
28618     return error_mark_node;
28619
28620   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28621 }
28622
28623 static bool
28624 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28625 {
28626   tree tattr;  
28627   cp_lexer_save_tokens (parser->lexer);
28628   tattr = cp_parser_attributes_opt (parser);
28629   gcc_assert (tattr) ;
28630   
28631   /* If the attributes are followed by a method introducer, this is not allowed.
28632      Dump the attributes and flag the situation.  */
28633   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28634       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28635     return true;
28636
28637   /* Otherwise, the attributes introduce some interstitial code, possibly so
28638      rewind to allow that check.  */
28639   cp_lexer_rollback_tokens (parser->lexer);
28640   return false;  
28641 }
28642
28643 /* Parse an Objective-C method prototype list.  */
28644
28645 static void
28646 cp_parser_objc_method_prototype_list (cp_parser* parser)
28647 {
28648   cp_token *token = cp_lexer_peek_token (parser->lexer);
28649
28650   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28651     {
28652       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28653         {
28654           tree attributes, sig;
28655           bool is_class_method;
28656           if (token->type == CPP_PLUS)
28657             is_class_method = true;
28658           else
28659             is_class_method = false;
28660           sig = cp_parser_objc_method_signature (parser, &attributes);
28661           if (sig == error_mark_node)
28662             {
28663               cp_parser_skip_to_end_of_block_or_statement (parser);
28664               token = cp_lexer_peek_token (parser->lexer);
28665               continue;
28666             }
28667           objc_add_method_declaration (is_class_method, sig, attributes);
28668           cp_parser_consume_semicolon_at_end_of_statement (parser);
28669         }
28670       else if (token->keyword == RID_AT_PROPERTY)
28671         cp_parser_objc_at_property_declaration (parser);
28672       else if (token->keyword == RID_ATTRIBUTE 
28673                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28674         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
28675                     OPT_Wattributes, 
28676                     "prefix attributes are ignored for methods");
28677       else
28678         /* Allow for interspersed non-ObjC++ code.  */
28679         cp_parser_objc_interstitial_code (parser);
28680
28681       token = cp_lexer_peek_token (parser->lexer);
28682     }
28683
28684   if (token->type != CPP_EOF)
28685     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
28686   else
28687     cp_parser_error (parser, "expected %<@end%>");
28688
28689   objc_finish_interface ();
28690 }
28691
28692 /* Parse an Objective-C method definition list.  */
28693
28694 static void
28695 cp_parser_objc_method_definition_list (cp_parser* parser)
28696 {
28697   cp_token *token = cp_lexer_peek_token (parser->lexer);
28698
28699   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28700     {
28701       tree meth;
28702
28703       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28704         {
28705           cp_token *ptk;
28706           tree sig, attribute;
28707           bool is_class_method;
28708           if (token->type == CPP_PLUS)
28709             is_class_method = true;
28710           else
28711             is_class_method = false;
28712           push_deferring_access_checks (dk_deferred);
28713           sig = cp_parser_objc_method_signature (parser, &attribute);
28714           if (sig == error_mark_node)
28715             {
28716               cp_parser_skip_to_end_of_block_or_statement (parser);
28717               token = cp_lexer_peek_token (parser->lexer);
28718               continue;
28719             }
28720           objc_start_method_definition (is_class_method, sig, attribute,
28721                                         NULL_TREE);
28722
28723           /* For historical reasons, we accept an optional semicolon.  */
28724           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28725             cp_lexer_consume_token (parser->lexer);
28726
28727           ptk = cp_lexer_peek_token (parser->lexer);
28728           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
28729                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28730             {
28731               perform_deferred_access_checks (tf_warning_or_error);
28732               stop_deferring_access_checks ();
28733               meth = cp_parser_function_definition_after_declarator (parser,
28734                                                                      false);
28735               pop_deferring_access_checks ();
28736               objc_finish_method_definition (meth);
28737             }
28738         }
28739       /* The following case will be removed once @synthesize is
28740          completely implemented.  */
28741       else if (token->keyword == RID_AT_PROPERTY)
28742         cp_parser_objc_at_property_declaration (parser);
28743       else if (token->keyword == RID_AT_SYNTHESIZE)
28744         cp_parser_objc_at_synthesize_declaration (parser);
28745       else if (token->keyword == RID_AT_DYNAMIC)
28746         cp_parser_objc_at_dynamic_declaration (parser);
28747       else if (token->keyword == RID_ATTRIBUTE 
28748                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28749         warning_at (token->location, OPT_Wattributes,
28750                     "prefix attributes are ignored for methods");
28751       else
28752         /* Allow for interspersed non-ObjC++ code.  */
28753         cp_parser_objc_interstitial_code (parser);
28754
28755       token = cp_lexer_peek_token (parser->lexer);
28756     }
28757
28758   if (token->type != CPP_EOF)
28759     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
28760   else
28761     cp_parser_error (parser, "expected %<@end%>");
28762
28763   objc_finish_implementation ();
28764 }
28765
28766 /* Parse Objective-C ivars.  */
28767
28768 static void
28769 cp_parser_objc_class_ivars (cp_parser* parser)
28770 {
28771   cp_token *token = cp_lexer_peek_token (parser->lexer);
28772
28773   if (token->type != CPP_OPEN_BRACE)
28774     return;     /* No ivars specified.  */
28775
28776   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
28777   token = cp_lexer_peek_token (parser->lexer);
28778
28779   while (token->type != CPP_CLOSE_BRACE 
28780         && token->keyword != RID_AT_END && token->type != CPP_EOF)
28781     {
28782       cp_decl_specifier_seq declspecs;
28783       int decl_class_or_enum_p;
28784       tree prefix_attributes;
28785
28786       cp_parser_objc_visibility_spec (parser);
28787
28788       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28789         break;
28790
28791       cp_parser_decl_specifier_seq (parser,
28792                                     CP_PARSER_FLAGS_OPTIONAL,
28793                                     &declspecs,
28794                                     &decl_class_or_enum_p);
28795
28796       /* auto, register, static, extern, mutable.  */
28797       if (declspecs.storage_class != sc_none)
28798         {
28799           cp_parser_error (parser, "invalid type for instance variable");         
28800           declspecs.storage_class = sc_none;
28801         }
28802
28803       /* thread_local.  */
28804       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28805         {
28806           cp_parser_error (parser, "invalid type for instance variable");
28807           declspecs.locations[ds_thread] = 0;
28808         }
28809       
28810       /* typedef.  */
28811       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28812         {
28813           cp_parser_error (parser, "invalid type for instance variable");
28814           declspecs.locations[ds_typedef] = 0;
28815         }
28816
28817       prefix_attributes = declspecs.attributes;
28818       declspecs.attributes = NULL_TREE;
28819
28820       /* Keep going until we hit the `;' at the end of the
28821          declaration.  */
28822       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28823         {
28824           tree width = NULL_TREE, attributes, first_attribute, decl;
28825           cp_declarator *declarator = NULL;
28826           int ctor_dtor_or_conv_p;
28827
28828           /* Check for a (possibly unnamed) bitfield declaration.  */
28829           token = cp_lexer_peek_token (parser->lexer);
28830           if (token->type == CPP_COLON)
28831             goto eat_colon;
28832
28833           if (token->type == CPP_NAME
28834               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28835                   == CPP_COLON))
28836             {
28837               /* Get the name of the bitfield.  */
28838               declarator = make_id_declarator (NULL_TREE,
28839                                                cp_parser_identifier (parser),
28840                                                sfk_none);
28841
28842              eat_colon:
28843               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
28844               /* Get the width of the bitfield.  */
28845               width
28846                 = cp_parser_constant_expression (parser);
28847             }
28848           else
28849             {
28850               /* Parse the declarator.  */
28851               declarator
28852                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28853                                         &ctor_dtor_or_conv_p,
28854                                         /*parenthesized_p=*/NULL,
28855                                         /*member_p=*/false,
28856                                         /*friend_p=*/false);
28857             }
28858
28859           /* Look for attributes that apply to the ivar.  */
28860           attributes = cp_parser_attributes_opt (parser);
28861           /* Remember which attributes are prefix attributes and
28862              which are not.  */
28863           first_attribute = attributes;
28864           /* Combine the attributes.  */
28865           attributes = chainon (prefix_attributes, attributes);
28866
28867           if (width)
28868               /* Create the bitfield declaration.  */
28869               decl = grokbitfield (declarator, &declspecs,
28870                                    width,
28871                                    attributes);
28872           else
28873             decl = grokfield (declarator, &declspecs,
28874                               NULL_TREE, /*init_const_expr_p=*/false,
28875                               NULL_TREE, attributes);
28876
28877           /* Add the instance variable.  */
28878           if (decl != error_mark_node && decl != NULL_TREE)
28879             objc_add_instance_variable (decl);
28880
28881           /* Reset PREFIX_ATTRIBUTES.  */
28882           while (attributes && TREE_CHAIN (attributes) != first_attribute)
28883             attributes = TREE_CHAIN (attributes);
28884           if (attributes)
28885             TREE_CHAIN (attributes) = NULL_TREE;
28886
28887           token = cp_lexer_peek_token (parser->lexer);
28888
28889           if (token->type == CPP_COMMA)
28890             {
28891               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
28892               continue;
28893             }
28894           break;
28895         }
28896
28897       cp_parser_consume_semicolon_at_end_of_statement (parser);
28898       token = cp_lexer_peek_token (parser->lexer);
28899     }
28900
28901   if (token->keyword == RID_AT_END)
28902     cp_parser_error (parser, "expected %<}%>");
28903
28904   /* Do not consume the RID_AT_END, so it will be read again as terminating
28905      the @interface of @implementation.  */ 
28906   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28907     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
28908     
28909   /* For historical reasons, we accept an optional semicolon.  */
28910   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28911     cp_lexer_consume_token (parser->lexer);
28912 }
28913
28914 /* Parse an Objective-C protocol declaration.  */
28915
28916 static void
28917 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28918 {
28919   tree proto, protorefs;
28920   cp_token *tok;
28921
28922   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
28923   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28924     {
28925       tok = cp_lexer_peek_token (parser->lexer);
28926       error_at (tok->location, "identifier expected after %<@protocol%>");
28927       cp_parser_consume_semicolon_at_end_of_statement (parser);
28928       return;
28929     }
28930
28931   /* See if we have a forward declaration or a definition.  */
28932   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28933
28934   /* Try a forward declaration first.  */
28935   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28936     {
28937       while (true)
28938         {
28939           tree id;
28940           
28941           id = cp_parser_identifier (parser);
28942           if (id == error_mark_node)
28943             break;
28944           
28945           objc_declare_protocol (id, attributes);
28946           
28947           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28948             cp_lexer_consume_token (parser->lexer);
28949           else
28950             break;
28951         }
28952       cp_parser_consume_semicolon_at_end_of_statement (parser);
28953     }
28954
28955   /* Ok, we got a full-fledged definition (or at least should).  */
28956   else
28957     {
28958       proto = cp_parser_identifier (parser);
28959       protorefs = cp_parser_objc_protocol_refs_opt (parser);
28960       objc_start_protocol (proto, protorefs, attributes);
28961       cp_parser_objc_method_prototype_list (parser);
28962     }
28963 }
28964
28965 /* Parse an Objective-C superclass or category.  */
28966
28967 static void
28968 cp_parser_objc_superclass_or_category (cp_parser *parser, 
28969                                        bool iface_p,
28970                                        tree *super,
28971                                        tree *categ, bool *is_class_extension)
28972 {
28973   cp_token *next = cp_lexer_peek_token (parser->lexer);
28974
28975   *super = *categ = NULL_TREE;
28976   *is_class_extension = false;
28977   if (next->type == CPP_COLON)
28978     {
28979       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
28980       *super = cp_parser_identifier (parser);
28981     }
28982   else if (next->type == CPP_OPEN_PAREN)
28983     {
28984       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
28985
28986       /* If there is no category name, and this is an @interface, we
28987          have a class extension.  */
28988       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28989         {
28990           *categ = NULL_TREE;
28991           *is_class_extension = true;
28992         }
28993       else
28994         *categ = cp_parser_identifier (parser);
28995
28996       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28997     }
28998 }
28999
29000 /* Parse an Objective-C class interface.  */
29001
29002 static void
29003 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29004 {
29005   tree name, super, categ, protos;
29006   bool is_class_extension;
29007
29008   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
29009   name = cp_parser_identifier (parser);
29010   if (name == error_mark_node)
29011     {
29012       /* It's hard to recover because even if valid @interface stuff
29013          is to follow, we can't compile it (or validate it) if we
29014          don't even know which class it refers to.  Let's assume this
29015          was a stray '@interface' token in the stream and skip it.
29016       */
29017       return;
29018     }
29019   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29020                                          &is_class_extension);
29021   protos = cp_parser_objc_protocol_refs_opt (parser);
29022
29023   /* We have either a class or a category on our hands.  */
29024   if (categ || is_class_extension)
29025     objc_start_category_interface (name, categ, protos, attributes);
29026   else
29027     {
29028       objc_start_class_interface (name, super, protos, attributes);
29029       /* Handle instance variable declarations, if any.  */
29030       cp_parser_objc_class_ivars (parser);
29031       objc_continue_interface ();
29032     }
29033
29034   cp_parser_objc_method_prototype_list (parser);
29035 }
29036
29037 /* Parse an Objective-C class implementation.  */
29038
29039 static void
29040 cp_parser_objc_class_implementation (cp_parser* parser)
29041 {
29042   tree name, super, categ;
29043   bool is_class_extension;
29044
29045   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
29046   name = cp_parser_identifier (parser);
29047   if (name == error_mark_node)
29048     {
29049       /* It's hard to recover because even if valid @implementation
29050          stuff is to follow, we can't compile it (or validate it) if
29051          we don't even know which class it refers to.  Let's assume
29052          this was a stray '@implementation' token in the stream and
29053          skip it.
29054       */
29055       return;
29056     }
29057   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29058                                          &is_class_extension);
29059
29060   /* We have either a class or a category on our hands.  */
29061   if (categ)
29062     objc_start_category_implementation (name, categ);
29063   else
29064     {
29065       objc_start_class_implementation (name, super);
29066       /* Handle instance variable declarations, if any.  */
29067       cp_parser_objc_class_ivars (parser);
29068       objc_continue_implementation ();
29069     }
29070
29071   cp_parser_objc_method_definition_list (parser);
29072 }
29073
29074 /* Consume the @end token and finish off the implementation.  */
29075
29076 static void
29077 cp_parser_objc_end_implementation (cp_parser* parser)
29078 {
29079   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
29080   objc_finish_implementation ();
29081 }
29082
29083 /* Parse an Objective-C declaration.  */
29084
29085 static void
29086 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29087 {
29088   /* Try to figure out what kind of declaration is present.  */
29089   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29090
29091   if (attributes)
29092     switch (kwd->keyword)
29093       {
29094         case RID_AT_ALIAS:
29095         case RID_AT_CLASS:
29096         case RID_AT_END:
29097           error_at (kwd->location, "attributes may not be specified before"
29098                     " the %<@%D%> Objective-C++ keyword",
29099                     kwd->u.value);
29100           attributes = NULL;
29101           break;
29102         case RID_AT_IMPLEMENTATION:
29103           warning_at (kwd->location, OPT_Wattributes,
29104                       "prefix attributes are ignored before %<@%D%>",
29105                       kwd->u.value);
29106           attributes = NULL;
29107         default:
29108           break;
29109       }
29110
29111   switch (kwd->keyword)
29112     {
29113     case RID_AT_ALIAS:
29114       cp_parser_objc_alias_declaration (parser);
29115       break;
29116     case RID_AT_CLASS:
29117       cp_parser_objc_class_declaration (parser);
29118       break;
29119     case RID_AT_PROTOCOL:
29120       cp_parser_objc_protocol_declaration (parser, attributes);
29121       break;
29122     case RID_AT_INTERFACE:
29123       cp_parser_objc_class_interface (parser, attributes);
29124       break;
29125     case RID_AT_IMPLEMENTATION:
29126       cp_parser_objc_class_implementation (parser);
29127       break;
29128     case RID_AT_END:
29129       cp_parser_objc_end_implementation (parser);
29130       break;
29131     default:
29132       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29133                 kwd->u.value);
29134       cp_parser_skip_to_end_of_block_or_statement (parser);
29135     }
29136 }
29137
29138 /* Parse an Objective-C try-catch-finally statement.
29139
29140    objc-try-catch-finally-stmt:
29141      @try compound-statement objc-catch-clause-seq [opt]
29142        objc-finally-clause [opt]
29143
29144    objc-catch-clause-seq:
29145      objc-catch-clause objc-catch-clause-seq [opt]
29146
29147    objc-catch-clause:
29148      @catch ( objc-exception-declaration ) compound-statement
29149
29150    objc-finally-clause:
29151      @finally compound-statement
29152
29153    objc-exception-declaration:
29154      parameter-declaration
29155      '...'
29156
29157    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29158
29159    Returns NULL_TREE.
29160
29161    PS: This function is identical to c_parser_objc_try_catch_finally_statement
29162    for C.  Keep them in sync.  */   
29163
29164 static tree
29165 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29166 {
29167   location_t location;
29168   tree stmt;
29169
29170   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29171   location = cp_lexer_peek_token (parser->lexer)->location;
29172   objc_maybe_warn_exceptions (location);
29173   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29174      node, lest it get absorbed into the surrounding block.  */
29175   stmt = push_stmt_list ();
29176   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29177   objc_begin_try_stmt (location, pop_stmt_list (stmt));
29178
29179   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29180     {
29181       cp_parameter_declarator *parm;
29182       tree parameter_declaration = error_mark_node;
29183       bool seen_open_paren = false;
29184
29185       cp_lexer_consume_token (parser->lexer);
29186       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29187         seen_open_paren = true;
29188       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29189         {
29190           /* We have "@catch (...)" (where the '...' are literally
29191              what is in the code).  Skip the '...'.
29192              parameter_declaration is set to NULL_TREE, and
29193              objc_being_catch_clauses() knows that that means
29194              '...'.  */
29195           cp_lexer_consume_token (parser->lexer);
29196           parameter_declaration = NULL_TREE;
29197         }
29198       else
29199         {
29200           /* We have "@catch (NSException *exception)" or something
29201              like that.  Parse the parameter declaration.  */
29202           parm = cp_parser_parameter_declaration (parser, false, NULL);
29203           if (parm == NULL)
29204             parameter_declaration = error_mark_node;
29205           else
29206             parameter_declaration = grokdeclarator (parm->declarator,
29207                                                     &parm->decl_specifiers,
29208                                                     PARM, /*initialized=*/0,
29209                                                     /*attrlist=*/NULL);
29210         }
29211       if (seen_open_paren)
29212         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29213       else
29214         {
29215           /* If there was no open parenthesis, we are recovering from
29216              an error, and we are trying to figure out what mistake
29217              the user has made.  */
29218
29219           /* If there is an immediate closing parenthesis, the user
29220              probably forgot the opening one (ie, they typed "@catch
29221              NSException *e)".  Parse the closing parenthesis and keep
29222              going.  */
29223           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29224             cp_lexer_consume_token (parser->lexer);
29225           
29226           /* If these is no immediate closing parenthesis, the user
29227              probably doesn't know that parenthesis are required at
29228              all (ie, they typed "@catch NSException *e").  So, just
29229              forget about the closing parenthesis and keep going.  */
29230         }
29231       objc_begin_catch_clause (parameter_declaration);
29232       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29233       objc_finish_catch_clause ();
29234     }
29235   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29236     {
29237       cp_lexer_consume_token (parser->lexer);
29238       location = cp_lexer_peek_token (parser->lexer)->location;
29239       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29240          node, lest it get absorbed into the surrounding block.  */
29241       stmt = push_stmt_list ();
29242       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29243       objc_build_finally_clause (location, pop_stmt_list (stmt));
29244     }
29245
29246   return objc_finish_try_stmt ();
29247 }
29248
29249 /* Parse an Objective-C synchronized statement.
29250
29251    objc-synchronized-stmt:
29252      @synchronized ( expression ) compound-statement
29253
29254    Returns NULL_TREE.  */
29255
29256 static tree
29257 cp_parser_objc_synchronized_statement (cp_parser *parser)
29258 {
29259   location_t location;
29260   tree lock, stmt;
29261
29262   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29263
29264   location = cp_lexer_peek_token (parser->lexer)->location;
29265   objc_maybe_warn_exceptions (location);
29266   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29267   lock = cp_parser_expression (parser);
29268   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29269
29270   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29271      node, lest it get absorbed into the surrounding block.  */
29272   stmt = push_stmt_list ();
29273   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29274
29275   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29276 }
29277
29278 /* Parse an Objective-C throw statement.
29279
29280    objc-throw-stmt:
29281      @throw assignment-expression [opt] ;
29282
29283    Returns a constructed '@throw' statement.  */
29284
29285 static tree
29286 cp_parser_objc_throw_statement (cp_parser *parser)
29287 {
29288   tree expr = NULL_TREE;
29289   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29290
29291   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29292
29293   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29294     expr = cp_parser_expression (parser);
29295
29296   cp_parser_consume_semicolon_at_end_of_statement (parser);
29297
29298   return objc_build_throw_stmt (loc, expr);
29299 }
29300
29301 /* Parse an Objective-C statement.  */
29302
29303 static tree
29304 cp_parser_objc_statement (cp_parser * parser)
29305 {
29306   /* Try to figure out what kind of declaration is present.  */
29307   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29308
29309   switch (kwd->keyword)
29310     {
29311     case RID_AT_TRY:
29312       return cp_parser_objc_try_catch_finally_statement (parser);
29313     case RID_AT_SYNCHRONIZED:
29314       return cp_parser_objc_synchronized_statement (parser);
29315     case RID_AT_THROW:
29316       return cp_parser_objc_throw_statement (parser);
29317     default:
29318       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29319                kwd->u.value);
29320       cp_parser_skip_to_end_of_block_or_statement (parser);
29321     }
29322
29323   return error_mark_node;
29324 }
29325
29326 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
29327    look ahead to see if an objc keyword follows the attributes.  This
29328    is to detect the use of prefix attributes on ObjC @interface and 
29329    @protocol.  */
29330
29331 static bool
29332 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29333 {
29334   cp_lexer_save_tokens (parser->lexer);
29335   *attrib = cp_parser_attributes_opt (parser);
29336   gcc_assert (*attrib);
29337   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29338     {
29339       cp_lexer_commit_tokens (parser->lexer);
29340       return true;
29341     }
29342   cp_lexer_rollback_tokens (parser->lexer);
29343   return false;  
29344 }
29345
29346 /* This routine is a minimal replacement for
29347    c_parser_struct_declaration () used when parsing the list of
29348    types/names or ObjC++ properties.  For example, when parsing the
29349    code
29350
29351    @property (readonly) int a, b, c;
29352
29353    this function is responsible for parsing "int a, int b, int c" and
29354    returning the declarations as CHAIN of DECLs.
29355
29356    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
29357    similar parsing.  */
29358 static tree
29359 cp_parser_objc_struct_declaration (cp_parser *parser)
29360 {
29361   tree decls = NULL_TREE;
29362   cp_decl_specifier_seq declspecs;
29363   int decl_class_or_enum_p;
29364   tree prefix_attributes;
29365
29366   cp_parser_decl_specifier_seq (parser,
29367                                 CP_PARSER_FLAGS_NONE,
29368                                 &declspecs,
29369                                 &decl_class_or_enum_p);
29370
29371   if (declspecs.type == error_mark_node)
29372     return error_mark_node;
29373
29374   /* auto, register, static, extern, mutable.  */
29375   if (declspecs.storage_class != sc_none)
29376     {
29377       cp_parser_error (parser, "invalid type for property");
29378       declspecs.storage_class = sc_none;
29379     }
29380   
29381   /* thread_local.  */
29382   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29383     {
29384       cp_parser_error (parser, "invalid type for property");
29385       declspecs.locations[ds_thread] = 0;
29386     }
29387   
29388   /* typedef.  */
29389   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29390     {
29391       cp_parser_error (parser, "invalid type for property");
29392       declspecs.locations[ds_typedef] = 0;
29393     }
29394
29395   prefix_attributes = declspecs.attributes;
29396   declspecs.attributes = NULL_TREE;
29397
29398   /* Keep going until we hit the `;' at the end of the declaration. */
29399   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29400     {
29401       tree attributes, first_attribute, decl;
29402       cp_declarator *declarator;
29403       cp_token *token;
29404
29405       /* Parse the declarator.  */
29406       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29407                                          NULL, NULL, false, false);
29408
29409       /* Look for attributes that apply to the ivar.  */
29410       attributes = cp_parser_attributes_opt (parser);
29411       /* Remember which attributes are prefix attributes and
29412          which are not.  */
29413       first_attribute = attributes;
29414       /* Combine the attributes.  */
29415       attributes = chainon (prefix_attributes, attributes);
29416       
29417       decl = grokfield (declarator, &declspecs,
29418                         NULL_TREE, /*init_const_expr_p=*/false,
29419                         NULL_TREE, attributes);
29420
29421       if (decl == error_mark_node || decl == NULL_TREE)
29422         return error_mark_node;
29423       
29424       /* Reset PREFIX_ATTRIBUTES.  */
29425       while (attributes && TREE_CHAIN (attributes) != first_attribute)
29426         attributes = TREE_CHAIN (attributes);
29427       if (attributes)
29428         TREE_CHAIN (attributes) = NULL_TREE;
29429
29430       DECL_CHAIN (decl) = decls;
29431       decls = decl;
29432
29433       token = cp_lexer_peek_token (parser->lexer);
29434       if (token->type == CPP_COMMA)
29435         {
29436           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
29437           continue;
29438         }
29439       else
29440         break;
29441     }
29442   return decls;
29443 }
29444
29445 /* Parse an Objective-C @property declaration.  The syntax is:
29446
29447    objc-property-declaration:
29448      '@property' objc-property-attributes[opt] struct-declaration ;
29449
29450    objc-property-attributes:
29451     '(' objc-property-attribute-list ')'
29452
29453    objc-property-attribute-list:
29454      objc-property-attribute
29455      objc-property-attribute-list, objc-property-attribute
29456
29457    objc-property-attribute
29458      'getter' = identifier
29459      'setter' = identifier
29460      'readonly'
29461      'readwrite'
29462      'assign'
29463      'retain'
29464      'copy'
29465      'nonatomic'
29466
29467   For example:
29468     @property NSString *name;
29469     @property (readonly) id object;
29470     @property (retain, nonatomic, getter=getTheName) id name;
29471     @property int a, b, c;
29472
29473    PS: This function is identical to
29474    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
29475 static void 
29476 cp_parser_objc_at_property_declaration (cp_parser *parser)
29477 {
29478   /* The following variables hold the attributes of the properties as
29479      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
29480      seen.  When we see an attribute, we set them to 'true' (if they
29481      are boolean properties) or to the identifier (if they have an
29482      argument, ie, for getter and setter).  Note that here we only
29483      parse the list of attributes, check the syntax and accumulate the
29484      attributes that we find.  objc_add_property_declaration() will
29485      then process the information.  */
29486   bool property_assign = false;
29487   bool property_copy = false;
29488   tree property_getter_ident = NULL_TREE;
29489   bool property_nonatomic = false;
29490   bool property_readonly = false;
29491   bool property_readwrite = false;
29492   bool property_retain = false;
29493   tree property_setter_ident = NULL_TREE;
29494
29495   /* 'properties' is the list of properties that we read.  Usually a
29496      single one, but maybe more (eg, in "@property int a, b, c;" there
29497      are three).  */
29498   tree properties;
29499   location_t loc;
29500
29501   loc = cp_lexer_peek_token (parser->lexer)->location;
29502
29503   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
29504
29505   /* Parse the optional attribute list...  */
29506   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29507     {
29508       /* Eat the '('.  */
29509       cp_lexer_consume_token (parser->lexer);
29510
29511       while (true)
29512         {
29513           bool syntax_error = false;
29514           cp_token *token = cp_lexer_peek_token (parser->lexer);
29515           enum rid keyword;
29516
29517           if (token->type != CPP_NAME)
29518             {
29519               cp_parser_error (parser, "expected identifier");
29520               break;
29521             }
29522           keyword = C_RID_CODE (token->u.value);
29523           cp_lexer_consume_token (parser->lexer);
29524           switch (keyword)
29525             {
29526             case RID_ASSIGN:    property_assign = true;    break;
29527             case RID_COPY:      property_copy = true;      break;
29528             case RID_NONATOMIC: property_nonatomic = true; break;
29529             case RID_READONLY:  property_readonly = true;  break;
29530             case RID_READWRITE: property_readwrite = true; break;
29531             case RID_RETAIN:    property_retain = true;    break;
29532
29533             case RID_GETTER:
29534             case RID_SETTER:
29535               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29536                 {
29537                   if (keyword == RID_GETTER)
29538                     cp_parser_error (parser,
29539                                      "missing %<=%> (after %<getter%> attribute)");
29540                   else
29541                     cp_parser_error (parser,
29542                                      "missing %<=%> (after %<setter%> attribute)");
29543                   syntax_error = true;
29544                   break;
29545                 }
29546               cp_lexer_consume_token (parser->lexer); /* eat the = */
29547               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29548                 {
29549                   cp_parser_error (parser, "expected identifier");
29550                   syntax_error = true;
29551                   break;
29552                 }
29553               if (keyword == RID_SETTER)
29554                 {
29555                   if (property_setter_ident != NULL_TREE)
29556                     {
29557                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29558                       cp_lexer_consume_token (parser->lexer);
29559                     }
29560                   else
29561                     property_setter_ident = cp_parser_objc_selector (parser);
29562                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29563                     cp_parser_error (parser, "setter name must terminate with %<:%>");
29564                   else
29565                     cp_lexer_consume_token (parser->lexer);
29566                 }
29567               else
29568                 {
29569                   if (property_getter_ident != NULL_TREE)
29570                     {
29571                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29572                       cp_lexer_consume_token (parser->lexer);
29573                     }
29574                   else
29575                     property_getter_ident = cp_parser_objc_selector (parser);
29576                 }
29577               break;
29578             default:
29579               cp_parser_error (parser, "unknown property attribute");
29580               syntax_error = true;
29581               break;
29582             }
29583
29584           if (syntax_error)
29585             break;
29586
29587           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29588             cp_lexer_consume_token (parser->lexer);
29589           else
29590             break;
29591         }
29592
29593       /* FIXME: "@property (setter, assign);" will generate a spurious
29594          "error: expected â€˜)’ before â€˜,’ token".  This is because
29595          cp_parser_require, unlike the C counterpart, will produce an
29596          error even if we are in error recovery.  */
29597       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29598         {
29599           cp_parser_skip_to_closing_parenthesis (parser,
29600                                                  /*recovering=*/true,
29601                                                  /*or_comma=*/false,
29602                                                  /*consume_paren=*/true);
29603         }
29604     }
29605
29606   /* ... and the property declaration(s).  */
29607   properties = cp_parser_objc_struct_declaration (parser);
29608
29609   if (properties == error_mark_node)
29610     {
29611       cp_parser_skip_to_end_of_statement (parser);
29612       /* If the next token is now a `;', consume it.  */
29613       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29614         cp_lexer_consume_token (parser->lexer);
29615       return;
29616     }
29617
29618   if (properties == NULL_TREE)
29619     cp_parser_error (parser, "expected identifier");
29620   else
29621     {
29622       /* Comma-separated properties are chained together in
29623          reverse order; add them one by one.  */
29624       properties = nreverse (properties);
29625       
29626       for (; properties; properties = TREE_CHAIN (properties))
29627         objc_add_property_declaration (loc, copy_node (properties),
29628                                        property_readonly, property_readwrite,
29629                                        property_assign, property_retain,
29630                                        property_copy, property_nonatomic,
29631                                        property_getter_ident, property_setter_ident);
29632     }
29633   
29634   cp_parser_consume_semicolon_at_end_of_statement (parser);
29635 }
29636
29637 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
29638
29639    objc-synthesize-declaration:
29640      @synthesize objc-synthesize-identifier-list ;
29641
29642    objc-synthesize-identifier-list:
29643      objc-synthesize-identifier
29644      objc-synthesize-identifier-list, objc-synthesize-identifier
29645
29646    objc-synthesize-identifier
29647      identifier
29648      identifier = identifier
29649
29650   For example:
29651     @synthesize MyProperty;
29652     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29653
29654   PS: This function is identical to c_parser_objc_at_synthesize_declaration
29655   for C.  Keep them in sync.
29656 */
29657 static void 
29658 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29659 {
29660   tree list = NULL_TREE;
29661   location_t loc;
29662   loc = cp_lexer_peek_token (parser->lexer)->location;
29663
29664   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
29665   while (true)
29666     {
29667       tree property, ivar;
29668       property = cp_parser_identifier (parser);
29669       if (property == error_mark_node)
29670         {
29671           cp_parser_consume_semicolon_at_end_of_statement (parser);
29672           return;
29673         }
29674       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29675         {
29676           cp_lexer_consume_token (parser->lexer);
29677           ivar = cp_parser_identifier (parser);
29678           if (ivar == error_mark_node)
29679             {
29680               cp_parser_consume_semicolon_at_end_of_statement (parser);
29681               return;
29682             }
29683         }
29684       else
29685         ivar = NULL_TREE;
29686       list = chainon (list, build_tree_list (ivar, property));
29687       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29688         cp_lexer_consume_token (parser->lexer);
29689       else
29690         break;
29691     }
29692   cp_parser_consume_semicolon_at_end_of_statement (parser);
29693   objc_add_synthesize_declaration (loc, list);
29694 }
29695
29696 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
29697
29698    objc-dynamic-declaration:
29699      @dynamic identifier-list ;
29700
29701    For example:
29702      @dynamic MyProperty;
29703      @dynamic MyProperty, AnotherProperty;
29704
29705   PS: This function is identical to c_parser_objc_at_dynamic_declaration
29706   for C.  Keep them in sync.
29707 */
29708 static void 
29709 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29710 {
29711   tree list = NULL_TREE;
29712   location_t loc;
29713   loc = cp_lexer_peek_token (parser->lexer)->location;
29714
29715   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
29716   while (true)
29717     {
29718       tree property;
29719       property = cp_parser_identifier (parser);
29720       if (property == error_mark_node)
29721         {
29722           cp_parser_consume_semicolon_at_end_of_statement (parser);
29723           return;
29724         }
29725       list = chainon (list, build_tree_list (NULL, property));
29726       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29727         cp_lexer_consume_token (parser->lexer);
29728       else
29729         break;
29730     }
29731   cp_parser_consume_semicolon_at_end_of_statement (parser);
29732   objc_add_dynamic_declaration (loc, list);
29733 }
29734
29735 \f
29736 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
29737
29738 /* Returns name of the next clause.
29739    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29740    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
29741    returned and the token is consumed.  */
29742
29743 static pragma_omp_clause
29744 cp_parser_omp_clause_name (cp_parser *parser)
29745 {
29746   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29747
29748   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29749     result = PRAGMA_OACC_CLAUSE_AUTO;
29750   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29751     result = PRAGMA_OMP_CLAUSE_IF;
29752   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29753     result = PRAGMA_OMP_CLAUSE_DEFAULT;
29754   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29755     result = PRAGMA_OACC_CLAUSE_DELETE;
29756   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29757     result = PRAGMA_OMP_CLAUSE_PRIVATE;
29758   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29759     result = PRAGMA_OMP_CLAUSE_FOR;
29760   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29761     {
29762       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29763       const char *p = IDENTIFIER_POINTER (id);
29764
29765       switch (p[0])
29766         {
29767         case 'a':
29768           if (!strcmp ("aligned", p))
29769             result = PRAGMA_OMP_CLAUSE_ALIGNED;
29770           else if (!strcmp ("async", p))
29771             result = PRAGMA_OACC_CLAUSE_ASYNC;
29772           break;
29773         case 'c':
29774           if (!strcmp ("collapse", p))
29775             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29776           else if (!strcmp ("copy", p))
29777             result = PRAGMA_OACC_CLAUSE_COPY;
29778           else if (!strcmp ("copyin", p))
29779             result = PRAGMA_OMP_CLAUSE_COPYIN;
29780           else if (!strcmp ("copyout", p))
29781             result = PRAGMA_OACC_CLAUSE_COPYOUT;
29782           else if (!strcmp ("copyprivate", p))
29783             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29784           else if (!strcmp ("create", p))
29785             result = PRAGMA_OACC_CLAUSE_CREATE;
29786           break;
29787         case 'd':
29788           if (!strcmp ("defaultmap", p))
29789             result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29790           else if (!strcmp ("depend", p))
29791             result = PRAGMA_OMP_CLAUSE_DEPEND;
29792           else if (!strcmp ("device", p))
29793             result = PRAGMA_OMP_CLAUSE_DEVICE;
29794           else if (!strcmp ("deviceptr", p))
29795             result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29796           else if (!strcmp ("device_resident", p))
29797             result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29798           else if (!strcmp ("dist_schedule", p))
29799             result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29800           break;
29801         case 'f':
29802           if (!strcmp ("final", p))
29803             result = PRAGMA_OMP_CLAUSE_FINAL;
29804           else if (!strcmp ("firstprivate", p))
29805             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29806           else if (!strcmp ("from", p))
29807             result = PRAGMA_OMP_CLAUSE_FROM;
29808           break;
29809         case 'g':
29810           if (!strcmp ("gang", p))
29811             result = PRAGMA_OACC_CLAUSE_GANG;
29812           else if (!strcmp ("grainsize", p))
29813             result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29814           break;
29815         case 'h':
29816           if (!strcmp ("hint", p))
29817             result = PRAGMA_OMP_CLAUSE_HINT;
29818           else if (!strcmp ("host", p))
29819             result = PRAGMA_OACC_CLAUSE_HOST;
29820           break;
29821         case 'i':
29822           if (!strcmp ("inbranch", p))
29823             result = PRAGMA_OMP_CLAUSE_INBRANCH;
29824           else if (!strcmp ("independent", p))
29825             result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29826           else if (!strcmp ("is_device_ptr", p))
29827             result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29828           break;
29829         case 'l':
29830           if (!strcmp ("lastprivate", p))
29831             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29832           else if (!strcmp ("linear", p))
29833             result = PRAGMA_OMP_CLAUSE_LINEAR;
29834           else if (!strcmp ("link", p))
29835             result = PRAGMA_OMP_CLAUSE_LINK;
29836           break;
29837         case 'm':
29838           if (!strcmp ("map", p))
29839             result = PRAGMA_OMP_CLAUSE_MAP;
29840           else if (!strcmp ("mergeable", p))
29841             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29842           else if (flag_cilkplus && !strcmp ("mask", p))
29843             result = PRAGMA_CILK_CLAUSE_MASK;
29844           break;
29845         case 'n':
29846           if (!strcmp ("nogroup", p))
29847             result = PRAGMA_OMP_CLAUSE_NOGROUP;
29848           else if (!strcmp ("notinbranch", p))
29849             result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29850           else if (!strcmp ("nowait", p))
29851             result = PRAGMA_OMP_CLAUSE_NOWAIT;
29852           else if (flag_cilkplus && !strcmp ("nomask", p))
29853             result = PRAGMA_CILK_CLAUSE_NOMASK;
29854           else if (!strcmp ("num_gangs", p))
29855             result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29856           else if (!strcmp ("num_tasks", p))
29857             result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29858           else if (!strcmp ("num_teams", p))
29859             result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29860           else if (!strcmp ("num_threads", p))
29861             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29862           else if (!strcmp ("num_workers", p))
29863             result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29864           break;
29865         case 'o':
29866           if (!strcmp ("ordered", p))
29867             result = PRAGMA_OMP_CLAUSE_ORDERED;
29868           break;
29869         case 'p':
29870           if (!strcmp ("parallel", p))
29871             result = PRAGMA_OMP_CLAUSE_PARALLEL;
29872           else if (!strcmp ("present", p))
29873             result = PRAGMA_OACC_CLAUSE_PRESENT;
29874           else if (!strcmp ("present_or_copy", p)
29875                    || !strcmp ("pcopy", p))
29876             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29877           else if (!strcmp ("present_or_copyin", p)
29878                    || !strcmp ("pcopyin", p))
29879             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29880           else if (!strcmp ("present_or_copyout", p)
29881                    || !strcmp ("pcopyout", p))
29882             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29883           else if (!strcmp ("present_or_create", p)
29884                    || !strcmp ("pcreate", p))
29885             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29886           else if (!strcmp ("priority", p))
29887             result = PRAGMA_OMP_CLAUSE_PRIORITY;
29888           else if (!strcmp ("proc_bind", p))
29889             result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29890           break;
29891         case 'r':
29892           if (!strcmp ("reduction", p))
29893             result = PRAGMA_OMP_CLAUSE_REDUCTION;
29894           break;
29895         case 's':
29896           if (!strcmp ("safelen", p))
29897             result = PRAGMA_OMP_CLAUSE_SAFELEN;
29898           else if (!strcmp ("schedule", p))
29899             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29900           else if (!strcmp ("sections", p))
29901             result = PRAGMA_OMP_CLAUSE_SECTIONS;
29902           else if (!strcmp ("self", p))
29903             result = PRAGMA_OACC_CLAUSE_SELF;
29904           else if (!strcmp ("seq", p))
29905             result = PRAGMA_OACC_CLAUSE_SEQ;
29906           else if (!strcmp ("shared", p))
29907             result = PRAGMA_OMP_CLAUSE_SHARED;
29908           else if (!strcmp ("simd", p))
29909             result = PRAGMA_OMP_CLAUSE_SIMD;
29910           else if (!strcmp ("simdlen", p))
29911             result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29912           break;
29913         case 't':
29914           if (!strcmp ("taskgroup", p))
29915             result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29916           else if (!strcmp ("thread_limit", p))
29917             result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29918           else if (!strcmp ("threads", p))
29919             result = PRAGMA_OMP_CLAUSE_THREADS;
29920           else if (!strcmp ("tile", p))
29921             result = PRAGMA_OACC_CLAUSE_TILE;
29922           else if (!strcmp ("to", p))
29923             result = PRAGMA_OMP_CLAUSE_TO;
29924           break;
29925         case 'u':
29926           if (!strcmp ("uniform", p))
29927             result = PRAGMA_OMP_CLAUSE_UNIFORM;
29928           else if (!strcmp ("untied", p))
29929             result = PRAGMA_OMP_CLAUSE_UNTIED;
29930           else if (!strcmp ("use_device", p))
29931             result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29932           else if (!strcmp ("use_device_ptr", p))
29933             result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29934           break;
29935         case 'v':
29936           if (!strcmp ("vector", p))
29937             result = PRAGMA_OACC_CLAUSE_VECTOR;
29938           else if (!strcmp ("vector_length", p))
29939             result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29940           else if (flag_cilkplus && !strcmp ("vectorlength", p))
29941             result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29942           break;
29943         case 'w':
29944           if (!strcmp ("wait", p))
29945             result = PRAGMA_OACC_CLAUSE_WAIT;
29946           else if (!strcmp ("worker", p))
29947             result = PRAGMA_OACC_CLAUSE_WORKER;
29948           break;
29949         }
29950     }
29951
29952   if (result != PRAGMA_OMP_CLAUSE_NONE)
29953     cp_lexer_consume_token (parser->lexer);
29954
29955   return result;
29956 }
29957
29958 /* Validate that a clause of the given type does not already exist.  */
29959
29960 static void
29961 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29962                            const char *name, location_t location)
29963 {
29964   tree c;
29965
29966   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29967     if (OMP_CLAUSE_CODE (c) == code)
29968       {
29969         error_at (location, "too many %qs clauses", name);
29970         break;
29971       }
29972 }
29973
29974 /* OpenMP 2.5:
29975    variable-list:
29976      identifier
29977      variable-list , identifier
29978
29979    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29980    colon).  An opening parenthesis will have been consumed by the caller.
29981
29982    If KIND is nonzero, create the appropriate node and install the decl
29983    in OMP_CLAUSE_DECL and add the node to the head of the list.
29984
29985    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29986    return the list created.
29987
29988    COLON can be NULL if only closing parenthesis should end the list,
29989    or pointer to bool which will receive false if the list is terminated
29990    by closing parenthesis or true if the list is terminated by colon.  */
29991
29992 static tree
29993 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29994                                 tree list, bool *colon)
29995 {
29996   cp_token *token;
29997   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29998   if (colon)
29999     {
30000       parser->colon_corrects_to_scope_p = false;
30001       *colon = false;
30002     }
30003   while (1)
30004     {
30005       tree name, decl;
30006
30007       token = cp_lexer_peek_token (parser->lexer);
30008       if (kind != 0
30009           && current_class_ptr
30010           && cp_parser_is_keyword (token, RID_THIS))
30011         {
30012           decl = finish_this_expr ();
30013           if (TREE_CODE (decl) == NON_LVALUE_EXPR
30014               || CONVERT_EXPR_P (decl))
30015             decl = TREE_OPERAND (decl, 0);
30016           cp_lexer_consume_token (parser->lexer);
30017         }
30018       else
30019         {
30020           name = cp_parser_id_expression (parser, /*template_p=*/false,
30021                                           /*check_dependency_p=*/true,
30022                                           /*template_p=*/NULL,
30023                                           /*declarator_p=*/false,
30024                                           /*optional_p=*/false);
30025           if (name == error_mark_node)
30026             goto skip_comma;
30027
30028           decl = cp_parser_lookup_name_simple (parser, name, token->location);
30029           if (decl == error_mark_node)
30030             cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30031                                          token->location);
30032         }
30033       if (decl == error_mark_node)
30034         ;
30035       else if (kind != 0)
30036         {
30037           switch (kind)
30038             {
30039             case OMP_CLAUSE__CACHE_:
30040               /* The OpenACC cache directive explicitly only allows "array
30041                  elements or subarrays".  */
30042               if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30043                 {
30044                   error_at (token->location, "expected %<[%>");
30045                   decl = error_mark_node;
30046                   break;
30047                 }
30048               /* FALLTHROUGH.  */
30049             case OMP_CLAUSE_MAP:
30050             case OMP_CLAUSE_FROM:
30051             case OMP_CLAUSE_TO:
30052               while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30053                 {
30054                   location_t loc
30055                     = cp_lexer_peek_token (parser->lexer)->location;
30056                   cp_id_kind idk = CP_ID_KIND_NONE;
30057                   cp_lexer_consume_token (parser->lexer);
30058                   decl = convert_from_reference (decl);
30059                   decl
30060                     = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30061                                                               decl, false,
30062                                                               &idk, loc);
30063                 }
30064               /* FALLTHROUGH.  */
30065             case OMP_CLAUSE_DEPEND:
30066             case OMP_CLAUSE_REDUCTION:
30067               while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30068                 {
30069                   tree low_bound = NULL_TREE, length = NULL_TREE;
30070
30071                   parser->colon_corrects_to_scope_p = false;
30072                   cp_lexer_consume_token (parser->lexer);
30073                   if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30074                     low_bound = cp_parser_expression (parser);
30075                   if (!colon)
30076                     parser->colon_corrects_to_scope_p
30077                       = saved_colon_corrects_to_scope_p;
30078                   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30079                     length = integer_one_node;
30080                   else
30081                     {
30082                       /* Look for `:'.  */
30083                       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30084                         goto skip_comma;
30085                       if (!cp_lexer_next_token_is (parser->lexer,
30086                                                    CPP_CLOSE_SQUARE))
30087                         length = cp_parser_expression (parser);
30088                     }
30089                   /* Look for the closing `]'.  */
30090                   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30091                                           RT_CLOSE_SQUARE))
30092                     goto skip_comma;
30093
30094                   decl = tree_cons (low_bound, length, decl);
30095                 }
30096               break;
30097             default:
30098               break;
30099             }
30100
30101           tree u = build_omp_clause (token->location, kind);
30102           OMP_CLAUSE_DECL (u) = decl;
30103           OMP_CLAUSE_CHAIN (u) = list;
30104           list = u;
30105         }
30106       else
30107         list = tree_cons (decl, NULL_TREE, list);
30108
30109     get_comma:
30110       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30111         break;
30112       cp_lexer_consume_token (parser->lexer);
30113     }
30114
30115   if (colon)
30116     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30117
30118   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30119     {
30120       *colon = true;
30121       cp_parser_require (parser, CPP_COLON, RT_COLON);
30122       return list;
30123     }
30124
30125   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30126     {
30127       int ending;
30128
30129       /* Try to resync to an unnested comma.  Copied from
30130          cp_parser_parenthesized_expression_list.  */
30131     skip_comma:
30132       if (colon)
30133         parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30134       ending = cp_parser_skip_to_closing_parenthesis (parser,
30135                                                       /*recovering=*/true,
30136                                                       /*or_comma=*/true,
30137                                                       /*consume_paren=*/true);
30138       if (ending < 0)
30139         goto get_comma;
30140     }
30141
30142   return list;
30143 }
30144
30145 /* Similarly, but expect leading and trailing parenthesis.  This is a very
30146    common case for omp clauses.  */
30147
30148 static tree
30149 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30150 {
30151   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30152     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30153   return list;
30154 }
30155
30156 /* OpenACC 2.0:
30157    copy ( variable-list )
30158    copyin ( variable-list )
30159    copyout ( variable-list )
30160    create ( variable-list )
30161    delete ( variable-list )
30162    present ( variable-list )
30163    present_or_copy ( variable-list )
30164      pcopy ( variable-list )
30165    present_or_copyin ( variable-list )
30166      pcopyin ( variable-list )
30167    present_or_copyout ( variable-list )
30168      pcopyout ( variable-list )
30169    present_or_create ( variable-list )
30170      pcreate ( variable-list ) */
30171
30172 static tree
30173 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30174                             tree list)
30175 {
30176   enum gomp_map_kind kind;
30177   switch (c_kind)
30178     {
30179     case PRAGMA_OACC_CLAUSE_COPY:
30180       kind = GOMP_MAP_FORCE_TOFROM;
30181       break;
30182     case PRAGMA_OACC_CLAUSE_COPYIN:
30183       kind = GOMP_MAP_FORCE_TO;
30184       break;
30185     case PRAGMA_OACC_CLAUSE_COPYOUT:
30186       kind = GOMP_MAP_FORCE_FROM;
30187       break;
30188     case PRAGMA_OACC_CLAUSE_CREATE:
30189       kind = GOMP_MAP_FORCE_ALLOC;
30190       break;
30191     case PRAGMA_OACC_CLAUSE_DELETE:
30192       kind = GOMP_MAP_DELETE;
30193       break;
30194     case PRAGMA_OACC_CLAUSE_DEVICE:
30195       kind = GOMP_MAP_FORCE_TO;
30196       break;
30197     case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30198       kind = GOMP_MAP_DEVICE_RESIDENT;
30199       break;
30200     case PRAGMA_OACC_CLAUSE_HOST:
30201     case PRAGMA_OACC_CLAUSE_SELF:
30202       kind = GOMP_MAP_FORCE_FROM;
30203       break;
30204     case PRAGMA_OACC_CLAUSE_LINK:
30205       kind = GOMP_MAP_LINK;
30206       break;
30207     case PRAGMA_OACC_CLAUSE_PRESENT:
30208       kind = GOMP_MAP_FORCE_PRESENT;
30209       break;
30210     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30211       kind = GOMP_MAP_TOFROM;
30212       break;
30213     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30214       kind = GOMP_MAP_TO;
30215       break;
30216     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30217       kind = GOMP_MAP_FROM;
30218       break;
30219     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30220       kind = GOMP_MAP_ALLOC;
30221       break;
30222     default:
30223       gcc_unreachable ();
30224     }
30225   tree nl, c;
30226   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30227
30228   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30229     OMP_CLAUSE_SET_MAP_KIND (c, kind);
30230
30231   return nl;
30232 }
30233
30234 /* OpenACC 2.0:
30235    deviceptr ( variable-list ) */
30236
30237 static tree
30238 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30239 {
30240   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30241   tree vars, t;
30242
30243   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30244      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30245      variable-list must only allow for pointer variables.  */
30246   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30247   for (t = vars; t; t = TREE_CHAIN (t))
30248     {
30249       tree v = TREE_PURPOSE (t);
30250       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30251       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30252       OMP_CLAUSE_DECL (u) = v;
30253       OMP_CLAUSE_CHAIN (u) = list;
30254       list = u;
30255     }
30256
30257   return list;
30258 }
30259
30260 /* OpenACC 2.0:
30261    auto
30262    independent
30263    nohost
30264    seq */
30265
30266 static tree
30267 cp_parser_oacc_simple_clause (cp_parser * /* parser  */,
30268                               enum omp_clause_code code,
30269                               tree list, location_t location)
30270 {
30271   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30272   tree c = build_omp_clause (location, code);
30273   OMP_CLAUSE_CHAIN (c) = list;
30274   return c;
30275 }
30276
30277  /* OpenACC:
30278    num_gangs ( expression )
30279    num_workers ( expression )
30280    vector_length ( expression )  */
30281
30282 static tree
30283 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30284                                   const char *str, tree list)
30285 {
30286   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30287
30288   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30289     return list;
30290
30291   tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30292
30293   if (t == error_mark_node
30294       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30295     {
30296       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30297                                              /*or_comma=*/false,
30298                                              /*consume_paren=*/true);
30299       return list;
30300     }
30301
30302   check_no_duplicate_clause (list, code, str, loc);
30303
30304   tree c = build_omp_clause (loc, code);
30305   OMP_CLAUSE_OPERAND (c, 0) = t;
30306   OMP_CLAUSE_CHAIN (c) = list;
30307   return c;
30308 }
30309
30310 /* OpenACC:
30311
30312     gang [( gang-arg-list )]
30313     worker [( [num:] int-expr )]
30314     vector [( [length:] int-expr )]
30315
30316   where gang-arg is one of:
30317
30318     [num:] int-expr
30319     static: size-expr
30320
30321   and size-expr may be:
30322
30323     *
30324     int-expr
30325 */
30326
30327 static tree
30328 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30329                              const char *str, tree list)
30330 {
30331   const char *id = "num";
30332   cp_lexer *lexer = parser->lexer;
30333   tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30334   location_t loc = cp_lexer_peek_token (lexer)->location;
30335
30336   if (kind == OMP_CLAUSE_VECTOR)
30337     id = "length";
30338
30339   if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30340     {
30341       cp_lexer_consume_token (lexer);
30342
30343       do
30344         {
30345           cp_token *next = cp_lexer_peek_token (lexer);
30346           int idx = 0;
30347
30348           /* Gang static argument.  */
30349           if (kind == OMP_CLAUSE_GANG
30350               && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30351             {
30352               cp_lexer_consume_token (lexer);
30353
30354               if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30355                 goto cleanup_error;
30356
30357               idx = 1;
30358               if (ops[idx] != NULL)
30359                 {
30360                   cp_parser_error (parser, "too many %<static%> arguments");
30361                   goto cleanup_error;
30362                 }
30363
30364               /* Check for the '*' argument.  */
30365               if (cp_lexer_next_token_is (lexer, CPP_MULT)
30366                   && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30367                       || cp_lexer_nth_token_is (parser->lexer, 2,
30368                                                 CPP_CLOSE_PAREN)))
30369                 {
30370                   cp_lexer_consume_token (lexer);
30371                   ops[idx] = integer_minus_one_node;
30372
30373                   if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30374                     {
30375                       cp_lexer_consume_token (lexer);
30376                       continue;
30377                     }
30378                   else break;
30379                 }
30380             }
30381           /* Worker num: argument and vector length: arguments.  */
30382           else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30383                    && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30384                    && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30385             {
30386               cp_lexer_consume_token (lexer);  /* id  */
30387               cp_lexer_consume_token (lexer);  /* ':'  */
30388             }
30389
30390           /* Now collect the actual argument.  */
30391           if (ops[idx] != NULL_TREE)
30392             {
30393               cp_parser_error (parser, "unexpected argument");
30394               goto cleanup_error;
30395             }
30396
30397           tree expr = cp_parser_assignment_expression (parser, NULL, false,
30398                                                        false);
30399           if (expr == error_mark_node)
30400             goto cleanup_error;
30401
30402           mark_exp_read (expr);
30403           ops[idx] = expr;
30404
30405           if (kind == OMP_CLAUSE_GANG
30406               && cp_lexer_next_token_is (lexer, CPP_COMMA))
30407             {
30408               cp_lexer_consume_token (lexer);
30409               continue;
30410             }
30411           break;
30412         }
30413       while (1);
30414
30415       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30416         goto cleanup_error;
30417     }
30418
30419   check_no_duplicate_clause (list, kind, str, loc);
30420
30421   c = build_omp_clause (loc, kind);
30422
30423   if (ops[1])
30424     OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30425
30426   OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30427   OMP_CLAUSE_CHAIN (c) = list;
30428
30429   return c;
30430
30431  cleanup_error:
30432   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30433   return list;
30434 }
30435
30436 /* OpenACC 2.0:
30437    tile ( size-expr-list ) */
30438
30439 static tree
30440 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30441 {
30442   tree c, expr = error_mark_node;
30443   tree tile = NULL_TREE;
30444
30445   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30446
30447   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30448     return list;
30449
30450   do
30451     {
30452       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30453           && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30454               || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30455         {
30456           cp_lexer_consume_token (parser->lexer);
30457           expr = integer_minus_one_node;
30458         }
30459       else
30460         expr = cp_parser_assignment_expression (parser, NULL, false, false);
30461
30462       if (expr == error_mark_node)
30463         return list;
30464
30465       tile = tree_cons (NULL_TREE, expr, tile);
30466
30467       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30468         cp_lexer_consume_token (parser->lexer);
30469     }
30470   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30471
30472   /* Consume the trailing ')'.  */
30473   cp_lexer_consume_token (parser->lexer);
30474
30475   c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30476   tile = nreverse (tile);
30477   OMP_CLAUSE_TILE_LIST (c) = tile;
30478   OMP_CLAUSE_CHAIN (c) = list;
30479   return c;
30480 }
30481
30482 /* OpenACC 2.0
30483    Parse wait clause or directive parameters.  */
30484
30485 static tree
30486 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30487 {
30488   vec<tree, va_gc> *args;
30489   tree t, args_tree;
30490
30491   args = cp_parser_parenthesized_expression_list (parser, non_attr,
30492                                                   /*cast_p=*/false,
30493                                                   /*allow_expansion_p=*/true,
30494                                                   /*non_constant_p=*/NULL);
30495
30496   if (args == NULL || args->length () == 0)
30497     {
30498       cp_parser_error (parser, "expected integer expression before ')'");
30499       if (args != NULL)
30500         release_tree_vector (args);
30501       return list;
30502     }
30503
30504   args_tree = build_tree_list_vec (args);
30505
30506   release_tree_vector (args);
30507
30508   for (t = args_tree; t; t = TREE_CHAIN (t))
30509     {
30510       tree targ = TREE_VALUE (t);
30511
30512       if (targ != error_mark_node)
30513         {
30514           if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30515             error ("%<wait%> expression must be integral");
30516           else
30517             {
30518               tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30519
30520               mark_rvalue_use (targ);
30521               OMP_CLAUSE_DECL (c) = targ;
30522               OMP_CLAUSE_CHAIN (c) = list;
30523               list = c;
30524             }
30525         }
30526     }
30527
30528   return list;
30529 }
30530
30531 /* OpenACC:
30532    wait ( int-expr-list ) */
30533
30534 static tree
30535 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30536 {
30537   location_t location = cp_lexer_peek_token (parser->lexer)->location;
30538
30539   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30540     return list;
30541
30542   list = cp_parser_oacc_wait_list (parser, location, list);
30543
30544   return list;
30545 }
30546
30547 /* OpenMP 3.0:
30548    collapse ( constant-expression ) */
30549
30550 static tree
30551 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30552 {
30553   tree c, num;
30554   location_t loc;
30555   HOST_WIDE_INT n;
30556
30557   loc = cp_lexer_peek_token (parser->lexer)->location;
30558   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30559     return list;
30560
30561   num = cp_parser_constant_expression (parser);
30562
30563   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30564     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30565                                            /*or_comma=*/false,
30566                                            /*consume_paren=*/true);
30567
30568   if (num == error_mark_node)
30569     return list;
30570   num = fold_non_dependent_expr (num);
30571   if (!tree_fits_shwi_p (num)
30572       || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30573       || (n = tree_to_shwi (num)) <= 0
30574       || (int) n != n)
30575     {
30576       error_at (loc, "collapse argument needs positive constant integer expression");
30577       return list;
30578     }
30579
30580   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30581   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30582   OMP_CLAUSE_CHAIN (c) = list;
30583   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30584
30585   return c;
30586 }
30587
30588 /* OpenMP 2.5:
30589    default ( shared | none )
30590
30591    OpenACC 2.0
30592    default (none) */
30593
30594 static tree
30595 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30596                               location_t location, bool is_oacc)
30597 {
30598   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30599   tree c;
30600
30601   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30602     return list;
30603   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30604     {
30605       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30606       const char *p = IDENTIFIER_POINTER (id);
30607
30608       switch (p[0])
30609         {
30610         case 'n':
30611           if (strcmp ("none", p) != 0)
30612             goto invalid_kind;
30613           kind = OMP_CLAUSE_DEFAULT_NONE;
30614           break;
30615
30616         case 's':
30617           if (strcmp ("shared", p) != 0 || is_oacc)
30618             goto invalid_kind;
30619           kind = OMP_CLAUSE_DEFAULT_SHARED;
30620           break;
30621
30622         default:
30623           goto invalid_kind;
30624         }
30625
30626       cp_lexer_consume_token (parser->lexer);
30627     }
30628   else
30629     {
30630     invalid_kind:
30631       if (is_oacc)
30632         cp_parser_error (parser, "expected %<none%>");
30633       else
30634         cp_parser_error (parser, "expected %<none%> or %<shared%>");
30635     }
30636
30637   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30638     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30639                                            /*or_comma=*/false,
30640                                            /*consume_paren=*/true);
30641
30642   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30643     return list;
30644
30645   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30646   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30647   OMP_CLAUSE_CHAIN (c) = list;
30648   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30649
30650   return c;
30651 }
30652
30653 /* OpenMP 3.1:
30654    final ( expression ) */
30655
30656 static tree
30657 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30658 {
30659   tree t, c;
30660
30661   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30662     return list;
30663
30664   t = cp_parser_condition (parser);
30665
30666   if (t == error_mark_node
30667       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30668     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30669                                            /*or_comma=*/false,
30670                                            /*consume_paren=*/true);
30671
30672   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30673
30674   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30675   OMP_CLAUSE_FINAL_EXPR (c) = t;
30676   OMP_CLAUSE_CHAIN (c) = list;
30677
30678   return c;
30679 }
30680
30681 /* OpenMP 2.5:
30682    if ( expression )
30683
30684    OpenMP 4.5:
30685    if ( directive-name-modifier : expression )
30686
30687    directive-name-modifier:
30688      parallel | task | taskloop | target data | target | target update
30689      | target enter data | target exit data  */
30690
30691 static tree
30692 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30693                          bool is_omp)
30694 {
30695   tree t, c;
30696   enum tree_code if_modifier = ERROR_MARK;
30697
30698   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30699     return list;
30700
30701   if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30702     {
30703       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30704       const char *p = IDENTIFIER_POINTER (id);
30705       int n = 2;
30706
30707       if (strcmp ("parallel", p) == 0)
30708         if_modifier = OMP_PARALLEL;
30709       else if (strcmp ("task", p) == 0)
30710         if_modifier = OMP_TASK;
30711       else if (strcmp ("taskloop", p) == 0)
30712         if_modifier = OMP_TASKLOOP;
30713       else if (strcmp ("target", p) == 0)
30714         {
30715           if_modifier = OMP_TARGET;
30716           if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30717             {
30718               id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30719               p = IDENTIFIER_POINTER (id);
30720               if (strcmp ("data", p) == 0)
30721                 if_modifier = OMP_TARGET_DATA;
30722               else if (strcmp ("update", p) == 0)
30723                 if_modifier = OMP_TARGET_UPDATE;
30724               else if (strcmp ("enter", p) == 0)
30725                 if_modifier = OMP_TARGET_ENTER_DATA;
30726               else if (strcmp ("exit", p) == 0)
30727                 if_modifier = OMP_TARGET_EXIT_DATA;
30728               if (if_modifier != OMP_TARGET)
30729                 n = 3;
30730               else
30731                 {
30732                   location_t loc
30733                     = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30734                   error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30735                                  "or %<exit%>");
30736                   if_modifier = ERROR_MARK;
30737                 }
30738               if (if_modifier == OMP_TARGET_ENTER_DATA
30739                   || if_modifier == OMP_TARGET_EXIT_DATA)
30740                 {
30741                   if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30742                     {
30743                       id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30744                       p = IDENTIFIER_POINTER (id);
30745                       if (strcmp ("data", p) == 0)
30746                         n = 4;
30747                     }
30748                   if (n != 4)
30749                     {
30750                       location_t loc
30751                         = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30752                       error_at (loc, "expected %<data%>");
30753                       if_modifier = ERROR_MARK;
30754                     }
30755                 }
30756             }
30757         }
30758       if (if_modifier != ERROR_MARK)
30759         {
30760           if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30761             {
30762               while (n-- > 0)
30763                 cp_lexer_consume_token (parser->lexer);
30764             }
30765           else
30766             {
30767               if (n > 2)
30768                 {
30769                   location_t loc
30770                     = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30771                   error_at (loc, "expected %<:%>");
30772                 }
30773               if_modifier = ERROR_MARK;
30774             }
30775         }
30776     }
30777
30778   t = cp_parser_condition (parser);
30779
30780   if (t == error_mark_node
30781       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30782     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30783                                            /*or_comma=*/false,
30784                                            /*consume_paren=*/true);
30785
30786   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30787     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30788       {
30789         if (if_modifier != ERROR_MARK
30790             && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30791           {
30792             const char *p = NULL;
30793             switch (if_modifier)
30794               {
30795               case OMP_PARALLEL: p = "parallel"; break;
30796               case OMP_TASK: p = "task"; break;
30797               case OMP_TASKLOOP: p = "taskloop"; break;
30798               case OMP_TARGET_DATA: p = "target data"; break;
30799               case OMP_TARGET: p = "target"; break;
30800               case OMP_TARGET_UPDATE: p = "target update"; break;
30801               case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30802               case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30803               default: gcc_unreachable ();
30804               }
30805             error_at (location, "too many %<if%> clauses with %qs modifier",
30806                       p);
30807             return list;
30808           }
30809         else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30810           {
30811             if (!is_omp)
30812               error_at (location, "too many %<if%> clauses");
30813             else
30814               error_at (location, "too many %<if%> clauses without modifier");
30815             return list;
30816           }
30817         else if (if_modifier == ERROR_MARK
30818                  || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30819           {
30820             error_at (location, "if any %<if%> clause has modifier, then all "
30821                                 "%<if%> clauses have to use modifier");
30822             return list;
30823           }
30824       }
30825
30826   c = build_omp_clause (location, OMP_CLAUSE_IF);
30827   OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30828   OMP_CLAUSE_IF_EXPR (c) = t;
30829   OMP_CLAUSE_CHAIN (c) = list;
30830
30831   return c;
30832 }
30833
30834 /* OpenMP 3.1:
30835    mergeable */
30836
30837 static tree
30838 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30839                                 tree list, location_t location)
30840 {
30841   tree c;
30842
30843   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30844                              location);
30845
30846   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30847   OMP_CLAUSE_CHAIN (c) = list;
30848   return c;
30849 }
30850
30851 /* OpenMP 2.5:
30852    nowait */
30853
30854 static tree
30855 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30856                              tree list, location_t location)
30857 {
30858   tree c;
30859
30860   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30861
30862   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30863   OMP_CLAUSE_CHAIN (c) = list;
30864   return c;
30865 }
30866
30867 /* OpenMP 2.5:
30868    num_threads ( expression ) */
30869
30870 static tree
30871 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30872                                   location_t location)
30873 {
30874   tree t, c;
30875
30876   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30877     return list;
30878
30879   t = cp_parser_expression (parser);
30880
30881   if (t == error_mark_node
30882       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30883     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30884                                            /*or_comma=*/false,
30885                                            /*consume_paren=*/true);
30886
30887   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30888                              "num_threads", location);
30889
30890   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30891   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30892   OMP_CLAUSE_CHAIN (c) = list;
30893
30894   return c;
30895 }
30896
30897 /* OpenMP 4.5:
30898    num_tasks ( expression ) */
30899
30900 static tree
30901 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30902                                 location_t location)
30903 {
30904   tree t, c;
30905
30906   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30907     return list;
30908
30909   t = cp_parser_expression (parser);
30910
30911   if (t == error_mark_node
30912       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30913     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30914                                            /*or_comma=*/false,
30915                                            /*consume_paren=*/true);
30916
30917   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30918                              "num_tasks", location);
30919
30920   c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30921   OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30922   OMP_CLAUSE_CHAIN (c) = list;
30923
30924   return c;
30925 }
30926
30927 /* OpenMP 4.5:
30928    grainsize ( expression ) */
30929
30930 static tree
30931 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30932                                 location_t location)
30933 {
30934   tree t, c;
30935
30936   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30937     return list;
30938
30939   t = cp_parser_expression (parser);
30940
30941   if (t == error_mark_node
30942       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30943     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30944                                            /*or_comma=*/false,
30945                                            /*consume_paren=*/true);
30946
30947   check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30948                              "grainsize", location);
30949
30950   c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30951   OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30952   OMP_CLAUSE_CHAIN (c) = list;
30953
30954   return c;
30955 }
30956
30957 /* OpenMP 4.5:
30958    priority ( expression ) */
30959
30960 static tree
30961 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30962                                location_t location)
30963 {
30964   tree t, c;
30965
30966   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30967     return list;
30968
30969   t = cp_parser_expression (parser);
30970
30971   if (t == error_mark_node
30972       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30973     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30974                                            /*or_comma=*/false,
30975                                            /*consume_paren=*/true);
30976
30977   check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30978                              "priority", location);
30979
30980   c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30981   OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30982   OMP_CLAUSE_CHAIN (c) = list;
30983
30984   return c;
30985 }
30986
30987 /* OpenMP 4.5:
30988    hint ( expression ) */
30989
30990 static tree
30991 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30992                            location_t location)
30993 {
30994   tree t, c;
30995
30996   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30997     return list;
30998
30999   t = cp_parser_expression (parser);
31000
31001   if (t == error_mark_node
31002       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31003     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31004                                            /*or_comma=*/false,
31005                                            /*consume_paren=*/true);
31006
31007   check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31008
31009   c = build_omp_clause (location, OMP_CLAUSE_HINT);
31010   OMP_CLAUSE_HINT_EXPR (c) = t;
31011   OMP_CLAUSE_CHAIN (c) = list;
31012
31013   return c;
31014 }
31015
31016 /* OpenMP 4.5:
31017    defaultmap ( tofrom : scalar ) */
31018
31019 static tree
31020 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31021                                  location_t location)
31022 {
31023   tree c, id;
31024   const char *p;
31025
31026   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31027     return list;
31028
31029   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31030     {
31031       cp_parser_error (parser, "expected %<tofrom%>");
31032       goto out_err;
31033     }
31034   id = cp_lexer_peek_token (parser->lexer)->u.value;
31035   p = IDENTIFIER_POINTER (id);
31036   if (strcmp (p, "tofrom") != 0)
31037     {
31038       cp_parser_error (parser, "expected %<tofrom%>");
31039       goto out_err;
31040     }
31041   cp_lexer_consume_token (parser->lexer);
31042   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31043     goto out_err;
31044
31045   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31046     {
31047       cp_parser_error (parser, "expected %<scalar%>");
31048       goto out_err;
31049     }
31050   id = cp_lexer_peek_token (parser->lexer)->u.value;
31051   p = IDENTIFIER_POINTER (id);
31052   if (strcmp (p, "scalar") != 0)
31053     {
31054       cp_parser_error (parser, "expected %<scalar%>");
31055       goto out_err;
31056     }
31057   cp_lexer_consume_token (parser->lexer);
31058   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31059     goto out_err;
31060
31061   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31062                              location);
31063
31064   c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31065   OMP_CLAUSE_CHAIN (c) = list;
31066   return c;
31067
31068  out_err:
31069   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31070                                          /*or_comma=*/false,
31071                                          /*consume_paren=*/true);
31072   return list;
31073 }
31074
31075 /* OpenMP 2.5:
31076    ordered
31077
31078    OpenMP 4.5:
31079    ordered ( constant-expression ) */
31080
31081 static tree
31082 cp_parser_omp_clause_ordered (cp_parser *parser,
31083                               tree list, location_t location)
31084 {
31085   tree c, num = NULL_TREE;
31086   HOST_WIDE_INT n;
31087
31088   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31089                              "ordered", location);
31090
31091   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31092     {
31093       cp_lexer_consume_token (parser->lexer);
31094
31095       num = cp_parser_constant_expression (parser);
31096
31097       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31098         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31099                                                /*or_comma=*/false,
31100                                                /*consume_paren=*/true);
31101
31102       if (num == error_mark_node)
31103         return list;
31104       num = fold_non_dependent_expr (num);
31105       if (!tree_fits_shwi_p (num)
31106           || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31107           || (n = tree_to_shwi (num)) <= 0
31108           || (int) n != n)
31109         {
31110           error_at (location,
31111                     "ordered argument needs positive constant integer "
31112                     "expression");
31113           return list;
31114         }
31115     }
31116
31117   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31118   OMP_CLAUSE_ORDERED_EXPR (c) = num;
31119   OMP_CLAUSE_CHAIN (c) = list;
31120   return c;
31121 }
31122
31123 /* OpenMP 2.5:
31124    reduction ( reduction-operator : variable-list )
31125
31126    reduction-operator:
31127      One of: + * - & ^ | && ||
31128
31129    OpenMP 3.1:
31130
31131    reduction-operator:
31132      One of: + * - & ^ | && || min max
31133
31134    OpenMP 4.0:
31135
31136    reduction-operator:
31137      One of: + * - & ^ | && ||
31138      id-expression  */
31139
31140 static tree
31141 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31142 {
31143   enum tree_code code = ERROR_MARK;
31144   tree nlist, c, id = NULL_TREE;
31145
31146   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31147     return list;
31148
31149   switch (cp_lexer_peek_token (parser->lexer)->type)
31150     {
31151     case CPP_PLUS: code = PLUS_EXPR; break;
31152     case CPP_MULT: code = MULT_EXPR; break;
31153     case CPP_MINUS: code = MINUS_EXPR; break;
31154     case CPP_AND: code = BIT_AND_EXPR; break;
31155     case CPP_XOR: code = BIT_XOR_EXPR; break;
31156     case CPP_OR: code = BIT_IOR_EXPR; break;
31157     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31158     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31159     default: break;
31160     }
31161
31162   if (code != ERROR_MARK)
31163     cp_lexer_consume_token (parser->lexer);
31164   else
31165     {
31166       bool saved_colon_corrects_to_scope_p;
31167       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31168       parser->colon_corrects_to_scope_p = false;
31169       id = cp_parser_id_expression (parser, /*template_p=*/false,
31170                                     /*check_dependency_p=*/true,
31171                                     /*template_p=*/NULL,
31172                                     /*declarator_p=*/false,
31173                                     /*optional_p=*/false);
31174       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31175       if (identifier_p (id))
31176         {
31177           const char *p = IDENTIFIER_POINTER (id);
31178
31179           if (strcmp (p, "min") == 0)
31180             code = MIN_EXPR;
31181           else if (strcmp (p, "max") == 0)
31182             code = MAX_EXPR;
31183           else if (id == ansi_opname (PLUS_EXPR))
31184             code = PLUS_EXPR;
31185           else if (id == ansi_opname (MULT_EXPR))
31186             code = MULT_EXPR;
31187           else if (id == ansi_opname (MINUS_EXPR))
31188             code = MINUS_EXPR;
31189           else if (id == ansi_opname (BIT_AND_EXPR))
31190             code = BIT_AND_EXPR;
31191           else if (id == ansi_opname (BIT_IOR_EXPR))
31192             code = BIT_IOR_EXPR;
31193           else if (id == ansi_opname (BIT_XOR_EXPR))
31194             code = BIT_XOR_EXPR;
31195           else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31196             code = TRUTH_ANDIF_EXPR;
31197           else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31198             code = TRUTH_ORIF_EXPR;
31199           id = omp_reduction_id (code, id, NULL_TREE);
31200           tree scope = parser->scope;
31201           if (scope)
31202             id = build_qualified_name (NULL_TREE, scope, id, false);
31203           parser->scope = NULL_TREE;
31204           parser->qualifying_scope = NULL_TREE;
31205           parser->object_scope = NULL_TREE;
31206         }
31207       else
31208         {
31209           error ("invalid reduction-identifier");
31210          resync_fail:
31211           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31212                                                  /*or_comma=*/false,
31213                                                  /*consume_paren=*/true);
31214           return list;
31215         }
31216     }
31217
31218   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31219     goto resync_fail;
31220
31221   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31222                                           NULL);
31223   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31224     {
31225       OMP_CLAUSE_REDUCTION_CODE (c) = code;
31226       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31227     }
31228
31229   return nlist;
31230 }
31231
31232 /* OpenMP 2.5:
31233    schedule ( schedule-kind )
31234    schedule ( schedule-kind , expression )
31235
31236    schedule-kind:
31237      static | dynamic | guided | runtime | auto
31238
31239    OpenMP 4.5:
31240    schedule ( schedule-modifier : schedule-kind )
31241    schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31242
31243    schedule-modifier:
31244      simd
31245      monotonic
31246      nonmonotonic  */
31247
31248 static tree
31249 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31250 {
31251   tree c, t;
31252   int modifiers = 0, nmodifiers = 0;
31253
31254   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31255     return list;
31256
31257   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31258
31259   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31260     {
31261       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31262       const char *p = IDENTIFIER_POINTER (id);
31263       if (strcmp ("simd", p) == 0)
31264         OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31265       else if (strcmp ("monotonic", p) == 0)
31266         modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31267       else if (strcmp ("nonmonotonic", p) == 0)
31268         modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31269       else
31270         break;
31271       cp_lexer_consume_token (parser->lexer);
31272       if (nmodifiers++ == 0
31273           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31274         cp_lexer_consume_token (parser->lexer);
31275       else
31276         {
31277           cp_parser_require (parser, CPP_COLON, RT_COLON);
31278           break;
31279         }
31280     }
31281
31282   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31283     {
31284       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31285       const char *p = IDENTIFIER_POINTER (id);
31286
31287       switch (p[0])
31288         {
31289         case 'd':
31290           if (strcmp ("dynamic", p) != 0)
31291             goto invalid_kind;
31292           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31293           break;
31294
31295         case 'g':
31296           if (strcmp ("guided", p) != 0)
31297             goto invalid_kind;
31298           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31299           break;
31300
31301         case 'r':
31302           if (strcmp ("runtime", p) != 0)
31303             goto invalid_kind;
31304           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31305           break;
31306
31307         default:
31308           goto invalid_kind;
31309         }
31310     }
31311   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31312     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31313   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31314     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31315   else
31316     goto invalid_kind;
31317   cp_lexer_consume_token (parser->lexer);
31318
31319   if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31320                     | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31321       == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31322           | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31323     {
31324       error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31325                           "specified");
31326       modifiers = 0;
31327     }
31328
31329   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31330     {
31331       cp_token *token;
31332       cp_lexer_consume_token (parser->lexer);
31333
31334       token = cp_lexer_peek_token (parser->lexer);
31335       t = cp_parser_assignment_expression (parser);
31336
31337       if (t == error_mark_node)
31338         goto resync_fail;
31339       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31340         error_at (token->location, "schedule %<runtime%> does not take "
31341                   "a %<chunk_size%> parameter");
31342       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31343         error_at (token->location, "schedule %<auto%> does not take "
31344                   "a %<chunk_size%> parameter");
31345       else
31346         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31347
31348       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31349         goto resync_fail;
31350     }
31351   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31352     goto resync_fail;
31353
31354   OMP_CLAUSE_SCHEDULE_KIND (c)
31355     = (enum omp_clause_schedule_kind)
31356       (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31357
31358   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31359   OMP_CLAUSE_CHAIN (c) = list;
31360   return c;
31361
31362  invalid_kind:
31363   cp_parser_error (parser, "invalid schedule kind");
31364  resync_fail:
31365   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31366                                          /*or_comma=*/false,
31367                                          /*consume_paren=*/true);
31368   return list;
31369 }
31370
31371 /* OpenMP 3.0:
31372    untied */
31373
31374 static tree
31375 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31376                              tree list, location_t location)
31377 {
31378   tree c;
31379
31380   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31381
31382   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31383   OMP_CLAUSE_CHAIN (c) = list;
31384   return c;
31385 }
31386
31387 /* OpenMP 4.0:
31388    inbranch
31389    notinbranch */
31390
31391 static tree
31392 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31393                              tree list, location_t location)
31394 {
31395   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31396   tree c = build_omp_clause (location, code);
31397   OMP_CLAUSE_CHAIN (c) = list;
31398   return c;
31399 }
31400
31401 /* OpenMP 4.0:
31402    parallel
31403    for
31404    sections
31405    taskgroup */
31406
31407 static tree
31408 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31409                                  enum omp_clause_code code,
31410                                  tree list, location_t location)
31411 {
31412   tree c = build_omp_clause (location, code);
31413   OMP_CLAUSE_CHAIN (c) = list;
31414   return c;
31415 }
31416
31417 /* OpenMP 4.5:
31418    nogroup */
31419
31420 static tree
31421 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31422                               tree list, location_t location)
31423 {
31424   check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31425   tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31426   OMP_CLAUSE_CHAIN (c) = list;
31427   return c;
31428 }
31429
31430 /* OpenMP 4.5:
31431    simd
31432    threads */
31433
31434 static tree
31435 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31436                                   enum omp_clause_code code,
31437                                   tree list, location_t location)
31438 {
31439   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31440   tree c = build_omp_clause (location, code);
31441   OMP_CLAUSE_CHAIN (c) = list;
31442   return c;
31443 }
31444
31445 /* OpenMP 4.0:
31446    num_teams ( expression ) */
31447
31448 static tree
31449 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31450                                 location_t location)
31451 {
31452   tree t, c;
31453
31454   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31455     return list;
31456
31457   t = cp_parser_expression (parser);
31458
31459   if (t == error_mark_node
31460       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31461     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31462                                            /*or_comma=*/false,
31463                                            /*consume_paren=*/true);
31464
31465   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31466                              "num_teams", location);
31467
31468   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31469   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31470   OMP_CLAUSE_CHAIN (c) = list;
31471
31472   return c;
31473 }
31474
31475 /* OpenMP 4.0:
31476    thread_limit ( expression ) */
31477
31478 static tree
31479 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31480                                    location_t location)
31481 {
31482   tree t, c;
31483
31484   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31485     return list;
31486
31487   t = cp_parser_expression (parser);
31488
31489   if (t == error_mark_node
31490       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31491     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31492                                            /*or_comma=*/false,
31493                                            /*consume_paren=*/true);
31494
31495   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31496                              "thread_limit", location);
31497
31498   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31499   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31500   OMP_CLAUSE_CHAIN (c) = list;
31501
31502   return c;
31503 }
31504
31505 /* OpenMP 4.0:
31506    aligned ( variable-list )
31507    aligned ( variable-list : constant-expression )  */
31508
31509 static tree
31510 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31511 {
31512   tree nlist, c, alignment = NULL_TREE;
31513   bool colon;
31514
31515   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31516     return list;
31517
31518   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31519                                           &colon);
31520
31521   if (colon)
31522     {
31523       alignment = cp_parser_constant_expression (parser);
31524
31525       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31526         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31527                                                /*or_comma=*/false,
31528                                                /*consume_paren=*/true);
31529
31530       if (alignment == error_mark_node)
31531         alignment = NULL_TREE;
31532     }
31533
31534   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31535     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31536
31537   return nlist;
31538 }
31539
31540 /* OpenMP 4.0:
31541    linear ( variable-list )
31542    linear ( variable-list : expression )
31543
31544    OpenMP 4.5:
31545    linear ( modifier ( variable-list ) )
31546    linear ( modifier ( variable-list ) : expression ) */
31547
31548 static tree
31549 cp_parser_omp_clause_linear (cp_parser *parser, tree list, 
31550                              bool is_cilk_simd_fn, bool declare_simd)
31551 {
31552   tree nlist, c, step = integer_one_node;
31553   bool colon;
31554   enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31555
31556   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31557     return list;
31558
31559   if (!is_cilk_simd_fn
31560       && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31561     {
31562       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31563       const char *p = IDENTIFIER_POINTER (id);
31564
31565       if (strcmp ("ref", p) == 0)
31566         kind = OMP_CLAUSE_LINEAR_REF;
31567       else if (strcmp ("val", p) == 0)
31568         kind = OMP_CLAUSE_LINEAR_VAL;
31569       else if (strcmp ("uval", p) == 0)
31570         kind = OMP_CLAUSE_LINEAR_UVAL;
31571       if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31572         cp_lexer_consume_token (parser->lexer);
31573       else
31574         kind = OMP_CLAUSE_LINEAR_DEFAULT;
31575     }
31576
31577   if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31578     nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31579                                             &colon);
31580   else
31581     {
31582       nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31583       colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31584       if (colon)
31585         cp_parser_require (parser, CPP_COLON, RT_COLON);
31586       else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31587         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31588                                                /*or_comma=*/false,
31589                                                /*consume_paren=*/true);
31590     }
31591
31592   if (colon)
31593     {
31594       step = NULL_TREE;
31595       if (declare_simd
31596           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31597           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31598         {
31599           cp_token *token = cp_lexer_peek_token (parser->lexer);
31600           cp_parser_parse_tentatively (parser);
31601           step = cp_parser_id_expression (parser, /*template_p=*/false,
31602                                           /*check_dependency_p=*/true,
31603                                           /*template_p=*/NULL,
31604                                           /*declarator_p=*/false,
31605                                           /*optional_p=*/false);
31606           if (step != error_mark_node)
31607             step = cp_parser_lookup_name_simple (parser, step, token->location);
31608           if (step == error_mark_node)
31609             {
31610               step = NULL_TREE;
31611               cp_parser_abort_tentative_parse (parser);
31612             }
31613           else if (!cp_parser_parse_definitely (parser))
31614             step = NULL_TREE;
31615         }
31616       if (!step)
31617         step = cp_parser_expression (parser);
31618
31619       if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31620         {
31621           sorry ("using parameters for %<linear%> step is not supported yet");
31622           step = integer_one_node;
31623         }
31624       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31625         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31626                                                /*or_comma=*/false,
31627                                                /*consume_paren=*/true);
31628
31629       if (step == error_mark_node)
31630         return list;
31631     }
31632
31633   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31634     {
31635       OMP_CLAUSE_LINEAR_STEP (c) = step;
31636       OMP_CLAUSE_LINEAR_KIND (c) = kind;
31637     }
31638
31639   return nlist;
31640 }
31641
31642 /* OpenMP 4.0:
31643    safelen ( constant-expression )  */
31644
31645 static tree
31646 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31647                               location_t location)
31648 {
31649   tree t, c;
31650
31651   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31652     return list;
31653
31654   t = cp_parser_constant_expression (parser);
31655
31656   if (t == error_mark_node
31657       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31658     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31659                                            /*or_comma=*/false,
31660                                            /*consume_paren=*/true);
31661
31662   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31663
31664   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31665   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31666   OMP_CLAUSE_CHAIN (c) = list;
31667
31668   return c;
31669 }
31670
31671 /* OpenMP 4.0:
31672    simdlen ( constant-expression )  */
31673
31674 static tree
31675 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31676                               location_t location)
31677 {
31678   tree t, c;
31679
31680   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31681     return list;
31682
31683   t = cp_parser_constant_expression (parser);
31684
31685   if (t == error_mark_node
31686       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31687     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31688                                            /*or_comma=*/false,
31689                                            /*consume_paren=*/true);
31690
31691   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31692
31693   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31694   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31695   OMP_CLAUSE_CHAIN (c) = list;
31696
31697   return c;
31698 }
31699
31700 /* OpenMP 4.5:
31701    vec:
31702      identifier [+/- integer]
31703      vec , identifier [+/- integer]
31704 */
31705
31706 static tree
31707 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31708                                   tree list)
31709 {
31710   tree vec = NULL;
31711
31712   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31713     {
31714       cp_parser_error (parser, "expected identifier");
31715       return list;
31716     }
31717
31718   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31719     {
31720       location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31721       tree t, identifier = cp_parser_identifier (parser);
31722       tree addend = NULL;
31723
31724       if (identifier == error_mark_node)
31725         t = error_mark_node;
31726       else
31727         {
31728           t = cp_parser_lookup_name_simple
31729                 (parser, identifier,
31730                  cp_lexer_peek_token (parser->lexer)->location);
31731           if (t == error_mark_node)
31732             cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31733                                          id_loc);
31734         }
31735
31736       bool neg = false;
31737       if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31738         neg = true;
31739       else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31740         {
31741           addend = integer_zero_node;
31742           goto add_to_vector;
31743         }
31744       cp_lexer_consume_token (parser->lexer);
31745
31746       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31747         {
31748           cp_parser_error (parser, "expected integer");
31749           return list;
31750         }
31751
31752       addend = cp_lexer_peek_token (parser->lexer)->u.value;
31753       if (TREE_CODE (addend) != INTEGER_CST)
31754         {
31755           cp_parser_error (parser, "expected integer");
31756           return list;
31757         }
31758       cp_lexer_consume_token (parser->lexer);
31759
31760     add_to_vector:
31761       if (t != error_mark_node)
31762         {
31763           vec = tree_cons (addend, t, vec);
31764           if (neg)
31765             OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31766         }
31767
31768       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31769         break;
31770
31771       cp_lexer_consume_token (parser->lexer);
31772     }
31773
31774   if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31775     {
31776       tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31777       OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31778       OMP_CLAUSE_DECL (u) = nreverse (vec);
31779       OMP_CLAUSE_CHAIN (u) = list;
31780       return u;
31781     }
31782   return list;
31783 }
31784
31785 /* OpenMP 4.0:
31786    depend ( depend-kind : variable-list )
31787
31788    depend-kind:
31789      in | out | inout
31790
31791    OpenMP 4.5:
31792    depend ( source )
31793
31794    depend ( sink : vec ) */
31795
31796 static tree
31797 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31798 {
31799   tree nlist, c;
31800   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31801
31802   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31803     return list;
31804
31805   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31806     {
31807       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31808       const char *p = IDENTIFIER_POINTER (id);
31809
31810       if (strcmp ("in", p) == 0)
31811         kind = OMP_CLAUSE_DEPEND_IN;
31812       else if (strcmp ("inout", p) == 0)
31813         kind = OMP_CLAUSE_DEPEND_INOUT;
31814       else if (strcmp ("out", p) == 0)
31815         kind = OMP_CLAUSE_DEPEND_OUT;
31816       else if (strcmp ("source", p) == 0)
31817         kind = OMP_CLAUSE_DEPEND_SOURCE;
31818       else if (strcmp ("sink", p) == 0)
31819         kind = OMP_CLAUSE_DEPEND_SINK;
31820       else
31821         goto invalid_kind;
31822     }
31823   else
31824     goto invalid_kind;
31825
31826   cp_lexer_consume_token (parser->lexer);
31827
31828   if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31829     {
31830       c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31831       OMP_CLAUSE_DEPEND_KIND (c) = kind;
31832       OMP_CLAUSE_DECL (c) = NULL_TREE;
31833       OMP_CLAUSE_CHAIN (c) = list;
31834       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31835         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31836                                                /*or_comma=*/false,
31837                                                /*consume_paren=*/true);
31838       return c;
31839     }
31840
31841   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31842     goto resync_fail;
31843
31844   if (kind == OMP_CLAUSE_DEPEND_SINK)
31845     nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31846   else
31847     {
31848       nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31849                                               list, NULL);
31850
31851       for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31852         OMP_CLAUSE_DEPEND_KIND (c) = kind;
31853     }
31854   return nlist;
31855
31856  invalid_kind:
31857   cp_parser_error (parser, "invalid depend kind");
31858  resync_fail:
31859   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31860                                          /*or_comma=*/false,
31861                                          /*consume_paren=*/true);
31862   return list;
31863 }
31864
31865 /* OpenMP 4.0:
31866    map ( map-kind : variable-list )
31867    map ( variable-list )
31868
31869    map-kind:
31870      alloc | to | from | tofrom
31871
31872    OpenMP 4.5:
31873    map-kind:
31874      alloc | to | from | tofrom | release | delete
31875
31876    map ( always [,] map-kind: variable-list ) */
31877
31878 static tree
31879 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31880 {
31881   tree nlist, c;
31882   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31883   bool always = false;
31884
31885   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31886     return list;
31887
31888   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31889     {
31890       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31891       const char *p = IDENTIFIER_POINTER (id);
31892
31893       if (strcmp ("always", p) == 0)
31894         {
31895           int nth = 2;
31896           if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31897             nth++;
31898           if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31899                || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31900                    == RID_DELETE))
31901               && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31902                   == CPP_COLON))
31903             {
31904               always = true;
31905               cp_lexer_consume_token (parser->lexer);
31906               if (nth == 3)
31907                 cp_lexer_consume_token (parser->lexer);
31908             }
31909         }
31910     }
31911
31912   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31913       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31914     {
31915       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31916       const char *p = IDENTIFIER_POINTER (id);
31917
31918       if (strcmp ("alloc", p) == 0)
31919         kind = GOMP_MAP_ALLOC;
31920       else if (strcmp ("to", p) == 0)
31921         kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31922       else if (strcmp ("from", p) == 0)
31923         kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31924       else if (strcmp ("tofrom", p) == 0)
31925         kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31926       else if (strcmp ("release", p) == 0)
31927         kind = GOMP_MAP_RELEASE;
31928       else
31929         {
31930           cp_parser_error (parser, "invalid map kind");
31931           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31932                                                  /*or_comma=*/false,
31933                                                  /*consume_paren=*/true);
31934           return list;
31935         }
31936       cp_lexer_consume_token (parser->lexer);
31937       cp_lexer_consume_token (parser->lexer);
31938     }
31939   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31940            && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31941     {
31942       kind = GOMP_MAP_DELETE;
31943       cp_lexer_consume_token (parser->lexer);
31944       cp_lexer_consume_token (parser->lexer);
31945     }
31946
31947   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31948                                           NULL);
31949
31950   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31951     OMP_CLAUSE_SET_MAP_KIND (c, kind);
31952
31953   return nlist;
31954 }
31955
31956 /* OpenMP 4.0:
31957    device ( expression ) */
31958
31959 static tree
31960 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31961                              location_t location)
31962 {
31963   tree t, c;
31964
31965   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31966     return list;
31967
31968   t = cp_parser_expression (parser);
31969
31970   if (t == error_mark_node
31971       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31972     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31973                                            /*or_comma=*/false,
31974                                            /*consume_paren=*/true);
31975
31976   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31977                              "device", location);
31978
31979   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31980   OMP_CLAUSE_DEVICE_ID (c) = t;
31981   OMP_CLAUSE_CHAIN (c) = list;
31982
31983   return c;
31984 }
31985
31986 /* OpenMP 4.0:
31987    dist_schedule ( static )
31988    dist_schedule ( static , expression )  */
31989
31990 static tree
31991 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31992                                     location_t location)
31993 {
31994   tree c, t;
31995
31996   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31997     return list;
31998
31999   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32000
32001   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32002     goto invalid_kind;
32003   cp_lexer_consume_token (parser->lexer);
32004
32005   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32006     {
32007       cp_lexer_consume_token (parser->lexer);
32008
32009       t = cp_parser_assignment_expression (parser);
32010
32011       if (t == error_mark_node)
32012         goto resync_fail;
32013       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32014
32015       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32016         goto resync_fail;
32017     }
32018   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32019     goto resync_fail;
32020
32021   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32022                              location);
32023   OMP_CLAUSE_CHAIN (c) = list;
32024   return c;
32025
32026  invalid_kind:
32027   cp_parser_error (parser, "invalid dist_schedule kind");
32028  resync_fail:
32029   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32030                                          /*or_comma=*/false,
32031                                          /*consume_paren=*/true);
32032   return list;
32033 }
32034
32035 /* OpenMP 4.0:
32036    proc_bind ( proc-bind-kind )
32037
32038    proc-bind-kind:
32039      master | close | spread  */
32040
32041 static tree
32042 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32043                                 location_t location)
32044 {
32045   tree c;
32046   enum omp_clause_proc_bind_kind kind;
32047
32048   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32049     return list;
32050
32051   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32052     {
32053       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32054       const char *p = IDENTIFIER_POINTER (id);
32055
32056       if (strcmp ("master", p) == 0)
32057         kind = OMP_CLAUSE_PROC_BIND_MASTER;
32058       else if (strcmp ("close", p) == 0)
32059         kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32060       else if (strcmp ("spread", p) == 0)
32061         kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32062       else
32063         goto invalid_kind;
32064     }
32065   else
32066     goto invalid_kind;
32067
32068   cp_lexer_consume_token (parser->lexer);
32069   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32070     goto resync_fail;
32071
32072   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32073   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32074                              location);
32075   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32076   OMP_CLAUSE_CHAIN (c) = list;
32077   return c;
32078
32079  invalid_kind:
32080   cp_parser_error (parser, "invalid depend kind");
32081  resync_fail:
32082   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32083                                          /*or_comma=*/false,
32084                                          /*consume_paren=*/true);
32085   return list;
32086 }
32087
32088 /* OpenACC:
32089    async [( int-expr )] */
32090
32091 static tree
32092 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32093 {
32094   tree c, t;
32095   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32096
32097   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32098
32099   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32100     {
32101       cp_lexer_consume_token (parser->lexer);
32102
32103       t = cp_parser_expression (parser);
32104       if (t == error_mark_node
32105           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32106         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32107                                                 /*or_comma=*/false,
32108                                                 /*consume_paren=*/true);
32109     }
32110
32111   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32112
32113   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32114   OMP_CLAUSE_ASYNC_EXPR (c) = t;
32115   OMP_CLAUSE_CHAIN (c) = list;
32116   list = c;
32117
32118   return list;
32119 }
32120
32121 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
32122    is a bitmask in MASK.  Return the list of clauses found.  */
32123
32124 static tree
32125 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32126                            const char *where, cp_token *pragma_tok,
32127                            bool finish_p = true)
32128 {
32129   tree clauses = NULL;
32130   bool first = true;
32131
32132   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32133     {
32134       location_t here;
32135       pragma_omp_clause c_kind;
32136       omp_clause_code code;
32137       const char *c_name;
32138       tree prev = clauses;
32139
32140       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32141         cp_lexer_consume_token (parser->lexer);
32142
32143       here = cp_lexer_peek_token (parser->lexer)->location;
32144       c_kind = cp_parser_omp_clause_name (parser);
32145
32146       switch (c_kind)
32147         {
32148         case PRAGMA_OACC_CLAUSE_ASYNC:
32149           clauses = cp_parser_oacc_clause_async (parser, clauses);
32150           c_name = "async";
32151           break;
32152         case PRAGMA_OACC_CLAUSE_AUTO:
32153           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32154                                                  clauses, here);
32155           c_name = "auto";
32156           break;
32157         case PRAGMA_OACC_CLAUSE_COLLAPSE:
32158           clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32159           c_name = "collapse";
32160           break;
32161         case PRAGMA_OACC_CLAUSE_COPY:
32162           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32163           c_name = "copy";
32164           break;
32165         case PRAGMA_OACC_CLAUSE_COPYIN:
32166           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32167           c_name = "copyin";
32168           break;
32169         case PRAGMA_OACC_CLAUSE_COPYOUT:
32170           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32171           c_name = "copyout";
32172           break;
32173         case PRAGMA_OACC_CLAUSE_CREATE:
32174           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32175           c_name = "create";
32176           break;
32177         case PRAGMA_OACC_CLAUSE_DELETE:
32178           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32179           c_name = "delete";
32180           break;
32181         case PRAGMA_OMP_CLAUSE_DEFAULT:
32182           clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32183           c_name = "default";
32184           break;
32185         case PRAGMA_OACC_CLAUSE_DEVICE:
32186           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32187           c_name = "device";
32188           break;
32189         case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32190           clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32191           c_name = "deviceptr";
32192           break;
32193         case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32194           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32195           c_name = "device_resident";
32196           break;
32197         case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32198           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32199                                             clauses);
32200           c_name = "firstprivate";
32201           break;
32202         case PRAGMA_OACC_CLAUSE_GANG:
32203           c_name = "gang";
32204           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32205                                                  c_name, clauses);
32206           break;
32207         case PRAGMA_OACC_CLAUSE_HOST:
32208           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32209           c_name = "host";
32210           break;
32211         case PRAGMA_OACC_CLAUSE_IF:
32212           clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32213           c_name = "if";
32214           break;
32215         case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32216           clauses = cp_parser_oacc_simple_clause (parser,
32217                                                   OMP_CLAUSE_INDEPENDENT,
32218                                                   clauses, here);
32219           c_name = "independent";
32220           break;
32221         case PRAGMA_OACC_CLAUSE_LINK:
32222           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32223           c_name = "link";
32224           break;
32225         case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32226           code = OMP_CLAUSE_NUM_GANGS;
32227           c_name = "num_gangs";
32228           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32229                                                       clauses);
32230           break;
32231         case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32232           c_name = "num_workers";
32233           code = OMP_CLAUSE_NUM_WORKERS;
32234           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32235                                                       clauses);
32236           break;
32237         case PRAGMA_OACC_CLAUSE_PRESENT:
32238           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32239           c_name = "present";
32240           break;
32241         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32242           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32243           c_name = "present_or_copy";
32244           break;
32245         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32246           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32247           c_name = "present_or_copyin";
32248           break;
32249         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32250           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32251           c_name = "present_or_copyout";
32252           break;
32253         case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32254           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32255           c_name = "present_or_create";
32256           break;
32257         case PRAGMA_OACC_CLAUSE_PRIVATE:
32258           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32259                                             clauses);
32260           c_name = "private";
32261           break;
32262         case PRAGMA_OACC_CLAUSE_REDUCTION:
32263           clauses = cp_parser_omp_clause_reduction (parser, clauses);
32264           c_name = "reduction";
32265           break;
32266         case PRAGMA_OACC_CLAUSE_SELF:
32267           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32268           c_name = "self";
32269           break;
32270         case PRAGMA_OACC_CLAUSE_SEQ:
32271           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32272                                                  clauses, here);
32273           c_name = "seq";
32274           break;
32275         case PRAGMA_OACC_CLAUSE_TILE:
32276           clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32277           c_name = "tile";
32278           break;
32279         case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32280           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32281                                             clauses);
32282           c_name = "use_device";
32283           break;
32284         case PRAGMA_OACC_CLAUSE_VECTOR:
32285           c_name = "vector";
32286           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32287                                                  c_name, clauses);
32288           break;
32289         case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32290           c_name = "vector_length";
32291           code = OMP_CLAUSE_VECTOR_LENGTH;
32292           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32293                                                       clauses);
32294           break;
32295         case PRAGMA_OACC_CLAUSE_WAIT:
32296           clauses = cp_parser_oacc_clause_wait (parser, clauses);
32297           c_name = "wait";
32298           break;
32299         case PRAGMA_OACC_CLAUSE_WORKER:
32300           c_name = "worker";
32301           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32302                                                  c_name, clauses);
32303           break;
32304         default:
32305           cp_parser_error (parser, "expected %<#pragma acc%> clause");
32306           goto saw_error;
32307         }
32308
32309       first = false;
32310
32311       if (((mask >> c_kind) & 1) == 0)
32312         {
32313           /* Remove the invalid clause(s) from the list to avoid
32314              confusing the rest of the compiler.  */
32315           clauses = prev;
32316           error_at (here, "%qs is not valid for %qs", c_name, where);
32317         }
32318     }
32319
32320  saw_error:
32321   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32322
32323   if (finish_p)
32324     return finish_omp_clauses (clauses, false);
32325
32326   return clauses;
32327 }
32328
32329 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
32330    is a bitmask in MASK.  Return the list of clauses found; the result
32331    of clause default goes in *pdefault.  */
32332
32333 static tree
32334 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32335                            const char *where, cp_token *pragma_tok,
32336                            bool finish_p = true)
32337 {
32338   tree clauses = NULL;
32339   bool first = true;
32340   cp_token *token = NULL;
32341
32342   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32343     {
32344       pragma_omp_clause c_kind;
32345       const char *c_name;
32346       tree prev = clauses;
32347
32348       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32349         cp_lexer_consume_token (parser->lexer);
32350
32351       token = cp_lexer_peek_token (parser->lexer);
32352       c_kind = cp_parser_omp_clause_name (parser);
32353
32354       switch (c_kind)
32355         {
32356         case PRAGMA_OMP_CLAUSE_COLLAPSE:
32357           clauses = cp_parser_omp_clause_collapse (parser, clauses,
32358                                                    token->location);
32359           c_name = "collapse";
32360           break;
32361         case PRAGMA_OMP_CLAUSE_COPYIN:
32362           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32363           c_name = "copyin";
32364           break;
32365         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32366           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32367                                             clauses);
32368           c_name = "copyprivate";
32369           break;
32370         case PRAGMA_OMP_CLAUSE_DEFAULT:
32371           clauses = cp_parser_omp_clause_default (parser, clauses,
32372                                                   token->location, false);
32373           c_name = "default";
32374           break;
32375         case PRAGMA_OMP_CLAUSE_FINAL:
32376           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32377           c_name = "final";
32378           break;
32379         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32380           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32381                                             clauses);
32382           c_name = "firstprivate";
32383           break;
32384         case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32385           clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32386                                                     token->location);
32387           c_name = "grainsize";
32388           break;
32389         case PRAGMA_OMP_CLAUSE_HINT:
32390           clauses = cp_parser_omp_clause_hint (parser, clauses,
32391                                                token->location);
32392           c_name = "hint";
32393           break;
32394         case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32395           clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32396                                                      token->location);
32397           c_name = "defaultmap";
32398           break;
32399         case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32400           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32401                                             clauses);
32402           c_name = "use_device_ptr";
32403           break;
32404         case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32405           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32406                                             clauses);
32407           c_name = "is_device_ptr";
32408           break;
32409         case PRAGMA_OMP_CLAUSE_IF:
32410           clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32411                                              true);
32412           c_name = "if";
32413           break;
32414         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32415           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32416                                             clauses);
32417           c_name = "lastprivate";
32418           break;
32419         case PRAGMA_OMP_CLAUSE_MERGEABLE:
32420           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32421                                                     token->location);
32422           c_name = "mergeable";
32423           break;
32424         case PRAGMA_OMP_CLAUSE_NOWAIT:
32425           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32426           c_name = "nowait";
32427           break;
32428         case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32429           clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32430                                                     token->location);
32431           c_name = "num_tasks";
32432           break;
32433         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32434           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32435                                                       token->location);
32436           c_name = "num_threads";
32437           break;
32438         case PRAGMA_OMP_CLAUSE_ORDERED:
32439           clauses = cp_parser_omp_clause_ordered (parser, clauses,
32440                                                   token->location);
32441           c_name = "ordered";
32442           break;
32443         case PRAGMA_OMP_CLAUSE_PRIORITY:
32444           clauses = cp_parser_omp_clause_priority (parser, clauses,
32445                                                    token->location);
32446           c_name = "priority";
32447           break;
32448         case PRAGMA_OMP_CLAUSE_PRIVATE:
32449           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32450                                             clauses);
32451           c_name = "private";
32452           break;
32453         case PRAGMA_OMP_CLAUSE_REDUCTION:
32454           clauses = cp_parser_omp_clause_reduction (parser, clauses);
32455           c_name = "reduction";
32456           break;
32457         case PRAGMA_OMP_CLAUSE_SCHEDULE:
32458           clauses = cp_parser_omp_clause_schedule (parser, clauses,
32459                                                    token->location);
32460           c_name = "schedule";
32461           break;
32462         case PRAGMA_OMP_CLAUSE_SHARED:
32463           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32464                                             clauses);
32465           c_name = "shared";
32466           break;
32467         case PRAGMA_OMP_CLAUSE_UNTIED:
32468           clauses = cp_parser_omp_clause_untied (parser, clauses,
32469                                                  token->location);
32470           c_name = "untied";
32471           break;
32472         case PRAGMA_OMP_CLAUSE_INBRANCH:
32473         case PRAGMA_CILK_CLAUSE_MASK:
32474           clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32475                                                  clauses, token->location);
32476           c_name = "inbranch";
32477           break;
32478         case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32479         case PRAGMA_CILK_CLAUSE_NOMASK:
32480           clauses = cp_parser_omp_clause_branch (parser,
32481                                                  OMP_CLAUSE_NOTINBRANCH,
32482                                                  clauses, token->location);
32483           c_name = "notinbranch";
32484           break;
32485         case PRAGMA_OMP_CLAUSE_PARALLEL:
32486           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32487                                                      clauses, token->location);
32488           c_name = "parallel";
32489           if (!first)
32490             {
32491              clause_not_first:
32492               error_at (token->location, "%qs must be the first clause of %qs",
32493                         c_name, where);
32494               clauses = prev;
32495             }
32496           break;
32497         case PRAGMA_OMP_CLAUSE_FOR:
32498           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32499                                                      clauses, token->location);
32500           c_name = "for";
32501           if (!first)
32502             goto clause_not_first;
32503           break;
32504         case PRAGMA_OMP_CLAUSE_SECTIONS:
32505           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32506                                                      clauses, token->location);
32507           c_name = "sections";
32508           if (!first)
32509             goto clause_not_first;
32510           break;
32511         case PRAGMA_OMP_CLAUSE_TASKGROUP:
32512           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32513                                                      clauses, token->location);
32514           c_name = "taskgroup";
32515           if (!first)
32516             goto clause_not_first;
32517           break;
32518         case PRAGMA_OMP_CLAUSE_LINK:
32519           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32520           c_name = "to";
32521           break;
32522         case PRAGMA_OMP_CLAUSE_TO:
32523           if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32524             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32525                                               clauses);
32526           else
32527             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32528           c_name = "to";
32529           break;
32530         case PRAGMA_OMP_CLAUSE_FROM:
32531           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32532           c_name = "from";
32533           break;
32534         case PRAGMA_OMP_CLAUSE_UNIFORM:
32535           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32536                                             clauses);
32537           c_name = "uniform";
32538           break;
32539         case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32540           clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32541                                                     token->location);
32542           c_name = "num_teams";
32543           break;
32544         case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32545           clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32546                                                        token->location);
32547           c_name = "thread_limit";
32548           break;
32549         case PRAGMA_OMP_CLAUSE_ALIGNED:
32550           clauses = cp_parser_omp_clause_aligned (parser, clauses);
32551           c_name = "aligned";
32552           break;
32553         case PRAGMA_OMP_CLAUSE_LINEAR:
32554           {
32555             bool cilk_simd_fn = false, declare_simd = false;
32556             if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32557               cilk_simd_fn = true;
32558             else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32559               declare_simd = true;
32560             clauses = cp_parser_omp_clause_linear (parser, clauses,
32561                                                    cilk_simd_fn, declare_simd);
32562           }
32563           c_name = "linear";
32564           break;
32565         case PRAGMA_OMP_CLAUSE_DEPEND:
32566           clauses = cp_parser_omp_clause_depend (parser, clauses,
32567                                                  token->location);
32568           c_name = "depend";
32569           break;
32570         case PRAGMA_OMP_CLAUSE_MAP:
32571           clauses = cp_parser_omp_clause_map (parser, clauses);
32572           c_name = "map";
32573           break;
32574         case PRAGMA_OMP_CLAUSE_DEVICE:
32575           clauses = cp_parser_omp_clause_device (parser, clauses,
32576                                                  token->location);
32577           c_name = "device";
32578           break;
32579         case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32580           clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32581                                                         token->location);
32582           c_name = "dist_schedule";
32583           break;
32584         case PRAGMA_OMP_CLAUSE_PROC_BIND:
32585           clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32586                                                     token->location);
32587           c_name = "proc_bind";
32588           break;
32589         case PRAGMA_OMP_CLAUSE_SAFELEN:
32590           clauses = cp_parser_omp_clause_safelen (parser, clauses,
32591                                                   token->location);
32592           c_name = "safelen";
32593           break;
32594         case PRAGMA_OMP_CLAUSE_SIMDLEN:
32595           clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32596                                                   token->location);
32597           c_name = "simdlen";
32598           break;
32599         case PRAGMA_OMP_CLAUSE_NOGROUP:
32600           clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32601                                                   token->location);
32602           c_name = "nogroup";
32603           break;
32604         case PRAGMA_OMP_CLAUSE_THREADS:
32605           clauses
32606             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32607                                                 clauses, token->location);
32608           c_name = "threads";
32609           break;
32610         case PRAGMA_OMP_CLAUSE_SIMD:
32611           clauses
32612             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32613                                                 clauses, token->location);
32614           c_name = "simd";
32615           break;
32616         case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32617           clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32618           c_name = "simdlen";
32619           break;
32620         default:
32621           cp_parser_error (parser, "expected %<#pragma omp%> clause");
32622           goto saw_error;
32623         }
32624
32625       first = false;
32626
32627       if (((mask >> c_kind) & 1) == 0)
32628         {
32629           /* Remove the invalid clause(s) from the list to avoid
32630              confusing the rest of the compiler.  */
32631           clauses = prev;
32632           error_at (token->location, "%qs is not valid for %qs", c_name, where);
32633         }
32634     }
32635  saw_error:
32636   /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32637      no reason to skip to the end.  */
32638   if (!(flag_cilkplus && pragma_tok == NULL))
32639     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32640   if (finish_p)
32641     {
32642       if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32643         return finish_omp_clauses (clauses, false, true);
32644       else
32645         return finish_omp_clauses (clauses, true);
32646     }
32647   return clauses;
32648 }
32649
32650 /* OpenMP 2.5:
32651    structured-block:
32652      statement
32653
32654    In practice, we're also interested in adding the statement to an
32655    outer node.  So it is convenient if we work around the fact that
32656    cp_parser_statement calls add_stmt.  */
32657
32658 static unsigned
32659 cp_parser_begin_omp_structured_block (cp_parser *parser)
32660 {
32661   unsigned save = parser->in_statement;
32662
32663   /* Only move the values to IN_OMP_BLOCK if they weren't false.
32664      This preserves the "not within loop or switch" style error messages
32665      for nonsense cases like
32666         void foo() {
32667         #pragma omp single
32668           break;
32669         }
32670   */
32671   if (parser->in_statement)
32672     parser->in_statement = IN_OMP_BLOCK;
32673
32674   return save;
32675 }
32676
32677 static void
32678 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32679 {
32680   parser->in_statement = save;
32681 }
32682
32683 static tree
32684 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32685 {
32686   tree stmt = begin_omp_structured_block ();
32687   unsigned int save = cp_parser_begin_omp_structured_block (parser);
32688
32689   cp_parser_statement (parser, NULL_TREE, false, if_p);
32690
32691   cp_parser_end_omp_structured_block (parser, save);
32692   return finish_omp_structured_block (stmt);
32693 }
32694
32695 /* OpenMP 2.5:
32696    # pragma omp atomic new-line
32697      expression-stmt
32698
32699    expression-stmt:
32700      x binop= expr | x++ | ++x | x-- | --x
32701    binop:
32702      +, *, -, /, &, ^, |, <<, >>
32703
32704   where x is an lvalue expression with scalar type.
32705
32706    OpenMP 3.1:
32707    # pragma omp atomic new-line
32708      update-stmt
32709
32710    # pragma omp atomic read new-line
32711      read-stmt
32712
32713    # pragma omp atomic write new-line
32714      write-stmt
32715
32716    # pragma omp atomic update new-line
32717      update-stmt
32718
32719    # pragma omp atomic capture new-line
32720      capture-stmt
32721
32722    # pragma omp atomic capture new-line
32723      capture-block
32724
32725    read-stmt:
32726      v = x
32727    write-stmt:
32728      x = expr
32729    update-stmt:
32730      expression-stmt | x = x binop expr
32731    capture-stmt:
32732      v = expression-stmt
32733    capture-block:
32734      { v = x; update-stmt; } | { update-stmt; v = x; }
32735
32736    OpenMP 4.0:
32737    update-stmt:
32738      expression-stmt | x = x binop expr | x = expr binop x
32739    capture-stmt:
32740      v = update-stmt
32741    capture-block:
32742      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32743
32744   where x and v are lvalue expressions with scalar type.  */
32745
32746 static void
32747 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32748 {
32749   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32750   tree rhs1 = NULL_TREE, orig_lhs;
32751   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32752   bool structured_block = false;
32753   bool seq_cst = false;
32754
32755   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32756     {
32757       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32758       const char *p = IDENTIFIER_POINTER (id);
32759
32760       if (!strcmp (p, "seq_cst"))
32761         {
32762           seq_cst = true;
32763           cp_lexer_consume_token (parser->lexer);
32764           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32765               && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32766             cp_lexer_consume_token (parser->lexer);
32767         }
32768     }
32769   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32770     {
32771       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32772       const char *p = IDENTIFIER_POINTER (id);
32773
32774       if (!strcmp (p, "read"))
32775         code = OMP_ATOMIC_READ;
32776       else if (!strcmp (p, "write"))
32777         code = NOP_EXPR;
32778       else if (!strcmp (p, "update"))
32779         code = OMP_ATOMIC;
32780       else if (!strcmp (p, "capture"))
32781         code = OMP_ATOMIC_CAPTURE_NEW;
32782       else
32783         p = NULL;
32784       if (p)
32785         cp_lexer_consume_token (parser->lexer);
32786     }
32787   if (!seq_cst)
32788     {
32789       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32790           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32791         cp_lexer_consume_token (parser->lexer);
32792
32793       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32794         {
32795           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32796           const char *p = IDENTIFIER_POINTER (id);
32797
32798           if (!strcmp (p, "seq_cst"))
32799             {
32800               seq_cst = true;
32801               cp_lexer_consume_token (parser->lexer);
32802             }
32803         }
32804     }
32805   cp_parser_require_pragma_eol (parser, pragma_tok);
32806
32807   switch (code)
32808     {
32809     case OMP_ATOMIC_READ:
32810     case NOP_EXPR: /* atomic write */
32811       v = cp_parser_unary_expression (parser);
32812       if (v == error_mark_node)
32813         goto saw_error;
32814       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32815         goto saw_error;
32816       if (code == NOP_EXPR)
32817         lhs = cp_parser_expression (parser);
32818       else
32819         lhs = cp_parser_unary_expression (parser);
32820       if (lhs == error_mark_node)
32821         goto saw_error;
32822       if (code == NOP_EXPR)
32823         {
32824           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32825              opcode.  */
32826           code = OMP_ATOMIC;
32827           rhs = lhs;
32828           lhs = v;
32829           v = NULL_TREE;
32830         }
32831       goto done;
32832     case OMP_ATOMIC_CAPTURE_NEW:
32833       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32834         {
32835           cp_lexer_consume_token (parser->lexer);
32836           structured_block = true;
32837         }
32838       else
32839         {
32840           v = cp_parser_unary_expression (parser);
32841           if (v == error_mark_node)
32842             goto saw_error;
32843           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32844             goto saw_error;
32845         }
32846     default:
32847       break;
32848     }
32849
32850 restart:
32851   lhs = cp_parser_unary_expression (parser);
32852   orig_lhs = lhs;
32853   switch (TREE_CODE (lhs))
32854     {
32855     case ERROR_MARK:
32856       goto saw_error;
32857
32858     case POSTINCREMENT_EXPR:
32859       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32860         code = OMP_ATOMIC_CAPTURE_OLD;
32861       /* FALLTHROUGH */
32862     case PREINCREMENT_EXPR:
32863       lhs = TREE_OPERAND (lhs, 0);
32864       opcode = PLUS_EXPR;
32865       rhs = integer_one_node;
32866       break;
32867
32868     case POSTDECREMENT_EXPR:
32869       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32870         code = OMP_ATOMIC_CAPTURE_OLD;
32871       /* FALLTHROUGH */
32872     case PREDECREMENT_EXPR:
32873       lhs = TREE_OPERAND (lhs, 0);
32874       opcode = MINUS_EXPR;
32875       rhs = integer_one_node;
32876       break;
32877
32878     case COMPOUND_EXPR:
32879       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32880          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32881          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32882          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32883          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32884                                              (TREE_OPERAND (lhs, 1), 0), 0)))
32885             == BOOLEAN_TYPE)
32886        /* Undo effects of boolean_increment for post {in,de}crement.  */
32887        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32888       /* FALLTHRU */
32889     case MODIFY_EXPR:
32890       if (TREE_CODE (lhs) == MODIFY_EXPR
32891          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32892         {
32893           /* Undo effects of boolean_increment.  */
32894           if (integer_onep (TREE_OPERAND (lhs, 1)))
32895             {
32896               /* This is pre or post increment.  */
32897               rhs = TREE_OPERAND (lhs, 1);
32898               lhs = TREE_OPERAND (lhs, 0);
32899               opcode = NOP_EXPR;
32900               if (code == OMP_ATOMIC_CAPTURE_NEW
32901                   && !structured_block
32902                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32903                 code = OMP_ATOMIC_CAPTURE_OLD;
32904               break;
32905             }
32906         }
32907       /* FALLTHRU */
32908     default:
32909       switch (cp_lexer_peek_token (parser->lexer)->type)
32910         {
32911         case CPP_MULT_EQ:
32912           opcode = MULT_EXPR;
32913           break;
32914         case CPP_DIV_EQ:
32915           opcode = TRUNC_DIV_EXPR;
32916           break;
32917         case CPP_PLUS_EQ:
32918           opcode = PLUS_EXPR;
32919           break;
32920         case CPP_MINUS_EQ:
32921           opcode = MINUS_EXPR;
32922           break;
32923         case CPP_LSHIFT_EQ:
32924           opcode = LSHIFT_EXPR;
32925           break;
32926         case CPP_RSHIFT_EQ:
32927           opcode = RSHIFT_EXPR;
32928           break;
32929         case CPP_AND_EQ:
32930           opcode = BIT_AND_EXPR;
32931           break;
32932         case CPP_OR_EQ:
32933           opcode = BIT_IOR_EXPR;
32934           break;
32935         case CPP_XOR_EQ:
32936           opcode = BIT_XOR_EXPR;
32937           break;
32938         case CPP_EQ:
32939           enum cp_parser_prec oprec;
32940           cp_token *token;
32941           cp_lexer_consume_token (parser->lexer);
32942           cp_parser_parse_tentatively (parser);
32943           rhs1 = cp_parser_simple_cast_expression (parser);
32944           if (rhs1 == error_mark_node)
32945             {
32946               cp_parser_abort_tentative_parse (parser);
32947               cp_parser_simple_cast_expression (parser);
32948               goto saw_error;
32949             }
32950           token = cp_lexer_peek_token (parser->lexer);
32951           if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32952             {
32953               cp_parser_abort_tentative_parse (parser);
32954               cp_parser_parse_tentatively (parser);
32955               rhs = cp_parser_binary_expression (parser, false, true,
32956                                                  PREC_NOT_OPERATOR, NULL);
32957               if (rhs == error_mark_node)
32958                 {
32959                   cp_parser_abort_tentative_parse (parser);
32960                   cp_parser_binary_expression (parser, false, true,
32961                                                PREC_NOT_OPERATOR, NULL);
32962                   goto saw_error;
32963                 }
32964               switch (TREE_CODE (rhs))
32965                 {
32966                 case MULT_EXPR:
32967                 case TRUNC_DIV_EXPR:
32968                 case RDIV_EXPR:
32969                 case PLUS_EXPR:
32970                 case MINUS_EXPR:
32971                 case LSHIFT_EXPR:
32972                 case RSHIFT_EXPR:
32973                 case BIT_AND_EXPR:
32974                 case BIT_IOR_EXPR:
32975                 case BIT_XOR_EXPR:
32976                   if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32977                     {
32978                       if (cp_parser_parse_definitely (parser))
32979                         {
32980                           opcode = TREE_CODE (rhs);
32981                           rhs1 = TREE_OPERAND (rhs, 0);
32982                           rhs = TREE_OPERAND (rhs, 1);
32983                           goto stmt_done;
32984                         }
32985                       else
32986                         goto saw_error;
32987                     }
32988                   break;
32989                 default:
32990                   break;
32991                 }
32992               cp_parser_abort_tentative_parse (parser);
32993               if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32994                 {
32995                   rhs = cp_parser_expression (parser);
32996                   if (rhs == error_mark_node)
32997                     goto saw_error;
32998                   opcode = NOP_EXPR;
32999                   rhs1 = NULL_TREE;
33000                   goto stmt_done;
33001                 }
33002               cp_parser_error (parser,
33003                                "invalid form of %<#pragma omp atomic%>");
33004               goto saw_error;
33005             }
33006           if (!cp_parser_parse_definitely (parser))
33007             goto saw_error;
33008           switch (token->type)
33009             {
33010             case CPP_SEMICOLON:
33011               if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33012                 {
33013                   code = OMP_ATOMIC_CAPTURE_OLD;
33014                   v = lhs;
33015                   lhs = NULL_TREE;
33016                   lhs1 = rhs1;
33017                   rhs1 = NULL_TREE;
33018                   cp_lexer_consume_token (parser->lexer);
33019                   goto restart;
33020                 }
33021               else if (structured_block)
33022                 {
33023                   opcode = NOP_EXPR;
33024                   rhs = rhs1;
33025                   rhs1 = NULL_TREE;
33026                   goto stmt_done;
33027                 }
33028               cp_parser_error (parser,
33029                                "invalid form of %<#pragma omp atomic%>");
33030               goto saw_error;
33031             case CPP_MULT:
33032               opcode = MULT_EXPR;
33033               break;
33034             case CPP_DIV:
33035               opcode = TRUNC_DIV_EXPR;
33036               break;
33037             case CPP_PLUS:
33038               opcode = PLUS_EXPR;
33039               break;
33040             case CPP_MINUS:
33041               opcode = MINUS_EXPR;
33042               break;
33043             case CPP_LSHIFT:
33044               opcode = LSHIFT_EXPR;
33045               break;
33046             case CPP_RSHIFT:
33047               opcode = RSHIFT_EXPR;
33048               break;
33049             case CPP_AND:
33050               opcode = BIT_AND_EXPR;
33051               break;
33052             case CPP_OR:
33053               opcode = BIT_IOR_EXPR;
33054               break;
33055             case CPP_XOR:
33056               opcode = BIT_XOR_EXPR;
33057               break;
33058             default:
33059               cp_parser_error (parser,
33060                                "invalid operator for %<#pragma omp atomic%>");
33061               goto saw_error;
33062             }
33063           oprec = TOKEN_PRECEDENCE (token);
33064           gcc_assert (oprec != PREC_NOT_OPERATOR);
33065           if (commutative_tree_code (opcode))
33066             oprec = (enum cp_parser_prec) (oprec - 1);
33067           cp_lexer_consume_token (parser->lexer);
33068           rhs = cp_parser_binary_expression (parser, false, false,
33069                                              oprec, NULL);
33070           if (rhs == error_mark_node)
33071             goto saw_error;
33072           goto stmt_done;
33073           /* FALLTHROUGH */
33074         default:
33075           cp_parser_error (parser,
33076                            "invalid operator for %<#pragma omp atomic%>");
33077           goto saw_error;
33078         }
33079       cp_lexer_consume_token (parser->lexer);
33080
33081       rhs = cp_parser_expression (parser);
33082       if (rhs == error_mark_node)
33083         goto saw_error;
33084       break;
33085     }
33086 stmt_done:
33087   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33088     {
33089       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33090         goto saw_error;
33091       v = cp_parser_unary_expression (parser);
33092       if (v == error_mark_node)
33093         goto saw_error;
33094       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33095         goto saw_error;
33096       lhs1 = cp_parser_unary_expression (parser);
33097       if (lhs1 == error_mark_node)
33098         goto saw_error;
33099     }
33100   if (structured_block)
33101     {
33102       cp_parser_consume_semicolon_at_end_of_statement (parser);
33103       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33104     }
33105 done:
33106   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33107   if (!structured_block)
33108     cp_parser_consume_semicolon_at_end_of_statement (parser);
33109   return;
33110
33111  saw_error:
33112   cp_parser_skip_to_end_of_block_or_statement (parser);
33113   if (structured_block)
33114     {
33115       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33116         cp_lexer_consume_token (parser->lexer);
33117       else if (code == OMP_ATOMIC_CAPTURE_NEW)
33118         {
33119           cp_parser_skip_to_end_of_block_or_statement (parser);
33120           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33121             cp_lexer_consume_token (parser->lexer);
33122         }
33123     }
33124 }
33125
33126
33127 /* OpenMP 2.5:
33128    # pragma omp barrier new-line  */
33129
33130 static void
33131 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33132 {
33133   cp_parser_require_pragma_eol (parser, pragma_tok);
33134   finish_omp_barrier ();
33135 }
33136
33137 /* OpenMP 2.5:
33138    # pragma omp critical [(name)] new-line
33139      structured-block
33140
33141    OpenMP 4.5:
33142    # pragma omp critical [(name) [hint(expression)]] new-line
33143      structured-block  */
33144
33145 #define OMP_CRITICAL_CLAUSE_MASK                \
33146         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33147
33148 static tree
33149 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33150 {
33151   tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33152
33153   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33154     {
33155       cp_lexer_consume_token (parser->lexer);
33156
33157       name = cp_parser_identifier (parser);
33158
33159       if (name == error_mark_node
33160           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33161         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33162                                                /*or_comma=*/false,
33163                                                /*consume_paren=*/true);
33164       if (name == error_mark_node)
33165         name = NULL;
33166
33167       clauses = cp_parser_omp_all_clauses (parser,
33168                                            OMP_CRITICAL_CLAUSE_MASK,
33169                                            "#pragma omp critical", pragma_tok);
33170     }
33171   else
33172     cp_parser_require_pragma_eol (parser, pragma_tok);
33173
33174   stmt = cp_parser_omp_structured_block (parser, if_p);
33175   return c_finish_omp_critical (input_location, stmt, name, clauses);
33176 }
33177
33178 /* OpenMP 2.5:
33179    # pragma omp flush flush-vars[opt] new-line
33180
33181    flush-vars:
33182      ( variable-list ) */
33183
33184 static void
33185 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33186 {
33187   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33188     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33189   cp_parser_require_pragma_eol (parser, pragma_tok);
33190
33191   finish_omp_flush ();
33192 }
33193
33194 /* Helper function, to parse omp for increment expression.  */
33195
33196 static tree
33197 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33198 {
33199   tree cond = cp_parser_binary_expression (parser, false, true,
33200                                            PREC_NOT_OPERATOR, NULL);
33201   if (cond == error_mark_node
33202       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33203     {
33204       cp_parser_skip_to_end_of_statement (parser);
33205       return error_mark_node;
33206     }
33207
33208   switch (TREE_CODE (cond))
33209     {
33210     case GT_EXPR:
33211     case GE_EXPR:
33212     case LT_EXPR:
33213     case LE_EXPR:
33214       break;
33215     case NE_EXPR:
33216       if (code == CILK_SIMD || code == CILK_FOR)
33217         break;
33218       /* Fall through: OpenMP disallows NE_EXPR.  */
33219     default:
33220       return error_mark_node;
33221     }
33222
33223   /* If decl is an iterator, preserve LHS and RHS of the relational
33224      expr until finish_omp_for.  */
33225   if (decl
33226       && (type_dependent_expression_p (decl)
33227           || CLASS_TYPE_P (TREE_TYPE (decl))))
33228     return cond;
33229
33230   return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33231                             TREE_CODE (cond),
33232                             TREE_OPERAND (cond, 0), ERROR_MARK,
33233                             TREE_OPERAND (cond, 1), ERROR_MARK,
33234                             /*overload=*/NULL, tf_warning_or_error);
33235 }
33236
33237 /* Helper function, to parse omp for increment expression.  */
33238
33239 static tree
33240 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33241 {
33242   cp_token *token = cp_lexer_peek_token (parser->lexer);
33243   enum tree_code op;
33244   tree lhs, rhs;
33245   cp_id_kind idk;
33246   bool decl_first;
33247
33248   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33249     {
33250       op = (token->type == CPP_PLUS_PLUS
33251             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33252       cp_lexer_consume_token (parser->lexer);
33253       lhs = cp_parser_simple_cast_expression (parser);
33254       if (lhs != decl
33255           && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33256         return error_mark_node;
33257       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33258     }
33259
33260   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33261   if (lhs != decl
33262       && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33263     return error_mark_node;
33264
33265   token = cp_lexer_peek_token (parser->lexer);
33266   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33267     {
33268       op = (token->type == CPP_PLUS_PLUS
33269             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33270       cp_lexer_consume_token (parser->lexer);
33271       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33272     }
33273
33274   op = cp_parser_assignment_operator_opt (parser);
33275   if (op == ERROR_MARK)
33276     return error_mark_node;
33277
33278   if (op != NOP_EXPR)
33279     {
33280       rhs = cp_parser_assignment_expression (parser);
33281       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33282       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33283     }
33284
33285   lhs = cp_parser_binary_expression (parser, false, false,
33286                                      PREC_ADDITIVE_EXPRESSION, NULL);
33287   token = cp_lexer_peek_token (parser->lexer);
33288   decl_first = (lhs == decl
33289                 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33290   if (decl_first)
33291     lhs = NULL_TREE;
33292   if (token->type != CPP_PLUS
33293       && token->type != CPP_MINUS)
33294     return error_mark_node;
33295
33296   do
33297     {
33298       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33299       cp_lexer_consume_token (parser->lexer);
33300       rhs = cp_parser_binary_expression (parser, false, false,
33301                                          PREC_ADDITIVE_EXPRESSION, NULL);
33302       token = cp_lexer_peek_token (parser->lexer);
33303       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33304         {
33305           if (lhs == NULL_TREE)
33306             {
33307               if (op == PLUS_EXPR)
33308                 lhs = rhs;
33309               else
33310                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33311                                         tf_warning_or_error);
33312             }
33313           else
33314             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33315                                      ERROR_MARK, NULL, tf_warning_or_error);
33316         }
33317     }
33318   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33319
33320   if (!decl_first)
33321     {
33322       if ((rhs != decl
33323            && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33324           || op == MINUS_EXPR)
33325         return error_mark_node;
33326       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33327     }
33328   else
33329     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33330
33331   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33332 }
33333
33334 /* Parse the initialization statement of either an OpenMP for loop or
33335    a Cilk Plus for loop.
33336
33337    Return true if the resulting construct should have an
33338    OMP_CLAUSE_PRIVATE added to it.  */
33339
33340 static tree
33341 cp_parser_omp_for_loop_init (cp_parser *parser,
33342                              enum tree_code code,
33343                              tree &this_pre_body,
33344                              vec<tree, va_gc> *for_block,
33345                              tree &init,
33346                              tree &orig_init,
33347                              tree &decl,
33348                              tree &real_decl)
33349 {
33350   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33351     return NULL_TREE;
33352
33353   tree add_private_clause = NULL_TREE;
33354
33355   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33356
33357      init-expr:
33358      var = lb
33359      integer-type var = lb
33360      random-access-iterator-type var = lb
33361      pointer-type var = lb
33362   */
33363   cp_decl_specifier_seq type_specifiers;
33364
33365   /* First, try to parse as an initialized declaration.  See
33366      cp_parser_condition, from whence the bulk of this is copied.  */
33367
33368   cp_parser_parse_tentatively (parser);
33369   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33370                                 /*is_trailing_return=*/false,
33371                                 &type_specifiers);
33372   if (cp_parser_parse_definitely (parser))
33373     {
33374       /* If parsing a type specifier seq succeeded, then this
33375          MUST be a initialized declaration.  */
33376       tree asm_specification, attributes;
33377       cp_declarator *declarator;
33378
33379       declarator = cp_parser_declarator (parser,
33380                                          CP_PARSER_DECLARATOR_NAMED,
33381                                          /*ctor_dtor_or_conv_p=*/NULL,
33382                                          /*parenthesized_p=*/NULL,
33383                                          /*member_p=*/false,
33384                                          /*friend_p=*/false);
33385       attributes = cp_parser_attributes_opt (parser);
33386       asm_specification = cp_parser_asm_specification_opt (parser);
33387
33388       if (declarator == cp_error_declarator) 
33389         cp_parser_skip_to_end_of_statement (parser);
33390
33391       else 
33392         {
33393           tree pushed_scope, auto_node;
33394
33395           decl = start_decl (declarator, &type_specifiers,
33396                              SD_INITIALIZED, attributes,
33397                              /*prefix_attributes=*/NULL_TREE,
33398                              &pushed_scope);
33399
33400           auto_node = type_uses_auto (TREE_TYPE (decl));
33401           if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33402             {
33403               if (cp_lexer_next_token_is (parser->lexer, 
33404                                           CPP_OPEN_PAREN))
33405                 {
33406                   if (code != CILK_SIMD && code != CILK_FOR)
33407                     error ("parenthesized initialization is not allowed in "
33408                            "OpenMP %<for%> loop");
33409                   else
33410                     error ("parenthesized initialization is "
33411                            "not allowed in for-loop");
33412                 }
33413               else
33414                 /* Trigger an error.  */
33415                 cp_parser_require (parser, CPP_EQ, RT_EQ);
33416
33417               init = error_mark_node;
33418               cp_parser_skip_to_end_of_statement (parser);
33419             }
33420           else if (CLASS_TYPE_P (TREE_TYPE (decl))
33421                    || type_dependent_expression_p (decl)
33422                    || auto_node)
33423             {
33424               bool is_direct_init, is_non_constant_init;
33425
33426               init = cp_parser_initializer (parser,
33427                                             &is_direct_init,
33428                                             &is_non_constant_init);
33429
33430               if (auto_node)
33431                 {
33432                   TREE_TYPE (decl)
33433                     = do_auto_deduction (TREE_TYPE (decl), init,
33434                                          auto_node);
33435
33436                   if (!CLASS_TYPE_P (TREE_TYPE (decl))
33437                       && !type_dependent_expression_p (decl))
33438                     goto non_class;
33439                 }
33440                       
33441               cp_finish_decl (decl, init, !is_non_constant_init,
33442                               asm_specification,
33443                               LOOKUP_ONLYCONVERTING);
33444               orig_init = init;
33445               if (CLASS_TYPE_P (TREE_TYPE (decl)))
33446                 {
33447                   vec_safe_push (for_block, this_pre_body);
33448                   init = NULL_TREE;
33449                 }
33450               else
33451                 init = pop_stmt_list (this_pre_body);
33452               this_pre_body = NULL_TREE;
33453             }
33454           else
33455             {
33456               /* Consume '='.  */
33457               cp_lexer_consume_token (parser->lexer);
33458               init = cp_parser_assignment_expression (parser);
33459
33460             non_class:
33461               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33462                 init = error_mark_node;
33463               else
33464                 cp_finish_decl (decl, NULL_TREE,
33465                                 /*init_const_expr_p=*/false,
33466                                 asm_specification,
33467                                 LOOKUP_ONLYCONVERTING);
33468             }
33469
33470           if (pushed_scope)
33471             pop_scope (pushed_scope);
33472         }
33473     }
33474   else 
33475     {
33476       cp_id_kind idk;
33477       /* If parsing a type specifier sequence failed, then
33478          this MUST be a simple expression.  */
33479       if (code == CILK_FOR)
33480         error ("%<_Cilk_for%> allows expression instead of declaration only "
33481                "in C, not in C++");
33482       cp_parser_parse_tentatively (parser);
33483       decl = cp_parser_primary_expression (parser, false, false,
33484                                            false, &idk);
33485       cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33486       if (!cp_parser_error_occurred (parser)
33487           && decl
33488           && (TREE_CODE (decl) == COMPONENT_REF
33489               || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33490         {
33491           cp_parser_abort_tentative_parse (parser);
33492           cp_parser_parse_tentatively (parser);
33493           cp_token *token = cp_lexer_peek_token (parser->lexer);
33494           tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33495                                                /*check_dependency_p=*/true,
33496                                                /*template_p=*/NULL,
33497                                                /*declarator_p=*/false,
33498                                                /*optional_p=*/false);
33499           if (name != error_mark_node
33500               && last_tok == cp_lexer_peek_token (parser->lexer))
33501             {
33502               decl = cp_parser_lookup_name_simple (parser, name,
33503                                                    token->location);
33504               if (TREE_CODE (decl) == FIELD_DECL)
33505                 add_private_clause = omp_privatize_field (decl, false);
33506             }
33507           cp_parser_abort_tentative_parse (parser);
33508           cp_parser_parse_tentatively (parser);
33509           decl = cp_parser_primary_expression (parser, false, false,
33510                                                false, &idk);
33511         }
33512       if (!cp_parser_error_occurred (parser)
33513           && decl
33514           && DECL_P (decl)
33515           && CLASS_TYPE_P (TREE_TYPE (decl)))
33516         {
33517           tree rhs;
33518
33519           cp_parser_parse_definitely (parser);
33520           cp_parser_require (parser, CPP_EQ, RT_EQ);
33521           rhs = cp_parser_assignment_expression (parser);
33522           orig_init = rhs;
33523           finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33524                                                  decl, NOP_EXPR,
33525                                                  rhs,
33526                                                  tf_warning_or_error));
33527           if (!add_private_clause)
33528             add_private_clause = decl;
33529         }
33530       else
33531         {
33532           decl = NULL;
33533           cp_parser_abort_tentative_parse (parser);
33534           init = cp_parser_expression (parser);
33535           if (init)
33536             {
33537               if (TREE_CODE (init) == MODIFY_EXPR
33538                   || TREE_CODE (init) == MODOP_EXPR)
33539                 real_decl = TREE_OPERAND (init, 0);
33540             }
33541         }
33542     }
33543   return add_private_clause;
33544 }
33545
33546 /* Parse the restricted form of the for statement allowed by OpenMP.  */
33547
33548 static tree
33549 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33550                         tree *cclauses, bool *if_p)
33551 {
33552   tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33553   tree real_decl, initv, condv, incrv, declv;
33554   tree this_pre_body, cl, ordered_cl = NULL_TREE;
33555   location_t loc_first;
33556   bool collapse_err = false;
33557   int i, collapse = 1, ordered = 0, count, nbraces = 0;
33558   vec<tree, va_gc> *for_block = make_tree_vector ();
33559   auto_vec<tree, 4> orig_inits;
33560
33561   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33562     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33563       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33564     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33565              && OMP_CLAUSE_ORDERED_EXPR (cl))
33566       {
33567         ordered_cl = cl;
33568         ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33569       }
33570
33571   if (ordered && ordered < collapse)
33572     {
33573       error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33574                 "%<ordered%> clause parameter is less than %<collapse%>");
33575       OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33576         = build_int_cst (NULL_TREE, collapse);
33577       ordered = collapse;
33578     }
33579   if (ordered)
33580     {
33581       for (tree *pc = &clauses; *pc; )
33582         if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33583           {
33584             error_at (OMP_CLAUSE_LOCATION (*pc),
33585                       "%<linear%> clause may not be specified together "
33586                       "with %<ordered%> clause with a parameter");
33587             *pc = OMP_CLAUSE_CHAIN (*pc);
33588           }
33589         else
33590           pc = &OMP_CLAUSE_CHAIN (*pc);
33591     }
33592
33593   gcc_assert (collapse >= 1 && ordered >= 0);
33594   count = ordered ? ordered : collapse;
33595
33596   declv = make_tree_vec (count);
33597   initv = make_tree_vec (count);
33598   condv = make_tree_vec (count);
33599   incrv = make_tree_vec (count);
33600
33601   loc_first = cp_lexer_peek_token (parser->lexer)->location;
33602
33603   for (i = 0; i < count; i++)
33604     {
33605       int bracecount = 0;
33606       tree add_private_clause = NULL_TREE;
33607       location_t loc;
33608
33609       if (code != CILK_FOR
33610           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33611         {
33612           cp_parser_error (parser, "for statement expected");
33613           return NULL;
33614         }
33615       if (code == CILK_FOR
33616           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33617         {
33618           cp_parser_error (parser, "_Cilk_for statement expected");
33619           return NULL;
33620         }
33621       loc = cp_lexer_consume_token (parser->lexer)->location;
33622
33623       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33624         return NULL;
33625
33626       init = orig_init = decl = real_decl = NULL;
33627       this_pre_body = push_stmt_list ();
33628
33629       add_private_clause
33630         = cp_parser_omp_for_loop_init (parser, code,
33631                                        this_pre_body, for_block,
33632                                        init, orig_init, decl, real_decl);
33633
33634       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33635       if (this_pre_body)
33636         {
33637           this_pre_body = pop_stmt_list (this_pre_body);
33638           if (pre_body)
33639             {
33640               tree t = pre_body;
33641               pre_body = push_stmt_list ();
33642               add_stmt (t);
33643               add_stmt (this_pre_body);
33644               pre_body = pop_stmt_list (pre_body);
33645             }
33646           else
33647             pre_body = this_pre_body;
33648         }
33649
33650       if (decl)
33651         real_decl = decl;
33652       if (cclauses != NULL
33653           && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33654           && real_decl != NULL_TREE)
33655         {
33656           tree *c;
33657           for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33658             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33659                 && OMP_CLAUSE_DECL (*c) == real_decl)
33660               {
33661                 error_at (loc, "iteration variable %qD"
33662                           " should not be firstprivate", real_decl);
33663                 *c = OMP_CLAUSE_CHAIN (*c);
33664               }
33665             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33666                      && OMP_CLAUSE_DECL (*c) == real_decl)
33667               {
33668                 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
33669                 tree l = *c;
33670                 *c = OMP_CLAUSE_CHAIN (*c);
33671                 if (code == OMP_SIMD)
33672                   {
33673                     OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33674                     cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33675                   }
33676                 else
33677                   {
33678                     OMP_CLAUSE_CHAIN (l) = clauses;
33679                     clauses = l;
33680                   }
33681                 add_private_clause = NULL_TREE;
33682               }
33683             else
33684               {
33685                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33686                     && OMP_CLAUSE_DECL (*c) == real_decl)
33687                   add_private_clause = NULL_TREE;
33688                 c = &OMP_CLAUSE_CHAIN (*c);
33689               }
33690         }
33691
33692       if (add_private_clause)
33693         {
33694           tree c;
33695           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33696             {
33697               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33698                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33699                   && OMP_CLAUSE_DECL (c) == decl)
33700                 break;
33701               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33702                        && OMP_CLAUSE_DECL (c) == decl)
33703                 error_at (loc, "iteration variable %qD "
33704                           "should not be firstprivate",
33705                           decl);
33706               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33707                        && OMP_CLAUSE_DECL (c) == decl)
33708                 error_at (loc, "iteration variable %qD should not be reduction",
33709                           decl);
33710             }
33711           if (c == NULL)
33712             {
33713               if (code != OMP_SIMD)
33714                 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33715               else if (collapse == 1)
33716                 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33717               else
33718                 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33719               OMP_CLAUSE_DECL (c) = add_private_clause;
33720               c = finish_omp_clauses (c, true);
33721               if (c)
33722                 {
33723                   OMP_CLAUSE_CHAIN (c) = clauses;
33724                   clauses = c;
33725                   /* For linear, signal that we need to fill up
33726                      the so far unknown linear step.  */
33727                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33728                     OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33729                 }
33730             }
33731         }
33732
33733       cond = NULL;
33734       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33735         cond = cp_parser_omp_for_cond (parser, decl, code);
33736       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33737
33738       incr = NULL;
33739       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33740         {
33741           /* If decl is an iterator, preserve the operator on decl
33742              until finish_omp_for.  */
33743           if (real_decl
33744               && ((processing_template_decl
33745                    && (TREE_TYPE (real_decl) == NULL_TREE
33746                        || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33747                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33748             incr = cp_parser_omp_for_incr (parser, real_decl);
33749           else
33750             incr = cp_parser_expression (parser);
33751           if (!EXPR_HAS_LOCATION (incr))
33752             protected_set_expr_location (incr, input_location);
33753         }
33754
33755       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33756         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33757                                                /*or_comma=*/false,
33758                                                /*consume_paren=*/true);
33759
33760       TREE_VEC_ELT (declv, i) = decl;
33761       TREE_VEC_ELT (initv, i) = init;
33762       TREE_VEC_ELT (condv, i) = cond;
33763       TREE_VEC_ELT (incrv, i) = incr;
33764       if (orig_init)
33765         {
33766           orig_inits.safe_grow_cleared (i + 1);
33767           orig_inits[i] = orig_init;
33768         }
33769
33770       if (i == count - 1)
33771         break;
33772
33773       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33774          in between the collapsed for loops to be still considered perfectly
33775          nested.  Hopefully the final version clarifies this.
33776          For now handle (multiple) {'s and empty statements.  */
33777       cp_parser_parse_tentatively (parser);
33778       do
33779         {
33780           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33781             break;
33782           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33783             {
33784               cp_lexer_consume_token (parser->lexer);
33785               bracecount++;
33786             }
33787           else if (bracecount
33788                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33789             cp_lexer_consume_token (parser->lexer);
33790           else
33791             {
33792               loc = cp_lexer_peek_token (parser->lexer)->location;
33793               error_at (loc, "not enough collapsed for loops");
33794               collapse_err = true;
33795               cp_parser_abort_tentative_parse (parser);
33796               declv = NULL_TREE;
33797               break;
33798             }
33799         }
33800       while (1);
33801
33802       if (declv)
33803         {
33804           cp_parser_parse_definitely (parser);
33805           nbraces += bracecount;
33806         }
33807     }
33808
33809   if (nbraces)
33810     if_p = NULL;
33811
33812   /* Note that we saved the original contents of this flag when we entered
33813      the structured block, and so we don't need to re-save it here.  */
33814   if (code == CILK_SIMD || code == CILK_FOR)
33815     parser->in_statement = IN_CILK_SIMD_FOR;
33816   else
33817     parser->in_statement = IN_OMP_FOR;
33818
33819   /* Note that the grammar doesn't call for a structured block here,
33820      though the loop as a whole is a structured block.  */
33821   body = push_stmt_list ();
33822   cp_parser_statement (parser, NULL_TREE, false, if_p);
33823   body = pop_stmt_list (body);
33824
33825   if (declv == NULL_TREE)
33826     ret = NULL_TREE;
33827   else
33828     ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33829                           body, pre_body, &orig_inits, clauses);
33830
33831   while (nbraces)
33832     {
33833       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33834         {
33835           cp_lexer_consume_token (parser->lexer);
33836           nbraces--;
33837         }
33838       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33839         cp_lexer_consume_token (parser->lexer);
33840       else
33841         {
33842           if (!collapse_err)
33843             {
33844               error_at (cp_lexer_peek_token (parser->lexer)->location,
33845                         "collapsed loops not perfectly nested");
33846             }
33847           collapse_err = true;
33848           cp_parser_statement_seq_opt (parser, NULL);
33849           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33850             break;
33851         }
33852     }
33853
33854   while (!for_block->is_empty ())
33855     add_stmt (pop_stmt_list (for_block->pop ()));
33856   release_tree_vector (for_block);
33857
33858   return ret;
33859 }
33860
33861 /* Helper function for OpenMP parsing, split clauses and call
33862    finish_omp_clauses on each of the set of clauses afterwards.  */
33863
33864 static void
33865 cp_omp_split_clauses (location_t loc, enum tree_code code,
33866                       omp_clause_mask mask, tree clauses, tree *cclauses)
33867 {
33868   int i;
33869   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33870   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33871     if (cclauses[i])
33872       cclauses[i] = finish_omp_clauses (cclauses[i], true);
33873 }
33874
33875 /* OpenMP 4.0:
33876    #pragma omp simd simd-clause[optseq] new-line
33877      for-loop  */
33878
33879 #define OMP_SIMD_CLAUSE_MASK                                    \
33880         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)      \
33881         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
33882         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
33883         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
33884         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
33885         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
33886         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
33887         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33888
33889 static tree
33890 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33891                     char *p_name, omp_clause_mask mask, tree *cclauses,
33892                     bool *if_p)
33893 {
33894   tree clauses, sb, ret;
33895   unsigned int save;
33896   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33897
33898   strcat (p_name, " simd");
33899   mask |= OMP_SIMD_CLAUSE_MASK;
33900
33901   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33902                                        cclauses == NULL);
33903   if (cclauses)
33904     {
33905       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33906       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33907       tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33908                                 OMP_CLAUSE_ORDERED);
33909       if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33910         {
33911           error_at (OMP_CLAUSE_LOCATION (c),
33912                     "%<ordered%> clause with parameter may not be specified "
33913                     "on %qs construct", p_name);
33914           OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33915         }
33916     }
33917
33918   sb = begin_omp_structured_block ();
33919   save = cp_parser_begin_omp_structured_block (parser);
33920
33921   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33922
33923   cp_parser_end_omp_structured_block (parser, save);
33924   add_stmt (finish_omp_structured_block (sb));
33925
33926   return ret;
33927 }
33928
33929 /* OpenMP 2.5:
33930    #pragma omp for for-clause[optseq] new-line
33931      for-loop
33932
33933    OpenMP 4.0:
33934    #pragma omp for simd for-simd-clause[optseq] new-line
33935      for-loop  */
33936
33937 #define OMP_FOR_CLAUSE_MASK                                     \
33938         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
33939         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33940         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
33941         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
33942         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
33943         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)      \
33944         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)     \
33945         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
33946         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33947
33948 static tree
33949 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33950                    char *p_name, omp_clause_mask mask, tree *cclauses,
33951                    bool *if_p)
33952 {
33953   tree clauses, sb, ret;
33954   unsigned int save;
33955   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33956
33957   strcat (p_name, " for");
33958   mask |= OMP_FOR_CLAUSE_MASK;
33959   /* parallel for{, simd} disallows nowait clause, but for
33960      target {teams distribute ,}parallel for{, simd} it should be accepted.  */
33961   if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
33962     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33963   /* Composite distribute parallel for{, simd} disallows ordered clause.  */
33964   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33965     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33966
33967   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33968     {
33969       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33970       const char *p = IDENTIFIER_POINTER (id);
33971
33972       if (strcmp (p, "simd") == 0)
33973         {
33974           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33975           if (cclauses == NULL)
33976             cclauses = cclauses_buf;
33977
33978           cp_lexer_consume_token (parser->lexer);
33979           if (!flag_openmp)  /* flag_openmp_simd  */
33980             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33981                                        cclauses, if_p);
33982           sb = begin_omp_structured_block ();
33983           save = cp_parser_begin_omp_structured_block (parser);
33984           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33985                                     cclauses, if_p);
33986           cp_parser_end_omp_structured_block (parser, save);
33987           tree body = finish_omp_structured_block (sb);
33988           if (ret == NULL)
33989             return ret;
33990           ret = make_node (OMP_FOR);
33991           TREE_TYPE (ret) = void_type_node;
33992           OMP_FOR_BODY (ret) = body;
33993           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33994           SET_EXPR_LOCATION (ret, loc);
33995           add_stmt (ret);
33996           return ret;
33997         }
33998     }
33999   if (!flag_openmp)  /* flag_openmp_simd  */
34000     {
34001       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34002       return NULL_TREE;
34003     }
34004
34005   /* Composite distribute parallel for disallows linear clause.  */
34006   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34007     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34008
34009   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34010                                        cclauses == NULL);
34011   if (cclauses)
34012     {
34013       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34014       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34015     }
34016
34017   sb = begin_omp_structured_block ();
34018   save = cp_parser_begin_omp_structured_block (parser);
34019
34020   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34021
34022   cp_parser_end_omp_structured_block (parser, save);
34023   add_stmt (finish_omp_structured_block (sb));
34024
34025   return ret;
34026 }
34027
34028 /* OpenMP 2.5:
34029    # pragma omp master new-line
34030      structured-block  */
34031
34032 static tree
34033 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34034 {
34035   cp_parser_require_pragma_eol (parser, pragma_tok);
34036   return c_finish_omp_master (input_location,
34037                               cp_parser_omp_structured_block (parser, if_p));
34038 }
34039
34040 /* OpenMP 2.5:
34041    # pragma omp ordered new-line
34042      structured-block
34043
34044    OpenMP 4.5:
34045    # pragma omp ordered ordered-clauses new-line
34046      structured-block  */
34047
34048 #define OMP_ORDERED_CLAUSE_MASK                                 \
34049         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)      \
34050         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34051
34052 #define OMP_ORDERED_DEPEND_CLAUSE_MASK                          \
34053         (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34054
34055 static bool
34056 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34057                        enum pragma_context context, bool *if_p)
34058 {
34059   location_t loc = pragma_tok->location;
34060
34061   if (context != pragma_stmt && context != pragma_compound)
34062     {
34063       cp_parser_error (parser, "expected declaration specifiers");
34064       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34065       return false;
34066     }
34067
34068   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34069     {
34070       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34071       const char *p = IDENTIFIER_POINTER (id);
34072
34073       if (strcmp (p, "depend") == 0)
34074         {
34075           if (context == pragma_stmt)
34076             {
34077               error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34078                         "%<depend%> clause may only be used in compound "
34079                         "statements");
34080               cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34081               return false;
34082             }
34083           tree clauses
34084             = cp_parser_omp_all_clauses (parser,
34085                                          OMP_ORDERED_DEPEND_CLAUSE_MASK,
34086                                          "#pragma omp ordered", pragma_tok);
34087           c_finish_omp_ordered (loc, clauses, NULL_TREE);
34088           return false;
34089         }
34090     }
34091
34092   tree clauses
34093     = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34094                                  "#pragma omp ordered", pragma_tok);
34095   c_finish_omp_ordered (loc, clauses,
34096                         cp_parser_omp_structured_block (parser, if_p));
34097   return true;
34098 }
34099
34100 /* OpenMP 2.5:
34101
34102    section-scope:
34103      { section-sequence }
34104
34105    section-sequence:
34106      section-directive[opt] structured-block
34107      section-sequence section-directive structured-block  */
34108
34109 static tree
34110 cp_parser_omp_sections_scope (cp_parser *parser)
34111 {
34112   tree stmt, substmt;
34113   bool error_suppress = false;
34114   cp_token *tok;
34115
34116   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34117     return NULL_TREE;
34118
34119   stmt = push_stmt_list ();
34120
34121   if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34122       != PRAGMA_OMP_SECTION)
34123     {
34124       substmt = cp_parser_omp_structured_block (parser, NULL);
34125       substmt = build1 (OMP_SECTION, void_type_node, substmt);
34126       add_stmt (substmt);
34127     }
34128
34129   while (1)
34130     {
34131       tok = cp_lexer_peek_token (parser->lexer);
34132       if (tok->type == CPP_CLOSE_BRACE)
34133         break;
34134       if (tok->type == CPP_EOF)
34135         break;
34136
34137       if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34138         {
34139           cp_lexer_consume_token (parser->lexer);
34140           cp_parser_require_pragma_eol (parser, tok);
34141           error_suppress = false;
34142         }
34143       else if (!error_suppress)
34144         {
34145           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34146           error_suppress = true;
34147         }
34148
34149       substmt = cp_parser_omp_structured_block (parser, NULL);
34150       substmt = build1 (OMP_SECTION, void_type_node, substmt);
34151       add_stmt (substmt);
34152     }
34153   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34154
34155   substmt = pop_stmt_list (stmt);
34156
34157   stmt = make_node (OMP_SECTIONS);
34158   TREE_TYPE (stmt) = void_type_node;
34159   OMP_SECTIONS_BODY (stmt) = substmt;
34160
34161   add_stmt (stmt);
34162   return stmt;
34163 }
34164
34165 /* OpenMP 2.5:
34166    # pragma omp sections sections-clause[optseq] newline
34167      sections-scope  */
34168
34169 #define OMP_SECTIONS_CLAUSE_MASK                                \
34170         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34171         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34172         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
34173         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
34174         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34175
34176 static tree
34177 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34178                         char *p_name, omp_clause_mask mask, tree *cclauses)
34179 {
34180   tree clauses, ret;
34181   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34182
34183   strcat (p_name, " sections");
34184   mask |= OMP_SECTIONS_CLAUSE_MASK;
34185   if (cclauses)
34186     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34187
34188   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34189                                        cclauses == NULL);
34190   if (cclauses)
34191     {
34192       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34193       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34194     }
34195
34196   ret = cp_parser_omp_sections_scope (parser);
34197   if (ret)
34198     OMP_SECTIONS_CLAUSES (ret) = clauses;
34199
34200   return ret;
34201 }
34202
34203 /* OpenMP 2.5:
34204    # pragma omp parallel parallel-clause[optseq] new-line
34205      structured-block
34206    # pragma omp parallel for parallel-for-clause[optseq] new-line
34207      structured-block
34208    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34209      structured-block
34210
34211    OpenMP 4.0:
34212    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34213      structured-block */
34214
34215 #define OMP_PARALLEL_CLAUSE_MASK                                \
34216         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34217         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34218         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34219         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
34220         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
34221         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)       \
34222         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
34223         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)  \
34224         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34225
34226 static tree
34227 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34228                         char *p_name, omp_clause_mask mask, tree *cclauses,
34229                         bool *if_p)
34230 {
34231   tree stmt, clauses, block;
34232   unsigned int save;
34233   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34234
34235   strcat (p_name, " parallel");
34236   mask |= OMP_PARALLEL_CLAUSE_MASK;
34237   /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
34238   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34239       && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34240     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34241
34242   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34243     {
34244       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34245       if (cclauses == NULL)
34246         cclauses = cclauses_buf;
34247
34248       cp_lexer_consume_token (parser->lexer);
34249       if (!flag_openmp)  /* flag_openmp_simd  */
34250         return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34251                                   if_p);
34252       block = begin_omp_parallel ();
34253       save = cp_parser_begin_omp_structured_block (parser);
34254       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34255                                     if_p);
34256       cp_parser_end_omp_structured_block (parser, save);
34257       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34258                                   block);
34259       if (ret == NULL_TREE)
34260         return ret;
34261       OMP_PARALLEL_COMBINED (stmt) = 1;
34262       return stmt;
34263     }
34264   /* When combined with distribute, parallel has to be followed by for.
34265      #pragma omp target parallel is allowed though.  */
34266   else if (cclauses
34267            && (mask & (OMP_CLAUSE_MASK_1
34268                        << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34269     {
34270       error_at (loc, "expected %<for%> after %qs", p_name);
34271       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34272       return NULL_TREE;
34273     }
34274   else if (!flag_openmp)  /* flag_openmp_simd  */
34275     {
34276       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34277       return NULL_TREE;
34278     }
34279   else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34280     {
34281       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34282       const char *p = IDENTIFIER_POINTER (id);
34283       if (strcmp (p, "sections") == 0)
34284         {
34285           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34286           cclauses = cclauses_buf;
34287
34288           cp_lexer_consume_token (parser->lexer);
34289           block = begin_omp_parallel ();
34290           save = cp_parser_begin_omp_structured_block (parser);
34291           cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34292           cp_parser_end_omp_structured_block (parser, save);
34293           stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34294                                       block);
34295           OMP_PARALLEL_COMBINED (stmt) = 1;
34296           return stmt;
34297         }
34298     }
34299
34300   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34301                                        cclauses == NULL);
34302   if (cclauses)
34303     {
34304       cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34305       clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34306     }
34307
34308   block = begin_omp_parallel ();
34309   save = cp_parser_begin_omp_structured_block (parser);
34310   cp_parser_statement (parser, NULL_TREE, false, if_p);
34311   cp_parser_end_omp_structured_block (parser, save);
34312   stmt = finish_omp_parallel (clauses, block);
34313   return stmt;
34314 }
34315
34316 /* OpenMP 2.5:
34317    # pragma omp single single-clause[optseq] new-line
34318      structured-block  */
34319
34320 #define OMP_SINGLE_CLAUSE_MASK                                  \
34321         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34322         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34323         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)  \
34324         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34325
34326 static tree
34327 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34328 {
34329   tree stmt = make_node (OMP_SINGLE);
34330   TREE_TYPE (stmt) = void_type_node;
34331
34332   OMP_SINGLE_CLAUSES (stmt)
34333     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34334                                  "#pragma omp single", pragma_tok);
34335   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34336
34337   return add_stmt (stmt);
34338 }
34339
34340 /* OpenMP 3.0:
34341    # pragma omp task task-clause[optseq] new-line
34342      structured-block  */
34343
34344 #define OMP_TASK_CLAUSE_MASK                                    \
34345         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34346         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
34347         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
34348         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34349         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34350         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
34351         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
34352         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
34353         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34354         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34355
34356 static tree
34357 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34358 {
34359   tree clauses, block;
34360   unsigned int save;
34361
34362   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34363                                        "#pragma omp task", pragma_tok);
34364   block = begin_omp_task ();
34365   save = cp_parser_begin_omp_structured_block (parser);
34366   cp_parser_statement (parser, NULL_TREE, false, if_p);
34367   cp_parser_end_omp_structured_block (parser, save);
34368   return finish_omp_task (clauses, block);
34369 }
34370
34371 /* OpenMP 3.0:
34372    # pragma omp taskwait new-line  */
34373
34374 static void
34375 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34376 {
34377   cp_parser_require_pragma_eol (parser, pragma_tok);
34378   finish_omp_taskwait ();
34379 }
34380
34381 /* OpenMP 3.1:
34382    # pragma omp taskyield new-line  */
34383
34384 static void
34385 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34386 {
34387   cp_parser_require_pragma_eol (parser, pragma_tok);
34388   finish_omp_taskyield ();
34389 }
34390
34391 /* OpenMP 4.0:
34392    # pragma omp taskgroup new-line
34393      structured-block  */
34394
34395 static tree
34396 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34397 {
34398   cp_parser_require_pragma_eol (parser, pragma_tok);
34399   return c_finish_omp_taskgroup (input_location,
34400                                  cp_parser_omp_structured_block (parser,
34401                                                                  if_p));
34402 }
34403
34404
34405 /* OpenMP 2.5:
34406    # pragma omp threadprivate (variable-list) */
34407
34408 static void
34409 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34410 {
34411   tree vars;
34412
34413   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34414   cp_parser_require_pragma_eol (parser, pragma_tok);
34415
34416   finish_omp_threadprivate (vars);
34417 }
34418
34419 /* OpenMP 4.0:
34420    # pragma omp cancel cancel-clause[optseq] new-line  */
34421
34422 #define OMP_CANCEL_CLAUSE_MASK                                  \
34423         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
34424         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
34425         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
34426         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)    \
34427         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34428
34429 static void
34430 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34431 {
34432   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34433                                             "#pragma omp cancel", pragma_tok);
34434   finish_omp_cancel (clauses);
34435 }
34436
34437 /* OpenMP 4.0:
34438    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
34439
34440 #define OMP_CANCELLATION_POINT_CLAUSE_MASK                      \
34441         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
34442         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
34443         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
34444         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34445
34446 static void
34447 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34448 {
34449   tree clauses;
34450   bool point_seen = false;
34451
34452   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34453     {
34454       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34455       const char *p = IDENTIFIER_POINTER (id);
34456
34457       if (strcmp (p, "point") == 0)
34458         {
34459           cp_lexer_consume_token (parser->lexer);
34460           point_seen = true;
34461         }
34462     }
34463   if (!point_seen)
34464     {
34465       cp_parser_error (parser, "expected %<point%>");
34466       cp_parser_require_pragma_eol (parser, pragma_tok);
34467       return;
34468     }
34469
34470   clauses = cp_parser_omp_all_clauses (parser,
34471                                        OMP_CANCELLATION_POINT_CLAUSE_MASK,
34472                                        "#pragma omp cancellation point",
34473                                        pragma_tok);
34474   finish_omp_cancellation_point (clauses);
34475 }
34476
34477 /* OpenMP 4.0:
34478    #pragma omp distribute distribute-clause[optseq] new-line
34479      for-loop  */
34480
34481 #define OMP_DISTRIBUTE_CLAUSE_MASK                              \
34482         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34483         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34484         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
34485         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34486         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34487
34488 static tree
34489 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34490                           char *p_name, omp_clause_mask mask, tree *cclauses,
34491                           bool *if_p)
34492 {
34493   tree clauses, sb, ret;
34494   unsigned int save;
34495   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34496
34497   strcat (p_name, " distribute");
34498   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34499
34500   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34501     {
34502       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34503       const char *p = IDENTIFIER_POINTER (id);
34504       bool simd = false;
34505       bool parallel = false;
34506
34507       if (strcmp (p, "simd") == 0)
34508         simd = true;
34509       else
34510         parallel = strcmp (p, "parallel") == 0;
34511       if (parallel || simd)
34512         {
34513           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34514           if (cclauses == NULL)
34515             cclauses = cclauses_buf;
34516           cp_lexer_consume_token (parser->lexer);
34517           if (!flag_openmp)  /* flag_openmp_simd  */
34518             {
34519               if (simd)
34520                 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34521                                            cclauses, if_p);
34522               else
34523                 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34524                                                cclauses, if_p);
34525             }
34526           sb = begin_omp_structured_block ();
34527           save = cp_parser_begin_omp_structured_block (parser);
34528           if (simd)
34529             ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34530                                       cclauses, if_p);
34531           else
34532             ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34533                                           cclauses, if_p);
34534           cp_parser_end_omp_structured_block (parser, save);
34535           tree body = finish_omp_structured_block (sb);
34536           if (ret == NULL)
34537             return ret;
34538           ret = make_node (OMP_DISTRIBUTE);
34539           TREE_TYPE (ret) = void_type_node;
34540           OMP_FOR_BODY (ret) = body;
34541           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34542           SET_EXPR_LOCATION (ret, loc);
34543           add_stmt (ret);
34544           return ret;
34545         }
34546     }
34547   if (!flag_openmp)  /* flag_openmp_simd  */
34548     {
34549       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34550       return NULL_TREE;
34551     }
34552
34553   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34554                                        cclauses == NULL);
34555   if (cclauses)
34556     {
34557       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34558       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34559     }
34560
34561   sb = begin_omp_structured_block ();
34562   save = cp_parser_begin_omp_structured_block (parser);
34563
34564   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34565
34566   cp_parser_end_omp_structured_block (parser, save);
34567   add_stmt (finish_omp_structured_block (sb));
34568
34569   return ret;
34570 }
34571
34572 /* OpenMP 4.0:
34573    # pragma omp teams teams-clause[optseq] new-line
34574      structured-block  */
34575
34576 #define OMP_TEAMS_CLAUSE_MASK                                   \
34577         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34578         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34579         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
34580         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
34581         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)    \
34582         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34583         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34584
34585 static tree
34586 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34587                      char *p_name, omp_clause_mask mask, tree *cclauses,
34588                      bool *if_p)
34589 {
34590   tree clauses, sb, ret;
34591   unsigned int save;
34592   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34593
34594   strcat (p_name, " teams");
34595   mask |= OMP_TEAMS_CLAUSE_MASK;
34596
34597   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34598     {
34599       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34600       const char *p = IDENTIFIER_POINTER (id);
34601       if (strcmp (p, "distribute") == 0)
34602         {
34603           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34604           if (cclauses == NULL)
34605             cclauses = cclauses_buf;
34606
34607           cp_lexer_consume_token (parser->lexer);
34608           if (!flag_openmp)  /* flag_openmp_simd  */
34609             return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34610                                              cclauses, if_p);
34611           sb = begin_omp_structured_block ();
34612           save = cp_parser_begin_omp_structured_block (parser);
34613           ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34614                                           cclauses, if_p);
34615           cp_parser_end_omp_structured_block (parser, save);
34616           tree body = finish_omp_structured_block (sb);
34617           if (ret == NULL)
34618             return ret;
34619           clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34620           ret = make_node (OMP_TEAMS);
34621           TREE_TYPE (ret) = void_type_node;
34622           OMP_TEAMS_CLAUSES (ret) = clauses;
34623           OMP_TEAMS_BODY (ret) = body;
34624           OMP_TEAMS_COMBINED (ret) = 1;
34625           return add_stmt (ret);
34626         }
34627     }
34628   if (!flag_openmp)  /* flag_openmp_simd  */
34629     {
34630       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34631       return NULL_TREE;
34632     }
34633
34634   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34635                                        cclauses == NULL);
34636   if (cclauses)
34637     {
34638       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34639       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34640     }
34641
34642   tree stmt = make_node (OMP_TEAMS);
34643   TREE_TYPE (stmt) = void_type_node;
34644   OMP_TEAMS_CLAUSES (stmt) = clauses;
34645   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34646
34647   return add_stmt (stmt);
34648 }
34649
34650 /* OpenMP 4.0:
34651    # pragma omp target data target-data-clause[optseq] new-line
34652      structured-block  */
34653
34654 #define OMP_TARGET_DATA_CLAUSE_MASK                             \
34655         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34656         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34657         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34658         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34659
34660 static tree
34661 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34662 {
34663   tree clauses
34664     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34665                                  "#pragma omp target data", pragma_tok);
34666   int map_seen = 0;
34667   for (tree *pc = &clauses; *pc;)
34668     {
34669       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34670         switch (OMP_CLAUSE_MAP_KIND (*pc))
34671           {
34672           case GOMP_MAP_TO:
34673           case GOMP_MAP_ALWAYS_TO:
34674           case GOMP_MAP_FROM:
34675           case GOMP_MAP_ALWAYS_FROM:
34676           case GOMP_MAP_TOFROM:
34677           case GOMP_MAP_ALWAYS_TOFROM:
34678           case GOMP_MAP_ALLOC:
34679             map_seen = 3;
34680             break;
34681           case GOMP_MAP_FIRSTPRIVATE_POINTER:
34682           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34683           case GOMP_MAP_ALWAYS_POINTER:
34684             break;
34685           default:
34686             map_seen |= 1;
34687             error_at (OMP_CLAUSE_LOCATION (*pc),
34688                       "%<#pragma omp target data%> with map-type other "
34689                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34690                       "on %<map%> clause");
34691             *pc = OMP_CLAUSE_CHAIN (*pc);
34692             continue;
34693           }
34694       pc = &OMP_CLAUSE_CHAIN (*pc);
34695     }
34696
34697   if (map_seen != 3)
34698     {
34699       if (map_seen == 0)
34700         error_at (pragma_tok->location,
34701                   "%<#pragma omp target data%> must contain at least "
34702                   "one %<map%> clause");
34703       return NULL_TREE;
34704     }
34705
34706   tree stmt = make_node (OMP_TARGET_DATA);
34707   TREE_TYPE (stmt) = void_type_node;
34708   OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34709
34710   keep_next_level (true);
34711   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34712
34713   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34714   return add_stmt (stmt);
34715 }
34716
34717 /* OpenMP 4.5:
34718    # pragma omp target enter data target-enter-data-clause[optseq] new-line
34719      structured-block  */
34720
34721 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK                       \
34722         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34723         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34724         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34725         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34726         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34727
34728 static tree
34729 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34730                                  enum pragma_context context)
34731 {
34732   bool data_seen = false;
34733   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34734     {
34735       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34736       const char *p = IDENTIFIER_POINTER (id);
34737
34738       if (strcmp (p, "data") == 0)
34739         {
34740           cp_lexer_consume_token (parser->lexer);
34741           data_seen = true;
34742         }
34743     }
34744   if (!data_seen)
34745     {
34746       cp_parser_error (parser, "expected %<data%>");
34747       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34748       return NULL_TREE;
34749     }
34750
34751   if (context == pragma_stmt)
34752     {
34753       error_at (pragma_tok->location,
34754                 "%<#pragma omp target enter data%> may only be "
34755                 "used in compound statements");
34756       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34757       return NULL_TREE;
34758     }
34759
34760   tree clauses
34761     = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34762                                  "#pragma omp target enter data", pragma_tok);
34763   int map_seen = 0;
34764   for (tree *pc = &clauses; *pc;)
34765     {
34766       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34767         switch (OMP_CLAUSE_MAP_KIND (*pc))
34768           {
34769           case GOMP_MAP_TO:
34770           case GOMP_MAP_ALWAYS_TO:
34771           case GOMP_MAP_ALLOC:
34772             map_seen = 3;
34773             break;
34774           case GOMP_MAP_FIRSTPRIVATE_POINTER:
34775           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34776           case GOMP_MAP_ALWAYS_POINTER:
34777             break;
34778           default:
34779             map_seen |= 1;
34780             error_at (OMP_CLAUSE_LOCATION (*pc),
34781                       "%<#pragma omp target enter data%> with map-type other "
34782                       "than %<to%> or %<alloc%> on %<map%> clause");
34783             *pc = OMP_CLAUSE_CHAIN (*pc);
34784             continue;
34785           }
34786       pc = &OMP_CLAUSE_CHAIN (*pc);
34787     }
34788
34789   if (map_seen != 3)
34790     {
34791       if (map_seen == 0)
34792         error_at (pragma_tok->location,
34793                   "%<#pragma omp target enter data%> must contain at least "
34794                   "one %<map%> clause");
34795       return NULL_TREE;
34796     }
34797
34798   tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34799   TREE_TYPE (stmt) = void_type_node;
34800   OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34801   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34802   return add_stmt (stmt);
34803 }
34804
34805 /* OpenMP 4.5:
34806    # pragma omp target exit data target-enter-data-clause[optseq] new-line
34807      structured-block  */
34808
34809 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK                        \
34810         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34811         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34812         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34813         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34814         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34815
34816 static tree
34817 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34818                                 enum pragma_context context)
34819 {
34820   bool data_seen = false;
34821   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34822     {
34823       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34824       const char *p = IDENTIFIER_POINTER (id);
34825
34826       if (strcmp (p, "data") == 0)
34827         {
34828           cp_lexer_consume_token (parser->lexer);
34829           data_seen = true;
34830         }
34831     }
34832   if (!data_seen)
34833     {
34834       cp_parser_error (parser, "expected %<data%>");
34835       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34836       return NULL_TREE;
34837     }
34838
34839   if (context == pragma_stmt)
34840     {
34841       error_at (pragma_tok->location,
34842                 "%<#pragma omp target exit data%> may only be "
34843                 "used in compound statements");
34844       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34845       return NULL_TREE;
34846     }
34847
34848   tree clauses
34849     = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34850                                  "#pragma omp target exit data", pragma_tok);
34851   int map_seen = 0;
34852   for (tree *pc = &clauses; *pc;)
34853     {
34854       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34855         switch (OMP_CLAUSE_MAP_KIND (*pc))
34856           {
34857           case GOMP_MAP_FROM:
34858           case GOMP_MAP_ALWAYS_FROM:
34859           case GOMP_MAP_RELEASE:
34860           case GOMP_MAP_DELETE:
34861             map_seen = 3;
34862             break;
34863           case GOMP_MAP_FIRSTPRIVATE_POINTER:
34864           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34865           case GOMP_MAP_ALWAYS_POINTER:
34866             break;
34867           default:
34868             map_seen |= 1;
34869             error_at (OMP_CLAUSE_LOCATION (*pc),
34870                       "%<#pragma omp target exit data%> with map-type other "
34871                       "than %<from%>, %<release%> or %<delete%> on %<map%>"
34872                       " clause");
34873             *pc = OMP_CLAUSE_CHAIN (*pc);
34874             continue;
34875           }
34876       pc = &OMP_CLAUSE_CHAIN (*pc);
34877     }
34878
34879   if (map_seen != 3)
34880     {
34881       if (map_seen == 0)
34882         error_at (pragma_tok->location,
34883                   "%<#pragma omp target exit data%> must contain at least "
34884                   "one %<map%> clause");
34885       return NULL_TREE;
34886     }
34887
34888   tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34889   TREE_TYPE (stmt) = void_type_node;
34890   OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34891   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34892   return add_stmt (stmt);
34893 }
34894
34895 /* OpenMP 4.0:
34896    # pragma omp target update target-update-clause[optseq] new-line */
34897
34898 #define OMP_TARGET_UPDATE_CLAUSE_MASK                           \
34899         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)         \
34900         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
34901         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34902         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34903         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34904         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34905
34906 static bool
34907 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34908                              enum pragma_context context)
34909 {
34910   if (context == pragma_stmt)
34911     {
34912       error_at (pragma_tok->location,
34913                 "%<#pragma omp target update%> may only be "
34914                 "used in compound statements");
34915       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34916       return false;
34917     }
34918
34919   tree clauses
34920     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34921                                  "#pragma omp target update", pragma_tok);
34922   if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34923       && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34924     {
34925       error_at (pragma_tok->location,
34926                 "%<#pragma omp target update%> must contain at least one "
34927                 "%<from%> or %<to%> clauses");
34928       return false;
34929     }
34930
34931   tree stmt = make_node (OMP_TARGET_UPDATE);
34932   TREE_TYPE (stmt) = void_type_node;
34933   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34934   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34935   add_stmt (stmt);
34936   return false;
34937 }
34938
34939 /* OpenMP 4.0:
34940    # pragma omp target target-clause[optseq] new-line
34941      structured-block  */
34942
34943 #define OMP_TARGET_CLAUSE_MASK                                  \
34944         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34945         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34946         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34947         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34948         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
34949         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34950         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34951         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)   \
34952         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34953
34954 static bool
34955 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34956                       enum pragma_context context, bool *if_p)
34957 {
34958   tree *pc = NULL, stmt;
34959
34960   if (context != pragma_stmt && context != pragma_compound)
34961     {
34962       cp_parser_error (parser, "expected declaration specifiers");
34963       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34964       return false;
34965     }
34966
34967   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34968     {
34969       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34970       const char *p = IDENTIFIER_POINTER (id);
34971       enum tree_code ccode = ERROR_MARK;
34972
34973       if (strcmp (p, "teams") == 0)
34974         ccode = OMP_TEAMS;
34975       else if (strcmp (p, "parallel") == 0)
34976         ccode = OMP_PARALLEL;
34977       else if (strcmp (p, "simd") == 0)
34978         ccode = OMP_SIMD;
34979       if (ccode != ERROR_MARK)
34980         {
34981           tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34982           char p_name[sizeof ("#pragma omp target teams distribute "
34983                               "parallel for simd")];
34984
34985           cp_lexer_consume_token (parser->lexer);
34986           strcpy (p_name, "#pragma omp target");
34987           if (!flag_openmp)  /* flag_openmp_simd  */
34988             {
34989               tree stmt;
34990               switch (ccode)
34991                 {
34992                 case OMP_TEAMS:
34993                   stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34994                                               OMP_TARGET_CLAUSE_MASK,
34995                                               cclauses, if_p);
34996                   break;
34997                 case OMP_PARALLEL:
34998                   stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34999                                                  OMP_TARGET_CLAUSE_MASK,
35000                                                  cclauses, if_p);
35001                   break;
35002                 case OMP_SIMD:
35003                   stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35004                                              OMP_TARGET_CLAUSE_MASK,
35005                                              cclauses, if_p);
35006                   break;
35007                 default:
35008                   gcc_unreachable ();
35009                 }
35010               return stmt != NULL_TREE;
35011             }
35012           keep_next_level (true);
35013           tree sb = begin_omp_structured_block (), ret;
35014           unsigned save = cp_parser_begin_omp_structured_block (parser);
35015           switch (ccode)
35016             {
35017             case OMP_TEAMS:
35018               ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35019                                          OMP_TARGET_CLAUSE_MASK, cclauses,
35020                                          if_p);
35021               break;
35022             case OMP_PARALLEL:
35023               ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35024                                             OMP_TARGET_CLAUSE_MASK, cclauses,
35025                                             if_p);
35026               break;
35027             case OMP_SIMD:
35028               ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35029                                         OMP_TARGET_CLAUSE_MASK, cclauses,
35030                                         if_p);
35031               break;
35032             default:
35033               gcc_unreachable ();
35034             }
35035           cp_parser_end_omp_structured_block (parser, save);
35036           tree body = finish_omp_structured_block (sb);
35037           if (ret == NULL_TREE)
35038             return false;
35039           if (ccode == OMP_TEAMS && !processing_template_decl)
35040             {
35041               /* For combined target teams, ensure the num_teams and
35042                  thread_limit clause expressions are evaluated on the host,
35043                  before entering the target construct.  */
35044               tree c;
35045               for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35046                    c; c = OMP_CLAUSE_CHAIN (c))
35047                 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35048                      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35049                     && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35050                   {
35051                     tree expr = OMP_CLAUSE_OPERAND (c, 0);
35052                     expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35053                     if (expr == error_mark_node)
35054                       continue;
35055                     tree tmp = TARGET_EXPR_SLOT (expr);
35056                     add_stmt (expr);
35057                     OMP_CLAUSE_OPERAND (c, 0) = expr;
35058                     tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35059                                                 OMP_CLAUSE_FIRSTPRIVATE);
35060                     OMP_CLAUSE_DECL (tc) = tmp;
35061                     OMP_CLAUSE_CHAIN (tc)
35062                       = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35063                     cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35064                   }
35065             }
35066           tree stmt = make_node (OMP_TARGET);
35067           TREE_TYPE (stmt) = void_type_node;
35068           OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35069           OMP_TARGET_BODY (stmt) = body;
35070           OMP_TARGET_COMBINED (stmt) = 1;
35071           add_stmt (stmt);
35072           pc = &OMP_TARGET_CLAUSES (stmt);
35073           goto check_clauses;
35074         }
35075       else if (!flag_openmp)  /* flag_openmp_simd  */
35076         {
35077           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35078           return false;
35079         }
35080       else if (strcmp (p, "data") == 0)
35081         {
35082           cp_lexer_consume_token (parser->lexer);
35083           cp_parser_omp_target_data (parser, pragma_tok, if_p);
35084           return true;
35085         }
35086       else if (strcmp (p, "enter") == 0)
35087         {
35088           cp_lexer_consume_token (parser->lexer);
35089           cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35090           return false;
35091         }
35092       else if (strcmp (p, "exit") == 0)
35093         {
35094           cp_lexer_consume_token (parser->lexer);
35095           cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35096           return false;
35097         }
35098       else if (strcmp (p, "update") == 0)
35099         {
35100           cp_lexer_consume_token (parser->lexer);
35101           return cp_parser_omp_target_update (parser, pragma_tok, context);
35102         }
35103     }
35104
35105   stmt = make_node (OMP_TARGET);
35106   TREE_TYPE (stmt) = void_type_node;
35107
35108   OMP_TARGET_CLAUSES (stmt)
35109     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35110                                  "#pragma omp target", pragma_tok);
35111   pc = &OMP_TARGET_CLAUSES (stmt);
35112   keep_next_level (true);
35113   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35114
35115   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35116   add_stmt (stmt);
35117
35118 check_clauses:
35119   while (*pc)
35120     {
35121       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35122         switch (OMP_CLAUSE_MAP_KIND (*pc))
35123           {
35124           case GOMP_MAP_TO:
35125           case GOMP_MAP_ALWAYS_TO:
35126           case GOMP_MAP_FROM:
35127           case GOMP_MAP_ALWAYS_FROM:
35128           case GOMP_MAP_TOFROM:
35129           case GOMP_MAP_ALWAYS_TOFROM:
35130           case GOMP_MAP_ALLOC:
35131           case GOMP_MAP_FIRSTPRIVATE_POINTER:
35132           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35133           case GOMP_MAP_ALWAYS_POINTER:
35134             break;
35135           default:
35136             error_at (OMP_CLAUSE_LOCATION (*pc),
35137                       "%<#pragma omp target%> with map-type other "
35138                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35139                       "on %<map%> clause");
35140             *pc = OMP_CLAUSE_CHAIN (*pc);
35141             continue;
35142           }
35143       pc = &OMP_CLAUSE_CHAIN (*pc);
35144     }
35145   return true;
35146 }
35147
35148 /* OpenACC 2.0:
35149    # pragma acc cache (variable-list) new-line
35150 */
35151
35152 static tree
35153 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35154 {
35155   tree stmt, clauses;
35156
35157   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35158   clauses = finish_omp_clauses (clauses, false);
35159
35160   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35161
35162   stmt = make_node (OACC_CACHE);
35163   TREE_TYPE (stmt) = void_type_node;
35164   OACC_CACHE_CLAUSES (stmt) = clauses;
35165   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35166   add_stmt (stmt);
35167
35168   return stmt;
35169 }
35170
35171 /* OpenACC 2.0:
35172    # pragma acc data oacc-data-clause[optseq] new-line
35173      structured-block  */
35174
35175 #define OACC_DATA_CLAUSE_MASK                                           \
35176         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35177         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35178         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35179         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35180         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35181         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35182         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35183         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35184         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35185         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35186         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35187
35188 static tree
35189 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35190 {
35191   tree stmt, clauses, block;
35192   unsigned int save;
35193
35194   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35195                                         "#pragma acc data", pragma_tok);
35196
35197   block = begin_omp_parallel ();
35198   save = cp_parser_begin_omp_structured_block (parser);
35199   cp_parser_statement (parser, NULL_TREE, false, if_p);
35200   cp_parser_end_omp_structured_block (parser, save);
35201   stmt = finish_oacc_data (clauses, block);
35202   return stmt;
35203 }
35204
35205 /* OpenACC 2.0:
35206   # pragma acc host_data <clauses> new-line
35207   structured-block  */
35208
35209 #define OACC_HOST_DATA_CLAUSE_MASK                                      \
35210   ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35211
35212 static tree
35213 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35214 {
35215   tree stmt, clauses, block;
35216   unsigned int save;
35217
35218   clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35219                                         "#pragma acc host_data", pragma_tok);
35220
35221   block = begin_omp_parallel ();
35222   save = cp_parser_begin_omp_structured_block (parser);
35223   cp_parser_statement (parser, NULL_TREE, false, if_p);
35224   cp_parser_end_omp_structured_block (parser, save);
35225   stmt = finish_oacc_host_data (clauses, block);
35226   return stmt;
35227 }
35228
35229 /* OpenACC 2.0:
35230    # pragma acc declare oacc-data-clause[optseq] new-line
35231 */
35232
35233 #define OACC_DECLARE_CLAUSE_MASK                                        \
35234         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35235         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35236         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35237         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35238         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35239         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)     \
35240         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)                \
35241         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35242         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35243         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35244         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35245         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35246
35247 static tree
35248 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35249 {
35250   tree clauses, stmt;
35251   bool error = false;
35252
35253   clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35254                                         "#pragma acc declare", pragma_tok, true);
35255
35256
35257   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35258     {
35259       error_at (pragma_tok->location,
35260                 "no valid clauses specified in %<#pragma acc declare%>");
35261       return NULL_TREE;
35262     }
35263
35264   for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35265     {
35266       location_t loc = OMP_CLAUSE_LOCATION (t);
35267       tree decl = OMP_CLAUSE_DECL (t);
35268       if (!DECL_P (decl))
35269         {
35270           error_at (loc, "array section in %<#pragma acc declare%>");
35271           error = true;
35272           continue;
35273         }
35274       gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35275       switch (OMP_CLAUSE_MAP_KIND (t))
35276         {
35277         case GOMP_MAP_FORCE_ALLOC:
35278         case GOMP_MAP_FORCE_TO:
35279         case GOMP_MAP_FORCE_DEVICEPTR:
35280         case GOMP_MAP_DEVICE_RESIDENT:
35281           break;
35282
35283         case GOMP_MAP_POINTER:
35284           /* Generated by c_finish_omp_clauses from array sections;
35285              avoid spurious diagnostics.  */
35286           break;
35287
35288         case GOMP_MAP_LINK:
35289           if (!global_bindings_p ()
35290               && (TREE_STATIC (decl)
35291                || !DECL_EXTERNAL (decl)))
35292             {
35293               error_at (loc,
35294                         "%qD must be a global variable in"
35295                         "%<#pragma acc declare link%>",
35296                         decl);
35297               error = true;
35298               continue;
35299             }
35300           break;
35301
35302         default:
35303           if (global_bindings_p ())
35304             {
35305               error_at (loc, "invalid OpenACC clause at file scope");
35306               error = true;
35307               continue;
35308             }
35309           if (DECL_EXTERNAL (decl))
35310             {
35311               error_at (loc,
35312                         "invalid use of %<extern%> variable %qD "
35313                         "in %<#pragma acc declare%>", decl);
35314               error = true;
35315               continue;
35316             }
35317           else if (TREE_PUBLIC (decl))
35318             {
35319               error_at (loc,
35320                         "invalid use of %<global%> variable %qD "
35321                         "in %<#pragma acc declare%>", decl);
35322               error = true;
35323               continue;
35324             }
35325           break;
35326         }
35327
35328       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35329           || lookup_attribute ("omp declare target link",
35330                                DECL_ATTRIBUTES (decl)))
35331         {
35332           error_at (loc, "variable %qD used more than once with "
35333                     "%<#pragma acc declare%>", decl);
35334           error = true;
35335           continue;
35336         }
35337
35338       if (!error)
35339         {
35340           tree id;
35341
35342           if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35343             id = get_identifier ("omp declare target link");
35344           else
35345             id = get_identifier ("omp declare target");
35346
35347           DECL_ATTRIBUTES (decl)
35348                            = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35349           if (global_bindings_p ())
35350             {
35351               symtab_node *node = symtab_node::get (decl);
35352               if (node != NULL)
35353                 {
35354                   node->offloadable = 1;
35355                   if (ENABLE_OFFLOADING)
35356                     {
35357                       g->have_offload = true;
35358                       if (is_a <varpool_node *> (node))
35359                         vec_safe_push (offload_vars, decl);
35360                     }
35361                 }
35362             }
35363         }
35364     }
35365
35366   if (error || global_bindings_p ())
35367     return NULL_TREE;
35368
35369   stmt = make_node (OACC_DECLARE);
35370   TREE_TYPE (stmt) = void_type_node;
35371   OACC_DECLARE_CLAUSES (stmt) = clauses;
35372   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35373
35374   add_stmt (stmt);
35375
35376   return NULL_TREE;
35377 }
35378
35379 /* OpenACC 2.0:
35380    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35381
35382    or
35383
35384    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35385
35386    LOC is the location of the #pragma token.
35387 */
35388
35389 #define OACC_ENTER_DATA_CLAUSE_MASK                                     \
35390         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35391         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35392         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35393         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35394         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35395         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
35396         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35397
35398 #define OACC_EXIT_DATA_CLAUSE_MASK                                      \
35399         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35400         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35401         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35402         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE)              \
35403         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35404
35405 static tree
35406 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35407                                 bool enter)
35408 {
35409   tree stmt, clauses;
35410
35411   if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35412      || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35413     {
35414       cp_parser_error (parser, enter
35415                        ? "expected %<data%> in %<#pragma acc enter data%>"
35416                        : "expected %<data%> in %<#pragma acc exit data%>");
35417       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35418       return NULL_TREE;
35419     }
35420
35421   const char *p =
35422     IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35423   if (strcmp (p, "data") != 0)
35424     {
35425       cp_parser_error (parser, "invalid pragma");
35426       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35427       return NULL_TREE;
35428     }
35429
35430   cp_lexer_consume_token (parser->lexer);
35431
35432   if (enter)
35433     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35434                                          "#pragma acc enter data", pragma_tok);
35435   else
35436     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35437                                          "#pragma acc exit data", pragma_tok);
35438
35439   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35440     {
35441       error_at (pragma_tok->location,
35442                 "%<#pragma acc enter data%> has no data movement clause");
35443       return NULL_TREE;
35444     }
35445
35446   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35447   TREE_TYPE (stmt) = void_type_node;
35448   OMP_STANDALONE_CLAUSES (stmt) = clauses;
35449   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35450   add_stmt (stmt);
35451   return stmt;
35452 }
35453
35454 /* OpenACC 2.0:
35455    # pragma acc loop oacc-loop-clause[optseq] new-line
35456      structured-block  */
35457
35458 #define OACC_LOOP_CLAUSE_MASK                                           \
35459         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)            \
35460         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
35461         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
35462         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
35463         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
35464         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
35465         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)                \
35466         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)         \
35467         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)                 \
35468         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35469
35470 static tree
35471 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35472                      omp_clause_mask mask, tree *cclauses, bool *if_p)
35473 {
35474   bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35475
35476   strcat (p_name, " loop");
35477   mask |= OACC_LOOP_CLAUSE_MASK;
35478
35479   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35480                                              cclauses == NULL);
35481   if (cclauses)
35482     {
35483       clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35484       if (*cclauses)
35485         *cclauses = finish_omp_clauses (*cclauses, false);
35486       if (clauses)
35487         clauses = finish_omp_clauses (clauses, false);
35488     }
35489
35490   tree block = begin_omp_structured_block ();
35491   int save = cp_parser_begin_omp_structured_block (parser);
35492   tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35493   cp_parser_end_omp_structured_block (parser, save);
35494   add_stmt (finish_omp_structured_block (block));
35495
35496   return stmt;
35497 }
35498
35499 /* OpenACC 2.0:
35500    # pragma acc kernels oacc-kernels-clause[optseq] new-line
35501      structured-block
35502
35503    or
35504
35505    # pragma acc parallel oacc-parallel-clause[optseq] new-line
35506      structured-block
35507 */
35508
35509 #define OACC_KERNELS_CLAUSE_MASK                                        \
35510         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35511         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35512         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35513         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35514         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35515         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
35516         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35517         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35518         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35519         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35520         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35521         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35522         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
35523         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35524
35525 #define OACC_PARALLEL_CLAUSE_MASK                                       \
35526         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35527         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35528         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35529         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35530         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35531         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
35532         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35533         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)        \
35534         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35535         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
35536         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
35537         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35538         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35539         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35540         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35541         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
35542         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
35543         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
35544         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
35545         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35546
35547 static tree
35548 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35549                                  char *p_name, bool *if_p)
35550 {
35551   omp_clause_mask mask;
35552   enum tree_code code;
35553   switch (cp_parser_pragma_kind (pragma_tok))
35554     {
35555     case PRAGMA_OACC_KERNELS:
35556       strcat (p_name, " kernels");
35557       mask = OACC_KERNELS_CLAUSE_MASK;
35558       code = OACC_KERNELS;
35559       break;
35560     case PRAGMA_OACC_PARALLEL:
35561       strcat (p_name, " parallel");
35562       mask = OACC_PARALLEL_CLAUSE_MASK;
35563       code = OACC_PARALLEL;
35564       break;
35565     default:
35566       gcc_unreachable ();
35567     }
35568
35569   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35570     {
35571       const char *p
35572         = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35573       if (strcmp (p, "loop") == 0)
35574         {
35575           cp_lexer_consume_token (parser->lexer);
35576           tree block = begin_omp_parallel ();
35577           tree clauses;
35578           cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35579                                if_p);
35580           return finish_omp_construct (code, block, clauses);
35581         }
35582     }
35583
35584   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35585
35586   tree block = begin_omp_parallel ();
35587   unsigned int save = cp_parser_begin_omp_structured_block (parser);
35588   cp_parser_statement (parser, NULL_TREE, false, if_p);
35589   cp_parser_end_omp_structured_block (parser, save);
35590   return finish_omp_construct (code, block, clauses);
35591 }
35592
35593 /* OpenACC 2.0:
35594    # pragma acc update oacc-update-clause[optseq] new-line
35595 */
35596
35597 #define OACC_UPDATE_CLAUSE_MASK                                         \
35598         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35599         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)              \
35600         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)                \
35601         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35602         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)                \
35603         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35604
35605 static tree
35606 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35607 {
35608   tree stmt, clauses;
35609
35610   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35611                                          "#pragma acc update", pragma_tok);
35612
35613   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35614     {
35615       error_at (pragma_tok->location,
35616                 "%<#pragma acc update%> must contain at least one "
35617                 "%<device%> or %<host%> or %<self%> clause");
35618       return NULL_TREE;
35619     }
35620
35621   stmt = make_node (OACC_UPDATE);
35622   TREE_TYPE (stmt) = void_type_node;
35623   OACC_UPDATE_CLAUSES (stmt) = clauses;
35624   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35625   add_stmt (stmt);
35626   return stmt;
35627 }
35628
35629 /* OpenACC 2.0:
35630    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35631
35632    LOC is the location of the #pragma token.
35633 */
35634
35635 #define OACC_WAIT_CLAUSE_MASK                                   \
35636         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35637
35638 static tree
35639 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35640 {
35641   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35642   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35643
35644   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35645     list = cp_parser_oacc_wait_list (parser, loc, list);
35646
35647   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35648                                         "#pragma acc wait", pragma_tok);
35649
35650   stmt = c_finish_oacc_wait (loc, list, clauses);
35651   stmt = finish_expr_stmt (stmt);
35652
35653   return stmt;
35654 }
35655
35656 /* OpenMP 4.0:
35657    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
35658
35659 #define OMP_DECLARE_SIMD_CLAUSE_MASK                            \
35660         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
35661         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
35662         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
35663         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)      \
35664         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)     \
35665         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35666
35667 static void
35668 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35669                             enum pragma_context context)
35670 {
35671   bool first_p = parser->omp_declare_simd == NULL;
35672   cp_omp_declare_simd_data data;
35673   if (first_p)
35674     {
35675       data.error_seen = false;
35676       data.fndecl_seen = false;
35677       data.tokens = vNULL;
35678       parser->omp_declare_simd = &data;
35679     }
35680   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35681          && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35682     cp_lexer_consume_token (parser->lexer);
35683   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35684     parser->omp_declare_simd->error_seen = true;
35685   cp_parser_require_pragma_eol (parser, pragma_tok);
35686   struct cp_token_cache *cp
35687     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35688   parser->omp_declare_simd->tokens.safe_push (cp);
35689   if (first_p)
35690     {
35691       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35692         cp_parser_pragma (parser, context, NULL);
35693       switch (context)
35694         {
35695         case pragma_external:
35696           cp_parser_declaration (parser);
35697           break;
35698         case pragma_member:
35699           cp_parser_member_declaration (parser);
35700           break;
35701         case pragma_objc_icode:
35702           cp_parser_block_declaration (parser, /*statement_p=*/false);
35703           break;
35704         default:
35705           cp_parser_declaration_statement (parser);
35706           break;
35707         }
35708       if (parser->omp_declare_simd
35709           && !parser->omp_declare_simd->error_seen
35710           && !parser->omp_declare_simd->fndecl_seen)
35711         error_at (pragma_tok->location,
35712                   "%<#pragma omp declare simd%> not immediately followed by "
35713                   "function declaration or definition");
35714       data.tokens.release ();
35715       parser->omp_declare_simd = NULL;
35716     }
35717 }
35718
35719 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.  
35720    This function is modelled similar to the late parsing of omp declare 
35721    simd.  */
35722
35723 static tree
35724 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35725 {
35726   struct cp_token_cache *ce;
35727   cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35728   int ii = 0;
35729
35730   if (parser->omp_declare_simd != NULL
35731       || lookup_attribute ("simd", attrs))
35732     {
35733       error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35734              "used in the same function marked as a Cilk Plus SIMD-enabled "
35735              " function");
35736       parser->cilk_simd_fn_info->tokens.release ();
35737       XDELETE (parser->cilk_simd_fn_info);
35738       parser->cilk_simd_fn_info = NULL;
35739       return attrs;
35740     }
35741   if (!info->error_seen && info->fndecl_seen)
35742     {
35743       error ("vector attribute not immediately followed by a single function"
35744              " declaration or definition");
35745       info->error_seen = true;
35746     }
35747   if (info->error_seen)
35748     return attrs;
35749
35750   FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35751     {
35752       tree c, cl;
35753
35754       cp_parser_push_lexer_for_tokens (parser, ce);
35755       parser->lexer->in_pragma = true;
35756       cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35757                                       "SIMD-enabled functions attribute", 
35758                                       NULL);
35759       cp_parser_pop_lexer (parser);
35760       if (cl)
35761         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35762
35763       c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35764       TREE_CHAIN (c) = attrs;
35765       attrs = c;
35766
35767       c = build_tree_list (get_identifier ("omp declare simd"), cl);
35768       TREE_CHAIN (c) = attrs;
35769       if (processing_template_decl)
35770         ATTR_IS_DEPENDENT (c) = 1;
35771       attrs = c;
35772     }
35773   info->fndecl_seen = true;
35774   parser->cilk_simd_fn_info->tokens.release ();
35775   XDELETE (parser->cilk_simd_fn_info);
35776   parser->cilk_simd_fn_info = NULL;
35777   return attrs;
35778 }
35779
35780 /* Finalize #pragma omp declare simd clauses after direct declarator has
35781    been parsed, and put that into "omp declare simd" attribute.  */
35782
35783 static tree
35784 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35785 {
35786   struct cp_token_cache *ce;
35787   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35788   int i;
35789
35790   if (!data->error_seen && data->fndecl_seen)
35791     {
35792       error ("%<#pragma omp declare simd%> not immediately followed by "
35793              "a single function declaration or definition");
35794       data->error_seen = true;
35795       return attrs;
35796     }
35797   if (data->error_seen)
35798     return attrs;
35799
35800   FOR_EACH_VEC_ELT (data->tokens, i, ce)
35801     {
35802       tree c, cl;
35803
35804       cp_parser_push_lexer_for_tokens (parser, ce);
35805       parser->lexer->in_pragma = true;
35806       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35807       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35808       cp_lexer_consume_token (parser->lexer);
35809       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35810                                       "#pragma omp declare simd", pragma_tok);
35811       cp_parser_pop_lexer (parser);
35812       if (cl)
35813         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35814       c = build_tree_list (get_identifier ("omp declare simd"), cl);
35815       TREE_CHAIN (c) = attrs;
35816       if (processing_template_decl)
35817         ATTR_IS_DEPENDENT (c) = 1;
35818       attrs = c;
35819     }
35820
35821   data->fndecl_seen = true;
35822   return attrs;
35823 }
35824
35825
35826 /* OpenMP 4.0:
35827    # pragma omp declare target new-line
35828    declarations and definitions
35829    # pragma omp end declare target new-line
35830
35831    OpenMP 4.5:
35832    # pragma omp declare target ( extended-list ) new-line
35833
35834    # pragma omp declare target declare-target-clauses[seq] new-line  */
35835
35836 #define OMP_DECLARE_TARGET_CLAUSE_MASK                          \
35837         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
35838         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35839
35840 static void
35841 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35842 {
35843   tree clauses = NULL_TREE;
35844   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35845     clauses
35846       = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35847                                    "#pragma omp declare target", pragma_tok);
35848   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35849     {
35850       clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35851                                         clauses);
35852       clauses = finish_omp_clauses (clauses, true);
35853       cp_parser_require_pragma_eol (parser, pragma_tok);
35854     }
35855   else
35856     {
35857       cp_parser_require_pragma_eol (parser, pragma_tok);
35858       scope_chain->omp_declare_target_attribute++;
35859       return;
35860     }
35861   if (scope_chain->omp_declare_target_attribute)
35862     error_at (pragma_tok->location,
35863               "%<#pragma omp declare target%> with clauses in between "
35864               "%<#pragma omp declare target%> without clauses and "
35865               "%<#pragma omp end declare target%>");
35866   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35867     {
35868       tree t = OMP_CLAUSE_DECL (c), id;
35869       tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35870       tree at2 = lookup_attribute ("omp declare target link",
35871                                    DECL_ATTRIBUTES (t));
35872       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35873         {
35874           id = get_identifier ("omp declare target link");
35875           std::swap (at1, at2);
35876         }
35877       else
35878         id = get_identifier ("omp declare target");
35879       if (at2)
35880         {
35881           error_at (OMP_CLAUSE_LOCATION (c),
35882                     "%qD specified both in declare target %<link%> and %<to%>"
35883                     " clauses", t);
35884           continue;
35885         }
35886       if (!at1)
35887         {
35888           symtab_node *node = symtab_node::get (t);
35889           DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35890           if (node != NULL)
35891             {
35892               node->offloadable = 1;
35893               if (ENABLE_OFFLOADING)
35894                 {
35895                   g->have_offload = true;
35896                   if (is_a <varpool_node *> (node))
35897                     vec_safe_push (offload_vars, t);
35898                 }
35899             }
35900         }
35901     }
35902 }
35903
35904 static void
35905 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35906 {
35907   const char *p = "";
35908   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35909     {
35910       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35911       p = IDENTIFIER_POINTER (id);
35912     }
35913   if (strcmp (p, "declare") == 0)
35914     {
35915       cp_lexer_consume_token (parser->lexer);
35916       p = "";
35917       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35918         {
35919           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35920           p = IDENTIFIER_POINTER (id);
35921         }
35922       if (strcmp (p, "target") == 0)
35923         cp_lexer_consume_token (parser->lexer);
35924       else
35925         {
35926           cp_parser_error (parser, "expected %<target%>");
35927           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35928           return;
35929         }
35930     }
35931   else
35932     {
35933       cp_parser_error (parser, "expected %<declare%>");
35934       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35935       return;
35936     }
35937   cp_parser_require_pragma_eol (parser, pragma_tok);
35938   if (!scope_chain->omp_declare_target_attribute)
35939     error_at (pragma_tok->location,
35940               "%<#pragma omp end declare target%> without corresponding "
35941               "%<#pragma omp declare target%>");
35942   else
35943     scope_chain->omp_declare_target_attribute--;
35944 }
35945
35946 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
35947    expression and optional initializer clause of
35948    #pragma omp declare reduction.  We store the expression(s) as
35949    either 3, 6 or 7 special statements inside of the artificial function's
35950    body.  The first two statements are DECL_EXPRs for the artificial
35951    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35952    expression that uses those variables.
35953    If there was any INITIALIZER clause, this is followed by further statements,
35954    the fourth and fifth statements are DECL_EXPRs for the artificial
35955    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
35956    constructor variant (first token after open paren is not omp_priv),
35957    then the sixth statement is a statement with the function call expression
35958    that uses the OMP_PRIV and optionally OMP_ORIG variable.
35959    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35960    to initialize the OMP_PRIV artificial variable and there is seventh
35961    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
35962
35963 static bool
35964 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35965 {
35966   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35967   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35968   type = TREE_TYPE (type);
35969   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35970   DECL_ARTIFICIAL (omp_out) = 1;
35971   pushdecl (omp_out);
35972   add_decl_expr (omp_out);
35973   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35974   DECL_ARTIFICIAL (omp_in) = 1;
35975   pushdecl (omp_in);
35976   add_decl_expr (omp_in);
35977   tree combiner;
35978   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35979
35980   keep_next_level (true);
35981   tree block = begin_omp_structured_block ();
35982   combiner = cp_parser_expression (parser);
35983   finish_expr_stmt (combiner);
35984   block = finish_omp_structured_block (block);
35985   add_stmt (block);
35986
35987   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35988     return false;
35989
35990   const char *p = "";
35991   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35992     {
35993       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35994       p = IDENTIFIER_POINTER (id);
35995     }
35996
35997   if (strcmp (p, "initializer") == 0)
35998     {
35999       cp_lexer_consume_token (parser->lexer);
36000       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36001         return false;
36002
36003       p = "";
36004       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36005         {
36006           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36007           p = IDENTIFIER_POINTER (id);
36008         }
36009
36010       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36011       DECL_ARTIFICIAL (omp_priv) = 1;
36012       pushdecl (omp_priv);
36013       add_decl_expr (omp_priv);
36014       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36015       DECL_ARTIFICIAL (omp_orig) = 1;
36016       pushdecl (omp_orig);
36017       add_decl_expr (omp_orig);
36018
36019       keep_next_level (true);
36020       block = begin_omp_structured_block ();
36021
36022       bool ctor = false;
36023       if (strcmp (p, "omp_priv") == 0)
36024         {
36025           bool is_direct_init, is_non_constant_init;
36026           ctor = true;
36027           cp_lexer_consume_token (parser->lexer);
36028           /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
36029           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36030               || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36031                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36032                      == CPP_CLOSE_PAREN
36033                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36034                      == CPP_CLOSE_PAREN))
36035             {
36036               finish_omp_structured_block (block);
36037               error ("invalid initializer clause");
36038               return false;
36039             }
36040           initializer = cp_parser_initializer (parser, &is_direct_init,
36041                                                &is_non_constant_init);
36042           cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36043                           NULL_TREE, LOOKUP_ONLYCONVERTING);
36044         }
36045       else
36046         {
36047           cp_parser_parse_tentatively (parser);
36048           tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36049                                                   /*check_dependency_p=*/true,
36050                                                   /*template_p=*/NULL,
36051                                                   /*declarator_p=*/false,
36052                                                   /*optional_p=*/false);
36053           vec<tree, va_gc> *args;
36054           if (fn_name == error_mark_node
36055               || cp_parser_error_occurred (parser)
36056               || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36057               || ((args = cp_parser_parenthesized_expression_list
36058                                 (parser, non_attr, /*cast_p=*/false,
36059                                  /*allow_expansion_p=*/true,
36060                                  /*non_constant_p=*/NULL)),
36061                   cp_parser_error_occurred (parser)))
36062             {
36063               finish_omp_structured_block (block);
36064               cp_parser_abort_tentative_parse (parser);
36065               cp_parser_error (parser, "expected id-expression (arguments)");
36066               return false;
36067             }
36068           unsigned int i;
36069           tree arg;
36070           FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36071             if (arg == omp_priv
36072                 || (TREE_CODE (arg) == ADDR_EXPR
36073                     && TREE_OPERAND (arg, 0) == omp_priv))
36074               break;
36075           cp_parser_abort_tentative_parse (parser);
36076           if (arg == NULL_TREE)
36077             error ("one of the initializer call arguments should be %<omp_priv%>"
36078                    " or %<&omp_priv%>");
36079           initializer = cp_parser_postfix_expression (parser, false, false, false,
36080                                                       false, NULL);
36081           finish_expr_stmt (initializer);
36082         }
36083
36084       block = finish_omp_structured_block (block);
36085       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36086       add_stmt (block);
36087
36088       if (ctor)
36089         add_decl_expr (omp_orig);
36090
36091       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36092         return false;
36093     }
36094
36095   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36096     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36097
36098   return true;
36099 }
36100
36101 /* OpenMP 4.0
36102    #pragma omp declare reduction (reduction-id : typename-list : expression) \
36103       initializer-clause[opt] new-line
36104
36105    initializer-clause:
36106       initializer (omp_priv initializer)
36107       initializer (function-name (argument-list))  */
36108
36109 static void
36110 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36111                                  enum pragma_context)
36112 {
36113   auto_vec<tree> types;
36114   enum tree_code reduc_code = ERROR_MARK;
36115   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36116   unsigned int i;
36117   cp_token *first_token;
36118   cp_token_cache *cp;
36119   int errs;
36120   void *p;
36121     
36122   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
36123   p = obstack_alloc (&declarator_obstack, 0);
36124
36125   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36126     goto fail;
36127
36128   switch (cp_lexer_peek_token (parser->lexer)->type)
36129     {
36130     case CPP_PLUS:
36131       reduc_code = PLUS_EXPR;
36132       break;
36133     case CPP_MULT:
36134       reduc_code = MULT_EXPR;
36135       break;
36136     case CPP_MINUS:
36137       reduc_code = MINUS_EXPR;
36138       break;
36139     case CPP_AND:
36140       reduc_code = BIT_AND_EXPR;
36141       break;
36142     case CPP_XOR:
36143       reduc_code = BIT_XOR_EXPR;
36144       break;
36145     case CPP_OR:
36146       reduc_code = BIT_IOR_EXPR;
36147       break;
36148     case CPP_AND_AND:
36149       reduc_code = TRUTH_ANDIF_EXPR;
36150       break;
36151     case CPP_OR_OR:
36152       reduc_code = TRUTH_ORIF_EXPR;
36153       break;
36154     case CPP_NAME:
36155       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36156       break;
36157     default:
36158       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36159                                "%<|%>, %<&&%>, %<||%> or identifier");
36160       goto fail;
36161     }
36162
36163   if (reduc_code != ERROR_MARK)
36164     cp_lexer_consume_token (parser->lexer);
36165
36166   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36167   if (reduc_id == error_mark_node)
36168     goto fail;
36169
36170   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36171     goto fail;
36172
36173   /* Types may not be defined in declare reduction type list.  */
36174   const char *saved_message;
36175   saved_message = parser->type_definition_forbidden_message;
36176   parser->type_definition_forbidden_message
36177     = G_("types may not be defined in declare reduction type list");
36178   bool saved_colon_corrects_to_scope_p;
36179   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36180   parser->colon_corrects_to_scope_p = false;
36181   bool saved_colon_doesnt_start_class_def_p;
36182   saved_colon_doesnt_start_class_def_p
36183     = parser->colon_doesnt_start_class_def_p;
36184   parser->colon_doesnt_start_class_def_p = true;
36185
36186   while (true)
36187     {
36188       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36189       type = cp_parser_type_id (parser);
36190       if (type == error_mark_node)
36191         ;
36192       else if (ARITHMETIC_TYPE_P (type)
36193                && (orig_reduc_id == NULL_TREE
36194                    || (TREE_CODE (type) != COMPLEX_TYPE
36195                        && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36196                                    "min") == 0
36197                            || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36198                                       "max") == 0))))
36199         error_at (loc, "predeclared arithmetic type %qT in "
36200                        "%<#pragma omp declare reduction%>", type);
36201       else if (TREE_CODE (type) == FUNCTION_TYPE
36202                || TREE_CODE (type) == METHOD_TYPE
36203                || TREE_CODE (type) == ARRAY_TYPE)
36204         error_at (loc, "function or array type %qT in "
36205                        "%<#pragma omp declare reduction%>", type);
36206       else if (TREE_CODE (type) == REFERENCE_TYPE)
36207         error_at (loc, "reference type %qT in "
36208                        "%<#pragma omp declare reduction%>", type);
36209       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36210         error_at (loc, "const, volatile or __restrict qualified type %qT in "
36211                        "%<#pragma omp declare reduction%>", type);
36212       else
36213         types.safe_push (type);
36214
36215       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36216         cp_lexer_consume_token (parser->lexer);
36217       else
36218         break;
36219     }
36220
36221   /* Restore the saved message.  */
36222   parser->type_definition_forbidden_message = saved_message;
36223   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36224   parser->colon_doesnt_start_class_def_p
36225     = saved_colon_doesnt_start_class_def_p;
36226
36227   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36228       || types.is_empty ())
36229     {
36230      fail:
36231       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36232       goto done;
36233     }
36234
36235   first_token = cp_lexer_peek_token (parser->lexer);
36236   cp = NULL;
36237   errs = errorcount;
36238   FOR_EACH_VEC_ELT (types, i, type)
36239     {
36240       tree fntype
36241         = build_function_type_list (void_type_node,
36242                                     cp_build_reference_type (type, false),
36243                                     NULL_TREE);
36244       tree this_reduc_id = reduc_id;
36245       if (!dependent_type_p (type))
36246         this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36247       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36248       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36249       DECL_ARTIFICIAL (fndecl) = 1;
36250       DECL_EXTERNAL (fndecl) = 1;
36251       DECL_DECLARED_INLINE_P (fndecl) = 1;
36252       DECL_IGNORED_P (fndecl) = 1;
36253       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36254       SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36255       DECL_ATTRIBUTES (fndecl)
36256         = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36257                      DECL_ATTRIBUTES (fndecl));
36258       if (processing_template_decl)
36259         fndecl = push_template_decl (fndecl);
36260       bool block_scope = false;
36261       tree block = NULL_TREE;
36262       if (current_function_decl)
36263         {
36264           block_scope = true;
36265           DECL_CONTEXT (fndecl) = global_namespace;
36266           if (!processing_template_decl)
36267             pushdecl (fndecl);
36268         }
36269       else if (current_class_type)
36270         {
36271           if (cp == NULL)
36272             {
36273               while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36274                      && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36275                 cp_lexer_consume_token (parser->lexer);
36276               if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36277                 goto fail;
36278               cp = cp_token_cache_new (first_token,
36279                                        cp_lexer_peek_nth_token (parser->lexer,
36280                                                                 2));
36281             }
36282           DECL_STATIC_FUNCTION_P (fndecl) = 1;
36283           finish_member_declaration (fndecl);
36284           DECL_PENDING_INLINE_INFO (fndecl) = cp;
36285           DECL_PENDING_INLINE_P (fndecl) = 1;
36286           vec_safe_push (unparsed_funs_with_definitions, fndecl);
36287           continue;
36288         }
36289       else
36290         {
36291           DECL_CONTEXT (fndecl) = current_namespace;
36292           pushdecl (fndecl);
36293         }
36294       if (!block_scope)
36295         start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36296       else
36297         block = begin_omp_structured_block ();
36298       if (cp)
36299         {
36300           cp_parser_push_lexer_for_tokens (parser, cp);
36301           parser->lexer->in_pragma = true;
36302         }
36303       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36304         {
36305           if (!block_scope)
36306             finish_function (0);
36307           else
36308             DECL_CONTEXT (fndecl) = current_function_decl;
36309           if (cp)
36310             cp_parser_pop_lexer (parser);
36311           goto fail;
36312         }
36313       if (cp)
36314         cp_parser_pop_lexer (parser);
36315       if (!block_scope)
36316         finish_function (0);
36317       else
36318         {
36319           DECL_CONTEXT (fndecl) = current_function_decl;
36320           block = finish_omp_structured_block (block);
36321           if (TREE_CODE (block) == BIND_EXPR)
36322             DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36323           else if (TREE_CODE (block) == STATEMENT_LIST)
36324             DECL_SAVED_TREE (fndecl) = block;
36325           if (processing_template_decl)
36326             add_decl_expr (fndecl);
36327         }
36328       cp_check_omp_declare_reduction (fndecl);
36329       if (cp == NULL && types.length () > 1)
36330         cp = cp_token_cache_new (first_token,
36331                                  cp_lexer_peek_nth_token (parser->lexer, 2));
36332       if (errs != errorcount)
36333         break;
36334     }
36335
36336   cp_parser_require_pragma_eol (parser, pragma_tok);
36337
36338  done:
36339   /* Free any declarators allocated.  */
36340   obstack_free (&declarator_obstack, p);
36341 }
36342
36343 /* OpenMP 4.0
36344    #pragma omp declare simd declare-simd-clauses[optseq] new-line
36345    #pragma omp declare reduction (reduction-id : typename-list : expression) \
36346       initializer-clause[opt] new-line
36347    #pragma omp declare target new-line  */
36348
36349 static void
36350 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36351                        enum pragma_context context)
36352 {
36353   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36354     {
36355       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36356       const char *p = IDENTIFIER_POINTER (id);
36357
36358       if (strcmp (p, "simd") == 0)
36359         {
36360           cp_lexer_consume_token (parser->lexer);
36361           cp_parser_omp_declare_simd (parser, pragma_tok,
36362                                       context);
36363           return;
36364         }
36365       cp_ensure_no_omp_declare_simd (parser);
36366       if (strcmp (p, "reduction") == 0)
36367         {
36368           cp_lexer_consume_token (parser->lexer);
36369           cp_parser_omp_declare_reduction (parser, pragma_tok,
36370                                            context);
36371           return;
36372         }
36373       if (!flag_openmp)  /* flag_openmp_simd  */
36374         {
36375           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36376           return;
36377         }
36378       if (strcmp (p, "target") == 0)
36379         {
36380           cp_lexer_consume_token (parser->lexer);
36381           cp_parser_omp_declare_target (parser, pragma_tok);
36382           return;
36383         }
36384     }
36385   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36386                            "or %<target%>");
36387   cp_parser_require_pragma_eol (parser, pragma_tok);
36388 }
36389
36390 /* OpenMP 4.5:
36391    #pragma omp taskloop taskloop-clause[optseq] new-line
36392      for-loop
36393
36394    #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36395      for-loop  */
36396
36397 #define OMP_TASKLOOP_CLAUSE_MASK                                \
36398         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
36399         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
36400         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36401         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
36402         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
36403         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)    \
36404         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)    \
36405         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)     \
36406         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
36407         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36408         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
36409         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
36410         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)      \
36411         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36412
36413 static tree
36414 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36415                         char *p_name, omp_clause_mask mask, tree *cclauses,
36416                         bool *if_p)
36417 {
36418   tree clauses, sb, ret;
36419   unsigned int save;
36420   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36421
36422   strcat (p_name, " taskloop");
36423   mask |= OMP_TASKLOOP_CLAUSE_MASK;
36424
36425   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36426     {
36427       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36428       const char *p = IDENTIFIER_POINTER (id);
36429
36430       if (strcmp (p, "simd") == 0)
36431         {
36432           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36433           if (cclauses == NULL)
36434             cclauses = cclauses_buf;
36435
36436           cp_lexer_consume_token (parser->lexer);
36437           if (!flag_openmp)  /* flag_openmp_simd  */
36438             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36439                                        cclauses, if_p);
36440           sb = begin_omp_structured_block ();
36441           save = cp_parser_begin_omp_structured_block (parser);
36442           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36443                                     cclauses, if_p);
36444           cp_parser_end_omp_structured_block (parser, save);
36445           tree body = finish_omp_structured_block (sb);
36446           if (ret == NULL)
36447             return ret;
36448           ret = make_node (OMP_TASKLOOP);
36449           TREE_TYPE (ret) = void_type_node;
36450           OMP_FOR_BODY (ret) = body;
36451           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36452           SET_EXPR_LOCATION (ret, loc);
36453           add_stmt (ret);
36454           return ret;
36455         }
36456     }
36457   if (!flag_openmp)  /* flag_openmp_simd  */
36458     {
36459       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36460       return NULL_TREE;
36461     }
36462
36463   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36464                                        cclauses == NULL);
36465   if (cclauses)
36466     {
36467       cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36468       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36469     }
36470
36471   sb = begin_omp_structured_block ();
36472   save = cp_parser_begin_omp_structured_block (parser);
36473
36474   ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36475                                 if_p);
36476
36477   cp_parser_end_omp_structured_block (parser, save);
36478   add_stmt (finish_omp_structured_block (sb));
36479
36480   return ret;
36481 }
36482
36483
36484 /* OpenACC 2.0:
36485    # pragma acc routine oacc-routine-clause[optseq] new-line
36486      function-definition
36487
36488    # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36489 */
36490
36491 #define OACC_ROUTINE_CLAUSE_MASK                                        \
36492         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
36493         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
36494         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
36495         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36496
36497
36498 /* Parse the OpenACC routine pragma.  This has an optional '( name )'
36499    component, which must resolve to a declared namespace-scope
36500    function.  The clauses are either processed directly (for a named
36501    function), or defered until the immediatley following declaration
36502    is parsed.  */
36503
36504 static void
36505 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36506                         enum pragma_context context)
36507 {
36508   bool first_p = parser->oacc_routine == NULL;
36509   location_t loc = pragma_tok->location;
36510   cp_omp_declare_simd_data data;
36511   if (first_p)
36512     {
36513       data.error_seen = false;
36514       data.fndecl_seen = false;
36515       data.tokens = vNULL;
36516       data.clauses = NULL_TREE;
36517       parser->oacc_routine = &data;
36518     }
36519
36520   tree decl = NULL_TREE;
36521   /* Create a dummy claue, to record location.  */
36522   tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36523
36524   if (context != pragma_external)
36525     {
36526       cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36527       parser->oacc_routine->error_seen = true;
36528       parser->oacc_routine = NULL;
36529       return;
36530     }
36531
36532   /* Look for optional '( name )'.  */
36533   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36534     {
36535       if (!first_p)
36536         {
36537           while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36538                  && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36539             cp_lexer_consume_token (parser->lexer);
36540           if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36541             parser->oacc_routine->error_seen = true;
36542           cp_parser_require_pragma_eol (parser, pragma_tok);
36543
36544           error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36545                     "%<#pragma acc routine%> not followed by a "
36546                     "function declaration or definition");
36547
36548           parser->oacc_routine->error_seen = true;
36549           return;
36550         }
36551
36552       cp_lexer_consume_token (parser->lexer);
36553       cp_token *token = cp_lexer_peek_token (parser->lexer);
36554
36555       /* We parse the name as an id-expression.  If it resolves to
36556          anything other than a non-overloaded function at namespace
36557          scope, it's an error.  */
36558       tree id = cp_parser_id_expression (parser,
36559                                          /*template_keyword_p=*/false,
36560                                          /*check_dependency_p=*/false,
36561                                          /*template_p=*/NULL,
36562                                          /*declarator_p=*/false,
36563                                          /*optional_p=*/false);
36564       decl = cp_parser_lookup_name_simple (parser, id, token->location);
36565       if (id != error_mark_node && decl == error_mark_node)
36566         cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36567                                      token->location);
36568
36569       if (decl == error_mark_node
36570           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36571         {
36572           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36573           parser->oacc_routine = NULL;
36574           return;
36575         }
36576
36577       /* Build a chain of clauses.  */
36578       parser->lexer->in_pragma = true;
36579       tree clauses = NULL_TREE;
36580       clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36581                                             "#pragma acc routine",
36582                                             cp_lexer_peek_token
36583                                             (parser->lexer));
36584
36585       /* Force clauses to be non-null, by attaching context to it.  */
36586       clauses = tree_cons (c_head, clauses, NULL_TREE);
36587
36588       if (decl && is_overloaded_fn (decl)
36589           && (TREE_CODE (decl) != FUNCTION_DECL
36590               || DECL_FUNCTION_TEMPLATE_P  (decl)))
36591         {
36592           error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36593           parser->oacc_routine = NULL;
36594           return;
36595         }
36596
36597       /* Perhaps we should use the same rule as declarations in different
36598          namespaces?  */
36599       if (!DECL_NAMESPACE_SCOPE_P (decl))
36600         {
36601           error_at (loc, "%<#pragma acc routine%> does not refer to a "
36602                     "namespace scope function");
36603           parser->oacc_routine = NULL;
36604           return;
36605         }
36606
36607       if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36608         {
36609           error_at (loc,
36610                     "%<#pragma acc routine%> does not refer to a function");
36611           parser->oacc_routine = NULL;
36612           return;
36613         }
36614
36615       data.clauses = clauses;
36616
36617       cp_finalize_oacc_routine (parser, decl, false);
36618       data.tokens.release ();
36619       parser->oacc_routine = NULL;
36620     }
36621   else
36622     {
36623       while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36624              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36625         cp_lexer_consume_token (parser->lexer);
36626       if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36627         parser->oacc_routine->error_seen = true;
36628       cp_parser_require_pragma_eol (parser, pragma_tok);
36629
36630       struct cp_token_cache *cp
36631         = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36632       parser->oacc_routine->tokens.safe_push (cp);
36633
36634       if (first_p)
36635         parser->oacc_routine->clauses = c_head;
36636
36637       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36638         cp_parser_pragma (parser, context, NULL);
36639
36640       if (first_p)
36641         {
36642           /* Create an empty list of clauses.  */
36643           parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36644                                                      NULL_TREE);
36645           cp_parser_declaration (parser);
36646
36647           if (parser->oacc_routine
36648               && !parser->oacc_routine->error_seen
36649               && !parser->oacc_routine->fndecl_seen)
36650             error_at (loc, "%<#pragma acc routine%> not followed by a "
36651                       "function declaration or definition");
36652
36653           data.tokens.release ();
36654           parser->oacc_routine = NULL;
36655         }
36656     }
36657 }
36658
36659 /* Finalize #pragma acc routine clauses after direct declarator has
36660    been parsed, and put that into "oacc function" attribute.  */
36661
36662 static tree
36663 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36664 {
36665   struct cp_token_cache *ce;
36666   cp_omp_declare_simd_data *data = parser->oacc_routine;
36667   tree cl, clauses = parser->oacc_routine->clauses;
36668   location_t loc;
36669
36670   loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36671   
36672   if ((!data->error_seen && data->fndecl_seen)
36673       || data->tokens.length () != 1)
36674     {
36675       error_at (loc, "%<#pragma acc routine%> not followed by a "
36676                 "function declaration or definition");
36677       data->error_seen = true;
36678       return attrs;
36679     }
36680   if (data->error_seen)
36681     return attrs;
36682
36683   ce = data->tokens[0];
36684
36685   cp_parser_push_lexer_for_tokens (parser, ce);
36686   parser->lexer->in_pragma = true;
36687   gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36688
36689   cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36690   cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36691                                   "#pragma acc routine", pragma_tok);
36692   cp_parser_pop_lexer (parser);
36693
36694   tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36695
36696   /* Force clauses to be non-null, by attaching context to it.  */
36697   parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36698
36699   data->fndecl_seen = true;
36700   return attrs;
36701 }
36702
36703 /* Apply any saved OpenACC routine clauses to a just-parsed
36704    declaration.  */
36705
36706 static void
36707 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36708 {
36709   if (__builtin_expect (parser->oacc_routine != NULL, 0))
36710     {
36711       tree clauses = parser->oacc_routine->clauses;
36712       location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36713
36714       if (parser->oacc_routine->error_seen)
36715         return;
36716       
36717       if (fndecl == error_mark_node)
36718         {
36719           parser->oacc_routine = NULL;
36720           return;
36721         }
36722
36723       if (TREE_CODE (fndecl) != FUNCTION_DECL)
36724         {
36725           cp_ensure_no_oacc_routine (parser);
36726           return;
36727         }
36728
36729       if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36730         {
36731           error_at (loc,
36732                     "%<#pragma acc routine%> not followed by a function "
36733                     "declaration or definition");
36734           parser->oacc_routine = NULL;
36735         }
36736           
36737       if (get_oacc_fn_attrib (fndecl))
36738         {
36739           error_at (loc, "%<#pragma acc routine%> already applied to %D",
36740                     fndecl);
36741           parser->oacc_routine = NULL;
36742         }
36743
36744       if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36745         {
36746           error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36747                     TREE_USED (fndecl) ? "use" : "definition");
36748           parser->oacc_routine = NULL;
36749         }
36750
36751       /* Process for function attrib  */
36752       tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36753       replace_oacc_fn_attrib (fndecl, dims);
36754       
36755       /* Add an "omp target" attribute.  */
36756       DECL_ATTRIBUTES (fndecl)
36757         = tree_cons (get_identifier ("omp declare target"),
36758                      NULL_TREE, DECL_ATTRIBUTES (fndecl));
36759     }
36760 }
36761
36762 /* Main entry point to OpenMP statement pragmas.  */
36763
36764 static void
36765 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36766 {
36767   tree stmt;
36768   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36769   omp_clause_mask mask (0);
36770
36771   switch (cp_parser_pragma_kind (pragma_tok))
36772     {
36773     case PRAGMA_OACC_ATOMIC:
36774       cp_parser_omp_atomic (parser, pragma_tok);
36775       return;
36776     case PRAGMA_OACC_CACHE:
36777       stmt = cp_parser_oacc_cache (parser, pragma_tok);
36778       break;
36779     case PRAGMA_OACC_DATA:
36780       stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36781       break;
36782     case PRAGMA_OACC_ENTER_DATA:
36783       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36784       break;
36785     case PRAGMA_OACC_EXIT_DATA:
36786       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36787       break;
36788     case PRAGMA_OACC_HOST_DATA:
36789       stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36790       break;
36791     case PRAGMA_OACC_KERNELS:
36792     case PRAGMA_OACC_PARALLEL:
36793       strcpy (p_name, "#pragma acc");
36794       stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36795                                               if_p);
36796       break;
36797     case PRAGMA_OACC_LOOP:
36798       strcpy (p_name, "#pragma acc");
36799       stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36800                                   if_p);
36801       break;
36802     case PRAGMA_OACC_UPDATE:
36803       stmt = cp_parser_oacc_update (parser, pragma_tok);
36804       break;
36805     case PRAGMA_OACC_WAIT:
36806       stmt = cp_parser_oacc_wait (parser, pragma_tok);
36807       break;
36808     case PRAGMA_OMP_ATOMIC:
36809       cp_parser_omp_atomic (parser, pragma_tok);
36810       return;
36811     case PRAGMA_OMP_CRITICAL:
36812       stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36813       break;
36814     case PRAGMA_OMP_DISTRIBUTE:
36815       strcpy (p_name, "#pragma omp");
36816       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36817                                        if_p);
36818       break;
36819     case PRAGMA_OMP_FOR:
36820       strcpy (p_name, "#pragma omp");
36821       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36822                                 if_p);
36823       break;
36824     case PRAGMA_OMP_MASTER:
36825       stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36826       break;
36827     case PRAGMA_OMP_PARALLEL:
36828       strcpy (p_name, "#pragma omp");
36829       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36830                                      if_p);
36831       break;
36832     case PRAGMA_OMP_SECTIONS:
36833       strcpy (p_name, "#pragma omp");
36834       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36835       break;
36836     case PRAGMA_OMP_SIMD:
36837       strcpy (p_name, "#pragma omp");
36838       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36839                                  if_p);
36840       break;
36841     case PRAGMA_OMP_SINGLE:
36842       stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36843       break;
36844     case PRAGMA_OMP_TASK:
36845       stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36846       break;
36847     case PRAGMA_OMP_TASKGROUP:
36848       stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36849       break;
36850     case PRAGMA_OMP_TASKLOOP:
36851       strcpy (p_name, "#pragma omp");
36852       stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36853                                      if_p);
36854       break;
36855     case PRAGMA_OMP_TEAMS:
36856       strcpy (p_name, "#pragma omp");
36857       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36858                                   if_p);
36859       break;
36860     default:
36861       gcc_unreachable ();
36862     }
36863
36864   protected_set_expr_location (stmt, pragma_tok->location);
36865 }
36866 \f
36867 /* Transactional Memory parsing routines.  */
36868
36869 /* Parse a transaction attribute.
36870
36871    txn-attribute:
36872         attribute
36873         [ [ identifier ] ]
36874
36875    We use this instead of cp_parser_attributes_opt for transactions to avoid
36876    the pedwarn in C++98 mode.  */
36877
36878 static tree
36879 cp_parser_txn_attribute_opt (cp_parser *parser)
36880 {
36881   cp_token *token;
36882   tree attr_name, attr = NULL;
36883
36884   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36885     return cp_parser_attributes_opt (parser);
36886
36887   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36888     return NULL_TREE;
36889   cp_lexer_consume_token (parser->lexer);
36890   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36891     goto error1;
36892
36893   token = cp_lexer_peek_token (parser->lexer);
36894   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36895     {
36896       token = cp_lexer_consume_token (parser->lexer);
36897
36898       attr_name = (token->type == CPP_KEYWORD
36899                    /* For keywords, use the canonical spelling,
36900                       not the parsed identifier.  */
36901                    ? ridpointers[(int) token->keyword]
36902                    : token->u.value);
36903       attr = build_tree_list (attr_name, NULL_TREE);
36904     }
36905   else
36906     cp_parser_error (parser, "expected identifier");
36907
36908   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36909  error1:
36910   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36911   return attr;
36912 }
36913
36914 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36915
36916    transaction-statement:
36917      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36918        compound-statement
36919      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36920 */
36921
36922 static tree
36923 cp_parser_transaction (cp_parser *parser, cp_token *token)
36924 {
36925   unsigned char old_in = parser->in_transaction;
36926   unsigned char this_in = 1, new_in;
36927   enum rid keyword = token->keyword;
36928   tree stmt, attrs, noex;
36929
36930   cp_lexer_consume_token (parser->lexer);
36931
36932   if (keyword == RID_TRANSACTION_RELAXED
36933       || keyword == RID_SYNCHRONIZED)
36934     this_in |= TM_STMT_ATTR_RELAXED;
36935   else
36936     {
36937       attrs = cp_parser_txn_attribute_opt (parser);
36938       if (attrs)
36939         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36940     }
36941
36942   /* Parse a noexcept specification.  */
36943   if (keyword == RID_ATOMIC_NOEXCEPT)
36944     noex = boolean_true_node;
36945   else if (keyword == RID_ATOMIC_CANCEL)
36946     {
36947       /* cancel-and-throw is unimplemented.  */
36948       sorry ("atomic_cancel");
36949       noex = NULL_TREE;
36950     }
36951   else
36952     noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36953
36954   /* Keep track if we're in the lexical scope of an outer transaction.  */
36955   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36956
36957   stmt = begin_transaction_stmt (token->location, NULL, this_in);
36958
36959   parser->in_transaction = new_in;
36960   cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36961   parser->in_transaction = old_in;
36962
36963   finish_transaction_stmt (stmt, NULL, this_in, noex);
36964
36965   return stmt;
36966 }
36967
36968 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36969
36970    transaction-expression:
36971      __transaction_atomic txn-noexcept-spec[opt] ( expression )
36972      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36973 */
36974
36975 static tree
36976 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36977 {
36978   unsigned char old_in = parser->in_transaction;
36979   unsigned char this_in = 1;
36980   cp_token *token;
36981   tree expr, noex;
36982   bool noex_expr;
36983   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36984
36985   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36986       || keyword == RID_TRANSACTION_RELAXED);
36987
36988   if (!flag_tm)
36989     error_at (loc,
36990               keyword == RID_TRANSACTION_RELAXED
36991               ? G_("%<__transaction_relaxed%> without transactional memory "
36992                   "support enabled")
36993               : G_("%<__transaction_atomic%> without transactional memory "
36994                    "support enabled"));
36995
36996   token = cp_parser_require_keyword (parser, keyword,
36997       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36998           : RT_TRANSACTION_RELAXED));
36999   gcc_assert (token != NULL);
37000
37001   if (keyword == RID_TRANSACTION_RELAXED)
37002     this_in |= TM_STMT_ATTR_RELAXED;
37003
37004   /* Set this early.  This might mean that we allow transaction_cancel in
37005      an expression that we find out later actually has to be a constexpr.
37006      However, we expect that cxx_constant_value will be able to deal with
37007      this; also, if the noexcept has no constexpr, then what we parse next
37008      really is a transaction's body.  */
37009   parser->in_transaction = this_in;
37010
37011   /* Parse a noexcept specification.  */
37012   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37013                                                true);
37014
37015   if (!noex || !noex_expr
37016       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37017     {
37018       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37019
37020       expr = cp_parser_expression (parser);
37021       expr = finish_parenthesized_expr (expr);
37022
37023       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37024     }
37025   else
37026     {
37027       /* The only expression that is available got parsed for the noexcept
37028          already.  noexcept is true then.  */
37029       expr = noex;
37030       noex = boolean_true_node;
37031     }
37032
37033   expr = build_transaction_expr (token->location, expr, this_in, noex);
37034   parser->in_transaction = old_in;
37035
37036   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37037     return error_mark_node;
37038
37039   return (flag_tm ? expr : error_mark_node);
37040 }
37041
37042 /* Parse a function-transaction-block.
37043
37044    function-transaction-block:
37045      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37046          function-body
37047      __transaction_atomic txn-attribute[opt] function-try-block
37048      __transaction_relaxed ctor-initializer[opt] function-body
37049      __transaction_relaxed function-try-block
37050 */
37051
37052 static bool
37053 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37054 {
37055   unsigned char old_in = parser->in_transaction;
37056   unsigned char new_in = 1;
37057   tree compound_stmt, stmt, attrs;
37058   bool ctor_initializer_p;
37059   cp_token *token;
37060
37061   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37062       || keyword == RID_TRANSACTION_RELAXED);
37063   token = cp_parser_require_keyword (parser, keyword,
37064       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37065           : RT_TRANSACTION_RELAXED));
37066   gcc_assert (token != NULL);
37067
37068   if (keyword == RID_TRANSACTION_RELAXED)
37069     new_in |= TM_STMT_ATTR_RELAXED;
37070   else
37071     {
37072       attrs = cp_parser_txn_attribute_opt (parser);
37073       if (attrs)
37074         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37075     }
37076
37077   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37078
37079   parser->in_transaction = new_in;
37080
37081   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37082     ctor_initializer_p = cp_parser_function_try_block (parser);
37083   else
37084     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37085       (parser, /*in_function_try_block=*/false);
37086
37087   parser->in_transaction = old_in;
37088
37089   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37090
37091   return ctor_initializer_p;
37092 }
37093
37094 /* Parse a __transaction_cancel statement.
37095
37096    cancel-statement:
37097      __transaction_cancel txn-attribute[opt] ;
37098      __transaction_cancel txn-attribute[opt] throw-expression ;
37099
37100    ??? Cancel and throw is not yet implemented.  */
37101
37102 static tree
37103 cp_parser_transaction_cancel (cp_parser *parser)
37104 {
37105   cp_token *token;
37106   bool is_outer = false;
37107   tree stmt, attrs;
37108
37109   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37110                                      RT_TRANSACTION_CANCEL);
37111   gcc_assert (token != NULL);
37112
37113   attrs = cp_parser_txn_attribute_opt (parser);
37114   if (attrs)
37115     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37116
37117   /* ??? Parse cancel-and-throw here.  */
37118
37119   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37120
37121   if (!flag_tm)
37122     {
37123       error_at (token->location, "%<__transaction_cancel%> without "
37124                 "transactional memory support enabled");
37125       return error_mark_node;
37126     }
37127   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37128     {
37129       error_at (token->location, "%<__transaction_cancel%> within a "
37130                 "%<__transaction_relaxed%>");
37131       return error_mark_node;
37132     }
37133   else if (is_outer)
37134     {
37135       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37136           && !is_tm_may_cancel_outer (current_function_decl))
37137         {
37138           error_at (token->location, "outer %<__transaction_cancel%> not "
37139                     "within outer %<__transaction_atomic%>");
37140           error_at (token->location,
37141                     "  or a %<transaction_may_cancel_outer%> function");
37142           return error_mark_node;
37143         }
37144     }
37145   else if (parser->in_transaction == 0)
37146     {
37147       error_at (token->location, "%<__transaction_cancel%> not within "
37148                 "%<__transaction_atomic%>");
37149       return error_mark_node;
37150     }
37151
37152   stmt = build_tm_abort_call (token->location, is_outer);
37153   add_stmt (stmt);
37154
37155   return stmt;
37156 }
37157 \f
37158 /* The parser.  */
37159
37160 static GTY (()) cp_parser *the_parser;
37161
37162 \f
37163 /* Special handling for the first token or line in the file.  The first
37164    thing in the file might be #pragma GCC pch_preprocess, which loads a
37165    PCH file, which is a GC collection point.  So we need to handle this
37166    first pragma without benefit of an existing lexer structure.
37167
37168    Always returns one token to the caller in *FIRST_TOKEN.  This is
37169    either the true first token of the file, or the first token after
37170    the initial pragma.  */
37171
37172 static void
37173 cp_parser_initial_pragma (cp_token *first_token)
37174 {
37175   tree name = NULL;
37176
37177   cp_lexer_get_preprocessor_token (NULL, first_token);
37178   if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37179     return;
37180
37181   cp_lexer_get_preprocessor_token (NULL, first_token);
37182   if (first_token->type == CPP_STRING)
37183     {
37184       name = first_token->u.value;
37185
37186       cp_lexer_get_preprocessor_token (NULL, first_token);
37187       if (first_token->type != CPP_PRAGMA_EOL)
37188         error_at (first_token->location,
37189                   "junk at end of %<#pragma GCC pch_preprocess%>");
37190     }
37191   else
37192     error_at (first_token->location, "expected string literal");
37193
37194   /* Skip to the end of the pragma.  */
37195   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37196     cp_lexer_get_preprocessor_token (NULL, first_token);
37197
37198   /* Now actually load the PCH file.  */
37199   if (name)
37200     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37201
37202   /* Read one more token to return to our caller.  We have to do this
37203      after reading the PCH file in, since its pointers have to be
37204      live.  */
37205   cp_lexer_get_preprocessor_token (NULL, first_token);
37206 }
37207
37208 /* Parses the grainsize pragma for the _Cilk_for statement.
37209    Syntax:
37210    #pragma cilk grainsize = <VALUE>.  */
37211
37212 static void
37213 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37214 {
37215   if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37216     {
37217       tree exp = cp_parser_binary_expression (parser, false, false,
37218                                               PREC_NOT_OPERATOR, NULL);
37219       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37220       if (!exp || exp == error_mark_node)
37221         {
37222           error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37223           return;
37224         }
37225
37226       /* Make sure the next token is _Cilk_for, it is invalid otherwise.  */
37227       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37228         cp_parser_cilk_for (parser, exp, if_p);
37229       else
37230         warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37231                     "%<#pragma cilk grainsize%> is not followed by "
37232                     "%<_Cilk_for%>");
37233       return;
37234     }
37235   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37236 }
37237
37238 /* Normal parsing of a pragma token.  Here we can (and must) use the
37239    regular lexer.  */
37240
37241 static bool
37242 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37243 {
37244   cp_token *pragma_tok;
37245   unsigned int id;
37246   tree stmt;
37247   bool ret;
37248
37249   pragma_tok = cp_lexer_consume_token (parser->lexer);
37250   gcc_assert (pragma_tok->type == CPP_PRAGMA);
37251   parser->lexer->in_pragma = true;
37252
37253   id = cp_parser_pragma_kind (pragma_tok);
37254   if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37255     cp_ensure_no_omp_declare_simd (parser);
37256   switch (id)
37257     {
37258     case PRAGMA_GCC_PCH_PREPROCESS:
37259       error_at (pragma_tok->location,
37260                 "%<#pragma GCC pch_preprocess%> must be first");
37261       break;
37262
37263     case PRAGMA_OMP_BARRIER:
37264       switch (context)
37265         {
37266         case pragma_compound:
37267           cp_parser_omp_barrier (parser, pragma_tok);
37268           return false;
37269         case pragma_stmt:
37270           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37271                     "used in compound statements");
37272           break;
37273         default:
37274           goto bad_stmt;
37275         }
37276       break;
37277
37278     case PRAGMA_OMP_FLUSH:
37279       switch (context)
37280         {
37281         case pragma_compound:
37282           cp_parser_omp_flush (parser, pragma_tok);
37283           return false;
37284         case pragma_stmt:
37285           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37286                     "used in compound statements");
37287           break;
37288         default:
37289           goto bad_stmt;
37290         }
37291       break;
37292
37293     case PRAGMA_OMP_TASKWAIT:
37294       switch (context)
37295         {
37296         case pragma_compound:
37297           cp_parser_omp_taskwait (parser, pragma_tok);
37298           return false;
37299         case pragma_stmt:
37300           error_at (pragma_tok->location,
37301                     "%<#pragma omp taskwait%> may only be "
37302                     "used in compound statements");
37303           break;
37304         default:
37305           goto bad_stmt;
37306         }
37307       break;
37308
37309     case PRAGMA_OMP_TASKYIELD:
37310       switch (context)
37311         {
37312         case pragma_compound:
37313           cp_parser_omp_taskyield (parser, pragma_tok);
37314           return false;
37315         case pragma_stmt:
37316           error_at (pragma_tok->location,
37317                     "%<#pragma omp taskyield%> may only be "
37318                     "used in compound statements");
37319           break;
37320         default:
37321           goto bad_stmt;
37322         }
37323       break;
37324
37325     case PRAGMA_OMP_CANCEL:
37326       switch (context)
37327         {
37328         case pragma_compound:
37329           cp_parser_omp_cancel (parser, pragma_tok);
37330           return false;
37331         case pragma_stmt:
37332           error_at (pragma_tok->location,
37333                     "%<#pragma omp cancel%> may only be "
37334                     "used in compound statements");
37335           break;
37336         default:
37337           goto bad_stmt;
37338         }
37339       break;
37340
37341     case PRAGMA_OMP_CANCELLATION_POINT:
37342       switch (context)
37343         {
37344         case pragma_compound:
37345           cp_parser_omp_cancellation_point (parser, pragma_tok);
37346           return false;
37347         case pragma_stmt:
37348           error_at (pragma_tok->location,
37349                     "%<#pragma omp cancellation point%> may only be "
37350                     "used in compound statements");
37351           break;
37352         default:
37353           goto bad_stmt;
37354         }
37355       break;
37356
37357     case PRAGMA_OMP_THREADPRIVATE:
37358       cp_parser_omp_threadprivate (parser, pragma_tok);
37359       return false;
37360
37361     case PRAGMA_OMP_DECLARE_REDUCTION:
37362       cp_parser_omp_declare (parser, pragma_tok, context);
37363       return false;
37364
37365     case PRAGMA_OACC_DECLARE:
37366       cp_parser_oacc_declare (parser, pragma_tok);
37367       return false;
37368
37369     case PRAGMA_OACC_ROUTINE:
37370       cp_parser_oacc_routine (parser, pragma_tok, context);
37371       return false;
37372
37373     case PRAGMA_OACC_ATOMIC:
37374     case PRAGMA_OACC_CACHE:
37375     case PRAGMA_OACC_DATA:
37376     case PRAGMA_OACC_ENTER_DATA:
37377     case PRAGMA_OACC_EXIT_DATA:
37378     case PRAGMA_OACC_HOST_DATA:
37379     case PRAGMA_OACC_KERNELS:
37380     case PRAGMA_OACC_PARALLEL:
37381     case PRAGMA_OACC_LOOP:
37382     case PRAGMA_OACC_UPDATE:
37383     case PRAGMA_OACC_WAIT:
37384     case PRAGMA_OMP_ATOMIC:
37385     case PRAGMA_OMP_CRITICAL:
37386     case PRAGMA_OMP_DISTRIBUTE:
37387     case PRAGMA_OMP_FOR:
37388     case PRAGMA_OMP_MASTER:
37389     case PRAGMA_OMP_PARALLEL:
37390     case PRAGMA_OMP_SECTIONS:
37391     case PRAGMA_OMP_SIMD:
37392     case PRAGMA_OMP_SINGLE:
37393     case PRAGMA_OMP_TASK:
37394     case PRAGMA_OMP_TASKGROUP:
37395     case PRAGMA_OMP_TASKLOOP:
37396     case PRAGMA_OMP_TEAMS:
37397       if (context != pragma_stmt && context != pragma_compound)
37398         goto bad_stmt;
37399       stmt = push_omp_privatization_clauses (false);
37400       cp_parser_omp_construct (parser, pragma_tok, if_p);
37401       pop_omp_privatization_clauses (stmt);
37402       return true;
37403
37404     case PRAGMA_OMP_ORDERED:
37405       stmt = push_omp_privatization_clauses (false);
37406       ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37407       pop_omp_privatization_clauses (stmt);
37408       return ret;
37409
37410     case PRAGMA_OMP_TARGET:
37411       stmt = push_omp_privatization_clauses (false);
37412       ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37413       pop_omp_privatization_clauses (stmt);
37414       return ret;
37415
37416     case PRAGMA_OMP_END_DECLARE_TARGET:
37417       cp_parser_omp_end_declare_target (parser, pragma_tok);
37418       return false;
37419
37420     case PRAGMA_OMP_SECTION:
37421       error_at (pragma_tok->location, 
37422                 "%<#pragma omp section%> may only be used in "
37423                 "%<#pragma omp sections%> construct");
37424       break;
37425
37426     case PRAGMA_IVDEP:
37427       {
37428         if (context == pragma_external)
37429           {
37430             error_at (pragma_tok->location,
37431                       "%<#pragma GCC ivdep%> must be inside a function");
37432             break;
37433           }
37434         cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37435         cp_token *tok;
37436         tok = cp_lexer_peek_token (the_parser->lexer);
37437         if (tok->type != CPP_KEYWORD
37438             || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37439                 && tok->keyword != RID_DO))
37440           {
37441             cp_parser_error (parser, "for, while or do statement expected");
37442             return false;
37443           }
37444         cp_parser_iteration_statement (parser, if_p, true);
37445         return true;
37446       }
37447
37448     case PRAGMA_CILK_SIMD:
37449       if (context == pragma_external)
37450         {
37451           error_at (pragma_tok->location,
37452                     "%<#pragma simd%> must be inside a function");
37453           break;
37454         }
37455       stmt = push_omp_privatization_clauses (false);
37456       cp_parser_cilk_simd (parser, pragma_tok, if_p);
37457       pop_omp_privatization_clauses (stmt);
37458       return true;
37459
37460     case PRAGMA_CILK_GRAINSIZE:
37461       if (context == pragma_external)
37462         {
37463           error_at (pragma_tok->location,
37464                     "%<#pragma cilk grainsize%> must be inside a function");
37465           break;
37466         }
37467
37468       /* Ignore the pragma if Cilk Plus is not enabled.  */
37469       if (flag_cilkplus)
37470         {
37471           cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37472           return true;
37473         }
37474       else
37475         {
37476           error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37477                     "%<#pragma cilk grainsize%>");
37478           break;
37479         }
37480
37481     default:
37482       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37483       c_invoke_pragma_handler (id);
37484       break;
37485
37486     bad_stmt:
37487       cp_parser_error (parser, "expected declaration specifiers");
37488       break;
37489     }
37490
37491   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37492   return false;
37493 }
37494
37495 /* The interface the pragma parsers have to the lexer.  */
37496
37497 enum cpp_ttype
37498 pragma_lex (tree *value, location_t *loc)
37499 {
37500   cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37501   enum cpp_ttype ret = tok->type;
37502
37503   *value = tok->u.value;
37504   if (loc)
37505     *loc = tok->location;
37506
37507   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37508     ret = CPP_EOF;
37509   else if (ret == CPP_STRING)
37510     *value = cp_parser_string_literal (the_parser, false, false);
37511   else
37512     {
37513       if (ret == CPP_KEYWORD)
37514         ret = CPP_NAME;
37515       cp_lexer_consume_token (the_parser->lexer);
37516     }
37517
37518   return ret;
37519 }
37520
37521 \f
37522 /* External interface.  */
37523
37524 /* Parse one entire translation unit.  */
37525
37526 void
37527 c_parse_file (void)
37528 {
37529   static bool already_called = false;
37530
37531   if (already_called)
37532     fatal_error (input_location,
37533                  "inter-module optimizations not implemented for C++");
37534   already_called = true;
37535
37536   the_parser = cp_parser_new ();
37537   push_deferring_access_checks (flag_access_control
37538                                 ? dk_no_deferred : dk_no_check);
37539   cp_parser_translation_unit (the_parser);
37540   the_parser = NULL;
37541 }
37542
37543 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's 
37544    vectorlength clause:
37545    Syntax:
37546    vectorlength ( constant-expression )  */
37547
37548 static tree
37549 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37550                                   bool is_simd_fn)
37551 {
37552   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37553   tree expr;
37554   /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37555      safelen clause.  Thus, vectorlength is represented as OMP 4.0
37556      safelen.  For SIMD-enabled function it is represented by OMP 4.0
37557      simdlen.  */
37558   if (!is_simd_fn)
37559     check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", 
37560                                loc);
37561   else
37562     check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37563                                loc);
37564
37565   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37566     return error_mark_node;
37567
37568   expr = cp_parser_constant_expression (parser);
37569   expr = maybe_constant_value (expr);
37570
37571   /* If expr == error_mark_node, then don't emit any errors nor
37572      create a clause.  if any of the above functions returns
37573      error mark node then they would have emitted an error message.  */
37574   if (expr == error_mark_node)
37575     ;
37576   else if (!TREE_TYPE (expr)
37577            || !TREE_CONSTANT (expr)
37578            || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37579     error_at (loc, "vectorlength must be an integer constant");
37580   else if (TREE_CONSTANT (expr)
37581            && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37582     error_at (loc, "vectorlength must be a power of 2");
37583   else 
37584     {
37585       tree c;
37586       if (!is_simd_fn)
37587         { 
37588           c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN); 
37589           OMP_CLAUSE_SAFELEN_EXPR (c) = expr; 
37590           OMP_CLAUSE_CHAIN (c) = clauses; 
37591           clauses = c;
37592         }
37593       else
37594         {
37595           c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37596           OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37597           OMP_CLAUSE_CHAIN (c) = clauses;
37598           clauses = c;
37599         }
37600     }
37601
37602   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37603     return error_mark_node;
37604   return clauses;
37605 }
37606
37607 /* Handles the Cilk Plus #pragma simd linear clause.
37608    Syntax:
37609    linear ( simd-linear-variable-list )
37610
37611    simd-linear-variable-list:
37612      simd-linear-variable
37613      simd-linear-variable-list , simd-linear-variable
37614
37615    simd-linear-variable:
37616      id-expression
37617      id-expression : simd-linear-step
37618
37619    simd-linear-step:
37620    conditional-expression */
37621
37622 static tree
37623 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37624 {
37625   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37626
37627   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37628     return clauses;
37629   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37630     {
37631       cp_parser_error (parser, "expected identifier");
37632       cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37633       return error_mark_node;
37634     }
37635
37636   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37637   parser->colon_corrects_to_scope_p = false;
37638   while (1)
37639     {
37640       cp_token *token = cp_lexer_peek_token (parser->lexer);
37641       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37642         {
37643           cp_parser_error (parser, "expected variable-name");
37644           clauses = error_mark_node;
37645           break;
37646         }
37647
37648       tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37649                                                false, false);
37650       tree decl = cp_parser_lookup_name_simple (parser, var_name,
37651                                                 token->location);
37652       if (decl == error_mark_node)
37653         {
37654           cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37655                                        token->location);
37656           clauses = error_mark_node;
37657         }
37658       else
37659         {
37660           tree e = NULL_TREE;
37661           tree step_size = integer_one_node;
37662
37663           /* If present, parse the linear step.  Otherwise, assume the default
37664              value of 1.  */
37665           if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37666             {
37667               cp_lexer_consume_token (parser->lexer);
37668
37669               e = cp_parser_assignment_expression (parser);
37670               e = maybe_constant_value (e);
37671
37672               if (e == error_mark_node)
37673                 {
37674                   /* If an error has occurred,  then the whole pragma is
37675                      considered ill-formed.  Thus, no reason to keep
37676                      parsing.  */
37677                   clauses = error_mark_node;
37678                   break;
37679                 }
37680               else if (type_dependent_expression_p (e)
37681                        || value_dependent_expression_p (e)
37682                        || (TREE_TYPE (e)
37683                            && INTEGRAL_TYPE_P (TREE_TYPE (e))
37684                            && (TREE_CONSTANT (e)
37685                                || DECL_P (e))))
37686                 step_size = e;
37687               else
37688                 cp_parser_error (parser,
37689                                  "step size must be an integer constant "
37690                                  "expression or an integer variable");
37691             }
37692
37693           /* Use the OMP_CLAUSE_LINEAR,  which has the same semantics.  */
37694           tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37695           OMP_CLAUSE_DECL (l) = decl;
37696           OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37697           OMP_CLAUSE_CHAIN (l) = clauses;
37698           clauses = l;
37699         }
37700       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37701         cp_lexer_consume_token (parser->lexer);
37702       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37703         break;
37704       else
37705         {
37706           error_at (cp_lexer_peek_token (parser->lexer)->location,
37707                     "expected %<,%> or %<)%> after %qE", decl);
37708           clauses = error_mark_node;
37709           break;
37710         }
37711     }
37712   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37713   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37714   return clauses;
37715 }
37716
37717 /* Returns the name of the next clause.  If the clause is not
37718    recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37719    token is not consumed.  Otherwise, the appropriate enum from the
37720    pragma_simd_clause is returned and the token is consumed.  */
37721
37722 static pragma_omp_clause
37723 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37724 {
37725   pragma_omp_clause clause_type;
37726   cp_token *token = cp_lexer_peek_token (parser->lexer);
37727
37728   if (token->keyword == RID_PRIVATE)
37729     clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37730   else if (!token->u.value || token->type != CPP_NAME)
37731     return PRAGMA_CILK_CLAUSE_NONE;
37732   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37733     clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37734   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37735     clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37736   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37737     clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37738   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37739     clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37740   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37741     clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37742   else
37743     return PRAGMA_CILK_CLAUSE_NONE;
37744
37745   cp_lexer_consume_token (parser->lexer);
37746   return clause_type;
37747 }
37748
37749 /* Parses all the #pragma simd clauses.  Returns a list of clauses found.  */
37750
37751 static tree
37752 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37753 {
37754   tree clauses = NULL_TREE;
37755
37756   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37757          && clauses != error_mark_node)
37758     {
37759       pragma_omp_clause c_kind;
37760       c_kind = cp_parser_cilk_simd_clause_name (parser);
37761       if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37762         clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37763       else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37764         clauses = cp_parser_cilk_simd_linear (parser, clauses);
37765       else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37766         /* Use the OpenMP 4.0 equivalent function.  */
37767         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37768       else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37769         /* Use the OpenMP 4.0 equivalent function.  */
37770         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37771                                           clauses);
37772       else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37773         /* Use the OMP 4.0 equivalent function.  */
37774         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37775                                           clauses);
37776       else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37777         /* Use the OMP 4.0 equivalent function.  */
37778         clauses = cp_parser_omp_clause_reduction (parser, clauses);
37779       else
37780         {
37781           clauses = error_mark_node;
37782           cp_parser_error (parser, "expected %<#pragma simd%> clause");
37783           break;
37784         }
37785     }
37786
37787   cp_parser_skip_to_pragma_eol (parser, pragma_token);
37788
37789   if (clauses == error_mark_node)
37790     return error_mark_node;
37791   else
37792     return c_finish_cilk_clauses (clauses);
37793 }
37794
37795 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops.  */
37796
37797 static void
37798 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37799 {
37800   tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37801
37802   if (clauses == error_mark_node)
37803     return;
37804
37805   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37806     {
37807       error_at (cp_lexer_peek_token (parser->lexer)->location,
37808                 "for statement expected");
37809       return;
37810     }
37811
37812   tree sb = begin_omp_structured_block ();
37813   int save = cp_parser_begin_omp_structured_block (parser);
37814   tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37815   if (ret)
37816     cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37817   cp_parser_end_omp_structured_block (parser, save);
37818   add_stmt (finish_omp_structured_block (sb));
37819 }
37820
37821 /* Main entry-point for parsing Cilk Plus _Cilk_for
37822    loops.  The return value is error_mark_node
37823    when errors happen and CILK_FOR tree on success.  */
37824
37825 static tree
37826 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37827 {
37828   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37829     gcc_unreachable ();
37830
37831   tree sb = begin_omp_structured_block ();
37832   int save = cp_parser_begin_omp_structured_block (parser);
37833
37834   tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37835   OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37836   OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37837   clauses = finish_omp_clauses (clauses, false);
37838
37839   tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37840   if (ret)
37841     cpp_validate_cilk_plus_loop (ret);
37842   else
37843     ret = error_mark_node;
37844
37845   cp_parser_end_omp_structured_block (parser, save);
37846   add_stmt (finish_omp_structured_block (sb));
37847   return ret;
37848 }
37849
37850 /* Create an identifier for a generic parameter type (a synthesized
37851    template parameter implied by `auto' or a concept identifier). */
37852
37853 static GTY(()) int generic_parm_count;
37854 static tree
37855 make_generic_type_name ()
37856 {
37857   char buf[32];
37858   sprintf (buf, "auto:%d", ++generic_parm_count);
37859   return get_identifier (buf);
37860 }
37861
37862 /* Predicate that behaves as is_auto_or_concept but matches the parent
37863    node of the generic type rather than the generic type itself.  This
37864    allows for type transformation in add_implicit_template_parms.  */
37865
37866 static inline bool
37867 tree_type_is_auto_or_concept (const_tree t)
37868 {
37869   return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37870 }
37871
37872 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37873    (creating a new template parameter list if necessary).  Returns the newly
37874    created template type parm.  */
37875
37876 static tree
37877 synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
37878 {
37879   gcc_assert (current_binding_level->kind == sk_function_parms);
37880
37881    /* Before committing to modifying any scope, if we're in an
37882       implicit template scope, and we're trying to synthesize a
37883       constrained parameter, try to find a previous parameter with
37884       the same name.  This is the same-type rule for abbreviated
37885       function templates.
37886
37887       NOTE: We can generate implicit parameters when tentatively
37888       parsing a nested name specifier, only to reject that parse
37889       later. However, matching the same template-id as part of a
37890       direct-declarator should generate an identical template
37891       parameter, so this rule will merge them. */
37892   if (parser->implicit_template_scope && constr)
37893     {
37894       tree t = parser->implicit_template_parms;
37895       while (t)
37896         {
37897           if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37898             {
37899               tree d = TREE_VALUE (t);
37900               if (TREE_CODE (d) == PARM_DECL)
37901                 /* Return the TEMPLATE_PARM_INDEX.  */
37902                 d = DECL_INITIAL (d);
37903               return d;
37904             }
37905           t = TREE_CHAIN (t);
37906         }
37907     }
37908
37909   /* We are either continuing a function template that already contains implicit
37910      template parameters, creating a new fully-implicit function template, or
37911      extending an existing explicit function template with implicit template
37912      parameters.  */
37913
37914   cp_binding_level *const entry_scope = current_binding_level;
37915
37916   bool become_template = false;
37917   cp_binding_level *parent_scope = 0;
37918
37919   if (parser->implicit_template_scope)
37920     {
37921       gcc_assert (parser->implicit_template_parms);
37922
37923       current_binding_level = parser->implicit_template_scope;
37924     }
37925   else
37926     {
37927       /* Roll back to the existing template parameter scope (in the case of
37928          extending an explicit function template) or introduce a new template
37929          parameter scope ahead of the function parameter scope (or class scope
37930          in the case of out-of-line member definitions).  The function scope is
37931          added back after template parameter synthesis below.  */
37932
37933       cp_binding_level *scope = entry_scope;
37934
37935       while (scope->kind == sk_function_parms)
37936         {
37937           parent_scope = scope;
37938           scope = scope->level_chain;
37939         }
37940       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37941         {
37942           /* If not defining a class, then any class scope is a scope level in
37943              an out-of-line member definition.  In this case simply wind back
37944              beyond the first such scope to inject the template parameter list.
37945              Otherwise wind back to the class being defined.  The latter can
37946              occur in class member friend declarations such as:
37947
37948                class A {
37949                  void foo (auto);
37950                };
37951                class B {
37952                  friend void A::foo (auto);
37953                };
37954
37955             The template parameter list synthesized for the friend declaration
37956             must be injected in the scope of 'B'.  This can also occur in
37957             erroneous cases such as:
37958
37959                struct A {
37960                  struct B {
37961                    void foo (auto);
37962                  };
37963                  void B::foo (auto) {}
37964                };
37965
37966             Here the attempted definition of 'B::foo' within 'A' is ill-formed
37967             but, nevertheless, the template parameter list synthesized for the
37968             declarator should be injected into the scope of 'A' as if the
37969             ill-formed template was specified explicitly.  */
37970
37971           while (scope->kind == sk_class && !scope->defining_class_p)
37972             {
37973               parent_scope = scope;
37974               scope = scope->level_chain;
37975             }
37976         }
37977
37978       current_binding_level = scope;
37979
37980       if (scope->kind != sk_template_parms
37981           || !function_being_declared_is_template_p (parser))
37982         {
37983           /* Introduce a new template parameter list for implicit template
37984              parameters.  */
37985
37986           become_template = true;
37987
37988           parser->implicit_template_scope
37989               = begin_scope (sk_template_parms, NULL);
37990
37991           ++processing_template_decl;
37992
37993           parser->fully_implicit_function_template_p = true;
37994           ++parser->num_template_parameter_lists;
37995         }
37996       else
37997         {
37998           /* Synthesize implicit template parameters at the end of the explicit
37999              template parameter list.  */
38000
38001           gcc_assert (current_template_parms);
38002
38003           parser->implicit_template_scope = scope;
38004
38005           tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38006           parser->implicit_template_parms
38007             = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38008         }
38009     }
38010
38011   /* Synthesize a new template parameter and track the current template
38012      parameter chain with implicit_template_parms.  */
38013
38014   tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38015   tree synth_id = make_generic_type_name ();
38016   tree synth_tmpl_parm;
38017   bool non_type = false;
38018
38019   if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38020     synth_tmpl_parm
38021       = finish_template_type_parm (class_type_node, synth_id);
38022   else if (TREE_CODE (proto) == TEMPLATE_DECL)
38023     synth_tmpl_parm
38024       = finish_constrained_template_template_parm (proto, synth_id);
38025   else
38026     {
38027       synth_tmpl_parm = copy_decl (proto);
38028       DECL_NAME (synth_tmpl_parm) = synth_id;
38029       non_type = true;
38030     }
38031
38032   // Attach the constraint to the parm before processing.
38033   tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38034   TREE_TYPE (node) = constr;
38035   tree new_parm
38036     = process_template_parm (parser->implicit_template_parms,
38037                              input_location,
38038                              node,
38039                              /*non_type=*/non_type,
38040                              /*param_pack=*/false);
38041
38042   // Chain the new parameter to the list of implicit parameters.
38043   if (parser->implicit_template_parms)
38044     parser->implicit_template_parms
38045       = TREE_CHAIN (parser->implicit_template_parms);
38046   else
38047     parser->implicit_template_parms = new_parm;
38048
38049   tree new_decl = getdecls ();
38050   if (non_type)
38051     /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
38052     new_decl = DECL_INITIAL (new_decl);
38053
38054   /* If creating a fully implicit function template, start the new implicit
38055      template parameter list with this synthesized type, otherwise grow the
38056      current template parameter list.  */
38057
38058   if (become_template)
38059     {
38060       parent_scope->level_chain = current_binding_level;
38061
38062       tree new_parms = make_tree_vec (1);
38063       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38064       current_template_parms = tree_cons (size_int (processing_template_decl),
38065                                           new_parms, current_template_parms);
38066     }
38067   else
38068     {
38069       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38070       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38071       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38072       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38073     }
38074
38075   // If the new parameter was constrained, we need to add that to the
38076   // constraints in the template parameter list.
38077   if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38078     {
38079       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38080       reqs = conjoin_constraints (reqs, req);
38081       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38082     }
38083
38084   current_binding_level = entry_scope;
38085
38086   return new_decl;
38087 }
38088
38089 /* Finish the declaration of a fully implicit function template.  Such a
38090    template has no explicit template parameter list so has not been through the
38091    normal template head and tail processing.  synthesize_implicit_template_parm
38092    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
38093    provided if the declaration is a class member such that its template
38094    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
38095    form is returned.  Otherwise NULL_TREE is returned. */
38096
38097 static tree
38098 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38099 {
38100   gcc_assert (parser->fully_implicit_function_template_p);
38101
38102   if (member_decl_opt && member_decl_opt != error_mark_node
38103       && DECL_VIRTUAL_P (member_decl_opt))
38104     {
38105       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38106                 "implicit templates may not be %<virtual%>");
38107       DECL_VIRTUAL_P (member_decl_opt) = false;
38108     }
38109
38110   if (member_decl_opt)
38111     member_decl_opt = finish_member_template_decl (member_decl_opt);
38112   end_template_decl ();
38113
38114   parser->fully_implicit_function_template_p = false;
38115   --parser->num_template_parameter_lists;
38116
38117   return member_decl_opt;
38118 }
38119
38120 #include "gt-cp-parser.h"