Fix cleanup location for try_finally_expr.
[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   /* Consume the `}' and keep location, if there is a `}' and compound_stmt is
10774      not empty. */
10775   cp_token *token = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10776   if (token &&
10777       TREE_CODE(compound_stmt) == STATEMENT_LIST &&
10778       STATEMENT_LIST_HEAD (compound_stmt) &&
10779       STATEMENT_LIST_TAIL (compound_stmt))
10780     {
10781       tree brace_stmt = build0(STATEMENT_LIST_END, void_type_node);
10782       SET_EXPR_LOCATION(brace_stmt, token->location);
10783       add_stmt(brace_stmt);
10784     }
10785   /* Finish the compound-statement.  */
10786   finish_compound_stmt (compound_stmt);
10787   return compound_stmt;
10788 }
10789
10790 /* Parse an (optional) statement-seq.
10791
10792    statement-seq:
10793      statement
10794      statement-seq [opt] statement  */
10795
10796 static void
10797 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10798 {
10799   /* Scan statements until there aren't any more.  */
10800   while (true)
10801     {
10802       cp_token *token = cp_lexer_peek_token (parser->lexer);
10803
10804       /* If we are looking at a `}', then we have run out of
10805          statements; the same is true if we have reached the end
10806          of file, or have stumbled upon a stray '@end'.  */
10807       if (token->type == CPP_CLOSE_BRACE
10808           || token->type == CPP_EOF
10809           || token->type == CPP_PRAGMA_EOL
10810           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10811         break;
10812       
10813       /* If we are in a compound statement and find 'else' then
10814          something went wrong.  */
10815       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10816         {
10817           if (parser->in_statement & IN_IF_STMT) 
10818             break;
10819           else
10820             {
10821               token = cp_lexer_consume_token (parser->lexer);
10822               error_at (token->location, "%<else%> without a previous %<if%>");
10823             }
10824         }
10825
10826       /* Parse the statement.  */
10827       cp_parser_statement (parser, in_statement_expr, true, NULL);
10828     }
10829 }
10830
10831 /* Parse a selection-statement.
10832
10833    selection-statement:
10834      if ( condition ) statement
10835      if ( condition ) statement else statement
10836      switch ( condition ) statement
10837
10838    Returns the new IF_STMT or SWITCH_STMT.
10839
10840    If IF_P is not NULL, *IF_P is set to indicate whether the statement
10841    is a (possibly labeled) if statement which is not enclosed in
10842    braces and has an else clause.  This is used to implement
10843    -Wparentheses.
10844
10845    CHAIN is a vector of if-else-if conditions.  This is used to implement
10846    -Wduplicated-cond.  */
10847
10848 static tree
10849 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10850                                vec<tree> *chain)
10851 {
10852   cp_token *token;
10853   enum rid keyword;
10854   token_indent_info guard_tinfo;
10855
10856   if (if_p != NULL)
10857     *if_p = false;
10858
10859   /* Peek at the next token.  */
10860   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10861   guard_tinfo = get_token_indent_info (token);
10862
10863   /* See what kind of keyword it is.  */
10864   keyword = token->keyword;
10865   switch (keyword)
10866     {
10867     case RID_IF:
10868     case RID_SWITCH:
10869       {
10870         tree statement;
10871         tree condition;
10872
10873         /* Look for the `('.  */
10874         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10875           {
10876             cp_parser_skip_to_end_of_statement (parser);
10877             return error_mark_node;
10878           }
10879
10880         /* Begin the selection-statement.  */
10881         if (keyword == RID_IF)
10882           statement = begin_if_stmt ();
10883         else
10884           statement = begin_switch_stmt ();
10885
10886         /* Parse the condition.  */
10887         condition = cp_parser_condition (parser);
10888         /* Look for the `)'.  */
10889         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10890           cp_parser_skip_to_closing_parenthesis (parser, true, false,
10891                                                  /*consume_paren=*/true);
10892
10893         if (keyword == RID_IF)
10894           {
10895             bool nested_if;
10896             unsigned char in_statement;
10897
10898             /* Add the condition.  */
10899             finish_if_stmt_cond (condition, statement);
10900
10901             if (warn_duplicated_cond)
10902               warn_duplicated_cond_add_or_warn (token->location, condition,
10903                                                 &chain);
10904
10905             /* Parse the then-clause.  */
10906             in_statement = parser->in_statement;
10907             parser->in_statement |= IN_IF_STMT;
10908             cp_parser_implicitly_scoped_statement (parser, &nested_if,
10909                                                    guard_tinfo);
10910             parser->in_statement = in_statement;
10911
10912             finish_then_clause (statement);
10913
10914             /* If the next token is `else', parse the else-clause.  */
10915             if (cp_lexer_next_token_is_keyword (parser->lexer,
10916                                                 RID_ELSE))
10917               {
10918                 guard_tinfo
10919                   = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10920                 /* Consume the `else' keyword.  */
10921                 cp_lexer_consume_token (parser->lexer);
10922                 if (warn_duplicated_cond)
10923                   {
10924                     if (cp_lexer_next_token_is_keyword (parser->lexer,
10925                                                         RID_IF)
10926                         && chain == NULL)
10927                       {
10928                         /* We've got "if (COND) else if (COND2)".  Start
10929                            the condition chain and add COND as the first
10930                            element.  */
10931                         chain = new vec<tree> ();
10932                         if (!CONSTANT_CLASS_P (condition)
10933                             && !TREE_SIDE_EFFECTS (condition))
10934                         {
10935                           /* Wrap it in a NOP_EXPR so that we can set the
10936                              location of the condition.  */
10937                           tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10938                                            condition);
10939                           SET_EXPR_LOCATION (e, token->location);
10940                           chain->safe_push (e);
10941                         }
10942                       }
10943                     else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10944                                                               RID_IF))
10945                       {
10946                         /* This is if-else without subsequent if.  Zap the
10947                            condition chain; we would have already warned at
10948                            this point.  */
10949                         delete chain;
10950                         chain = NULL;
10951                       }
10952                   }
10953                 begin_else_clause (statement);
10954                 /* Parse the else-clause.  */
10955                 cp_parser_implicitly_scoped_statement (parser, NULL,
10956                                                        guard_tinfo, chain);
10957
10958                 finish_else_clause (statement);
10959
10960                 /* If we are currently parsing a then-clause, then
10961                    IF_P will not be NULL.  We set it to true to
10962                    indicate that this if statement has an else clause.
10963                    This may trigger the Wparentheses warning below
10964                    when we get back up to the parent if statement.  */
10965                 if (if_p != NULL)
10966                   *if_p = true;
10967               }
10968             else
10969               {
10970                 /* This if statement does not have an else clause.  If
10971                    NESTED_IF is true, then the then-clause has an if
10972                    statement which does have an else clause.  We warn
10973                    about the potential ambiguity.  */
10974                 if (nested_if)
10975                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10976                               "suggest explicit braces to avoid ambiguous"
10977                               " %<else%>");
10978                 if (warn_duplicated_cond)
10979                   {
10980                     /* We don't need the condition chain anymore.  */
10981                     delete chain;
10982                     chain = NULL;
10983                   }
10984               }
10985
10986             /* Now we're all done with the if-statement.  */
10987             finish_if_stmt (statement);
10988           }
10989         else
10990           {
10991             bool in_switch_statement_p;
10992             unsigned char in_statement;
10993
10994             /* Add the condition.  */
10995             finish_switch_cond (condition, statement);
10996
10997             /* Parse the body of the switch-statement.  */
10998             in_switch_statement_p = parser->in_switch_statement_p;
10999             in_statement = parser->in_statement;
11000             parser->in_switch_statement_p = true;
11001             parser->in_statement |= IN_SWITCH_STMT;
11002             cp_parser_implicitly_scoped_statement (parser, NULL,
11003                                                    guard_tinfo);
11004             parser->in_switch_statement_p = in_switch_statement_p;
11005             parser->in_statement = in_statement;
11006
11007             /* Now we're all done with the switch-statement.  */
11008             finish_switch_stmt (statement);
11009           }
11010
11011         return statement;
11012       }
11013       break;
11014
11015     default:
11016       cp_parser_error (parser, "expected selection-statement");
11017       return error_mark_node;
11018     }
11019 }
11020
11021 /* Parse a condition.
11022
11023    condition:
11024      expression
11025      type-specifier-seq declarator = initializer-clause
11026      type-specifier-seq declarator braced-init-list
11027
11028    GNU Extension:
11029
11030    condition:
11031      type-specifier-seq declarator asm-specification [opt]
11032        attributes [opt] = assignment-expression
11033
11034    Returns the expression that should be tested.  */
11035
11036 static tree
11037 cp_parser_condition (cp_parser* parser)
11038 {
11039   cp_decl_specifier_seq type_specifiers;
11040   const char *saved_message;
11041   int declares_class_or_enum;
11042
11043   /* Try the declaration first.  */
11044   cp_parser_parse_tentatively (parser);
11045   /* New types are not allowed in the type-specifier-seq for a
11046      condition.  */
11047   saved_message = parser->type_definition_forbidden_message;
11048   parser->type_definition_forbidden_message
11049     = G_("types may not be defined in conditions");
11050   /* Parse the type-specifier-seq.  */
11051   cp_parser_decl_specifier_seq (parser,
11052                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11053                                 &type_specifiers,
11054                                 &declares_class_or_enum);
11055   /* Restore the saved message.  */
11056   parser->type_definition_forbidden_message = saved_message;
11057   /* If all is well, we might be looking at a declaration.  */
11058   if (!cp_parser_error_occurred (parser))
11059     {
11060       tree decl;
11061       tree asm_specification;
11062       tree attributes;
11063       cp_declarator *declarator;
11064       tree initializer = NULL_TREE;
11065
11066       /* Parse the declarator.  */
11067       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11068                                          /*ctor_dtor_or_conv_p=*/NULL,
11069                                          /*parenthesized_p=*/NULL,
11070                                          /*member_p=*/false,
11071                                          /*friend_p=*/false);
11072       /* Parse the attributes.  */
11073       attributes = cp_parser_attributes_opt (parser);
11074       /* Parse the asm-specification.  */
11075       asm_specification = cp_parser_asm_specification_opt (parser);
11076       /* If the next token is not an `=' or '{', then we might still be
11077          looking at an expression.  For example:
11078
11079            if (A(a).x)
11080
11081          looks like a decl-specifier-seq and a declarator -- but then
11082          there is no `=', so this is an expression.  */
11083       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11084           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11085         cp_parser_simulate_error (parser);
11086         
11087       /* If we did see an `=' or '{', then we are looking at a declaration
11088          for sure.  */
11089       if (cp_parser_parse_definitely (parser))
11090         {
11091           tree pushed_scope;
11092           bool non_constant_p;
11093           bool flags = LOOKUP_ONLYCONVERTING;
11094
11095           /* Create the declaration.  */
11096           decl = start_decl (declarator, &type_specifiers,
11097                              /*initialized_p=*/true,
11098                              attributes, /*prefix_attributes=*/NULL_TREE,
11099                              &pushed_scope);
11100
11101           /* Parse the initializer.  */
11102           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11103             {
11104               initializer = cp_parser_braced_list (parser, &non_constant_p);
11105               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11106               flags = 0;
11107             }
11108           else
11109             {
11110               /* Consume the `='.  */
11111               cp_parser_require (parser, CPP_EQ, RT_EQ);
11112               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11113             }
11114           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11115             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11116
11117           /* Process the initializer.  */
11118           cp_finish_decl (decl,
11119                           initializer, !non_constant_p,
11120                           asm_specification,
11121                           flags);
11122
11123           if (pushed_scope)
11124             pop_scope (pushed_scope);
11125
11126           return convert_from_reference (decl);
11127         }
11128     }
11129   /* If we didn't even get past the declarator successfully, we are
11130      definitely not looking at a declaration.  */
11131   else
11132     cp_parser_abort_tentative_parse (parser);
11133
11134   /* Otherwise, we are looking at an expression.  */
11135   return cp_parser_expression (parser);
11136 }
11137
11138 /* Parses a for-statement or range-for-statement until the closing ')',
11139    not included. */
11140
11141 static tree
11142 cp_parser_for (cp_parser *parser, bool ivdep)
11143 {
11144   tree init, scope, decl;
11145   bool is_range_for;
11146
11147   /* Begin the for-statement.  */
11148   scope = begin_for_scope (&init);
11149
11150   /* Parse the initialization.  */
11151   is_range_for = cp_parser_for_init_statement (parser, &decl);
11152
11153   if (is_range_for)
11154     return cp_parser_range_for (parser, scope, init, decl, ivdep);
11155   else
11156     return cp_parser_c_for (parser, scope, init, ivdep);
11157 }
11158
11159 static tree
11160 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11161 {
11162   /* Normal for loop */
11163   tree condition = NULL_TREE;
11164   tree expression = NULL_TREE;
11165   tree stmt;
11166
11167   stmt = begin_for_stmt (scope, init);
11168   /* The for-init-statement has already been parsed in
11169      cp_parser_for_init_statement, so no work is needed here.  */
11170   finish_for_init_stmt (stmt);
11171
11172   /* If there's a condition, process it.  */
11173   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11174     condition = cp_parser_condition (parser);
11175   else if (ivdep)
11176     {
11177       cp_parser_error (parser, "missing loop condition in loop with "
11178                        "%<GCC ivdep%> pragma");
11179       condition = error_mark_node;
11180     }
11181   finish_for_cond (condition, stmt, ivdep);
11182   /* Look for the `;'.  */
11183   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11184
11185   /* If there's an expression, process it.  */
11186   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11187     expression = cp_parser_expression (parser);
11188   finish_for_expr (expression, stmt);
11189
11190   return stmt;
11191 }
11192
11193 /* Tries to parse a range-based for-statement:
11194
11195   range-based-for:
11196     decl-specifier-seq declarator : expression
11197
11198   The decl-specifier-seq declarator and the `:' are already parsed by
11199   cp_parser_for_init_statement. If processing_template_decl it returns a
11200   newly created RANGE_FOR_STMT; if not, it is converted to a
11201   regular FOR_STMT.  */
11202
11203 static tree
11204 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11205                      bool ivdep)
11206 {
11207   tree stmt, range_expr;
11208   cxx_binding *binding = NULL;
11209   tree name = NULL_TREE;
11210
11211   /* Get the range declaration momentarily out of the way so that
11212      the range expression doesn't clash with it. */
11213   if (range_decl != error_mark_node)
11214     {
11215       name = DECL_NAME (range_decl);
11216       binding = IDENTIFIER_BINDING (name);
11217       IDENTIFIER_BINDING (name) = binding->previous;
11218     }
11219
11220   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11221     {
11222       bool expr_non_constant_p;
11223       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11224     }
11225   else
11226     range_expr = cp_parser_expression (parser);
11227
11228   /* Put the range declaration back into scope. */
11229   if (range_decl != error_mark_node)
11230     {
11231       binding->previous = IDENTIFIER_BINDING (name);
11232       IDENTIFIER_BINDING (name) = binding;
11233     }
11234
11235   /* If in template, STMT is converted to a normal for-statement
11236      at instantiation. If not, it is done just ahead. */
11237   if (processing_template_decl)
11238     {
11239       if (check_for_bare_parameter_packs (range_expr))
11240         range_expr = error_mark_node;
11241       stmt = begin_range_for_stmt (scope, init);
11242       if (ivdep)
11243         RANGE_FOR_IVDEP (stmt) = 1;
11244       finish_range_for_decl (stmt, range_decl, range_expr);
11245       if (!type_dependent_expression_p (range_expr)
11246           /* do_auto_deduction doesn't mess with template init-lists.  */
11247           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11248         do_range_for_auto_deduction (range_decl, range_expr);
11249     }
11250   else
11251     {
11252       stmt = begin_for_stmt (scope, init);
11253       stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11254     }
11255   return stmt;
11256 }
11257
11258 /* Subroutine of cp_convert_range_for: given the initializer expression,
11259    builds up the range temporary.  */
11260
11261 static tree
11262 build_range_temp (tree range_expr)
11263 {
11264   tree range_type, range_temp;
11265
11266   /* Find out the type deduced by the declaration
11267      `auto &&__range = range_expr'.  */
11268   range_type = cp_build_reference_type (make_auto (), true);
11269   range_type = do_auto_deduction (range_type, range_expr,
11270                                   type_uses_auto (range_type));
11271
11272   /* Create the __range variable.  */
11273   range_temp = build_decl (input_location, VAR_DECL,
11274                            get_identifier ("__for_range"), range_type);
11275   TREE_USED (range_temp) = 1;
11276   DECL_ARTIFICIAL (range_temp) = 1;
11277
11278   return range_temp;
11279 }
11280
11281 /* Used by cp_parser_range_for in template context: we aren't going to
11282    do a full conversion yet, but we still need to resolve auto in the
11283    type of the for-range-declaration if present.  This is basically
11284    a shortcut version of cp_convert_range_for.  */
11285
11286 static void
11287 do_range_for_auto_deduction (tree decl, tree range_expr)
11288 {
11289   tree auto_node = type_uses_auto (TREE_TYPE (decl));
11290   if (auto_node)
11291     {
11292       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11293       range_temp = convert_from_reference (build_range_temp (range_expr));
11294       iter_type = (cp_parser_perform_range_for_lookup
11295                    (range_temp, &begin_dummy, &end_dummy));
11296       if (iter_type)
11297         {
11298           iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11299                                   iter_type);
11300           iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11301                                             tf_warning_or_error);
11302           TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11303                                                 iter_decl, auto_node);
11304         }
11305     }
11306 }
11307
11308 /* Converts a range-based for-statement into a normal
11309    for-statement, as per the definition.
11310
11311       for (RANGE_DECL : RANGE_EXPR)
11312         BLOCK
11313
11314    should be equivalent to:
11315
11316       {
11317         auto &&__range = RANGE_EXPR;
11318         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11319               __begin != __end;
11320               ++__begin)
11321           {
11322               RANGE_DECL = *__begin;
11323               BLOCK
11324           }
11325       }
11326
11327    If RANGE_EXPR is an array:
11328         BEGIN_EXPR = __range
11329         END_EXPR = __range + ARRAY_SIZE(__range)
11330    Else if RANGE_EXPR has a member 'begin' or 'end':
11331         BEGIN_EXPR = __range.begin()
11332         END_EXPR = __range.end()
11333    Else:
11334         BEGIN_EXPR = begin(__range)
11335         END_EXPR = end(__range);
11336
11337    If __range has a member 'begin' but not 'end', or vice versa, we must
11338    still use the second alternative (it will surely fail, however).
11339    When calling begin()/end() in the third alternative we must use
11340    argument dependent lookup, but always considering 'std' as an associated
11341    namespace.  */
11342
11343 tree
11344 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11345                       bool ivdep)
11346 {
11347   tree begin, end;
11348   tree iter_type, begin_expr, end_expr;
11349   tree condition, expression;
11350
11351   if (range_decl == error_mark_node || range_expr == error_mark_node)
11352     /* If an error happened previously do nothing or else a lot of
11353        unhelpful errors would be issued.  */
11354     begin_expr = end_expr = iter_type = error_mark_node;
11355   else
11356     {
11357       tree range_temp;
11358
11359       if (VAR_P (range_expr)
11360           && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11361         /* Can't bind a reference to an array of runtime bound.  */
11362         range_temp = range_expr;
11363       else
11364         {
11365           range_temp = build_range_temp (range_expr);
11366           pushdecl (range_temp);
11367           cp_finish_decl (range_temp, range_expr,
11368                           /*is_constant_init*/false, NULL_TREE,
11369                           LOOKUP_ONLYCONVERTING);
11370           range_temp = convert_from_reference (range_temp);
11371         }
11372       iter_type = cp_parser_perform_range_for_lookup (range_temp,
11373                                                       &begin_expr, &end_expr);
11374     }
11375
11376   /* The new for initialization statement.  */
11377   begin = build_decl (input_location, VAR_DECL,
11378                       get_identifier ("__for_begin"), iter_type);
11379   TREE_USED (begin) = 1;
11380   DECL_ARTIFICIAL (begin) = 1;
11381   pushdecl (begin);
11382   cp_finish_decl (begin, begin_expr,
11383                   /*is_constant_init*/false, NULL_TREE,
11384                   LOOKUP_ONLYCONVERTING);
11385
11386   if (cxx_dialect >= cxx1z)
11387     iter_type = cv_unqualified (TREE_TYPE (end_expr));
11388   end = build_decl (input_location, VAR_DECL,
11389                     get_identifier ("__for_end"), iter_type);
11390   TREE_USED (end) = 1;
11391   DECL_ARTIFICIAL (end) = 1;
11392   pushdecl (end);
11393   cp_finish_decl (end, end_expr,
11394                   /*is_constant_init*/false, NULL_TREE,
11395                   LOOKUP_ONLYCONVERTING);
11396
11397   finish_for_init_stmt (statement);
11398
11399   /* The new for condition.  */
11400   condition = build_x_binary_op (input_location, NE_EXPR,
11401                                  begin, ERROR_MARK,
11402                                  end, ERROR_MARK,
11403                                  NULL, tf_warning_or_error);
11404   finish_for_cond (condition, statement, ivdep);
11405
11406   /* The new increment expression.  */
11407   expression = finish_unary_op_expr (input_location,
11408                                      PREINCREMENT_EXPR, begin,
11409                                      tf_warning_or_error);
11410   finish_for_expr (expression, statement);
11411
11412   /* The declaration is initialized with *__begin inside the loop body.  */
11413   cp_finish_decl (range_decl,
11414                   build_x_indirect_ref (input_location, begin, RO_NULL,
11415                                         tf_warning_or_error),
11416                   /*is_constant_init*/false, NULL_TREE,
11417                   LOOKUP_ONLYCONVERTING);
11418
11419   return statement;
11420 }
11421
11422 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11423    We need to solve both at the same time because the method used
11424    depends on the existence of members begin or end.
11425    Returns the type deduced for the iterator expression.  */
11426
11427 static tree
11428 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11429 {
11430   if (error_operand_p (range))
11431     {
11432       *begin = *end = error_mark_node;
11433       return error_mark_node;
11434     }
11435
11436   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11437     {
11438       error ("range-based %<for%> expression of type %qT "
11439              "has incomplete type", TREE_TYPE (range));
11440       *begin = *end = error_mark_node;
11441       return error_mark_node;
11442     }
11443   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11444     {
11445       /* If RANGE is an array, we will use pointer arithmetic.  */
11446       *begin = range;
11447       *end = build_binary_op (input_location, PLUS_EXPR,
11448                               range,
11449                               array_type_nelts_top (TREE_TYPE (range)),
11450                               0);
11451       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11452     }
11453   else
11454     {
11455       /* If it is not an array, we must do a bit of magic.  */
11456       tree id_begin, id_end;
11457       tree member_begin, member_end;
11458
11459       *begin = *end = error_mark_node;
11460
11461       id_begin = get_identifier ("begin");
11462       id_end = get_identifier ("end");
11463       member_begin = lookup_member (TREE_TYPE (range), id_begin,
11464                                     /*protect=*/2, /*want_type=*/false,
11465                                     tf_warning_or_error);
11466       member_end = lookup_member (TREE_TYPE (range), id_end,
11467                                   /*protect=*/2, /*want_type=*/false,
11468                                   tf_warning_or_error);
11469
11470       if (member_begin != NULL_TREE || member_end != NULL_TREE)
11471         {
11472           /* Use the member functions.  */
11473           if (member_begin != NULL_TREE)
11474             *begin = cp_parser_range_for_member_function (range, id_begin);
11475           else
11476             error ("range-based %<for%> expression of type %qT has an "
11477                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11478
11479           if (member_end != NULL_TREE)
11480             *end = cp_parser_range_for_member_function (range, id_end);
11481           else
11482             error ("range-based %<for%> expression of type %qT has a "
11483                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11484         }
11485       else
11486         {
11487           /* Use global functions with ADL.  */
11488           vec<tree, va_gc> *vec;
11489           vec = make_tree_vector ();
11490
11491           vec_safe_push (vec, range);
11492
11493           member_begin = perform_koenig_lookup (id_begin, vec,
11494                                                 tf_warning_or_error);
11495           *begin = finish_call_expr (member_begin, &vec, false, true,
11496                                      tf_warning_or_error);
11497           member_end = perform_koenig_lookup (id_end, vec,
11498                                               tf_warning_or_error);
11499           *end = finish_call_expr (member_end, &vec, false, true,
11500                                    tf_warning_or_error);
11501
11502           release_tree_vector (vec);
11503         }
11504
11505       /* Last common checks.  */
11506       if (*begin == error_mark_node || *end == error_mark_node)
11507         {
11508           /* If one of the expressions is an error do no more checks.  */
11509           *begin = *end = error_mark_node;
11510           return error_mark_node;
11511         }
11512       else if (type_dependent_expression_p (*begin)
11513                || type_dependent_expression_p (*end))
11514         /* Can happen, when, eg, in a template context, Koenig lookup
11515            can't resolve begin/end (c++/58503).  */
11516         return NULL_TREE;
11517       else
11518         {
11519           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11520           /* The unqualified type of the __begin and __end temporaries should
11521              be the same, as required by the multiple auto declaration.  */
11522           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11523             {
11524               if (cxx_dialect >= cxx1z
11525                   && (build_x_binary_op (input_location, NE_EXPR,
11526                                          *begin, ERROR_MARK,
11527                                          *end, ERROR_MARK,
11528                                          NULL, tf_none)
11529                       != error_mark_node))
11530                 /* P0184R0 allows __begin and __end to have different types,
11531                    but make sure they are comparable so we can give a better
11532                    diagnostic.  */;
11533               else
11534                 error ("inconsistent begin/end types in range-based %<for%> "
11535                        "statement: %qT and %qT",
11536                        TREE_TYPE (*begin), TREE_TYPE (*end));
11537             }
11538           return iter_type;
11539         }
11540     }
11541 }
11542
11543 /* Helper function for cp_parser_perform_range_for_lookup.
11544    Builds a tree for RANGE.IDENTIFIER().  */
11545
11546 static tree
11547 cp_parser_range_for_member_function (tree range, tree identifier)
11548 {
11549   tree member, res;
11550   vec<tree, va_gc> *vec;
11551
11552   member = finish_class_member_access_expr (range, identifier,
11553                                             false, tf_warning_or_error);
11554   if (member == error_mark_node)
11555     return error_mark_node;
11556
11557   vec = make_tree_vector ();
11558   res = finish_call_expr (member, &vec,
11559                           /*disallow_virtual=*/false,
11560                           /*koenig_p=*/false,
11561                           tf_warning_or_error);
11562   release_tree_vector (vec);
11563   return res;
11564 }
11565
11566 /* Parse an iteration-statement.
11567
11568    iteration-statement:
11569      while ( condition ) statement
11570      do statement while ( expression ) ;
11571      for ( for-init-statement condition [opt] ; expression [opt] )
11572        statement
11573
11574    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
11575
11576 static tree
11577 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11578 {
11579   cp_token *token;
11580   enum rid keyword;
11581   tree statement;
11582   unsigned char in_statement;
11583   token_indent_info guard_tinfo;
11584
11585   /* Peek at the next token.  */
11586   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11587   if (!token)
11588     return error_mark_node;
11589
11590   guard_tinfo = get_token_indent_info (token);
11591
11592   /* Remember whether or not we are already within an iteration
11593      statement.  */
11594   in_statement = parser->in_statement;
11595
11596   /* See what kind of keyword it is.  */
11597   keyword = token->keyword;
11598   switch (keyword)
11599     {
11600     case RID_WHILE:
11601       {
11602         tree condition;
11603
11604         /* Begin the while-statement.  */
11605         statement = begin_while_stmt ();
11606         /* Look for the `('.  */
11607         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11608         /* Parse the condition.  */
11609         condition = cp_parser_condition (parser);
11610         finish_while_stmt_cond (condition, statement, ivdep);
11611         /* Look for the `)'.  */
11612         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11613         /* Parse the dependent statement.  */
11614         parser->in_statement = IN_ITERATION_STMT;
11615         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11616         parser->in_statement = in_statement;
11617         /* We're done with the while-statement.  */
11618         finish_while_stmt (statement);
11619       }
11620       break;
11621
11622     case RID_DO:
11623       {
11624         tree expression;
11625
11626         /* Begin the do-statement.  */
11627         statement = begin_do_stmt ();
11628         /* Parse the body of the do-statement.  */
11629         parser->in_statement = IN_ITERATION_STMT;
11630         cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11631         parser->in_statement = in_statement;
11632         finish_do_body (statement);
11633         /* Look for the `while' keyword.  */
11634         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11635         /* Look for the `('.  */
11636         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11637         /* Parse the expression.  */
11638         expression = cp_parser_expression (parser);
11639         /* We're done with the do-statement.  */
11640         finish_do_stmt (expression, statement, ivdep);
11641         /* Look for the `)'.  */
11642         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11643         /* Look for the `;'.  */
11644         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11645       }
11646       break;
11647
11648     case RID_FOR:
11649       {
11650         /* Look for the `('.  */
11651         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11652
11653         statement = cp_parser_for (parser, ivdep);
11654
11655         /* Look for the `)'.  */
11656         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11657
11658         /* Parse the body of the for-statement.  */
11659         parser->in_statement = IN_ITERATION_STMT;
11660         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11661         parser->in_statement = in_statement;
11662
11663         /* We're done with the for-statement.  */
11664         finish_for_stmt (statement);
11665       }
11666       break;
11667
11668     default:
11669       cp_parser_error (parser, "expected iteration-statement");
11670       statement = error_mark_node;
11671       break;
11672     }
11673
11674   return statement;
11675 }
11676
11677 /* Parse a for-init-statement or the declarator of a range-based-for.
11678    Returns true if a range-based-for declaration is seen.
11679
11680    for-init-statement:
11681      expression-statement
11682      simple-declaration  */
11683
11684 static bool
11685 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11686 {
11687   /* If the next token is a `;', then we have an empty
11688      expression-statement.  Grammatically, this is also a
11689      simple-declaration, but an invalid one, because it does not
11690      declare anything.  Therefore, if we did not handle this case
11691      specially, we would issue an error message about an invalid
11692      declaration.  */
11693   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11694     {
11695       bool is_range_for = false;
11696       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11697
11698       /* A colon is used in range-based for.  */
11699       parser->colon_corrects_to_scope_p = false;
11700
11701       /* We're going to speculatively look for a declaration, falling back
11702          to an expression, if necessary.  */
11703       cp_parser_parse_tentatively (parser);
11704       /* Parse the declaration.  */
11705       cp_parser_simple_declaration (parser,
11706                                     /*function_definition_allowed_p=*/false,
11707                                     decl);
11708       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11709       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11710         {
11711           /* It is a range-for, consume the ':' */
11712           cp_lexer_consume_token (parser->lexer);
11713           is_range_for = true;
11714           if (cxx_dialect < cxx11)
11715             {
11716               pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11717                        "range-based %<for%> loops only available with "
11718                        "-std=c++11 or -std=gnu++11");
11719               *decl = error_mark_node;
11720             }
11721         }
11722       else
11723           /* The ';' is not consumed yet because we told
11724              cp_parser_simple_declaration not to.  */
11725           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11726
11727       if (cp_parser_parse_definitely (parser))
11728         return is_range_for;
11729       /* If the tentative parse failed, then we shall need to look for an
11730          expression-statement.  */
11731     }
11732   /* If we are here, it is an expression-statement.  */
11733   cp_parser_expression_statement (parser, NULL_TREE);
11734   return false;
11735 }
11736
11737 /* Parse a jump-statement.
11738
11739    jump-statement:
11740      break ;
11741      continue ;
11742      return expression [opt] ;
11743      return braced-init-list ;
11744      goto identifier ;
11745
11746    GNU extension:
11747
11748    jump-statement:
11749      goto * expression ;
11750
11751    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
11752
11753 static tree
11754 cp_parser_jump_statement (cp_parser* parser)
11755 {
11756   tree statement = error_mark_node;
11757   cp_token *token;
11758   enum rid keyword;
11759   unsigned char in_statement;
11760
11761   /* Peek at the next token.  */
11762   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11763   if (!token)
11764     return error_mark_node;
11765
11766   /* See what kind of keyword it is.  */
11767   keyword = token->keyword;
11768   switch (keyword)
11769     {
11770     case RID_BREAK:
11771       in_statement = parser->in_statement & ~IN_IF_STMT;      
11772       switch (in_statement)
11773         {
11774         case 0:
11775           error_at (token->location, "break statement not within loop or switch");
11776           break;
11777         default:
11778           gcc_assert ((in_statement & IN_SWITCH_STMT)
11779                       || in_statement == IN_ITERATION_STMT);
11780           statement = finish_break_stmt ();
11781           if (in_statement == IN_ITERATION_STMT)
11782             break_maybe_infinite_loop ();
11783           break;
11784         case IN_OMP_BLOCK:
11785           error_at (token->location, "invalid exit from OpenMP structured block");
11786           break;
11787         case IN_OMP_FOR:
11788           error_at (token->location, "break statement used with OpenMP for loop");
11789           break;
11790         case IN_CILK_SIMD_FOR:
11791           error_at (token->location, "break statement used with Cilk Plus for loop");
11792           break;
11793         }
11794       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11795       break;
11796
11797     case RID_CONTINUE:
11798       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11799         {
11800         case 0:
11801           error_at (token->location, "continue statement not within a loop");
11802           break;
11803         case IN_CILK_SIMD_FOR:
11804           error_at (token->location,
11805                     "continue statement within %<#pragma simd%> loop body");
11806           /* Fall through.  */
11807         case IN_ITERATION_STMT:
11808         case IN_OMP_FOR:
11809           statement = finish_continue_stmt ();
11810           break;
11811         case IN_OMP_BLOCK:
11812           error_at (token->location, "invalid exit from OpenMP structured block");
11813           break;
11814         default:
11815           gcc_unreachable ();
11816         }
11817       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11818       break;
11819
11820     case RID_RETURN:
11821       {
11822         tree expr;
11823         bool expr_non_constant_p;
11824
11825         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11826           {
11827             cp_lexer_set_source_position (parser->lexer);
11828             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11829             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11830           }
11831         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11832           expr = cp_parser_expression (parser);
11833         else
11834           /* If the next token is a `;', then there is no
11835              expression.  */
11836           expr = NULL_TREE;
11837         /* Build the return-statement.  */
11838         statement = finish_return_stmt (expr);
11839         /* Look for the final `;'.  */
11840         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11841       }
11842       break;
11843
11844     case RID_GOTO:
11845       if (parser->in_function_body
11846           && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11847         {
11848           error ("%<goto%> in %<constexpr%> function");
11849           cp_function_chain->invalid_constexpr = true;
11850         }
11851
11852       /* Create the goto-statement.  */
11853       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11854         {
11855           /* Issue a warning about this use of a GNU extension.  */
11856           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11857           /* Consume the '*' token.  */
11858           cp_lexer_consume_token (parser->lexer);
11859           /* Parse the dependent expression.  */
11860           finish_goto_stmt (cp_parser_expression (parser));
11861         }
11862       else
11863         finish_goto_stmt (cp_parser_identifier (parser));
11864       /* Look for the final `;'.  */
11865       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11866       break;
11867
11868     default:
11869       cp_parser_error (parser, "expected jump-statement");
11870       break;
11871     }
11872
11873   return statement;
11874 }
11875
11876 /* Parse a declaration-statement.
11877
11878    declaration-statement:
11879      block-declaration  */
11880
11881 static void
11882 cp_parser_declaration_statement (cp_parser* parser)
11883 {
11884   void *p;
11885
11886   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11887   p = obstack_alloc (&declarator_obstack, 0);
11888
11889  /* Parse the block-declaration.  */
11890   cp_parser_block_declaration (parser, /*statement_p=*/true);
11891
11892   /* Free any declarators allocated.  */
11893   obstack_free (&declarator_obstack, p);
11894 }
11895
11896 /* Some dependent statements (like `if (cond) statement'), are
11897    implicitly in their own scope.  In other words, if the statement is
11898    a single statement (as opposed to a compound-statement), it is
11899    none-the-less treated as if it were enclosed in braces.  Any
11900    declarations appearing in the dependent statement are out of scope
11901    after control passes that point.  This function parses a statement,
11902    but ensures that is in its own scope, even if it is not a
11903    compound-statement.
11904
11905    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11906    is a (possibly labeled) if statement which is not enclosed in
11907    braces and has an else clause.  This is used to implement
11908    -Wparentheses.
11909
11910    CHAIN is a vector of if-else-if conditions.  This is used to implement
11911    -Wduplicated-cond.
11912
11913    Returns the new statement.  */
11914
11915 static tree
11916 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11917                                        const token_indent_info &guard_tinfo,
11918                                        vec<tree> *chain)
11919 {
11920   tree statement;
11921   location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11922   token_indent_info body_tinfo
11923     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11924
11925   if (if_p != NULL)
11926     *if_p = false;
11927
11928   /* Mark if () ; with a special NOP_EXPR.  */
11929   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11930     {
11931       cp_lexer_consume_token (parser->lexer);
11932       statement = add_stmt (build_empty_stmt (body_loc));
11933
11934       if (guard_tinfo.keyword == RID_IF
11935           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11936         warning_at (body_loc, OPT_Wempty_body,
11937                     "suggest braces around empty body in an %<if%> statement");
11938       else if (guard_tinfo.keyword == RID_ELSE)
11939         warning_at (body_loc, OPT_Wempty_body,
11940                     "suggest braces around empty body in an %<else%> statement");
11941     }
11942   /* if a compound is opened, we simply parse the statement directly.  */
11943   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11944     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11945   /* If the token is not a `{', then we must take special action.  */
11946   else
11947     {
11948       /* Create a compound-statement.  */
11949       statement = begin_compound_stmt (0);
11950       /* Parse the dependent-statement.  */
11951       cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11952       /* Finish the dummy compound-statement.  */
11953       finish_compound_stmt (statement);
11954     }
11955
11956   token_indent_info next_tinfo
11957     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11958   warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11959
11960   /* Return the statement.  */
11961   return statement;
11962 }
11963
11964 /* For some dependent statements (like `while (cond) statement'), we
11965    have already created a scope.  Therefore, even if the dependent
11966    statement is a compound-statement, we do not want to create another
11967    scope.  */
11968
11969 static void
11970 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11971                                     const token_indent_info &guard_tinfo)
11972 {
11973   /* If the token is a `{', then we must take special action.  */
11974   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11975     {
11976       token_indent_info body_tinfo
11977         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11978
11979       cp_parser_statement (parser, NULL_TREE, false, if_p);
11980       token_indent_info next_tinfo
11981         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11982       warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11983     }
11984   else
11985     {
11986       /* Avoid calling cp_parser_compound_statement, so that we
11987          don't create a new scope.  Do everything else by hand.  */
11988       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11989       /* If the next keyword is `__label__' we have a label declaration.  */
11990       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11991         cp_parser_label_declaration (parser);
11992       /* Parse an (optional) statement-seq.  */
11993       cp_parser_statement_seq_opt (parser, NULL_TREE);
11994       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11995     }
11996 }
11997
11998 /* Declarations [gram.dcl.dcl] */
11999
12000 /* Parse an optional declaration-sequence.
12001
12002    declaration-seq:
12003      declaration
12004      declaration-seq declaration  */
12005
12006 static void
12007 cp_parser_declaration_seq_opt (cp_parser* parser)
12008 {
12009   while (true)
12010     {
12011       cp_token *token;
12012
12013       token = cp_lexer_peek_token (parser->lexer);
12014
12015       if (token->type == CPP_CLOSE_BRACE
12016           || token->type == CPP_EOF
12017           || token->type == CPP_PRAGMA_EOL)
12018         break;
12019
12020       if (token->type == CPP_SEMICOLON)
12021         {
12022           /* A declaration consisting of a single semicolon is
12023              invalid.  Allow it unless we're being pedantic.  */
12024           cp_lexer_consume_token (parser->lexer);
12025           if (!in_system_header_at (input_location))
12026             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12027           continue;
12028         }
12029
12030       /* If we're entering or exiting a region that's implicitly
12031          extern "C", modify the lang context appropriately.  */
12032       if (!parser->implicit_extern_c && token->implicit_extern_c)
12033         {
12034           push_lang_context (lang_name_c);
12035           parser->implicit_extern_c = true;
12036         }
12037       else if (parser->implicit_extern_c && !token->implicit_extern_c)
12038         {
12039           pop_lang_context ();
12040           parser->implicit_extern_c = false;
12041         }
12042
12043       if (token->type == CPP_PRAGMA)
12044         {
12045           /* A top-level declaration can consist solely of a #pragma.
12046              A nested declaration cannot, so this is done here and not
12047              in cp_parser_declaration.  (A #pragma at block scope is
12048              handled in cp_parser_statement.)  */
12049           cp_parser_pragma (parser, pragma_external, NULL);
12050           continue;
12051         }
12052
12053       /* Parse the declaration itself.  */
12054       cp_parser_declaration (parser);
12055     }
12056 }
12057
12058 /* Parse a declaration.
12059
12060    declaration:
12061      block-declaration
12062      function-definition
12063      template-declaration
12064      explicit-instantiation
12065      explicit-specialization
12066      linkage-specification
12067      namespace-definition
12068
12069    GNU extension:
12070
12071    declaration:
12072       __extension__ declaration */
12073
12074 static void
12075 cp_parser_declaration (cp_parser* parser)
12076 {
12077   cp_token token1;
12078   cp_token token2;
12079   int saved_pedantic;
12080   void *p;
12081   tree attributes = NULL_TREE;
12082
12083   /* Check for the `__extension__' keyword.  */
12084   if (cp_parser_extension_opt (parser, &saved_pedantic))
12085     {
12086       /* Parse the qualified declaration.  */
12087       cp_parser_declaration (parser);
12088       /* Restore the PEDANTIC flag.  */
12089       pedantic = saved_pedantic;
12090
12091       return;
12092     }
12093
12094   /* Try to figure out what kind of declaration is present.  */
12095   token1 = *cp_lexer_peek_token (parser->lexer);
12096
12097   if (token1.type != CPP_EOF)
12098     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12099   else
12100     {
12101       token2.type = CPP_EOF;
12102       token2.keyword = RID_MAX;
12103     }
12104
12105   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12106   p = obstack_alloc (&declarator_obstack, 0);
12107
12108   /* If the next token is `extern' and the following token is a string
12109      literal, then we have a linkage specification.  */
12110   if (token1.keyword == RID_EXTERN
12111       && cp_parser_is_pure_string_literal (&token2))
12112     cp_parser_linkage_specification (parser);
12113   /* If the next token is `template', then we have either a template
12114      declaration, an explicit instantiation, or an explicit
12115      specialization.  */
12116   else if (token1.keyword == RID_TEMPLATE)
12117     {
12118       /* `template <>' indicates a template specialization.  */
12119       if (token2.type == CPP_LESS
12120           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12121         cp_parser_explicit_specialization (parser);
12122       /* `template <' indicates a template declaration.  */
12123       else if (token2.type == CPP_LESS)
12124         cp_parser_template_declaration (parser, /*member_p=*/false);
12125       /* Anything else must be an explicit instantiation.  */
12126       else
12127         cp_parser_explicit_instantiation (parser);
12128     }
12129   /* If the next token is `export', then we have a template
12130      declaration.  */
12131   else if (token1.keyword == RID_EXPORT)
12132     cp_parser_template_declaration (parser, /*member_p=*/false);
12133   /* If the next token is `extern', 'static' or 'inline' and the one
12134      after that is `template', we have a GNU extended explicit
12135      instantiation directive.  */
12136   else if (cp_parser_allow_gnu_extensions_p (parser)
12137            && (token1.keyword == RID_EXTERN
12138                || token1.keyword == RID_STATIC
12139                || token1.keyword == RID_INLINE)
12140            && token2.keyword == RID_TEMPLATE)
12141     cp_parser_explicit_instantiation (parser);
12142   /* If the next token is `namespace', check for a named or unnamed
12143      namespace definition.  */
12144   else if (token1.keyword == RID_NAMESPACE
12145            && (/* A named namespace definition.  */
12146                (token2.type == CPP_NAME
12147                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12148                     != CPP_EQ))
12149                || (token2.type == CPP_OPEN_SQUARE
12150                    && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12151                    == CPP_OPEN_SQUARE)
12152                /* An unnamed namespace definition.  */
12153                || token2.type == CPP_OPEN_BRACE
12154                || token2.keyword == RID_ATTRIBUTE))
12155     cp_parser_namespace_definition (parser);
12156   /* An inline (associated) namespace definition.  */
12157   else if (token1.keyword == RID_INLINE
12158            && token2.keyword == RID_NAMESPACE)
12159     cp_parser_namespace_definition (parser);
12160   /* Objective-C++ declaration/definition.  */
12161   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12162     cp_parser_objc_declaration (parser, NULL_TREE);
12163   else if (c_dialect_objc ()
12164            && token1.keyword == RID_ATTRIBUTE
12165            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12166     cp_parser_objc_declaration (parser, attributes);
12167   /* At this point we may have a template declared by a concept
12168      introduction.  */
12169   else if (flag_concepts
12170            && cp_parser_template_declaration_after_export (parser,
12171                                                            /*member_p=*/false))
12172     /* We did.  */;
12173   else
12174     /* Try to parse a block-declaration, or a function-definition.  */
12175     cp_parser_block_declaration (parser, /*statement_p=*/false);
12176
12177   /* Free any declarators allocated.  */
12178   obstack_free (&declarator_obstack, p);
12179 }
12180
12181 /* Parse a block-declaration.
12182
12183    block-declaration:
12184      simple-declaration
12185      asm-definition
12186      namespace-alias-definition
12187      using-declaration
12188      using-directive
12189
12190    GNU Extension:
12191
12192    block-declaration:
12193      __extension__ block-declaration
12194
12195    C++0x Extension:
12196
12197    block-declaration:
12198      static_assert-declaration
12199
12200    If STATEMENT_P is TRUE, then this block-declaration is occurring as
12201    part of a declaration-statement.  */
12202
12203 static void
12204 cp_parser_block_declaration (cp_parser *parser,
12205                              bool      statement_p)
12206 {
12207   cp_token *token1;
12208   int saved_pedantic;
12209
12210   /* Check for the `__extension__' keyword.  */
12211   if (cp_parser_extension_opt (parser, &saved_pedantic))
12212     {
12213       /* Parse the qualified declaration.  */
12214       cp_parser_block_declaration (parser, statement_p);
12215       /* Restore the PEDANTIC flag.  */
12216       pedantic = saved_pedantic;
12217
12218       return;
12219     }
12220
12221   /* Peek at the next token to figure out which kind of declaration is
12222      present.  */
12223   token1 = cp_lexer_peek_token (parser->lexer);
12224
12225   /* If the next keyword is `asm', we have an asm-definition.  */
12226   if (token1->keyword == RID_ASM)
12227     {
12228       if (statement_p)
12229         cp_parser_commit_to_tentative_parse (parser);
12230       cp_parser_asm_definition (parser);
12231     }
12232   /* If the next keyword is `namespace', we have a
12233      namespace-alias-definition.  */
12234   else if (token1->keyword == RID_NAMESPACE)
12235     cp_parser_namespace_alias_definition (parser);
12236   /* If the next keyword is `using', we have a
12237      using-declaration, a using-directive, or an alias-declaration.  */
12238   else if (token1->keyword == RID_USING)
12239     {
12240       cp_token *token2;
12241
12242       if (statement_p)
12243         cp_parser_commit_to_tentative_parse (parser);
12244       /* If the token after `using' is `namespace', then we have a
12245          using-directive.  */
12246       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12247       if (token2->keyword == RID_NAMESPACE)
12248         cp_parser_using_directive (parser);
12249       /* If the second token after 'using' is '=', then we have an
12250          alias-declaration.  */
12251       else if (cxx_dialect >= cxx11
12252                && token2->type == CPP_NAME
12253                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12254                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12255         cp_parser_alias_declaration (parser);
12256       /* Otherwise, it's a using-declaration.  */
12257       else
12258         cp_parser_using_declaration (parser,
12259                                      /*access_declaration_p=*/false);
12260     }
12261   /* If the next keyword is `__label__' we have a misplaced label
12262      declaration.  */
12263   else if (token1->keyword == RID_LABEL)
12264     {
12265       cp_lexer_consume_token (parser->lexer);
12266       error_at (token1->location, "%<__label__%> not at the beginning of a block");
12267       cp_parser_skip_to_end_of_statement (parser);
12268       /* If the next token is now a `;', consume it.  */
12269       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12270         cp_lexer_consume_token (parser->lexer);
12271     }
12272   /* If the next token is `static_assert' we have a static assertion.  */
12273   else if (token1->keyword == RID_STATIC_ASSERT)
12274     cp_parser_static_assert (parser, /*member_p=*/false);
12275   /* Anything else must be a simple-declaration.  */
12276   else
12277     cp_parser_simple_declaration (parser, !statement_p,
12278                                   /*maybe_range_for_decl*/NULL);
12279 }
12280
12281 /* Parse a simple-declaration.
12282
12283    simple-declaration:
12284      decl-specifier-seq [opt] init-declarator-list [opt] ;
12285
12286    init-declarator-list:
12287      init-declarator
12288      init-declarator-list , init-declarator
12289
12290    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12291    function-definition as a simple-declaration.
12292
12293    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12294    parsed declaration if it is an uninitialized single declarator not followed
12295    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12296    if present, will not be consumed.  */
12297
12298 static void
12299 cp_parser_simple_declaration (cp_parser* parser,
12300                               bool function_definition_allowed_p,
12301                               tree *maybe_range_for_decl)
12302 {
12303   cp_decl_specifier_seq decl_specifiers;
12304   int declares_class_or_enum;
12305   bool saw_declarator;
12306   location_t comma_loc = UNKNOWN_LOCATION;
12307   location_t init_loc = UNKNOWN_LOCATION;
12308
12309   if (maybe_range_for_decl)
12310     *maybe_range_for_decl = NULL_TREE;
12311
12312   /* Defer access checks until we know what is being declared; the
12313      checks for names appearing in the decl-specifier-seq should be
12314      done as if we were in the scope of the thing being declared.  */
12315   push_deferring_access_checks (dk_deferred);
12316
12317   /* Parse the decl-specifier-seq.  We have to keep track of whether
12318      or not the decl-specifier-seq declares a named class or
12319      enumeration type, since that is the only case in which the
12320      init-declarator-list is allowed to be empty.
12321
12322      [dcl.dcl]
12323
12324      In a simple-declaration, the optional init-declarator-list can be
12325      omitted only when declaring a class or enumeration, that is when
12326      the decl-specifier-seq contains either a class-specifier, an
12327      elaborated-type-specifier, or an enum-specifier.  */
12328   cp_parser_decl_specifier_seq (parser,
12329                                 CP_PARSER_FLAGS_OPTIONAL,
12330                                 &decl_specifiers,
12331                                 &declares_class_or_enum);
12332   /* We no longer need to defer access checks.  */
12333   stop_deferring_access_checks ();
12334
12335   /* In a block scope, a valid declaration must always have a
12336      decl-specifier-seq.  By not trying to parse declarators, we can
12337      resolve the declaration/expression ambiguity more quickly.  */
12338   if (!function_definition_allowed_p
12339       && !decl_specifiers.any_specifiers_p)
12340     {
12341       cp_parser_error (parser, "expected declaration");
12342       goto done;
12343     }
12344
12345   /* If the next two tokens are both identifiers, the code is
12346      erroneous. The usual cause of this situation is code like:
12347
12348        T t;
12349
12350      where "T" should name a type -- but does not.  */
12351   if (!decl_specifiers.any_type_specifiers_p
12352       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12353     {
12354       /* If parsing tentatively, we should commit; we really are
12355          looking at a declaration.  */
12356       cp_parser_commit_to_tentative_parse (parser);
12357       /* Give up.  */
12358       goto done;
12359     }
12360
12361   /* If we have seen at least one decl-specifier, and the next token
12362      is not a parenthesis, then we must be looking at a declaration.
12363      (After "int (" we might be looking at a functional cast.)  */
12364   if (decl_specifiers.any_specifiers_p
12365       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12366       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12367       && !cp_parser_error_occurred (parser))
12368     cp_parser_commit_to_tentative_parse (parser);
12369
12370   tree last_type;
12371
12372   last_type = NULL_TREE;
12373
12374   /* Keep going until we hit the `;' at the end of the simple
12375      declaration.  */
12376   saw_declarator = false;
12377   while (cp_lexer_next_token_is_not (parser->lexer,
12378                                      CPP_SEMICOLON))
12379     {
12380       cp_token *token;
12381       bool function_definition_p;
12382       tree decl;
12383       tree auto_result = NULL_TREE;
12384
12385       if (saw_declarator)
12386         {
12387           /* If we are processing next declarator, comma is expected */
12388           token = cp_lexer_peek_token (parser->lexer);
12389           gcc_assert (token->type == CPP_COMMA);
12390           cp_lexer_consume_token (parser->lexer);
12391           if (maybe_range_for_decl)
12392             {
12393               *maybe_range_for_decl = error_mark_node;
12394               if (comma_loc == UNKNOWN_LOCATION)
12395                 comma_loc = token->location;
12396             }
12397         }
12398       else
12399         saw_declarator = true;
12400
12401       /* Parse the init-declarator.  */
12402       decl = cp_parser_init_declarator (parser, &decl_specifiers,
12403                                         /*checks=*/NULL,
12404                                         function_definition_allowed_p,
12405                                         /*member_p=*/false,
12406                                         declares_class_or_enum,
12407                                         &function_definition_p,
12408                                         maybe_range_for_decl,
12409                                         &init_loc,
12410                                         &auto_result);
12411       /* If an error occurred while parsing tentatively, exit quickly.
12412          (That usually happens when in the body of a function; each
12413          statement is treated as a declaration-statement until proven
12414          otherwise.)  */
12415       if (cp_parser_error_occurred (parser))
12416         goto done;
12417
12418       if (auto_result
12419           && (!processing_template_decl || !type_uses_auto (auto_result)))
12420         {
12421           if (last_type
12422               && last_type != error_mark_node
12423               && !same_type_p (auto_result, last_type))
12424             {
12425               /* If the list of declarators contains more than one declarator,
12426                  the type of each declared variable is determined as described
12427                  above. If the type deduced for the template parameter U is not
12428                  the same in each deduction, the program is ill-formed.  */
12429               error_at (decl_specifiers.locations[ds_type_spec],
12430                         "inconsistent deduction for %qT: %qT and then %qT",
12431                         decl_specifiers.type, last_type, auto_result);
12432               last_type = error_mark_node;
12433             }
12434           else
12435             last_type = auto_result;
12436         }
12437
12438       /* Handle function definitions specially.  */
12439       if (function_definition_p)
12440         {
12441           /* If the next token is a `,', then we are probably
12442              processing something like:
12443
12444                void f() {}, *p;
12445
12446              which is erroneous.  */
12447           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12448             {
12449               cp_token *token = cp_lexer_peek_token (parser->lexer);
12450               error_at (token->location,
12451                         "mixing"
12452                         " declarations and function-definitions is forbidden");
12453             }
12454           /* Otherwise, we're done with the list of declarators.  */
12455           else
12456             {
12457               pop_deferring_access_checks ();
12458               return;
12459             }
12460         }
12461       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12462         *maybe_range_for_decl = decl;
12463       /* The next token should be either a `,' or a `;'.  */
12464       token = cp_lexer_peek_token (parser->lexer);
12465       /* If it's a `,', there are more declarators to come.  */
12466       if (token->type == CPP_COMMA)
12467         /* will be consumed next time around */;
12468       /* If it's a `;', we are done.  */
12469       else if (token->type == CPP_SEMICOLON)
12470         break;
12471       else if (maybe_range_for_decl)
12472         {
12473           if (declares_class_or_enum && token->type == CPP_COLON)
12474             pedwarn (decl_specifiers.locations[ds_type_spec], 0,
12475                      "types may not be defined in a for-range-declaration");
12476           break;
12477         }
12478       /* Anything else is an error.  */
12479       else
12480         {
12481           /* If we have already issued an error message we don't need
12482              to issue another one.  */
12483           if ((decl != error_mark_node
12484                && DECL_INITIAL (decl) != error_mark_node)
12485               || cp_parser_uncommitted_to_tentative_parse_p (parser))
12486             cp_parser_error (parser, "expected %<,%> or %<;%>");
12487           /* Skip tokens until we reach the end of the statement.  */
12488           cp_parser_skip_to_end_of_statement (parser);
12489           /* If the next token is now a `;', consume it.  */
12490           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12491             cp_lexer_consume_token (parser->lexer);
12492           goto done;
12493         }
12494       /* After the first time around, a function-definition is not
12495          allowed -- even if it was OK at first.  For example:
12496
12497            int i, f() {}
12498
12499          is not valid.  */
12500       function_definition_allowed_p = false;
12501     }
12502
12503   /* Issue an error message if no declarators are present, and the
12504      decl-specifier-seq does not itself declare a class or
12505      enumeration: [dcl.dcl]/3.  */
12506   if (!saw_declarator)
12507     {
12508       if (cp_parser_declares_only_class_p (parser))
12509         {
12510           if (!declares_class_or_enum
12511               && decl_specifiers.type
12512               && OVERLOAD_TYPE_P (decl_specifiers.type))
12513             /* Ensure an error is issued anyway when finish_decltype_type,
12514                called via cp_parser_decl_specifier_seq, returns a class or
12515                an enumeration (c++/51786).  */
12516             decl_specifiers.type = NULL_TREE;
12517           shadow_tag (&decl_specifiers);
12518         }
12519       /* Perform any deferred access checks.  */
12520       perform_deferred_access_checks (tf_warning_or_error);
12521     }
12522
12523   /* Consume the `;'.  */
12524   if (!maybe_range_for_decl)
12525     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12526   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12527     {
12528       if (init_loc != UNKNOWN_LOCATION)
12529         error_at (init_loc, "initializer in range-based %<for%> loop");
12530       if (comma_loc != UNKNOWN_LOCATION)
12531         error_at (comma_loc,
12532                   "multiple declarations in range-based %<for%> loop");
12533     }
12534
12535  done:
12536   pop_deferring_access_checks ();
12537 }
12538
12539 /* Parse a decl-specifier-seq.
12540
12541    decl-specifier-seq:
12542      decl-specifier-seq [opt] decl-specifier
12543      decl-specifier attribute-specifier-seq [opt] (C++11)
12544
12545    decl-specifier:
12546      storage-class-specifier
12547      type-specifier
12548      function-specifier
12549      friend
12550      typedef
12551
12552    GNU Extension:
12553
12554    decl-specifier:
12555      attributes
12556
12557    Concepts Extension:
12558
12559    decl-specifier:
12560      concept
12561
12562    Set *DECL_SPECS to a representation of the decl-specifier-seq.
12563
12564    The parser flags FLAGS is used to control type-specifier parsing.
12565
12566    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12567    flags:
12568
12569      1: one of the decl-specifiers is an elaborated-type-specifier
12570         (i.e., a type declaration)
12571      2: one of the decl-specifiers is an enum-specifier or a
12572         class-specifier (i.e., a type definition)
12573
12574    */
12575
12576 static void
12577 cp_parser_decl_specifier_seq (cp_parser* parser,
12578                               cp_parser_flags flags,
12579                               cp_decl_specifier_seq *decl_specs,
12580                               int* declares_class_or_enum)
12581 {
12582   bool constructor_possible_p = !parser->in_declarator_p;
12583   bool found_decl_spec = false;
12584   cp_token *start_token = NULL;
12585   cp_decl_spec ds;
12586
12587   /* Clear DECL_SPECS.  */
12588   clear_decl_specs (decl_specs);
12589
12590   /* Assume no class or enumeration type is declared.  */
12591   *declares_class_or_enum = 0;
12592
12593   /* Keep reading specifiers until there are no more to read.  */
12594   while (true)
12595     {
12596       bool constructor_p;
12597       cp_token *token;
12598       ds = ds_last;
12599
12600       /* Peek at the next token.  */
12601       token = cp_lexer_peek_token (parser->lexer);
12602
12603       /* Save the first token of the decl spec list for error
12604          reporting.  */
12605       if (!start_token)
12606         start_token = token;
12607       /* Handle attributes.  */
12608       if (cp_next_tokens_can_be_attribute_p (parser))
12609         {
12610           /* Parse the attributes.  */
12611           tree attrs = cp_parser_attributes_opt (parser);
12612
12613           /* In a sequence of declaration specifiers, c++11 attributes
12614              appertain to the type that precede them. In that case
12615              [dcl.spec]/1 says:
12616
12617                  The attribute-specifier-seq affects the type only for
12618                  the declaration it appears in, not other declarations
12619                  involving the same type.
12620
12621              But for now let's force the user to position the
12622              attribute either at the beginning of the declaration or
12623              after the declarator-id, which would clearly mean that it
12624              applies to the declarator.  */
12625           if (cxx11_attribute_p (attrs))
12626             {
12627               if (!found_decl_spec)
12628                 /* The c++11 attribute is at the beginning of the
12629                    declaration.  It appertains to the entity being
12630                    declared.  */;
12631               else
12632                 {
12633                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12634                     {
12635                       /*  This is an attribute following a
12636                           class-specifier.  */
12637                       if (decl_specs->type_definition_p)
12638                         warn_misplaced_attr_for_class_type (token->location,
12639                                                             decl_specs->type);
12640                       attrs = NULL_TREE;
12641                     }
12642                   else
12643                     {
12644                       decl_specs->std_attributes
12645                         = chainon (decl_specs->std_attributes,
12646                                    attrs);
12647                       if (decl_specs->locations[ds_std_attribute] == 0)
12648                         decl_specs->locations[ds_std_attribute] = token->location;
12649                     }
12650                   continue;
12651                 }
12652             }
12653
12654             decl_specs->attributes
12655               = chainon (decl_specs->attributes,
12656                          attrs);
12657           if (decl_specs->locations[ds_attribute] == 0)
12658             decl_specs->locations[ds_attribute] = token->location;
12659           continue;
12660         }
12661       /* Assume we will find a decl-specifier keyword.  */
12662       found_decl_spec = true;
12663       /* If the next token is an appropriate keyword, we can simply
12664          add it to the list.  */
12665       switch (token->keyword)
12666         {
12667           /* decl-specifier:
12668                friend
12669                constexpr */
12670         case RID_FRIEND:
12671           if (!at_class_scope_p ())
12672             {
12673               error_at (token->location, "%<friend%> used outside of class");
12674               cp_lexer_purge_token (parser->lexer);
12675             }
12676           else
12677             {
12678               ds = ds_friend;
12679               /* Consume the token.  */
12680               cp_lexer_consume_token (parser->lexer);
12681             }
12682           break;
12683
12684         case RID_CONSTEXPR:
12685           ds = ds_constexpr;
12686           cp_lexer_consume_token (parser->lexer);
12687           break;
12688
12689         case RID_CONCEPT:
12690           ds = ds_concept;
12691           cp_lexer_consume_token (parser->lexer);
12692           break;
12693
12694           /* function-specifier:
12695                inline
12696                virtual
12697                explicit  */
12698         case RID_INLINE:
12699         case RID_VIRTUAL:
12700         case RID_EXPLICIT:
12701           cp_parser_function_specifier_opt (parser, decl_specs);
12702           break;
12703
12704           /* decl-specifier:
12705                typedef  */
12706         case RID_TYPEDEF:
12707           ds = ds_typedef;
12708           /* Consume the token.  */
12709           cp_lexer_consume_token (parser->lexer);
12710           /* A constructor declarator cannot appear in a typedef.  */
12711           constructor_possible_p = false;
12712           /* The "typedef" keyword can only occur in a declaration; we
12713              may as well commit at this point.  */
12714           cp_parser_commit_to_tentative_parse (parser);
12715
12716           if (decl_specs->storage_class != sc_none)
12717             decl_specs->conflicting_specifiers_p = true;
12718           break;
12719
12720           /* storage-class-specifier:
12721                auto
12722                register
12723                static
12724                extern
12725                mutable
12726
12727              GNU Extension:
12728                thread  */
12729         case RID_AUTO:
12730           if (cxx_dialect == cxx98) 
12731             {
12732               /* Consume the token.  */
12733               cp_lexer_consume_token (parser->lexer);
12734
12735               /* Complain about `auto' as a storage specifier, if
12736                  we're complaining about C++0x compatibility.  */
12737               warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12738                           " changes meaning in C++11; please remove it");
12739
12740               /* Set the storage class anyway.  */
12741               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12742                                            token);
12743             }
12744           else
12745             /* C++0x auto type-specifier.  */
12746             found_decl_spec = false;
12747           break;
12748
12749         case RID_REGISTER:
12750         case RID_STATIC:
12751         case RID_EXTERN:
12752         case RID_MUTABLE:
12753           /* Consume the token.  */
12754           cp_lexer_consume_token (parser->lexer);
12755           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12756                                        token);
12757           break;
12758         case RID_THREAD:
12759           /* Consume the token.  */
12760           ds = ds_thread;
12761           cp_lexer_consume_token (parser->lexer);
12762           break;
12763
12764         default:
12765           /* We did not yet find a decl-specifier yet.  */
12766           found_decl_spec = false;
12767           break;
12768         }
12769
12770       if (found_decl_spec
12771           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12772           && token->keyword != RID_CONSTEXPR)
12773         error ("decl-specifier invalid in condition");
12774
12775       if (ds != ds_last)
12776         set_and_check_decl_spec_loc (decl_specs, ds, token);
12777
12778       /* Constructors are a special case.  The `S' in `S()' is not a
12779          decl-specifier; it is the beginning of the declarator.  */
12780       constructor_p
12781         = (!found_decl_spec
12782            && constructor_possible_p
12783            && (cp_parser_constructor_declarator_p
12784                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12785
12786       /* If we don't have a DECL_SPEC yet, then we must be looking at
12787          a type-specifier.  */
12788       if (!found_decl_spec && !constructor_p)
12789         {
12790           int decl_spec_declares_class_or_enum;
12791           bool is_cv_qualifier;
12792           tree type_spec;
12793
12794           type_spec
12795             = cp_parser_type_specifier (parser, flags,
12796                                         decl_specs,
12797                                         /*is_declaration=*/true,
12798                                         &decl_spec_declares_class_or_enum,
12799                                         &is_cv_qualifier);
12800           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12801
12802           /* If this type-specifier referenced a user-defined type
12803              (a typedef, class-name, etc.), then we can't allow any
12804              more such type-specifiers henceforth.
12805
12806              [dcl.spec]
12807
12808              The longest sequence of decl-specifiers that could
12809              possibly be a type name is taken as the
12810              decl-specifier-seq of a declaration.  The sequence shall
12811              be self-consistent as described below.
12812
12813              [dcl.type]
12814
12815              As a general rule, at most one type-specifier is allowed
12816              in the complete decl-specifier-seq of a declaration.  The
12817              only exceptions are the following:
12818
12819              -- const or volatile can be combined with any other
12820                 type-specifier.
12821
12822              -- signed or unsigned can be combined with char, long,
12823                 short, or int.
12824
12825              -- ..
12826
12827              Example:
12828
12829                typedef char* Pc;
12830                void g (const int Pc);
12831
12832              Here, Pc is *not* part of the decl-specifier seq; it's
12833              the declarator.  Therefore, once we see a type-specifier
12834              (other than a cv-qualifier), we forbid any additional
12835              user-defined types.  We *do* still allow things like `int
12836              int' to be considered a decl-specifier-seq, and issue the
12837              error message later.  */
12838           if (type_spec && !is_cv_qualifier)
12839             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12840           /* A constructor declarator cannot follow a type-specifier.  */
12841           if (type_spec)
12842             {
12843               constructor_possible_p = false;
12844               found_decl_spec = true;
12845               if (!is_cv_qualifier)
12846                 decl_specs->any_type_specifiers_p = true;
12847             }
12848         }
12849
12850       /* If we still do not have a DECL_SPEC, then there are no more
12851          decl-specifiers.  */
12852       if (!found_decl_spec)
12853         break;
12854
12855       decl_specs->any_specifiers_p = true;
12856       /* After we see one decl-specifier, further decl-specifiers are
12857          always optional.  */
12858       flags |= CP_PARSER_FLAGS_OPTIONAL;
12859     }
12860
12861   /* Don't allow a friend specifier with a class definition.  */
12862   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12863       && (*declares_class_or_enum & 2))
12864     error_at (decl_specs->locations[ds_friend],
12865               "class definition may not be declared a friend");
12866 }
12867
12868 /* Parse an (optional) storage-class-specifier.
12869
12870    storage-class-specifier:
12871      auto
12872      register
12873      static
12874      extern
12875      mutable
12876
12877    GNU Extension:
12878
12879    storage-class-specifier:
12880      thread
12881
12882    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
12883
12884 static tree
12885 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12886 {
12887   switch (cp_lexer_peek_token (parser->lexer)->keyword)
12888     {
12889     case RID_AUTO:
12890       if (cxx_dialect != cxx98)
12891         return NULL_TREE;
12892       /* Fall through for C++98.  */
12893
12894     case RID_REGISTER:
12895     case RID_STATIC:
12896     case RID_EXTERN:
12897     case RID_MUTABLE:
12898     case RID_THREAD:
12899       /* Consume the token.  */
12900       return cp_lexer_consume_token (parser->lexer)->u.value;
12901
12902     default:
12903       return NULL_TREE;
12904     }
12905 }
12906
12907 /* Parse an (optional) function-specifier.
12908
12909    function-specifier:
12910      inline
12911      virtual
12912      explicit
12913
12914    Returns an IDENTIFIER_NODE corresponding to the keyword used.
12915    Updates DECL_SPECS, if it is non-NULL.  */
12916
12917 static tree
12918 cp_parser_function_specifier_opt (cp_parser* parser,
12919                                   cp_decl_specifier_seq *decl_specs)
12920 {
12921   cp_token *token = cp_lexer_peek_token (parser->lexer);
12922   switch (token->keyword)
12923     {
12924     case RID_INLINE:
12925       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12926       break;
12927
12928     case RID_VIRTUAL:
12929       /* 14.5.2.3 [temp.mem]
12930
12931          A member function template shall not be virtual.  */
12932       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12933         error_at (token->location, "templates may not be %<virtual%>");
12934       else
12935         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12936       break;
12937
12938     case RID_EXPLICIT:
12939       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12940       break;
12941
12942     default:
12943       return NULL_TREE;
12944     }
12945
12946   /* Consume the token.  */
12947   return cp_lexer_consume_token (parser->lexer)->u.value;
12948 }
12949
12950 /* Parse a linkage-specification.
12951
12952    linkage-specification:
12953      extern string-literal { declaration-seq [opt] }
12954      extern string-literal declaration  */
12955
12956 static void
12957 cp_parser_linkage_specification (cp_parser* parser)
12958 {
12959   tree linkage;
12960
12961   /* Look for the `extern' keyword.  */
12962   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12963
12964   /* Look for the string-literal.  */
12965   linkage = cp_parser_string_literal (parser, false, false);
12966
12967   /* Transform the literal into an identifier.  If the literal is a
12968      wide-character string, or contains embedded NULs, then we can't
12969      handle it as the user wants.  */
12970   if (strlen (TREE_STRING_POINTER (linkage))
12971       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12972     {
12973       cp_parser_error (parser, "invalid linkage-specification");
12974       /* Assume C++ linkage.  */
12975       linkage = lang_name_cplusplus;
12976     }
12977   else
12978     linkage = get_identifier (TREE_STRING_POINTER (linkage));
12979
12980   /* We're now using the new linkage.  */
12981   push_lang_context (linkage);
12982
12983   /* If the next token is a `{', then we're using the first
12984      production.  */
12985   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12986     {
12987       cp_ensure_no_omp_declare_simd (parser);
12988       cp_ensure_no_oacc_routine (parser);
12989
12990       /* Consume the `{' token.  */
12991       cp_lexer_consume_token (parser->lexer);
12992       /* Parse the declarations.  */
12993       cp_parser_declaration_seq_opt (parser);
12994       /* Look for the closing `}'.  */
12995       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12996     }
12997   /* Otherwise, there's just one declaration.  */
12998   else
12999     {
13000       bool saved_in_unbraced_linkage_specification_p;
13001
13002       saved_in_unbraced_linkage_specification_p
13003         = parser->in_unbraced_linkage_specification_p;
13004       parser->in_unbraced_linkage_specification_p = true;
13005       cp_parser_declaration (parser);
13006       parser->in_unbraced_linkage_specification_p
13007         = saved_in_unbraced_linkage_specification_p;
13008     }
13009
13010   /* We're done with the linkage-specification.  */
13011   pop_lang_context ();
13012 }
13013
13014 /* Parse a static_assert-declaration.
13015
13016    static_assert-declaration:
13017      static_assert ( constant-expression , string-literal ) ; 
13018      static_assert ( constant-expression ) ; (C++1Z)
13019
13020    If MEMBER_P, this static_assert is a class member.  */
13021
13022 static void 
13023 cp_parser_static_assert(cp_parser *parser, bool member_p)
13024 {
13025   tree condition;
13026   tree message;
13027   cp_token *token;
13028   location_t saved_loc;
13029   bool dummy;
13030
13031   /* Peek at the `static_assert' token so we can keep track of exactly
13032      where the static assertion started.  */
13033   token = cp_lexer_peek_token (parser->lexer);
13034   saved_loc = token->location;
13035
13036   /* Look for the `static_assert' keyword.  */
13037   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
13038                                   RT_STATIC_ASSERT))
13039     return;
13040
13041   /*  We know we are in a static assertion; commit to any tentative
13042       parse.  */
13043   if (cp_parser_parsing_tentatively (parser))
13044     cp_parser_commit_to_tentative_parse (parser);
13045
13046   /* Parse the `(' starting the static assertion condition.  */
13047   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13048
13049   /* Parse the constant-expression.  Allow a non-constant expression
13050      here in order to give better diagnostics in finish_static_assert.  */
13051   condition = 
13052     cp_parser_constant_expression (parser,
13053                                    /*allow_non_constant_p=*/true,
13054                                    /*non_constant_p=*/&dummy);
13055
13056   if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13057     {
13058       if (cxx_dialect < cxx1z)
13059         pedwarn (input_location, OPT_Wpedantic,
13060                  "static_assert without a message "
13061                  "only available with -std=c++1z or -std=gnu++1z");
13062       /* Eat the ')'  */
13063       cp_lexer_consume_token (parser->lexer);
13064       message = build_string (1, "");
13065       TREE_TYPE (message) = char_array_type_node;
13066       fix_string_type (message);
13067     }
13068   else
13069     {
13070       /* Parse the separating `,'.  */
13071       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13072
13073       /* Parse the string-literal message.  */
13074       message = cp_parser_string_literal (parser, 
13075                                           /*translate=*/false,
13076                                           /*wide_ok=*/true);
13077
13078       /* A `)' completes the static assertion.  */
13079       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13080         cp_parser_skip_to_closing_parenthesis (parser, 
13081                                                /*recovering=*/true, 
13082                                                /*or_comma=*/false,
13083                                                /*consume_paren=*/true);
13084     }
13085
13086   /* A semicolon terminates the declaration.  */
13087   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13088
13089   /* Complete the static assertion, which may mean either processing 
13090      the static assert now or saving it for template instantiation.  */
13091   finish_static_assert (condition, message, saved_loc, member_p);
13092 }
13093
13094 /* Parse the expression in decltype ( expression ).  */
13095
13096 static tree
13097 cp_parser_decltype_expr (cp_parser *parser,
13098                          bool &id_expression_or_member_access_p)
13099 {
13100   cp_token *id_expr_start_token;
13101   tree expr;
13102
13103   /* Since we're going to preserve any side-effects from this parse, set up a
13104      firewall to protect our callers from cp_parser_commit_to_tentative_parse
13105      in the expression.  */
13106   tentative_firewall firewall (parser);
13107
13108   /* First, try parsing an id-expression.  */
13109   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13110   cp_parser_parse_tentatively (parser);
13111   expr = cp_parser_id_expression (parser,
13112                                   /*template_keyword_p=*/false,
13113                                   /*check_dependency_p=*/true,
13114                                   /*template_p=*/NULL,
13115                                   /*declarator_p=*/false,
13116                                   /*optional_p=*/false);
13117
13118   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13119     {
13120       bool non_integral_constant_expression_p = false;
13121       tree id_expression = expr;
13122       cp_id_kind idk;
13123       const char *error_msg;
13124
13125       if (identifier_p (expr))
13126         /* Lookup the name we got back from the id-expression.  */
13127         expr = cp_parser_lookup_name_simple (parser, expr,
13128                                              id_expr_start_token->location);
13129
13130       if (expr
13131           && expr != error_mark_node
13132           && TREE_CODE (expr) != TYPE_DECL
13133           && (TREE_CODE (expr) != BIT_NOT_EXPR
13134               || !TYPE_P (TREE_OPERAND (expr, 0)))
13135           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13136         {
13137           /* Complete lookup of the id-expression.  */
13138           expr = (finish_id_expression
13139                   (id_expression, expr, parser->scope, &idk,
13140                    /*integral_constant_expression_p=*/false,
13141                    /*allow_non_integral_constant_expression_p=*/true,
13142                    &non_integral_constant_expression_p,
13143                    /*template_p=*/false,
13144                    /*done=*/true,
13145                    /*address_p=*/false,
13146                    /*template_arg_p=*/false,
13147                    &error_msg,
13148                    id_expr_start_token->location));
13149
13150           if (expr == error_mark_node)
13151             /* We found an id-expression, but it was something that we
13152                should not have found. This is an error, not something
13153                we can recover from, so note that we found an
13154                id-expression and we'll recover as gracefully as
13155                possible.  */
13156             id_expression_or_member_access_p = true;
13157         }
13158
13159       if (expr 
13160           && expr != error_mark_node
13161           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13162         /* We have an id-expression.  */
13163         id_expression_or_member_access_p = true;
13164     }
13165
13166   if (!id_expression_or_member_access_p)
13167     {
13168       /* Abort the id-expression parse.  */
13169       cp_parser_abort_tentative_parse (parser);
13170
13171       /* Parsing tentatively, again.  */
13172       cp_parser_parse_tentatively (parser);
13173
13174       /* Parse a class member access.  */
13175       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13176                                            /*cast_p=*/false, /*decltype*/true,
13177                                            /*member_access_only_p=*/true, NULL);
13178
13179       if (expr 
13180           && expr != error_mark_node
13181           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13182         /* We have an id-expression.  */
13183         id_expression_or_member_access_p = true;
13184     }
13185
13186   if (id_expression_or_member_access_p)
13187     /* We have parsed the complete id-expression or member access.  */
13188     cp_parser_parse_definitely (parser);
13189   else
13190     {
13191       /* Abort our attempt to parse an id-expression or member access
13192          expression.  */
13193       cp_parser_abort_tentative_parse (parser);
13194
13195       /* Parse a full expression.  */
13196       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13197                                    /*decltype_p=*/true);
13198     }
13199
13200   return expr;
13201 }
13202
13203 /* Parse a `decltype' type. Returns the type.
13204
13205    simple-type-specifier:
13206      decltype ( expression )
13207    C++14 proposal:
13208      decltype ( auto )  */
13209
13210 static tree
13211 cp_parser_decltype (cp_parser *parser)
13212 {
13213   tree expr;
13214   bool id_expression_or_member_access_p = false;
13215   const char *saved_message;
13216   bool saved_integral_constant_expression_p;
13217   bool saved_non_integral_constant_expression_p;
13218   bool saved_greater_than_is_operator_p;
13219   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13220
13221   if (start_token->type == CPP_DECLTYPE)
13222     {
13223       /* Already parsed.  */
13224       cp_lexer_consume_token (parser->lexer);
13225       return saved_checks_value (start_token->u.tree_check_value);
13226     }
13227
13228   /* Look for the `decltype' token.  */
13229   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13230     return error_mark_node;
13231
13232   /* Parse the opening `('.  */
13233   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13234     return error_mark_node;
13235
13236   /* decltype (auto) */
13237   if (cxx_dialect >= cxx14
13238       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13239     {
13240       cp_lexer_consume_token (parser->lexer);
13241       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13242         return error_mark_node;
13243       expr = make_decltype_auto ();
13244       AUTO_IS_DECLTYPE (expr) = true;
13245       goto rewrite;
13246     }
13247
13248   /* Types cannot be defined in a `decltype' expression.  Save away the
13249      old message.  */
13250   saved_message = parser->type_definition_forbidden_message;
13251
13252   /* And create the new one.  */
13253   parser->type_definition_forbidden_message
13254     = G_("types may not be defined in %<decltype%> expressions");
13255
13256   /* The restrictions on constant-expressions do not apply inside
13257      decltype expressions.  */
13258   saved_integral_constant_expression_p
13259     = parser->integral_constant_expression_p;
13260   saved_non_integral_constant_expression_p
13261     = parser->non_integral_constant_expression_p;
13262   parser->integral_constant_expression_p = false;
13263
13264   /* Within a parenthesized expression, a `>' token is always
13265      the greater-than operator.  */
13266   saved_greater_than_is_operator_p
13267     = parser->greater_than_is_operator_p;
13268   parser->greater_than_is_operator_p = true;
13269
13270   /* Do not actually evaluate the expression.  */
13271   ++cp_unevaluated_operand;
13272
13273   /* Do not warn about problems with the expression.  */
13274   ++c_inhibit_evaluation_warnings;
13275
13276   expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13277
13278   /* Go back to evaluating expressions.  */
13279   --cp_unevaluated_operand;
13280   --c_inhibit_evaluation_warnings;
13281
13282   /* The `>' token might be the end of a template-id or
13283      template-parameter-list now.  */
13284   parser->greater_than_is_operator_p
13285     = saved_greater_than_is_operator_p;
13286
13287   /* Restore the old message and the integral constant expression
13288      flags.  */
13289   parser->type_definition_forbidden_message = saved_message;
13290   parser->integral_constant_expression_p
13291     = saved_integral_constant_expression_p;
13292   parser->non_integral_constant_expression_p
13293     = saved_non_integral_constant_expression_p;
13294
13295   /* Parse to the closing `)'.  */
13296   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13297     {
13298       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13299                                              /*consume_paren=*/true);
13300       return error_mark_node;
13301     }
13302
13303   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13304                                tf_warning_or_error);
13305
13306  rewrite:
13307   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13308      it again.  */
13309   start_token->type = CPP_DECLTYPE;
13310   start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13311   start_token->u.tree_check_value->value = expr;
13312   start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13313   start_token->keyword = RID_MAX;
13314   cp_lexer_purge_tokens_after (parser->lexer, start_token);
13315
13316   return expr;
13317 }
13318
13319 /* Special member functions [gram.special] */
13320
13321 /* Parse a conversion-function-id.
13322
13323    conversion-function-id:
13324      operator conversion-type-id
13325
13326    Returns an IDENTIFIER_NODE representing the operator.  */
13327
13328 static tree
13329 cp_parser_conversion_function_id (cp_parser* parser)
13330 {
13331   tree type;
13332   tree saved_scope;
13333   tree saved_qualifying_scope;
13334   tree saved_object_scope;
13335   tree pushed_scope = NULL_TREE;
13336
13337   /* Look for the `operator' token.  */
13338   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13339     return error_mark_node;
13340   /* When we parse the conversion-type-id, the current scope will be
13341      reset.  However, we need that information in able to look up the
13342      conversion function later, so we save it here.  */
13343   saved_scope = parser->scope;
13344   saved_qualifying_scope = parser->qualifying_scope;
13345   saved_object_scope = parser->object_scope;
13346   /* We must enter the scope of the class so that the names of
13347      entities declared within the class are available in the
13348      conversion-type-id.  For example, consider:
13349
13350        struct S {
13351          typedef int I;
13352          operator I();
13353        };
13354
13355        S::operator I() { ... }
13356
13357      In order to see that `I' is a type-name in the definition, we
13358      must be in the scope of `S'.  */
13359   if (saved_scope)
13360     pushed_scope = push_scope (saved_scope);
13361   /* Parse the conversion-type-id.  */
13362   type = cp_parser_conversion_type_id (parser);
13363   /* Leave the scope of the class, if any.  */
13364   if (pushed_scope)
13365     pop_scope (pushed_scope);
13366   /* Restore the saved scope.  */
13367   parser->scope = saved_scope;
13368   parser->qualifying_scope = saved_qualifying_scope;
13369   parser->object_scope = saved_object_scope;
13370   /* If the TYPE is invalid, indicate failure.  */
13371   if (type == error_mark_node)
13372     return error_mark_node;
13373   return mangle_conv_op_name_for_type (type);
13374 }
13375
13376 /* Parse a conversion-type-id:
13377
13378    conversion-type-id:
13379      type-specifier-seq conversion-declarator [opt]
13380
13381    Returns the TYPE specified.  */
13382
13383 static tree
13384 cp_parser_conversion_type_id (cp_parser* parser)
13385 {
13386   tree attributes;
13387   cp_decl_specifier_seq type_specifiers;
13388   cp_declarator *declarator;
13389   tree type_specified;
13390   const char *saved_message;
13391
13392   /* Parse the attributes.  */
13393   attributes = cp_parser_attributes_opt (parser);
13394
13395   saved_message = parser->type_definition_forbidden_message;
13396   parser->type_definition_forbidden_message
13397     = G_("types may not be defined in a conversion-type-id");
13398
13399   /* Parse the type-specifiers.  */
13400   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13401                                 /*is_trailing_return=*/false,
13402                                 &type_specifiers);
13403
13404   parser->type_definition_forbidden_message = saved_message;
13405
13406   /* If that didn't work, stop.  */
13407   if (type_specifiers.type == error_mark_node)
13408     return error_mark_node;
13409   /* Parse the conversion-declarator.  */
13410   declarator = cp_parser_conversion_declarator_opt (parser);
13411
13412   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
13413                                     /*initialized=*/0, &attributes);
13414   if (attributes)
13415     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13416
13417   /* Don't give this error when parsing tentatively.  This happens to
13418      work because we always parse this definitively once.  */
13419   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13420       && type_uses_auto (type_specified))
13421     {
13422       if (cxx_dialect < cxx14)
13423         {
13424           error ("invalid use of %<auto%> in conversion operator");
13425           return error_mark_node;
13426         }
13427       else if (template_parm_scope_p ())
13428         warning (0, "use of %<auto%> in member template "
13429                  "conversion operator can never be deduced");
13430     }
13431
13432   return type_specified;
13433 }
13434
13435 /* Parse an (optional) conversion-declarator.
13436
13437    conversion-declarator:
13438      ptr-operator conversion-declarator [opt]
13439
13440    */
13441
13442 static cp_declarator *
13443 cp_parser_conversion_declarator_opt (cp_parser* parser)
13444 {
13445   enum tree_code code;
13446   tree class_type, std_attributes = NULL_TREE;
13447   cp_cv_quals cv_quals;
13448
13449   /* We don't know if there's a ptr-operator next, or not.  */
13450   cp_parser_parse_tentatively (parser);
13451   /* Try the ptr-operator.  */
13452   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13453                                  &std_attributes);
13454   /* If it worked, look for more conversion-declarators.  */
13455   if (cp_parser_parse_definitely (parser))
13456     {
13457       cp_declarator *declarator;
13458
13459       /* Parse another optional declarator.  */
13460       declarator = cp_parser_conversion_declarator_opt (parser);
13461
13462       declarator = cp_parser_make_indirect_declarator
13463         (code, class_type, cv_quals, declarator, std_attributes);
13464
13465       return declarator;
13466    }
13467
13468   return NULL;
13469 }
13470
13471 /* Parse an (optional) ctor-initializer.
13472
13473    ctor-initializer:
13474      : mem-initializer-list
13475
13476    Returns TRUE iff the ctor-initializer was actually present.  */
13477
13478 static bool
13479 cp_parser_ctor_initializer_opt (cp_parser* parser)
13480 {
13481   /* If the next token is not a `:', then there is no
13482      ctor-initializer.  */
13483   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13484     {
13485       /* Do default initialization of any bases and members.  */
13486       if (DECL_CONSTRUCTOR_P (current_function_decl))
13487         finish_mem_initializers (NULL_TREE);
13488
13489       return false;
13490     }
13491
13492   /* Consume the `:' token.  */
13493   cp_lexer_consume_token (parser->lexer);
13494   /* And the mem-initializer-list.  */
13495   cp_parser_mem_initializer_list (parser);
13496
13497   return true;
13498 }
13499
13500 /* Parse a mem-initializer-list.
13501
13502    mem-initializer-list:
13503      mem-initializer ... [opt]
13504      mem-initializer ... [opt] , mem-initializer-list  */
13505
13506 static void
13507 cp_parser_mem_initializer_list (cp_parser* parser)
13508 {
13509   tree mem_initializer_list = NULL_TREE;
13510   tree target_ctor = error_mark_node;
13511   cp_token *token = cp_lexer_peek_token (parser->lexer);
13512
13513   /* Let the semantic analysis code know that we are starting the
13514      mem-initializer-list.  */
13515   if (!DECL_CONSTRUCTOR_P (current_function_decl))
13516     error_at (token->location,
13517               "only constructors take member initializers");
13518
13519   /* Loop through the list.  */
13520   while (true)
13521     {
13522       tree mem_initializer;
13523
13524       token = cp_lexer_peek_token (parser->lexer);
13525       /* Parse the mem-initializer.  */
13526       mem_initializer = cp_parser_mem_initializer (parser);
13527       /* If the next token is a `...', we're expanding member initializers. */
13528       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13529         {
13530           /* Consume the `...'. */
13531           cp_lexer_consume_token (parser->lexer);
13532
13533           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13534              can be expanded but members cannot. */
13535           if (mem_initializer != error_mark_node
13536               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13537             {
13538               error_at (token->location,
13539                         "cannot expand initializer for member %<%D%>",
13540                         TREE_PURPOSE (mem_initializer));
13541               mem_initializer = error_mark_node;
13542             }
13543
13544           /* Construct the pack expansion type. */
13545           if (mem_initializer != error_mark_node)
13546             mem_initializer = make_pack_expansion (mem_initializer);
13547         }
13548       if (target_ctor != error_mark_node
13549           && mem_initializer != error_mark_node)
13550         {
13551           error ("mem-initializer for %qD follows constructor delegation",
13552                  TREE_PURPOSE (mem_initializer));
13553           mem_initializer = error_mark_node;
13554         }
13555       /* Look for a target constructor. */
13556       if (mem_initializer != error_mark_node
13557           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13558           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13559         {
13560           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13561           if (mem_initializer_list)
13562             {
13563               error ("constructor delegation follows mem-initializer for %qD",
13564                      TREE_PURPOSE (mem_initializer_list));
13565               mem_initializer = error_mark_node;
13566             }
13567           target_ctor = mem_initializer;
13568         }
13569       /* Add it to the list, unless it was erroneous.  */
13570       if (mem_initializer != error_mark_node)
13571         {
13572           TREE_CHAIN (mem_initializer) = mem_initializer_list;
13573           mem_initializer_list = mem_initializer;
13574         }
13575       /* If the next token is not a `,', we're done.  */
13576       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13577         break;
13578       /* Consume the `,' token.  */
13579       cp_lexer_consume_token (parser->lexer);
13580     }
13581
13582   /* Perform semantic analysis.  */
13583   if (DECL_CONSTRUCTOR_P (current_function_decl))
13584     finish_mem_initializers (mem_initializer_list);
13585 }
13586
13587 /* Parse a mem-initializer.
13588
13589    mem-initializer:
13590      mem-initializer-id ( expression-list [opt] )
13591      mem-initializer-id braced-init-list
13592
13593    GNU extension:
13594
13595    mem-initializer:
13596      ( expression-list [opt] )
13597
13598    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
13599    class) or FIELD_DECL (for a non-static data member) to initialize;
13600    the TREE_VALUE is the expression-list.  An empty initialization
13601    list is represented by void_list_node.  */
13602
13603 static tree
13604 cp_parser_mem_initializer (cp_parser* parser)
13605 {
13606   tree mem_initializer_id;
13607   tree expression_list;
13608   tree member;
13609   cp_token *token = cp_lexer_peek_token (parser->lexer);
13610
13611   /* Find out what is being initialized.  */
13612   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13613     {
13614       permerror (token->location,
13615                  "anachronistic old-style base class initializer");
13616       mem_initializer_id = NULL_TREE;
13617     }
13618   else
13619     {
13620       mem_initializer_id = cp_parser_mem_initializer_id (parser);
13621       if (mem_initializer_id == error_mark_node)
13622         return mem_initializer_id;
13623     }
13624   member = expand_member_init (mem_initializer_id);
13625   if (member && !DECL_P (member))
13626     in_base_initializer = 1;
13627
13628   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13629     {
13630       bool expr_non_constant_p;
13631       cp_lexer_set_source_position (parser->lexer);
13632       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13633       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13634       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13635       expression_list = build_tree_list (NULL_TREE, expression_list);
13636     }
13637   else
13638     {
13639       vec<tree, va_gc> *vec;
13640       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13641                                                      /*cast_p=*/false,
13642                                                      /*allow_expansion_p=*/true,
13643                                                      /*non_constant_p=*/NULL);
13644       if (vec == NULL)
13645         return error_mark_node;
13646       expression_list = build_tree_list_vec (vec);
13647       release_tree_vector (vec);
13648     }
13649
13650   if (expression_list == error_mark_node)
13651     return error_mark_node;
13652   if (!expression_list)
13653     expression_list = void_type_node;
13654
13655   in_base_initializer = 0;
13656
13657   return member ? build_tree_list (member, expression_list) : error_mark_node;
13658 }
13659
13660 /* Parse a mem-initializer-id.
13661
13662    mem-initializer-id:
13663      :: [opt] nested-name-specifier [opt] class-name
13664      decltype-specifier (C++11)
13665      identifier
13666
13667    Returns a TYPE indicating the class to be initialized for the first
13668    production (and the second in C++11).  Returns an IDENTIFIER_NODE
13669    indicating the data member to be initialized for the last production.  */
13670
13671 static tree
13672 cp_parser_mem_initializer_id (cp_parser* parser)
13673 {
13674   bool global_scope_p;
13675   bool nested_name_specifier_p;
13676   bool template_p = false;
13677   tree id;
13678
13679   cp_token *token = cp_lexer_peek_token (parser->lexer);
13680
13681   /* `typename' is not allowed in this context ([temp.res]).  */
13682   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13683     {
13684       error_at (token->location, 
13685                 "keyword %<typename%> not allowed in this context (a qualified "
13686                 "member initializer is implicitly a type)");
13687       cp_lexer_consume_token (parser->lexer);
13688     }
13689   /* Look for the optional `::' operator.  */
13690   global_scope_p
13691     = (cp_parser_global_scope_opt (parser,
13692                                    /*current_scope_valid_p=*/false)
13693        != NULL_TREE);
13694   /* Look for the optional nested-name-specifier.  The simplest way to
13695      implement:
13696
13697        [temp.res]
13698
13699        The keyword `typename' is not permitted in a base-specifier or
13700        mem-initializer; in these contexts a qualified name that
13701        depends on a template-parameter is implicitly assumed to be a
13702        type name.
13703
13704      is to assume that we have seen the `typename' keyword at this
13705      point.  */
13706   nested_name_specifier_p
13707     = (cp_parser_nested_name_specifier_opt (parser,
13708                                             /*typename_keyword_p=*/true,
13709                                             /*check_dependency_p=*/true,
13710                                             /*type_p=*/true,
13711                                             /*is_declaration=*/true)
13712        != NULL_TREE);
13713   if (nested_name_specifier_p)
13714     template_p = cp_parser_optional_template_keyword (parser);
13715   /* If there is a `::' operator or a nested-name-specifier, then we
13716      are definitely looking for a class-name.  */
13717   if (global_scope_p || nested_name_specifier_p)
13718     return cp_parser_class_name (parser,
13719                                  /*typename_keyword_p=*/true,
13720                                  /*template_keyword_p=*/template_p,
13721                                  typename_type,
13722                                  /*check_dependency_p=*/true,
13723                                  /*class_head_p=*/false,
13724                                  /*is_declaration=*/true);
13725   /* Otherwise, we could also be looking for an ordinary identifier.  */
13726   cp_parser_parse_tentatively (parser);
13727   if (cp_lexer_next_token_is_decltype (parser->lexer))
13728     /* Try a decltype-specifier.  */
13729     id = cp_parser_decltype (parser);
13730   else
13731     /* Otherwise, try a class-name.  */
13732     id = cp_parser_class_name (parser,
13733                                /*typename_keyword_p=*/true,
13734                                /*template_keyword_p=*/false,
13735                                none_type,
13736                                /*check_dependency_p=*/true,
13737                                /*class_head_p=*/false,
13738                                /*is_declaration=*/true);
13739   /* If we found one, we're done.  */
13740   if (cp_parser_parse_definitely (parser))
13741     return id;
13742   /* Otherwise, look for an ordinary identifier.  */
13743   return cp_parser_identifier (parser);
13744 }
13745
13746 /* Overloading [gram.over] */
13747
13748 /* Parse an operator-function-id.
13749
13750    operator-function-id:
13751      operator operator
13752
13753    Returns an IDENTIFIER_NODE for the operator which is a
13754    human-readable spelling of the identifier, e.g., `operator +'.  */
13755
13756 static cp_expr
13757 cp_parser_operator_function_id (cp_parser* parser)
13758 {
13759   /* Look for the `operator' keyword.  */
13760   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13761     return error_mark_node;
13762   /* And then the name of the operator itself.  */
13763   return cp_parser_operator (parser);
13764 }
13765
13766 /* Return an identifier node for a user-defined literal operator.
13767    The suffix identifier is chained to the operator name identifier.  */
13768
13769 static tree
13770 cp_literal_operator_id (const char* name)
13771 {
13772   tree identifier;
13773   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13774                               + strlen (name) + 10);
13775   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13776   identifier = get_identifier (buffer);
13777
13778   return identifier;
13779 }
13780
13781 /* Parse an operator.
13782
13783    operator:
13784      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13785      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13786      || ++ -- , ->* -> () []
13787
13788    GNU Extensions:
13789
13790    operator:
13791      <? >? <?= >?=
13792
13793    Returns an IDENTIFIER_NODE for the operator which is a
13794    human-readable spelling of the identifier, e.g., `operator +'.  */
13795
13796 static cp_expr
13797 cp_parser_operator (cp_parser* parser)
13798 {
13799   tree id = NULL_TREE;
13800   cp_token *token;
13801   bool utf8 = false;
13802
13803   /* Peek at the next token.  */
13804   token = cp_lexer_peek_token (parser->lexer);
13805
13806   location_t start_loc = token->location;
13807
13808   /* Figure out which operator we have.  */
13809   switch (token->type)
13810     {
13811     case CPP_KEYWORD:
13812       {
13813         enum tree_code op;
13814
13815         /* The keyword should be either `new' or `delete'.  */
13816         if (token->keyword == RID_NEW)
13817           op = NEW_EXPR;
13818         else if (token->keyword == RID_DELETE)
13819           op = DELETE_EXPR;
13820         else
13821           break;
13822
13823         /* Consume the `new' or `delete' token.  */
13824         location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13825
13826         /* Peek at the next token.  */
13827         token = cp_lexer_peek_token (parser->lexer);
13828         /* If it's a `[' token then this is the array variant of the
13829            operator.  */
13830         if (token->type == CPP_OPEN_SQUARE)
13831           {
13832             /* Consume the `[' token.  */
13833             cp_lexer_consume_token (parser->lexer);
13834             /* Look for the `]' token.  */
13835             if (cp_token *close_token
13836                 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13837               end_loc = close_token->location;
13838             id = ansi_opname (op == NEW_EXPR
13839                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13840           }
13841         /* Otherwise, we have the non-array variant.  */
13842         else
13843           id = ansi_opname (op);
13844
13845         location_t loc = make_location (start_loc, start_loc, end_loc);
13846
13847         return cp_expr (id, loc);
13848       }
13849
13850     case CPP_PLUS:
13851       id = ansi_opname (PLUS_EXPR);
13852       break;
13853
13854     case CPP_MINUS:
13855       id = ansi_opname (MINUS_EXPR);
13856       break;
13857
13858     case CPP_MULT:
13859       id = ansi_opname (MULT_EXPR);
13860       break;
13861
13862     case CPP_DIV:
13863       id = ansi_opname (TRUNC_DIV_EXPR);
13864       break;
13865
13866     case CPP_MOD:
13867       id = ansi_opname (TRUNC_MOD_EXPR);
13868       break;
13869
13870     case CPP_XOR:
13871       id = ansi_opname (BIT_XOR_EXPR);
13872       break;
13873
13874     case CPP_AND:
13875       id = ansi_opname (BIT_AND_EXPR);
13876       break;
13877
13878     case CPP_OR:
13879       id = ansi_opname (BIT_IOR_EXPR);
13880       break;
13881
13882     case CPP_COMPL:
13883       id = ansi_opname (BIT_NOT_EXPR);
13884       break;
13885
13886     case CPP_NOT:
13887       id = ansi_opname (TRUTH_NOT_EXPR);
13888       break;
13889
13890     case CPP_EQ:
13891       id = ansi_assopname (NOP_EXPR);
13892       break;
13893
13894     case CPP_LESS:
13895       id = ansi_opname (LT_EXPR);
13896       break;
13897
13898     case CPP_GREATER:
13899       id = ansi_opname (GT_EXPR);
13900       break;
13901
13902     case CPP_PLUS_EQ:
13903       id = ansi_assopname (PLUS_EXPR);
13904       break;
13905
13906     case CPP_MINUS_EQ:
13907       id = ansi_assopname (MINUS_EXPR);
13908       break;
13909
13910     case CPP_MULT_EQ:
13911       id = ansi_assopname (MULT_EXPR);
13912       break;
13913
13914     case CPP_DIV_EQ:
13915       id = ansi_assopname (TRUNC_DIV_EXPR);
13916       break;
13917
13918     case CPP_MOD_EQ:
13919       id = ansi_assopname (TRUNC_MOD_EXPR);
13920       break;
13921
13922     case CPP_XOR_EQ:
13923       id = ansi_assopname (BIT_XOR_EXPR);
13924       break;
13925
13926     case CPP_AND_EQ:
13927       id = ansi_assopname (BIT_AND_EXPR);
13928       break;
13929
13930     case CPP_OR_EQ:
13931       id = ansi_assopname (BIT_IOR_EXPR);
13932       break;
13933
13934     case CPP_LSHIFT:
13935       id = ansi_opname (LSHIFT_EXPR);
13936       break;
13937
13938     case CPP_RSHIFT:
13939       id = ansi_opname (RSHIFT_EXPR);
13940       break;
13941
13942     case CPP_LSHIFT_EQ:
13943       id = ansi_assopname (LSHIFT_EXPR);
13944       break;
13945
13946     case CPP_RSHIFT_EQ:
13947       id = ansi_assopname (RSHIFT_EXPR);
13948       break;
13949
13950     case CPP_EQ_EQ:
13951       id = ansi_opname (EQ_EXPR);
13952       break;
13953
13954     case CPP_NOT_EQ:
13955       id = ansi_opname (NE_EXPR);
13956       break;
13957
13958     case CPP_LESS_EQ:
13959       id = ansi_opname (LE_EXPR);
13960       break;
13961
13962     case CPP_GREATER_EQ:
13963       id = ansi_opname (GE_EXPR);
13964       break;
13965
13966     case CPP_AND_AND:
13967       id = ansi_opname (TRUTH_ANDIF_EXPR);
13968       break;
13969
13970     case CPP_OR_OR:
13971       id = ansi_opname (TRUTH_ORIF_EXPR);
13972       break;
13973
13974     case CPP_PLUS_PLUS:
13975       id = ansi_opname (POSTINCREMENT_EXPR);
13976       break;
13977
13978     case CPP_MINUS_MINUS:
13979       id = ansi_opname (PREDECREMENT_EXPR);
13980       break;
13981
13982     case CPP_COMMA:
13983       id = ansi_opname (COMPOUND_EXPR);
13984       break;
13985
13986     case CPP_DEREF_STAR:
13987       id = ansi_opname (MEMBER_REF);
13988       break;
13989
13990     case CPP_DEREF:
13991       id = ansi_opname (COMPONENT_REF);
13992       break;
13993
13994     case CPP_OPEN_PAREN:
13995       /* Consume the `('.  */
13996       cp_lexer_consume_token (parser->lexer);
13997       /* Look for the matching `)'.  */
13998       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13999       return ansi_opname (CALL_EXPR);
14000
14001     case CPP_OPEN_SQUARE:
14002       /* Consume the `['.  */
14003       cp_lexer_consume_token (parser->lexer);
14004       /* Look for the matching `]'.  */
14005       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14006       return ansi_opname (ARRAY_REF);
14007
14008     case CPP_UTF8STRING:
14009     case CPP_UTF8STRING_USERDEF:
14010       utf8 = true;
14011     case CPP_STRING:
14012     case CPP_WSTRING:
14013     case CPP_STRING16:
14014     case CPP_STRING32:
14015     case CPP_STRING_USERDEF:
14016     case CPP_WSTRING_USERDEF:
14017     case CPP_STRING16_USERDEF:
14018     case CPP_STRING32_USERDEF:
14019       {
14020         tree str, string_tree;
14021         int sz, len;
14022
14023         if (cxx_dialect == cxx98)
14024           maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14025
14026         /* Consume the string.  */
14027         str = cp_parser_string_literal (parser, /*translate=*/true,
14028                                       /*wide_ok=*/true, /*lookup_udlit=*/false);
14029         if (str == error_mark_node)
14030           return error_mark_node;
14031         else if (TREE_CODE (str) == USERDEF_LITERAL)
14032           {
14033             string_tree = USERDEF_LITERAL_VALUE (str);
14034             id = USERDEF_LITERAL_SUFFIX_ID (str);
14035           }
14036         else
14037           {
14038             string_tree = str;
14039             /* Look for the suffix identifier.  */
14040             token = cp_lexer_peek_token (parser->lexer);
14041             if (token->type == CPP_NAME)
14042               id = cp_parser_identifier (parser);
14043             else if (token->type == CPP_KEYWORD)
14044               {
14045                 error ("unexpected keyword;"
14046                        " remove space between quotes and suffix identifier");
14047                 return error_mark_node;
14048               }
14049             else
14050               {
14051                 error ("expected suffix identifier");
14052                 return error_mark_node;
14053               }
14054           }
14055         sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14056                                (TREE_TYPE (TREE_TYPE (string_tree))));
14057         len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14058         if (len != 0)
14059           {
14060             error ("expected empty string after %<operator%> keyword");
14061             return error_mark_node;
14062           }
14063         if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14064             != char_type_node)
14065           {
14066             error ("invalid encoding prefix in literal operator");
14067             return error_mark_node;
14068           }
14069         if (id != error_mark_node)
14070           {
14071             const char *name = IDENTIFIER_POINTER (id);
14072             id = cp_literal_operator_id (name);
14073           }
14074         return id;
14075       }
14076
14077     default:
14078       /* Anything else is an error.  */
14079       break;
14080     }
14081
14082   /* If we have selected an identifier, we need to consume the
14083      operator token.  */
14084   if (id)
14085     cp_lexer_consume_token (parser->lexer);
14086   /* Otherwise, no valid operator name was present.  */
14087   else
14088     {
14089       cp_parser_error (parser, "expected operator");
14090       id = error_mark_node;
14091     }
14092
14093   return cp_expr (id, start_loc);
14094 }
14095
14096 /* Parse a template-declaration.
14097
14098    template-declaration:
14099      export [opt] template < template-parameter-list > declaration
14100
14101    If MEMBER_P is TRUE, this template-declaration occurs within a
14102    class-specifier.
14103
14104    The grammar rule given by the standard isn't correct.  What
14105    is really meant is:
14106
14107    template-declaration:
14108      export [opt] template-parameter-list-seq
14109        decl-specifier-seq [opt] init-declarator [opt] ;
14110      export [opt] template-parameter-list-seq
14111        function-definition
14112
14113    template-parameter-list-seq:
14114      template-parameter-list-seq [opt]
14115      template < template-parameter-list >
14116
14117    Concept Extensions:
14118
14119    template-parameter-list-seq:
14120      template < template-parameter-list > requires-clause [opt]
14121
14122    requires-clause:
14123      requires logical-or-expression  */
14124
14125 static void
14126 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14127 {
14128   /* Check for `export'.  */
14129   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14130     {
14131       /* Consume the `export' token.  */
14132       cp_lexer_consume_token (parser->lexer);
14133       /* Warn that we do not support `export'.  */
14134       warning (0, "keyword %<export%> not implemented, and will be ignored");
14135     }
14136
14137   cp_parser_template_declaration_after_export (parser, member_p);
14138 }
14139
14140 /* Parse a template-parameter-list.
14141
14142    template-parameter-list:
14143      template-parameter
14144      template-parameter-list , template-parameter
14145
14146    Returns a TREE_LIST.  Each node represents a template parameter.
14147    The nodes are connected via their TREE_CHAINs.  */
14148
14149 static tree
14150 cp_parser_template_parameter_list (cp_parser* parser)
14151 {
14152   tree parameter_list = NULL_TREE;
14153
14154   begin_template_parm_list ();
14155
14156   /* The loop below parses the template parms.  We first need to know
14157      the total number of template parms to be able to compute proper
14158      canonical types of each dependent type. So after the loop, when
14159      we know the total number of template parms,
14160      end_template_parm_list computes the proper canonical types and
14161      fixes up the dependent types accordingly.  */
14162   while (true)
14163     {
14164       tree parameter;
14165       bool is_non_type;
14166       bool is_parameter_pack;
14167       location_t parm_loc;
14168
14169       /* Parse the template-parameter.  */
14170       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14171       parameter = cp_parser_template_parameter (parser, 
14172                                                 &is_non_type,
14173                                                 &is_parameter_pack);
14174       /* Add it to the list.  */
14175       if (parameter != error_mark_node)
14176         parameter_list = process_template_parm (parameter_list,
14177                                                 parm_loc,
14178                                                 parameter,
14179                                                 is_non_type,
14180                                                 is_parameter_pack);
14181       else
14182        {
14183          tree err_parm = build_tree_list (parameter, parameter);
14184          parameter_list = chainon (parameter_list, err_parm);
14185        }
14186
14187       /* If the next token is not a `,', we're done.  */
14188       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14189         break;
14190       /* Otherwise, consume the `,' token.  */
14191       cp_lexer_consume_token (parser->lexer);
14192     }
14193
14194   return end_template_parm_list (parameter_list);
14195 }
14196
14197 /* Parse a introduction-list.
14198
14199    introduction-list:
14200      introduced-parameter
14201      introduction-list , introduced-parameter
14202
14203    introduced-parameter:
14204      ...[opt] identifier
14205
14206    Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
14207    then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
14208    WILDCARD_DECL will also have DECL_NAME set and token location in
14209    DECL_SOURCE_LOCATION.  */
14210
14211 static tree
14212 cp_parser_introduction_list (cp_parser *parser)
14213 {
14214   vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14215
14216   while (true)
14217     {
14218       bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14219       if (is_pack)
14220         cp_lexer_consume_token (parser->lexer);
14221
14222       /* Build placeholder. */
14223       tree parm = build_nt (WILDCARD_DECL);
14224       DECL_SOURCE_LOCATION (parm)
14225         = cp_lexer_peek_token (parser->lexer)->location;
14226       DECL_NAME (parm) = cp_parser_identifier (parser);
14227       WILDCARD_PACK_P (parm) = is_pack;
14228       vec_safe_push (introduction_vec, parm);
14229
14230       /* If the next token is not a `,', we're done.  */
14231       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14232         break;
14233       /* Otherwise, consume the `,' token.  */
14234       cp_lexer_consume_token (parser->lexer);
14235     }
14236
14237   /* Convert the vec into a TREE_VEC.  */
14238   tree introduction_list = make_tree_vec (introduction_vec->length ());
14239   unsigned int n;
14240   tree parm;
14241   FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14242     TREE_VEC_ELT (introduction_list, n) = parm;
14243
14244   release_tree_vector (introduction_vec);
14245   return introduction_list;
14246 }
14247
14248 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14249    is an abstract declarator. */
14250
14251 static inline cp_declarator*
14252 get_id_declarator (cp_declarator *declarator)
14253 {
14254   cp_declarator *d = declarator;
14255   while (d && d->kind != cdk_id)
14256     d = d->declarator;
14257   return d;
14258 }
14259
14260 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14261    is an abstract declarator. */
14262
14263 static inline tree
14264 get_unqualified_id (cp_declarator *declarator)
14265 {
14266   declarator = get_id_declarator (declarator);
14267   if (declarator)
14268     return declarator->u.id.unqualified_name;
14269   else
14270     return NULL_TREE;
14271 }
14272
14273 /* Returns true if DECL represents a constrained-parameter.  */
14274
14275 static inline bool
14276 is_constrained_parameter (tree decl)
14277 {
14278   return (decl
14279           && TREE_CODE (decl) == TYPE_DECL
14280           && CONSTRAINED_PARM_CONCEPT (decl)
14281           && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14282 }
14283
14284 /* Returns true if PARM declares a constrained-parameter. */
14285
14286 static inline bool
14287 is_constrained_parameter (cp_parameter_declarator *parm)
14288 {
14289   return is_constrained_parameter (parm->decl_specifiers.type);
14290 }
14291
14292 /* Check that the type parameter is only a declarator-id, and that its
14293    type is not cv-qualified. */
14294
14295 bool
14296 cp_parser_check_constrained_type_parm (cp_parser *parser,
14297                                        cp_parameter_declarator *parm)
14298 {
14299   if (!parm->declarator)
14300     return true;
14301
14302   if (parm->declarator->kind != cdk_id)
14303     {
14304       cp_parser_error (parser, "invalid constrained type parameter");
14305       return false;
14306     }
14307
14308   /* Don't allow cv-qualified type parameters.  */
14309   if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14310       || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14311     {
14312       cp_parser_error (parser, "cv-qualified type parameter");
14313       return false;
14314     }
14315
14316   return true;
14317 }
14318
14319 /* Finish parsing/processing a template type parameter and checking
14320    various restrictions. */
14321
14322 static inline tree
14323 cp_parser_constrained_type_template_parm (cp_parser *parser,
14324                                           tree id,
14325                                           cp_parameter_declarator* parmdecl)
14326 {
14327   if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14328     return finish_template_type_parm (class_type_node, id);
14329   else
14330     return error_mark_node;
14331 }
14332
14333 static tree
14334 finish_constrained_template_template_parm (tree proto, tree id)
14335 {
14336   /* FIXME: This should probably be copied, and we may need to adjust
14337      the template parameter depths.  */
14338   tree saved_parms = current_template_parms;
14339   begin_template_parm_list ();
14340   current_template_parms = DECL_TEMPLATE_PARMS (proto);
14341   end_template_parm_list ();
14342
14343   tree parm = finish_template_template_parm (class_type_node, id);
14344   current_template_parms = saved_parms;
14345
14346   return parm;
14347 }
14348
14349 /* Finish parsing/processing a template template parameter by borrowing
14350    the template parameter list from the prototype parameter.  */
14351
14352 static tree
14353 cp_parser_constrained_template_template_parm (cp_parser *parser,
14354                                               tree proto,
14355                                               tree id,
14356                                               cp_parameter_declarator *parmdecl)
14357 {
14358   if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14359     return error_mark_node;
14360   return finish_constrained_template_template_parm (proto, id);
14361 }
14362
14363 /* Create a new non-type template parameter from the given PARM
14364    declarator.  */
14365
14366 static tree
14367 constrained_non_type_template_parm (bool *is_non_type,
14368                                     cp_parameter_declarator *parm)
14369 {
14370   *is_non_type = true;
14371   cp_declarator *decl = parm->declarator;
14372   cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14373   specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14374   return grokdeclarator (decl, specs, TPARM, 0, NULL);
14375 }
14376
14377 /* Build a constrained template parameter based on the PARMDECL
14378    declarator. The type of PARMDECL is the constrained type, which
14379    refers to the prototype template parameter that ultimately
14380    specifies the type of the declared parameter. */
14381
14382 static tree
14383 finish_constrained_parameter (cp_parser *parser,
14384                               cp_parameter_declarator *parmdecl,
14385                               bool *is_non_type,
14386                               bool *is_parameter_pack)
14387 {
14388   tree decl = parmdecl->decl_specifiers.type;
14389   tree id = get_unqualified_id (parmdecl->declarator);
14390   tree def = parmdecl->default_argument;
14391   tree proto = DECL_INITIAL (decl);
14392
14393   /* A template parameter constrained by a variadic concept shall also
14394      be declared as a template parameter pack.  */
14395   bool is_variadic = template_parameter_pack_p (proto);
14396   if (is_variadic && !*is_parameter_pack)
14397     cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14398
14399   /* Build the parameter. Return an error if the declarator was invalid. */
14400   tree parm;
14401   if (TREE_CODE (proto) == TYPE_DECL)
14402     parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14403   else if (TREE_CODE (proto) == TEMPLATE_DECL)
14404     parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14405                                                          parmdecl);
14406   else
14407     parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14408   if (parm == error_mark_node)
14409     return error_mark_node;
14410
14411   /* Finish the parameter decl and create a node attaching the
14412      default argument and constraint.  */
14413   parm = build_tree_list (def, parm);
14414   TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14415
14416   return parm;
14417 }
14418
14419 /* Returns true if the parsed type actually represents the declaration
14420    of a type template-parameter.  */
14421
14422 static inline bool
14423 declares_constrained_type_template_parameter (tree type)
14424 {
14425   return (is_constrained_parameter (type)
14426           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14427 }
14428
14429
14430 /* Returns true if the parsed type actually represents the declaration of
14431    a template template-parameter.  */
14432
14433 static bool
14434 declares_constrained_template_template_parameter (tree type)
14435 {
14436   return (is_constrained_parameter (type)
14437           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14438 }
14439
14440 /* Parse a default argument for a type template-parameter.
14441    Note that diagnostics are handled in cp_parser_template_parameter.  */
14442
14443 static tree
14444 cp_parser_default_type_template_argument (cp_parser *parser)
14445 {
14446   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14447
14448   /* Consume the `=' token.  */
14449   cp_lexer_consume_token (parser->lexer);
14450
14451   cp_token *token = cp_lexer_peek_token (parser->lexer);
14452
14453   /* Parse the default-argument.  */
14454   push_deferring_access_checks (dk_no_deferred);
14455   tree default_argument = cp_parser_type_id (parser);
14456   pop_deferring_access_checks ();
14457
14458   if (flag_concepts && type_uses_auto (default_argument))
14459     {
14460       error_at (token->location,
14461                 "invalid use of %<auto%> in default template argument");
14462       return error_mark_node;
14463     }
14464
14465   return default_argument;
14466 }
14467
14468 /* Parse a default argument for a template template-parameter.  */
14469
14470 static tree
14471 cp_parser_default_template_template_argument (cp_parser *parser)
14472 {
14473   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14474
14475   bool is_template;
14476
14477   /* Consume the `='.  */
14478   cp_lexer_consume_token (parser->lexer);
14479   /* Parse the id-expression.  */
14480   push_deferring_access_checks (dk_no_deferred);
14481   /* save token before parsing the id-expression, for error
14482      reporting */
14483   const cp_token* token = cp_lexer_peek_token (parser->lexer);
14484   tree default_argument
14485     = cp_parser_id_expression (parser,
14486                                /*template_keyword_p=*/false,
14487                                /*check_dependency_p=*/true,
14488                                /*template_p=*/&is_template,
14489                                /*declarator_p=*/false,
14490                                /*optional_p=*/false);
14491   if (TREE_CODE (default_argument) == TYPE_DECL)
14492     /* If the id-expression was a template-id that refers to
14493        a template-class, we already have the declaration here,
14494        so no further lookup is needed.  */
14495     ;
14496   else
14497     /* Look up the name.  */
14498     default_argument
14499       = cp_parser_lookup_name (parser, default_argument,
14500                                none_type,
14501                                /*is_template=*/is_template,
14502                                /*is_namespace=*/false,
14503                                /*check_dependency=*/true,
14504                                /*ambiguous_decls=*/NULL,
14505                                token->location);
14506   /* See if the default argument is valid.  */
14507   default_argument = check_template_template_default_arg (default_argument);
14508   pop_deferring_access_checks ();
14509   return default_argument;
14510 }
14511
14512 /* Parse a template-parameter.
14513
14514    template-parameter:
14515      type-parameter
14516      parameter-declaration
14517
14518    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
14519    the parameter.  The TREE_PURPOSE is the default value, if any.
14520    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
14521    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
14522    set to true iff this parameter is a parameter pack. */
14523
14524 static tree
14525 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14526                               bool *is_parameter_pack)
14527 {
14528   cp_token *token;
14529   cp_parameter_declarator *parameter_declarator;
14530   tree parm;
14531
14532   /* Assume it is a type parameter or a template parameter.  */
14533   *is_non_type = false;
14534   /* Assume it not a parameter pack. */
14535   *is_parameter_pack = false;
14536   /* Peek at the next token.  */
14537   token = cp_lexer_peek_token (parser->lexer);
14538   /* If it is `class' or `template', we have a type-parameter.  */
14539   if (token->keyword == RID_TEMPLATE)
14540     return cp_parser_type_parameter (parser, is_parameter_pack);
14541   /* If it is `class' or `typename' we do not know yet whether it is a
14542      type parameter or a non-type parameter.  Consider:
14543
14544        template <typename T, typename T::X X> ...
14545
14546      or:
14547
14548        template <class C, class D*> ...
14549
14550      Here, the first parameter is a type parameter, and the second is
14551      a non-type parameter.  We can tell by looking at the token after
14552      the identifier -- if it is a `,', `=', or `>' then we have a type
14553      parameter.  */
14554   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14555     {
14556       /* Peek at the token after `class' or `typename'.  */
14557       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14558       /* If it's an ellipsis, we have a template type parameter
14559          pack. */
14560       if (token->type == CPP_ELLIPSIS)
14561         return cp_parser_type_parameter (parser, is_parameter_pack);
14562       /* If it's an identifier, skip it.  */
14563       if (token->type == CPP_NAME)
14564         token = cp_lexer_peek_nth_token (parser->lexer, 3);
14565       /* Now, see if the token looks like the end of a template
14566          parameter.  */
14567       if (token->type == CPP_COMMA
14568           || token->type == CPP_EQ
14569           || token->type == CPP_GREATER)
14570         return cp_parser_type_parameter (parser, is_parameter_pack);
14571     }
14572
14573   /* Otherwise, it is a non-type parameter or a constrained parameter.
14574
14575      [temp.param]
14576
14577      When parsing a default template-argument for a non-type
14578      template-parameter, the first non-nested `>' is taken as the end
14579      of the template parameter-list rather than a greater-than
14580      operator.  */
14581   parameter_declarator
14582      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14583                                         /*parenthesized_p=*/NULL);
14584
14585   if (!parameter_declarator)
14586     return error_mark_node;
14587
14588   /* If the parameter declaration is marked as a parameter pack, set
14589    *IS_PARAMETER_PACK to notify the caller.  */
14590   if (parameter_declarator->template_parameter_pack_p)
14591     *is_parameter_pack = true;
14592
14593   if (parameter_declarator->default_argument)
14594     {
14595       /* Can happen in some cases of erroneous input (c++/34892).  */
14596       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14597         /* Consume the `...' for better error recovery.  */
14598         cp_lexer_consume_token (parser->lexer);
14599     }
14600
14601   // The parameter may have been constrained.
14602   if (is_constrained_parameter (parameter_declarator))
14603     return finish_constrained_parameter (parser,
14604                                          parameter_declarator,
14605                                          is_non_type,
14606                                          is_parameter_pack);
14607
14608   // Now we're sure that the parameter is a non-type parameter.
14609   *is_non_type = true;
14610
14611   parm = grokdeclarator (parameter_declarator->declarator,
14612                          &parameter_declarator->decl_specifiers,
14613                          TPARM, /*initialized=*/0,
14614                          /*attrlist=*/NULL);
14615   if (parm == error_mark_node)
14616     return error_mark_node;
14617
14618   return build_tree_list (parameter_declarator->default_argument, parm);
14619 }
14620
14621 /* Parse a type-parameter.
14622
14623    type-parameter:
14624      class identifier [opt]
14625      class identifier [opt] = type-id
14626      typename identifier [opt]
14627      typename identifier [opt] = type-id
14628      template < template-parameter-list > class identifier [opt]
14629      template < template-parameter-list > class identifier [opt]
14630        = id-expression
14631
14632    GNU Extension (variadic templates):
14633
14634    type-parameter:
14635      class ... identifier [opt]
14636      typename ... identifier [opt]
14637
14638    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
14639    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
14640    the declaration of the parameter.
14641
14642    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14643
14644 static tree
14645 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14646 {
14647   cp_token *token;
14648   tree parameter;
14649
14650   /* Look for a keyword to tell us what kind of parameter this is.  */
14651   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14652   if (!token)
14653     return error_mark_node;
14654
14655   switch (token->keyword)
14656     {
14657     case RID_CLASS:
14658     case RID_TYPENAME:
14659       {
14660         tree identifier;
14661         tree default_argument;
14662
14663         /* If the next token is an ellipsis, we have a template
14664            argument pack. */
14665         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14666           {
14667             /* Consume the `...' token. */
14668             cp_lexer_consume_token (parser->lexer);
14669             maybe_warn_variadic_templates ();
14670
14671             *is_parameter_pack = true;
14672           }
14673
14674         /* If the next token is an identifier, then it names the
14675            parameter.  */
14676         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14677           identifier = cp_parser_identifier (parser);
14678         else
14679           identifier = NULL_TREE;
14680
14681         /* Create the parameter.  */
14682         parameter = finish_template_type_parm (class_type_node, identifier);
14683
14684         /* If the next token is an `=', we have a default argument.  */
14685         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14686           {
14687             default_argument
14688               = cp_parser_default_type_template_argument (parser);
14689
14690             /* Template parameter packs cannot have default
14691                arguments. */
14692             if (*is_parameter_pack)
14693               {
14694                 if (identifier)
14695                   error_at (token->location,
14696                             "template parameter pack %qD cannot have a "
14697                             "default argument", identifier);
14698                 else
14699                   error_at (token->location,
14700                             "template parameter packs cannot have "
14701                             "default arguments");
14702                 default_argument = NULL_TREE;
14703               }
14704             else if (check_for_bare_parameter_packs (default_argument))
14705               default_argument = error_mark_node;
14706           }
14707         else
14708           default_argument = NULL_TREE;
14709
14710         /* Create the combined representation of the parameter and the
14711            default argument.  */
14712         parameter = build_tree_list (default_argument, parameter);
14713       }
14714       break;
14715
14716     case RID_TEMPLATE:
14717       {
14718         tree identifier;
14719         tree default_argument;
14720
14721         /* Look for the `<'.  */
14722         cp_parser_require (parser, CPP_LESS, RT_LESS);
14723         /* Parse the template-parameter-list.  */
14724         cp_parser_template_parameter_list (parser);
14725         /* Look for the `>'.  */
14726         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14727
14728         // If template requirements are present, parse them.
14729         if (flag_concepts)
14730           {
14731             tree reqs = get_shorthand_constraints (current_template_parms);
14732             if (tree r = cp_parser_requires_clause_opt (parser))
14733               reqs = conjoin_constraints (reqs, normalize_expression (r));
14734             TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14735           }
14736
14737         /* Look for the `class' or 'typename' keywords.  */
14738         cp_parser_type_parameter_key (parser);
14739         /* If the next token is an ellipsis, we have a template
14740            argument pack. */
14741         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14742           {
14743             /* Consume the `...' token. */
14744             cp_lexer_consume_token (parser->lexer);
14745             maybe_warn_variadic_templates ();
14746
14747             *is_parameter_pack = true;
14748           }
14749         /* If the next token is an `=', then there is a
14750            default-argument.  If the next token is a `>', we are at
14751            the end of the parameter-list.  If the next token is a `,',
14752            then we are at the end of this parameter.  */
14753         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14754             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14755             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14756           {
14757             identifier = cp_parser_identifier (parser);
14758             /* Treat invalid names as if the parameter were nameless.  */
14759             if (identifier == error_mark_node)
14760               identifier = NULL_TREE;
14761           }
14762         else
14763           identifier = NULL_TREE;
14764
14765         /* Create the template parameter.  */
14766         parameter = finish_template_template_parm (class_type_node,
14767                                                    identifier);
14768
14769         /* If the next token is an `=', then there is a
14770            default-argument.  */
14771         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14772           {
14773             default_argument
14774               = cp_parser_default_template_template_argument (parser);
14775
14776             /* Template parameter packs cannot have default
14777                arguments. */
14778             if (*is_parameter_pack)
14779               {
14780                 if (identifier)
14781                   error_at (token->location,
14782                             "template parameter pack %qD cannot "
14783                             "have a default argument",
14784                             identifier);
14785                 else
14786                   error_at (token->location, "template parameter packs cannot "
14787                             "have default arguments");
14788                 default_argument = NULL_TREE;
14789               }
14790           }
14791         else
14792           default_argument = NULL_TREE;
14793
14794         /* Create the combined representation of the parameter and the
14795            default argument.  */
14796         parameter = build_tree_list (default_argument, parameter);
14797       }
14798       break;
14799
14800     default:
14801       gcc_unreachable ();
14802       break;
14803     }
14804
14805   return parameter;
14806 }
14807
14808 /* Parse a template-id.
14809
14810    template-id:
14811      template-name < template-argument-list [opt] >
14812
14813    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14814    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
14815    returned.  Otherwise, if the template-name names a function, or set
14816    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
14817    names a class, returns a TYPE_DECL for the specialization.
14818
14819    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14820    uninstantiated templates.  */
14821
14822 static tree
14823 cp_parser_template_id (cp_parser *parser,
14824                        bool template_keyword_p,
14825                        bool check_dependency_p,
14826                        enum tag_types tag_type,
14827                        bool is_declaration)
14828 {
14829   tree templ;
14830   tree arguments;
14831   tree template_id;
14832   cp_token_position start_of_id = 0;
14833   cp_token *next_token = NULL, *next_token_2 = NULL;
14834   bool is_identifier;
14835
14836   /* If the next token corresponds to a template-id, there is no need
14837      to reparse it.  */
14838   next_token = cp_lexer_peek_token (parser->lexer);
14839   if (next_token->type == CPP_TEMPLATE_ID)
14840     {
14841       cp_lexer_consume_token (parser->lexer);
14842       return saved_checks_value (next_token->u.tree_check_value);
14843     }
14844
14845   /* Avoid performing name lookup if there is no possibility of
14846      finding a template-id.  */
14847   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14848       || (next_token->type == CPP_NAME
14849           && !cp_parser_nth_token_starts_template_argument_list_p
14850                (parser, 2)))
14851     {
14852       cp_parser_error (parser, "expected template-id");
14853       return error_mark_node;
14854     }
14855
14856   /* Remember where the template-id starts.  */
14857   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14858     start_of_id = cp_lexer_token_position (parser->lexer, false);
14859
14860   push_deferring_access_checks (dk_deferred);
14861
14862   /* Parse the template-name.  */
14863   is_identifier = false;
14864   templ = cp_parser_template_name (parser, template_keyword_p,
14865                                    check_dependency_p,
14866                                    is_declaration,
14867                                    tag_type,
14868                                    &is_identifier);
14869   if (templ == error_mark_node || is_identifier)
14870     {
14871       pop_deferring_access_checks ();
14872       return templ;
14873     }
14874
14875   /* Since we're going to preserve any side-effects from this parse, set up a
14876      firewall to protect our callers from cp_parser_commit_to_tentative_parse
14877      in the template arguments.  */
14878   tentative_firewall firewall (parser);
14879
14880   /* If we find the sequence `[:' after a template-name, it's probably
14881      a digraph-typo for `< ::'. Substitute the tokens and check if we can
14882      parse correctly the argument list.  */
14883   next_token = cp_lexer_peek_token (parser->lexer);
14884   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14885   if (next_token->type == CPP_OPEN_SQUARE
14886       && next_token->flags & DIGRAPH
14887       && next_token_2->type == CPP_COLON
14888       && !(next_token_2->flags & PREV_WHITE))
14889     {
14890       cp_parser_parse_tentatively (parser);
14891       /* Change `:' into `::'.  */
14892       next_token_2->type = CPP_SCOPE;
14893       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14894          CPP_LESS.  */
14895       cp_lexer_consume_token (parser->lexer);
14896
14897       /* Parse the arguments.  */
14898       arguments = cp_parser_enclosed_template_argument_list (parser);
14899       if (!cp_parser_parse_definitely (parser))
14900         {
14901           /* If we couldn't parse an argument list, then we revert our changes
14902              and return simply an error. Maybe this is not a template-id
14903              after all.  */
14904           next_token_2->type = CPP_COLON;
14905           cp_parser_error (parser, "expected %<<%>");
14906           pop_deferring_access_checks ();
14907           return error_mark_node;
14908         }
14909       /* Otherwise, emit an error about the invalid digraph, but continue
14910          parsing because we got our argument list.  */
14911       if (permerror (next_token->location,
14912                      "%<<::%> cannot begin a template-argument list"))
14913         {
14914           static bool hint = false;
14915           inform (next_token->location,
14916                   "%<<:%> is an alternate spelling for %<[%>."
14917                   " Insert whitespace between %<<%> and %<::%>");
14918           if (!hint && !flag_permissive)
14919             {
14920               inform (next_token->location, "(if you use %<-fpermissive%> "
14921                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14922                       "accept your code)");
14923               hint = true;
14924             }
14925         }
14926     }
14927   else
14928     {
14929       /* Look for the `<' that starts the template-argument-list.  */
14930       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14931         {
14932           pop_deferring_access_checks ();
14933           return error_mark_node;
14934         }
14935       /* Parse the arguments.  */
14936       arguments = cp_parser_enclosed_template_argument_list (parser);
14937     }
14938
14939   /* Build a representation of the specialization.  */
14940   if (identifier_p (templ))
14941     template_id = build_min_nt_loc (next_token->location,
14942                                     TEMPLATE_ID_EXPR,
14943                                     templ, arguments);
14944   else if (DECL_TYPE_TEMPLATE_P (templ)
14945            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14946     {
14947       bool entering_scope;
14948       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14949          template (rather than some instantiation thereof) only if
14950          is not nested within some other construct.  For example, in
14951          "template <typename T> void f(T) { A<T>::", A<T> is just an
14952          instantiation of A.  */
14953       entering_scope = (template_parm_scope_p ()
14954                         && cp_lexer_next_token_is (parser->lexer,
14955                                                    CPP_SCOPE));
14956       template_id
14957         = finish_template_type (templ, arguments, entering_scope);
14958     }
14959   /* A template-like identifier may be a partial concept id. */
14960   else if (flag_concepts
14961            && (template_id = (cp_parser_maybe_partial_concept_id
14962                               (parser, templ, arguments))))
14963     return template_id;
14964   else if (variable_template_p (templ))
14965     {
14966       template_id = lookup_template_variable (templ, arguments);
14967       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14968         SET_EXPR_LOCATION (template_id, next_token->location);
14969     }
14970   else
14971     {
14972       /* If it's not a class-template or a template-template, it should be
14973          a function-template.  */
14974       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14975                    || TREE_CODE (templ) == OVERLOAD
14976                    || BASELINK_P (templ)));
14977
14978       template_id = lookup_template_function (templ, arguments);
14979       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14980         SET_EXPR_LOCATION (template_id, next_token->location);
14981     }
14982
14983   /* If parsing tentatively, replace the sequence of tokens that makes
14984      up the template-id with a CPP_TEMPLATE_ID token.  That way,
14985      should we re-parse the token stream, we will not have to repeat
14986      the effort required to do the parse, nor will we issue duplicate
14987      error messages about problems during instantiation of the
14988      template.  */
14989   if (start_of_id
14990       /* Don't do this if we had a parse error in a declarator; re-parsing
14991          might succeed if a name changes meaning (60361).  */
14992       && !(cp_parser_error_occurred (parser)
14993            && cp_parser_parsing_tentatively (parser)
14994            && parser->in_declarator_p))
14995     {
14996       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14997
14998       /* Reset the contents of the START_OF_ID token.  */
14999       token->type = CPP_TEMPLATE_ID;
15000
15001       /* Update the location to be of the form:
15002            template-name < template-argument-list [opt] >
15003            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15004          with caret == start at the start of the template-name,
15005          ranging until the closing '>'.  */
15006       location_t finish_loc
15007         = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15008       location_t combined_loc
15009         = make_location (token->location, token->location, finish_loc);
15010       token->location = combined_loc;
15011
15012       /* Retrieve any deferred checks.  Do not pop this access checks yet
15013          so the memory will not be reclaimed during token replacing below.  */
15014       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15015       token->u.tree_check_value->value = template_id;
15016       token->u.tree_check_value->checks = get_deferred_access_checks ();
15017       token->keyword = RID_MAX;
15018
15019       /* Purge all subsequent tokens.  */
15020       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15021
15022       /* ??? Can we actually assume that, if template_id ==
15023          error_mark_node, we will have issued a diagnostic to the
15024          user, as opposed to simply marking the tentative parse as
15025          failed?  */
15026       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15027         error_at (token->location, "parse error in template argument list");
15028     }
15029
15030   pop_to_parent_deferring_access_checks ();
15031   return template_id;
15032 }
15033
15034 /* Parse a template-name.
15035
15036    template-name:
15037      identifier
15038
15039    The standard should actually say:
15040
15041    template-name:
15042      identifier
15043      operator-function-id
15044
15045    A defect report has been filed about this issue.
15046
15047    A conversion-function-id cannot be a template name because they cannot
15048    be part of a template-id. In fact, looking at this code:
15049
15050    a.operator K<int>()
15051
15052    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15053    It is impossible to call a templated conversion-function-id with an
15054    explicit argument list, since the only allowed template parameter is
15055    the type to which it is converting.
15056
15057    If TEMPLATE_KEYWORD_P is true, then we have just seen the
15058    `template' keyword, in a construction like:
15059
15060      T::template f<3>()
15061
15062    In that case `f' is taken to be a template-name, even though there
15063    is no way of knowing for sure.
15064
15065    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15066    name refers to a set of overloaded functions, at least one of which
15067    is a template, or an IDENTIFIER_NODE with the name of the template,
15068    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
15069    names are looked up inside uninstantiated templates.  */
15070
15071 static tree
15072 cp_parser_template_name (cp_parser* parser,
15073                          bool template_keyword_p,
15074                          bool check_dependency_p,
15075                          bool is_declaration,
15076                          enum tag_types tag_type,
15077                          bool *is_identifier)
15078 {
15079   tree identifier;
15080   tree decl;
15081   tree fns;
15082   cp_token *token = cp_lexer_peek_token (parser->lexer);
15083
15084   /* If the next token is `operator', then we have either an
15085      operator-function-id or a conversion-function-id.  */
15086   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15087     {
15088       /* We don't know whether we're looking at an
15089          operator-function-id or a conversion-function-id.  */
15090       cp_parser_parse_tentatively (parser);
15091       /* Try an operator-function-id.  */
15092       identifier = cp_parser_operator_function_id (parser);
15093       /* If that didn't work, try a conversion-function-id.  */
15094       if (!cp_parser_parse_definitely (parser))
15095         {
15096           cp_parser_error (parser, "expected template-name");
15097           return error_mark_node;
15098         }
15099     }
15100   /* Look for the identifier.  */
15101   else
15102     identifier = cp_parser_identifier (parser);
15103
15104   /* If we didn't find an identifier, we don't have a template-id.  */
15105   if (identifier == error_mark_node)
15106     return error_mark_node;
15107
15108   /* If the name immediately followed the `template' keyword, then it
15109      is a template-name.  However, if the next token is not `<', then
15110      we do not treat it as a template-name, since it is not being used
15111      as part of a template-id.  This enables us to handle constructs
15112      like:
15113
15114        template <typename T> struct S { S(); };
15115        template <typename T> S<T>::S();
15116
15117      correctly.  We would treat `S' as a template -- if it were `S<T>'
15118      -- but we do not if there is no `<'.  */
15119
15120   if (processing_template_decl
15121       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15122     {
15123       /* In a declaration, in a dependent context, we pretend that the
15124          "template" keyword was present in order to improve error
15125          recovery.  For example, given:
15126
15127            template <typename T> void f(T::X<int>);
15128
15129          we want to treat "X<int>" as a template-id.  */
15130       if (is_declaration
15131           && !template_keyword_p
15132           && parser->scope && TYPE_P (parser->scope)
15133           && check_dependency_p
15134           && dependent_scope_p (parser->scope)
15135           /* Do not do this for dtors (or ctors), since they never
15136              need the template keyword before their name.  */
15137           && !constructor_name_p (identifier, parser->scope))
15138         {
15139           cp_token_position start = 0;
15140
15141           /* Explain what went wrong.  */
15142           error_at (token->location, "non-template %qD used as template",
15143                     identifier);
15144           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15145                   parser->scope, identifier);
15146           /* If parsing tentatively, find the location of the "<" token.  */
15147           if (cp_parser_simulate_error (parser))
15148             start = cp_lexer_token_position (parser->lexer, true);
15149           /* Parse the template arguments so that we can issue error
15150              messages about them.  */
15151           cp_lexer_consume_token (parser->lexer);
15152           cp_parser_enclosed_template_argument_list (parser);
15153           /* Skip tokens until we find a good place from which to
15154              continue parsing.  */
15155           cp_parser_skip_to_closing_parenthesis (parser,
15156                                                  /*recovering=*/true,
15157                                                  /*or_comma=*/true,
15158                                                  /*consume_paren=*/false);
15159           /* If parsing tentatively, permanently remove the
15160              template argument list.  That will prevent duplicate
15161              error messages from being issued about the missing
15162              "template" keyword.  */
15163           if (start)
15164             cp_lexer_purge_tokens_after (parser->lexer, start);
15165           if (is_identifier)
15166             *is_identifier = true;
15167           return identifier;
15168         }
15169
15170       /* If the "template" keyword is present, then there is generally
15171          no point in doing name-lookup, so we just return IDENTIFIER.
15172          But, if the qualifying scope is non-dependent then we can
15173          (and must) do name-lookup normally.  */
15174       if (template_keyword_p
15175           && (!parser->scope
15176               || (TYPE_P (parser->scope)
15177                   && dependent_type_p (parser->scope))))
15178         return identifier;
15179     }
15180
15181   /* Look up the name.  */
15182   decl = cp_parser_lookup_name (parser, identifier,
15183                                 tag_type,
15184                                 /*is_template=*/true,
15185                                 /*is_namespace=*/false,
15186                                 check_dependency_p,
15187                                 /*ambiguous_decls=*/NULL,
15188                                 token->location);
15189
15190   decl = strip_using_decl (decl);
15191
15192   /* If DECL is a template, then the name was a template-name.  */
15193   if (TREE_CODE (decl) == TEMPLATE_DECL)
15194     {
15195       if (TREE_DEPRECATED (decl)
15196           && deprecated_state != DEPRECATED_SUPPRESS)
15197         warn_deprecated_use (decl, NULL_TREE);
15198     }
15199   else
15200     {
15201       tree fn = NULL_TREE;
15202
15203       /* The standard does not explicitly indicate whether a name that
15204          names a set of overloaded declarations, some of which are
15205          templates, is a template-name.  However, such a name should
15206          be a template-name; otherwise, there is no way to form a
15207          template-id for the overloaded templates.  */
15208       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15209       if (TREE_CODE (fns) == OVERLOAD)
15210         for (fn = fns; fn; fn = OVL_NEXT (fn))
15211           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15212             break;
15213
15214       if (!fn)
15215         {
15216           /* The name does not name a template.  */
15217           cp_parser_error (parser, "expected template-name");
15218           return error_mark_node;
15219         }
15220     }
15221
15222   /* If DECL is dependent, and refers to a function, then just return
15223      its name; we will look it up again during template instantiation.  */
15224   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15225     {
15226       tree scope = ovl_scope (decl);
15227       if (TYPE_P (scope) && dependent_type_p (scope))
15228         return identifier;
15229     }
15230
15231   return decl;
15232 }
15233
15234 /* Parse a template-argument-list.
15235
15236    template-argument-list:
15237      template-argument ... [opt]
15238      template-argument-list , template-argument ... [opt]
15239
15240    Returns a TREE_VEC containing the arguments.  */
15241
15242 static tree
15243 cp_parser_template_argument_list (cp_parser* parser)
15244 {
15245   tree fixed_args[10];
15246   unsigned n_args = 0;
15247   unsigned alloced = 10;
15248   tree *arg_ary = fixed_args;
15249   tree vec;
15250   bool saved_in_template_argument_list_p;
15251   bool saved_ice_p;
15252   bool saved_non_ice_p;
15253
15254   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15255   parser->in_template_argument_list_p = true;
15256   /* Even if the template-id appears in an integral
15257      constant-expression, the contents of the argument list do
15258      not.  */
15259   saved_ice_p = parser->integral_constant_expression_p;
15260   parser->integral_constant_expression_p = false;
15261   saved_non_ice_p = parser->non_integral_constant_expression_p;
15262   parser->non_integral_constant_expression_p = false;
15263
15264   /* Parse the arguments.  */
15265   do
15266     {
15267       tree argument;
15268
15269       if (n_args)
15270         /* Consume the comma.  */
15271         cp_lexer_consume_token (parser->lexer);
15272
15273       /* Parse the template-argument.  */
15274       argument = cp_parser_template_argument (parser);
15275
15276       /* If the next token is an ellipsis, we're expanding a template
15277          argument pack. */
15278       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15279         {
15280           if (argument == error_mark_node)
15281             {
15282               cp_token *token = cp_lexer_peek_token (parser->lexer);
15283               error_at (token->location,
15284                         "expected parameter pack before %<...%>");
15285             }
15286           /* Consume the `...' token. */
15287           cp_lexer_consume_token (parser->lexer);
15288
15289           /* Make the argument into a TYPE_PACK_EXPANSION or
15290              EXPR_PACK_EXPANSION. */
15291           argument = make_pack_expansion (argument);
15292         }
15293
15294       if (n_args == alloced)
15295         {
15296           alloced *= 2;
15297
15298           if (arg_ary == fixed_args)
15299             {
15300               arg_ary = XNEWVEC (tree, alloced);
15301               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15302             }
15303           else
15304             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15305         }
15306       arg_ary[n_args++] = argument;
15307     }
15308   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15309
15310   vec = make_tree_vec (n_args);
15311
15312   while (n_args--)
15313     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15314
15315   if (arg_ary != fixed_args)
15316     free (arg_ary);
15317   parser->non_integral_constant_expression_p = saved_non_ice_p;
15318   parser->integral_constant_expression_p = saved_ice_p;
15319   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15320   if (CHECKING_P)
15321     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15322   return vec;
15323 }
15324
15325 /* Parse a template-argument.
15326
15327    template-argument:
15328      assignment-expression
15329      type-id
15330      id-expression
15331
15332    The representation is that of an assignment-expression, type-id, or
15333    id-expression -- except that the qualified id-expression is
15334    evaluated, so that the value returned is either a DECL or an
15335    OVERLOAD.
15336
15337    Although the standard says "assignment-expression", it forbids
15338    throw-expressions or assignments in the template argument.
15339    Therefore, we use "conditional-expression" instead.  */
15340
15341 static tree
15342 cp_parser_template_argument (cp_parser* parser)
15343 {
15344   tree argument;
15345   bool template_p;
15346   bool address_p;
15347   bool maybe_type_id = false;
15348   cp_token *token = NULL, *argument_start_token = NULL;
15349   location_t loc = 0;
15350   cp_id_kind idk;
15351
15352   /* There's really no way to know what we're looking at, so we just
15353      try each alternative in order.
15354
15355        [temp.arg]
15356
15357        In a template-argument, an ambiguity between a type-id and an
15358        expression is resolved to a type-id, regardless of the form of
15359        the corresponding template-parameter.
15360
15361      Therefore, we try a type-id first.  */
15362   cp_parser_parse_tentatively (parser);
15363   argument = cp_parser_template_type_arg (parser);
15364   /* If there was no error parsing the type-id but the next token is a
15365      '>>', our behavior depends on which dialect of C++ we're
15366      parsing. In C++98, we probably found a typo for '> >'. But there
15367      are type-id which are also valid expressions. For instance:
15368
15369      struct X { int operator >> (int); };
15370      template <int V> struct Foo {};
15371      Foo<X () >> 5> r;
15372
15373      Here 'X()' is a valid type-id of a function type, but the user just
15374      wanted to write the expression "X() >> 5". Thus, we remember that we
15375      found a valid type-id, but we still try to parse the argument as an
15376      expression to see what happens. 
15377
15378      In C++0x, the '>>' will be considered two separate '>'
15379      tokens.  */
15380   if (!cp_parser_error_occurred (parser)
15381       && cxx_dialect == cxx98
15382       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15383     {
15384       maybe_type_id = true;
15385       cp_parser_abort_tentative_parse (parser);
15386     }
15387   else
15388     {
15389       /* If the next token isn't a `,' or a `>', then this argument wasn't
15390       really finished. This means that the argument is not a valid
15391       type-id.  */
15392       if (!cp_parser_next_token_ends_template_argument_p (parser))
15393         cp_parser_error (parser, "expected template-argument");
15394       /* If that worked, we're done.  */
15395       if (cp_parser_parse_definitely (parser))
15396         return argument;
15397     }
15398   /* We're still not sure what the argument will be.  */
15399   cp_parser_parse_tentatively (parser);
15400   /* Try a template.  */
15401   argument_start_token = cp_lexer_peek_token (parser->lexer);
15402   argument = cp_parser_id_expression (parser,
15403                                       /*template_keyword_p=*/false,
15404                                       /*check_dependency_p=*/true,
15405                                       &template_p,
15406                                       /*declarator_p=*/false,
15407                                       /*optional_p=*/false);
15408   /* If the next token isn't a `,' or a `>', then this argument wasn't
15409      really finished.  */
15410   if (!cp_parser_next_token_ends_template_argument_p (parser))
15411     cp_parser_error (parser, "expected template-argument");
15412   if (!cp_parser_error_occurred (parser))
15413     {
15414       /* Figure out what is being referred to.  If the id-expression
15415          was for a class template specialization, then we will have a
15416          TYPE_DECL at this point.  There is no need to do name lookup
15417          at this point in that case.  */
15418       if (TREE_CODE (argument) != TYPE_DECL)
15419         argument = cp_parser_lookup_name (parser, argument,
15420                                           none_type,
15421                                           /*is_template=*/template_p,
15422                                           /*is_namespace=*/false,
15423                                           /*check_dependency=*/true,
15424                                           /*ambiguous_decls=*/NULL,
15425                                           argument_start_token->location);
15426       /* Handle a constrained-type-specifier for a non-type template
15427          parameter.  */
15428       if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15429         argument = decl;
15430       else if (TREE_CODE (argument) != TEMPLATE_DECL
15431                && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15432         cp_parser_error (parser, "expected template-name");
15433     }
15434   if (cp_parser_parse_definitely (parser))
15435     {
15436       if (TREE_DEPRECATED (argument))
15437         warn_deprecated_use (argument, NULL_TREE);
15438       return argument;
15439     }
15440   /* It must be a non-type argument.  In C++17 any constant-expression is
15441      allowed.  */
15442   if (cxx_dialect > cxx14)
15443     goto general_expr;
15444
15445   /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15446
15447      -- an integral constant-expression of integral or enumeration
15448         type; or
15449
15450      -- the name of a non-type template-parameter; or
15451
15452      -- the name of an object or function with external linkage...
15453
15454      -- the address of an object or function with external linkage...
15455
15456      -- a pointer to member...  */
15457   /* Look for a non-type template parameter.  */
15458   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15459     {
15460       cp_parser_parse_tentatively (parser);
15461       argument = cp_parser_primary_expression (parser,
15462                                                /*address_p=*/false,
15463                                                /*cast_p=*/false,
15464                                                /*template_arg_p=*/true,
15465                                                &idk);
15466       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15467           || !cp_parser_next_token_ends_template_argument_p (parser))
15468         cp_parser_simulate_error (parser);
15469       if (cp_parser_parse_definitely (parser))
15470         return argument;
15471     }
15472
15473   /* If the next token is "&", the argument must be the address of an
15474      object or function with external linkage.  */
15475   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15476   if (address_p)
15477     {
15478       loc = cp_lexer_peek_token (parser->lexer)->location;
15479       cp_lexer_consume_token (parser->lexer);
15480     }
15481   /* See if we might have an id-expression.  */
15482   token = cp_lexer_peek_token (parser->lexer);
15483   if (token->type == CPP_NAME
15484       || token->keyword == RID_OPERATOR
15485       || token->type == CPP_SCOPE
15486       || token->type == CPP_TEMPLATE_ID
15487       || token->type == CPP_NESTED_NAME_SPECIFIER)
15488     {
15489       cp_parser_parse_tentatively (parser);
15490       argument = cp_parser_primary_expression (parser,
15491                                                address_p,
15492                                                /*cast_p=*/false,
15493                                                /*template_arg_p=*/true,
15494                                                &idk);
15495       if (cp_parser_error_occurred (parser)
15496           || !cp_parser_next_token_ends_template_argument_p (parser))
15497         cp_parser_abort_tentative_parse (parser);
15498       else
15499         {
15500           tree probe;
15501
15502           if (INDIRECT_REF_P (argument))
15503             {
15504               /* Strip the dereference temporarily.  */
15505               gcc_assert (REFERENCE_REF_P (argument));
15506               argument = TREE_OPERAND (argument, 0);
15507             }
15508
15509           /* If we're in a template, we represent a qualified-id referring
15510              to a static data member as a SCOPE_REF even if the scope isn't
15511              dependent so that we can check access control later.  */
15512           probe = argument;
15513           if (TREE_CODE (probe) == SCOPE_REF)
15514             probe = TREE_OPERAND (probe, 1);
15515           if (VAR_P (probe))
15516             {
15517               /* A variable without external linkage might still be a
15518                  valid constant-expression, so no error is issued here
15519                  if the external-linkage check fails.  */
15520               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15521                 cp_parser_simulate_error (parser);
15522             }
15523           else if (is_overloaded_fn (argument))
15524             /* All overloaded functions are allowed; if the external
15525                linkage test does not pass, an error will be issued
15526                later.  */
15527             ;
15528           else if (address_p
15529                    && (TREE_CODE (argument) == OFFSET_REF
15530                        || TREE_CODE (argument) == SCOPE_REF))
15531             /* A pointer-to-member.  */
15532             ;
15533           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15534             ;
15535           else
15536             cp_parser_simulate_error (parser);
15537
15538           if (cp_parser_parse_definitely (parser))
15539             {
15540               if (address_p)
15541                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15542                                              tf_warning_or_error);
15543               else
15544                 argument = convert_from_reference (argument);
15545               return argument;
15546             }
15547         }
15548     }
15549   /* If the argument started with "&", there are no other valid
15550      alternatives at this point.  */
15551   if (address_p)
15552     {
15553       cp_parser_error (parser, "invalid non-type template argument");
15554       return error_mark_node;
15555     }
15556
15557  general_expr:
15558   /* If the argument wasn't successfully parsed as a type-id followed
15559      by '>>', the argument can only be a constant expression now.
15560      Otherwise, we try parsing the constant-expression tentatively,
15561      because the argument could really be a type-id.  */
15562   if (maybe_type_id)
15563     cp_parser_parse_tentatively (parser);
15564
15565   if (cxx_dialect <= cxx14)
15566     argument = cp_parser_constant_expression (parser);
15567   else
15568     {
15569       /* With C++17 generalized non-type template arguments we need to handle
15570          lvalue constant expressions, too.  */
15571       argument = cp_parser_assignment_expression (parser);
15572       require_potential_constant_expression (argument);
15573     }
15574
15575   if (!maybe_type_id)
15576     return argument;
15577   if (!cp_parser_next_token_ends_template_argument_p (parser))
15578     cp_parser_error (parser, "expected template-argument");
15579   if (cp_parser_parse_definitely (parser))
15580     return argument;
15581   /* We did our best to parse the argument as a non type-id, but that
15582      was the only alternative that matched (albeit with a '>' after
15583      it). We can assume it's just a typo from the user, and a
15584      diagnostic will then be issued.  */
15585   return cp_parser_template_type_arg (parser);
15586 }
15587
15588 /* Parse an explicit-instantiation.
15589
15590    explicit-instantiation:
15591      template declaration
15592
15593    Although the standard says `declaration', what it really means is:
15594
15595    explicit-instantiation:
15596      template decl-specifier-seq [opt] declarator [opt] ;
15597
15598    Things like `template int S<int>::i = 5, int S<double>::j;' are not
15599    supposed to be allowed.  A defect report has been filed about this
15600    issue.
15601
15602    GNU Extension:
15603
15604    explicit-instantiation:
15605      storage-class-specifier template
15606        decl-specifier-seq [opt] declarator [opt] ;
15607      function-specifier template
15608        decl-specifier-seq [opt] declarator [opt] ;  */
15609
15610 static void
15611 cp_parser_explicit_instantiation (cp_parser* parser)
15612 {
15613   int declares_class_or_enum;
15614   cp_decl_specifier_seq decl_specifiers;
15615   tree extension_specifier = NULL_TREE;
15616
15617   timevar_push (TV_TEMPLATE_INST);
15618
15619   /* Look for an (optional) storage-class-specifier or
15620      function-specifier.  */
15621   if (cp_parser_allow_gnu_extensions_p (parser))
15622     {
15623       extension_specifier
15624         = cp_parser_storage_class_specifier_opt (parser);
15625       if (!extension_specifier)
15626         extension_specifier
15627           = cp_parser_function_specifier_opt (parser,
15628                                               /*decl_specs=*/NULL);
15629     }
15630
15631   /* Look for the `template' keyword.  */
15632   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15633   /* Let the front end know that we are processing an explicit
15634      instantiation.  */
15635   begin_explicit_instantiation ();
15636   /* [temp.explicit] says that we are supposed to ignore access
15637      control while processing explicit instantiation directives.  */
15638   push_deferring_access_checks (dk_no_check);
15639   /* Parse a decl-specifier-seq.  */
15640   cp_parser_decl_specifier_seq (parser,
15641                                 CP_PARSER_FLAGS_OPTIONAL,
15642                                 &decl_specifiers,
15643                                 &declares_class_or_enum);
15644   /* If there was exactly one decl-specifier, and it declared a class,
15645      and there's no declarator, then we have an explicit type
15646      instantiation.  */
15647   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15648     {
15649       tree type;
15650
15651       type = check_tag_decl (&decl_specifiers,
15652                              /*explicit_type_instantiation_p=*/true);
15653       /* Turn access control back on for names used during
15654          template instantiation.  */
15655       pop_deferring_access_checks ();
15656       if (type)
15657         do_type_instantiation (type, extension_specifier,
15658                                /*complain=*/tf_error);
15659     }
15660   else
15661     {
15662       cp_declarator *declarator;
15663       tree decl;
15664
15665       /* Parse the declarator.  */
15666       declarator
15667         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15668                                 /*ctor_dtor_or_conv_p=*/NULL,
15669                                 /*parenthesized_p=*/NULL,
15670                                 /*member_p=*/false,
15671                                 /*friend_p=*/false);
15672       if (declares_class_or_enum & 2)
15673         cp_parser_check_for_definition_in_return_type (declarator,
15674                                                        decl_specifiers.type,
15675                                                        decl_specifiers.locations[ds_type_spec]);
15676       if (declarator != cp_error_declarator)
15677         {
15678           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15679             permerror (decl_specifiers.locations[ds_inline],
15680                        "explicit instantiation shall not use"
15681                        " %<inline%> specifier");
15682           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15683             permerror (decl_specifiers.locations[ds_constexpr],
15684                        "explicit instantiation shall not use"
15685                        " %<constexpr%> specifier");
15686
15687           decl = grokdeclarator (declarator, &decl_specifiers,
15688                                  NORMAL, 0, &decl_specifiers.attributes);
15689           /* Turn access control back on for names used during
15690              template instantiation.  */
15691           pop_deferring_access_checks ();
15692           /* Do the explicit instantiation.  */
15693           do_decl_instantiation (decl, extension_specifier);
15694         }
15695       else
15696         {
15697           pop_deferring_access_checks ();
15698           /* Skip the body of the explicit instantiation.  */
15699           cp_parser_skip_to_end_of_statement (parser);
15700         }
15701     }
15702   /* We're done with the instantiation.  */
15703   end_explicit_instantiation ();
15704
15705   cp_parser_consume_semicolon_at_end_of_statement (parser);
15706
15707   timevar_pop (TV_TEMPLATE_INST);
15708 }
15709
15710 /* Parse an explicit-specialization.
15711
15712    explicit-specialization:
15713      template < > declaration
15714
15715    Although the standard says `declaration', what it really means is:
15716
15717    explicit-specialization:
15718      template <> decl-specifier [opt] init-declarator [opt] ;
15719      template <> function-definition
15720      template <> explicit-specialization
15721      template <> template-declaration  */
15722
15723 static void
15724 cp_parser_explicit_specialization (cp_parser* parser)
15725 {
15726   bool need_lang_pop;
15727   cp_token *token = cp_lexer_peek_token (parser->lexer);
15728
15729   /* Look for the `template' keyword.  */
15730   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15731   /* Look for the `<'.  */
15732   cp_parser_require (parser, CPP_LESS, RT_LESS);
15733   /* Look for the `>'.  */
15734   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15735   /* We have processed another parameter list.  */
15736   ++parser->num_template_parameter_lists;
15737   /* [temp]
15738
15739      A template ... explicit specialization ... shall not have C
15740      linkage.  */
15741   if (current_lang_name == lang_name_c)
15742     {
15743       error_at (token->location, "template specialization with C linkage");
15744       /* Give it C++ linkage to avoid confusing other parts of the
15745          front end.  */
15746       push_lang_context (lang_name_cplusplus);
15747       need_lang_pop = true;
15748     }
15749   else
15750     need_lang_pop = false;
15751   /* Let the front end know that we are beginning a specialization.  */
15752   if (!begin_specialization ())
15753     {
15754       end_specialization ();
15755       return;
15756     }
15757
15758   /* If the next keyword is `template', we need to figure out whether
15759      or not we're looking a template-declaration.  */
15760   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15761     {
15762       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15763           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15764         cp_parser_template_declaration_after_export (parser,
15765                                                      /*member_p=*/false);
15766       else
15767         cp_parser_explicit_specialization (parser);
15768     }
15769   else
15770     /* Parse the dependent declaration.  */
15771     cp_parser_single_declaration (parser,
15772                                   /*checks=*/NULL,
15773                                   /*member_p=*/false,
15774                                   /*explicit_specialization_p=*/true,
15775                                   /*friend_p=*/NULL);
15776   /* We're done with the specialization.  */
15777   end_specialization ();
15778   /* For the erroneous case of a template with C linkage, we pushed an
15779      implicit C++ linkage scope; exit that scope now.  */
15780   if (need_lang_pop)
15781     pop_lang_context ();
15782   /* We're done with this parameter list.  */
15783   --parser->num_template_parameter_lists;
15784 }
15785
15786 /* Parse a type-specifier.
15787
15788    type-specifier:
15789      simple-type-specifier
15790      class-specifier
15791      enum-specifier
15792      elaborated-type-specifier
15793      cv-qualifier
15794
15795    GNU Extension:
15796
15797    type-specifier:
15798      __complex__
15799
15800    Returns a representation of the type-specifier.  For a
15801    class-specifier, enum-specifier, or elaborated-type-specifier, a
15802    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15803
15804    The parser flags FLAGS is used to control type-specifier parsing.
15805
15806    If IS_DECLARATION is TRUE, then this type-specifier is appearing
15807    in a decl-specifier-seq.
15808
15809    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15810    class-specifier, enum-specifier, or elaborated-type-specifier, then
15811    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
15812    if a type is declared; 2 if it is defined.  Otherwise, it is set to
15813    zero.
15814
15815    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15816    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
15817    is set to FALSE.  */
15818
15819 static tree
15820 cp_parser_type_specifier (cp_parser* parser,
15821                           cp_parser_flags flags,
15822                           cp_decl_specifier_seq *decl_specs,
15823                           bool is_declaration,
15824                           int* declares_class_or_enum,
15825                           bool* is_cv_qualifier)
15826 {
15827   tree type_spec = NULL_TREE;
15828   cp_token *token;
15829   enum rid keyword;
15830   cp_decl_spec ds = ds_last;
15831
15832   /* Assume this type-specifier does not declare a new type.  */
15833   if (declares_class_or_enum)
15834     *declares_class_or_enum = 0;
15835   /* And that it does not specify a cv-qualifier.  */
15836   if (is_cv_qualifier)
15837     *is_cv_qualifier = false;
15838   /* Peek at the next token.  */
15839   token = cp_lexer_peek_token (parser->lexer);
15840
15841   /* If we're looking at a keyword, we can use that to guide the
15842      production we choose.  */
15843   keyword = token->keyword;
15844   switch (keyword)
15845     {
15846     case RID_ENUM:
15847       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15848         goto elaborated_type_specifier;
15849
15850       /* Look for the enum-specifier.  */
15851       type_spec = cp_parser_enum_specifier (parser);
15852       /* If that worked, we're done.  */
15853       if (type_spec)
15854         {
15855           if (declares_class_or_enum)
15856             *declares_class_or_enum = 2;
15857           if (decl_specs)
15858             cp_parser_set_decl_spec_type (decl_specs,
15859                                           type_spec,
15860                                           token,
15861                                           /*type_definition_p=*/true);
15862           return type_spec;
15863         }
15864       else
15865         goto elaborated_type_specifier;
15866
15867       /* Any of these indicate either a class-specifier, or an
15868          elaborated-type-specifier.  */
15869     case RID_CLASS:
15870     case RID_STRUCT:
15871     case RID_UNION:
15872       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15873         goto elaborated_type_specifier;
15874
15875       /* Parse tentatively so that we can back up if we don't find a
15876          class-specifier.  */
15877       cp_parser_parse_tentatively (parser);
15878       /* Look for the class-specifier.  */
15879       type_spec = cp_parser_class_specifier (parser);
15880       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15881       /* If that worked, we're done.  */
15882       if (cp_parser_parse_definitely (parser))
15883         {
15884           if (declares_class_or_enum)
15885             *declares_class_or_enum = 2;
15886           if (decl_specs)
15887             cp_parser_set_decl_spec_type (decl_specs,
15888                                           type_spec,
15889                                           token,
15890                                           /*type_definition_p=*/true);
15891           return type_spec;
15892         }
15893
15894       /* Fall through.  */
15895     elaborated_type_specifier:
15896       /* We're declaring (not defining) a class or enum.  */
15897       if (declares_class_or_enum)
15898         *declares_class_or_enum = 1;
15899
15900       /* Fall through.  */
15901     case RID_TYPENAME:
15902       /* Look for an elaborated-type-specifier.  */
15903       type_spec
15904         = (cp_parser_elaborated_type_specifier
15905            (parser,
15906             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15907             is_declaration));
15908       if (decl_specs)
15909         cp_parser_set_decl_spec_type (decl_specs,
15910                                       type_spec,
15911                                       token,
15912                                       /*type_definition_p=*/false);
15913       return type_spec;
15914
15915     case RID_CONST:
15916       ds = ds_const;
15917       if (is_cv_qualifier)
15918         *is_cv_qualifier = true;
15919       break;
15920
15921     case RID_VOLATILE:
15922       ds = ds_volatile;
15923       if (is_cv_qualifier)
15924         *is_cv_qualifier = true;
15925       break;
15926
15927     case RID_RESTRICT:
15928       ds = ds_restrict;
15929       if (is_cv_qualifier)
15930         *is_cv_qualifier = true;
15931       break;
15932
15933     case RID_COMPLEX:
15934       /* The `__complex__' keyword is a GNU extension.  */
15935       ds = ds_complex;
15936       break;
15937
15938     default:
15939       break;
15940     }
15941
15942   /* Handle simple keywords.  */
15943   if (ds != ds_last)
15944     {
15945       if (decl_specs)
15946         {
15947           set_and_check_decl_spec_loc (decl_specs, ds, token);
15948           decl_specs->any_specifiers_p = true;
15949         }
15950       return cp_lexer_consume_token (parser->lexer)->u.value;
15951     }
15952
15953   /* If we do not already have a type-specifier, assume we are looking
15954      at a simple-type-specifier.  */
15955   type_spec = cp_parser_simple_type_specifier (parser,
15956                                                decl_specs,
15957                                                flags);
15958
15959   /* If we didn't find a type-specifier, and a type-specifier was not
15960      optional in this context, issue an error message.  */
15961   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15962     {
15963       cp_parser_error (parser, "expected type specifier");
15964       return error_mark_node;
15965     }
15966
15967   return type_spec;
15968 }
15969
15970 /* Parse a simple-type-specifier.
15971
15972    simple-type-specifier:
15973      :: [opt] nested-name-specifier [opt] type-name
15974      :: [opt] nested-name-specifier template template-id
15975      char
15976      wchar_t
15977      bool
15978      short
15979      int
15980      long
15981      signed
15982      unsigned
15983      float
15984      double
15985      void
15986
15987    C++0x Extension:
15988
15989    simple-type-specifier:
15990      auto
15991      decltype ( expression )   
15992      char16_t
15993      char32_t
15994      __underlying_type ( type-id )
15995
15996    GNU Extension:
15997
15998    simple-type-specifier:
15999      __int128
16000      __typeof__ unary-expression
16001      __typeof__ ( type-id )
16002      __typeof__ ( type-id ) { initializer-list , [opt] }
16003
16004    Concepts Extension:
16005
16006    simple-type-specifier:
16007      constrained-type-specifier
16008
16009    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
16010    appropriately updated.  */
16011
16012 static tree
16013 cp_parser_simple_type_specifier (cp_parser* parser,
16014                                  cp_decl_specifier_seq *decl_specs,
16015                                  cp_parser_flags flags)
16016 {
16017   tree type = NULL_TREE;
16018   cp_token *token;
16019   int idx;
16020
16021   /* Peek at the next token.  */
16022   token = cp_lexer_peek_token (parser->lexer);
16023
16024   /* If we're looking at a keyword, things are easy.  */
16025   switch (token->keyword)
16026     {
16027     case RID_CHAR:
16028       if (decl_specs)
16029         decl_specs->explicit_char_p = true;
16030       type = char_type_node;
16031       break;
16032     case RID_CHAR16:
16033       type = char16_type_node;
16034       break;
16035     case RID_CHAR32:
16036       type = char32_type_node;
16037       break;
16038     case RID_WCHAR:
16039       type = wchar_type_node;
16040       break;
16041     case RID_BOOL:
16042       type = boolean_type_node;
16043       break;
16044     case RID_SHORT:
16045       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16046       type = short_integer_type_node;
16047       break;
16048     case RID_INT:
16049       if (decl_specs)
16050         decl_specs->explicit_int_p = true;
16051       type = integer_type_node;
16052       break;
16053     case RID_INT_N_0:
16054     case RID_INT_N_1:
16055     case RID_INT_N_2:
16056     case RID_INT_N_3:
16057       idx = token->keyword - RID_INT_N_0;
16058       if (! int_n_enabled_p [idx])
16059         break;
16060       if (decl_specs)
16061         {
16062           decl_specs->explicit_intN_p = true;
16063           decl_specs->int_n_idx = idx;
16064         }
16065       type = int_n_trees [idx].signed_type;
16066       break;
16067     case RID_LONG:
16068       if (decl_specs)
16069         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16070       type = long_integer_type_node;
16071       break;
16072     case RID_SIGNED:
16073       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16074       type = integer_type_node;
16075       break;
16076     case RID_UNSIGNED:
16077       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16078       type = unsigned_type_node;
16079       break;
16080     case RID_FLOAT:
16081       type = float_type_node;
16082       break;
16083     case RID_DOUBLE:
16084       type = double_type_node;
16085       break;
16086     case RID_VOID:
16087       type = void_type_node;
16088       break;
16089
16090     case RID_AUTO:
16091       maybe_warn_cpp0x (CPP0X_AUTO);
16092       if (parser->auto_is_implicit_function_template_parm_p)
16093         {
16094           /* The 'auto' might be the placeholder return type for a function decl
16095              with trailing return type.  */
16096           bool have_trailing_return_fn_decl = false;
16097
16098           cp_parser_parse_tentatively (parser);
16099           cp_lexer_consume_token (parser->lexer);
16100           while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16101                  && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16102                  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16103                  && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16104             {
16105               if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16106                 {
16107                   cp_lexer_consume_token (parser->lexer);
16108                   cp_parser_skip_to_closing_parenthesis (parser,
16109                                                          /*recovering*/false,
16110                                                          /*or_comma*/false,
16111                                                          /*consume_paren*/true);
16112                   continue;
16113                 }
16114
16115               if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16116                 {
16117                   have_trailing_return_fn_decl = true;
16118                   break;
16119                 }
16120
16121               cp_lexer_consume_token (parser->lexer);
16122             }
16123           cp_parser_abort_tentative_parse (parser);
16124
16125           if (have_trailing_return_fn_decl)
16126             {
16127               type = make_auto ();
16128               break;
16129             }
16130
16131           if (cxx_dialect >= cxx14)
16132             {
16133               type = synthesize_implicit_template_parm (parser, NULL_TREE);
16134               type = TREE_TYPE (type);
16135             }
16136           else
16137             type = error_mark_node;
16138
16139           if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16140             {
16141               if (cxx_dialect < cxx14)
16142                 error_at (token->location,
16143                          "use of %<auto%> in lambda parameter declaration "
16144                          "only available with "
16145                          "-std=c++14 or -std=gnu++14");
16146             }
16147           else if (cxx_dialect < cxx14)
16148             error_at (token->location,
16149                      "use of %<auto%> in parameter declaration "
16150                      "only available with "
16151                      "-std=c++14 or -std=gnu++14");
16152           else if (!flag_concepts)
16153             pedwarn (token->location, OPT_Wpedantic,
16154                      "ISO C++ forbids use of %<auto%> in parameter "
16155                      "declaration");
16156         }
16157       else
16158         type = make_auto ();
16159       break;
16160
16161     case RID_DECLTYPE:
16162       /* Since DR 743, decltype can either be a simple-type-specifier by
16163          itself or begin a nested-name-specifier.  Parsing it will replace
16164          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16165          handling below decide what to do.  */
16166       cp_parser_decltype (parser);
16167       cp_lexer_set_token_position (parser->lexer, token);
16168       break;
16169
16170     case RID_TYPEOF:
16171       /* Consume the `typeof' token.  */
16172       cp_lexer_consume_token (parser->lexer);
16173       /* Parse the operand to `typeof'.  */
16174       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16175       /* If it is not already a TYPE, take its type.  */
16176       if (!TYPE_P (type))
16177         type = finish_typeof (type);
16178
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_UNDERLYING_TYPE:
16187       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16188       if (decl_specs)
16189         cp_parser_set_decl_spec_type (decl_specs, type,
16190                                       token,
16191                                       /*type_definition_p=*/false);
16192
16193       return type;
16194
16195     case RID_BASES:
16196     case RID_DIRECT_BASES:
16197       type = cp_parser_trait_expr (parser, token->keyword);
16198       if (decl_specs)
16199        cp_parser_set_decl_spec_type (decl_specs, type,
16200                                      token,
16201                                      /*type_definition_p=*/false);
16202       return type;
16203     default:
16204       break;
16205     }
16206
16207   /* If token is an already-parsed decltype not followed by ::,
16208      it's a simple-type-specifier.  */
16209   if (token->type == CPP_DECLTYPE
16210       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16211     {
16212       type = saved_checks_value (token->u.tree_check_value);
16213       if (decl_specs)
16214         {
16215           cp_parser_set_decl_spec_type (decl_specs, type,
16216                                         token,
16217                                         /*type_definition_p=*/false);
16218           /* Remember that we are handling a decltype in order to
16219              implement the resolution of DR 1510 when the argument
16220              isn't instantiation dependent.  */
16221           decl_specs->decltype_p = true;
16222         }
16223       cp_lexer_consume_token (parser->lexer);
16224       return type;
16225     }
16226
16227   /* If the type-specifier was for a built-in type, we're done.  */
16228   if (type)
16229     {
16230       /* Record the type.  */
16231       if (decl_specs
16232           && (token->keyword != RID_SIGNED
16233               && token->keyword != RID_UNSIGNED
16234               && token->keyword != RID_SHORT
16235               && token->keyword != RID_LONG))
16236         cp_parser_set_decl_spec_type (decl_specs,
16237                                       type,
16238                                       token,
16239                                       /*type_definition_p=*/false);
16240       if (decl_specs)
16241         decl_specs->any_specifiers_p = true;
16242
16243       /* Consume the token.  */
16244       cp_lexer_consume_token (parser->lexer);
16245
16246       if (type == error_mark_node)
16247         return error_mark_node;
16248
16249       /* There is no valid C++ program where a non-template type is
16250          followed by a "<".  That usually indicates that the user thought
16251          that the type was a template.  */
16252       cp_parser_check_for_invalid_template_id (parser, type, none_type,
16253                                                token->location);
16254
16255       return TYPE_NAME (type);
16256     }
16257
16258   /* The type-specifier must be a user-defined type.  */
16259   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16260     {
16261       bool qualified_p;
16262       bool global_p;
16263
16264       /* Don't gobble tokens or issue error messages if this is an
16265          optional type-specifier.  */
16266       if (flags & CP_PARSER_FLAGS_OPTIONAL)
16267         cp_parser_parse_tentatively (parser);
16268
16269       /* Look for the optional `::' operator.  */
16270       global_p
16271         = (cp_parser_global_scope_opt (parser,
16272                                        /*current_scope_valid_p=*/false)
16273            != NULL_TREE);
16274       /* Look for the nested-name specifier.  */
16275       qualified_p
16276         = (cp_parser_nested_name_specifier_opt (parser,
16277                                                 /*typename_keyword_p=*/false,
16278                                                 /*check_dependency_p=*/true,
16279                                                 /*type_p=*/false,
16280                                                 /*is_declaration=*/false)
16281            != NULL_TREE);
16282       token = cp_lexer_peek_token (parser->lexer);
16283       /* If we have seen a nested-name-specifier, and the next token
16284          is `template', then we are using the template-id production.  */
16285       if (parser->scope
16286           && cp_parser_optional_template_keyword (parser))
16287         {
16288           /* Look for the template-id.  */
16289           type = cp_parser_template_id (parser,
16290                                         /*template_keyword_p=*/true,
16291                                         /*check_dependency_p=*/true,
16292                                         none_type,
16293                                         /*is_declaration=*/false);
16294           /* If the template-id did not name a type, we are out of
16295              luck.  */
16296           if (TREE_CODE (type) != TYPE_DECL)
16297             {
16298               cp_parser_error (parser, "expected template-id for type");
16299               type = NULL_TREE;
16300             }
16301         }
16302       /* Otherwise, look for a type-name.  */
16303       else
16304         type = cp_parser_type_name (parser);
16305       /* Keep track of all name-lookups performed in class scopes.  */
16306       if (type
16307           && !global_p
16308           && !qualified_p
16309           && TREE_CODE (type) == TYPE_DECL
16310           && identifier_p (DECL_NAME (type)))
16311         maybe_note_name_used_in_class (DECL_NAME (type), type);
16312       /* If it didn't work out, we don't have a TYPE.  */
16313       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16314           && !cp_parser_parse_definitely (parser))
16315         type = NULL_TREE;
16316       if (type && decl_specs)
16317         cp_parser_set_decl_spec_type (decl_specs, type,
16318                                       token,
16319                                       /*type_definition_p=*/false);
16320     }
16321
16322   /* If we didn't get a type-name, issue an error message.  */
16323   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16324     {
16325       cp_parser_error (parser, "expected type-name");
16326       return error_mark_node;
16327     }
16328
16329   if (type && type != error_mark_node)
16330     {
16331       /* See if TYPE is an Objective-C type, and if so, parse and
16332          accept any protocol references following it.  Do this before
16333          the cp_parser_check_for_invalid_template_id() call, because
16334          Objective-C types can be followed by '<...>' which would
16335          enclose protocol names rather than template arguments, and so
16336          everything is fine.  */
16337       if (c_dialect_objc () && !parser->scope
16338           && (objc_is_id (type) || objc_is_class_name (type)))
16339         {
16340           tree protos = cp_parser_objc_protocol_refs_opt (parser);
16341           tree qual_type = objc_get_protocol_qualified_type (type, protos);
16342
16343           /* Clobber the "unqualified" type previously entered into
16344              DECL_SPECS with the new, improved protocol-qualified version.  */
16345           if (decl_specs)
16346             decl_specs->type = qual_type;
16347
16348           return qual_type;
16349         }
16350
16351       /* There is no valid C++ program where a non-template type is
16352          followed by a "<".  That usually indicates that the user
16353          thought that the type was a template.  */
16354       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16355                                                none_type,
16356                                                token->location);
16357     }
16358
16359   return type;
16360 }
16361
16362 /* Parse a type-name.
16363
16364    type-name:
16365      class-name
16366      enum-name
16367      typedef-name
16368      simple-template-id [in c++0x]
16369
16370    enum-name:
16371      identifier
16372
16373    typedef-name:
16374      identifier
16375
16376   Concepts:
16377
16378    type-name:
16379      concept-name
16380      partial-concept-id
16381
16382    concept-name:
16383      identifier
16384
16385    Returns a TYPE_DECL for the type.  */
16386
16387 static tree
16388 cp_parser_type_name (cp_parser* parser)
16389 {
16390   return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16391 }
16392
16393 /* See above. */
16394 static tree
16395 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16396 {
16397   tree type_decl;
16398
16399   /* We can't know yet whether it is a class-name or not.  */
16400   cp_parser_parse_tentatively (parser);
16401   /* Try a class-name.  */
16402   type_decl = cp_parser_class_name (parser,
16403                                     typename_keyword_p,
16404                                     /*template_keyword_p=*/false,
16405                                     none_type,
16406                                     /*check_dependency_p=*/true,
16407                                     /*class_head_p=*/false,
16408                                     /*is_declaration=*/false);
16409   /* If it's not a class-name, keep looking.  */
16410   if (!cp_parser_parse_definitely (parser))
16411     {
16412       if (cxx_dialect < cxx11)
16413         /* It must be a typedef-name or an enum-name.  */
16414         return cp_parser_nonclass_name (parser);
16415
16416       cp_parser_parse_tentatively (parser);
16417       /* It is either a simple-template-id representing an
16418          instantiation of an alias template...  */
16419       type_decl = cp_parser_template_id (parser,
16420                                          /*template_keyword_p=*/false,
16421                                          /*check_dependency_p=*/true,
16422                                          none_type,
16423                                          /*is_declaration=*/false);
16424       /* Note that this must be an instantiation of an alias template
16425          because [temp.names]/6 says:
16426          
16427              A template-id that names an alias template specialization
16428              is a type-name.
16429
16430          Whereas [temp.names]/7 says:
16431          
16432              A simple-template-id that names a class template
16433              specialization is a class-name.
16434
16435          With concepts, this could also be a partial-concept-id that
16436          declares a non-type template parameter. */
16437       if (type_decl != NULL_TREE
16438           && TREE_CODE (type_decl) == TYPE_DECL
16439           && TYPE_DECL_ALIAS_P (type_decl))
16440         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16441       else if (is_constrained_parameter (type_decl))
16442         /* Don't do anything. */ ;
16443       else
16444         cp_parser_simulate_error (parser);
16445
16446       if (!cp_parser_parse_definitely (parser))
16447         /* ... Or a typedef-name or an enum-name.  */
16448         return cp_parser_nonclass_name (parser);
16449     }
16450
16451   return type_decl;
16452 }
16453
16454 /*  Check if DECL and ARGS can form a constrained-type-specifier.
16455     If ARGS is non-null, we try to form a concept check of the
16456     form DECL<?, ARGS> where ? is a wildcard that matches any
16457     kind of template argument. If ARGS is NULL, then we try to
16458     form a concept check of the form DECL<?>. */
16459
16460 static tree
16461 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16462                                             tree decl, tree args)
16463 {
16464   gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16465
16466   /* If we a constrained-type-specifier cannot be deduced. */
16467   if (parser->prevent_constrained_type_specifiers)
16468     return NULL_TREE;
16469
16470   /* A constrained type specifier can only be found in an
16471      overload set or as a reference to a template declaration.
16472
16473      FIXME: This might be masking a bug.  It's possible that
16474      that the deduction below is causing template specializations
16475      to be formed with the wildcard as an argument.  */
16476   if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16477     return NULL_TREE;
16478
16479   /* Try to build a call expression that evaluates the
16480      concept. This can fail if the overload set refers
16481      only to non-templates. */
16482   tree placeholder = build_nt (WILDCARD_DECL);
16483   tree check = build_concept_check (decl, placeholder, args);
16484   if (check == error_mark_node)
16485     return NULL_TREE;
16486
16487   /* Deduce the checked constraint and the prototype parameter.
16488
16489      FIXME: In certain cases, failure to deduce should be a
16490      diagnosable error.  */
16491   tree conc;
16492   tree proto;
16493   if (!deduce_constrained_parameter (check, conc, proto))
16494     return NULL_TREE;
16495
16496   /* In template parameter scope, this results in a constrained
16497      parameter. Return a descriptor of that parm. */
16498   if (processing_template_parmlist)
16499     return build_constrained_parameter (conc, proto, args);
16500
16501   /* In a parameter-declaration-clause, constrained-type
16502      specifiers result in invented template parameters.  */
16503   if (parser->auto_is_implicit_function_template_parm_p)
16504     {
16505       tree x = build_constrained_parameter (conc, proto, args);
16506       return synthesize_implicit_template_parm (parser, x);
16507     }
16508   else
16509     {
16510      /* Otherwise, we're in a context where the constrained
16511         type name is deduced and the constraint applies
16512         after deduction. */
16513       return make_constrained_auto (conc, args);
16514     }
16515
16516   return NULL_TREE;
16517 }
16518
16519 /* If DECL refers to a concept, return a TYPE_DECL representing
16520    the result of using the constrained type specifier in the
16521    current context.  DECL refers to a concept if
16522
16523   - it is an overload set containing a function concept taking a single
16524     type argument, or
16525
16526   - it is a variable concept taking a single type argument.  */
16527
16528 static tree
16529 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16530 {
16531   if (flag_concepts
16532       && (TREE_CODE (decl) == OVERLOAD
16533           || BASELINK_P (decl)
16534           || variable_concept_p (decl)))
16535     return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16536   else
16537     return NULL_TREE;
16538 }
16539
16540 /* Check if DECL and ARGS form a partial-concept-id.  If so,
16541    assign ID to the resulting constrained placeholder.
16542
16543    Returns true if the partial-concept-id designates a placeholder
16544    and false otherwise. Note that *id is set to NULL_TREE in
16545    this case. */
16546
16547 static tree
16548 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16549 {
16550   return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16551 }
16552
16553 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16554    or a concept-name.
16555
16556    enum-name:
16557      identifier
16558
16559    typedef-name:
16560      identifier
16561
16562    concept-name:
16563      identifier
16564
16565    Returns a TYPE_DECL for the type.  */
16566
16567 static tree
16568 cp_parser_nonclass_name (cp_parser* parser)
16569 {
16570   tree type_decl;
16571   tree identifier;
16572
16573   cp_token *token = cp_lexer_peek_token (parser->lexer);
16574   identifier = cp_parser_identifier (parser);
16575   if (identifier == error_mark_node)
16576     return error_mark_node;
16577
16578   /* Look up the type-name.  */
16579   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16580
16581   type_decl = strip_using_decl (type_decl);
16582   
16583   /* If we found an overload set, then it may refer to a concept-name. */
16584   if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16585     type_decl = decl;
16586
16587   if (TREE_CODE (type_decl) != TYPE_DECL
16588       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16589     {
16590       /* See if this is an Objective-C type.  */
16591       tree protos = cp_parser_objc_protocol_refs_opt (parser);
16592       tree type = objc_get_protocol_qualified_type (identifier, protos);
16593       if (type)
16594         type_decl = TYPE_NAME (type);
16595     }
16596
16597   /* Issue an error if we did not find a type-name.  */
16598   if (TREE_CODE (type_decl) != TYPE_DECL
16599       /* In Objective-C, we have the complication that class names are
16600          normally type names and start declarations (eg, the
16601          "NSObject" in "NSObject *object;"), but can be used in an
16602          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16603          is an expression.  So, a classname followed by a dot is not a
16604          valid type-name.  */
16605       || (objc_is_class_name (TREE_TYPE (type_decl))
16606           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16607     {
16608       if (!cp_parser_simulate_error (parser))
16609         cp_parser_name_lookup_error (parser, identifier, type_decl,
16610                                      NLE_TYPE, token->location);
16611       return error_mark_node;
16612     }
16613   /* Remember that the name was used in the definition of the
16614      current class so that we can check later to see if the
16615      meaning would have been different after the class was
16616      entirely defined.  */
16617   else if (type_decl != error_mark_node
16618            && !parser->scope)
16619     maybe_note_name_used_in_class (identifier, type_decl);
16620   
16621   return type_decl;
16622 }
16623
16624 /* Parse an elaborated-type-specifier.  Note that the grammar given
16625    here incorporates the resolution to DR68.
16626
16627    elaborated-type-specifier:
16628      class-key :: [opt] nested-name-specifier [opt] identifier
16629      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16630      enum-key :: [opt] nested-name-specifier [opt] identifier
16631      typename :: [opt] nested-name-specifier identifier
16632      typename :: [opt] nested-name-specifier template [opt]
16633        template-id
16634
16635    GNU extension:
16636
16637    elaborated-type-specifier:
16638      class-key attributes :: [opt] nested-name-specifier [opt] identifier
16639      class-key attributes :: [opt] nested-name-specifier [opt]
16640                template [opt] template-id
16641      enum attributes :: [opt] nested-name-specifier [opt] identifier
16642
16643    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16644    declared `friend'.  If IS_DECLARATION is TRUE, then this
16645    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16646    something is being declared.
16647
16648    Returns the TYPE specified.  */
16649
16650 static tree
16651 cp_parser_elaborated_type_specifier (cp_parser* parser,
16652                                      bool is_friend,
16653                                      bool is_declaration)
16654 {
16655   enum tag_types tag_type;
16656   tree identifier;
16657   tree type = NULL_TREE;
16658   tree attributes = NULL_TREE;
16659   tree globalscope;
16660   cp_token *token = NULL;
16661
16662   /* See if we're looking at the `enum' keyword.  */
16663   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16664     {
16665       /* Consume the `enum' token.  */
16666       cp_lexer_consume_token (parser->lexer);
16667       /* Remember that it's an enumeration type.  */
16668       tag_type = enum_type;
16669       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16670          enums) is used here.  */
16671       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16672           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16673         {
16674             pedwarn (input_location, 0, "elaborated-type-specifier "
16675                       "for a scoped enum must not use the %<%D%> keyword",
16676                       cp_lexer_peek_token (parser->lexer)->u.value);
16677           /* Consume the `struct' or `class' and parse it anyway.  */
16678           cp_lexer_consume_token (parser->lexer);
16679         }
16680       /* Parse the attributes.  */
16681       attributes = cp_parser_attributes_opt (parser);
16682     }
16683   /* Or, it might be `typename'.  */
16684   else if (cp_lexer_next_token_is_keyword (parser->lexer,
16685                                            RID_TYPENAME))
16686     {
16687       /* Consume the `typename' token.  */
16688       cp_lexer_consume_token (parser->lexer);
16689       /* Remember that it's a `typename' type.  */
16690       tag_type = typename_type;
16691     }
16692   /* Otherwise it must be a class-key.  */
16693   else
16694     {
16695       tag_type = cp_parser_class_key (parser);
16696       if (tag_type == none_type)
16697         return error_mark_node;
16698       /* Parse the attributes.  */
16699       attributes = cp_parser_attributes_opt (parser);
16700     }
16701
16702   /* Look for the `::' operator.  */
16703   globalscope =  cp_parser_global_scope_opt (parser,
16704                                              /*current_scope_valid_p=*/false);
16705   /* Look for the nested-name-specifier.  */
16706   if (tag_type == typename_type && !globalscope)
16707     {
16708       if (!cp_parser_nested_name_specifier (parser,
16709                                            /*typename_keyword_p=*/true,
16710                                            /*check_dependency_p=*/true,
16711                                            /*type_p=*/true,
16712                                             is_declaration))
16713         return error_mark_node;
16714     }
16715   else
16716     /* Even though `typename' is not present, the proposed resolution
16717        to Core Issue 180 says that in `class A<T>::B', `B' should be
16718        considered a type-name, even if `A<T>' is dependent.  */
16719     cp_parser_nested_name_specifier_opt (parser,
16720                                          /*typename_keyword_p=*/true,
16721                                          /*check_dependency_p=*/true,
16722                                          /*type_p=*/true,
16723                                          is_declaration);
16724  /* For everything but enumeration types, consider a template-id.
16725     For an enumeration type, consider only a plain identifier.  */
16726   if (tag_type != enum_type)
16727     {
16728       bool template_p = false;
16729       tree decl;
16730
16731       /* Allow the `template' keyword.  */
16732       template_p = cp_parser_optional_template_keyword (parser);
16733       /* If we didn't see `template', we don't know if there's a
16734          template-id or not.  */
16735       if (!template_p)
16736         cp_parser_parse_tentatively (parser);
16737       /* Parse the template-id.  */
16738       token = cp_lexer_peek_token (parser->lexer);
16739       decl = cp_parser_template_id (parser, template_p,
16740                                     /*check_dependency_p=*/true,
16741                                     tag_type,
16742                                     is_declaration);
16743       /* If we didn't find a template-id, look for an ordinary
16744          identifier.  */
16745       if (!template_p && !cp_parser_parse_definitely (parser))
16746         ;
16747       /* We can get here when cp_parser_template_id, called by
16748          cp_parser_class_name with tag_type == none_type, succeeds
16749          and caches a BASELINK.  Then, when called again here,
16750          instead of failing and returning an error_mark_node
16751          returns it (see template/typename17.C in C++11).
16752          ??? Could we diagnose this earlier?  */
16753       else if (tag_type == typename_type && BASELINK_P (decl))
16754         {
16755           cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16756           type = error_mark_node;
16757         }
16758       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16759          in effect, then we must assume that, upon instantiation, the
16760          template will correspond to a class.  */
16761       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16762                && tag_type == typename_type)
16763         type = make_typename_type (parser->scope, decl,
16764                                    typename_type,
16765                                    /*complain=*/tf_error);
16766       /* If the `typename' keyword is in effect and DECL is not a type
16767          decl, then type is non existent.   */
16768       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16769         ; 
16770       else if (TREE_CODE (decl) == TYPE_DECL)
16771         type = check_elaborated_type_specifier (tag_type, decl,
16772                                                 /*allow_template_p=*/true);
16773       else if (decl == error_mark_node)
16774         type = error_mark_node; 
16775     }
16776
16777   if (!type)
16778     {
16779       token = cp_lexer_peek_token (parser->lexer);
16780       identifier = cp_parser_identifier (parser);
16781
16782       if (identifier == error_mark_node)
16783         {
16784           parser->scope = NULL_TREE;
16785           return error_mark_node;
16786         }
16787
16788       /* For a `typename', we needn't call xref_tag.  */
16789       if (tag_type == typename_type
16790           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16791         return cp_parser_make_typename_type (parser, identifier,
16792                                              token->location);
16793
16794       /* Template parameter lists apply only if we are not within a
16795          function parameter list.  */
16796       bool template_parm_lists_apply
16797           = parser->num_template_parameter_lists;
16798       if (template_parm_lists_apply)
16799         for (cp_binding_level *s = current_binding_level;
16800              s && s->kind != sk_template_parms;
16801              s = s->level_chain)
16802           if (s->kind == sk_function_parms)
16803             template_parm_lists_apply = false;
16804
16805       /* Look up a qualified name in the usual way.  */
16806       if (parser->scope)
16807         {
16808           tree decl;
16809           tree ambiguous_decls;
16810
16811           decl = cp_parser_lookup_name (parser, identifier,
16812                                         tag_type,
16813                                         /*is_template=*/false,
16814                                         /*is_namespace=*/false,
16815                                         /*check_dependency=*/true,
16816                                         &ambiguous_decls,
16817                                         token->location);
16818
16819           /* If the lookup was ambiguous, an error will already have been
16820              issued.  */
16821           if (ambiguous_decls)
16822             return error_mark_node;
16823
16824           /* If we are parsing friend declaration, DECL may be a
16825              TEMPLATE_DECL tree node here.  However, we need to check
16826              whether this TEMPLATE_DECL results in valid code.  Consider
16827              the following example:
16828
16829                namespace N {
16830                  template <class T> class C {};
16831                }
16832                class X {
16833                  template <class T> friend class N::C; // #1, valid code
16834                };
16835                template <class T> class Y {
16836                  friend class N::C;                    // #2, invalid code
16837                };
16838
16839              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16840              name lookup of `N::C'.  We see that friend declaration must
16841              be template for the code to be valid.  Note that
16842              processing_template_decl does not work here since it is
16843              always 1 for the above two cases.  */
16844
16845           decl = (cp_parser_maybe_treat_template_as_class
16846                   (decl, /*tag_name_p=*/is_friend
16847                          && template_parm_lists_apply));
16848
16849           if (TREE_CODE (decl) != TYPE_DECL)
16850             {
16851               cp_parser_diagnose_invalid_type_name (parser,
16852                                                     identifier,
16853                                                     token->location);
16854               return error_mark_node;
16855             }
16856
16857           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16858             {
16859               bool allow_template = (template_parm_lists_apply
16860                                      || DECL_SELF_REFERENCE_P (decl));
16861               type = check_elaborated_type_specifier (tag_type, decl,
16862                                                       allow_template);
16863
16864               if (type == error_mark_node)
16865                 return error_mark_node;
16866             }
16867
16868           /* Forward declarations of nested types, such as
16869
16870                class C1::C2;
16871                class C1::C2::C3;
16872
16873              are invalid unless all components preceding the final '::'
16874              are complete.  If all enclosing types are complete, these
16875              declarations become merely pointless.
16876
16877              Invalid forward declarations of nested types are errors
16878              caught elsewhere in parsing.  Those that are pointless arrive
16879              here.  */
16880
16881           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16882               && !is_friend && !processing_explicit_instantiation)
16883             warning (0, "declaration %qD does not declare anything", decl);
16884
16885           type = TREE_TYPE (decl);
16886         }
16887       else
16888         {
16889           /* An elaborated-type-specifier sometimes introduces a new type and
16890              sometimes names an existing type.  Normally, the rule is that it
16891              introduces a new type only if there is not an existing type of
16892              the same name already in scope.  For example, given:
16893
16894                struct S {};
16895                void f() { struct S s; }
16896
16897              the `struct S' in the body of `f' is the same `struct S' as in
16898              the global scope; the existing definition is used.  However, if
16899              there were no global declaration, this would introduce a new
16900              local class named `S'.
16901
16902              An exception to this rule applies to the following code:
16903
16904                namespace N { struct S; }
16905
16906              Here, the elaborated-type-specifier names a new type
16907              unconditionally; even if there is already an `S' in the
16908              containing scope this declaration names a new type.
16909              This exception only applies if the elaborated-type-specifier
16910              forms the complete declaration:
16911
16912                [class.name]
16913
16914                A declaration consisting solely of `class-key identifier ;' is
16915                either a redeclaration of the name in the current scope or a
16916                forward declaration of the identifier as a class name.  It
16917                introduces the name into the current scope.
16918
16919              We are in this situation precisely when the next token is a `;'.
16920
16921              An exception to the exception is that a `friend' declaration does
16922              *not* name a new type; i.e., given:
16923
16924                struct S { friend struct T; };
16925
16926              `T' is not a new type in the scope of `S'.
16927
16928              Also, `new struct S' or `sizeof (struct S)' never results in the
16929              definition of a new type; a new type can only be declared in a
16930              declaration context.  */
16931
16932           tag_scope ts;
16933           bool template_p;
16934
16935           if (is_friend)
16936             /* Friends have special name lookup rules.  */
16937             ts = ts_within_enclosing_non_class;
16938           else if (is_declaration
16939                    && cp_lexer_next_token_is (parser->lexer,
16940                                               CPP_SEMICOLON))
16941             /* This is a `class-key identifier ;' */
16942             ts = ts_current;
16943           else
16944             ts = ts_global;
16945
16946           template_p =
16947             (template_parm_lists_apply
16948              && (cp_parser_next_token_starts_class_definition_p (parser)
16949                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16950           /* An unqualified name was used to reference this type, so
16951              there were no qualifying templates.  */
16952           if (template_parm_lists_apply
16953               && !cp_parser_check_template_parameters (parser,
16954                                                        /*num_templates=*/0,
16955                                                        token->location,
16956                                                        /*declarator=*/NULL))
16957             return error_mark_node;
16958           type = xref_tag (tag_type, identifier, ts, template_p);
16959         }
16960     }
16961
16962   if (type == error_mark_node)
16963     return error_mark_node;
16964
16965   /* Allow attributes on forward declarations of classes.  */
16966   if (attributes)
16967     {
16968       if (TREE_CODE (type) == TYPENAME_TYPE)
16969         warning (OPT_Wattributes,
16970                  "attributes ignored on uninstantiated type");
16971       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16972                && ! processing_explicit_instantiation)
16973         warning (OPT_Wattributes,
16974                  "attributes ignored on template instantiation");
16975       else if (is_declaration && cp_parser_declares_only_class_p (parser))
16976         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16977       else
16978         warning (OPT_Wattributes,
16979                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16980     }
16981
16982   if (tag_type != enum_type)
16983     {
16984       /* Indicate whether this class was declared as a `class' or as a
16985          `struct'.  */
16986       if (CLASS_TYPE_P (type))
16987         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16988       cp_parser_check_class_key (tag_type, type);
16989     }
16990
16991   /* A "<" cannot follow an elaborated type specifier.  If that
16992      happens, the user was probably trying to form a template-id.  */
16993   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16994                                            token->location);
16995
16996   return type;
16997 }
16998
16999 /* Parse an enum-specifier.
17000
17001    enum-specifier:
17002      enum-head { enumerator-list [opt] }
17003      enum-head { enumerator-list , } [C++0x]
17004
17005    enum-head:
17006      enum-key identifier [opt] enum-base [opt]
17007      enum-key nested-name-specifier identifier enum-base [opt]
17008
17009    enum-key:
17010      enum
17011      enum class   [C++0x]
17012      enum struct  [C++0x]
17013
17014    enum-base:   [C++0x]
17015      : type-specifier-seq
17016
17017    opaque-enum-specifier:
17018      enum-key identifier enum-base [opt] ;
17019
17020    GNU Extensions:
17021      enum-key attributes[opt] identifier [opt] enum-base [opt] 
17022        { enumerator-list [opt] }attributes[opt]
17023      enum-key attributes[opt] identifier [opt] enum-base [opt]
17024        { enumerator-list, }attributes[opt] [C++0x]
17025
17026    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17027    if the token stream isn't an enum-specifier after all.  */
17028
17029 static tree
17030 cp_parser_enum_specifier (cp_parser* parser)
17031 {
17032   tree identifier;
17033   tree type = NULL_TREE;
17034   tree prev_scope;
17035   tree nested_name_specifier = NULL_TREE;
17036   tree attributes;
17037   bool scoped_enum_p = false;
17038   bool has_underlying_type = false;
17039   bool nested_being_defined = false;
17040   bool new_value_list = false;
17041   bool is_new_type = false;
17042   bool is_anonymous = false;
17043   tree underlying_type = NULL_TREE;
17044   cp_token *type_start_token = NULL;
17045   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17046
17047   parser->colon_corrects_to_scope_p = false;
17048
17049   /* Parse tentatively so that we can back up if we don't find a
17050      enum-specifier.  */
17051   cp_parser_parse_tentatively (parser);
17052
17053   /* Caller guarantees that the current token is 'enum', an identifier
17054      possibly follows, and the token after that is an opening brace.
17055      If we don't have an identifier, fabricate an anonymous name for
17056      the enumeration being defined.  */
17057   cp_lexer_consume_token (parser->lexer);
17058
17059   /* Parse the "class" or "struct", which indicates a scoped
17060      enumeration type in C++0x.  */
17061   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17062       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17063     {
17064       if (cxx_dialect < cxx11)
17065         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17066
17067       /* Consume the `struct' or `class' token.  */
17068       cp_lexer_consume_token (parser->lexer);
17069
17070       scoped_enum_p = true;
17071     }
17072
17073   attributes = cp_parser_attributes_opt (parser);
17074
17075   /* Clear the qualification.  */
17076   parser->scope = NULL_TREE;
17077   parser->qualifying_scope = NULL_TREE;
17078   parser->object_scope = NULL_TREE;
17079
17080   /* Figure out in what scope the declaration is being placed.  */
17081   prev_scope = current_scope ();
17082
17083   type_start_token = cp_lexer_peek_token (parser->lexer);
17084
17085   push_deferring_access_checks (dk_no_check);
17086   nested_name_specifier
17087       = cp_parser_nested_name_specifier_opt (parser,
17088                                              /*typename_keyword_p=*/true,
17089                                              /*check_dependency_p=*/false,
17090                                              /*type_p=*/false,
17091                                              /*is_declaration=*/false);
17092
17093   if (nested_name_specifier)
17094     {
17095       tree name;
17096
17097       identifier = cp_parser_identifier (parser);
17098       name =  cp_parser_lookup_name (parser, identifier,
17099                                      enum_type,
17100                                      /*is_template=*/false,
17101                                      /*is_namespace=*/false,
17102                                      /*check_dependency=*/true,
17103                                      /*ambiguous_decls=*/NULL,
17104                                      input_location);
17105       if (name && name != error_mark_node)
17106         {
17107           type = TREE_TYPE (name);
17108           if (TREE_CODE (type) == TYPENAME_TYPE)
17109             {
17110               /* Are template enums allowed in ISO? */
17111               if (template_parm_scope_p ())
17112                 pedwarn (type_start_token->location, OPT_Wpedantic,
17113                          "%qD is an enumeration template", name);
17114               /* ignore a typename reference, for it will be solved by name
17115                  in start_enum.  */
17116               type = NULL_TREE;
17117             }
17118         }
17119       else if (nested_name_specifier == error_mark_node)
17120         /* We already issued an error.  */;
17121       else
17122         {
17123           error_at (type_start_token->location,
17124                     "%qD does not name an enumeration in %qT",
17125                     identifier, nested_name_specifier);
17126           nested_name_specifier = error_mark_node;
17127         }
17128     }
17129   else
17130     {
17131       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17132         identifier = cp_parser_identifier (parser);
17133       else
17134         {
17135           identifier = make_anon_name ();
17136           is_anonymous = true;
17137           if (scoped_enum_p)
17138             error_at (type_start_token->location,
17139                       "anonymous scoped enum is not allowed");
17140         }
17141     }
17142   pop_deferring_access_checks ();
17143
17144   /* Check for the `:' that denotes a specified underlying type in C++0x.
17145      Note that a ':' could also indicate a bitfield width, however.  */
17146   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17147     {
17148       cp_decl_specifier_seq type_specifiers;
17149
17150       /* Consume the `:'.  */
17151       cp_lexer_consume_token (parser->lexer);
17152
17153       /* Parse the type-specifier-seq.  */
17154       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17155                                     /*is_trailing_return=*/false,
17156                                     &type_specifiers);
17157
17158       /* At this point this is surely not elaborated type specifier.  */
17159       if (!cp_parser_parse_definitely (parser))
17160         return NULL_TREE;
17161
17162       if (cxx_dialect < cxx11)
17163         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17164
17165       has_underlying_type = true;
17166
17167       /* If that didn't work, stop.  */
17168       if (type_specifiers.type != error_mark_node)
17169         {
17170           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17171                                             /*initialized=*/0, NULL);
17172           if (underlying_type == error_mark_node
17173               || check_for_bare_parameter_packs (underlying_type))
17174             underlying_type = NULL_TREE;
17175         }
17176     }
17177
17178   /* Look for the `{' but don't consume it yet.  */
17179   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17180     {
17181       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17182         {
17183           cp_parser_error (parser, "expected %<{%>");
17184           if (has_underlying_type)
17185             {
17186               type = NULL_TREE;
17187               goto out;
17188             }
17189         }
17190       /* An opaque-enum-specifier must have a ';' here.  */
17191       if ((scoped_enum_p || underlying_type)
17192           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17193         {
17194           cp_parser_error (parser, "expected %<;%> or %<{%>");
17195           if (has_underlying_type)
17196             {
17197               type = NULL_TREE;
17198               goto out;
17199             }
17200         }
17201     }
17202
17203   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17204     return NULL_TREE;
17205
17206   if (nested_name_specifier)
17207     {
17208       if (CLASS_TYPE_P (nested_name_specifier))
17209         {
17210           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17211           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17212           push_scope (nested_name_specifier);
17213         }
17214       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17215         {
17216           push_nested_namespace (nested_name_specifier);
17217         }
17218     }
17219
17220   /* Issue an error message if type-definitions are forbidden here.  */
17221   if (!cp_parser_check_type_definition (parser))
17222     type = error_mark_node;
17223   else
17224     /* Create the new type.  We do this before consuming the opening
17225        brace so the enum will be recorded as being on the line of its
17226        tag (or the 'enum' keyword, if there is no tag).  */
17227     type = start_enum (identifier, type, underlying_type,
17228                        attributes, scoped_enum_p, &is_new_type);
17229
17230   /* If the next token is not '{' it is an opaque-enum-specifier or an
17231      elaborated-type-specifier.  */
17232   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17233     {
17234       timevar_push (TV_PARSE_ENUM);
17235       if (nested_name_specifier
17236           && nested_name_specifier != error_mark_node)
17237         {
17238           /* The following catches invalid code such as:
17239              enum class S<int>::E { A, B, C }; */
17240           if (!processing_specialization
17241               && CLASS_TYPE_P (nested_name_specifier)
17242               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17243             error_at (type_start_token->location, "cannot add an enumerator "
17244                       "list to a template instantiation");
17245
17246           if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17247             {
17248               error_at (type_start_token->location,
17249                         "%<%T::%E%> has not been declared",
17250                         TYPE_CONTEXT (nested_name_specifier),
17251                         nested_name_specifier);
17252               type = error_mark_node;
17253             }
17254           else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17255                    && !CLASS_TYPE_P (nested_name_specifier))
17256             {
17257               error_at (type_start_token->location, "nested name specifier "
17258                         "%qT for enum declaration does not name a class "
17259                         "or namespace", nested_name_specifier);
17260               type = error_mark_node;
17261             }
17262           /* If that scope does not contain the scope in which the
17263              class was originally declared, the program is invalid.  */
17264           else if (prev_scope && !is_ancestor (prev_scope,
17265                                                nested_name_specifier))
17266             {
17267               if (at_namespace_scope_p ())
17268                 error_at (type_start_token->location,
17269                           "declaration of %qD in namespace %qD which does not "
17270                           "enclose %qD",
17271                           type, prev_scope, nested_name_specifier);
17272               else
17273                 error_at (type_start_token->location,
17274                           "declaration of %qD in %qD which does not "
17275                           "enclose %qD",
17276                           type, prev_scope, nested_name_specifier);
17277               type = error_mark_node;
17278             }
17279         }
17280
17281       if (scoped_enum_p)
17282         begin_scope (sk_scoped_enum, type);
17283
17284       /* Consume the opening brace.  */
17285       cp_lexer_consume_token (parser->lexer);
17286
17287       if (type == error_mark_node)
17288         ; /* Nothing to add */
17289       else if (OPAQUE_ENUM_P (type)
17290                || (cxx_dialect > cxx98 && processing_specialization))
17291         {
17292           new_value_list = true;
17293           SET_OPAQUE_ENUM_P (type, false);
17294           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17295         }
17296       else
17297         {
17298           error_at (type_start_token->location,
17299                     "multiple definition of %q#T", type);
17300           inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17301                   "previous definition here");
17302           type = error_mark_node;
17303         }
17304
17305       if (type == error_mark_node)
17306         cp_parser_skip_to_end_of_block_or_statement (parser);
17307       /* If the next token is not '}', then there are some enumerators.  */
17308       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17309         {
17310           if (is_anonymous && !scoped_enum_p)
17311             pedwarn (type_start_token->location, OPT_Wpedantic,
17312                      "ISO C++ forbids empty anonymous enum");
17313         }
17314       else
17315         cp_parser_enumerator_list (parser, type);
17316
17317       /* Consume the final '}'.  */
17318       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17319
17320       if (scoped_enum_p)
17321         finish_scope ();
17322       timevar_pop (TV_PARSE_ENUM);
17323     }
17324   else
17325     {
17326       /* If a ';' follows, then it is an opaque-enum-specifier
17327         and additional restrictions apply.  */
17328       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17329         {
17330           if (is_anonymous)
17331             error_at (type_start_token->location,
17332                       "opaque-enum-specifier without name");
17333           else if (nested_name_specifier)
17334             error_at (type_start_token->location,
17335                       "opaque-enum-specifier must use a simple identifier");
17336         }
17337     }
17338
17339   /* Look for trailing attributes to apply to this enumeration, and
17340      apply them if appropriate.  */
17341   if (cp_parser_allow_gnu_extensions_p (parser))
17342     {
17343       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17344       cplus_decl_attributes (&type,
17345                              trailing_attr,
17346                              (int) ATTR_FLAG_TYPE_IN_PLACE);
17347     }
17348
17349   /* Finish up the enumeration.  */
17350   if (type != error_mark_node)
17351     {
17352       if (new_value_list)
17353         finish_enum_value_list (type);
17354       if (is_new_type)
17355         finish_enum (type);
17356     }
17357
17358   if (nested_name_specifier)
17359     {
17360       if (CLASS_TYPE_P (nested_name_specifier))
17361         {
17362           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17363           pop_scope (nested_name_specifier);
17364         }
17365       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17366         {
17367           pop_nested_namespace (nested_name_specifier);
17368         }
17369     }
17370  out:
17371   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17372   return type;
17373 }
17374
17375 /* Parse an enumerator-list.  The enumerators all have the indicated
17376    TYPE.
17377
17378    enumerator-list:
17379      enumerator-definition
17380      enumerator-list , enumerator-definition  */
17381
17382 static void
17383 cp_parser_enumerator_list (cp_parser* parser, tree type)
17384 {
17385   while (true)
17386     {
17387       /* Parse an enumerator-definition.  */
17388       cp_parser_enumerator_definition (parser, type);
17389
17390       /* If the next token is not a ',', we've reached the end of
17391          the list.  */
17392       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17393         break;
17394       /* Otherwise, consume the `,' and keep going.  */
17395       cp_lexer_consume_token (parser->lexer);
17396       /* If the next token is a `}', there is a trailing comma.  */
17397       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17398         {
17399           if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17400             pedwarn (input_location, OPT_Wpedantic,
17401                      "comma at end of enumerator list");
17402           break;
17403         }
17404     }
17405 }
17406
17407 /* Parse an enumerator-definition.  The enumerator has the indicated
17408    TYPE.
17409
17410    enumerator-definition:
17411      enumerator
17412      enumerator = constant-expression
17413
17414    enumerator:
17415      identifier
17416
17417    GNU Extensions:
17418
17419    enumerator-definition:
17420      enumerator attributes [opt]
17421      enumerator attributes [opt] = constant-expression  */
17422
17423 static void
17424 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17425 {
17426   tree identifier;
17427   tree value;
17428   location_t loc;
17429
17430   /* Save the input location because we are interested in the location
17431      of the identifier and not the location of the explicit value.  */
17432   loc = cp_lexer_peek_token (parser->lexer)->location;
17433
17434   /* Look for the identifier.  */
17435   identifier = cp_parser_identifier (parser);
17436   if (identifier == error_mark_node)
17437     return;
17438
17439   /* Parse any specified attributes.  */
17440   tree attrs = cp_parser_attributes_opt (parser);
17441
17442   /* If the next token is an '=', then there is an explicit value.  */
17443   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17444     {
17445       /* Consume the `=' token.  */
17446       cp_lexer_consume_token (parser->lexer);
17447       /* Parse the value.  */
17448       value = cp_parser_constant_expression (parser);
17449     }
17450   else
17451     value = NULL_TREE;
17452
17453   /* If we are processing a template, make sure the initializer of the
17454      enumerator doesn't contain any bare template parameter pack.  */
17455   if (check_for_bare_parameter_packs (value))
17456     value = error_mark_node;
17457
17458   /* Create the enumerator.  */
17459   build_enumerator (identifier, value, type, attrs, loc);
17460 }
17461
17462 /* Parse a namespace-name.
17463
17464    namespace-name:
17465      original-namespace-name
17466      namespace-alias
17467
17468    Returns the NAMESPACE_DECL for the namespace.  */
17469
17470 static tree
17471 cp_parser_namespace_name (cp_parser* parser)
17472 {
17473   tree identifier;
17474   tree namespace_decl;
17475
17476   cp_token *token = cp_lexer_peek_token (parser->lexer);
17477
17478   /* Get the name of the namespace.  */
17479   identifier = cp_parser_identifier (parser);
17480   if (identifier == error_mark_node)
17481     return error_mark_node;
17482
17483   /* Look up the identifier in the currently active scope.  Look only
17484      for namespaces, due to:
17485
17486        [basic.lookup.udir]
17487
17488        When looking up a namespace-name in a using-directive or alias
17489        definition, only namespace names are considered.
17490
17491      And:
17492
17493        [basic.lookup.qual]
17494
17495        During the lookup of a name preceding the :: scope resolution
17496        operator, object, function, and enumerator names are ignored.
17497
17498      (Note that cp_parser_qualifying_entity only calls this
17499      function if the token after the name is the scope resolution
17500      operator.)  */
17501   namespace_decl = cp_parser_lookup_name (parser, identifier,
17502                                           none_type,
17503                                           /*is_template=*/false,
17504                                           /*is_namespace=*/true,
17505                                           /*check_dependency=*/true,
17506                                           /*ambiguous_decls=*/NULL,
17507                                           token->location);
17508   /* If it's not a namespace, issue an error.  */
17509   if (namespace_decl == error_mark_node
17510       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17511     {
17512       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17513         error_at (token->location, "%qD is not a namespace-name", identifier);
17514       cp_parser_error (parser, "expected namespace-name");
17515       namespace_decl = error_mark_node;
17516     }
17517
17518   return namespace_decl;
17519 }
17520
17521 /* Parse a namespace-definition.
17522
17523    namespace-definition:
17524      named-namespace-definition
17525      unnamed-namespace-definition
17526
17527    named-namespace-definition:
17528      original-namespace-definition
17529      extension-namespace-definition
17530
17531    original-namespace-definition:
17532      namespace identifier { namespace-body }
17533
17534    extension-namespace-definition:
17535      namespace original-namespace-name { namespace-body }
17536
17537    unnamed-namespace-definition:
17538      namespace { namespace-body } */
17539
17540 static void
17541 cp_parser_namespace_definition (cp_parser* parser)
17542 {
17543   tree identifier, attribs;
17544   bool has_visibility;
17545   bool is_inline;
17546   cp_token* token;
17547   int nested_definition_count = 0;
17548
17549   cp_ensure_no_omp_declare_simd (parser);
17550   cp_ensure_no_oacc_routine (parser);
17551   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17552     {
17553       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17554       is_inline = true;
17555       cp_lexer_consume_token (parser->lexer);
17556     }
17557   else
17558     is_inline = false;
17559
17560   /* Look for the `namespace' keyword.  */
17561   token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17562
17563   /* Parse any specified attributes before the identifier.  */
17564   attribs = cp_parser_attributes_opt (parser);
17565
17566   /* Get the name of the namespace.  We do not attempt to distinguish
17567      between an original-namespace-definition and an
17568      extension-namespace-definition at this point.  The semantic
17569      analysis routines are responsible for that.  */
17570   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17571     identifier = cp_parser_identifier (parser);
17572   else
17573     identifier = NULL_TREE;
17574
17575   /* Parse any specified attributes after the identifier.  */
17576   tree post_ident_attribs = cp_parser_attributes_opt (parser);
17577   if (post_ident_attribs)
17578     {
17579       if (attribs)
17580         attribs = chainon (attribs, post_ident_attribs);
17581       else
17582         attribs = post_ident_attribs;
17583     }
17584
17585   /* Start the namespace.  */
17586   push_namespace (identifier);
17587
17588   /* Parse any nested namespace definition. */
17589   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17590     {
17591       if (attribs)
17592         error_at (token->location, "a nested namespace definition cannot have attributes");
17593       if (cxx_dialect < cxx1z)
17594         pedwarn (input_location, OPT_Wpedantic,
17595                  "nested namespace definitions only available with "
17596                  "-std=c++1z or -std=gnu++1z");
17597       if (is_inline)
17598         error_at (token->location, "a nested namespace definition cannot be inline");
17599       while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17600         {
17601           cp_lexer_consume_token (parser->lexer);
17602           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17603             identifier = cp_parser_identifier (parser);
17604           else
17605             {
17606               cp_parser_error (parser, "nested identifier required");
17607               break;
17608             }
17609           ++nested_definition_count;
17610           push_namespace (identifier);
17611         }
17612     }
17613
17614   /* Look for the `{' to validate starting the namespace.  */
17615   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17616
17617   /* "inline namespace" is equivalent to a stub namespace definition
17618      followed by a strong using directive.  */
17619   if (is_inline)
17620     {
17621       tree name_space = current_namespace;
17622       /* Set up namespace association.  */
17623       DECL_NAMESPACE_ASSOCIATIONS (name_space)
17624         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17625                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
17626       /* Import the contents of the inline namespace.  */
17627       pop_namespace ();
17628       do_using_directive (name_space);
17629       push_namespace (identifier);
17630     }
17631
17632   has_visibility = handle_namespace_attrs (current_namespace, attribs);
17633
17634   warning  (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17635
17636   /* Parse the body of the namespace.  */
17637   cp_parser_namespace_body (parser);
17638
17639   if (has_visibility)
17640     pop_visibility (1);
17641
17642   /* Finish the nested namespace definitions.  */
17643   while (nested_definition_count--)
17644     pop_namespace ();
17645
17646   /* Finish the namespace.  */
17647   pop_namespace ();
17648   /* Look for the final `}'.  */
17649   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17650 }
17651
17652 /* Parse a namespace-body.
17653
17654    namespace-body:
17655      declaration-seq [opt]  */
17656
17657 static void
17658 cp_parser_namespace_body (cp_parser* parser)
17659 {
17660   cp_parser_declaration_seq_opt (parser);
17661 }
17662
17663 /* Parse a namespace-alias-definition.
17664
17665    namespace-alias-definition:
17666      namespace identifier = qualified-namespace-specifier ;  */
17667
17668 static void
17669 cp_parser_namespace_alias_definition (cp_parser* parser)
17670 {
17671   tree identifier;
17672   tree namespace_specifier;
17673
17674   cp_token *token = cp_lexer_peek_token (parser->lexer);
17675
17676   /* Look for the `namespace' keyword.  */
17677   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17678   /* Look for the identifier.  */
17679   identifier = cp_parser_identifier (parser);
17680   if (identifier == error_mark_node)
17681     return;
17682   /* Look for the `=' token.  */
17683   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17684       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
17685     {
17686       error_at (token->location, "%<namespace%> definition is not allowed here");
17687       /* Skip the definition.  */
17688       cp_lexer_consume_token (parser->lexer);
17689       if (cp_parser_skip_to_closing_brace (parser))
17690         cp_lexer_consume_token (parser->lexer);
17691       return;
17692     }
17693   cp_parser_require (parser, CPP_EQ, RT_EQ);
17694   /* Look for the qualified-namespace-specifier.  */
17695   namespace_specifier
17696     = cp_parser_qualified_namespace_specifier (parser);
17697   /* Look for the `;' token.  */
17698   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17699
17700   /* Register the alias in the symbol table.  */
17701   do_namespace_alias (identifier, namespace_specifier);
17702 }
17703
17704 /* Parse a qualified-namespace-specifier.
17705
17706    qualified-namespace-specifier:
17707      :: [opt] nested-name-specifier [opt] namespace-name
17708
17709    Returns a NAMESPACE_DECL corresponding to the specified
17710    namespace.  */
17711
17712 static tree
17713 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17714 {
17715   /* Look for the optional `::'.  */
17716   cp_parser_global_scope_opt (parser,
17717                               /*current_scope_valid_p=*/false);
17718
17719   /* Look for the optional nested-name-specifier.  */
17720   cp_parser_nested_name_specifier_opt (parser,
17721                                        /*typename_keyword_p=*/false,
17722                                        /*check_dependency_p=*/true,
17723                                        /*type_p=*/false,
17724                                        /*is_declaration=*/true);
17725
17726   return cp_parser_namespace_name (parser);
17727 }
17728
17729 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17730    access declaration.
17731
17732    using-declaration:
17733      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17734      using :: unqualified-id ;  
17735
17736    access-declaration:
17737      qualified-id ;  
17738
17739    */
17740
17741 static bool
17742 cp_parser_using_declaration (cp_parser* parser, 
17743                              bool access_declaration_p)
17744 {
17745   cp_token *token;
17746   bool typename_p = false;
17747   bool global_scope_p;
17748   tree decl;
17749   tree identifier;
17750   tree qscope;
17751   int oldcount = errorcount;
17752   cp_token *diag_token = NULL;
17753
17754   if (access_declaration_p)
17755     {
17756       diag_token = cp_lexer_peek_token (parser->lexer);
17757       cp_parser_parse_tentatively (parser);
17758     }
17759   else
17760     {
17761       /* Look for the `using' keyword.  */
17762       cp_parser_require_keyword (parser, RID_USING, RT_USING);
17763       
17764       /* Peek at the next token.  */
17765       token = cp_lexer_peek_token (parser->lexer);
17766       /* See if it's `typename'.  */
17767       if (token->keyword == RID_TYPENAME)
17768         {
17769           /* Remember that we've seen it.  */
17770           typename_p = true;
17771           /* Consume the `typename' token.  */
17772           cp_lexer_consume_token (parser->lexer);
17773         }
17774     }
17775
17776   /* Look for the optional global scope qualification.  */
17777   global_scope_p
17778     = (cp_parser_global_scope_opt (parser,
17779                                    /*current_scope_valid_p=*/false)
17780        != NULL_TREE);
17781
17782   /* If we saw `typename', or didn't see `::', then there must be a
17783      nested-name-specifier present.  */
17784   if (typename_p || !global_scope_p)
17785     {
17786       qscope = cp_parser_nested_name_specifier (parser, typename_p,
17787                                                 /*check_dependency_p=*/true,
17788                                                 /*type_p=*/false,
17789                                                 /*is_declaration=*/true);
17790       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17791         {
17792           cp_parser_skip_to_end_of_block_or_statement (parser);
17793           return false;
17794         }
17795     }
17796   /* Otherwise, we could be in either of the two productions.  In that
17797      case, treat the nested-name-specifier as optional.  */
17798   else
17799     qscope = cp_parser_nested_name_specifier_opt (parser,
17800                                                   /*typename_keyword_p=*/false,
17801                                                   /*check_dependency_p=*/true,
17802                                                   /*type_p=*/false,
17803                                                   /*is_declaration=*/true);
17804   if (!qscope)
17805     qscope = global_namespace;
17806   else if (UNSCOPED_ENUM_P (qscope))
17807     qscope = CP_TYPE_CONTEXT (qscope);
17808
17809   if (access_declaration_p && cp_parser_error_occurred (parser))
17810     /* Something has already gone wrong; there's no need to parse
17811        further.  Since an error has occurred, the return value of
17812        cp_parser_parse_definitely will be false, as required.  */
17813     return cp_parser_parse_definitely (parser);
17814
17815   token = cp_lexer_peek_token (parser->lexer);
17816   /* Parse the unqualified-id.  */
17817   identifier = cp_parser_unqualified_id (parser,
17818                                          /*template_keyword_p=*/false,
17819                                          /*check_dependency_p=*/true,
17820                                          /*declarator_p=*/true,
17821                                          /*optional_p=*/false);
17822
17823   if (access_declaration_p)
17824     {
17825       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17826         cp_parser_simulate_error (parser);
17827       if (!cp_parser_parse_definitely (parser))
17828         return false;
17829     }
17830
17831   /* The function we call to handle a using-declaration is different
17832      depending on what scope we are in.  */
17833   if (qscope == error_mark_node || identifier == error_mark_node)
17834     ;
17835   else if (!identifier_p (identifier)
17836            && TREE_CODE (identifier) != BIT_NOT_EXPR)
17837     /* [namespace.udecl]
17838
17839        A using declaration shall not name a template-id.  */
17840     error_at (token->location,
17841               "a template-id may not appear in a using-declaration");
17842   else
17843     {
17844       if (at_class_scope_p ())
17845         {
17846           /* Create the USING_DECL.  */
17847           decl = do_class_using_decl (parser->scope, identifier);
17848
17849           if (decl && typename_p)
17850             USING_DECL_TYPENAME_P (decl) = 1;
17851
17852           if (check_for_bare_parameter_packs (decl))
17853             {
17854               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17855               return false;
17856             }
17857           else
17858             /* Add it to the list of members in this class.  */
17859             finish_member_declaration (decl);
17860         }
17861       else
17862         {
17863           decl = cp_parser_lookup_name_simple (parser,
17864                                                identifier,
17865                                                token->location);
17866           if (decl == error_mark_node)
17867             cp_parser_name_lookup_error (parser, identifier,
17868                                          decl, NLE_NULL,
17869                                          token->location);
17870           else if (check_for_bare_parameter_packs (decl))
17871             {
17872               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17873               return false;
17874             }
17875           else if (!at_namespace_scope_p ())
17876             do_local_using_decl (decl, qscope, identifier);
17877           else
17878             do_toplevel_using_decl (decl, qscope, identifier);
17879         }
17880     }
17881
17882   /* Look for the final `;'.  */
17883   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17884
17885   if (access_declaration_p && errorcount == oldcount)
17886     warning_at (diag_token->location, OPT_Wdeprecated,
17887                 "access declarations are deprecated "
17888                 "in favour of using-declarations; "
17889                 "suggestion: add the %<using%> keyword");
17890
17891   return true;
17892 }
17893
17894 /* Parse an alias-declaration.
17895
17896    alias-declaration:
17897      using identifier attribute-specifier-seq [opt] = type-id  */
17898
17899 static tree
17900 cp_parser_alias_declaration (cp_parser* parser)
17901 {
17902   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17903   location_t id_location;
17904   cp_declarator *declarator;
17905   cp_decl_specifier_seq decl_specs;
17906   bool member_p;
17907   const char *saved_message = NULL;
17908
17909   /* Look for the `using' keyword.  */
17910   cp_token *using_token
17911     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17912   if (using_token == NULL)
17913     return error_mark_node;
17914
17915   id_location = cp_lexer_peek_token (parser->lexer)->location;
17916   id = cp_parser_identifier (parser);
17917   if (id == error_mark_node)
17918     return error_mark_node;
17919
17920   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17921   attributes = cp_parser_attributes_opt (parser);
17922   if (attributes == error_mark_node)
17923     return error_mark_node;
17924
17925   cp_parser_require (parser, CPP_EQ, RT_EQ);
17926
17927   if (cp_parser_error_occurred (parser))
17928     return error_mark_node;
17929
17930   cp_parser_commit_to_tentative_parse (parser);
17931
17932   /* Now we are going to parse the type-id of the declaration.  */
17933
17934   /*
17935     [dcl.type]/3 says:
17936
17937         "A type-specifier-seq shall not define a class or enumeration
17938          unless it appears in the type-id of an alias-declaration (7.1.3) that
17939          is not the declaration of a template-declaration."
17940
17941     In other words, if we currently are in an alias template, the
17942     type-id should not define a type.
17943
17944     So let's set parser->type_definition_forbidden_message in that
17945     case; cp_parser_check_type_definition (called by
17946     cp_parser_class_specifier) will then emit an error if a type is
17947     defined in the type-id.  */
17948   if (parser->num_template_parameter_lists)
17949     {
17950       saved_message = parser->type_definition_forbidden_message;
17951       parser->type_definition_forbidden_message =
17952         G_("types may not be defined in alias template declarations");
17953     }
17954
17955   type = cp_parser_type_id (parser);
17956
17957   /* Restore the error message if need be.  */
17958   if (parser->num_template_parameter_lists)
17959     parser->type_definition_forbidden_message = saved_message;
17960
17961   if (type == error_mark_node
17962       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17963     {
17964       cp_parser_skip_to_end_of_block_or_statement (parser);
17965       return error_mark_node;
17966     }
17967
17968   /* A typedef-name can also be introduced by an alias-declaration. The
17969      identifier following the using keyword becomes a typedef-name. It has
17970      the same semantics as if it were introduced by the typedef
17971      specifier. In particular, it does not define a new type and it shall
17972      not appear in the type-id.  */
17973
17974   clear_decl_specs (&decl_specs);
17975   decl_specs.type = type;
17976   if (attributes != NULL_TREE)
17977     {
17978       decl_specs.attributes = attributes;
17979       set_and_check_decl_spec_loc (&decl_specs,
17980                                    ds_attribute,
17981                                    attrs_token);
17982     }
17983   set_and_check_decl_spec_loc (&decl_specs,
17984                                ds_typedef,
17985                                using_token);
17986   set_and_check_decl_spec_loc (&decl_specs,
17987                                ds_alias,
17988                                using_token);
17989
17990   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17991   declarator->id_loc = id_location;
17992
17993   member_p = at_class_scope_p ();
17994   if (member_p)
17995     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17996                       NULL_TREE, attributes);
17997   else
17998     decl = start_decl (declarator, &decl_specs, 0,
17999                        attributes, NULL_TREE, &pushed_scope);
18000   if (decl == error_mark_node)
18001     return decl;
18002
18003   // Attach constraints to the alias declaration.
18004   if (flag_concepts && current_template_parms)
18005     {
18006       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18007       tree constr = build_constraints (reqs, NULL_TREE);
18008       set_constraints (decl, constr);
18009     }
18010
18011   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18012
18013   if (pushed_scope)
18014     pop_scope (pushed_scope);
18015
18016   /* If decl is a template, return its TEMPLATE_DECL so that it gets
18017      added into the symbol table; otherwise, return the TYPE_DECL.  */
18018   if (DECL_LANG_SPECIFIC (decl)
18019       && DECL_TEMPLATE_INFO (decl)
18020       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18021     {
18022       decl = DECL_TI_TEMPLATE (decl);
18023       if (member_p)
18024         check_member_template (decl);
18025     }
18026
18027   return decl;
18028 }
18029
18030 /* Parse a using-directive.
18031
18032    using-directive:
18033      using namespace :: [opt] nested-name-specifier [opt]
18034        namespace-name ;  */
18035
18036 static void
18037 cp_parser_using_directive (cp_parser* parser)
18038 {
18039   tree namespace_decl;
18040   tree attribs;
18041
18042   /* Look for the `using' keyword.  */
18043   cp_parser_require_keyword (parser, RID_USING, RT_USING);
18044   /* And the `namespace' keyword.  */
18045   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18046   /* Look for the optional `::' operator.  */
18047   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18048   /* And the optional nested-name-specifier.  */
18049   cp_parser_nested_name_specifier_opt (parser,
18050                                        /*typename_keyword_p=*/false,
18051                                        /*check_dependency_p=*/true,
18052                                        /*type_p=*/false,
18053                                        /*is_declaration=*/true);
18054   /* Get the namespace being used.  */
18055   namespace_decl = cp_parser_namespace_name (parser);
18056   /* And any specified attributes.  */
18057   attribs = cp_parser_attributes_opt (parser);
18058   /* Update the symbol table.  */
18059   parse_using_directive (namespace_decl, attribs);
18060   /* Look for the final `;'.  */
18061   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18062 }
18063
18064 /* Parse an asm-definition.
18065
18066    asm-definition:
18067      asm ( string-literal ) ;
18068
18069    GNU Extension:
18070
18071    asm-definition:
18072      asm volatile [opt] ( string-literal ) ;
18073      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18074      asm volatile [opt] ( string-literal : asm-operand-list [opt]
18075                           : asm-operand-list [opt] ) ;
18076      asm volatile [opt] ( string-literal : asm-operand-list [opt]
18077                           : asm-operand-list [opt]
18078                           : asm-clobber-list [opt] ) ;
18079      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18080                                : asm-clobber-list [opt]
18081                                : asm-goto-list ) ;  */
18082
18083 static void
18084 cp_parser_asm_definition (cp_parser* parser)
18085 {
18086   tree string;
18087   tree outputs = NULL_TREE;
18088   tree inputs = NULL_TREE;
18089   tree clobbers = NULL_TREE;
18090   tree labels = NULL_TREE;
18091   tree asm_stmt;
18092   bool volatile_p = false;
18093   bool extended_p = false;
18094   bool invalid_inputs_p = false;
18095   bool invalid_outputs_p = false;
18096   bool goto_p = false;
18097   required_token missing = RT_NONE;
18098
18099   /* Look for the `asm' keyword.  */
18100   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18101
18102   if (parser->in_function_body
18103       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18104     {
18105       error ("%<asm%> in %<constexpr%> function");
18106       cp_function_chain->invalid_constexpr = true;
18107     }
18108
18109   /* See if the next token is `volatile'.  */
18110   if (cp_parser_allow_gnu_extensions_p (parser)
18111       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18112     {
18113       /* Remember that we saw the `volatile' keyword.  */
18114       volatile_p = true;
18115       /* Consume the token.  */
18116       cp_lexer_consume_token (parser->lexer);
18117     }
18118   if (cp_parser_allow_gnu_extensions_p (parser)
18119       && parser->in_function_body
18120       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18121     {
18122       /* Remember that we saw the `goto' keyword.  */
18123       goto_p = true;
18124       /* Consume the token.  */
18125       cp_lexer_consume_token (parser->lexer);
18126     }
18127   /* Look for the opening `('.  */
18128   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18129     return;
18130   /* Look for the string.  */
18131   string = cp_parser_string_literal (parser, false, false);
18132   if (string == error_mark_node)
18133     {
18134       cp_parser_skip_to_closing_parenthesis (parser, true, false,
18135                                              /*consume_paren=*/true);
18136       return;
18137     }
18138
18139   /* If we're allowing GNU extensions, check for the extended assembly
18140      syntax.  Unfortunately, the `:' tokens need not be separated by
18141      a space in C, and so, for compatibility, we tolerate that here
18142      too.  Doing that means that we have to treat the `::' operator as
18143      two `:' tokens.  */
18144   if (cp_parser_allow_gnu_extensions_p (parser)
18145       && parser->in_function_body
18146       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18147           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18148     {
18149       bool inputs_p = false;
18150       bool clobbers_p = false;
18151       bool labels_p = false;
18152
18153       /* The extended syntax was used.  */
18154       extended_p = true;
18155
18156       /* Look for outputs.  */
18157       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18158         {
18159           /* Consume the `:'.  */
18160           cp_lexer_consume_token (parser->lexer);
18161           /* Parse the output-operands.  */
18162           if (cp_lexer_next_token_is_not (parser->lexer,
18163                                           CPP_COLON)
18164               && cp_lexer_next_token_is_not (parser->lexer,
18165                                              CPP_SCOPE)
18166               && cp_lexer_next_token_is_not (parser->lexer,
18167                                              CPP_CLOSE_PAREN)
18168               && !goto_p)
18169             {
18170               outputs = cp_parser_asm_operand_list (parser);
18171               if (outputs == error_mark_node)
18172                 invalid_outputs_p = true;
18173             }
18174         }
18175       /* If the next token is `::', there are no outputs, and the
18176          next token is the beginning of the inputs.  */
18177       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18178         /* The inputs are coming next.  */
18179         inputs_p = true;
18180
18181       /* Look for inputs.  */
18182       if (inputs_p
18183           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18184         {
18185           /* Consume the `:' or `::'.  */
18186           cp_lexer_consume_token (parser->lexer);
18187           /* Parse the output-operands.  */
18188           if (cp_lexer_next_token_is_not (parser->lexer,
18189                                           CPP_COLON)
18190               && cp_lexer_next_token_is_not (parser->lexer,
18191                                              CPP_SCOPE)
18192               && cp_lexer_next_token_is_not (parser->lexer,
18193                                              CPP_CLOSE_PAREN))
18194             {
18195               inputs = cp_parser_asm_operand_list (parser);
18196               if (inputs == error_mark_node)
18197                 invalid_inputs_p = true;
18198             }
18199         }
18200       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18201         /* The clobbers are coming next.  */
18202         clobbers_p = true;
18203
18204       /* Look for clobbers.  */
18205       if (clobbers_p
18206           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18207         {
18208           clobbers_p = true;
18209           /* Consume the `:' or `::'.  */
18210           cp_lexer_consume_token (parser->lexer);
18211           /* Parse the clobbers.  */
18212           if (cp_lexer_next_token_is_not (parser->lexer,
18213                                           CPP_COLON)
18214               && cp_lexer_next_token_is_not (parser->lexer,
18215                                              CPP_CLOSE_PAREN))
18216             clobbers = cp_parser_asm_clobber_list (parser);
18217         }
18218       else if (goto_p
18219                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18220         /* The labels are coming next.  */
18221         labels_p = true;
18222
18223       /* Look for labels.  */
18224       if (labels_p
18225           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18226         {
18227           labels_p = true;
18228           /* Consume the `:' or `::'.  */
18229           cp_lexer_consume_token (parser->lexer);
18230           /* Parse the labels.  */
18231           labels = cp_parser_asm_label_list (parser);
18232         }
18233
18234       if (goto_p && !labels_p)
18235         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18236     }
18237   else if (goto_p)
18238     missing = RT_COLON_SCOPE;
18239
18240   /* Look for the closing `)'.  */
18241   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18242                           missing ? missing : RT_CLOSE_PAREN))
18243     cp_parser_skip_to_closing_parenthesis (parser, true, false,
18244                                            /*consume_paren=*/true);
18245   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18246
18247   if (!invalid_inputs_p && !invalid_outputs_p)
18248     {
18249       /* Create the ASM_EXPR.  */
18250       if (parser->in_function_body)
18251         {
18252           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18253                                       inputs, clobbers, labels);
18254           /* If the extended syntax was not used, mark the ASM_EXPR.  */
18255           if (!extended_p)
18256             {
18257               tree temp = asm_stmt;
18258               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18259                 temp = TREE_OPERAND (temp, 0);
18260
18261               ASM_INPUT_P (temp) = 1;
18262             }
18263         }
18264       else
18265         symtab->finalize_toplevel_asm (string);
18266     }
18267 }
18268
18269 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18270    type that comes from the decl-specifier-seq.  */
18271
18272 static tree
18273 strip_declarator_types (tree type, cp_declarator *declarator)
18274 {
18275   for (cp_declarator *d = declarator; d;)
18276     switch (d->kind)
18277       {
18278       case cdk_id:
18279       case cdk_error:
18280         d = NULL;
18281         break;
18282
18283       default:
18284         if (TYPE_PTRMEMFUNC_P (type))
18285           type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18286         type = TREE_TYPE (type);
18287         d = d->declarator;
18288         break;
18289       }
18290
18291   return type;
18292 }
18293
18294 /* Declarators [gram.dcl.decl] */
18295
18296 /* Parse an init-declarator.
18297
18298    init-declarator:
18299      declarator initializer [opt]
18300
18301    GNU Extension:
18302
18303    init-declarator:
18304      declarator asm-specification [opt] attributes [opt] initializer [opt]
18305
18306    function-definition:
18307      decl-specifier-seq [opt] declarator ctor-initializer [opt]
18308        function-body
18309      decl-specifier-seq [opt] declarator function-try-block
18310
18311    GNU Extension:
18312
18313    function-definition:
18314      __extension__ function-definition
18315
18316    TM Extension:
18317
18318    function-definition:
18319      decl-specifier-seq [opt] declarator function-transaction-block
18320
18321    The DECL_SPECIFIERS apply to this declarator.  Returns a
18322    representation of the entity declared.  If MEMBER_P is TRUE, then
18323    this declarator appears in a class scope.  The new DECL created by
18324    this declarator is returned.
18325
18326    The CHECKS are access checks that should be performed once we know
18327    what entity is being declared (and, therefore, what classes have
18328    befriended it).
18329
18330    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18331    for a function-definition here as well.  If the declarator is a
18332    declarator for a function-definition, *FUNCTION_DEFINITION_P will
18333    be TRUE upon return.  By that point, the function-definition will
18334    have been completely parsed.
18335
18336    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18337    is FALSE.
18338
18339    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18340    parsed declaration if it is an uninitialized single declarator not followed
18341    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18342    if present, will not be consumed.  If returned, this declarator will be
18343    created with SD_INITIALIZED but will not call cp_finish_decl.
18344
18345    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18346    and there is an initializer, the pointed location_t is set to the
18347    location of the '=' or `(', or '{' in C++11 token introducing the
18348    initializer.  */
18349
18350 static tree
18351 cp_parser_init_declarator (cp_parser* parser,
18352                            cp_decl_specifier_seq *decl_specifiers,
18353                            vec<deferred_access_check, va_gc> *checks,
18354                            bool function_definition_allowed_p,
18355                            bool member_p,
18356                            int declares_class_or_enum,
18357                            bool* function_definition_p,
18358                            tree* maybe_range_for_decl,
18359                            location_t* init_loc,
18360                            tree* auto_result)
18361 {
18362   cp_token *token = NULL, *asm_spec_start_token = NULL,
18363            *attributes_start_token = NULL;
18364   cp_declarator *declarator;
18365   tree prefix_attributes;
18366   tree attributes = NULL;
18367   tree asm_specification;
18368   tree initializer;
18369   tree decl = NULL_TREE;
18370   tree scope;
18371   int is_initialized;
18372   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
18373      initialized with "= ..", CPP_OPEN_PAREN if initialized with
18374      "(...)".  */
18375   enum cpp_ttype initialization_kind;
18376   bool is_direct_init = false;
18377   bool is_non_constant_init;
18378   int ctor_dtor_or_conv_p;
18379   bool friend_p = cp_parser_friend_p (decl_specifiers);
18380   tree pushed_scope = NULL_TREE;
18381   bool range_for_decl_p = false;
18382   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18383   location_t tmp_init_loc = UNKNOWN_LOCATION;
18384
18385   /* Gather the attributes that were provided with the
18386      decl-specifiers.  */
18387   prefix_attributes = decl_specifiers->attributes;
18388
18389   /* Assume that this is not the declarator for a function
18390      definition.  */
18391   if (function_definition_p)
18392     *function_definition_p = false;
18393
18394   /* Default arguments are only permitted for function parameters.  */
18395   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18396     parser->default_arg_ok_p = false;
18397
18398   /* Defer access checks while parsing the declarator; we cannot know
18399      what names are accessible until we know what is being
18400      declared.  */
18401   resume_deferring_access_checks ();
18402
18403   /* Parse the declarator.  */
18404   token = cp_lexer_peek_token (parser->lexer);
18405   declarator
18406     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18407                             &ctor_dtor_or_conv_p,
18408                             /*parenthesized_p=*/NULL,
18409                             member_p, friend_p);
18410   /* Gather up the deferred checks.  */
18411   stop_deferring_access_checks ();
18412
18413   parser->default_arg_ok_p = saved_default_arg_ok_p;
18414
18415   /* If the DECLARATOR was erroneous, there's no need to go
18416      further.  */
18417   if (declarator == cp_error_declarator)
18418     return error_mark_node;
18419
18420   /* Check that the number of template-parameter-lists is OK.  */
18421   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18422                                                        token->location))
18423     return error_mark_node;
18424
18425   if (declares_class_or_enum & 2)
18426     cp_parser_check_for_definition_in_return_type (declarator,
18427                                                    decl_specifiers->type,
18428                                                    decl_specifiers->locations[ds_type_spec]);
18429
18430   /* Figure out what scope the entity declared by the DECLARATOR is
18431      located in.  `grokdeclarator' sometimes changes the scope, so
18432      we compute it now.  */
18433   scope = get_scope_of_declarator (declarator);
18434
18435   /* Perform any lookups in the declared type which were thought to be
18436      dependent, but are not in the scope of the declarator.  */
18437   decl_specifiers->type
18438     = maybe_update_decl_type (decl_specifiers->type, scope);
18439
18440   /* If we're allowing GNU extensions, look for an
18441      asm-specification.  */
18442   if (cp_parser_allow_gnu_extensions_p (parser))
18443     {
18444       /* Look for an asm-specification.  */
18445       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18446       asm_specification = cp_parser_asm_specification_opt (parser);
18447     }
18448   else
18449     asm_specification = NULL_TREE;
18450
18451   /* Look for attributes.  */
18452   attributes_start_token = cp_lexer_peek_token (parser->lexer);
18453   attributes = cp_parser_attributes_opt (parser);
18454
18455   /* Peek at the next token.  */
18456   token = cp_lexer_peek_token (parser->lexer);
18457
18458   bool bogus_implicit_tmpl = false;
18459
18460   if (function_declarator_p (declarator))
18461     {
18462       /* Check to see if the token indicates the start of a
18463          function-definition.  */
18464       if (cp_parser_token_starts_function_definition_p (token))
18465         {
18466           if (!function_definition_allowed_p)
18467             {
18468               /* If a function-definition should not appear here, issue an
18469                  error message.  */
18470               cp_parser_error (parser,
18471                                "a function-definition is not allowed here");
18472               return error_mark_node;
18473             }
18474
18475           location_t func_brace_location
18476             = cp_lexer_peek_token (parser->lexer)->location;
18477
18478           /* Neither attributes nor an asm-specification are allowed
18479              on a function-definition.  */
18480           if (asm_specification)
18481             error_at (asm_spec_start_token->location,
18482                       "an asm-specification is not allowed "
18483                       "on a function-definition");
18484           if (attributes)
18485             error_at (attributes_start_token->location,
18486                       "attributes are not allowed "
18487                       "on a function-definition");
18488           /* This is a function-definition.  */
18489           *function_definition_p = true;
18490
18491           /* Parse the function definition.  */
18492           if (member_p)
18493             decl = cp_parser_save_member_function_body (parser,
18494                                                         decl_specifiers,
18495                                                         declarator,
18496                                                         prefix_attributes);
18497           else
18498             decl =
18499               (cp_parser_function_definition_from_specifiers_and_declarator
18500                (parser, decl_specifiers, prefix_attributes, declarator));
18501
18502           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18503             {
18504               /* This is where the prologue starts...  */
18505               DECL_STRUCT_FUNCTION (decl)->function_start_locus
18506                 = func_brace_location;
18507             }
18508
18509           return decl;
18510         }
18511     }
18512   else if (parser->fully_implicit_function_template_p)
18513     {
18514       /* A non-template declaration involving a function parameter list
18515          containing an implicit template parameter will be made into a
18516          template.  If the resulting declaration is not going to be an
18517          actual function then finish the template scope here to prevent it.
18518          An error message will be issued once we have a decl to talk about.
18519
18520          FIXME probably we should do type deduction rather than create an
18521          implicit template, but the standard currently doesn't allow it. */
18522       bogus_implicit_tmpl = true;
18523       finish_fully_implicit_template (parser, NULL_TREE);
18524     }
18525
18526   /* [dcl.dcl]
18527
18528      Only in function declarations for constructors, destructors, and
18529      type conversions can the decl-specifier-seq be omitted.
18530
18531      We explicitly postpone this check past the point where we handle
18532      function-definitions because we tolerate function-definitions
18533      that are missing their return types in some modes.  */
18534   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18535     {
18536       cp_parser_error (parser,
18537                        "expected constructor, destructor, or type conversion");
18538       return error_mark_node;
18539     }
18540
18541   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
18542   if (token->type == CPP_EQ
18543       || token->type == CPP_OPEN_PAREN
18544       || token->type == CPP_OPEN_BRACE)
18545     {
18546       is_initialized = SD_INITIALIZED;
18547       initialization_kind = token->type;
18548       if (maybe_range_for_decl)
18549         *maybe_range_for_decl = error_mark_node;
18550       tmp_init_loc = token->location;
18551       if (init_loc && *init_loc == UNKNOWN_LOCATION)
18552         *init_loc = tmp_init_loc;
18553
18554       if (token->type == CPP_EQ
18555           && function_declarator_p (declarator))
18556         {
18557           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18558           if (t2->keyword == RID_DEFAULT)
18559             is_initialized = SD_DEFAULTED;
18560           else if (t2->keyword == RID_DELETE)
18561             is_initialized = SD_DELETED;
18562         }
18563     }
18564   else
18565     {
18566       /* If the init-declarator isn't initialized and isn't followed by a
18567          `,' or `;', it's not a valid init-declarator.  */
18568       if (token->type != CPP_COMMA
18569           && token->type != CPP_SEMICOLON)
18570         {
18571           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18572             range_for_decl_p = true;
18573           else
18574             {
18575               if (!maybe_range_for_decl)
18576                 cp_parser_error (parser, "expected initializer");
18577               return error_mark_node;
18578             }
18579         }
18580       is_initialized = SD_UNINITIALIZED;
18581       initialization_kind = CPP_EOF;
18582     }
18583
18584   /* Because start_decl has side-effects, we should only call it if we
18585      know we're going ahead.  By this point, we know that we cannot
18586      possibly be looking at any other construct.  */
18587   cp_parser_commit_to_tentative_parse (parser);
18588
18589   /* Enter the newly declared entry in the symbol table.  If we're
18590      processing a declaration in a class-specifier, we wait until
18591      after processing the initializer.  */
18592   if (!member_p)
18593     {
18594       if (parser->in_unbraced_linkage_specification_p)
18595         decl_specifiers->storage_class = sc_extern;
18596       decl = start_decl (declarator, decl_specifiers,
18597                          range_for_decl_p? SD_INITIALIZED : is_initialized,
18598                          attributes, prefix_attributes, &pushed_scope);
18599       cp_finalize_omp_declare_simd (parser, decl);
18600       cp_finalize_oacc_routine (parser, decl, false);
18601       /* Adjust location of decl if declarator->id_loc is more appropriate:
18602          set, and decl wasn't merged with another decl, in which case its
18603          location would be different from input_location, and more accurate.  */
18604       if (DECL_P (decl)
18605           && declarator->id_loc != UNKNOWN_LOCATION
18606           && DECL_SOURCE_LOCATION (decl) == input_location)
18607         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18608     }
18609   else if (scope)
18610     /* Enter the SCOPE.  That way unqualified names appearing in the
18611        initializer will be looked up in SCOPE.  */
18612     pushed_scope = push_scope (scope);
18613
18614   /* Perform deferred access control checks, now that we know in which
18615      SCOPE the declared entity resides.  */
18616   if (!member_p && decl)
18617     {
18618       tree saved_current_function_decl = NULL_TREE;
18619
18620       /* If the entity being declared is a function, pretend that we
18621          are in its scope.  If it is a `friend', it may have access to
18622          things that would not otherwise be accessible.  */
18623       if (TREE_CODE (decl) == FUNCTION_DECL)
18624         {
18625           saved_current_function_decl = current_function_decl;
18626           current_function_decl = decl;
18627         }
18628
18629       /* Perform access checks for template parameters.  */
18630       cp_parser_perform_template_parameter_access_checks (checks);
18631
18632       /* Perform the access control checks for the declarator and the
18633          decl-specifiers.  */
18634       perform_deferred_access_checks (tf_warning_or_error);
18635
18636       /* Restore the saved value.  */
18637       if (TREE_CODE (decl) == FUNCTION_DECL)
18638         current_function_decl = saved_current_function_decl;
18639     }
18640
18641   /* Parse the initializer.  */
18642   initializer = NULL_TREE;
18643   is_direct_init = false;
18644   is_non_constant_init = true;
18645   if (is_initialized)
18646     {
18647       if (function_declarator_p (declarator))
18648         {
18649            if (initialization_kind == CPP_EQ)
18650              initializer = cp_parser_pure_specifier (parser);
18651            else
18652              {
18653                /* If the declaration was erroneous, we don't really
18654                   know what the user intended, so just silently
18655                   consume the initializer.  */
18656                if (decl != error_mark_node)
18657                  error_at (tmp_init_loc, "initializer provided for function");
18658                cp_parser_skip_to_closing_parenthesis (parser,
18659                                                       /*recovering=*/true,
18660                                                       /*or_comma=*/false,
18661                                                       /*consume_paren=*/true);
18662              }
18663         }
18664       else
18665         {
18666           /* We want to record the extra mangling scope for in-class
18667              initializers of class members and initializers of static data
18668              member templates.  The former involves deferring
18669              parsing of the initializer until end of class as with default
18670              arguments.  So right here we only handle the latter.  */
18671           if (!member_p && processing_template_decl)
18672             start_lambda_scope (decl);
18673           initializer = cp_parser_initializer (parser,
18674                                                &is_direct_init,
18675                                                &is_non_constant_init);
18676           if (!member_p && processing_template_decl)
18677             finish_lambda_scope ();
18678           if (initializer == error_mark_node)
18679             cp_parser_skip_to_end_of_statement (parser);
18680         }
18681     }
18682
18683   /* The old parser allows attributes to appear after a parenthesized
18684      initializer.  Mark Mitchell proposed removing this functionality
18685      on the GCC mailing lists on 2002-08-13.  This parser accepts the
18686      attributes -- but ignores them.  */
18687   if (cp_parser_allow_gnu_extensions_p (parser)
18688       && initialization_kind == CPP_OPEN_PAREN)
18689     if (cp_parser_attributes_opt (parser))
18690       warning (OPT_Wattributes,
18691                "attributes after parenthesized initializer ignored");
18692
18693   /* And now complain about a non-function implicit template.  */
18694   if (bogus_implicit_tmpl && decl != error_mark_node)
18695     error_at (DECL_SOURCE_LOCATION (decl),
18696               "non-function %qD declared as implicit template", decl);
18697
18698   /* For an in-class declaration, use `grokfield' to create the
18699      declaration.  */
18700   if (member_p)
18701     {
18702       if (pushed_scope)
18703         {
18704           pop_scope (pushed_scope);
18705           pushed_scope = NULL_TREE;
18706         }
18707       decl = grokfield (declarator, decl_specifiers,
18708                         initializer, !is_non_constant_init,
18709                         /*asmspec=*/NULL_TREE,
18710                         chainon (attributes, prefix_attributes));
18711       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18712         cp_parser_save_default_args (parser, decl);
18713       cp_finalize_omp_declare_simd (parser, decl);
18714       cp_finalize_oacc_routine (parser, decl, false);
18715     }
18716
18717   /* Finish processing the declaration.  But, skip member
18718      declarations.  */
18719   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18720     {
18721       cp_finish_decl (decl,
18722                       initializer, !is_non_constant_init,
18723                       asm_specification,
18724                       /* If the initializer is in parentheses, then this is
18725                          a direct-initialization, which means that an
18726                          `explicit' constructor is OK.  Otherwise, an
18727                          `explicit' constructor cannot be used.  */
18728                       ((is_direct_init || !is_initialized)
18729                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18730     }
18731   else if ((cxx_dialect != cxx98) && friend_p
18732            && decl && TREE_CODE (decl) == FUNCTION_DECL)
18733     /* Core issue #226 (C++0x only): A default template-argument
18734        shall not be specified in a friend class template
18735        declaration. */
18736     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
18737                              /*is_partial=*/false, /*is_friend_decl=*/1);
18738
18739   if (!friend_p && pushed_scope)
18740     pop_scope (pushed_scope);
18741
18742   if (function_declarator_p (declarator)
18743       && parser->fully_implicit_function_template_p)
18744     {
18745       if (member_p)
18746         decl = finish_fully_implicit_template (parser, decl);
18747       else
18748         finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18749     }
18750
18751   if (auto_result && is_initialized && decl_specifiers->type
18752       && type_uses_auto (decl_specifiers->type))
18753     *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18754
18755   return decl;
18756 }
18757
18758 /* Parse a declarator.
18759
18760    declarator:
18761      direct-declarator
18762      ptr-operator declarator
18763
18764    abstract-declarator:
18765      ptr-operator abstract-declarator [opt]
18766      direct-abstract-declarator
18767
18768    GNU Extensions:
18769
18770    declarator:
18771      attributes [opt] direct-declarator
18772      attributes [opt] ptr-operator declarator
18773
18774    abstract-declarator:
18775      attributes [opt] ptr-operator abstract-declarator [opt]
18776      attributes [opt] direct-abstract-declarator
18777
18778    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18779    detect constructor, destructor or conversion operators. It is set
18780    to -1 if the declarator is a name, and +1 if it is a
18781    function. Otherwise it is set to zero. Usually you just want to
18782    test for >0, but internally the negative value is used.
18783
18784    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18785    a decl-specifier-seq unless it declares a constructor, destructor,
18786    or conversion.  It might seem that we could check this condition in
18787    semantic analysis, rather than parsing, but that makes it difficult
18788    to handle something like `f()'.  We want to notice that there are
18789    no decl-specifiers, and therefore realize that this is an
18790    expression, not a declaration.)
18791
18792    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18793    the declarator is a direct-declarator of the form "(...)".
18794
18795    MEMBER_P is true iff this declarator is a member-declarator.
18796
18797    FRIEND_P is true iff this declarator is a friend.  */
18798
18799 static cp_declarator *
18800 cp_parser_declarator (cp_parser* parser,
18801                       cp_parser_declarator_kind dcl_kind,
18802                       int* ctor_dtor_or_conv_p,
18803                       bool* parenthesized_p,
18804                       bool member_p, bool friend_p)
18805 {
18806   cp_declarator *declarator;
18807   enum tree_code code;
18808   cp_cv_quals cv_quals;
18809   tree class_type;
18810   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18811
18812   /* Assume this is not a constructor, destructor, or type-conversion
18813      operator.  */
18814   if (ctor_dtor_or_conv_p)
18815     *ctor_dtor_or_conv_p = 0;
18816
18817   if (cp_parser_allow_gnu_extensions_p (parser))
18818     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18819
18820   /* Check for the ptr-operator production.  */
18821   cp_parser_parse_tentatively (parser);
18822   /* Parse the ptr-operator.  */
18823   code = cp_parser_ptr_operator (parser,
18824                                  &class_type,
18825                                  &cv_quals,
18826                                  &std_attributes);
18827
18828   /* If that worked, then we have a ptr-operator.  */
18829   if (cp_parser_parse_definitely (parser))
18830     {
18831       /* If a ptr-operator was found, then this declarator was not
18832          parenthesized.  */
18833       if (parenthesized_p)
18834         *parenthesized_p = true;
18835       /* The dependent declarator is optional if we are parsing an
18836          abstract-declarator.  */
18837       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18838         cp_parser_parse_tentatively (parser);
18839
18840       /* Parse the dependent declarator.  */
18841       declarator = cp_parser_declarator (parser, dcl_kind,
18842                                          /*ctor_dtor_or_conv_p=*/NULL,
18843                                          /*parenthesized_p=*/NULL,
18844                                          /*member_p=*/false,
18845                                          friend_p);
18846
18847       /* If we are parsing an abstract-declarator, we must handle the
18848          case where the dependent declarator is absent.  */
18849       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18850           && !cp_parser_parse_definitely (parser))
18851         declarator = NULL;
18852
18853       declarator = cp_parser_make_indirect_declarator
18854         (code, class_type, cv_quals, declarator, std_attributes);
18855     }
18856   /* Everything else is a direct-declarator.  */
18857   else
18858     {
18859       if (parenthesized_p)
18860         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18861                                                    CPP_OPEN_PAREN);
18862       declarator = cp_parser_direct_declarator (parser, dcl_kind,
18863                                                 ctor_dtor_or_conv_p,
18864                                                 member_p, friend_p);
18865     }
18866
18867   if (gnu_attributes && declarator && declarator != cp_error_declarator)
18868     declarator->attributes = gnu_attributes;
18869   return declarator;
18870 }
18871
18872 /* Parse a direct-declarator or direct-abstract-declarator.
18873
18874    direct-declarator:
18875      declarator-id
18876      direct-declarator ( parameter-declaration-clause )
18877        cv-qualifier-seq [opt]
18878        ref-qualifier [opt]
18879        exception-specification [opt]
18880      direct-declarator [ constant-expression [opt] ]
18881      ( declarator )
18882
18883    direct-abstract-declarator:
18884      direct-abstract-declarator [opt]
18885        ( parameter-declaration-clause )
18886        cv-qualifier-seq [opt]
18887        ref-qualifier [opt]
18888        exception-specification [opt]
18889      direct-abstract-declarator [opt] [ constant-expression [opt] ]
18890      ( abstract-declarator )
18891
18892    Returns a representation of the declarator.  DCL_KIND is
18893    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18894    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
18895    we are parsing a direct-declarator.  It is
18896    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18897    of ambiguity we prefer an abstract declarator, as per
18898    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18899    as for cp_parser_declarator.  */
18900
18901 static cp_declarator *
18902 cp_parser_direct_declarator (cp_parser* parser,
18903                              cp_parser_declarator_kind dcl_kind,
18904                              int* ctor_dtor_or_conv_p,
18905                              bool member_p, bool friend_p)
18906 {
18907   cp_token *token;
18908   cp_declarator *declarator = NULL;
18909   tree scope = NULL_TREE;
18910   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18911   bool saved_in_declarator_p = parser->in_declarator_p;
18912   bool first = true;
18913   tree pushed_scope = NULL_TREE;
18914
18915   while (true)
18916     {
18917       /* Peek at the next token.  */
18918       token = cp_lexer_peek_token (parser->lexer);
18919       if (token->type == CPP_OPEN_PAREN)
18920         {
18921           /* This is either a parameter-declaration-clause, or a
18922              parenthesized declarator. When we know we are parsing a
18923              named declarator, it must be a parenthesized declarator
18924              if FIRST is true. For instance, `(int)' is a
18925              parameter-declaration-clause, with an omitted
18926              direct-abstract-declarator. But `((*))', is a
18927              parenthesized abstract declarator. Finally, when T is a
18928              template parameter `(T)' is a
18929              parameter-declaration-clause, and not a parenthesized
18930              named declarator.
18931
18932              We first try and parse a parameter-declaration-clause,
18933              and then try a nested declarator (if FIRST is true).
18934
18935              It is not an error for it not to be a
18936              parameter-declaration-clause, even when FIRST is
18937              false. Consider,
18938
18939                int i (int);
18940                int i (3);
18941
18942              The first is the declaration of a function while the
18943              second is the definition of a variable, including its
18944              initializer.
18945
18946              Having seen only the parenthesis, we cannot know which of
18947              these two alternatives should be selected.  Even more
18948              complex are examples like:
18949
18950                int i (int (a));
18951                int i (int (3));
18952
18953              The former is a function-declaration; the latter is a
18954              variable initialization.
18955
18956              Thus again, we try a parameter-declaration-clause, and if
18957              that fails, we back out and return.  */
18958
18959           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18960             {
18961               tree params;
18962               bool is_declarator = false;
18963
18964               /* In a member-declarator, the only valid interpretation
18965                  of a parenthesis is the start of a
18966                  parameter-declaration-clause.  (It is invalid to
18967                  initialize a static data member with a parenthesized
18968                  initializer; only the "=" form of initialization is
18969                  permitted.)  */
18970               if (!member_p)
18971                 cp_parser_parse_tentatively (parser);
18972
18973               /* Consume the `('.  */
18974               cp_lexer_consume_token (parser->lexer);
18975               if (first)
18976                 {
18977                   /* If this is going to be an abstract declarator, we're
18978                      in a declarator and we can't have default args.  */
18979                   parser->default_arg_ok_p = false;
18980                   parser->in_declarator_p = true;
18981                 }
18982
18983               begin_scope (sk_function_parms, NULL_TREE);
18984
18985               /* Parse the parameter-declaration-clause.  */
18986               params = cp_parser_parameter_declaration_clause (parser);
18987
18988               /* Consume the `)'.  */
18989               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18990
18991               /* If all went well, parse the cv-qualifier-seq,
18992                  ref-qualifier and the exception-specification.  */
18993               if (member_p || cp_parser_parse_definitely (parser))
18994                 {
18995                   cp_cv_quals cv_quals;
18996                   cp_virt_specifiers virt_specifiers;
18997                   cp_ref_qualifier ref_qual;
18998                   tree exception_specification;
18999                   tree late_return;
19000                   tree attrs;
19001                   bool memfn = (member_p || (pushed_scope
19002                                              && CLASS_TYPE_P (pushed_scope)));
19003
19004                   is_declarator = true;
19005
19006                   if (ctor_dtor_or_conv_p)
19007                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19008                   first = false;
19009
19010                   /* Parse the cv-qualifier-seq.  */
19011                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19012                   /* Parse the ref-qualifier. */
19013                   ref_qual = cp_parser_ref_qualifier_opt (parser);
19014                   /* Parse the tx-qualifier.  */
19015                   tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19016                   /* And the exception-specification.  */
19017                   exception_specification
19018                     = cp_parser_exception_specification_opt (parser);
19019
19020                   attrs = cp_parser_std_attribute_spec_seq (parser);
19021
19022                   /* In here, we handle cases where attribute is used after
19023                      the function declaration.  For example:
19024                      void func (int x) __attribute__((vector(..)));  */
19025                   tree gnu_attrs = NULL_TREE;
19026                   if (flag_cilkplus
19027                       && cp_next_tokens_can_be_gnu_attribute_p (parser))
19028                     {
19029                       cp_parser_parse_tentatively (parser);
19030                       tree attr = cp_parser_gnu_attributes_opt (parser);
19031                       if (cp_lexer_next_token_is_not (parser->lexer,
19032                                                       CPP_SEMICOLON)
19033                           && cp_lexer_next_token_is_not (parser->lexer,
19034                                                          CPP_OPEN_BRACE))
19035                         cp_parser_abort_tentative_parse (parser);
19036                       else if (!cp_parser_parse_definitely (parser))
19037                         ;
19038                       else
19039                         gnu_attrs = attr;
19040                     }
19041                   tree requires_clause = NULL_TREE;
19042                   late_return = (cp_parser_late_return_type_opt
19043                                  (parser, declarator, requires_clause,
19044                                   memfn ? cv_quals : -1));
19045
19046                   /* Parse the virt-specifier-seq.  */
19047                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19048
19049                   /* Create the function-declarator.  */
19050                   declarator = make_call_declarator (declarator,
19051                                                      params,
19052                                                      cv_quals,
19053                                                      virt_specifiers,
19054                                                      ref_qual,
19055                                                      tx_qual,
19056                                                      exception_specification,
19057                                                      late_return,
19058                                                      requires_clause);
19059                   declarator->std_attributes = attrs;
19060                   declarator->attributes = gnu_attrs;
19061                   /* Any subsequent parameter lists are to do with
19062                      return type, so are not those of the declared
19063                      function.  */
19064                   parser->default_arg_ok_p = false;
19065                 }
19066
19067               /* Remove the function parms from scope.  */
19068               pop_bindings_and_leave_scope ();
19069
19070               if (is_declarator)
19071                 /* Repeat the main loop.  */
19072                 continue;
19073             }
19074
19075           /* If this is the first, we can try a parenthesized
19076              declarator.  */
19077           if (first)
19078             {
19079               bool saved_in_type_id_in_expr_p;
19080
19081               parser->default_arg_ok_p = saved_default_arg_ok_p;
19082               parser->in_declarator_p = saved_in_declarator_p;
19083
19084               /* Consume the `('.  */
19085               cp_lexer_consume_token (parser->lexer);
19086               /* Parse the nested declarator.  */
19087               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19088               parser->in_type_id_in_expr_p = true;
19089               declarator
19090                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19091                                         /*parenthesized_p=*/NULL,
19092                                         member_p, friend_p);
19093               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19094               first = false;
19095               /* Expect a `)'.  */
19096               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19097                 declarator = cp_error_declarator;
19098               if (declarator == cp_error_declarator)
19099                 break;
19100
19101               goto handle_declarator;
19102             }
19103           /* Otherwise, we must be done.  */
19104           else
19105             break;
19106         }
19107       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19108                && token->type == CPP_OPEN_SQUARE
19109                && !cp_next_tokens_can_be_attribute_p (parser))
19110         {
19111           /* Parse an array-declarator.  */
19112           tree bounds, attrs;
19113
19114           if (ctor_dtor_or_conv_p)
19115             *ctor_dtor_or_conv_p = 0;
19116
19117           first = false;
19118           parser->default_arg_ok_p = false;
19119           parser->in_declarator_p = true;
19120           /* Consume the `['.  */
19121           cp_lexer_consume_token (parser->lexer);
19122           /* Peek at the next token.  */
19123           token = cp_lexer_peek_token (parser->lexer);
19124           /* If the next token is `]', then there is no
19125              constant-expression.  */
19126           if (token->type != CPP_CLOSE_SQUARE)
19127             {
19128               bool non_constant_p;
19129               bounds
19130                 = cp_parser_constant_expression (parser,
19131                                                  /*allow_non_constant=*/true,
19132                                                  &non_constant_p);
19133               if (!non_constant_p)
19134                 /* OK */;
19135               else if (error_operand_p (bounds))
19136                 /* Already gave an error.  */;
19137               else if (!parser->in_function_body
19138                        || current_binding_level->kind == sk_function_parms)
19139                 {
19140                   /* Normally, the array bound must be an integral constant
19141                      expression.  However, as an extension, we allow VLAs
19142                      in function scopes as long as they aren't part of a
19143                      parameter declaration.  */
19144                   cp_parser_error (parser,
19145                                    "array bound is not an integer constant");
19146                   bounds = error_mark_node;
19147                 }
19148               else if (processing_template_decl
19149                        && !type_dependent_expression_p (bounds))
19150                 {
19151                   /* Remember this wasn't a constant-expression.  */
19152                   bounds = build_nop (TREE_TYPE (bounds), bounds);
19153                   TREE_SIDE_EFFECTS (bounds) = 1;
19154                 }
19155             }
19156           else
19157             bounds = NULL_TREE;
19158           /* Look for the closing `]'.  */
19159           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19160             {
19161               declarator = cp_error_declarator;
19162               break;
19163             }
19164
19165           attrs = cp_parser_std_attribute_spec_seq (parser);
19166           declarator = make_array_declarator (declarator, bounds);
19167           declarator->std_attributes = attrs;
19168         }
19169       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19170         {
19171           {
19172             tree qualifying_scope;
19173             tree unqualified_name;
19174             tree attrs;
19175             special_function_kind sfk;
19176             bool abstract_ok;
19177             bool pack_expansion_p = false;
19178             cp_token *declarator_id_start_token;
19179
19180             /* Parse a declarator-id */
19181             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19182             if (abstract_ok)
19183               {
19184                 cp_parser_parse_tentatively (parser);
19185
19186                 /* If we see an ellipsis, we should be looking at a
19187                    parameter pack. */
19188                 if (token->type == CPP_ELLIPSIS)
19189                   {
19190                     /* Consume the `...' */
19191                     cp_lexer_consume_token (parser->lexer);
19192
19193                     pack_expansion_p = true;
19194                   }
19195               }
19196
19197             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19198             unqualified_name
19199               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19200             qualifying_scope = parser->scope;
19201             if (abstract_ok)
19202               {
19203                 bool okay = false;
19204
19205                 if (!unqualified_name && pack_expansion_p)
19206                   {
19207                     /* Check whether an error occurred. */
19208                     okay = !cp_parser_error_occurred (parser);
19209
19210                     /* We already consumed the ellipsis to mark a
19211                        parameter pack, but we have no way to report it,
19212                        so abort the tentative parse. We will be exiting
19213                        immediately anyway. */
19214                     cp_parser_abort_tentative_parse (parser);
19215                   }
19216                 else
19217                   okay = cp_parser_parse_definitely (parser);
19218
19219                 if (!okay)
19220                   unqualified_name = error_mark_node;
19221                 else if (unqualified_name
19222                          && (qualifying_scope
19223                              || (!identifier_p (unqualified_name))))
19224                   {
19225                     cp_parser_error (parser, "expected unqualified-id");
19226                     unqualified_name = error_mark_node;
19227                   }
19228               }
19229
19230             if (!unqualified_name)
19231               return NULL;
19232             if (unqualified_name == error_mark_node)
19233               {
19234                 declarator = cp_error_declarator;
19235                 pack_expansion_p = false;
19236                 declarator->parameter_pack_p = false;
19237                 break;
19238               }
19239
19240             attrs = cp_parser_std_attribute_spec_seq (parser);
19241
19242             if (qualifying_scope && at_namespace_scope_p ()
19243                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19244               {
19245                 /* In the declaration of a member of a template class
19246                    outside of the class itself, the SCOPE will sometimes
19247                    be a TYPENAME_TYPE.  For example, given:
19248
19249                    template <typename T>
19250                    int S<T>::R::i = 3;
19251
19252                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
19253                    this context, we must resolve S<T>::R to an ordinary
19254                    type, rather than a typename type.
19255
19256                    The reason we normally avoid resolving TYPENAME_TYPEs
19257                    is that a specialization of `S' might render
19258                    `S<T>::R' not a type.  However, if `S' is
19259                    specialized, then this `i' will not be used, so there
19260                    is no harm in resolving the types here.  */
19261                 tree type;
19262
19263                 /* Resolve the TYPENAME_TYPE.  */
19264                 type = resolve_typename_type (qualifying_scope,
19265                                               /*only_current_p=*/false);
19266                 /* If that failed, the declarator is invalid.  */
19267                 if (TREE_CODE (type) == TYPENAME_TYPE)
19268                   {
19269                     if (typedef_variant_p (type))
19270                       error_at (declarator_id_start_token->location,
19271                                 "cannot define member of dependent typedef "
19272                                 "%qT", type);
19273                     else
19274                       error_at (declarator_id_start_token->location,
19275                                 "%<%T::%E%> is not a type",
19276                                 TYPE_CONTEXT (qualifying_scope),
19277                                 TYPE_IDENTIFIER (qualifying_scope));
19278                   }
19279                 qualifying_scope = type;
19280               }
19281
19282             sfk = sfk_none;
19283
19284             if (unqualified_name)
19285               {
19286                 tree class_type;
19287
19288                 if (qualifying_scope
19289                     && CLASS_TYPE_P (qualifying_scope))
19290                   class_type = qualifying_scope;
19291                 else
19292                   class_type = current_class_type;
19293
19294                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19295                   {
19296                     tree name_type = TREE_TYPE (unqualified_name);
19297                     if (class_type && same_type_p (name_type, class_type))
19298                       {
19299                         if (qualifying_scope
19300                             && CLASSTYPE_USE_TEMPLATE (name_type))
19301                           {
19302                             error_at (declarator_id_start_token->location,
19303                                       "invalid use of constructor as a template");
19304                             inform (declarator_id_start_token->location,
19305                                     "use %<%T::%D%> instead of %<%T::%D%> to "
19306                                     "name the constructor in a qualified name",
19307                                     class_type,
19308                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19309                                     class_type, name_type);
19310                             declarator = cp_error_declarator;
19311                             break;
19312                           }
19313                         else
19314                           unqualified_name = constructor_name (class_type);
19315                       }
19316                     else
19317                       {
19318                         /* We do not attempt to print the declarator
19319                            here because we do not have enough
19320                            information about its original syntactic
19321                            form.  */
19322                         cp_parser_error (parser, "invalid declarator");
19323                         declarator = cp_error_declarator;
19324                         break;
19325                       }
19326                   }
19327
19328                 if (class_type)
19329                   {
19330                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19331                       sfk = sfk_destructor;
19332                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19333                       sfk = sfk_conversion;
19334                     else if (/* There's no way to declare a constructor
19335                                 for an anonymous type, even if the type
19336                                 got a name for linkage purposes.  */
19337                              !TYPE_WAS_ANONYMOUS (class_type)
19338                              /* Handle correctly (c++/19200):
19339
19340                                 struct S {
19341                                   struct T{};
19342                                   friend void S(T);
19343                                 };
19344
19345                                 and also:
19346
19347                                 namespace N {
19348                                   void S();
19349                                 }
19350
19351                                 struct S {
19352                                   friend void N::S();
19353                                 };  */
19354                              && !(friend_p
19355                                   && class_type != qualifying_scope)
19356                              && constructor_name_p (unqualified_name,
19357                                                     class_type))
19358                       {
19359                         unqualified_name = constructor_name (class_type);
19360                         sfk = sfk_constructor;
19361                       }
19362                     else if (is_overloaded_fn (unqualified_name)
19363                              && DECL_CONSTRUCTOR_P (get_first_fn
19364                                                     (unqualified_name)))
19365                       sfk = sfk_constructor;
19366
19367                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
19368                       *ctor_dtor_or_conv_p = -1;
19369                   }
19370               }
19371             declarator = make_id_declarator (qualifying_scope,
19372                                              unqualified_name,
19373                                              sfk);
19374             declarator->std_attributes = attrs;
19375             declarator->id_loc = token->location;
19376             declarator->parameter_pack_p = pack_expansion_p;
19377
19378             if (pack_expansion_p)
19379               maybe_warn_variadic_templates ();
19380           }
19381
19382         handle_declarator:;
19383           scope = get_scope_of_declarator (declarator);
19384           if (scope)
19385             {
19386               /* Any names that appear after the declarator-id for a
19387                  member are looked up in the containing scope.  */
19388               if (at_function_scope_p ())
19389                 {
19390                   /* But declarations with qualified-ids can't appear in a
19391                      function.  */
19392                   cp_parser_error (parser, "qualified-id in declaration");
19393                   declarator = cp_error_declarator;
19394                   break;
19395                 }
19396               pushed_scope = push_scope (scope);
19397             }
19398           parser->in_declarator_p = true;
19399           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19400               || (declarator && declarator->kind == cdk_id))
19401             /* Default args are only allowed on function
19402                declarations.  */
19403             parser->default_arg_ok_p = saved_default_arg_ok_p;
19404           else
19405             parser->default_arg_ok_p = false;
19406
19407           first = false;
19408         }
19409       /* We're done.  */
19410       else
19411         break;
19412     }
19413
19414   /* For an abstract declarator, we might wind up with nothing at this
19415      point.  That's an error; the declarator is not optional.  */
19416   if (!declarator)
19417     cp_parser_error (parser, "expected declarator");
19418
19419   /* If we entered a scope, we must exit it now.  */
19420   if (pushed_scope)
19421     pop_scope (pushed_scope);
19422
19423   parser->default_arg_ok_p = saved_default_arg_ok_p;
19424   parser->in_declarator_p = saved_in_declarator_p;
19425
19426   return declarator;
19427 }
19428
19429 /* Parse a ptr-operator.
19430
19431    ptr-operator:
19432      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19433      * cv-qualifier-seq [opt]
19434      &
19435      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19436      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19437
19438    GNU Extension:
19439
19440    ptr-operator:
19441      & cv-qualifier-seq [opt]
19442
19443    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19444    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19445    an rvalue reference. In the case of a pointer-to-member, *TYPE is
19446    filled in with the TYPE containing the member.  *CV_QUALS is
19447    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19448    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
19449    Note that the tree codes returned by this function have nothing
19450    to do with the types of trees that will be eventually be created
19451    to represent the pointer or reference type being parsed. They are
19452    just constants with suggestive names. */
19453 static enum tree_code
19454 cp_parser_ptr_operator (cp_parser* parser,
19455                         tree* type,
19456                         cp_cv_quals *cv_quals,
19457                         tree *attributes)
19458 {
19459   enum tree_code code = ERROR_MARK;
19460   cp_token *token;
19461   tree attrs = NULL_TREE;
19462
19463   /* Assume that it's not a pointer-to-member.  */
19464   *type = NULL_TREE;
19465   /* And that there are no cv-qualifiers.  */
19466   *cv_quals = TYPE_UNQUALIFIED;
19467
19468   /* Peek at the next token.  */
19469   token = cp_lexer_peek_token (parser->lexer);
19470
19471   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
19472   if (token->type == CPP_MULT)
19473     code = INDIRECT_REF;
19474   else if (token->type == CPP_AND)
19475     code = ADDR_EXPR;
19476   else if ((cxx_dialect != cxx98) &&
19477            token->type == CPP_AND_AND) /* C++0x only */
19478     code = NON_LVALUE_EXPR;
19479
19480   if (code != ERROR_MARK)
19481     {
19482       /* Consume the `*', `&' or `&&'.  */
19483       cp_lexer_consume_token (parser->lexer);
19484
19485       /* A `*' can be followed by a cv-qualifier-seq, and so can a
19486          `&', if we are allowing GNU extensions.  (The only qualifier
19487          that can legally appear after `&' is `restrict', but that is
19488          enforced during semantic analysis.  */
19489       if (code == INDIRECT_REF
19490           || cp_parser_allow_gnu_extensions_p (parser))
19491         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19492
19493       attrs = cp_parser_std_attribute_spec_seq (parser);
19494       if (attributes != NULL)
19495         *attributes = attrs;
19496     }
19497   else
19498     {
19499       /* Try the pointer-to-member case.  */
19500       cp_parser_parse_tentatively (parser);
19501       /* Look for the optional `::' operator.  */
19502       cp_parser_global_scope_opt (parser,
19503                                   /*current_scope_valid_p=*/false);
19504       /* Look for the nested-name specifier.  */
19505       token = cp_lexer_peek_token (parser->lexer);
19506       cp_parser_nested_name_specifier (parser,
19507                                        /*typename_keyword_p=*/false,
19508                                        /*check_dependency_p=*/true,
19509                                        /*type_p=*/false,
19510                                        /*is_declaration=*/false);
19511       /* If we found it, and the next token is a `*', then we are
19512          indeed looking at a pointer-to-member operator.  */
19513       if (!cp_parser_error_occurred (parser)
19514           && cp_parser_require (parser, CPP_MULT, RT_MULT))
19515         {
19516           /* Indicate that the `*' operator was used.  */
19517           code = INDIRECT_REF;
19518
19519           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19520             error_at (token->location, "%qD is a namespace", parser->scope);
19521           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19522             error_at (token->location, "cannot form pointer to member of "
19523                       "non-class %q#T", parser->scope);
19524           else
19525             {
19526               /* The type of which the member is a member is given by the
19527                  current SCOPE.  */
19528               *type = parser->scope;
19529               /* The next name will not be qualified.  */
19530               parser->scope = NULL_TREE;
19531               parser->qualifying_scope = NULL_TREE;
19532               parser->object_scope = NULL_TREE;
19533               /* Look for optional c++11 attributes.  */
19534               attrs = cp_parser_std_attribute_spec_seq (parser);
19535               if (attributes != NULL)
19536                 *attributes = attrs;
19537               /* Look for the optional cv-qualifier-seq.  */
19538               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19539             }
19540         }
19541       /* If that didn't work we don't have a ptr-operator.  */
19542       if (!cp_parser_parse_definitely (parser))
19543         cp_parser_error (parser, "expected ptr-operator");
19544     }
19545
19546   return code;
19547 }
19548
19549 /* Parse an (optional) cv-qualifier-seq.
19550
19551    cv-qualifier-seq:
19552      cv-qualifier cv-qualifier-seq [opt]
19553
19554    cv-qualifier:
19555      const
19556      volatile
19557
19558    GNU Extension:
19559
19560    cv-qualifier:
19561      __restrict__
19562
19563    Returns a bitmask representing the cv-qualifiers.  */
19564
19565 static cp_cv_quals
19566 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19567 {
19568   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19569
19570   while (true)
19571     {
19572       cp_token *token;
19573       cp_cv_quals cv_qualifier;
19574
19575       /* Peek at the next token.  */
19576       token = cp_lexer_peek_token (parser->lexer);
19577       /* See if it's a cv-qualifier.  */
19578       switch (token->keyword)
19579         {
19580         case RID_CONST:
19581           cv_qualifier = TYPE_QUAL_CONST;
19582           break;
19583
19584         case RID_VOLATILE:
19585           cv_qualifier = TYPE_QUAL_VOLATILE;
19586           break;
19587
19588         case RID_RESTRICT:
19589           cv_qualifier = TYPE_QUAL_RESTRICT;
19590           break;
19591
19592         default:
19593           cv_qualifier = TYPE_UNQUALIFIED;
19594           break;
19595         }
19596
19597       if (!cv_qualifier)
19598         break;
19599
19600       if (cv_quals & cv_qualifier)
19601         {
19602           error_at (token->location, "duplicate cv-qualifier");
19603           cp_lexer_purge_token (parser->lexer);
19604         }
19605       else
19606         {
19607           cp_lexer_consume_token (parser->lexer);
19608           cv_quals |= cv_qualifier;
19609         }
19610     }
19611
19612   return cv_quals;
19613 }
19614
19615 /* Parse an (optional) ref-qualifier
19616
19617    ref-qualifier:
19618      &
19619      &&
19620
19621    Returns cp_ref_qualifier representing ref-qualifier. */
19622
19623 static cp_ref_qualifier
19624 cp_parser_ref_qualifier_opt (cp_parser* parser)
19625 {
19626   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19627
19628   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
19629   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19630     return ref_qual;
19631
19632   while (true)
19633     {
19634       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19635       cp_token *token = cp_lexer_peek_token (parser->lexer);
19636
19637       switch (token->type)
19638         {
19639         case CPP_AND:
19640           curr_ref_qual = REF_QUAL_LVALUE;
19641           break;
19642
19643         case CPP_AND_AND:
19644           curr_ref_qual = REF_QUAL_RVALUE;
19645           break;
19646
19647         default:
19648           curr_ref_qual = REF_QUAL_NONE;
19649           break;
19650         }
19651
19652       if (!curr_ref_qual)
19653         break;
19654       else if (ref_qual)
19655         {
19656           error_at (token->location, "multiple ref-qualifiers");
19657           cp_lexer_purge_token (parser->lexer);
19658         }
19659       else
19660         {
19661           ref_qual = curr_ref_qual;
19662           cp_lexer_consume_token (parser->lexer);
19663         }
19664     }
19665
19666   return ref_qual;
19667 }
19668
19669 /* Parse an optional tx-qualifier.
19670
19671    tx-qualifier:
19672      transaction_safe
19673      transaction_safe_dynamic  */
19674
19675 static tree
19676 cp_parser_tx_qualifier_opt (cp_parser *parser)
19677 {
19678   cp_token *token = cp_lexer_peek_token (parser->lexer);
19679   if (token->type == CPP_NAME)
19680     {
19681       tree name = token->u.value;
19682       const char *p = IDENTIFIER_POINTER (name);
19683       const int len = strlen ("transaction_safe");
19684       if (!strncmp (p, "transaction_safe", len))
19685         {
19686           p += len;
19687           if (*p == '\0'
19688               || !strcmp (p, "_dynamic"))
19689             {
19690               cp_lexer_consume_token (parser->lexer);
19691               if (!flag_tm)
19692                 {
19693                   error ("%E requires %<-fgnu-tm%>", name);
19694                   return NULL_TREE;
19695                 }
19696               else
19697                 return name;
19698             }
19699         }
19700     }
19701   return NULL_TREE;
19702 }
19703
19704 /* Parse an (optional) virt-specifier-seq.
19705
19706    virt-specifier-seq:
19707      virt-specifier virt-specifier-seq [opt]
19708
19709    virt-specifier:
19710      override
19711      final
19712
19713    Returns a bitmask representing the virt-specifiers.  */
19714
19715 static cp_virt_specifiers
19716 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19717 {
19718   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19719
19720   while (true)
19721     {
19722       cp_token *token;
19723       cp_virt_specifiers virt_specifier;
19724
19725       /* Peek at the next token.  */
19726       token = cp_lexer_peek_token (parser->lexer);
19727       /* See if it's a virt-specifier-qualifier.  */
19728       if (token->type != CPP_NAME)
19729         break;
19730       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19731         {
19732           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19733           virt_specifier = VIRT_SPEC_OVERRIDE;
19734         }
19735       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19736         {
19737           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19738           virt_specifier = VIRT_SPEC_FINAL;
19739         }
19740       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19741         {
19742           virt_specifier = VIRT_SPEC_FINAL;
19743         }
19744       else
19745         break;
19746
19747       if (virt_specifiers & virt_specifier)
19748         {
19749           error_at (token->location, "duplicate virt-specifier");
19750           cp_lexer_purge_token (parser->lexer);
19751         }
19752       else
19753         {
19754           cp_lexer_consume_token (parser->lexer);
19755           virt_specifiers |= virt_specifier;
19756         }
19757     }
19758   return virt_specifiers;
19759 }
19760
19761 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19762    is in scope even though it isn't real.  */
19763
19764 void
19765 inject_this_parameter (tree ctype, cp_cv_quals quals)
19766 {
19767   tree this_parm;
19768
19769   if (current_class_ptr)
19770     {
19771       /* We don't clear this between NSDMIs.  Is it already what we want?  */
19772       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19773       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19774           && cp_type_quals (type) == quals)
19775         return;
19776     }
19777
19778   this_parm = build_this_parm (ctype, quals);
19779   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
19780   current_class_ptr = NULL_TREE;
19781   current_class_ref
19782     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19783   current_class_ptr = this_parm;
19784 }
19785
19786 /* Return true iff our current scope is a non-static data member
19787    initializer.  */
19788
19789 bool
19790 parsing_nsdmi (void)
19791 {
19792   /* We recognize NSDMI context by the context-less 'this' pointer set up
19793      by the function above.  */
19794   if (current_class_ptr
19795       && TREE_CODE (current_class_ptr) == PARM_DECL
19796       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19797     return true;
19798   return false;
19799 }
19800
19801 /* Parse a late-specified return type, if any.  This is not a separate
19802    non-terminal, but part of a function declarator, which looks like
19803
19804    -> trailing-type-specifier-seq abstract-declarator(opt)
19805
19806    Returns the type indicated by the type-id.
19807
19808    In addition to this, parse any queued up omp declare simd
19809    clauses and Cilk Plus SIMD-enabled function's vector attributes.
19810
19811    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19812    function.  */
19813
19814 static tree
19815 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19816                                 tree& requires_clause, cp_cv_quals quals)
19817 {
19818   cp_token *token;
19819   tree type = NULL_TREE;
19820   bool declare_simd_p = (parser->omp_declare_simd
19821                          && declarator
19822                          && declarator->kind == cdk_id);
19823
19824   bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info 
19825                                 && declarator && declarator->kind == cdk_id);
19826
19827   bool oacc_routine_p = (parser->oacc_routine
19828                          && declarator
19829                          && declarator->kind == cdk_id);
19830
19831   /* Peek at the next token.  */
19832   token = cp_lexer_peek_token (parser->lexer);
19833   /* A late-specified return type is indicated by an initial '->'. */
19834   if (token->type != CPP_DEREF
19835       && token->keyword != RID_REQUIRES
19836       && !(token->type == CPP_NAME
19837            && token->u.value == ridpointers[RID_REQUIRES])
19838       && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19839     return NULL_TREE;
19840
19841   tree save_ccp = current_class_ptr;
19842   tree save_ccr = current_class_ref;
19843   if (quals >= 0)
19844     {
19845       /* DR 1207: 'this' is in scope in the trailing return type.  */
19846       inject_this_parameter (current_class_type, quals);
19847     }
19848
19849   if (token->type == CPP_DEREF)
19850     {
19851       /* Consume the ->.  */
19852       cp_lexer_consume_token (parser->lexer);
19853
19854       type = cp_parser_trailing_type_id (parser);
19855     }
19856
19857   /* Function declarations may be followed by a trailing
19858      requires-clause.  */
19859   requires_clause = cp_parser_requires_clause_opt (parser);
19860
19861   if (cilk_simd_fn_vector_p)
19862     declarator->attributes
19863       = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19864                                                   declarator->attributes);
19865   if (declare_simd_p)
19866     declarator->attributes
19867       = cp_parser_late_parsing_omp_declare_simd (parser,
19868                                                  declarator->attributes);
19869   if (oacc_routine_p)
19870     declarator->attributes
19871       = cp_parser_late_parsing_oacc_routine (parser,
19872                                              declarator->attributes);
19873
19874   if (quals >= 0)
19875     {
19876       current_class_ptr = save_ccp;
19877       current_class_ref = save_ccr;
19878     }
19879
19880   return type;
19881 }
19882
19883 /* Parse a declarator-id.
19884
19885    declarator-id:
19886      id-expression
19887      :: [opt] nested-name-specifier [opt] type-name
19888
19889    In the `id-expression' case, the value returned is as for
19890    cp_parser_id_expression if the id-expression was an unqualified-id.
19891    If the id-expression was a qualified-id, then a SCOPE_REF is
19892    returned.  The first operand is the scope (either a NAMESPACE_DECL
19893    or TREE_TYPE), but the second is still just a representation of an
19894    unqualified-id.  */
19895
19896 static tree
19897 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19898 {
19899   tree id;
19900   /* The expression must be an id-expression.  Assume that qualified
19901      names are the names of types so that:
19902
19903        template <class T>
19904        int S<T>::R::i = 3;
19905
19906      will work; we must treat `S<T>::R' as the name of a type.
19907      Similarly, assume that qualified names are templates, where
19908      required, so that:
19909
19910        template <class T>
19911        int S<T>::R<T>::i = 3;
19912
19913      will work, too.  */
19914   id = cp_parser_id_expression (parser,
19915                                 /*template_keyword_p=*/false,
19916                                 /*check_dependency_p=*/false,
19917                                 /*template_p=*/NULL,
19918                                 /*declarator_p=*/true,
19919                                 optional_p);
19920   if (id && BASELINK_P (id))
19921     id = BASELINK_FUNCTIONS (id);
19922   return id;
19923 }
19924
19925 /* Parse a type-id.
19926
19927    type-id:
19928      type-specifier-seq abstract-declarator [opt]
19929
19930    Returns the TYPE specified.  */
19931
19932 static tree
19933 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19934                      bool is_trailing_return)
19935 {
19936   cp_decl_specifier_seq type_specifier_seq;
19937   cp_declarator *abstract_declarator;
19938
19939   /* Parse the type-specifier-seq.  */
19940   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19941                                 is_trailing_return,
19942                                 &type_specifier_seq);
19943   if (type_specifier_seq.type == error_mark_node)
19944     return error_mark_node;
19945
19946   /* There might or might not be an abstract declarator.  */
19947   cp_parser_parse_tentatively (parser);
19948   /* Look for the declarator.  */
19949   abstract_declarator
19950     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19951                             /*parenthesized_p=*/NULL,
19952                             /*member_p=*/false,
19953                             /*friend_p=*/false);
19954   /* Check to see if there really was a declarator.  */
19955   if (!cp_parser_parse_definitely (parser))
19956     abstract_declarator = NULL;
19957
19958   if (type_specifier_seq.type
19959       /* The concepts TS allows 'auto' as a type-id.  */
19960       && (!flag_concepts || parser->in_type_id_in_expr_p)
19961       /* None of the valid uses of 'auto' in C++14 involve the type-id
19962          nonterminal, but it is valid in a trailing-return-type.  */
19963       && !(cxx_dialect >= cxx14 && is_trailing_return)
19964       && type_uses_auto (type_specifier_seq.type))
19965     {
19966       /* A type-id with type 'auto' is only ok if the abstract declarator
19967          is a function declarator with a late-specified return type.
19968
19969          A type-id with 'auto' is also valid in a trailing-return-type
19970          in a compound-requirement. */
19971       if (abstract_declarator
19972           && abstract_declarator->kind == cdk_function
19973           && abstract_declarator->u.function.late_return_type)
19974         /* OK */;
19975       else if (parser->in_result_type_constraint_p)
19976         /* OK */;
19977       else
19978         {
19979           error ("invalid use of %<auto%>");
19980           return error_mark_node;
19981         }
19982     }
19983   
19984   return groktypename (&type_specifier_seq, abstract_declarator,
19985                        is_template_arg);
19986 }
19987
19988 static tree
19989 cp_parser_type_id (cp_parser *parser)
19990 {
19991   return cp_parser_type_id_1 (parser, false, false);
19992 }
19993
19994 static tree
19995 cp_parser_template_type_arg (cp_parser *parser)
19996 {
19997   tree r;
19998   const char *saved_message = parser->type_definition_forbidden_message;
19999   parser->type_definition_forbidden_message
20000     = G_("types may not be defined in template arguments");
20001   r = cp_parser_type_id_1 (parser, true, false);
20002   parser->type_definition_forbidden_message = saved_message;
20003   if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20004     {
20005       error ("invalid use of %<auto%> in template argument");
20006       r = error_mark_node;
20007     }
20008   return r;
20009 }
20010
20011 static tree
20012 cp_parser_trailing_type_id (cp_parser *parser)
20013 {
20014   return cp_parser_type_id_1 (parser, false, true);
20015 }
20016
20017 /* Parse a type-specifier-seq.
20018
20019    type-specifier-seq:
20020      type-specifier type-specifier-seq [opt]
20021
20022    GNU extension:
20023
20024    type-specifier-seq:
20025      attributes type-specifier-seq [opt]
20026
20027    If IS_DECLARATION is true, we are at the start of a "condition" or
20028    exception-declaration, so we might be followed by a declarator-id.
20029
20030    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20031    i.e. we've just seen "->".
20032
20033    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
20034
20035 static void
20036 cp_parser_type_specifier_seq (cp_parser* parser,
20037                               bool is_declaration,
20038                               bool is_trailing_return,
20039                               cp_decl_specifier_seq *type_specifier_seq)
20040 {
20041   bool seen_type_specifier = false;
20042   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20043   cp_token *start_token = NULL;
20044
20045   /* Clear the TYPE_SPECIFIER_SEQ.  */
20046   clear_decl_specs (type_specifier_seq);
20047
20048   /* In the context of a trailing return type, enum E { } is an
20049      elaborated-type-specifier followed by a function-body, not an
20050      enum-specifier.  */
20051   if (is_trailing_return)
20052     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20053
20054   /* Parse the type-specifiers and attributes.  */
20055   while (true)
20056     {
20057       tree type_specifier;
20058       bool is_cv_qualifier;
20059
20060       /* Check for attributes first.  */
20061       if (cp_next_tokens_can_be_attribute_p (parser))
20062         {
20063           type_specifier_seq->attributes =
20064             chainon (type_specifier_seq->attributes,
20065                      cp_parser_attributes_opt (parser));
20066           continue;
20067         }
20068
20069       /* record the token of the beginning of the type specifier seq,
20070          for error reporting purposes*/
20071      if (!start_token)
20072        start_token = cp_lexer_peek_token (parser->lexer);
20073
20074       /* Look for the type-specifier.  */
20075       type_specifier = cp_parser_type_specifier (parser,
20076                                                  flags,
20077                                                  type_specifier_seq,
20078                                                  /*is_declaration=*/false,
20079                                                  NULL,
20080                                                  &is_cv_qualifier);
20081       if (!type_specifier)
20082         {
20083           /* If the first type-specifier could not be found, this is not a
20084              type-specifier-seq at all.  */
20085           if (!seen_type_specifier)
20086             {
20087               /* Set in_declarator_p to avoid skipping to the semicolon.  */
20088               int in_decl = parser->in_declarator_p;
20089               parser->in_declarator_p = true;
20090
20091               if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20092                   || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20093                 cp_parser_error (parser, "expected type-specifier");
20094
20095               parser->in_declarator_p = in_decl;
20096
20097               type_specifier_seq->type = error_mark_node;
20098               return;
20099             }
20100           /* If subsequent type-specifiers could not be found, the
20101              type-specifier-seq is complete.  */
20102           break;
20103         }
20104
20105       seen_type_specifier = true;
20106       /* The standard says that a condition can be:
20107
20108             type-specifier-seq declarator = assignment-expression
20109
20110          However, given:
20111
20112            struct S {};
20113            if (int S = ...)
20114
20115          we should treat the "S" as a declarator, not as a
20116          type-specifier.  The standard doesn't say that explicitly for
20117          type-specifier-seq, but it does say that for
20118          decl-specifier-seq in an ordinary declaration.  Perhaps it
20119          would be clearer just to allow a decl-specifier-seq here, and
20120          then add a semantic restriction that if any decl-specifiers
20121          that are not type-specifiers appear, the program is invalid.  */
20122       if (is_declaration && !is_cv_qualifier)
20123         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20124     }
20125 }
20126
20127 /* Return whether the function currently being declared has an associated
20128    template parameter list.  */
20129
20130 static bool
20131 function_being_declared_is_template_p (cp_parser* parser)
20132 {
20133   if (!current_template_parms || processing_template_parmlist)
20134     return false;
20135
20136   if (parser->implicit_template_scope)
20137     return true;
20138
20139   if (at_class_scope_p ()
20140       && TYPE_BEING_DEFINED (current_class_type))
20141     return parser->num_template_parameter_lists != 0;
20142
20143   return ((int) parser->num_template_parameter_lists > template_class_depth
20144           (current_class_type));
20145 }
20146
20147 /* Parse a parameter-declaration-clause.
20148
20149    parameter-declaration-clause:
20150      parameter-declaration-list [opt] ... [opt]
20151      parameter-declaration-list , ...
20152
20153    Returns a representation for the parameter declarations.  A return
20154    value of NULL indicates a parameter-declaration-clause consisting
20155    only of an ellipsis.  */
20156
20157 static tree
20158 cp_parser_parameter_declaration_clause (cp_parser* parser)
20159 {
20160   tree parameters;
20161   cp_token *token;
20162   bool ellipsis_p;
20163   bool is_error;
20164
20165   struct cleanup {
20166     cp_parser* parser;
20167     int auto_is_implicit_function_template_parm_p;
20168     ~cleanup() {
20169       parser->auto_is_implicit_function_template_parm_p
20170         = auto_is_implicit_function_template_parm_p;
20171     }
20172   } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20173
20174   (void) cleanup;
20175
20176   if (!processing_specialization
20177       && !processing_template_parmlist
20178       && !processing_explicit_instantiation)
20179     if (!current_function_decl
20180         || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20181       parser->auto_is_implicit_function_template_parm_p = true;
20182
20183   /* Peek at the next token.  */
20184   token = cp_lexer_peek_token (parser->lexer);
20185   /* Check for trivial parameter-declaration-clauses.  */
20186   if (token->type == CPP_ELLIPSIS)
20187     {
20188       /* Consume the `...' token.  */
20189       cp_lexer_consume_token (parser->lexer);
20190       return NULL_TREE;
20191     }
20192   else if (token->type == CPP_CLOSE_PAREN)
20193     /* There are no parameters.  */
20194     {
20195 #ifndef NO_IMPLICIT_EXTERN_C
20196       if (in_system_header_at (input_location)
20197           && current_class_type == NULL
20198           && current_lang_name == lang_name_c)
20199         return NULL_TREE;
20200       else
20201 #endif
20202         return void_list_node;
20203     }
20204   /* Check for `(void)', too, which is a special case.  */
20205   else if (token->keyword == RID_VOID
20206            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20207                == CPP_CLOSE_PAREN))
20208     {
20209       /* Consume the `void' token.  */
20210       cp_lexer_consume_token (parser->lexer);
20211       /* There are no parameters.  */
20212       return void_list_node;
20213     }
20214
20215   /* Parse the parameter-declaration-list.  */
20216   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20217   /* If a parse error occurred while parsing the
20218      parameter-declaration-list, then the entire
20219      parameter-declaration-clause is erroneous.  */
20220   if (is_error)
20221     return NULL;
20222
20223   /* Peek at the next token.  */
20224   token = cp_lexer_peek_token (parser->lexer);
20225   /* If it's a `,', the clause should terminate with an ellipsis.  */
20226   if (token->type == CPP_COMMA)
20227     {
20228       /* Consume the `,'.  */
20229       cp_lexer_consume_token (parser->lexer);
20230       /* Expect an ellipsis.  */
20231       ellipsis_p
20232         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20233     }
20234   /* It might also be `...' if the optional trailing `,' was
20235      omitted.  */
20236   else if (token->type == CPP_ELLIPSIS)
20237     {
20238       /* Consume the `...' token.  */
20239       cp_lexer_consume_token (parser->lexer);
20240       /* And remember that we saw it.  */
20241       ellipsis_p = true;
20242     }
20243   else
20244     ellipsis_p = false;
20245
20246   /* Finish the parameter list.  */
20247   if (!ellipsis_p)
20248     parameters = chainon (parameters, void_list_node);
20249
20250   return parameters;
20251 }
20252
20253 /* Parse a parameter-declaration-list.
20254
20255    parameter-declaration-list:
20256      parameter-declaration
20257      parameter-declaration-list , parameter-declaration
20258
20259    Returns a representation of the parameter-declaration-list, as for
20260    cp_parser_parameter_declaration_clause.  However, the
20261    `void_list_node' is never appended to the list.  Upon return,
20262    *IS_ERROR will be true iff an error occurred.  */
20263
20264 static tree
20265 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20266 {
20267   tree parameters = NULL_TREE;
20268   tree *tail = &parameters;
20269   bool saved_in_unbraced_linkage_specification_p;
20270   int index = 0;
20271
20272   /* Assume all will go well.  */
20273   *is_error = false;
20274   /* The special considerations that apply to a function within an
20275      unbraced linkage specifications do not apply to the parameters
20276      to the function.  */
20277   saved_in_unbraced_linkage_specification_p
20278     = parser->in_unbraced_linkage_specification_p;
20279   parser->in_unbraced_linkage_specification_p = false;
20280
20281   /* Look for more parameters.  */
20282   while (true)
20283     {
20284       cp_parameter_declarator *parameter;
20285       tree decl = error_mark_node;
20286       bool parenthesized_p = false;
20287       int template_parm_idx = (function_being_declared_is_template_p (parser)?
20288                                TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20289                                                 (current_template_parms)) : 0);
20290
20291       /* Parse the parameter.  */
20292       parameter
20293         = cp_parser_parameter_declaration (parser,
20294                                            /*template_parm_p=*/false,
20295                                            &parenthesized_p);
20296
20297       /* We don't know yet if the enclosing context is deprecated, so wait
20298          and warn in grokparms if appropriate.  */
20299       deprecated_state = DEPRECATED_SUPPRESS;
20300
20301       if (parameter)
20302         {
20303           /* If a function parameter pack was specified and an implicit template
20304              parameter was introduced during cp_parser_parameter_declaration,
20305              change any implicit parameters introduced into packs.  */
20306           if (parser->implicit_template_parms
20307               && parameter->declarator
20308               && parameter->declarator->parameter_pack_p)
20309             {
20310               int latest_template_parm_idx = TREE_VEC_LENGTH
20311                 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20312
20313               if (latest_template_parm_idx != template_parm_idx)
20314                 parameter->decl_specifiers.type = convert_generic_types_to_packs
20315                   (parameter->decl_specifiers.type,
20316                    template_parm_idx, latest_template_parm_idx);
20317             }
20318
20319           decl = grokdeclarator (parameter->declarator,
20320                                  &parameter->decl_specifiers,
20321                                  PARM,
20322                                  parameter->default_argument != NULL_TREE,
20323                                  &parameter->decl_specifiers.attributes);
20324         }
20325
20326       deprecated_state = DEPRECATED_NORMAL;
20327
20328       /* If a parse error occurred parsing the parameter declaration,
20329          then the entire parameter-declaration-list is erroneous.  */
20330       if (decl == error_mark_node)
20331         {
20332           *is_error = true;
20333           parameters = error_mark_node;
20334           break;
20335         }
20336
20337       if (parameter->decl_specifiers.attributes)
20338         cplus_decl_attributes (&decl,
20339                                parameter->decl_specifiers.attributes,
20340                                0);
20341       if (DECL_NAME (decl))
20342         decl = pushdecl (decl);
20343
20344       if (decl != error_mark_node)
20345         {
20346           retrofit_lang_decl (decl);
20347           DECL_PARM_INDEX (decl) = ++index;
20348           DECL_PARM_LEVEL (decl) = function_parm_depth ();
20349         }
20350
20351       /* Add the new parameter to the list.  */
20352       *tail = build_tree_list (parameter->default_argument, decl);
20353       tail = &TREE_CHAIN (*tail);
20354
20355       /* Peek at the next token.  */
20356       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20357           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20358           /* These are for Objective-C++ */
20359           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20360           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20361         /* The parameter-declaration-list is complete.  */
20362         break;
20363       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20364         {
20365           cp_token *token;
20366
20367           /* Peek at the next token.  */
20368           token = cp_lexer_peek_nth_token (parser->lexer, 2);
20369           /* If it's an ellipsis, then the list is complete.  */
20370           if (token->type == CPP_ELLIPSIS)
20371             break;
20372           /* Otherwise, there must be more parameters.  Consume the
20373              `,'.  */
20374           cp_lexer_consume_token (parser->lexer);
20375           /* When parsing something like:
20376
20377                 int i(float f, double d)
20378
20379              we can tell after seeing the declaration for "f" that we
20380              are not looking at an initialization of a variable "i",
20381              but rather at the declaration of a function "i".
20382
20383              Due to the fact that the parsing of template arguments
20384              (as specified to a template-id) requires backtracking we
20385              cannot use this technique when inside a template argument
20386              list.  */
20387           if (!parser->in_template_argument_list_p
20388               && !parser->in_type_id_in_expr_p
20389               && cp_parser_uncommitted_to_tentative_parse_p (parser)
20390               /* However, a parameter-declaration of the form
20391                  "float(f)" (which is a valid declaration of a
20392                  parameter "f") can also be interpreted as an
20393                  expression (the conversion of "f" to "float").  */
20394               && !parenthesized_p)
20395             cp_parser_commit_to_tentative_parse (parser);
20396         }
20397       else
20398         {
20399           cp_parser_error (parser, "expected %<,%> or %<...%>");
20400           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20401             cp_parser_skip_to_closing_parenthesis (parser,
20402                                                    /*recovering=*/true,
20403                                                    /*or_comma=*/false,
20404                                                    /*consume_paren=*/false);
20405           break;
20406         }
20407     }
20408
20409   parser->in_unbraced_linkage_specification_p
20410     = saved_in_unbraced_linkage_specification_p;
20411
20412   /* Reset implicit_template_scope if we are about to leave the function
20413      parameter list that introduced it.  Note that for out-of-line member
20414      definitions, there will be one or more class scopes before we get to
20415      the template parameter scope.  */
20416
20417   if (cp_binding_level *its = parser->implicit_template_scope)
20418     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20419       {
20420         while (maybe_its->kind == sk_class)
20421           maybe_its = maybe_its->level_chain;
20422         if (maybe_its == its)
20423           {
20424             parser->implicit_template_parms = 0;
20425             parser->implicit_template_scope = 0;
20426           }
20427       }
20428
20429   return parameters;
20430 }
20431
20432 /* Parse a parameter declaration.
20433
20434    parameter-declaration:
20435      decl-specifier-seq ... [opt] declarator
20436      decl-specifier-seq declarator = assignment-expression
20437      decl-specifier-seq ... [opt] abstract-declarator [opt]
20438      decl-specifier-seq abstract-declarator [opt] = assignment-expression
20439
20440    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20441    declares a template parameter.  (In that case, a non-nested `>'
20442    token encountered during the parsing of the assignment-expression
20443    is not interpreted as a greater-than operator.)
20444
20445    Returns a representation of the parameter, or NULL if an error
20446    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20447    true iff the declarator is of the form "(p)".  */
20448
20449 static cp_parameter_declarator *
20450 cp_parser_parameter_declaration (cp_parser *parser,
20451                                  bool template_parm_p,
20452                                  bool *parenthesized_p)
20453 {
20454   int declares_class_or_enum;
20455   cp_decl_specifier_seq decl_specifiers;
20456   cp_declarator *declarator;
20457   tree default_argument;
20458   cp_token *token = NULL, *declarator_token_start = NULL;
20459   const char *saved_message;
20460   bool template_parameter_pack_p = false;
20461
20462   /* In a template parameter, `>' is not an operator.
20463
20464      [temp.param]
20465
20466      When parsing a default template-argument for a non-type
20467      template-parameter, the first non-nested `>' is taken as the end
20468      of the template parameter-list rather than a greater-than
20469      operator.  */
20470
20471   /* Type definitions may not appear in parameter types.  */
20472   saved_message = parser->type_definition_forbidden_message;
20473   parser->type_definition_forbidden_message
20474     = G_("types may not be defined in parameter types");
20475
20476   /* Parse the declaration-specifiers.  */
20477   cp_parser_decl_specifier_seq (parser,
20478                                 CP_PARSER_FLAGS_NONE,
20479                                 &decl_specifiers,
20480                                 &declares_class_or_enum);
20481
20482   /* Complain about missing 'typename' or other invalid type names.  */
20483   if (!decl_specifiers.any_type_specifiers_p
20484       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20485     decl_specifiers.type = error_mark_node;
20486
20487   /* If an error occurred, there's no reason to attempt to parse the
20488      rest of the declaration.  */
20489   if (cp_parser_error_occurred (parser))
20490     {
20491       parser->type_definition_forbidden_message = saved_message;
20492       return NULL;
20493     }
20494
20495   /* Peek at the next token.  */
20496   token = cp_lexer_peek_token (parser->lexer);
20497
20498   /* If the next token is a `)', `,', `=', `>', or `...', then there
20499      is no declarator. However, when variadic templates are enabled,
20500      there may be a declarator following `...'.  */
20501   if (token->type == CPP_CLOSE_PAREN
20502       || token->type == CPP_COMMA
20503       || token->type == CPP_EQ
20504       || token->type == CPP_GREATER)
20505     {
20506       declarator = NULL;
20507       if (parenthesized_p)
20508         *parenthesized_p = false;
20509     }
20510   /* Otherwise, there should be a declarator.  */
20511   else
20512     {
20513       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20514       parser->default_arg_ok_p = false;
20515
20516       /* After seeing a decl-specifier-seq, if the next token is not a
20517          "(", there is no possibility that the code is a valid
20518          expression.  Therefore, if parsing tentatively, we commit at
20519          this point.  */
20520       if (!parser->in_template_argument_list_p
20521           /* In an expression context, having seen:
20522
20523                (int((char ...
20524
20525              we cannot be sure whether we are looking at a
20526              function-type (taking a "char" as a parameter) or a cast
20527              of some object of type "char" to "int".  */
20528           && !parser->in_type_id_in_expr_p
20529           && cp_parser_uncommitted_to_tentative_parse_p (parser)
20530           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20531           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20532         cp_parser_commit_to_tentative_parse (parser);
20533       /* Parse the declarator.  */
20534       declarator_token_start = token;
20535       declarator = cp_parser_declarator (parser,
20536                                          CP_PARSER_DECLARATOR_EITHER,
20537                                          /*ctor_dtor_or_conv_p=*/NULL,
20538                                          parenthesized_p,
20539                                          /*member_p=*/false,
20540                                          /*friend_p=*/false);
20541       parser->default_arg_ok_p = saved_default_arg_ok_p;
20542       /* After the declarator, allow more attributes.  */
20543       decl_specifiers.attributes
20544         = chainon (decl_specifiers.attributes,
20545                    cp_parser_attributes_opt (parser));
20546
20547       /* If the declarator is a template parameter pack, remember that and
20548          clear the flag in the declarator itself so we don't get errors
20549          from grokdeclarator.  */
20550       if (template_parm_p && declarator && declarator->parameter_pack_p)
20551         {
20552           declarator->parameter_pack_p = false;
20553           template_parameter_pack_p = true;
20554         }
20555     }
20556
20557   /* If the next token is an ellipsis, and we have not seen a declarator
20558      name, and if either the type of the declarator contains parameter
20559      packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20560      for, eg, abbreviated integral type names), then we actually have a
20561      parameter pack expansion expression. Otherwise, leave the ellipsis
20562      for a C-style variadic function. */
20563   token = cp_lexer_peek_token (parser->lexer);
20564   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20565     {
20566       tree type = decl_specifiers.type;
20567
20568       if (type && DECL_P (type))
20569         type = TREE_TYPE (type);
20570
20571       if (((type
20572             && TREE_CODE (type) != TYPE_PACK_EXPANSION
20573             && (template_parm_p || uses_parameter_packs (type)))
20574            || (!type && template_parm_p))
20575           && declarator_can_be_parameter_pack (declarator))
20576         {
20577           /* Consume the `...'. */
20578           cp_lexer_consume_token (parser->lexer);
20579           maybe_warn_variadic_templates ();
20580           
20581           /* Build a pack expansion type */
20582           if (template_parm_p)
20583             template_parameter_pack_p = true;
20584           else if (declarator)
20585             declarator->parameter_pack_p = true;
20586           else
20587             decl_specifiers.type = make_pack_expansion (type);
20588         }
20589     }
20590
20591   /* The restriction on defining new types applies only to the type
20592      of the parameter, not to the default argument.  */
20593   parser->type_definition_forbidden_message = saved_message;
20594
20595   /* If the next token is `=', then process a default argument.  */
20596   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20597     {
20598       tree type = decl_specifiers.type;
20599       token = cp_lexer_peek_token (parser->lexer);
20600       /* If we are defining a class, then the tokens that make up the
20601          default argument must be saved and processed later.  */
20602       if (!template_parm_p && at_class_scope_p ()
20603           && TYPE_BEING_DEFINED (current_class_type)
20604           && !LAMBDA_TYPE_P (current_class_type))
20605         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20606
20607       // A constrained-type-specifier may declare a type template-parameter.
20608       else if (declares_constrained_type_template_parameter (type))
20609         default_argument
20610           = cp_parser_default_type_template_argument (parser);
20611
20612       // A constrained-type-specifier may declare a template-template-parameter.
20613       else if (declares_constrained_template_template_parameter (type))
20614         default_argument
20615           = cp_parser_default_template_template_argument (parser);
20616
20617       /* Outside of a class definition, we can just parse the
20618          assignment-expression.  */
20619       else
20620         default_argument
20621           = cp_parser_default_argument (parser, template_parm_p);
20622
20623       if (!parser->default_arg_ok_p)
20624         {
20625           permerror (token->location,
20626                      "default arguments are only "
20627                      "permitted for function parameters");
20628         }
20629       else if ((declarator && declarator->parameter_pack_p)
20630                || template_parameter_pack_p
20631                || (decl_specifiers.type
20632                    && PACK_EXPANSION_P (decl_specifiers.type)))
20633         {
20634           /* Find the name of the parameter pack.  */     
20635           cp_declarator *id_declarator = declarator;
20636           while (id_declarator && id_declarator->kind != cdk_id)
20637             id_declarator = id_declarator->declarator;
20638           
20639           if (id_declarator && id_declarator->kind == cdk_id)
20640             error_at (declarator_token_start->location,
20641                       template_parm_p
20642                       ? G_("template parameter pack %qD "
20643                            "cannot have a default argument")
20644                       : G_("parameter pack %qD cannot have "
20645                            "a default argument"),
20646                       id_declarator->u.id.unqualified_name);
20647           else
20648             error_at (declarator_token_start->location,
20649                       template_parm_p
20650                       ? G_("template parameter pack cannot have "
20651                            "a default argument")
20652                       : G_("parameter pack cannot have a "
20653                            "default argument"));
20654
20655           default_argument = NULL_TREE;
20656         }
20657     }
20658   else
20659     default_argument = NULL_TREE;
20660
20661   return make_parameter_declarator (&decl_specifiers,
20662                                     declarator,
20663                                     default_argument,
20664                                     template_parameter_pack_p);
20665 }
20666
20667 /* Parse a default argument and return it.
20668
20669    TEMPLATE_PARM_P is true if this is a default argument for a
20670    non-type template parameter.  */
20671 static tree
20672 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20673 {
20674   tree default_argument = NULL_TREE;
20675   bool saved_greater_than_is_operator_p;
20676   bool saved_local_variables_forbidden_p;
20677   bool non_constant_p, is_direct_init;
20678
20679   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20680      set correctly.  */
20681   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20682   parser->greater_than_is_operator_p = !template_parm_p;
20683   /* Local variable names (and the `this' keyword) may not
20684      appear in a default argument.  */
20685   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20686   parser->local_variables_forbidden_p = true;
20687   /* Parse the assignment-expression.  */
20688   if (template_parm_p)
20689     push_deferring_access_checks (dk_no_deferred);
20690   tree saved_class_ptr = NULL_TREE;
20691   tree saved_class_ref = NULL_TREE;
20692   /* The "this" pointer is not valid in a default argument.  */
20693   if (cfun)
20694     {
20695       saved_class_ptr = current_class_ptr;
20696       cp_function_chain->x_current_class_ptr = NULL_TREE;
20697       saved_class_ref = current_class_ref;
20698       cp_function_chain->x_current_class_ref = NULL_TREE;
20699     }
20700   default_argument
20701     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20702   /* Restore the "this" pointer.  */
20703   if (cfun)
20704     {
20705       cp_function_chain->x_current_class_ptr = saved_class_ptr;
20706       cp_function_chain->x_current_class_ref = saved_class_ref;
20707     }
20708   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20709     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20710   if (template_parm_p)
20711     pop_deferring_access_checks ();
20712   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20713   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20714
20715   return default_argument;
20716 }
20717
20718 /* Parse a function-body.
20719
20720    function-body:
20721      compound_statement  */
20722
20723 static void
20724 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20725 {
20726   cp_parser_compound_statement (parser, NULL, (in_function_try_block
20727                                                ? BCS_TRY_BLOCK : BCS_NORMAL),
20728                                 true);
20729 }
20730
20731 /* Parse a ctor-initializer-opt followed by a function-body.  Return
20732    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
20733    is true we are parsing a function-try-block.  */
20734
20735 static bool
20736 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20737                                                   bool in_function_try_block)
20738 {
20739   tree body, list;
20740   bool ctor_initializer_p;
20741   const bool check_body_p =
20742      DECL_CONSTRUCTOR_P (current_function_decl)
20743      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20744   tree last = NULL;
20745
20746   /* Begin the function body.  */
20747   body = begin_function_body ();
20748   /* Parse the optional ctor-initializer.  */
20749   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20750
20751   /* If we're parsing a constexpr constructor definition, we need
20752      to check that the constructor body is indeed empty.  However,
20753      before we get to cp_parser_function_body lot of junk has been
20754      generated, so we can't just check that we have an empty block.
20755      Rather we take a snapshot of the outermost block, and check whether
20756      cp_parser_function_body changed its state.  */
20757   if (check_body_p)
20758     {
20759       list = cur_stmt_list;
20760       if (STATEMENT_LIST_TAIL (list))
20761         last = STATEMENT_LIST_TAIL (list)->stmt;
20762     }
20763   /* Parse the function-body.  */
20764   cp_parser_function_body (parser, in_function_try_block);
20765   if (check_body_p)
20766     check_constexpr_ctor_body (last, list, /*complain=*/true);
20767   /* Finish the function body.  */
20768   finish_function_body (body);
20769
20770   return ctor_initializer_p;
20771 }
20772
20773 /* Parse an initializer.
20774
20775    initializer:
20776      = initializer-clause
20777      ( expression-list )
20778
20779    Returns an expression representing the initializer.  If no
20780    initializer is present, NULL_TREE is returned.
20781
20782    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20783    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
20784    set to TRUE if there is no initializer present.  If there is an
20785    initializer, and it is not a constant-expression, *NON_CONSTANT_P
20786    is set to true; otherwise it is set to false.  */
20787
20788 static tree
20789 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20790                        bool* non_constant_p)
20791 {
20792   cp_token *token;
20793   tree init;
20794
20795   /* Peek at the next token.  */
20796   token = cp_lexer_peek_token (parser->lexer);
20797
20798   /* Let our caller know whether or not this initializer was
20799      parenthesized.  */
20800   *is_direct_init = (token->type != CPP_EQ);
20801   /* Assume that the initializer is constant.  */
20802   *non_constant_p = false;
20803
20804   if (token->type == CPP_EQ)
20805     {
20806       /* Consume the `='.  */
20807       cp_lexer_consume_token (parser->lexer);
20808       /* Parse the initializer-clause.  */
20809       init = cp_parser_initializer_clause (parser, non_constant_p);
20810     }
20811   else if (token->type == CPP_OPEN_PAREN)
20812     {
20813       vec<tree, va_gc> *vec;
20814       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20815                                                      /*cast_p=*/false,
20816                                                      /*allow_expansion_p=*/true,
20817                                                      non_constant_p);
20818       if (vec == NULL)
20819         return error_mark_node;
20820       init = build_tree_list_vec (vec);
20821       release_tree_vector (vec);
20822     }
20823   else if (token->type == CPP_OPEN_BRACE)
20824     {
20825       cp_lexer_set_source_position (parser->lexer);
20826       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20827       init = cp_parser_braced_list (parser, non_constant_p);
20828       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20829     }
20830   else
20831     {
20832       /* Anything else is an error.  */
20833       cp_parser_error (parser, "expected initializer");
20834       init = error_mark_node;
20835     }
20836
20837   return init;
20838 }
20839
20840 /* Parse an initializer-clause.
20841
20842    initializer-clause:
20843      assignment-expression
20844      braced-init-list
20845
20846    Returns an expression representing the initializer.
20847
20848    If the `assignment-expression' production is used the value
20849    returned is simply a representation for the expression.
20850
20851    Otherwise, calls cp_parser_braced_list.  */
20852
20853 static cp_expr
20854 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20855 {
20856   cp_expr initializer;
20857
20858   /* Assume the expression is constant.  */
20859   *non_constant_p = false;
20860
20861   /* If it is not a `{', then we are looking at an
20862      assignment-expression.  */
20863   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20864     {
20865       initializer
20866         = cp_parser_constant_expression (parser,
20867                                         /*allow_non_constant_p=*/true,
20868                                         non_constant_p);
20869     }
20870   else
20871     initializer = cp_parser_braced_list (parser, non_constant_p);
20872
20873   return initializer;
20874 }
20875
20876 /* Parse a brace-enclosed initializer list.
20877
20878    braced-init-list:
20879      { initializer-list , [opt] }
20880      { }
20881
20882    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
20883    the elements of the initializer-list (or NULL, if the last
20884    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
20885    NULL_TREE.  There is no way to detect whether or not the optional
20886    trailing `,' was provided.  NON_CONSTANT_P is as for
20887    cp_parser_initializer.  */     
20888
20889 static cp_expr
20890 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20891 {
20892   tree initializer;
20893   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20894
20895   /* Consume the `{' token.  */
20896   cp_lexer_consume_token (parser->lexer);
20897   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
20898   initializer = make_node (CONSTRUCTOR);
20899   /* If it's not a `}', then there is a non-trivial initializer.  */
20900   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20901     {
20902       /* Parse the initializer list.  */
20903       CONSTRUCTOR_ELTS (initializer)
20904         = cp_parser_initializer_list (parser, non_constant_p);
20905       /* A trailing `,' token is allowed.  */
20906       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20907         cp_lexer_consume_token (parser->lexer);
20908     }
20909   else
20910     *non_constant_p = false;
20911   /* Now, there should be a trailing `}'.  */
20912   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20913   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20914   TREE_TYPE (initializer) = init_list_type_node;
20915
20916   cp_expr result (initializer);
20917   /* Build a location of the form:
20918        { ... }
20919        ^~~~~~~
20920      with caret==start at the open brace, finish at the close brace.  */
20921   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20922   result.set_location (combined_loc);
20923   return result;
20924 }
20925
20926 /* Consume tokens up to, and including, the next non-nested closing `]'.
20927    Returns true iff we found a closing `]'.  */
20928
20929 static bool
20930 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20931 {
20932   unsigned square_depth = 0;
20933
20934   while (true)
20935     {
20936       cp_token * token = cp_lexer_peek_token (parser->lexer);
20937
20938       switch (token->type)
20939         {
20940         case CPP_EOF:
20941         case CPP_PRAGMA_EOL:
20942           /* If we've run out of tokens, then there is no closing `]'.  */
20943           return false;
20944
20945         case CPP_OPEN_SQUARE:
20946           ++square_depth;
20947           break;
20948
20949         case CPP_CLOSE_SQUARE:
20950           if (!square_depth--)
20951             {
20952               cp_lexer_consume_token (parser->lexer);
20953               return true;
20954             }
20955           break;
20956
20957         default:
20958           break;
20959         }
20960
20961       /* Consume the token.  */
20962       cp_lexer_consume_token (parser->lexer);
20963     }
20964 }
20965
20966 /* Return true if we are looking at an array-designator, false otherwise.  */
20967
20968 static bool
20969 cp_parser_array_designator_p (cp_parser *parser)
20970 {
20971   /* Consume the `['.  */
20972   cp_lexer_consume_token (parser->lexer);
20973
20974   cp_lexer_save_tokens (parser->lexer);
20975
20976   /* Skip tokens until the next token is a closing square bracket.
20977      If we find the closing `]', and the next token is a `=', then
20978      we are looking at an array designator.  */
20979   bool array_designator_p
20980     = (cp_parser_skip_to_closing_square_bracket (parser)
20981        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20982   
20983   /* Roll back the tokens we skipped.  */
20984   cp_lexer_rollback_tokens (parser->lexer);
20985
20986   return array_designator_p;
20987 }
20988
20989 /* Parse an initializer-list.
20990
20991    initializer-list:
20992      initializer-clause ... [opt]
20993      initializer-list , initializer-clause ... [opt]
20994
20995    GNU Extension:
20996
20997    initializer-list:
20998      designation initializer-clause ...[opt]
20999      initializer-list , designation initializer-clause ...[opt]
21000
21001    designation:
21002      . identifier =
21003      identifier :
21004      [ constant-expression ] =
21005
21006    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
21007    for the initializer.  If the INDEX of the elt is non-NULL, it is the
21008    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
21009    as for cp_parser_initializer.  */
21010
21011 static vec<constructor_elt, va_gc> *
21012 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21013 {
21014   vec<constructor_elt, va_gc> *v = NULL;
21015
21016   /* Assume all of the expressions are constant.  */
21017   *non_constant_p = false;
21018
21019   /* Parse the rest of the list.  */
21020   while (true)
21021     {
21022       cp_token *token;
21023       tree designator;
21024       tree initializer;
21025       bool clause_non_constant_p;
21026
21027       /* If the next token is an identifier and the following one is a
21028          colon, we are looking at the GNU designated-initializer
21029          syntax.  */
21030       if (cp_parser_allow_gnu_extensions_p (parser)
21031           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21032           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21033         {
21034           /* Warn the user that they are using an extension.  */
21035           pedwarn (input_location, OPT_Wpedantic, 
21036                    "ISO C++ does not allow designated initializers");
21037           /* Consume the identifier.  */
21038           designator = cp_lexer_consume_token (parser->lexer)->u.value;
21039           /* Consume the `:'.  */
21040           cp_lexer_consume_token (parser->lexer);
21041         }
21042       /* Also handle the C99 syntax, '. id ='.  */
21043       else if (cp_parser_allow_gnu_extensions_p (parser)
21044                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21045                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21046                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21047         {
21048           /* Warn the user that they are using an extension.  */
21049           pedwarn (input_location, OPT_Wpedantic,
21050                    "ISO C++ does not allow C99 designated initializers");
21051           /* Consume the `.'.  */
21052           cp_lexer_consume_token (parser->lexer);
21053           /* Consume the identifier.  */
21054           designator = cp_lexer_consume_token (parser->lexer)->u.value;
21055           /* Consume the `='.  */
21056           cp_lexer_consume_token (parser->lexer);
21057         }
21058       /* Also handle C99 array designators, '[ const ] ='.  */
21059       else if (cp_parser_allow_gnu_extensions_p (parser)
21060                && !c_dialect_objc ()
21061                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21062         {
21063           /* In C++11, [ could start a lambda-introducer.  */
21064           bool non_const = false;
21065
21066           cp_parser_parse_tentatively (parser);
21067
21068           if (!cp_parser_array_designator_p (parser))
21069             {
21070               cp_parser_simulate_error (parser);
21071               designator = NULL_TREE;
21072             }
21073           else
21074             {
21075               designator = cp_parser_constant_expression (parser, true,
21076                                                           &non_const);
21077               cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21078               cp_parser_require (parser, CPP_EQ, RT_EQ);
21079             }
21080
21081           if (!cp_parser_parse_definitely (parser))
21082             designator = NULL_TREE;
21083           else if (non_const)
21084             require_potential_rvalue_constant_expression (designator);
21085         }
21086       else
21087         designator = NULL_TREE;
21088
21089       /* Parse the initializer.  */
21090       initializer = cp_parser_initializer_clause (parser,
21091                                                   &clause_non_constant_p);
21092       /* If any clause is non-constant, so is the entire initializer.  */
21093       if (clause_non_constant_p)
21094         *non_constant_p = true;
21095
21096       /* If we have an ellipsis, this is an initializer pack
21097          expansion.  */
21098       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21099         {
21100           /* Consume the `...'.  */
21101           cp_lexer_consume_token (parser->lexer);
21102
21103           /* Turn the initializer into an initializer expansion.  */
21104           initializer = make_pack_expansion (initializer);
21105         }
21106
21107       /* Add it to the vector.  */
21108       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21109
21110       /* If the next token is not a comma, we have reached the end of
21111          the list.  */
21112       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21113         break;
21114
21115       /* Peek at the next token.  */
21116       token = cp_lexer_peek_nth_token (parser->lexer, 2);
21117       /* If the next token is a `}', then we're still done.  An
21118          initializer-clause can have a trailing `,' after the
21119          initializer-list and before the closing `}'.  */
21120       if (token->type == CPP_CLOSE_BRACE)
21121         break;
21122
21123       /* Consume the `,' token.  */
21124       cp_lexer_consume_token (parser->lexer);
21125     }
21126
21127   return v;
21128 }
21129
21130 /* Classes [gram.class] */
21131
21132 /* Parse a class-name.
21133
21134    class-name:
21135      identifier
21136      template-id
21137
21138    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21139    to indicate that names looked up in dependent types should be
21140    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
21141    keyword has been used to indicate that the name that appears next
21142    is a template.  TAG_TYPE indicates the explicit tag given before
21143    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
21144    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
21145    is the class being defined in a class-head.  If ENUM_OK is TRUE,
21146    enum-names are also accepted.
21147
21148    Returns the TYPE_DECL representing the class.  */
21149
21150 static tree
21151 cp_parser_class_name (cp_parser *parser,
21152                       bool typename_keyword_p,
21153                       bool template_keyword_p,
21154                       enum tag_types tag_type,
21155                       bool check_dependency_p,
21156                       bool class_head_p,
21157                       bool is_declaration,
21158                       bool enum_ok)
21159 {
21160   tree decl;
21161   tree scope;
21162   bool typename_p;
21163   cp_token *token;
21164   tree identifier = NULL_TREE;
21165
21166   /* All class-names start with an identifier.  */
21167   token = cp_lexer_peek_token (parser->lexer);
21168   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21169     {
21170       cp_parser_error (parser, "expected class-name");
21171       return error_mark_node;
21172     }
21173
21174   /* PARSER->SCOPE can be cleared when parsing the template-arguments
21175      to a template-id, so we save it here.  */
21176   scope = parser->scope;
21177   if (scope == error_mark_node)
21178     return error_mark_node;
21179
21180   /* Any name names a type if we're following the `typename' keyword
21181      in a qualified name where the enclosing scope is type-dependent.  */
21182   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21183                 && dependent_type_p (scope));
21184   /* Handle the common case (an identifier, but not a template-id)
21185      efficiently.  */
21186   if (token->type == CPP_NAME
21187       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21188     {
21189       cp_token *identifier_token;
21190       bool ambiguous_p;
21191
21192       /* Look for the identifier.  */
21193       identifier_token = cp_lexer_peek_token (parser->lexer);
21194       ambiguous_p = identifier_token->error_reported;
21195       identifier = cp_parser_identifier (parser);
21196       /* If the next token isn't an identifier, we are certainly not
21197          looking at a class-name.  */
21198       if (identifier == error_mark_node)
21199         decl = error_mark_node;
21200       /* If we know this is a type-name, there's no need to look it
21201          up.  */
21202       else if (typename_p)
21203         decl = identifier;
21204       else
21205         {
21206           tree ambiguous_decls;
21207           /* If we already know that this lookup is ambiguous, then
21208              we've already issued an error message; there's no reason
21209              to check again.  */
21210           if (ambiguous_p)
21211             {
21212               cp_parser_simulate_error (parser);
21213               return error_mark_node;
21214             }
21215           /* If the next token is a `::', then the name must be a type
21216              name.
21217
21218              [basic.lookup.qual]
21219
21220              During the lookup for a name preceding the :: scope
21221              resolution operator, object, function, and enumerator
21222              names are ignored.  */
21223           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21224             tag_type = scope_type;
21225           /* Look up the name.  */
21226           decl = cp_parser_lookup_name (parser, identifier,
21227                                         tag_type,
21228                                         /*is_template=*/false,
21229                                         /*is_namespace=*/false,
21230                                         check_dependency_p,
21231                                         &ambiguous_decls,
21232                                         identifier_token->location);
21233           if (ambiguous_decls)
21234             {
21235               if (cp_parser_parsing_tentatively (parser))
21236                 cp_parser_simulate_error (parser);
21237               return error_mark_node;
21238             }
21239         }
21240     }
21241   else
21242     {
21243       /* Try a template-id.  */
21244       decl = cp_parser_template_id (parser, template_keyword_p,
21245                                     check_dependency_p,
21246                                     tag_type,
21247                                     is_declaration);
21248       if (decl == error_mark_node)
21249         return error_mark_node;
21250     }
21251
21252   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21253
21254   /* If this is a typename, create a TYPENAME_TYPE.  */
21255   if (typename_p && decl != error_mark_node)
21256     {
21257       decl = make_typename_type (scope, decl, typename_type,
21258                                  /*complain=*/tf_error);
21259       if (decl != error_mark_node)
21260         decl = TYPE_NAME (decl);
21261     }
21262
21263   decl = strip_using_decl (decl);
21264
21265   /* Check to see that it is really the name of a class.  */
21266   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21267       && identifier_p (TREE_OPERAND (decl, 0))
21268       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21269     /* Situations like this:
21270
21271          template <typename T> struct A {
21272            typename T::template X<int>::I i;
21273          };
21274
21275        are problematic.  Is `T::template X<int>' a class-name?  The
21276        standard does not seem to be definitive, but there is no other
21277        valid interpretation of the following `::'.  Therefore, those
21278        names are considered class-names.  */
21279     {
21280       decl = make_typename_type (scope, decl, tag_type, tf_error);
21281       if (decl != error_mark_node)
21282         decl = TYPE_NAME (decl);
21283     }
21284   else if (TREE_CODE (decl) != TYPE_DECL
21285            || TREE_TYPE (decl) == error_mark_node
21286            || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21287                 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21288            /* In Objective-C 2.0, a classname followed by '.' starts a
21289               dot-syntax expression, and it's not a type-name.  */
21290            || (c_dialect_objc ()
21291                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
21292                && objc_is_class_name (decl)))
21293     decl = error_mark_node;
21294
21295   if (decl == error_mark_node)
21296     cp_parser_error (parser, "expected class-name");
21297   else if (identifier && !parser->scope)
21298     maybe_note_name_used_in_class (identifier, decl);
21299
21300   return decl;
21301 }
21302
21303 /* Parse a class-specifier.
21304
21305    class-specifier:
21306      class-head { member-specification [opt] }
21307
21308    Returns the TREE_TYPE representing the class.  */
21309
21310 static tree
21311 cp_parser_class_specifier_1 (cp_parser* parser)
21312 {
21313   tree type;
21314   tree attributes = NULL_TREE;
21315   bool nested_name_specifier_p;
21316   unsigned saved_num_template_parameter_lists;
21317   bool saved_in_function_body;
21318   unsigned char in_statement;
21319   bool in_switch_statement_p;
21320   bool saved_in_unbraced_linkage_specification_p;
21321   tree old_scope = NULL_TREE;
21322   tree scope = NULL_TREE;
21323   cp_token *closing_brace;
21324
21325   push_deferring_access_checks (dk_no_deferred);
21326
21327   /* Parse the class-head.  */
21328   type = cp_parser_class_head (parser,
21329                                &nested_name_specifier_p);
21330   /* If the class-head was a semantic disaster, skip the entire body
21331      of the class.  */
21332   if (!type)
21333     {
21334       cp_parser_skip_to_end_of_block_or_statement (parser);
21335       pop_deferring_access_checks ();
21336       return error_mark_node;
21337     }
21338
21339   /* Look for the `{'.  */
21340   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21341     {
21342       pop_deferring_access_checks ();
21343       return error_mark_node;
21344     }
21345
21346   cp_ensure_no_omp_declare_simd (parser);
21347   cp_ensure_no_oacc_routine (parser);
21348
21349   /* Issue an error message if type-definitions are forbidden here.  */
21350   cp_parser_check_type_definition (parser);
21351   /* Remember that we are defining one more class.  */
21352   ++parser->num_classes_being_defined;
21353   /* Inside the class, surrounding template-parameter-lists do not
21354      apply.  */
21355   saved_num_template_parameter_lists
21356     = parser->num_template_parameter_lists;
21357   parser->num_template_parameter_lists = 0;
21358   /* We are not in a function body.  */
21359   saved_in_function_body = parser->in_function_body;
21360   parser->in_function_body = false;
21361   /* Or in a loop.  */
21362   in_statement = parser->in_statement;
21363   parser->in_statement = 0;
21364   /* Or in a switch.  */
21365   in_switch_statement_p = parser->in_switch_statement_p;
21366   parser->in_switch_statement_p = false;
21367   /* We are not immediately inside an extern "lang" block.  */
21368   saved_in_unbraced_linkage_specification_p
21369     = parser->in_unbraced_linkage_specification_p;
21370   parser->in_unbraced_linkage_specification_p = false;
21371
21372   // Associate constraints with the type.
21373   if (flag_concepts)
21374     type = associate_classtype_constraints (type);
21375
21376   /* Start the class.  */
21377   if (nested_name_specifier_p)
21378     {
21379       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21380       old_scope = push_inner_scope (scope);
21381     }
21382   type = begin_class_definition (type);
21383
21384   if (type == error_mark_node)
21385     /* If the type is erroneous, skip the entire body of the class.  */
21386     cp_parser_skip_to_closing_brace (parser);
21387   else
21388     /* Parse the member-specification.  */
21389     cp_parser_member_specification_opt (parser);
21390
21391   /* Look for the trailing `}'.  */
21392   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21393   /* Look for trailing attributes to apply to this class.  */
21394   if (cp_parser_allow_gnu_extensions_p (parser))
21395     attributes = cp_parser_gnu_attributes_opt (parser);
21396   if (type != error_mark_node)
21397     type = finish_struct (type, attributes);
21398   if (nested_name_specifier_p)
21399     pop_inner_scope (old_scope, scope);
21400
21401   /* We've finished a type definition.  Check for the common syntax
21402      error of forgetting a semicolon after the definition.  We need to
21403      be careful, as we can't just check for not-a-semicolon and be done
21404      with it; the user might have typed:
21405
21406      class X { } c = ...;
21407      class X { } *p = ...;
21408
21409      and so forth.  Instead, enumerate all the possible tokens that
21410      might follow this production; if we don't see one of them, then
21411      complain and silently insert the semicolon.  */
21412   {
21413     cp_token *token = cp_lexer_peek_token (parser->lexer);
21414     bool want_semicolon = true;
21415
21416     if (cp_next_tokens_can_be_std_attribute_p (parser))
21417       /* Don't try to parse c++11 attributes here.  As per the
21418          grammar, that should be a task for
21419          cp_parser_decl_specifier_seq.  */
21420       want_semicolon = false;
21421
21422     switch (token->type)
21423       {
21424       case CPP_NAME:
21425       case CPP_SEMICOLON:
21426       case CPP_MULT:
21427       case CPP_AND:
21428       case CPP_OPEN_PAREN:
21429       case CPP_CLOSE_PAREN:
21430       case CPP_COMMA:
21431         want_semicolon = false;
21432         break;
21433
21434         /* While it's legal for type qualifiers and storage class
21435            specifiers to follow type definitions in the grammar, only
21436            compiler testsuites contain code like that.  Assume that if
21437            we see such code, then what we're really seeing is a case
21438            like:
21439
21440            class X { }
21441            const <type> var = ...;
21442
21443            or
21444
21445            class Y { }
21446            static <type> func (...) ...
21447
21448            i.e. the qualifier or specifier applies to the next
21449            declaration.  To do so, however, we need to look ahead one
21450            more token to see if *that* token is a type specifier.
21451
21452            This code could be improved to handle:
21453
21454            class Z { }
21455            static const <type> var = ...;  */
21456       case CPP_KEYWORD:
21457         if (keyword_is_decl_specifier (token->keyword))
21458           {
21459             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21460
21461             /* Handling user-defined types here would be nice, but very
21462                tricky.  */
21463             want_semicolon
21464               = (lookahead->type == CPP_KEYWORD
21465                  && keyword_begins_type_specifier (lookahead->keyword));
21466           }
21467         break;
21468       default:
21469         break;
21470       }
21471
21472     /* If we don't have a type, then something is very wrong and we
21473        shouldn't try to do anything clever.  Likewise for not seeing the
21474        closing brace.  */
21475     if (closing_brace && TYPE_P (type) && want_semicolon)
21476       {
21477         cp_token_position prev
21478           = cp_lexer_previous_token_position (parser->lexer);
21479         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21480         location_t loc = prev_token->location;
21481
21482         if (CLASSTYPE_DECLARED_CLASS (type))
21483           error_at (loc, "expected %<;%> after class definition");
21484         else if (TREE_CODE (type) == RECORD_TYPE)
21485           error_at (loc, "expected %<;%> after struct definition");
21486         else if (TREE_CODE (type) == UNION_TYPE)
21487           error_at (loc, "expected %<;%> after union definition");
21488         else
21489           gcc_unreachable ();
21490
21491         /* Unget one token and smash it to look as though we encountered
21492            a semicolon in the input stream.  */
21493         cp_lexer_set_token_position (parser->lexer, prev);
21494         token = cp_lexer_peek_token (parser->lexer);
21495         token->type = CPP_SEMICOLON;
21496         token->keyword = RID_MAX;
21497       }
21498   }
21499
21500   /* If this class is not itself within the scope of another class,
21501      then we need to parse the bodies of all of the queued function
21502      definitions.  Note that the queued functions defined in a class
21503      are not always processed immediately following the
21504      class-specifier for that class.  Consider:
21505
21506        struct A {
21507          struct B { void f() { sizeof (A); } };
21508        };
21509
21510      If `f' were processed before the processing of `A' were
21511      completed, there would be no way to compute the size of `A'.
21512      Note that the nesting we are interested in here is lexical --
21513      not the semantic nesting given by TYPE_CONTEXT.  In particular,
21514      for:
21515
21516        struct A { struct B; };
21517        struct A::B { void f() { } };
21518
21519      there is no need to delay the parsing of `A::B::f'.  */
21520   if (--parser->num_classes_being_defined == 0)
21521     {
21522       tree decl;
21523       tree class_type = NULL_TREE;
21524       tree pushed_scope = NULL_TREE;
21525       unsigned ix;
21526       cp_default_arg_entry *e;
21527       tree save_ccp, save_ccr;
21528
21529       /* In a first pass, parse default arguments to the functions.
21530          Then, in a second pass, parse the bodies of the functions.
21531          This two-phased approach handles cases like:
21532
21533             struct S {
21534               void f() { g(); }
21535               void g(int i = 3);
21536             };
21537
21538          */
21539       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21540         {
21541           decl = e->decl;
21542           /* If there are default arguments that have not yet been processed,
21543              take care of them now.  */
21544           if (class_type != e->class_type)
21545             {
21546               if (pushed_scope)
21547                 pop_scope (pushed_scope);
21548               class_type = e->class_type;
21549               pushed_scope = push_scope (class_type);
21550             }
21551           /* Make sure that any template parameters are in scope.  */
21552           maybe_begin_member_template_processing (decl);
21553           /* Parse the default argument expressions.  */
21554           cp_parser_late_parsing_default_args (parser, decl);
21555           /* Remove any template parameters from the symbol table.  */
21556           maybe_end_member_template_processing ();
21557         }
21558       vec_safe_truncate (unparsed_funs_with_default_args, 0);
21559       /* Now parse any NSDMIs.  */
21560       save_ccp = current_class_ptr;
21561       save_ccr = current_class_ref;
21562       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21563         {
21564           if (class_type != DECL_CONTEXT (decl))
21565             {
21566               if (pushed_scope)
21567                 pop_scope (pushed_scope);
21568               class_type = DECL_CONTEXT (decl);
21569               pushed_scope = push_scope (class_type);
21570             }
21571           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21572           cp_parser_late_parsing_nsdmi (parser, decl);
21573         }
21574       vec_safe_truncate (unparsed_nsdmis, 0);
21575       current_class_ptr = save_ccp;
21576       current_class_ref = save_ccr;
21577       if (pushed_scope)
21578         pop_scope (pushed_scope);
21579
21580       /* Now do some post-NSDMI bookkeeping.  */
21581       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21582         after_nsdmi_defaulted_late_checks (class_type);
21583       vec_safe_truncate (unparsed_classes, 0);
21584       after_nsdmi_defaulted_late_checks (type);
21585
21586       /* Now parse the body of the functions.  */
21587       if (flag_openmp)
21588         {
21589           /* OpenMP UDRs need to be parsed before all other functions.  */
21590           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21591             if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21592               cp_parser_late_parsing_for_member (parser, decl);
21593           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21594             if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21595               cp_parser_late_parsing_for_member (parser, decl);
21596         }
21597       else
21598         FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21599           cp_parser_late_parsing_for_member (parser, decl);
21600       vec_safe_truncate (unparsed_funs_with_definitions, 0);
21601     }
21602   else
21603     vec_safe_push (unparsed_classes, type);
21604
21605   /* Put back any saved access checks.  */
21606   pop_deferring_access_checks ();
21607
21608   /* Restore saved state.  */
21609   parser->in_switch_statement_p = in_switch_statement_p;
21610   parser->in_statement = in_statement;
21611   parser->in_function_body = saved_in_function_body;
21612   parser->num_template_parameter_lists
21613     = saved_num_template_parameter_lists;
21614   parser->in_unbraced_linkage_specification_p
21615     = saved_in_unbraced_linkage_specification_p;
21616
21617   return type;
21618 }
21619
21620 static tree
21621 cp_parser_class_specifier (cp_parser* parser)
21622 {
21623   tree ret;
21624   timevar_push (TV_PARSE_STRUCT);
21625   ret = cp_parser_class_specifier_1 (parser);
21626   timevar_pop (TV_PARSE_STRUCT);
21627   return ret;
21628 }
21629
21630 /* Parse a class-head.
21631
21632    class-head:
21633      class-key identifier [opt] base-clause [opt]
21634      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21635      class-key nested-name-specifier [opt] template-id
21636        base-clause [opt]
21637
21638    class-virt-specifier:
21639      final
21640
21641    GNU Extensions:
21642      class-key attributes identifier [opt] base-clause [opt]
21643      class-key attributes nested-name-specifier identifier base-clause [opt]
21644      class-key attributes nested-name-specifier [opt] template-id
21645        base-clause [opt]
21646
21647    Upon return BASES is initialized to the list of base classes (or
21648    NULL, if there are none) in the same form returned by
21649    cp_parser_base_clause.
21650
21651    Returns the TYPE of the indicated class.  Sets
21652    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21653    involving a nested-name-specifier was used, and FALSE otherwise.
21654
21655    Returns error_mark_node if this is not a class-head.
21656
21657    Returns NULL_TREE if the class-head is syntactically valid, but
21658    semantically invalid in a way that means we should skip the entire
21659    body of the class.  */
21660
21661 static tree
21662 cp_parser_class_head (cp_parser* parser,
21663                       bool* nested_name_specifier_p)
21664 {
21665   tree nested_name_specifier;
21666   enum tag_types class_key;
21667   tree id = NULL_TREE;
21668   tree type = NULL_TREE;
21669   tree attributes;
21670   tree bases;
21671   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21672   bool template_id_p = false;
21673   bool qualified_p = false;
21674   bool invalid_nested_name_p = false;
21675   bool invalid_explicit_specialization_p = false;
21676   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21677   tree pushed_scope = NULL_TREE;
21678   unsigned num_templates;
21679   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21680   /* Assume no nested-name-specifier will be present.  */
21681   *nested_name_specifier_p = false;
21682   /* Assume no template parameter lists will be used in defining the
21683      type.  */
21684   num_templates = 0;
21685   parser->colon_corrects_to_scope_p = false;
21686
21687   /* Look for the class-key.  */
21688   class_key = cp_parser_class_key (parser);
21689   if (class_key == none_type)
21690     return error_mark_node;
21691
21692   /* Parse the attributes.  */
21693   attributes = cp_parser_attributes_opt (parser);
21694
21695   /* If the next token is `::', that is invalid -- but sometimes
21696      people do try to write:
21697
21698        struct ::S {};
21699
21700      Handle this gracefully by accepting the extra qualifier, and then
21701      issuing an error about it later if this really is a
21702      class-head.  If it turns out just to be an elaborated type
21703      specifier, remain silent.  */
21704   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21705     qualified_p = true;
21706
21707   push_deferring_access_checks (dk_no_check);
21708
21709   /* Determine the name of the class.  Begin by looking for an
21710      optional nested-name-specifier.  */
21711   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21712   nested_name_specifier
21713     = cp_parser_nested_name_specifier_opt (parser,
21714                                            /*typename_keyword_p=*/false,
21715                                            /*check_dependency_p=*/false,
21716                                            /*type_p=*/true,
21717                                            /*is_declaration=*/false);
21718   /* If there was a nested-name-specifier, then there *must* be an
21719      identifier.  */
21720   if (nested_name_specifier)
21721     {
21722       type_start_token = cp_lexer_peek_token (parser->lexer);
21723       /* Although the grammar says `identifier', it really means
21724          `class-name' or `template-name'.  You are only allowed to
21725          define a class that has already been declared with this
21726          syntax.
21727
21728          The proposed resolution for Core Issue 180 says that wherever
21729          you see `class T::X' you should treat `X' as a type-name.
21730
21731          It is OK to define an inaccessible class; for example:
21732
21733            class A { class B; };
21734            class A::B {};
21735
21736          We do not know if we will see a class-name, or a
21737          template-name.  We look for a class-name first, in case the
21738          class-name is a template-id; if we looked for the
21739          template-name first we would stop after the template-name.  */
21740       cp_parser_parse_tentatively (parser);
21741       type = cp_parser_class_name (parser,
21742                                    /*typename_keyword_p=*/false,
21743                                    /*template_keyword_p=*/false,
21744                                    class_type,
21745                                    /*check_dependency_p=*/false,
21746                                    /*class_head_p=*/true,
21747                                    /*is_declaration=*/false);
21748       /* If that didn't work, ignore the nested-name-specifier.  */
21749       if (!cp_parser_parse_definitely (parser))
21750         {
21751           invalid_nested_name_p = true;
21752           type_start_token = cp_lexer_peek_token (parser->lexer);
21753           id = cp_parser_identifier (parser);
21754           if (id == error_mark_node)
21755             id = NULL_TREE;
21756         }
21757       /* If we could not find a corresponding TYPE, treat this
21758          declaration like an unqualified declaration.  */
21759       if (type == error_mark_node)
21760         nested_name_specifier = NULL_TREE;
21761       /* Otherwise, count the number of templates used in TYPE and its
21762          containing scopes.  */
21763       else
21764         {
21765           tree scope;
21766
21767           for (scope = TREE_TYPE (type);
21768                scope && TREE_CODE (scope) != NAMESPACE_DECL;
21769                scope = get_containing_scope (scope))
21770             if (TYPE_P (scope)
21771                 && CLASS_TYPE_P (scope)
21772                 && CLASSTYPE_TEMPLATE_INFO (scope)
21773                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21774                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21775                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21776               ++num_templates;
21777         }
21778     }
21779   /* Otherwise, the identifier is optional.  */
21780   else
21781     {
21782       /* We don't know whether what comes next is a template-id,
21783          an identifier, or nothing at all.  */
21784       cp_parser_parse_tentatively (parser);
21785       /* Check for a template-id.  */
21786       type_start_token = cp_lexer_peek_token (parser->lexer);
21787       id = cp_parser_template_id (parser,
21788                                   /*template_keyword_p=*/false,
21789                                   /*check_dependency_p=*/true,
21790                                   class_key,
21791                                   /*is_declaration=*/true);
21792       /* If that didn't work, it could still be an identifier.  */
21793       if (!cp_parser_parse_definitely (parser))
21794         {
21795           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21796             {
21797               type_start_token = cp_lexer_peek_token (parser->lexer);
21798               id = cp_parser_identifier (parser);
21799             }
21800           else
21801             id = NULL_TREE;
21802         }
21803       else
21804         {
21805           template_id_p = true;
21806           ++num_templates;
21807         }
21808     }
21809
21810   pop_deferring_access_checks ();
21811
21812   if (id)
21813     {
21814       cp_parser_check_for_invalid_template_id (parser, id,
21815                                                class_key,
21816                                                type_start_token->location);
21817     }
21818   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21819
21820   /* If it's not a `:' or a `{' then we can't really be looking at a
21821      class-head, since a class-head only appears as part of a
21822      class-specifier.  We have to detect this situation before calling
21823      xref_tag, since that has irreversible side-effects.  */
21824   if (!cp_parser_next_token_starts_class_definition_p (parser))
21825     {
21826       cp_parser_error (parser, "expected %<{%> or %<:%>");
21827       type = error_mark_node;
21828       goto out;
21829     }
21830
21831   /* At this point, we're going ahead with the class-specifier, even
21832      if some other problem occurs.  */
21833   cp_parser_commit_to_tentative_parse (parser);
21834   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21835     {
21836       cp_parser_error (parser,
21837                        "cannot specify %<override%> for a class");
21838       type = error_mark_node;
21839       goto out;
21840     }
21841   /* Issue the error about the overly-qualified name now.  */
21842   if (qualified_p)
21843     {
21844       cp_parser_error (parser,
21845                        "global qualification of class name is invalid");
21846       type = error_mark_node;
21847       goto out;
21848     }
21849   else if (invalid_nested_name_p)
21850     {
21851       cp_parser_error (parser,
21852                        "qualified name does not name a class");
21853       type = error_mark_node;
21854       goto out;
21855     }
21856   else if (nested_name_specifier)
21857     {
21858       tree scope;
21859
21860       /* Reject typedef-names in class heads.  */
21861       if (!DECL_IMPLICIT_TYPEDEF_P (type))
21862         {
21863           error_at (type_start_token->location,
21864                     "invalid class name in declaration of %qD",
21865                     type);
21866           type = NULL_TREE;
21867           goto done;
21868         }
21869
21870       /* Figure out in what scope the declaration is being placed.  */
21871       scope = current_scope ();
21872       /* If that scope does not contain the scope in which the
21873          class was originally declared, the program is invalid.  */
21874       if (scope && !is_ancestor (scope, nested_name_specifier))
21875         {
21876           if (at_namespace_scope_p ())
21877             error_at (type_start_token->location,
21878                       "declaration of %qD in namespace %qD which does not "
21879                       "enclose %qD",
21880                       type, scope, nested_name_specifier);
21881           else
21882             error_at (type_start_token->location,
21883                       "declaration of %qD in %qD which does not enclose %qD",
21884                       type, scope, nested_name_specifier);
21885           type = NULL_TREE;
21886           goto done;
21887         }
21888       /* [dcl.meaning]
21889
21890          A declarator-id shall not be qualified except for the
21891          definition of a ... nested class outside of its class
21892          ... [or] the definition or explicit instantiation of a
21893          class member of a namespace outside of its namespace.  */
21894       if (scope == nested_name_specifier)
21895         {
21896           permerror (nested_name_specifier_token_start->location,
21897                      "extra qualification not allowed");
21898           nested_name_specifier = NULL_TREE;
21899           num_templates = 0;
21900         }
21901     }
21902   /* An explicit-specialization must be preceded by "template <>".  If
21903      it is not, try to recover gracefully.  */
21904   if (at_namespace_scope_p ()
21905       && parser->num_template_parameter_lists == 0
21906       && template_id_p)
21907     {
21908       error_at (type_start_token->location,
21909                 "an explicit specialization must be preceded by %<template <>%>");
21910       invalid_explicit_specialization_p = true;
21911       /* Take the same action that would have been taken by
21912          cp_parser_explicit_specialization.  */
21913       ++parser->num_template_parameter_lists;
21914       begin_specialization ();
21915     }
21916   /* There must be no "return" statements between this point and the
21917      end of this function; set "type "to the correct return value and
21918      use "goto done;" to return.  */
21919   /* Make sure that the right number of template parameters were
21920      present.  */
21921   if (!cp_parser_check_template_parameters (parser, num_templates,
21922                                             type_start_token->location,
21923                                             /*declarator=*/NULL))
21924     {
21925       /* If something went wrong, there is no point in even trying to
21926          process the class-definition.  */
21927       type = NULL_TREE;
21928       goto done;
21929     }
21930
21931   /* Look up the type.  */
21932   if (template_id_p)
21933     {
21934       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21935           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21936               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21937         {
21938           error_at (type_start_token->location,
21939                     "function template %qD redeclared as a class template", id);
21940           type = error_mark_node;
21941         }
21942       else
21943         {
21944           type = TREE_TYPE (id);
21945           type = maybe_process_partial_specialization (type);
21946         }
21947       if (nested_name_specifier)
21948         pushed_scope = push_scope (nested_name_specifier);
21949     }
21950   else if (nested_name_specifier)
21951     {
21952       tree class_type;
21953
21954       /* Given:
21955
21956             template <typename T> struct S { struct T };
21957             template <typename T> struct S<T>::T { };
21958
21959          we will get a TYPENAME_TYPE when processing the definition of
21960          `S::T'.  We need to resolve it to the actual type before we
21961          try to define it.  */
21962       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21963         {
21964           class_type = resolve_typename_type (TREE_TYPE (type),
21965                                               /*only_current_p=*/false);
21966           if (TREE_CODE (class_type) != TYPENAME_TYPE)
21967             type = TYPE_NAME (class_type);
21968           else
21969             {
21970               cp_parser_error (parser, "could not resolve typename type");
21971               type = error_mark_node;
21972             }
21973         }
21974
21975       if (maybe_process_partial_specialization (TREE_TYPE (type))
21976           == error_mark_node)
21977         {
21978           type = NULL_TREE;
21979           goto done;
21980         }
21981
21982       class_type = current_class_type;
21983       /* Enter the scope indicated by the nested-name-specifier.  */
21984       pushed_scope = push_scope (nested_name_specifier);
21985       /* Get the canonical version of this type.  */
21986       type = TYPE_MAIN_DECL (TREE_TYPE (type));
21987       /* Call push_template_decl if it seems like we should be defining a
21988          template either from the template headers or the type we're
21989          defining, so that we diagnose both extra and missing headers.  */
21990       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21991            || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21992           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21993         {
21994           type = push_template_decl (type);
21995           if (type == error_mark_node)
21996             {
21997               type = NULL_TREE;
21998               goto done;
21999             }
22000         }
22001
22002       type = TREE_TYPE (type);
22003       *nested_name_specifier_p = true;
22004     }
22005   else      /* The name is not a nested name.  */
22006     {
22007       /* If the class was unnamed, create a dummy name.  */
22008       if (!id)
22009         id = make_anon_name ();
22010       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22011                        parser->num_template_parameter_lists);
22012     }
22013
22014   /* Indicate whether this class was declared as a `class' or as a
22015      `struct'.  */
22016   if (TREE_CODE (type) == RECORD_TYPE)
22017     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22018   cp_parser_check_class_key (class_key, type);
22019
22020   /* If this type was already complete, and we see another definition,
22021      that's an error.  */
22022   if (type != error_mark_node && COMPLETE_TYPE_P (type))
22023     {
22024       error_at (type_start_token->location, "redefinition of %q#T",
22025                 type);
22026       error_at (type_start_token->location, "previous definition of %q+#T",
22027                 type);
22028       type = NULL_TREE;
22029       goto done;
22030     }
22031   else if (type == error_mark_node)
22032     type = NULL_TREE;
22033
22034   if (type)
22035     {
22036       /* Apply attributes now, before any use of the class as a template
22037          argument in its base list.  */
22038       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22039       fixup_attribute_variants (type);
22040     }
22041
22042   /* We will have entered the scope containing the class; the names of
22043      base classes should be looked up in that context.  For example:
22044
22045        struct A { struct B {}; struct C; };
22046        struct A::C : B {};
22047
22048      is valid.  */
22049
22050   /* Get the list of base-classes, if there is one.  */
22051   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22052     {
22053       /* PR59482: enter the class scope so that base-specifiers are looked
22054          up correctly.  */
22055       if (type)
22056         pushclass (type);
22057       bases = cp_parser_base_clause (parser);
22058       /* PR59482: get out of the previously pushed class scope so that the
22059          subsequent pops pop the right thing.  */
22060       if (type)
22061         popclass ();
22062     }
22063   else
22064     bases = NULL_TREE;
22065
22066   /* If we're really defining a class, process the base classes.
22067      If they're invalid, fail.  */
22068   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22069       && !xref_basetypes (type, bases))
22070     type = NULL_TREE;
22071
22072  done:
22073   /* Leave the scope given by the nested-name-specifier.  We will
22074      enter the class scope itself while processing the members.  */
22075   if (pushed_scope)
22076     pop_scope (pushed_scope);
22077
22078   if (invalid_explicit_specialization_p)
22079     {
22080       end_specialization ();
22081       --parser->num_template_parameter_lists;
22082     }
22083
22084   if (type)
22085     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22086   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22087     CLASSTYPE_FINAL (type) = 1;
22088  out:
22089   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22090   return type;
22091 }
22092
22093 /* Parse a class-key.
22094
22095    class-key:
22096      class
22097      struct
22098      union
22099
22100    Returns the kind of class-key specified, or none_type to indicate
22101    error.  */
22102
22103 static enum tag_types
22104 cp_parser_class_key (cp_parser* parser)
22105 {
22106   cp_token *token;
22107   enum tag_types tag_type;
22108
22109   /* Look for the class-key.  */
22110   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22111   if (!token)
22112     return none_type;
22113
22114   /* Check to see if the TOKEN is a class-key.  */
22115   tag_type = cp_parser_token_is_class_key (token);
22116   if (!tag_type)
22117     cp_parser_error (parser, "expected class-key");
22118   return tag_type;
22119 }
22120
22121 /* Parse a type-parameter-key.
22122
22123    type-parameter-key:
22124      class
22125      typename
22126  */
22127
22128 static void
22129 cp_parser_type_parameter_key (cp_parser* parser)
22130 {
22131   /* Look for the type-parameter-key.  */
22132   enum tag_types tag_type = none_type;
22133   cp_token *token = cp_lexer_peek_token (parser->lexer);
22134   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22135     {
22136       cp_lexer_consume_token (parser->lexer);
22137       if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22138         /* typename is not allowed in a template template parameter
22139            by the standard until C++1Z.  */
22140         pedwarn (token->location, OPT_Wpedantic, 
22141                  "ISO C++ forbids typename key in template template parameter;"
22142                  " use -std=c++1z or -std=gnu++1z");
22143     }
22144   else
22145     cp_parser_error (parser, "expected %<class%> or %<typename%>");
22146
22147   return;
22148 }
22149
22150 /* Parse an (optional) member-specification.
22151
22152    member-specification:
22153      member-declaration member-specification [opt]
22154      access-specifier : member-specification [opt]  */
22155
22156 static void
22157 cp_parser_member_specification_opt (cp_parser* parser)
22158 {
22159   while (true)
22160     {
22161       cp_token *token;
22162       enum rid keyword;
22163
22164       /* Peek at the next token.  */
22165       token = cp_lexer_peek_token (parser->lexer);
22166       /* If it's a `}', or EOF then we've seen all the members.  */
22167       if (token->type == CPP_CLOSE_BRACE
22168           || token->type == CPP_EOF
22169           || token->type == CPP_PRAGMA_EOL)
22170         break;
22171
22172       /* See if this token is a keyword.  */
22173       keyword = token->keyword;
22174       switch (keyword)
22175         {
22176         case RID_PUBLIC:
22177         case RID_PROTECTED:
22178         case RID_PRIVATE:
22179           /* Consume the access-specifier.  */
22180           cp_lexer_consume_token (parser->lexer);
22181           /* Remember which access-specifier is active.  */
22182           current_access_specifier = token->u.value;
22183           /* Look for the `:'.  */
22184           cp_parser_require (parser, CPP_COLON, RT_COLON);
22185           break;
22186
22187         default:
22188           /* Accept #pragmas at class scope.  */
22189           if (token->type == CPP_PRAGMA)
22190             {
22191               cp_parser_pragma (parser, pragma_member, NULL);
22192               break;
22193             }
22194
22195           /* Otherwise, the next construction must be a
22196              member-declaration.  */
22197           cp_parser_member_declaration (parser);
22198         }
22199     }
22200 }
22201
22202 /* Parse a member-declaration.
22203
22204    member-declaration:
22205      decl-specifier-seq [opt] member-declarator-list [opt] ;
22206      function-definition ; [opt]
22207      :: [opt] nested-name-specifier template [opt] unqualified-id ;
22208      using-declaration
22209      template-declaration
22210      alias-declaration
22211
22212    member-declarator-list:
22213      member-declarator
22214      member-declarator-list , member-declarator
22215
22216    member-declarator:
22217      declarator pure-specifier [opt]
22218      declarator constant-initializer [opt]
22219      identifier [opt] : constant-expression
22220
22221    GNU Extensions:
22222
22223    member-declaration:
22224      __extension__ member-declaration
22225
22226    member-declarator:
22227      declarator attributes [opt] pure-specifier [opt]
22228      declarator attributes [opt] constant-initializer [opt]
22229      identifier [opt] attributes [opt] : constant-expression  
22230
22231    C++0x Extensions:
22232
22233    member-declaration:
22234      static_assert-declaration  */
22235
22236 static void
22237 cp_parser_member_declaration (cp_parser* parser)
22238 {
22239   cp_decl_specifier_seq decl_specifiers;
22240   tree prefix_attributes;
22241   tree decl;
22242   int declares_class_or_enum;
22243   bool friend_p;
22244   cp_token *token = NULL;
22245   cp_token *decl_spec_token_start = NULL;
22246   cp_token *initializer_token_start = NULL;
22247   int saved_pedantic;
22248   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22249
22250   /* Check for the `__extension__' keyword.  */
22251   if (cp_parser_extension_opt (parser, &saved_pedantic))
22252     {
22253       /* Recurse.  */
22254       cp_parser_member_declaration (parser);
22255       /* Restore the old value of the PEDANTIC flag.  */
22256       pedantic = saved_pedantic;
22257
22258       return;
22259     }
22260
22261   /* Check for a template-declaration.  */
22262   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22263     {
22264       /* An explicit specialization here is an error condition, and we
22265          expect the specialization handler to detect and report this.  */
22266       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22267           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22268         cp_parser_explicit_specialization (parser);
22269       else
22270         cp_parser_template_declaration (parser, /*member_p=*/true);
22271
22272       return;
22273     }
22274   /* Check for a template introduction.  */
22275   else if (cp_parser_template_declaration_after_export (parser, true))
22276     return;
22277
22278   /* Check for a using-declaration.  */
22279   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22280     {
22281       if (cxx_dialect < cxx11)
22282         {
22283           /* Parse the using-declaration.  */
22284           cp_parser_using_declaration (parser,
22285                                        /*access_declaration_p=*/false);
22286           return;
22287         }
22288       else
22289         {
22290           tree decl;
22291           bool alias_decl_expected;
22292           cp_parser_parse_tentatively (parser);
22293           decl = cp_parser_alias_declaration (parser);
22294           /* Note that if we actually see the '=' token after the
22295              identifier, cp_parser_alias_declaration commits the
22296              tentative parse.  In that case, we really expect an
22297              alias-declaration.  Otherwise, we expect a using
22298              declaration.  */
22299           alias_decl_expected =
22300             !cp_parser_uncommitted_to_tentative_parse_p (parser);
22301           cp_parser_parse_definitely (parser);
22302
22303           if (alias_decl_expected)
22304             finish_member_declaration (decl);
22305           else
22306             cp_parser_using_declaration (parser,
22307                                          /*access_declaration_p=*/false);
22308           return;
22309         }
22310     }
22311
22312   /* Check for @defs.  */
22313   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22314     {
22315       tree ivar, member;
22316       tree ivar_chains = cp_parser_objc_defs_expression (parser);
22317       ivar = ivar_chains;
22318       while (ivar)
22319         {
22320           member = ivar;
22321           ivar = TREE_CHAIN (member);
22322           TREE_CHAIN (member) = NULL_TREE;
22323           finish_member_declaration (member);
22324         }
22325       return;
22326     }
22327
22328   /* If the next token is `static_assert' we have a static assertion.  */
22329   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22330     {
22331       cp_parser_static_assert (parser, /*member_p=*/true);
22332       return;
22333     }
22334
22335   parser->colon_corrects_to_scope_p = false;
22336
22337   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22338       goto out;
22339
22340   /* Parse the decl-specifier-seq.  */
22341   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22342   cp_parser_decl_specifier_seq (parser,
22343                                 CP_PARSER_FLAGS_OPTIONAL,
22344                                 &decl_specifiers,
22345                                 &declares_class_or_enum);
22346   /* Check for an invalid type-name.  */
22347   if (!decl_specifiers.any_type_specifiers_p
22348       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22349     goto out;
22350   /* If there is no declarator, then the decl-specifier-seq should
22351      specify a type.  */
22352   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22353     {
22354       /* If there was no decl-specifier-seq, and the next token is a
22355          `;', then we have something like:
22356
22357            struct S { ; };
22358
22359          [class.mem]
22360
22361          Each member-declaration shall declare at least one member
22362          name of the class.  */
22363       if (!decl_specifiers.any_specifiers_p)
22364         {
22365           cp_token *token = cp_lexer_peek_token (parser->lexer);
22366           if (!in_system_header_at (token->location))
22367             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22368         }
22369       else
22370         {
22371           tree type;
22372
22373           /* See if this declaration is a friend.  */
22374           friend_p = cp_parser_friend_p (&decl_specifiers);
22375           /* If there were decl-specifiers, check to see if there was
22376              a class-declaration.  */
22377           type = check_tag_decl (&decl_specifiers,
22378                                  /*explicit_type_instantiation_p=*/false);
22379           /* Nested classes have already been added to the class, but
22380              a `friend' needs to be explicitly registered.  */
22381           if (friend_p)
22382             {
22383               /* If the `friend' keyword was present, the friend must
22384                  be introduced with a class-key.  */
22385                if (!declares_class_or_enum && cxx_dialect < cxx11)
22386                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22387                           "in C++03 a class-key must be used "
22388                           "when declaring a friend");
22389                /* In this case:
22390
22391                     template <typename T> struct A {
22392                       friend struct A<T>::B;
22393                     };
22394
22395                   A<T>::B will be represented by a TYPENAME_TYPE, and
22396                   therefore not recognized by check_tag_decl.  */
22397                if (!type)
22398                  {
22399                    type = decl_specifiers.type;
22400                    if (type && TREE_CODE (type) == TYPE_DECL)
22401                      type = TREE_TYPE (type);
22402                  }
22403                if (!type || !TYPE_P (type))
22404                  error_at (decl_spec_token_start->location,
22405                            "friend declaration does not name a class or "
22406                            "function");
22407                else
22408                  make_friend_class (current_class_type, type,
22409                                     /*complain=*/true);
22410             }
22411           /* If there is no TYPE, an error message will already have
22412              been issued.  */
22413           else if (!type || type == error_mark_node)
22414             ;
22415           /* An anonymous aggregate has to be handled specially; such
22416              a declaration really declares a data member (with a
22417              particular type), as opposed to a nested class.  */
22418           else if (ANON_AGGR_TYPE_P (type))
22419             {
22420               /* C++11 9.5/6.  */
22421               if (decl_specifiers.storage_class != sc_none)
22422                 error_at (decl_spec_token_start->location,
22423                           "a storage class on an anonymous aggregate "
22424                           "in class scope is not allowed");
22425
22426               /* Remove constructors and such from TYPE, now that we
22427                  know it is an anonymous aggregate.  */
22428               fixup_anonymous_aggr (type);
22429               /* And make the corresponding data member.  */
22430               decl = build_decl (decl_spec_token_start->location,
22431                                  FIELD_DECL, NULL_TREE, type);
22432               /* Add it to the class.  */
22433               finish_member_declaration (decl);
22434             }
22435           else
22436             cp_parser_check_access_in_redeclaration
22437                                               (TYPE_NAME (type),
22438                                                decl_spec_token_start->location);
22439         }
22440     }
22441   else
22442     {
22443       bool assume_semicolon = false;
22444
22445       /* Clear attributes from the decl_specifiers but keep them
22446          around as prefix attributes that apply them to the entity
22447          being declared.  */
22448       prefix_attributes = decl_specifiers.attributes;
22449       decl_specifiers.attributes = NULL_TREE;
22450
22451       /* See if these declarations will be friends.  */
22452       friend_p = cp_parser_friend_p (&decl_specifiers);
22453
22454       /* Keep going until we hit the `;' at the end of the
22455          declaration.  */
22456       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22457         {
22458           tree attributes = NULL_TREE;
22459           tree first_attribute;
22460
22461           /* Peek at the next token.  */
22462           token = cp_lexer_peek_token (parser->lexer);
22463
22464           /* Check for a bitfield declaration.  */
22465           if (token->type == CPP_COLON
22466               || (token->type == CPP_NAME
22467                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22468                   == CPP_COLON))
22469             {
22470               tree identifier;
22471               tree width;
22472
22473               /* Get the name of the bitfield.  Note that we cannot just
22474                  check TOKEN here because it may have been invalidated by
22475                  the call to cp_lexer_peek_nth_token above.  */
22476               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22477                 identifier = cp_parser_identifier (parser);
22478               else
22479                 identifier = NULL_TREE;
22480
22481               /* Consume the `:' token.  */
22482               cp_lexer_consume_token (parser->lexer);
22483               /* Get the width of the bitfield.  */
22484               width
22485                 = cp_parser_constant_expression (parser);
22486
22487               /* Look for attributes that apply to the bitfield.  */
22488               attributes = cp_parser_attributes_opt (parser);
22489               /* Remember which attributes are prefix attributes and
22490                  which are not.  */
22491               first_attribute = attributes;
22492               /* Combine the attributes.  */
22493               attributes = chainon (prefix_attributes, attributes);
22494
22495               /* Create the bitfield declaration.  */
22496               decl = grokbitfield (identifier
22497                                    ? make_id_declarator (NULL_TREE,
22498                                                          identifier,
22499                                                          sfk_none)
22500                                    : NULL,
22501                                    &decl_specifiers,
22502                                    width,
22503                                    attributes);
22504             }
22505           else
22506             {
22507               cp_declarator *declarator;
22508               tree initializer;
22509               tree asm_specification;
22510               int ctor_dtor_or_conv_p;
22511
22512               /* Parse the declarator.  */
22513               declarator
22514                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22515                                         &ctor_dtor_or_conv_p,
22516                                         /*parenthesized_p=*/NULL,
22517                                         /*member_p=*/true,
22518                                         friend_p);
22519
22520               /* If something went wrong parsing the declarator, make sure
22521                  that we at least consume some tokens.  */
22522               if (declarator == cp_error_declarator)
22523                 {
22524                   /* Skip to the end of the statement.  */
22525                   cp_parser_skip_to_end_of_statement (parser);
22526                   /* If the next token is not a semicolon, that is
22527                      probably because we just skipped over the body of
22528                      a function.  So, we consume a semicolon if
22529                      present, but do not issue an error message if it
22530                      is not present.  */
22531                   if (cp_lexer_next_token_is (parser->lexer,
22532                                               CPP_SEMICOLON))
22533                     cp_lexer_consume_token (parser->lexer);
22534                   goto out;
22535                 }
22536
22537               if (declares_class_or_enum & 2)
22538                 cp_parser_check_for_definition_in_return_type
22539                                             (declarator, decl_specifiers.type,
22540                                              decl_specifiers.locations[ds_type_spec]);
22541
22542               /* Look for an asm-specification.  */
22543               asm_specification = cp_parser_asm_specification_opt (parser);
22544               /* Look for attributes that apply to the declaration.  */
22545               attributes = cp_parser_attributes_opt (parser);
22546               /* Remember which attributes are prefix attributes and
22547                  which are not.  */
22548               first_attribute = attributes;
22549               /* Combine the attributes.  */
22550               attributes = chainon (prefix_attributes, attributes);
22551
22552               /* If it's an `=', then we have a constant-initializer or a
22553                  pure-specifier.  It is not correct to parse the
22554                  initializer before registering the member declaration
22555                  since the member declaration should be in scope while
22556                  its initializer is processed.  However, the rest of the
22557                  front end does not yet provide an interface that allows
22558                  us to handle this correctly.  */
22559               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22560                 {
22561                   /* In [class.mem]:
22562
22563                      A pure-specifier shall be used only in the declaration of
22564                      a virtual function.
22565
22566                      A member-declarator can contain a constant-initializer
22567                      only if it declares a static member of integral or
22568                      enumeration type.
22569
22570                      Therefore, if the DECLARATOR is for a function, we look
22571                      for a pure-specifier; otherwise, we look for a
22572                      constant-initializer.  When we call `grokfield', it will
22573                      perform more stringent semantics checks.  */
22574                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
22575                   if (function_declarator_p (declarator)
22576                       || (decl_specifiers.type
22577                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22578                           && declarator->kind == cdk_id
22579                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22580                               == FUNCTION_TYPE)))
22581                     initializer = cp_parser_pure_specifier (parser);
22582                   else if (decl_specifiers.storage_class != sc_static)
22583                     initializer = cp_parser_save_nsdmi (parser);
22584                   else if (cxx_dialect >= cxx11)
22585                     {
22586                       bool nonconst;
22587                       /* Don't require a constant rvalue in C++11, since we
22588                          might want a reference constant.  We'll enforce
22589                          constancy later.  */
22590                       cp_lexer_consume_token (parser->lexer);
22591                       /* Parse the initializer.  */
22592                       initializer = cp_parser_initializer_clause (parser,
22593                                                                   &nonconst);
22594                     }
22595                   else
22596                     /* Parse the initializer.  */
22597                     initializer = cp_parser_constant_initializer (parser);
22598                 }
22599               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22600                        && !function_declarator_p (declarator))
22601                 {
22602                   bool x;
22603                   if (decl_specifiers.storage_class != sc_static)
22604                     initializer = cp_parser_save_nsdmi (parser);
22605                   else
22606                     initializer = cp_parser_initializer (parser, &x, &x);
22607                 }
22608               /* Otherwise, there is no initializer.  */
22609               else
22610                 initializer = NULL_TREE;
22611
22612               /* See if we are probably looking at a function
22613                  definition.  We are certainly not looking at a
22614                  member-declarator.  Calling `grokfield' has
22615                  side-effects, so we must not do it unless we are sure
22616                  that we are looking at a member-declarator.  */
22617               if (cp_parser_token_starts_function_definition_p
22618                   (cp_lexer_peek_token (parser->lexer)))
22619                 {
22620                   /* The grammar does not allow a pure-specifier to be
22621                      used when a member function is defined.  (It is
22622                      possible that this fact is an oversight in the
22623                      standard, since a pure function may be defined
22624                      outside of the class-specifier.  */
22625                   if (initializer && initializer_token_start)
22626                     error_at (initializer_token_start->location,
22627                               "pure-specifier on function-definition");
22628                   decl = cp_parser_save_member_function_body (parser,
22629                                                               &decl_specifiers,
22630                                                               declarator,
22631                                                               attributes);
22632                   if (parser->fully_implicit_function_template_p)
22633                     decl = finish_fully_implicit_template (parser, decl);
22634                   /* If the member was not a friend, declare it here.  */
22635                   if (!friend_p)
22636                     finish_member_declaration (decl);
22637                   /* Peek at the next token.  */
22638                   token = cp_lexer_peek_token (parser->lexer);
22639                   /* If the next token is a semicolon, consume it.  */
22640                   if (token->type == CPP_SEMICOLON)
22641                     cp_lexer_consume_token (parser->lexer);
22642                   goto out;
22643                 }
22644               else
22645                 if (declarator->kind == cdk_function)
22646                   declarator->id_loc = token->location;
22647               /* Create the declaration.  */
22648               decl = grokfield (declarator, &decl_specifiers,
22649                                 initializer, /*init_const_expr_p=*/true,
22650                                 asm_specification, attributes);
22651               if (parser->fully_implicit_function_template_p)
22652                 {
22653                   if (friend_p)
22654                     finish_fully_implicit_template (parser, 0);
22655                   else
22656                     decl = finish_fully_implicit_template (parser, decl);
22657                 }
22658             }
22659
22660           cp_finalize_omp_declare_simd (parser, decl);
22661           cp_finalize_oacc_routine (parser, decl, false);
22662
22663           /* Reset PREFIX_ATTRIBUTES.  */
22664           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22665             attributes = TREE_CHAIN (attributes);
22666           if (attributes)
22667             TREE_CHAIN (attributes) = NULL_TREE;
22668
22669           /* If there is any qualification still in effect, clear it
22670              now; we will be starting fresh with the next declarator.  */
22671           parser->scope = NULL_TREE;
22672           parser->qualifying_scope = NULL_TREE;
22673           parser->object_scope = NULL_TREE;
22674           /* If it's a `,', then there are more declarators.  */
22675           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22676             {
22677               cp_lexer_consume_token (parser->lexer);
22678               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22679                 {
22680                   cp_token *token = cp_lexer_previous_token (parser->lexer);
22681                   error_at (token->location,
22682                             "stray %<,%> at end of member declaration");
22683                 }
22684             }
22685           /* If the next token isn't a `;', then we have a parse error.  */
22686           else if (cp_lexer_next_token_is_not (parser->lexer,
22687                                                CPP_SEMICOLON))
22688             {
22689               /* The next token might be a ways away from where the
22690                  actual semicolon is missing.  Find the previous token
22691                  and use that for our error position.  */
22692               cp_token *token = cp_lexer_previous_token (parser->lexer);
22693               error_at (token->location,
22694                         "expected %<;%> at end of member declaration");
22695
22696               /* Assume that the user meant to provide a semicolon.  If
22697                  we were to cp_parser_skip_to_end_of_statement, we might
22698                  skip to a semicolon inside a member function definition
22699                  and issue nonsensical error messages.  */
22700               assume_semicolon = true;
22701             }
22702
22703           if (decl)
22704             {
22705               /* Add DECL to the list of members.  */
22706               if (!friend_p
22707                   /* Explicitly include, eg, NSDMIs, for better error
22708                      recovery (c++/58650).  */
22709                   || !DECL_DECLARES_FUNCTION_P (decl))
22710                 finish_member_declaration (decl);
22711
22712               if (TREE_CODE (decl) == FUNCTION_DECL)
22713                 cp_parser_save_default_args (parser, decl);
22714               else if (TREE_CODE (decl) == FIELD_DECL
22715                        && !DECL_C_BIT_FIELD (decl)
22716                        && DECL_INITIAL (decl))
22717                 /* Add DECL to the queue of NSDMI to be parsed later.  */
22718                 vec_safe_push (unparsed_nsdmis, decl);
22719             }
22720
22721           if (assume_semicolon)
22722             goto out;
22723         }
22724     }
22725
22726   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22727  out:
22728   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22729 }
22730
22731 /* Parse a pure-specifier.
22732
22733    pure-specifier:
22734      = 0
22735
22736    Returns INTEGER_ZERO_NODE if a pure specifier is found.
22737    Otherwise, ERROR_MARK_NODE is returned.  */
22738
22739 static tree
22740 cp_parser_pure_specifier (cp_parser* parser)
22741 {
22742   cp_token *token;
22743
22744   /* Look for the `=' token.  */
22745   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22746     return error_mark_node;
22747   /* Look for the `0' token.  */
22748   token = cp_lexer_peek_token (parser->lexer);
22749
22750   if (token->type == CPP_EOF
22751       || token->type == CPP_PRAGMA_EOL)
22752     return error_mark_node;
22753
22754   cp_lexer_consume_token (parser->lexer);
22755
22756   /* Accept = default or = delete in c++0x mode.  */
22757   if (token->keyword == RID_DEFAULT
22758       || token->keyword == RID_DELETE)
22759     {
22760       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22761       return token->u.value;
22762     }
22763
22764   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
22765   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22766     {
22767       cp_parser_error (parser,
22768                        "invalid pure specifier (only %<= 0%> is allowed)");
22769       cp_parser_skip_to_end_of_statement (parser);
22770       return error_mark_node;
22771     }
22772   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22773     {
22774       error_at (token->location, "templates may not be %<virtual%>");
22775       return error_mark_node;
22776     }
22777
22778   return integer_zero_node;
22779 }
22780
22781 /* Parse a constant-initializer.
22782
22783    constant-initializer:
22784      = constant-expression
22785
22786    Returns a representation of the constant-expression.  */
22787
22788 static tree
22789 cp_parser_constant_initializer (cp_parser* parser)
22790 {
22791   /* Look for the `=' token.  */
22792   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22793     return error_mark_node;
22794
22795   /* It is invalid to write:
22796
22797        struct S { static const int i = { 7 }; };
22798
22799      */
22800   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22801     {
22802       cp_parser_error (parser,
22803                        "a brace-enclosed initializer is not allowed here");
22804       /* Consume the opening brace.  */
22805       cp_lexer_consume_token (parser->lexer);
22806       /* Skip the initializer.  */
22807       cp_parser_skip_to_closing_brace (parser);
22808       /* Look for the trailing `}'.  */
22809       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22810
22811       return error_mark_node;
22812     }
22813
22814   return cp_parser_constant_expression (parser);
22815 }
22816
22817 /* Derived classes [gram.class.derived] */
22818
22819 /* Parse a base-clause.
22820
22821    base-clause:
22822      : base-specifier-list
22823
22824    base-specifier-list:
22825      base-specifier ... [opt]
22826      base-specifier-list , base-specifier ... [opt]
22827
22828    Returns a TREE_LIST representing the base-classes, in the order in
22829    which they were declared.  The representation of each node is as
22830    described by cp_parser_base_specifier.
22831
22832    In the case that no bases are specified, this function will return
22833    NULL_TREE, not ERROR_MARK_NODE.  */
22834
22835 static tree
22836 cp_parser_base_clause (cp_parser* parser)
22837 {
22838   tree bases = NULL_TREE;
22839
22840   /* Look for the `:' that begins the list.  */
22841   cp_parser_require (parser, CPP_COLON, RT_COLON);
22842
22843   /* Scan the base-specifier-list.  */
22844   while (true)
22845     {
22846       cp_token *token;
22847       tree base;
22848       bool pack_expansion_p = false;
22849
22850       /* Look for the base-specifier.  */
22851       base = cp_parser_base_specifier (parser);
22852       /* Look for the (optional) ellipsis. */
22853       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22854         {
22855           /* Consume the `...'. */
22856           cp_lexer_consume_token (parser->lexer);
22857
22858           pack_expansion_p = true;
22859         }
22860
22861       /* Add BASE to the front of the list.  */
22862       if (base && base != error_mark_node)
22863         {
22864           if (pack_expansion_p)
22865             /* Make this a pack expansion type. */
22866             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22867
22868           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22869             {
22870               TREE_CHAIN (base) = bases;
22871               bases = base;
22872             }
22873         }
22874       /* Peek at the next token.  */
22875       token = cp_lexer_peek_token (parser->lexer);
22876       /* If it's not a comma, then the list is complete.  */
22877       if (token->type != CPP_COMMA)
22878         break;
22879       /* Consume the `,'.  */
22880       cp_lexer_consume_token (parser->lexer);
22881     }
22882
22883   /* PARSER->SCOPE may still be non-NULL at this point, if the last
22884      base class had a qualified name.  However, the next name that
22885      appears is certainly not qualified.  */
22886   parser->scope = NULL_TREE;
22887   parser->qualifying_scope = NULL_TREE;
22888   parser->object_scope = NULL_TREE;
22889
22890   return nreverse (bases);
22891 }
22892
22893 /* Parse a base-specifier.
22894
22895    base-specifier:
22896      :: [opt] nested-name-specifier [opt] class-name
22897      virtual access-specifier [opt] :: [opt] nested-name-specifier
22898        [opt] class-name
22899      access-specifier virtual [opt] :: [opt] nested-name-specifier
22900        [opt] class-name
22901
22902    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
22903    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22904    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
22905    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
22906
22907 static tree
22908 cp_parser_base_specifier (cp_parser* parser)
22909 {
22910   cp_token *token;
22911   bool done = false;
22912   bool virtual_p = false;
22913   bool duplicate_virtual_error_issued_p = false;
22914   bool duplicate_access_error_issued_p = false;
22915   bool class_scope_p, template_p;
22916   tree access = access_default_node;
22917   tree type;
22918
22919   /* Process the optional `virtual' and `access-specifier'.  */
22920   while (!done)
22921     {
22922       /* Peek at the next token.  */
22923       token = cp_lexer_peek_token (parser->lexer);
22924       /* Process `virtual'.  */
22925       switch (token->keyword)
22926         {
22927         case RID_VIRTUAL:
22928           /* If `virtual' appears more than once, issue an error.  */
22929           if (virtual_p && !duplicate_virtual_error_issued_p)
22930             {
22931               cp_parser_error (parser,
22932                                "%<virtual%> specified more than once in base-specified");
22933               duplicate_virtual_error_issued_p = true;
22934             }
22935
22936           virtual_p = true;
22937
22938           /* Consume the `virtual' token.  */
22939           cp_lexer_consume_token (parser->lexer);
22940
22941           break;
22942
22943         case RID_PUBLIC:
22944         case RID_PROTECTED:
22945         case RID_PRIVATE:
22946           /* If more than one access specifier appears, issue an
22947              error.  */
22948           if (access != access_default_node
22949               && !duplicate_access_error_issued_p)
22950             {
22951               cp_parser_error (parser,
22952                                "more than one access specifier in base-specified");
22953               duplicate_access_error_issued_p = true;
22954             }
22955
22956           access = ridpointers[(int) token->keyword];
22957
22958           /* Consume the access-specifier.  */
22959           cp_lexer_consume_token (parser->lexer);
22960
22961           break;
22962
22963         default:
22964           done = true;
22965           break;
22966         }
22967     }
22968   /* It is not uncommon to see programs mechanically, erroneously, use
22969      the 'typename' keyword to denote (dependent) qualified types
22970      as base classes.  */
22971   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22972     {
22973       token = cp_lexer_peek_token (parser->lexer);
22974       if (!processing_template_decl)
22975         error_at (token->location,
22976                   "keyword %<typename%> not allowed outside of templates");
22977       else
22978         error_at (token->location,
22979                   "keyword %<typename%> not allowed in this context "
22980                   "(the base class is implicitly a type)");
22981       cp_lexer_consume_token (parser->lexer);
22982     }
22983
22984   /* Look for the optional `::' operator.  */
22985   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22986   /* Look for the nested-name-specifier.  The simplest way to
22987      implement:
22988
22989        [temp.res]
22990
22991        The keyword `typename' is not permitted in a base-specifier or
22992        mem-initializer; in these contexts a qualified name that
22993        depends on a template-parameter is implicitly assumed to be a
22994        type name.
22995
22996      is to pretend that we have seen the `typename' keyword at this
22997      point.  */
22998   cp_parser_nested_name_specifier_opt (parser,
22999                                        /*typename_keyword_p=*/true,
23000                                        /*check_dependency_p=*/true,
23001                                        typename_type,
23002                                        /*is_declaration=*/true);
23003   /* If the base class is given by a qualified name, assume that names
23004      we see are type names or templates, as appropriate.  */
23005   class_scope_p = (parser->scope && TYPE_P (parser->scope));
23006   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23007
23008   if (!parser->scope
23009       && cp_lexer_next_token_is_decltype (parser->lexer))
23010     /* DR 950 allows decltype as a base-specifier.  */
23011     type = cp_parser_decltype (parser);
23012   else
23013     {
23014       /* Otherwise, look for the class-name.  */
23015       type = cp_parser_class_name (parser,
23016                                    class_scope_p,
23017                                    template_p,
23018                                    typename_type,
23019                                    /*check_dependency_p=*/true,
23020                                    /*class_head_p=*/false,
23021                                    /*is_declaration=*/true);
23022       type = TREE_TYPE (type);
23023     }
23024
23025   if (type == error_mark_node)
23026     return error_mark_node;
23027
23028   return finish_base_specifier (type, access, virtual_p);
23029 }
23030
23031 /* Exception handling [gram.exception] */
23032
23033 /* Parse an (optional) noexcept-specification.
23034
23035    noexcept-specification:
23036      noexcept ( constant-expression ) [opt]
23037
23038    If no noexcept-specification is present, returns NULL_TREE.
23039    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23040    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23041    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
23042    Otherwise, returns a noexcept specification unless RETURN_COND is true,
23043    in which case a boolean condition is returned instead.  */
23044
23045 static tree
23046 cp_parser_noexcept_specification_opt (cp_parser* parser,
23047                                       bool require_constexpr,
23048                                       bool* consumed_expr,
23049                                       bool return_cond)
23050 {
23051   cp_token *token;
23052   const char *saved_message;
23053
23054   /* Peek at the next token.  */
23055   token = cp_lexer_peek_token (parser->lexer);
23056
23057   /* Is it a noexcept-specification?  */
23058   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23059     {
23060       tree expr;
23061       cp_lexer_consume_token (parser->lexer);
23062
23063       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23064         {
23065           cp_lexer_consume_token (parser->lexer);
23066
23067           if (require_constexpr)
23068             {
23069               /* Types may not be defined in an exception-specification.  */
23070               saved_message = parser->type_definition_forbidden_message;
23071               parser->type_definition_forbidden_message
23072               = G_("types may not be defined in an exception-specification");
23073
23074               expr = cp_parser_constant_expression (parser);
23075
23076               /* Restore the saved message.  */
23077               parser->type_definition_forbidden_message = saved_message;
23078             }
23079           else
23080             {
23081               expr = cp_parser_expression (parser);
23082               *consumed_expr = true;
23083             }
23084
23085           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23086         }
23087       else
23088         {
23089           expr = boolean_true_node;
23090           if (!require_constexpr)
23091             *consumed_expr = false;
23092         }
23093
23094       /* We cannot build a noexcept-spec right away because this will check
23095          that expr is a constexpr.  */
23096       if (!return_cond)
23097         return build_noexcept_spec (expr, tf_warning_or_error);
23098       else
23099         return expr;
23100     }
23101   else
23102     return NULL_TREE;
23103 }
23104
23105 /* Parse an (optional) exception-specification.
23106
23107    exception-specification:
23108      throw ( type-id-list [opt] )
23109
23110    Returns a TREE_LIST representing the exception-specification.  The
23111    TREE_VALUE of each node is a type.  */
23112
23113 static tree
23114 cp_parser_exception_specification_opt (cp_parser* parser)
23115 {
23116   cp_token *token;
23117   tree type_id_list;
23118   const char *saved_message;
23119
23120   /* Peek at the next token.  */
23121   token = cp_lexer_peek_token (parser->lexer);
23122
23123   /* Is it a noexcept-specification?  */
23124   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23125                                                       false);
23126   if (type_id_list != NULL_TREE)
23127     return type_id_list;
23128
23129   /* If it's not `throw', then there's no exception-specification.  */
23130   if (!cp_parser_is_keyword (token, RID_THROW))
23131     return NULL_TREE;
23132
23133 #if 0
23134   /* Enable this once a lot of code has transitioned to noexcept?  */
23135   if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23136     warning (OPT_Wdeprecated, "dynamic exception specifications are "
23137              "deprecated in C++0x; use %<noexcept%> instead");
23138 #endif
23139
23140   /* Consume the `throw'.  */
23141   cp_lexer_consume_token (parser->lexer);
23142
23143   /* Look for the `('.  */
23144   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23145
23146   /* Peek at the next token.  */
23147   token = cp_lexer_peek_token (parser->lexer);
23148   /* If it's not a `)', then there is a type-id-list.  */
23149   if (token->type != CPP_CLOSE_PAREN)
23150     {
23151       /* Types may not be defined in an exception-specification.  */
23152       saved_message = parser->type_definition_forbidden_message;
23153       parser->type_definition_forbidden_message
23154         = G_("types may not be defined in an exception-specification");
23155       /* Parse the type-id-list.  */
23156       type_id_list = cp_parser_type_id_list (parser);
23157       /* Restore the saved message.  */
23158       parser->type_definition_forbidden_message = saved_message;
23159     }
23160   else
23161     type_id_list = empty_except_spec;
23162
23163   /* Look for the `)'.  */
23164   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23165
23166   return type_id_list;
23167 }
23168
23169 /* Parse an (optional) type-id-list.
23170
23171    type-id-list:
23172      type-id ... [opt]
23173      type-id-list , type-id ... [opt]
23174
23175    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
23176    in the order that the types were presented.  */
23177
23178 static tree
23179 cp_parser_type_id_list (cp_parser* parser)
23180 {
23181   tree types = NULL_TREE;
23182
23183   while (true)
23184     {
23185       cp_token *token;
23186       tree type;
23187
23188       token = cp_lexer_peek_token (parser->lexer);
23189
23190       /* Get the next type-id.  */
23191       type = cp_parser_type_id (parser);
23192       /* Check for invalid 'auto'.  */
23193       if (flag_concepts && type_uses_auto (type))
23194         {
23195           error_at (token->location,
23196                     "invalid use of %<auto%> in exception-specification");
23197           type = error_mark_node;
23198         }
23199       /* Parse the optional ellipsis. */
23200       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23201         {
23202           /* Consume the `...'. */
23203           cp_lexer_consume_token (parser->lexer);
23204
23205           /* Turn the type into a pack expansion expression. */
23206           type = make_pack_expansion (type);
23207         }
23208       /* Add it to the list.  */
23209       types = add_exception_specifier (types, type, /*complain=*/1);
23210       /* Peek at the next token.  */
23211       token = cp_lexer_peek_token (parser->lexer);
23212       /* If it is not a `,', we are done.  */
23213       if (token->type != CPP_COMMA)
23214         break;
23215       /* Consume the `,'.  */
23216       cp_lexer_consume_token (parser->lexer);
23217     }
23218
23219   return nreverse (types);
23220 }
23221
23222 /* Parse a try-block.
23223
23224    try-block:
23225      try compound-statement handler-seq  */
23226
23227 static tree
23228 cp_parser_try_block (cp_parser* parser)
23229 {
23230   tree try_block;
23231
23232   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23233   if (parser->in_function_body
23234       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23235     error ("%<try%> in %<constexpr%> function");
23236
23237   try_block = begin_try_block ();
23238   cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23239   finish_try_block (try_block);
23240   cp_parser_handler_seq (parser);
23241   finish_handler_sequence (try_block);
23242
23243   return try_block;
23244 }
23245
23246 /* Parse a function-try-block.
23247
23248    function-try-block:
23249      try ctor-initializer [opt] function-body handler-seq  */
23250
23251 static bool
23252 cp_parser_function_try_block (cp_parser* parser)
23253 {
23254   tree compound_stmt;
23255   tree try_block;
23256   bool ctor_initializer_p;
23257
23258   /* Look for the `try' keyword.  */
23259   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23260     return false;
23261   /* Let the rest of the front end know where we are.  */
23262   try_block = begin_function_try_block (&compound_stmt);
23263   /* Parse the function-body.  */
23264   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23265     (parser, /*in_function_try_block=*/true);
23266   /* We're done with the `try' part.  */
23267   finish_function_try_block (try_block);
23268   /* Parse the handlers.  */
23269   cp_parser_handler_seq (parser);
23270   /* We're done with the handlers.  */
23271   finish_function_handler_sequence (try_block, compound_stmt);
23272
23273   return ctor_initializer_p;
23274 }
23275
23276 /* Parse a handler-seq.
23277
23278    handler-seq:
23279      handler handler-seq [opt]  */
23280
23281 static void
23282 cp_parser_handler_seq (cp_parser* parser)
23283 {
23284   while (true)
23285     {
23286       cp_token *token;
23287
23288       /* Parse the handler.  */
23289       cp_parser_handler (parser);
23290       /* Peek at the next token.  */
23291       token = cp_lexer_peek_token (parser->lexer);
23292       /* If it's not `catch' then there are no more handlers.  */
23293       if (!cp_parser_is_keyword (token, RID_CATCH))
23294         break;
23295     }
23296 }
23297
23298 /* Parse a handler.
23299
23300    handler:
23301      catch ( exception-declaration ) compound-statement  */
23302
23303 static void
23304 cp_parser_handler (cp_parser* parser)
23305 {
23306   tree handler;
23307   tree declaration;
23308
23309   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23310   handler = begin_handler ();
23311   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23312   declaration = cp_parser_exception_declaration (parser);
23313   finish_handler_parms (declaration, handler);
23314   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23315   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23316   finish_handler (handler);
23317 }
23318
23319 /* Parse an exception-declaration.
23320
23321    exception-declaration:
23322      type-specifier-seq declarator
23323      type-specifier-seq abstract-declarator
23324      type-specifier-seq
23325      ...
23326
23327    Returns a VAR_DECL for the declaration, or NULL_TREE if the
23328    ellipsis variant is used.  */
23329
23330 static tree
23331 cp_parser_exception_declaration (cp_parser* parser)
23332 {
23333   cp_decl_specifier_seq type_specifiers;
23334   cp_declarator *declarator;
23335   const char *saved_message;
23336
23337   /* If it's an ellipsis, it's easy to handle.  */
23338   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23339     {
23340       /* Consume the `...' token.  */
23341       cp_lexer_consume_token (parser->lexer);
23342       return NULL_TREE;
23343     }
23344
23345   /* Types may not be defined in exception-declarations.  */
23346   saved_message = parser->type_definition_forbidden_message;
23347   parser->type_definition_forbidden_message
23348     = G_("types may not be defined in exception-declarations");
23349
23350   /* Parse the type-specifier-seq.  */
23351   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23352                                 /*is_trailing_return=*/false,
23353                                 &type_specifiers);
23354   /* If it's a `)', then there is no declarator.  */
23355   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23356     declarator = NULL;
23357   else
23358     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23359                                        /*ctor_dtor_or_conv_p=*/NULL,
23360                                        /*parenthesized_p=*/NULL,
23361                                        /*member_p=*/false,
23362                                        /*friend_p=*/false);
23363
23364   /* Restore the saved message.  */
23365   parser->type_definition_forbidden_message = saved_message;
23366
23367   if (!type_specifiers.any_specifiers_p)
23368     return error_mark_node;
23369
23370   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23371 }
23372
23373 /* Parse a throw-expression.
23374
23375    throw-expression:
23376      throw assignment-expression [opt]
23377
23378    Returns a THROW_EXPR representing the throw-expression.  */
23379
23380 static tree
23381 cp_parser_throw_expression (cp_parser* parser)
23382 {
23383   tree expression;
23384   cp_token* token;
23385
23386   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23387   token = cp_lexer_peek_token (parser->lexer);
23388   /* Figure out whether or not there is an assignment-expression
23389      following the "throw" keyword.  */
23390   if (token->type == CPP_COMMA
23391       || token->type == CPP_SEMICOLON
23392       || token->type == CPP_CLOSE_PAREN
23393       || token->type == CPP_CLOSE_SQUARE
23394       || token->type == CPP_CLOSE_BRACE
23395       || token->type == CPP_COLON)
23396     expression = NULL_TREE;
23397   else
23398     expression = cp_parser_assignment_expression (parser);
23399
23400   return build_throw (expression);
23401 }
23402
23403 /* GNU Extensions */
23404
23405 /* Parse an (optional) asm-specification.
23406
23407    asm-specification:
23408      asm ( string-literal )
23409
23410    If the asm-specification is present, returns a STRING_CST
23411    corresponding to the string-literal.  Otherwise, returns
23412    NULL_TREE.  */
23413
23414 static tree
23415 cp_parser_asm_specification_opt (cp_parser* parser)
23416 {
23417   cp_token *token;
23418   tree asm_specification;
23419
23420   /* Peek at the next token.  */
23421   token = cp_lexer_peek_token (parser->lexer);
23422   /* If the next token isn't the `asm' keyword, then there's no
23423      asm-specification.  */
23424   if (!cp_parser_is_keyword (token, RID_ASM))
23425     return NULL_TREE;
23426
23427   /* Consume the `asm' token.  */
23428   cp_lexer_consume_token (parser->lexer);
23429   /* Look for the `('.  */
23430   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23431
23432   /* Look for the string-literal.  */
23433   asm_specification = cp_parser_string_literal (parser, false, false);
23434
23435   /* Look for the `)'.  */
23436   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23437
23438   return asm_specification;
23439 }
23440
23441 /* Parse an asm-operand-list.
23442
23443    asm-operand-list:
23444      asm-operand
23445      asm-operand-list , asm-operand
23446
23447    asm-operand:
23448      string-literal ( expression )
23449      [ string-literal ] string-literal ( expression )
23450
23451    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
23452    each node is the expression.  The TREE_PURPOSE is itself a
23453    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23454    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23455    is a STRING_CST for the string literal before the parenthesis. Returns
23456    ERROR_MARK_NODE if any of the operands are invalid.  */
23457
23458 static tree
23459 cp_parser_asm_operand_list (cp_parser* parser)
23460 {
23461   tree asm_operands = NULL_TREE;
23462   bool invalid_operands = false;
23463
23464   while (true)
23465     {
23466       tree string_literal;
23467       tree expression;
23468       tree name;
23469
23470       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23471         {
23472           /* Consume the `[' token.  */
23473           cp_lexer_consume_token (parser->lexer);
23474           /* Read the operand name.  */
23475           name = cp_parser_identifier (parser);
23476           if (name != error_mark_node)
23477             name = build_string (IDENTIFIER_LENGTH (name),
23478                                  IDENTIFIER_POINTER (name));
23479           /* Look for the closing `]'.  */
23480           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23481         }
23482       else
23483         name = NULL_TREE;
23484       /* Look for the string-literal.  */
23485       string_literal = cp_parser_string_literal (parser, false, false);
23486
23487       /* Look for the `('.  */
23488       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23489       /* Parse the expression.  */
23490       expression = cp_parser_expression (parser);
23491       /* Look for the `)'.  */
23492       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23493
23494       if (name == error_mark_node 
23495           || string_literal == error_mark_node 
23496           || expression == error_mark_node)
23497         invalid_operands = true;
23498
23499       /* Add this operand to the list.  */
23500       asm_operands = tree_cons (build_tree_list (name, string_literal),
23501                                 expression,
23502                                 asm_operands);
23503       /* If the next token is not a `,', there are no more
23504          operands.  */
23505       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23506         break;
23507       /* Consume the `,'.  */
23508       cp_lexer_consume_token (parser->lexer);
23509     }
23510
23511   return invalid_operands ? error_mark_node : nreverse (asm_operands);
23512 }
23513
23514 /* Parse an asm-clobber-list.
23515
23516    asm-clobber-list:
23517      string-literal
23518      asm-clobber-list , string-literal
23519
23520    Returns a TREE_LIST, indicating the clobbers in the order that they
23521    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
23522
23523 static tree
23524 cp_parser_asm_clobber_list (cp_parser* parser)
23525 {
23526   tree clobbers = NULL_TREE;
23527
23528   while (true)
23529     {
23530       tree string_literal;
23531
23532       /* Look for the string literal.  */
23533       string_literal = cp_parser_string_literal (parser, false, false);
23534       /* Add it to the list.  */
23535       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23536       /* If the next token is not a `,', then the list is
23537          complete.  */
23538       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23539         break;
23540       /* Consume the `,' token.  */
23541       cp_lexer_consume_token (parser->lexer);
23542     }
23543
23544   return clobbers;
23545 }
23546
23547 /* Parse an asm-label-list.
23548
23549    asm-label-list:
23550      identifier
23551      asm-label-list , identifier
23552
23553    Returns a TREE_LIST, indicating the labels in the order that they
23554    appeared.  The TREE_VALUE of each node is a label.  */
23555
23556 static tree
23557 cp_parser_asm_label_list (cp_parser* parser)
23558 {
23559   tree labels = NULL_TREE;
23560
23561   while (true)
23562     {
23563       tree identifier, label, name;
23564
23565       /* Look for the identifier.  */
23566       identifier = cp_parser_identifier (parser);
23567       if (!error_operand_p (identifier))
23568         {
23569           label = lookup_label (identifier);
23570           if (TREE_CODE (label) == LABEL_DECL)
23571             {
23572               TREE_USED (label) = 1;
23573               check_goto (label);
23574               name = build_string (IDENTIFIER_LENGTH (identifier),
23575                                    IDENTIFIER_POINTER (identifier));
23576               labels = tree_cons (name, label, labels);
23577             }
23578         }
23579       /* If the next token is not a `,', then the list is
23580          complete.  */
23581       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23582         break;
23583       /* Consume the `,' token.  */
23584       cp_lexer_consume_token (parser->lexer);
23585     }
23586
23587   return nreverse (labels);
23588 }
23589
23590 /* Return TRUE iff the next tokens in the stream are possibly the
23591    beginning of a GNU extension attribute. */
23592
23593 static bool
23594 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23595 {
23596   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23597 }
23598
23599 /* Return TRUE iff the next tokens in the stream are possibly the
23600    beginning of a standard C++-11 attribute specifier.  */
23601
23602 static bool
23603 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23604 {
23605   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23606 }
23607
23608 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23609    beginning of a standard C++-11 attribute specifier.  */
23610
23611 static bool
23612 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23613 {
23614   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23615
23616   return (cxx_dialect >= cxx11
23617           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23618               || (token->type == CPP_OPEN_SQUARE
23619                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23620                   && token->type == CPP_OPEN_SQUARE)));
23621 }
23622
23623 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23624    beginning of a GNU extension attribute.  */
23625
23626 static bool
23627 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23628 {
23629   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23630
23631   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23632 }
23633
23634 /* Return true iff the next tokens can be the beginning of either a
23635    GNU attribute list, or a standard C++11 attribute sequence.  */
23636
23637 static bool
23638 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23639 {
23640   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23641           || cp_next_tokens_can_be_std_attribute_p (parser));
23642 }
23643
23644 /* Return true iff the next Nth tokens can be the beginning of either
23645    a GNU attribute list, or a standard C++11 attribute sequence.  */
23646
23647 static bool
23648 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23649 {
23650   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23651           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23652 }
23653
23654 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23655    of GNU attributes, or return NULL.  */
23656
23657 static tree
23658 cp_parser_attributes_opt (cp_parser *parser)
23659 {
23660   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23661       return cp_parser_gnu_attributes_opt (parser);
23662   return cp_parser_std_attribute_spec_seq (parser);
23663 }
23664
23665 #define CILK_SIMD_FN_CLAUSE_MASK                                  \
23666         ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH)   \
23667          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR)       \
23668          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM)      \
23669          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK)         \
23670          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23671
23672 /* Parses the Cilk Plus SIMD-enabled function's attribute.  Syntax:
23673    vector [(<clauses>)]  */
23674
23675 static void
23676 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23677 {  
23678   bool first_p = parser->cilk_simd_fn_info == NULL;
23679   cp_token *token = v_token;
23680   if (first_p)
23681     {
23682       parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23683       parser->cilk_simd_fn_info->error_seen = false;
23684       parser->cilk_simd_fn_info->fndecl_seen = false;
23685       parser->cilk_simd_fn_info->tokens = vNULL;
23686     }
23687   int paren_scope = 0;
23688   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23689     {
23690       cp_lexer_consume_token (parser->lexer);
23691       v_token = cp_lexer_peek_token (parser->lexer);
23692       paren_scope++;
23693     }
23694   while (paren_scope > 0)
23695     {
23696       token = cp_lexer_peek_token (parser->lexer);
23697       if (token->type == CPP_OPEN_PAREN)
23698         paren_scope++;
23699       else if (token->type == CPP_CLOSE_PAREN)
23700         paren_scope--;
23701       /* Do not push the last ')'  */
23702       if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23703         cp_lexer_consume_token (parser->lexer);
23704     }
23705
23706   token->type = CPP_PRAGMA_EOL;
23707   parser->lexer->next_token = token;
23708   cp_lexer_consume_token (parser->lexer);
23709
23710   struct cp_token_cache *cp
23711     = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23712   parser->cilk_simd_fn_info->tokens.safe_push (cp);
23713 }
23714
23715 /* Parse an (optional) series of attributes.
23716
23717    attributes:
23718      attributes attribute
23719
23720    attribute:
23721      __attribute__ (( attribute-list [opt] ))
23722
23723    The return value is as for cp_parser_gnu_attribute_list.  */
23724
23725 static tree
23726 cp_parser_gnu_attributes_opt (cp_parser* parser)
23727 {
23728   tree attributes = NULL_TREE;
23729
23730   while (true)
23731     {
23732       cp_token *token;
23733       tree attribute_list;
23734       bool ok = true;
23735
23736       /* Peek at the next token.  */
23737       token = cp_lexer_peek_token (parser->lexer);
23738       /* If it's not `__attribute__', then we're done.  */
23739       if (token->keyword != RID_ATTRIBUTE)
23740         break;
23741
23742       /* Consume the `__attribute__' keyword.  */
23743       cp_lexer_consume_token (parser->lexer);
23744       /* Look for the two `(' tokens.  */
23745       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23746       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23747
23748       /* Peek at the next token.  */
23749       token = cp_lexer_peek_token (parser->lexer);
23750       if (token->type != CPP_CLOSE_PAREN)
23751         /* Parse the attribute-list.  */
23752         attribute_list = cp_parser_gnu_attribute_list (parser);
23753       else
23754         /* If the next token is a `)', then there is no attribute
23755            list.  */
23756         attribute_list = NULL;
23757
23758       /* Look for the two `)' tokens.  */
23759       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23760         ok = false;
23761       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23762         ok = false;
23763       if (!ok)
23764         cp_parser_skip_to_end_of_statement (parser);
23765
23766       /* Add these new attributes to the list.  */
23767       attributes = chainon (attributes, attribute_list);
23768     }
23769
23770   return attributes;
23771 }
23772
23773 /* Parse a GNU attribute-list.
23774
23775    attribute-list:
23776      attribute
23777      attribute-list , attribute
23778
23779    attribute:
23780      identifier
23781      identifier ( identifier )
23782      identifier ( identifier , expression-list )
23783      identifier ( expression-list )
23784
23785    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
23786    to an attribute.  The TREE_PURPOSE of each node is the identifier
23787    indicating which attribute is in use.  The TREE_VALUE represents
23788    the arguments, if any.  */
23789
23790 static tree
23791 cp_parser_gnu_attribute_list (cp_parser* parser)
23792 {
23793   tree attribute_list = NULL_TREE;
23794   bool save_translate_strings_p = parser->translate_strings_p;
23795
23796   parser->translate_strings_p = false;
23797   while (true)
23798     {
23799       cp_token *token;
23800       tree identifier;
23801       tree attribute;
23802
23803       /* Look for the identifier.  We also allow keywords here; for
23804          example `__attribute__ ((const))' is legal.  */
23805       token = cp_lexer_peek_token (parser->lexer);
23806       if (token->type == CPP_NAME
23807           || token->type == CPP_KEYWORD)
23808         {
23809           tree arguments = NULL_TREE;
23810
23811           /* Consume the token, but save it since we need it for the
23812              SIMD enabled function parsing.  */
23813           cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23814
23815           /* Save away the identifier that indicates which attribute
23816              this is.  */
23817           identifier = (token->type == CPP_KEYWORD) 
23818             /* For keywords, use the canonical spelling, not the
23819                parsed identifier.  */
23820             ? ridpointers[(int) token->keyword]
23821             : id_token->u.value;
23822           
23823           attribute = build_tree_list (identifier, NULL_TREE);
23824
23825           /* Peek at the next token.  */
23826           token = cp_lexer_peek_token (parser->lexer);
23827           /* If it's an `(', then parse the attribute arguments.  */
23828           if (token->type == CPP_OPEN_PAREN)
23829             {
23830               vec<tree, va_gc> *vec;
23831               int attr_flag = (attribute_takes_identifier_p (identifier)
23832                                ? id_attr : normal_attr);
23833               if (is_cilkplus_vector_p (identifier))
23834                 {
23835                   cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23836                   continue;
23837                 }
23838               else
23839                 vec = cp_parser_parenthesized_expression_list 
23840                   (parser, attr_flag, /*cast_p=*/false, 
23841                    /*allow_expansion_p=*/false, 
23842                    /*non_constant_p=*/NULL);
23843               if (vec == NULL)
23844                 arguments = error_mark_node;
23845               else
23846                 {
23847                   arguments = build_tree_list_vec (vec);
23848                   release_tree_vector (vec);
23849                 }
23850               /* Save the arguments away.  */
23851               TREE_VALUE (attribute) = arguments;
23852             }
23853           else if (is_cilkplus_vector_p (identifier))
23854             {
23855               cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23856               continue;
23857             }
23858
23859           if (arguments != error_mark_node)
23860             {
23861               /* Add this attribute to the list.  */
23862               TREE_CHAIN (attribute) = attribute_list;
23863               attribute_list = attribute;
23864             }
23865
23866           token = cp_lexer_peek_token (parser->lexer);
23867         }
23868       /* Now, look for more attributes.  If the next token isn't a
23869          `,', we're done.  */
23870       if (token->type != CPP_COMMA)
23871         break;
23872
23873       /* Consume the comma and keep going.  */
23874       cp_lexer_consume_token (parser->lexer);
23875     }
23876   parser->translate_strings_p = save_translate_strings_p;
23877
23878   /* We built up the list in reverse order.  */
23879   return nreverse (attribute_list);
23880 }
23881
23882 /*  Parse a standard C++11 attribute.
23883
23884     The returned representation is a TREE_LIST which TREE_PURPOSE is
23885     the scoped name of the attribute, and the TREE_VALUE is its
23886     arguments list.
23887
23888     Note that the scoped name of the attribute is itself a TREE_LIST
23889     which TREE_PURPOSE is the namespace of the attribute, and
23890     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
23891     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23892     and which TREE_PURPOSE is directly the attribute name.
23893
23894     Clients of the attribute code should use get_attribute_namespace
23895     and get_attribute_name to get the actual namespace and name of
23896     attributes, regardless of their being GNU or C++11 attributes.
23897
23898     attribute:
23899       attribute-token attribute-argument-clause [opt]
23900
23901     attribute-token:
23902       identifier
23903       attribute-scoped-token
23904
23905     attribute-scoped-token:
23906       attribute-namespace :: identifier
23907
23908     attribute-namespace:
23909       identifier
23910
23911     attribute-argument-clause:
23912       ( balanced-token-seq )
23913
23914     balanced-token-seq:
23915       balanced-token [opt]
23916       balanced-token-seq balanced-token
23917
23918     balanced-token:
23919       ( balanced-token-seq )
23920       [ balanced-token-seq ]
23921       { balanced-token-seq }.  */
23922
23923 static tree
23924 cp_parser_std_attribute (cp_parser *parser)
23925 {
23926   tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23927   cp_token *token;
23928
23929   /* First, parse name of the attribute, a.k.a attribute-token.  */
23930
23931   token = cp_lexer_peek_token (parser->lexer);
23932   if (token->type == CPP_NAME)
23933     attr_id = token->u.value;
23934   else if (token->type == CPP_KEYWORD)
23935     attr_id = ridpointers[(int) token->keyword];
23936   else if (token->flags & NAMED_OP)
23937     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23938
23939   if (attr_id == NULL_TREE)
23940     return NULL_TREE;
23941
23942   cp_lexer_consume_token (parser->lexer);
23943
23944   token = cp_lexer_peek_token (parser->lexer);
23945   if (token->type == CPP_SCOPE)
23946     {
23947       /* We are seeing a scoped attribute token.  */
23948
23949       cp_lexer_consume_token (parser->lexer);
23950       attr_ns = attr_id;
23951
23952       token = cp_lexer_consume_token (parser->lexer);
23953       if (token->type == CPP_NAME)
23954         attr_id = token->u.value;
23955       else if (token->type == CPP_KEYWORD)
23956         attr_id = ridpointers[(int) token->keyword];
23957       else
23958         {
23959           error_at (token->location,
23960                     "expected an identifier for the attribute name");
23961           return error_mark_node;
23962         }
23963       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23964                                    NULL_TREE);
23965       token = cp_lexer_peek_token (parser->lexer);
23966     }
23967   else
23968     {
23969       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23970                                    NULL_TREE);
23971       /* C++11 noreturn attribute is equivalent to GNU's.  */
23972       if (is_attribute_p ("noreturn", attr_id))
23973         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23974       /* C++14 deprecated attribute is equivalent to GNU's.  */
23975       else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23976         {
23977           if (cxx_dialect == cxx11)
23978             pedwarn (token->location, OPT_Wpedantic,
23979                      "%<deprecated%> is a C++14 feature;"
23980                      " use %<gnu::deprecated%>");
23981           TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23982         }
23983       /* Transactional Memory TS optimize_for_synchronized attribute is
23984          equivalent to GNU transaction_callable.  */
23985       else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23986         TREE_PURPOSE (attribute)
23987           = get_identifier ("transaction_callable");
23988       /* Transactional Memory attributes are GNU attributes.  */
23989       else if (tm_attr_to_mask (attr_id))
23990         TREE_PURPOSE (attribute) = attr_id;
23991     }
23992
23993   /* Now parse the optional argument clause of the attribute.  */
23994
23995   if (token->type != CPP_OPEN_PAREN)
23996     return attribute;
23997
23998   {
23999     vec<tree, va_gc> *vec;
24000     int attr_flag = normal_attr;
24001
24002     if (attr_ns == get_identifier ("gnu")
24003         && attribute_takes_identifier_p (attr_id))
24004       /* A GNU attribute that takes an identifier in parameter.  */
24005       attr_flag = id_attr;
24006
24007     vec = cp_parser_parenthesized_expression_list
24008       (parser, attr_flag, /*cast_p=*/false,
24009        /*allow_expansion_p=*/true,
24010        /*non_constant_p=*/NULL);
24011     if (vec == NULL)
24012       arguments = error_mark_node;
24013     else
24014       {
24015         arguments = build_tree_list_vec (vec);
24016         release_tree_vector (vec);
24017       }
24018
24019     if (arguments == error_mark_node)
24020       attribute = error_mark_node;
24021     else
24022       TREE_VALUE (attribute) = arguments;
24023   }
24024
24025   return attribute;
24026 }
24027
24028 /* Check that the attribute ATTRIBUTE appears at most once in the
24029    attribute-list ATTRIBUTES.  This is enforced for noreturn (7.6.3)
24030    and deprecated (7.6.5).  Note that carries_dependency (7.6.4)
24031    isn't implemented yet in GCC.  */
24032
24033 static void
24034 cp_parser_check_std_attribute (tree attributes, tree attribute)
24035 {
24036   if (attributes)
24037     {
24038       tree name = get_attribute_name (attribute);
24039       if (is_attribute_p ("noreturn", name)
24040           && lookup_attribute ("noreturn", attributes))
24041         error ("attribute noreturn can appear at most once "
24042                "in an attribute-list");
24043       else if (is_attribute_p ("deprecated", name)
24044                && lookup_attribute ("deprecated", attributes))
24045         error ("attribute deprecated can appear at most once "
24046                "in an attribute-list");
24047     }
24048 }
24049
24050 /* Parse a list of standard C++-11 attributes.
24051
24052    attribute-list:
24053      attribute [opt]
24054      attribute-list , attribute[opt]
24055      attribute ...
24056      attribute-list , attribute ...
24057 */
24058
24059 static tree
24060 cp_parser_std_attribute_list (cp_parser *parser)
24061 {
24062   tree attributes = NULL_TREE, attribute = NULL_TREE;
24063   cp_token *token = NULL;
24064
24065   while (true)
24066     {
24067       attribute = cp_parser_std_attribute (parser);
24068       if (attribute == error_mark_node)
24069         break;
24070       if (attribute != NULL_TREE)
24071         {
24072           cp_parser_check_std_attribute (attributes, attribute);
24073           TREE_CHAIN (attribute) = attributes;
24074           attributes = attribute;
24075         }
24076       token = cp_lexer_peek_token (parser->lexer);
24077       if (token->type == CPP_ELLIPSIS)
24078         {
24079           cp_lexer_consume_token (parser->lexer);
24080           if (attribute == NULL_TREE)
24081             error_at (token->location,
24082                       "expected attribute before %<...%>");
24083           else
24084             TREE_VALUE (attribute)
24085               = make_pack_expansion (TREE_VALUE (attribute));
24086           token = cp_lexer_peek_token (parser->lexer);
24087         }
24088       if (token->type != CPP_COMMA)
24089         break;
24090       cp_lexer_consume_token (parser->lexer);
24091     }
24092   attributes = nreverse (attributes);
24093   return attributes;
24094 }
24095
24096 /* Parse a standard C++-11 attribute specifier.
24097
24098    attribute-specifier:
24099      [ [ attribute-list ] ]
24100      alignment-specifier
24101
24102    alignment-specifier:
24103      alignas ( type-id ... [opt] )
24104      alignas ( alignment-expression ... [opt] ).  */
24105
24106 static tree
24107 cp_parser_std_attribute_spec (cp_parser *parser)
24108 {
24109   tree attributes = NULL_TREE;
24110   cp_token *token = cp_lexer_peek_token (parser->lexer);
24111
24112   if (token->type == CPP_OPEN_SQUARE
24113       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24114     {
24115       cp_lexer_consume_token (parser->lexer);
24116       cp_lexer_consume_token (parser->lexer);
24117
24118       attributes = cp_parser_std_attribute_list (parser);
24119
24120       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24121           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24122         cp_parser_skip_to_end_of_statement (parser);
24123       else
24124         /* Warn about parsing c++11 attribute in non-c++1 mode, only
24125            when we are sure that we have actually parsed them.  */
24126         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24127     }
24128   else
24129     {
24130       tree alignas_expr;
24131
24132       /* Look for an alignment-specifier.  */
24133
24134       token = cp_lexer_peek_token (parser->lexer);
24135
24136       if (token->type != CPP_KEYWORD
24137           || token->keyword != RID_ALIGNAS)
24138         return NULL_TREE;
24139
24140       cp_lexer_consume_token (parser->lexer);
24141       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24142
24143       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24144         {
24145           cp_parser_error (parser, "expected %<(%>");
24146           return error_mark_node;
24147         }
24148
24149       cp_parser_parse_tentatively (parser);
24150       alignas_expr = cp_parser_type_id (parser);
24151
24152       if (!cp_parser_parse_definitely (parser))
24153         {
24154           alignas_expr = cp_parser_assignment_expression (parser);
24155           if (alignas_expr == error_mark_node)
24156             cp_parser_skip_to_end_of_statement (parser);
24157           if (alignas_expr == NULL_TREE
24158               || alignas_expr == error_mark_node)
24159             return alignas_expr;
24160         }
24161
24162       alignas_expr = cxx_alignas_expr (alignas_expr);
24163       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24164
24165       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24166         {
24167           cp_lexer_consume_token (parser->lexer);
24168           alignas_expr = make_pack_expansion (alignas_expr);
24169         }
24170
24171       if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24172         {
24173           cp_parser_error (parser, "expected %<)%>");
24174           return error_mark_node;
24175         }
24176
24177       /* Build the C++-11 representation of an 'aligned'
24178          attribute.  */
24179       attributes =
24180         build_tree_list (build_tree_list (get_identifier ("gnu"),
24181                                           get_identifier ("aligned")),
24182                          alignas_expr);
24183     }
24184
24185   return attributes;
24186 }
24187
24188 /* Parse a standard C++-11 attribute-specifier-seq.
24189
24190    attribute-specifier-seq:
24191      attribute-specifier-seq [opt] attribute-specifier
24192  */
24193
24194 static tree
24195 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24196 {
24197   tree attr_specs = NULL_TREE;
24198   tree attr_last = NULL_TREE;
24199
24200   while (true)
24201     {
24202       tree attr_spec = cp_parser_std_attribute_spec (parser);
24203       if (attr_spec == NULL_TREE)
24204         break;
24205       if (attr_spec == error_mark_node)
24206         return error_mark_node;
24207
24208       if (attr_last)
24209         TREE_CHAIN (attr_last) = attr_spec;
24210       else
24211         attr_specs = attr_last = attr_spec;
24212       attr_last = tree_last (attr_last);
24213     }
24214
24215   return attr_specs;
24216 }
24217
24218 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
24219    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
24220    current value of the PEDANTIC flag, regardless of whether or not
24221    the `__extension__' keyword is present.  The caller is responsible
24222    for restoring the value of the PEDANTIC flag.  */
24223
24224 static bool
24225 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24226 {
24227   /* Save the old value of the PEDANTIC flag.  */
24228   *saved_pedantic = pedantic;
24229
24230   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24231     {
24232       /* Consume the `__extension__' token.  */
24233       cp_lexer_consume_token (parser->lexer);
24234       /* We're not being pedantic while the `__extension__' keyword is
24235          in effect.  */
24236       pedantic = 0;
24237
24238       return true;
24239     }
24240
24241   return false;
24242 }
24243
24244 /* Parse a label declaration.
24245
24246    label-declaration:
24247      __label__ label-declarator-seq ;
24248
24249    label-declarator-seq:
24250      identifier , label-declarator-seq
24251      identifier  */
24252
24253 static void
24254 cp_parser_label_declaration (cp_parser* parser)
24255 {
24256   /* Look for the `__label__' keyword.  */
24257   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24258
24259   while (true)
24260     {
24261       tree identifier;
24262
24263       /* Look for an identifier.  */
24264       identifier = cp_parser_identifier (parser);
24265       /* If we failed, stop.  */
24266       if (identifier == error_mark_node)
24267         break;
24268       /* Declare it as a label.  */
24269       finish_label_decl (identifier);
24270       /* If the next token is a `;', stop.  */
24271       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24272         break;
24273       /* Look for the `,' separating the label declarations.  */
24274       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24275     }
24276
24277   /* Look for the final `;'.  */
24278   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24279 }
24280
24281 // -------------------------------------------------------------------------- //
24282 // Requires Clause
24283
24284 // Parse a requires clause.
24285 //
24286 //    requires-clause:
24287 //      'requires' logical-or-expression
24288 //
24289 // The required logical-or-expression must be a constant expression. Note
24290 // that we don't check that the expression is constepxr here. We defer until
24291 // we analyze constraints and then, we only check atomic constraints.
24292 static tree
24293 cp_parser_requires_clause (cp_parser *parser)
24294 {
24295   // Parse the requires clause so that it is not automatically folded.
24296   ++processing_template_decl;
24297   tree expr = cp_parser_binary_expression (parser, false, false,
24298                                            PREC_NOT_OPERATOR, NULL);
24299   if (check_for_bare_parameter_packs (expr))
24300     expr = error_mark_node;
24301   --processing_template_decl;
24302   return expr;
24303 }
24304
24305 // Optionally parse a requires clause:
24306 static tree
24307 cp_parser_requires_clause_opt (cp_parser *parser)
24308 {
24309   cp_token *tok = cp_lexer_peek_token (parser->lexer);
24310   if (tok->keyword != RID_REQUIRES)
24311     {
24312       if (!flag_concepts && tok->type == CPP_NAME
24313           && tok->u.value == ridpointers[RID_REQUIRES])
24314         {
24315           error_at (cp_lexer_peek_token (parser->lexer)->location,
24316                     "%<requires%> only available with -fconcepts");
24317           /* Parse and discard the requires-clause.  */
24318           cp_lexer_consume_token (parser->lexer);
24319           cp_parser_requires_clause (parser);
24320         }
24321       return NULL_TREE;
24322     }
24323   cp_lexer_consume_token (parser->lexer);
24324   return cp_parser_requires_clause (parser);
24325 }
24326
24327
24328 /*---------------------------------------------------------------------------
24329                            Requires expressions
24330 ---------------------------------------------------------------------------*/
24331
24332 /* Parse a requires expression
24333
24334    requirement-expression:
24335        'requires' requirement-parameter-list [opt] requirement-body */
24336 static tree
24337 cp_parser_requires_expression (cp_parser *parser)
24338 {
24339   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24340   location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24341
24342   /* A requires-expression shall appear only within a concept
24343      definition or a requires-clause.
24344
24345      TODO: Implement this diagnostic correctly. */
24346   if (!processing_template_decl)
24347     {
24348       error_at (loc, "a requires expression cannot appear outside a template");
24349       cp_parser_skip_to_end_of_statement (parser);
24350       return error_mark_node;
24351     }
24352
24353   tree parms, reqs;
24354   {
24355     /* Local parameters are delared as variables within the scope
24356        of the expression.  They are not visible past the end of
24357        the expression.  Expressions within the requires-expression
24358        are unevaluated.  */
24359     struct scope_sentinel
24360     {
24361       scope_sentinel ()
24362       {
24363         ++cp_unevaluated_operand;
24364         begin_scope (sk_block, NULL_TREE);
24365       }
24366
24367       ~scope_sentinel ()
24368       {
24369         pop_bindings_and_leave_scope ();
24370         --cp_unevaluated_operand;
24371       }
24372     } s;
24373
24374     /* Parse the optional parameter list. */
24375     if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24376       {
24377         parms = cp_parser_requirement_parameter_list (parser);
24378         if (parms == error_mark_node)
24379           return error_mark_node;
24380       }
24381     else
24382       parms = NULL_TREE;
24383
24384     /* Parse the requirement body. */
24385     reqs = cp_parser_requirement_body (parser);
24386     if (reqs == error_mark_node)
24387       return error_mark_node;
24388   }
24389
24390   /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24391      the parm chain.  */
24392   grokparms (parms, &parms);
24393   return finish_requires_expr (parms, reqs);
24394 }
24395
24396 /* Parse a parameterized requirement.
24397
24398    requirement-parameter-list:
24399        '(' parameter-declaration-clause ')' */
24400 static tree
24401 cp_parser_requirement_parameter_list (cp_parser *parser)
24402 {
24403   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24404     return error_mark_node;
24405
24406   tree parms = cp_parser_parameter_declaration_clause (parser);
24407
24408   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24409     return error_mark_node;
24410
24411   return parms;
24412 }
24413
24414 /* Parse the body of a requirement.
24415
24416    requirement-body:
24417        '{' requirement-list '}' */
24418 static tree
24419 cp_parser_requirement_body (cp_parser *parser)
24420 {
24421   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24422     return error_mark_node;
24423
24424   tree reqs = cp_parser_requirement_list (parser);
24425
24426   if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24427     return error_mark_node;
24428
24429   return reqs;
24430 }
24431
24432 /* Parse a list of requirements.
24433
24434    requirement-list:
24435        requirement
24436        requirement-list ';' requirement[opt] */
24437 static tree
24438 cp_parser_requirement_list (cp_parser *parser)
24439 {
24440   tree result = NULL_TREE;
24441   while (true)
24442     {
24443       tree req = cp_parser_requirement (parser);
24444       if (req == error_mark_node)
24445         return error_mark_node;
24446
24447       result = tree_cons (NULL_TREE, req, result);
24448
24449       /* If we see a semi-colon, consume it. */
24450       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24451         cp_lexer_consume_token (parser->lexer);
24452
24453       /* Stop processing at the end of the list. */
24454       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24455         break;
24456     }
24457
24458   /* Reverse the order of requirements so they are analyzed in
24459      declaration order. */
24460   return nreverse (result);
24461 }
24462
24463 /* Parse a syntactic requirement or type requirement.
24464
24465      requirement:
24466        simple-requirement
24467        compound-requirement
24468        type-requirement
24469        nested-requirement */
24470 static tree
24471 cp_parser_requirement (cp_parser *parser)
24472 {
24473   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24474     return cp_parser_compound_requirement (parser);
24475   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24476     return cp_parser_type_requirement (parser);
24477   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24478     return cp_parser_nested_requirement (parser);
24479   else
24480     return cp_parser_simple_requirement (parser);
24481 }
24482
24483 /* Parse a simple requirement.
24484
24485      simple-requirement:
24486        expression ';' */
24487 static tree
24488 cp_parser_simple_requirement (cp_parser *parser)
24489 {
24490   tree expr = cp_parser_expression (parser, NULL, false, false);
24491   if (!expr || expr == error_mark_node)
24492     return error_mark_node;
24493
24494   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24495     return error_mark_node;
24496
24497   return finish_simple_requirement (expr);
24498 }
24499
24500 /* Parse a type requirement
24501
24502      type-requirement
24503          nested-name-specifier [opt] required-type-name ';'
24504
24505      required-type-name:
24506          type-name
24507          'template' [opt] simple-template-id  */
24508 static tree
24509 cp_parser_type_requirement (cp_parser *parser)
24510 {
24511   cp_lexer_consume_token (parser->lexer);
24512
24513   // Save the scope before parsing name specifiers.
24514   tree saved_scope = parser->scope;
24515   tree saved_object_scope = parser->object_scope;
24516   tree saved_qualifying_scope = parser->qualifying_scope;
24517   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24518   cp_parser_nested_name_specifier_opt (parser,
24519                                        /*typename_keyword_p=*/true,
24520                                        /*check_dependency_p=*/false,
24521                                        /*type_p=*/true,
24522                                        /*is_declaration=*/false);
24523
24524   tree type;
24525   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24526     {
24527       cp_lexer_consume_token (parser->lexer);
24528       type = cp_parser_template_id (parser,
24529                                     /*template_keyword_p=*/true,
24530                                     /*check_dependency=*/false,
24531                                     /*tag_type=*/none_type,
24532                                     /*is_declaration=*/false);
24533       type = make_typename_type (parser->scope, type, typename_type,
24534                                  /*complain=*/tf_error);
24535     }
24536   else
24537    type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24538
24539   if (TREE_CODE (type) == TYPE_DECL)
24540     type = TREE_TYPE (type);
24541
24542   parser->scope = saved_scope;
24543   parser->object_scope = saved_object_scope;
24544   parser->qualifying_scope = saved_qualifying_scope;
24545
24546   if (type == error_mark_node)
24547     cp_parser_skip_to_end_of_statement (parser);
24548
24549   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24550     return error_mark_node;
24551   if (type == error_mark_node)
24552     return error_mark_node;
24553
24554   return finish_type_requirement (type);
24555 }
24556
24557 /* Parse a compound requirement
24558
24559      compound-requirement:
24560          '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24561 static tree
24562 cp_parser_compound_requirement (cp_parser *parser)
24563 {
24564   /* Parse an expression enclosed in '{ }'s. */
24565   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24566     return error_mark_node;
24567
24568   tree expr = cp_parser_expression (parser, NULL, false, false);
24569   if (!expr || expr == error_mark_node)
24570     return error_mark_node;
24571
24572   if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24573     return error_mark_node;
24574
24575   /* Parse the optional noexcept. */
24576   bool noexcept_p = false;
24577   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24578     {
24579       cp_lexer_consume_token (parser->lexer);
24580       noexcept_p = true;
24581     }
24582
24583   /* Parse the optional trailing return type. */
24584   tree type = NULL_TREE;
24585   if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24586     {
24587       cp_lexer_consume_token (parser->lexer);
24588       bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24589       parser->in_result_type_constraint_p = true;
24590       type = cp_parser_trailing_type_id (parser);
24591       parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24592       if (type == error_mark_node)
24593         return error_mark_node;
24594     }
24595
24596   return finish_compound_requirement (expr, type, noexcept_p);
24597 }
24598
24599 /* Parse a nested requirement. This is the same as a requires clause.
24600
24601    nested-requirement:
24602      requires-clause */
24603 static tree
24604 cp_parser_nested_requirement (cp_parser *parser)
24605 {
24606   cp_lexer_consume_token (parser->lexer);
24607   tree req = cp_parser_requires_clause (parser);
24608   if (req == error_mark_node)
24609     return error_mark_node;
24610   return finish_nested_requirement (req);
24611 }
24612
24613 /* Support Functions */
24614
24615 /* Return the appropriate prefer_type argument for lookup_name_real based on
24616    tag_type.  */
24617
24618 static inline int
24619 prefer_type_arg (tag_types tag_type)
24620 {
24621   switch (tag_type)
24622     {
24623     case none_type:  return 0;  // No preference.
24624     case scope_type: return 1;  // Type or namespace.
24625     default:         return 2;  // Type only.
24626     }
24627 }
24628
24629 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24630    NAME should have one of the representations used for an
24631    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24632    is returned.  If PARSER->SCOPE is a dependent type, then a
24633    SCOPE_REF is returned.
24634
24635    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24636    returned; the name was already resolved when the TEMPLATE_ID_EXPR
24637    was formed.  Abstractly, such entities should not be passed to this
24638    function, because they do not need to be looked up, but it is
24639    simpler to check for this special case here, rather than at the
24640    call-sites.
24641
24642    In cases not explicitly covered above, this function returns a
24643    DECL, OVERLOAD, or baselink representing the result of the lookup.
24644    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24645    is returned.
24646
24647    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24648    (e.g., "struct") that was used.  In that case bindings that do not
24649    refer to types are ignored.
24650
24651    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24652    ignored.
24653
24654    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24655    are ignored.
24656
24657    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24658    types.
24659
24660    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24661    TREE_LIST of candidates if name-lookup results in an ambiguity, and
24662    NULL_TREE otherwise.  */
24663
24664 static cp_expr
24665 cp_parser_lookup_name (cp_parser *parser, tree name,
24666                        enum tag_types tag_type,
24667                        bool is_template,
24668                        bool is_namespace,
24669                        bool check_dependency,
24670                        tree *ambiguous_decls,
24671                        location_t name_location)
24672 {
24673   tree decl;
24674   tree object_type = parser->context->object_type;
24675
24676   /* Assume that the lookup will be unambiguous.  */
24677   if (ambiguous_decls)
24678     *ambiguous_decls = NULL_TREE;
24679
24680   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24681      no longer valid.  Note that if we are parsing tentatively, and
24682      the parse fails, OBJECT_TYPE will be automatically restored.  */
24683   parser->context->object_type = NULL_TREE;
24684
24685   if (name == error_mark_node)
24686     return error_mark_node;
24687
24688   /* A template-id has already been resolved; there is no lookup to
24689      do.  */
24690   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24691     return name;
24692   if (BASELINK_P (name))
24693     {
24694       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24695                   == TEMPLATE_ID_EXPR);
24696       return name;
24697     }
24698
24699   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
24700      it should already have been checked to make sure that the name
24701      used matches the type being destroyed.  */
24702   if (TREE_CODE (name) == BIT_NOT_EXPR)
24703     {
24704       tree type;
24705
24706       /* Figure out to which type this destructor applies.  */
24707       if (parser->scope)
24708         type = parser->scope;
24709       else if (object_type)
24710         type = object_type;
24711       else
24712         type = current_class_type;
24713       /* If that's not a class type, there is no destructor.  */
24714       if (!type || !CLASS_TYPE_P (type))
24715         return error_mark_node;
24716       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24717         lazily_declare_fn (sfk_destructor, type);
24718       if (!CLASSTYPE_DESTRUCTORS (type))
24719           return error_mark_node;
24720       /* If it was a class type, return the destructor.  */
24721       return CLASSTYPE_DESTRUCTORS (type);
24722     }
24723
24724   /* By this point, the NAME should be an ordinary identifier.  If
24725      the id-expression was a qualified name, the qualifying scope is
24726      stored in PARSER->SCOPE at this point.  */
24727   gcc_assert (identifier_p (name));
24728
24729   /* Perform the lookup.  */
24730   if (parser->scope)
24731     {
24732       bool dependent_p;
24733
24734       if (parser->scope == error_mark_node)
24735         return error_mark_node;
24736
24737       /* If the SCOPE is dependent, the lookup must be deferred until
24738          the template is instantiated -- unless we are explicitly
24739          looking up names in uninstantiated templates.  Even then, we
24740          cannot look up the name if the scope is not a class type; it
24741          might, for example, be a template type parameter.  */
24742       dependent_p = (TYPE_P (parser->scope)
24743                      && dependent_scope_p (parser->scope));
24744       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24745           && dependent_p)
24746         /* Defer lookup.  */
24747         decl = error_mark_node;
24748       else
24749         {
24750           tree pushed_scope = NULL_TREE;
24751
24752           /* If PARSER->SCOPE is a dependent type, then it must be a
24753              class type, and we must not be checking dependencies;
24754              otherwise, we would have processed this lookup above.  So
24755              that PARSER->SCOPE is not considered a dependent base by
24756              lookup_member, we must enter the scope here.  */
24757           if (dependent_p)
24758             pushed_scope = push_scope (parser->scope);
24759
24760           /* If the PARSER->SCOPE is a template specialization, it
24761              may be instantiated during name lookup.  In that case,
24762              errors may be issued.  Even if we rollback the current
24763              tentative parse, those errors are valid.  */
24764           decl = lookup_qualified_name (parser->scope, name,
24765                                         prefer_type_arg (tag_type),
24766                                         /*complain=*/true);
24767
24768           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24769              lookup result and the nested-name-specifier nominates a class C:
24770                * if the name specified after the nested-name-specifier, when
24771                looked up in C, is the injected-class-name of C (Clause 9), or
24772                * if the name specified after the nested-name-specifier is the
24773                same as the identifier or the simple-template-id's template-
24774                name in the last component of the nested-name-specifier,
24775              the name is instead considered to name the constructor of
24776              class C. [ Note: for example, the constructor is not an
24777              acceptable lookup result in an elaborated-type-specifier so
24778              the constructor would not be used in place of the
24779              injected-class-name. --end note ] Such a constructor name
24780              shall be used only in the declarator-id of a declaration that
24781              names a constructor or in a using-declaration.  */
24782           if (tag_type == none_type
24783               && DECL_SELF_REFERENCE_P (decl)
24784               && same_type_p (DECL_CONTEXT (decl), parser->scope))
24785             decl = lookup_qualified_name (parser->scope, ctor_identifier,
24786                                           prefer_type_arg (tag_type),
24787                                           /*complain=*/true);
24788
24789           /* If we have a single function from a using decl, pull it out.  */
24790           if (TREE_CODE (decl) == OVERLOAD
24791               && !really_overloaded_fn (decl))
24792             decl = OVL_FUNCTION (decl);
24793
24794           if (pushed_scope)
24795             pop_scope (pushed_scope);
24796         }
24797
24798       /* If the scope is a dependent type and either we deferred lookup or
24799          we did lookup but didn't find the name, rememeber the name.  */
24800       if (decl == error_mark_node && TYPE_P (parser->scope)
24801           && dependent_type_p (parser->scope))
24802         {
24803           if (tag_type)
24804             {
24805               tree type;
24806
24807               /* The resolution to Core Issue 180 says that `struct
24808                  A::B' should be considered a type-name, even if `A'
24809                  is dependent.  */
24810               type = make_typename_type (parser->scope, name, tag_type,
24811                                          /*complain=*/tf_error);
24812               if (type != error_mark_node)
24813                 decl = TYPE_NAME (type);
24814             }
24815           else if (is_template
24816                    && (cp_parser_next_token_ends_template_argument_p (parser)
24817                        || cp_lexer_next_token_is (parser->lexer,
24818                                                   CPP_CLOSE_PAREN)))
24819             decl = make_unbound_class_template (parser->scope,
24820                                                 name, NULL_TREE,
24821                                                 /*complain=*/tf_error);
24822           else
24823             decl = build_qualified_name (/*type=*/NULL_TREE,
24824                                          parser->scope, name,
24825                                          is_template);
24826         }
24827       parser->qualifying_scope = parser->scope;
24828       parser->object_scope = NULL_TREE;
24829     }
24830   else if (object_type)
24831     {
24832       /* Look up the name in the scope of the OBJECT_TYPE, unless the
24833          OBJECT_TYPE is not a class.  */
24834       if (CLASS_TYPE_P (object_type))
24835         /* If the OBJECT_TYPE is a template specialization, it may
24836            be instantiated during name lookup.  In that case, errors
24837            may be issued.  Even if we rollback the current tentative
24838            parse, those errors are valid.  */
24839         decl = lookup_member (object_type,
24840                               name,
24841                               /*protect=*/0,
24842                               prefer_type_arg (tag_type),
24843                               tf_warning_or_error);
24844       else
24845         decl = NULL_TREE;
24846
24847       if (!decl)
24848         {
24849           /* Look it up in the enclosing context.  */
24850           decl = lookup_name_real (name, prefer_type_arg (tag_type),
24851                                    /*nonclass=*/0,
24852                                    /*block_p=*/true, is_namespace, 0);
24853           /* DR 141 says when looking for a template-name after -> or ., only
24854              consider class templates.  We need to fix our handling of
24855              dependent expressions to implement that properly, but for now
24856              let's ignore namespace-scope function templates.  */
24857           if (decl && is_template && !DECL_TYPE_TEMPLATE_P (decl))
24858             {
24859               tree d = decl;
24860               if (is_overloaded_fn (d))
24861                 d = get_first_fn (d);
24862               if (DECL_P (d) && !DECL_CLASS_SCOPE_P (d))
24863                 decl = NULL_TREE;
24864             }
24865         }
24866       if (object_type == unknown_type_node)
24867         /* The object is type-dependent, so we can't look anything up; we used
24868            this to get the DR 141 behavior.  */
24869         object_type = NULL_TREE;
24870       parser->object_scope = object_type;
24871       parser->qualifying_scope = NULL_TREE;
24872     }
24873   else
24874     {
24875       decl = lookup_name_real (name, prefer_type_arg (tag_type),
24876                                /*nonclass=*/0,
24877                                /*block_p=*/true, is_namespace, 0);
24878       parser->qualifying_scope = NULL_TREE;
24879       parser->object_scope = NULL_TREE;
24880     }
24881
24882   /* If the lookup failed, let our caller know.  */
24883   if (!decl || decl == error_mark_node)
24884     return error_mark_node;
24885
24886   /* Pull out the template from an injected-class-name (or multiple).  */
24887   if (is_template)
24888     decl = maybe_get_template_decl_from_type_decl (decl);
24889
24890   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
24891   if (TREE_CODE (decl) == TREE_LIST)
24892     {
24893       if (ambiguous_decls)
24894         *ambiguous_decls = decl;
24895       /* The error message we have to print is too complicated for
24896          cp_parser_error, so we incorporate its actions directly.  */
24897       if (!cp_parser_simulate_error (parser))
24898         {
24899           error_at (name_location, "reference to %qD is ambiguous",
24900                     name);
24901           print_candidates (decl);
24902         }
24903       return error_mark_node;
24904     }
24905
24906   gcc_assert (DECL_P (decl)
24907               || TREE_CODE (decl) == OVERLOAD
24908               || TREE_CODE (decl) == SCOPE_REF
24909               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24910               || BASELINK_P (decl));
24911
24912   /* If we have resolved the name of a member declaration, check to
24913      see if the declaration is accessible.  When the name resolves to
24914      set of overloaded functions, accessibility is checked when
24915      overload resolution is done.
24916
24917      During an explicit instantiation, access is not checked at all,
24918      as per [temp.explicit].  */
24919   if (DECL_P (decl))
24920     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24921
24922   maybe_record_typedef_use (decl);
24923
24924   return cp_expr (decl, name_location);
24925 }
24926
24927 /* Like cp_parser_lookup_name, but for use in the typical case where
24928    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24929    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
24930
24931 static tree
24932 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24933 {
24934   return cp_parser_lookup_name (parser, name,
24935                                 none_type,
24936                                 /*is_template=*/false,
24937                                 /*is_namespace=*/false,
24938                                 /*check_dependency=*/true,
24939                                 /*ambiguous_decls=*/NULL,
24940                                 location);
24941 }
24942
24943 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24944    the current context, return the TYPE_DECL.  If TAG_NAME_P is
24945    true, the DECL indicates the class being defined in a class-head,
24946    or declared in an elaborated-type-specifier.
24947
24948    Otherwise, return DECL.  */
24949
24950 static tree
24951 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24952 {
24953   /* If the TEMPLATE_DECL is being declared as part of a class-head,
24954      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24955
24956        struct A {
24957          template <typename T> struct B;
24958        };
24959
24960        template <typename T> struct A::B {};
24961
24962      Similarly, in an elaborated-type-specifier:
24963
24964        namespace N { struct X{}; }
24965
24966        struct A {
24967          template <typename T> friend struct N::X;
24968        };
24969
24970      However, if the DECL refers to a class type, and we are in
24971      the scope of the class, then the name lookup automatically
24972      finds the TYPE_DECL created by build_self_reference rather
24973      than a TEMPLATE_DECL.  For example, in:
24974
24975        template <class T> struct S {
24976          S s;
24977        };
24978
24979      there is no need to handle such case.  */
24980
24981   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24982     return DECL_TEMPLATE_RESULT (decl);
24983
24984   return decl;
24985 }
24986
24987 /* If too many, or too few, template-parameter lists apply to the
24988    declarator, issue an error message.  Returns TRUE if all went well,
24989    and FALSE otherwise.  */
24990
24991 static bool
24992 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24993                                                 cp_declarator *declarator,
24994                                                 location_t declarator_location)
24995 {
24996   switch (declarator->kind)
24997     {
24998     case cdk_id:
24999       {
25000         unsigned num_templates = 0;
25001         tree scope = declarator->u.id.qualifying_scope;
25002
25003         if (scope)
25004           num_templates = num_template_headers_for_class (scope);
25005         else if (TREE_CODE (declarator->u.id.unqualified_name)
25006                  == TEMPLATE_ID_EXPR)
25007           /* If the DECLARATOR has the form `X<y>' then it uses one
25008              additional level of template parameters.  */
25009           ++num_templates;
25010
25011         return cp_parser_check_template_parameters 
25012           (parser, num_templates, declarator_location, declarator);
25013       }
25014
25015     case cdk_function:
25016     case cdk_array:
25017     case cdk_pointer:
25018     case cdk_reference:
25019     case cdk_ptrmem:
25020       return (cp_parser_check_declarator_template_parameters
25021               (parser, declarator->declarator, declarator_location));
25022
25023     case cdk_error:
25024       return true;
25025
25026     default:
25027       gcc_unreachable ();
25028     }
25029   return false;
25030 }
25031
25032 /* NUM_TEMPLATES were used in the current declaration.  If that is
25033    invalid, return FALSE and issue an error messages.  Otherwise,
25034    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
25035    declarator and we can print more accurate diagnostics.  */
25036
25037 static bool
25038 cp_parser_check_template_parameters (cp_parser* parser,
25039                                      unsigned num_templates,
25040                                      location_t location,
25041                                      cp_declarator *declarator)
25042 {
25043   /* If there are the same number of template classes and parameter
25044      lists, that's OK.  */
25045   if (parser->num_template_parameter_lists == num_templates)
25046     return true;
25047   /* If there are more, but only one more, then we are referring to a
25048      member template.  That's OK too.  */
25049   if (parser->num_template_parameter_lists == num_templates + 1)
25050     return true;
25051   /* If there are more template classes than parameter lists, we have
25052      something like:
25053
25054        template <class T> void S<T>::R<T>::f ();  */
25055   if (parser->num_template_parameter_lists < num_templates)
25056     {
25057       if (declarator && !current_function_decl)
25058         error_at (location, "specializing member %<%T::%E%> "
25059                   "requires %<template<>%> syntax", 
25060                   declarator->u.id.qualifying_scope,
25061                   declarator->u.id.unqualified_name);
25062       else if (declarator)
25063         error_at (location, "invalid declaration of %<%T::%E%>",
25064                   declarator->u.id.qualifying_scope,
25065                   declarator->u.id.unqualified_name);
25066       else 
25067         error_at (location, "too few template-parameter-lists");
25068       return false;
25069     }
25070   /* Otherwise, there are too many template parameter lists.  We have
25071      something like:
25072
25073      template <class T> template <class U> void S::f();  */
25074   error_at (location, "too many template-parameter-lists");
25075   return false;
25076 }
25077
25078 /* Parse an optional `::' token indicating that the following name is
25079    from the global namespace.  If so, PARSER->SCOPE is set to the
25080    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25081    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25082    Returns the new value of PARSER->SCOPE, if the `::' token is
25083    present, and NULL_TREE otherwise.  */
25084
25085 static tree
25086 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25087 {
25088   cp_token *token;
25089
25090   /* Peek at the next token.  */
25091   token = cp_lexer_peek_token (parser->lexer);
25092   /* If we're looking at a `::' token then we're starting from the
25093      global namespace, not our current location.  */
25094   if (token->type == CPP_SCOPE)
25095     {
25096       /* Consume the `::' token.  */
25097       cp_lexer_consume_token (parser->lexer);
25098       /* Set the SCOPE so that we know where to start the lookup.  */
25099       parser->scope = global_namespace;
25100       parser->qualifying_scope = global_namespace;
25101       parser->object_scope = NULL_TREE;
25102
25103       return parser->scope;
25104     }
25105   else if (!current_scope_valid_p)
25106     {
25107       parser->scope = NULL_TREE;
25108       parser->qualifying_scope = NULL_TREE;
25109       parser->object_scope = NULL_TREE;
25110     }
25111
25112   return NULL_TREE;
25113 }
25114
25115 /* Returns TRUE if the upcoming token sequence is the start of a
25116    constructor declarator.  If FRIEND_P is true, the declarator is
25117    preceded by the `friend' specifier.  */
25118
25119 static bool
25120 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25121 {
25122   bool constructor_p;
25123   bool outside_class_specifier_p;
25124   tree nested_name_specifier;
25125   cp_token *next_token;
25126
25127   /* The common case is that this is not a constructor declarator, so
25128      try to avoid doing lots of work if at all possible.  It's not
25129      valid declare a constructor at function scope.  */
25130   if (parser->in_function_body)
25131     return false;
25132   /* And only certain tokens can begin a constructor declarator.  */
25133   next_token = cp_lexer_peek_token (parser->lexer);
25134   if (next_token->type != CPP_NAME
25135       && next_token->type != CPP_SCOPE
25136       && next_token->type != CPP_NESTED_NAME_SPECIFIER
25137       && next_token->type != CPP_TEMPLATE_ID)
25138     return false;
25139
25140   /* Parse tentatively; we are going to roll back all of the tokens
25141      consumed here.  */
25142   cp_parser_parse_tentatively (parser);
25143   /* Assume that we are looking at a constructor declarator.  */
25144   constructor_p = true;
25145
25146   /* Look for the optional `::' operator.  */
25147   cp_parser_global_scope_opt (parser,
25148                               /*current_scope_valid_p=*/false);
25149   /* Look for the nested-name-specifier.  */
25150   nested_name_specifier
25151     = (cp_parser_nested_name_specifier_opt (parser,
25152                                             /*typename_keyword_p=*/false,
25153                                             /*check_dependency_p=*/false,
25154                                             /*type_p=*/false,
25155                                             /*is_declaration=*/false));
25156
25157   outside_class_specifier_p = (!at_class_scope_p ()
25158                                || !TYPE_BEING_DEFINED (current_class_type)
25159                                || friend_p);
25160
25161   /* Outside of a class-specifier, there must be a
25162      nested-name-specifier.  */
25163   if (!nested_name_specifier && outside_class_specifier_p)
25164     constructor_p = false;
25165   else if (nested_name_specifier == error_mark_node)
25166     constructor_p = false;
25167
25168   /* If we have a class scope, this is easy; DR 147 says that S::S always
25169      names the constructor, and no other qualified name could.  */
25170   if (constructor_p && nested_name_specifier
25171       && CLASS_TYPE_P (nested_name_specifier))
25172     {
25173       tree id = cp_parser_unqualified_id (parser,
25174                                           /*template_keyword_p=*/false,
25175                                           /*check_dependency_p=*/false,
25176                                           /*declarator_p=*/true,
25177                                           /*optional_p=*/false);
25178       if (is_overloaded_fn (id))
25179         id = DECL_NAME (get_first_fn (id));
25180       if (!constructor_name_p (id, nested_name_specifier))
25181         constructor_p = false;
25182     }
25183   /* If we still think that this might be a constructor-declarator,
25184      look for a class-name.  */
25185   else if (constructor_p)
25186     {
25187       /* If we have:
25188
25189            template <typename T> struct S {
25190              S();
25191            };
25192
25193          we must recognize that the nested `S' names a class.  */
25194       tree type_decl;
25195       type_decl = cp_parser_class_name (parser,
25196                                         /*typename_keyword_p=*/false,
25197                                         /*template_keyword_p=*/false,
25198                                         none_type,
25199                                         /*check_dependency_p=*/false,
25200                                         /*class_head_p=*/false,
25201                                         /*is_declaration=*/false);
25202       /* If there was no class-name, then this is not a constructor.
25203          Otherwise, if we are in a class-specifier and we aren't
25204          handling a friend declaration, check that its type matches
25205          current_class_type (c++/38313).  Note: error_mark_node
25206          is left alone for error recovery purposes.  */
25207       constructor_p = (!cp_parser_error_occurred (parser)
25208                        && (outside_class_specifier_p
25209                            || type_decl == error_mark_node
25210                            || same_type_p (current_class_type,
25211                                            TREE_TYPE (type_decl))));
25212
25213       /* If we're still considering a constructor, we have to see a `(',
25214          to begin the parameter-declaration-clause, followed by either a
25215          `)', an `...', or a decl-specifier.  We need to check for a
25216          type-specifier to avoid being fooled into thinking that:
25217
25218            S (f) (int);
25219
25220          is a constructor.  (It is actually a function named `f' that
25221          takes one parameter (of type `int') and returns a value of type
25222          `S'.  */
25223       if (constructor_p
25224           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25225         constructor_p = false;
25226
25227       if (constructor_p
25228           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25229           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25230           /* A parameter declaration begins with a decl-specifier,
25231              which is either the "attribute" keyword, a storage class
25232              specifier, or (usually) a type-specifier.  */
25233           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25234         {
25235           tree type;
25236           tree pushed_scope = NULL_TREE;
25237           unsigned saved_num_template_parameter_lists;
25238
25239           /* Names appearing in the type-specifier should be looked up
25240              in the scope of the class.  */
25241           if (current_class_type)
25242             type = NULL_TREE;
25243           else
25244             {
25245               type = TREE_TYPE (type_decl);
25246               if (TREE_CODE (type) == TYPENAME_TYPE)
25247                 {
25248                   type = resolve_typename_type (type,
25249                                                 /*only_current_p=*/false);
25250                   if (TREE_CODE (type) == TYPENAME_TYPE)
25251                     {
25252                       cp_parser_abort_tentative_parse (parser);
25253                       return false;
25254                     }
25255                 }
25256               pushed_scope = push_scope (type);
25257             }
25258
25259           /* Inside the constructor parameter list, surrounding
25260              template-parameter-lists do not apply.  */
25261           saved_num_template_parameter_lists
25262             = parser->num_template_parameter_lists;
25263           parser->num_template_parameter_lists = 0;
25264
25265           /* Look for the type-specifier.  */
25266           cp_parser_type_specifier (parser,
25267                                     CP_PARSER_FLAGS_NONE,
25268                                     /*decl_specs=*/NULL,
25269                                     /*is_declarator=*/true,
25270                                     /*declares_class_or_enum=*/NULL,
25271                                     /*is_cv_qualifier=*/NULL);
25272
25273           parser->num_template_parameter_lists
25274             = saved_num_template_parameter_lists;
25275
25276           /* Leave the scope of the class.  */
25277           if (pushed_scope)
25278             pop_scope (pushed_scope);
25279
25280           constructor_p = !cp_parser_error_occurred (parser);
25281         }
25282     }
25283
25284   /* We did not really want to consume any tokens.  */
25285   cp_parser_abort_tentative_parse (parser);
25286
25287   return constructor_p;
25288 }
25289
25290 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25291    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
25292    they must be performed once we are in the scope of the function.
25293
25294    Returns the function defined.  */
25295
25296 static tree
25297 cp_parser_function_definition_from_specifiers_and_declarator
25298   (cp_parser* parser,
25299    cp_decl_specifier_seq *decl_specifiers,
25300    tree attributes,
25301    const cp_declarator *declarator)
25302 {
25303   tree fn;
25304   bool success_p;
25305
25306   /* Begin the function-definition.  */
25307   success_p = start_function (decl_specifiers, declarator, attributes);
25308
25309   /* The things we're about to see are not directly qualified by any
25310      template headers we've seen thus far.  */
25311   reset_specialization ();
25312
25313   /* If there were names looked up in the decl-specifier-seq that we
25314      did not check, check them now.  We must wait until we are in the
25315      scope of the function to perform the checks, since the function
25316      might be a friend.  */
25317   perform_deferred_access_checks (tf_warning_or_error);
25318
25319   if (success_p)
25320     {
25321       cp_finalize_omp_declare_simd (parser, current_function_decl);
25322       parser->omp_declare_simd = NULL;
25323       cp_finalize_oacc_routine (parser, current_function_decl, true);
25324       parser->oacc_routine = NULL;
25325     }
25326
25327   if (!success_p)
25328     {
25329       /* Skip the entire function.  */
25330       cp_parser_skip_to_end_of_block_or_statement (parser);
25331       fn = error_mark_node;
25332     }
25333   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25334     {
25335       /* Seen already, skip it.  An error message has already been output.  */
25336       cp_parser_skip_to_end_of_block_or_statement (parser);
25337       fn = current_function_decl;
25338       current_function_decl = NULL_TREE;
25339       /* If this is a function from a class, pop the nested class.  */
25340       if (current_class_name)
25341         pop_nested_class ();
25342     }
25343   else
25344     {
25345       timevar_id_t tv;
25346       if (DECL_DECLARED_INLINE_P (current_function_decl))
25347         tv = TV_PARSE_INLINE;
25348       else
25349         tv = TV_PARSE_FUNC;
25350       timevar_push (tv);
25351       fn = cp_parser_function_definition_after_declarator (parser,
25352                                                          /*inline_p=*/false);
25353       timevar_pop (tv);
25354     }
25355
25356   return fn;
25357 }
25358
25359 /* Parse the part of a function-definition that follows the
25360    declarator.  INLINE_P is TRUE iff this function is an inline
25361    function defined within a class-specifier.
25362
25363    Returns the function defined.  */
25364
25365 static tree
25366 cp_parser_function_definition_after_declarator (cp_parser* parser,
25367                                                 bool inline_p)
25368 {
25369   tree fn;
25370   bool ctor_initializer_p = false;
25371   bool saved_in_unbraced_linkage_specification_p;
25372   bool saved_in_function_body;
25373   unsigned saved_num_template_parameter_lists;
25374   cp_token *token;
25375   bool fully_implicit_function_template_p
25376     = parser->fully_implicit_function_template_p;
25377   parser->fully_implicit_function_template_p = false;
25378   tree implicit_template_parms
25379     = parser->implicit_template_parms;
25380   parser->implicit_template_parms = 0;
25381   cp_binding_level* implicit_template_scope
25382     = parser->implicit_template_scope;
25383   parser->implicit_template_scope = 0;
25384
25385   saved_in_function_body = parser->in_function_body;
25386   parser->in_function_body = true;
25387   /* If the next token is `return', then the code may be trying to
25388      make use of the "named return value" extension that G++ used to
25389      support.  */
25390   token = cp_lexer_peek_token (parser->lexer);
25391   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25392     {
25393       /* Consume the `return' keyword.  */
25394       cp_lexer_consume_token (parser->lexer);
25395       /* Look for the identifier that indicates what value is to be
25396          returned.  */
25397       cp_parser_identifier (parser);
25398       /* Issue an error message.  */
25399       error_at (token->location,
25400                 "named return values are no longer supported");
25401       /* Skip tokens until we reach the start of the function body.  */
25402       while (true)
25403         {
25404           cp_token *token = cp_lexer_peek_token (parser->lexer);
25405           if (token->type == CPP_OPEN_BRACE
25406               || token->type == CPP_EOF
25407               || token->type == CPP_PRAGMA_EOL)
25408             break;
25409           cp_lexer_consume_token (parser->lexer);
25410         }
25411     }
25412   /* The `extern' in `extern "C" void f () { ... }' does not apply to
25413      anything declared inside `f'.  */
25414   saved_in_unbraced_linkage_specification_p
25415     = parser->in_unbraced_linkage_specification_p;
25416   parser->in_unbraced_linkage_specification_p = false;
25417   /* Inside the function, surrounding template-parameter-lists do not
25418      apply.  */
25419   saved_num_template_parameter_lists
25420     = parser->num_template_parameter_lists;
25421   parser->num_template_parameter_lists = 0;
25422
25423   start_lambda_scope (current_function_decl);
25424
25425   /* If the next token is `try', `__transaction_atomic', or
25426      `__transaction_relaxed`, then we are looking at either function-try-block
25427      or function-transaction-block.  Note that all of these include the
25428      function-body.  */
25429   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25430     ctor_initializer_p = cp_parser_function_transaction (parser,
25431         RID_TRANSACTION_ATOMIC);
25432   else if (cp_lexer_next_token_is_keyword (parser->lexer,
25433       RID_TRANSACTION_RELAXED))
25434     ctor_initializer_p = cp_parser_function_transaction (parser,
25435         RID_TRANSACTION_RELAXED);
25436   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25437     ctor_initializer_p = cp_parser_function_try_block (parser);
25438   else
25439     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25440       (parser, /*in_function_try_block=*/false);
25441
25442   finish_lambda_scope ();
25443
25444   /* Finish the function.  */
25445   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25446                         (inline_p ? 2 : 0));
25447   /* Generate code for it, if necessary.  */
25448   expand_or_defer_fn (fn);
25449   /* Restore the saved values.  */
25450   parser->in_unbraced_linkage_specification_p
25451     = saved_in_unbraced_linkage_specification_p;
25452   parser->num_template_parameter_lists
25453     = saved_num_template_parameter_lists;
25454   parser->in_function_body = saved_in_function_body;
25455
25456   parser->fully_implicit_function_template_p
25457     = fully_implicit_function_template_p;
25458   parser->implicit_template_parms
25459     = implicit_template_parms;
25460   parser->implicit_template_scope
25461     = implicit_template_scope;
25462
25463   if (parser->fully_implicit_function_template_p)
25464     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25465
25466   return fn;
25467 }
25468
25469 /* Parse a template-declaration body (following argument list).  */
25470
25471 static void
25472 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25473                                                  tree parameter_list,
25474                                                  bool member_p)
25475 {
25476   tree decl = NULL_TREE;
25477   bool friend_p = false;
25478
25479   /* We just processed one more parameter list.  */
25480   ++parser->num_template_parameter_lists;
25481
25482   /* Get the deferred access checks from the parameter list.  These
25483      will be checked once we know what is being declared, as for a
25484      member template the checks must be performed in the scope of the
25485      class containing the member.  */
25486   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25487
25488   /* Tentatively parse for a new template parameter list, which can either be
25489      the template keyword or a template introduction.  */
25490   if (cp_parser_template_declaration_after_export (parser, member_p))
25491     /* OK */;
25492   else if (cxx_dialect >= cxx11
25493            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25494     decl = cp_parser_alias_declaration (parser);
25495   else
25496     {
25497       /* There are no access checks when parsing a template, as we do not
25498          know if a specialization will be a friend.  */
25499       push_deferring_access_checks (dk_no_check);
25500       cp_token *token = cp_lexer_peek_token (parser->lexer);
25501       decl = cp_parser_single_declaration (parser,
25502                                            checks,
25503                                            member_p,
25504                                            /*explicit_specialization_p=*/false,
25505                                            &friend_p);
25506       pop_deferring_access_checks ();
25507
25508       /* If this is a member template declaration, let the front
25509          end know.  */
25510       if (member_p && !friend_p && decl)
25511         {
25512           if (TREE_CODE (decl) == TYPE_DECL)
25513             cp_parser_check_access_in_redeclaration (decl, token->location);
25514
25515           decl = finish_member_template_decl (decl);
25516         }
25517       else if (friend_p && decl
25518                && DECL_DECLARES_TYPE_P (decl))
25519         make_friend_class (current_class_type, TREE_TYPE (decl),
25520                            /*complain=*/true);
25521     }
25522   /* We are done with the current parameter list.  */
25523   --parser->num_template_parameter_lists;
25524
25525   pop_deferring_access_checks ();
25526
25527   /* Finish up.  */
25528   finish_template_decl (parameter_list);
25529
25530   /* Check the template arguments for a literal operator template.  */
25531   if (decl
25532       && DECL_DECLARES_FUNCTION_P (decl)
25533       && UDLIT_OPER_P (DECL_NAME (decl)))
25534     {
25535       bool ok = true;
25536       if (parameter_list == NULL_TREE)
25537         ok = false;
25538       else
25539         {
25540           int num_parms = TREE_VEC_LENGTH (parameter_list);
25541           if (num_parms == 1)
25542             {
25543               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25544               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25545               if (TREE_TYPE (parm) != char_type_node
25546                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25547                 ok = false;
25548             }
25549           else if (num_parms == 2 && cxx_dialect >= cxx14)
25550             {
25551               tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25552               tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25553               tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25554               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25555               if (parm == error_mark_node
25556                   || TREE_TYPE (parm) != TREE_TYPE (type)
25557                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25558                 ok = false;
25559             }
25560           else
25561             ok = false;
25562         }
25563       if (!ok)
25564         {
25565           if (cxx_dialect >= cxx14)
25566             error ("literal operator template %qD has invalid parameter list."
25567                    "  Expected non-type template argument pack <char...>"
25568                    " or <typename CharT, CharT...>",
25569                    decl);
25570           else
25571             error ("literal operator template %qD has invalid parameter list."
25572                    "  Expected non-type template argument pack <char...>",
25573                    decl);
25574         }
25575     }
25576
25577   /* Register member declarations.  */
25578   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25579     finish_member_declaration (decl);
25580   /* If DECL is a function template, we must return to parse it later.
25581      (Even though there is no definition, there might be default
25582      arguments that need handling.)  */
25583   if (member_p && decl
25584       && DECL_DECLARES_FUNCTION_P (decl))
25585     vec_safe_push (unparsed_funs_with_definitions, decl);
25586 }
25587
25588 /* Parse a template introduction header for a template-declaration.  Returns
25589    false if tentative parse fails.  */
25590
25591 static bool
25592 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25593 {
25594   cp_parser_parse_tentatively (parser);
25595
25596   tree saved_scope = parser->scope;
25597   tree saved_object_scope = parser->object_scope;
25598   tree saved_qualifying_scope = parser->qualifying_scope;
25599
25600   /* Look for the optional `::' operator.  */
25601   cp_parser_global_scope_opt (parser,
25602                               /*current_scope_valid_p=*/false);
25603   /* Look for the nested-name-specifier.  */
25604   cp_parser_nested_name_specifier_opt (parser,
25605                                        /*typename_keyword_p=*/false,
25606                                        /*check_dependency_p=*/true,
25607                                        /*type_p=*/false,
25608                                        /*is_declaration=*/false);
25609
25610   cp_token *token = cp_lexer_peek_token (parser->lexer);
25611   tree concept_name = cp_parser_identifier (parser);
25612
25613   /* Look up the concept for which we will be matching
25614      template parameters.  */
25615   tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25616                                                  token->location);
25617   parser->scope = saved_scope;
25618   parser->object_scope = saved_object_scope;
25619   parser->qualifying_scope = saved_qualifying_scope;
25620
25621   if (concept_name == error_mark_node)
25622     cp_parser_simulate_error (parser);
25623
25624   /* Look for opening brace for introduction.  */
25625   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25626
25627   if (!cp_parser_parse_definitely (parser))
25628     return false;
25629
25630   push_deferring_access_checks (dk_deferred);
25631
25632   /* Build vector of placeholder parameters and grab
25633      matching identifiers.  */
25634   tree introduction_list = cp_parser_introduction_list (parser);
25635
25636   /* The introduction-list shall not be empty.  */
25637   int nargs = TREE_VEC_LENGTH (introduction_list);
25638   if (nargs == 0)
25639     {
25640       error ("empty introduction-list");
25641       return true;
25642     }
25643
25644   /* Look for closing brace for introduction.  */
25645   if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25646     return true;
25647
25648   if (tmpl_decl == error_mark_node)
25649     {
25650       cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25651                                    token->location);
25652       return true;
25653     }
25654
25655   /* Build and associate the constraint.  */
25656   tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25657   if (parms && parms != error_mark_node)
25658     {
25659       cp_parser_template_declaration_after_parameters (parser, parms,
25660                                                        member_p);
25661       return true;
25662     }
25663
25664   error_at (token->location, "no matching concept for template-introduction");
25665   return true;
25666 }
25667
25668 /* Parse a normal template-declaration following the template keyword.  */
25669
25670 static void
25671 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25672 {
25673   tree parameter_list;
25674   bool need_lang_pop;
25675   location_t location = input_location;
25676
25677   /* Look for the `<' token.  */
25678   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25679     return;
25680   if (at_class_scope_p () && current_function_decl)
25681     {
25682       /* 14.5.2.2 [temp.mem]
25683
25684          A local class shall not have member templates.  */
25685       error_at (location,
25686                 "invalid declaration of member template in local class");
25687       cp_parser_skip_to_end_of_block_or_statement (parser);
25688       return;
25689     }
25690   /* [temp]
25691
25692      A template ... shall not have C linkage.  */
25693   if (current_lang_name == lang_name_c)
25694     {
25695       error_at (location, "template with C linkage");
25696       /* Give it C++ linkage to avoid confusing other parts of the
25697          front end.  */
25698       push_lang_context (lang_name_cplusplus);
25699       need_lang_pop = true;
25700     }
25701   else
25702     need_lang_pop = false;
25703
25704   /* We cannot perform access checks on the template parameter
25705      declarations until we know what is being declared, just as we
25706      cannot check the decl-specifier list.  */
25707   push_deferring_access_checks (dk_deferred);
25708
25709   /* If the next token is `>', then we have an invalid
25710      specialization.  Rather than complain about an invalid template
25711      parameter, issue an error message here.  */
25712   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25713     {
25714       cp_parser_error (parser, "invalid explicit specialization");
25715       begin_specialization ();
25716       parameter_list = NULL_TREE;
25717     }
25718   else
25719     {
25720       /* Parse the template parameters.  */
25721       parameter_list = cp_parser_template_parameter_list (parser);
25722     }
25723
25724   /* Look for the `>'.  */
25725   cp_parser_skip_to_end_of_template_parameter_list (parser);
25726
25727   /* Manage template requirements */
25728   if (flag_concepts)
25729   {
25730     tree reqs = get_shorthand_constraints (current_template_parms);
25731     if (tree r = cp_parser_requires_clause_opt (parser))
25732       reqs = conjoin_constraints (reqs, normalize_expression (r));
25733     TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25734   }
25735
25736   cp_parser_template_declaration_after_parameters (parser, parameter_list,
25737                                                    member_p);
25738
25739   /* For the erroneous case of a template with C linkage, we pushed an
25740      implicit C++ linkage scope; exit that scope now.  */
25741   if (need_lang_pop)
25742     pop_lang_context ();
25743 }
25744
25745 /* Parse a template-declaration, assuming that the `export' (and
25746    `extern') keywords, if present, has already been scanned.  MEMBER_P
25747    is as for cp_parser_template_declaration.  */
25748
25749 static bool
25750 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25751 {
25752   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25753     {
25754       cp_lexer_consume_token (parser->lexer);
25755       cp_parser_explicit_template_declaration (parser, member_p);
25756       return true;
25757     }
25758   else if (flag_concepts)
25759     return cp_parser_template_introduction (parser, member_p);
25760
25761   return false;
25762 }
25763
25764 /* Perform the deferred access checks from a template-parameter-list.
25765    CHECKS is a TREE_LIST of access checks, as returned by
25766    get_deferred_access_checks.  */
25767
25768 static void
25769 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25770 {
25771   ++processing_template_parmlist;
25772   perform_access_checks (checks, tf_warning_or_error);
25773   --processing_template_parmlist;
25774 }
25775
25776 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25777    `function-definition' sequence that follows a template header.
25778    If MEMBER_P is true, this declaration appears in a class scope.
25779
25780    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
25781    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
25782
25783 static tree
25784 cp_parser_single_declaration (cp_parser* parser,
25785                               vec<deferred_access_check, va_gc> *checks,
25786                               bool member_p,
25787                               bool explicit_specialization_p,
25788                               bool* friend_p)
25789 {
25790   int declares_class_or_enum;
25791   tree decl = NULL_TREE;
25792   cp_decl_specifier_seq decl_specifiers;
25793   bool function_definition_p = false;
25794   cp_token *decl_spec_token_start;
25795
25796   /* This function is only used when processing a template
25797      declaration.  */
25798   gcc_assert (innermost_scope_kind () == sk_template_parms
25799               || innermost_scope_kind () == sk_template_spec);
25800
25801   /* Defer access checks until we know what is being declared.  */
25802   push_deferring_access_checks (dk_deferred);
25803
25804   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25805      alternative.  */
25806   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25807   cp_parser_decl_specifier_seq (parser,
25808                                 CP_PARSER_FLAGS_OPTIONAL,
25809                                 &decl_specifiers,
25810                                 &declares_class_or_enum);
25811   if (friend_p)
25812     *friend_p = cp_parser_friend_p (&decl_specifiers);
25813
25814   /* There are no template typedefs.  */
25815   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25816     {
25817       error_at (decl_spec_token_start->location,
25818                 "template declaration of %<typedef%>");
25819       decl = error_mark_node;
25820     }
25821
25822   /* Gather up the access checks that occurred the
25823      decl-specifier-seq.  */
25824   stop_deferring_access_checks ();
25825
25826   /* Check for the declaration of a template class.  */
25827   if (declares_class_or_enum)
25828     {
25829       if (cp_parser_declares_only_class_p (parser)
25830           || (declares_class_or_enum & 2))
25831         {
25832           // If this is a declaration, but not a definition, associate
25833           // any constraints with the type declaration. Constraints
25834           // are associated with definitions in cp_parser_class_specifier.
25835           if (declares_class_or_enum == 1)
25836             associate_classtype_constraints (decl_specifiers.type);
25837
25838           decl = shadow_tag (&decl_specifiers);
25839
25840           /* In this case:
25841
25842                struct C {
25843                  friend template <typename T> struct A<T>::B;
25844                };
25845
25846              A<T>::B will be represented by a TYPENAME_TYPE, and
25847              therefore not recognized by shadow_tag.  */
25848           if (friend_p && *friend_p
25849               && !decl
25850               && decl_specifiers.type
25851               && TYPE_P (decl_specifiers.type))
25852             decl = decl_specifiers.type;
25853
25854           if (decl && decl != error_mark_node)
25855             decl = TYPE_NAME (decl);
25856           else
25857             decl = error_mark_node;
25858
25859           /* Perform access checks for template parameters.  */
25860           cp_parser_perform_template_parameter_access_checks (checks);
25861
25862           /* Give a helpful diagnostic for
25863                template <class T> struct A { } a;
25864              if we aren't already recovering from an error.  */
25865           if (!cp_parser_declares_only_class_p (parser)
25866               && !seen_error ())
25867             {
25868               error_at (cp_lexer_peek_token (parser->lexer)->location,
25869                         "a class template declaration must not declare "
25870                         "anything else");
25871               cp_parser_skip_to_end_of_block_or_statement (parser);
25872               goto out;
25873             }
25874         }
25875     }
25876
25877   /* Complain about missing 'typename' or other invalid type names.  */
25878   if (!decl_specifiers.any_type_specifiers_p
25879       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25880     {
25881       /* cp_parser_parse_and_diagnose_invalid_type_name calls
25882          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25883          the rest of this declaration.  */
25884       decl = error_mark_node;
25885       goto out;
25886     }
25887
25888   /* If it's not a template class, try for a template function.  If
25889      the next token is a `;', then this declaration does not declare
25890      anything.  But, if there were errors in the decl-specifiers, then
25891      the error might well have come from an attempted class-specifier.
25892      In that case, there's no need to warn about a missing declarator.  */
25893   if (!decl
25894       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25895           || decl_specifiers.type != error_mark_node))
25896     {
25897       decl = cp_parser_init_declarator (parser,
25898                                         &decl_specifiers,
25899                                         checks,
25900                                         /*function_definition_allowed_p=*/true,
25901                                         member_p,
25902                                         declares_class_or_enum,
25903                                         &function_definition_p,
25904                                         NULL, NULL, NULL);
25905
25906     /* 7.1.1-1 [dcl.stc]
25907
25908        A storage-class-specifier shall not be specified in an explicit
25909        specialization...  */
25910     if (decl
25911         && explicit_specialization_p
25912         && decl_specifiers.storage_class != sc_none)
25913       {
25914         error_at (decl_spec_token_start->location,
25915                   "explicit template specialization cannot have a storage class");
25916         decl = error_mark_node;
25917       }
25918
25919     if (decl && VAR_P (decl))
25920       check_template_variable (decl);
25921     }
25922
25923   /* Look for a trailing `;' after the declaration.  */
25924   if (!function_definition_p
25925       && (decl == error_mark_node
25926           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25927     cp_parser_skip_to_end_of_block_or_statement (parser);
25928
25929  out:
25930   pop_deferring_access_checks ();
25931
25932   /* Clear any current qualification; whatever comes next is the start
25933      of something new.  */
25934   parser->scope = NULL_TREE;
25935   parser->qualifying_scope = NULL_TREE;
25936   parser->object_scope = NULL_TREE;
25937
25938   return decl;
25939 }
25940
25941 /* Parse a cast-expression that is not the operand of a unary "&".  */
25942
25943 static cp_expr
25944 cp_parser_simple_cast_expression (cp_parser *parser)
25945 {
25946   return cp_parser_cast_expression (parser, /*address_p=*/false,
25947                                     /*cast_p=*/false, /*decltype*/false, NULL);
25948 }
25949
25950 /* Parse a functional cast to TYPE.  Returns an expression
25951    representing the cast.  */
25952
25953 static cp_expr
25954 cp_parser_functional_cast (cp_parser* parser, tree type)
25955 {
25956   vec<tree, va_gc> *vec;
25957   tree expression_list;
25958   cp_expr cast;
25959   bool nonconst_p;
25960
25961   location_t start_loc = input_location;
25962
25963   if (!type)
25964     type = error_mark_node;
25965
25966   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25967     {
25968       cp_lexer_set_source_position (parser->lexer);
25969       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25970       expression_list = cp_parser_braced_list (parser, &nonconst_p);
25971       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25972       if (TREE_CODE (type) == TYPE_DECL)
25973         type = TREE_TYPE (type);
25974
25975       cast = finish_compound_literal (type, expression_list,
25976                                       tf_warning_or_error);
25977       /* Create a location of the form:
25978             type_name{i, f}
25979             ^~~~~~~~~~~~~~~
25980          with caret == start at the start of the type name,
25981          finishing at the closing brace.  */
25982       location_t finish_loc
25983         = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25984       location_t combined_loc = make_location (start_loc, start_loc,
25985                                                finish_loc);
25986       cast.set_location (combined_loc);
25987       return cast;
25988    }
25989
25990
25991   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25992                                                  /*cast_p=*/true,
25993                                                  /*allow_expansion_p=*/true,
25994                                                  /*non_constant_p=*/NULL);
25995   if (vec == NULL)
25996     expression_list = error_mark_node;
25997   else
25998     {
25999       expression_list = build_tree_list_vec (vec);
26000       release_tree_vector (vec);
26001     }
26002
26003   cast = build_functional_cast (type, expression_list,
26004                                 tf_warning_or_error);
26005   /* [expr.const]/1: In an integral constant expression "only type
26006      conversions to integral or enumeration type can be used".  */
26007   if (TREE_CODE (type) == TYPE_DECL)
26008     type = TREE_TYPE (type);
26009   if (cast != error_mark_node
26010       && !cast_valid_in_integral_constant_expression_p (type)
26011       && cp_parser_non_integral_constant_expression (parser,
26012                                                      NIC_CONSTRUCTOR))
26013     return error_mark_node;
26014
26015   /* Create a location of the form:
26016        float(i)
26017        ^~~~~~~~
26018      with caret == start at the start of the type name,
26019      finishing at the closing paren.  */
26020   location_t finish_loc
26021     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26022   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26023   cast.set_location (combined_loc);
26024   return cast;
26025 }
26026
26027 /* Save the tokens that make up the body of a member function defined
26028    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
26029    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
26030    specifiers applied to the declaration.  Returns the FUNCTION_DECL
26031    for the member function.  */
26032
26033 static tree
26034 cp_parser_save_member_function_body (cp_parser* parser,
26035                                      cp_decl_specifier_seq *decl_specifiers,
26036                                      cp_declarator *declarator,
26037                                      tree attributes)
26038 {
26039   cp_token *first;
26040   cp_token *last;
26041   tree fn;
26042   bool function_try_block = false;
26043
26044   /* Create the FUNCTION_DECL.  */
26045   fn = grokmethod (decl_specifiers, declarator, attributes);
26046   cp_finalize_omp_declare_simd (parser, fn);
26047   cp_finalize_oacc_routine (parser, fn, true);
26048   /* If something went badly wrong, bail out now.  */
26049   if (fn == error_mark_node)
26050     {
26051       /* If there's a function-body, skip it.  */
26052       if (cp_parser_token_starts_function_definition_p
26053           (cp_lexer_peek_token (parser->lexer)))
26054         cp_parser_skip_to_end_of_block_or_statement (parser);
26055       return error_mark_node;
26056     }
26057
26058   /* Remember it, if there default args to post process.  */
26059   cp_parser_save_default_args (parser, fn);
26060
26061   /* Save away the tokens that make up the body of the
26062      function.  */
26063   first = parser->lexer->next_token;
26064
26065   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26066     cp_lexer_consume_token (parser->lexer);
26067   else if (cp_lexer_next_token_is_keyword (parser->lexer,
26068                                            RID_TRANSACTION_ATOMIC))
26069     {
26070       cp_lexer_consume_token (parser->lexer);
26071       /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
26072       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26073           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26074           && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26075               || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26076           && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26077           && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26078         {
26079           cp_lexer_consume_token (parser->lexer);
26080           cp_lexer_consume_token (parser->lexer);
26081           cp_lexer_consume_token (parser->lexer);
26082           cp_lexer_consume_token (parser->lexer);
26083           cp_lexer_consume_token (parser->lexer);
26084         }
26085       else
26086         while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26087                && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26088           {
26089             cp_lexer_consume_token (parser->lexer);
26090             if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26091               break;
26092           }
26093     }
26094
26095   /* Handle function try blocks.  */
26096   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26097     {
26098       cp_lexer_consume_token (parser->lexer);
26099       function_try_block = true;
26100     }
26101   /* We can have braced-init-list mem-initializers before the fn body.  */
26102   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26103     {
26104       cp_lexer_consume_token (parser->lexer);
26105       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26106         {
26107           /* cache_group will stop after an un-nested { } pair, too.  */
26108           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26109             break;
26110
26111           /* variadic mem-inits have ... after the ')'.  */
26112           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26113             cp_lexer_consume_token (parser->lexer);
26114         }
26115     }
26116   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26117   /* Handle function try blocks.  */
26118   if (function_try_block)
26119     while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26120       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26121   last = parser->lexer->next_token;
26122
26123   /* Save away the inline definition; we will process it when the
26124      class is complete.  */
26125   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26126   DECL_PENDING_INLINE_P (fn) = 1;
26127
26128   /* We need to know that this was defined in the class, so that
26129      friend templates are handled correctly.  */
26130   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26131
26132   /* Add FN to the queue of functions to be parsed later.  */
26133   vec_safe_push (unparsed_funs_with_definitions, fn);
26134
26135   return fn;
26136 }
26137
26138 /* Save the tokens that make up the in-class initializer for a non-static
26139    data member.  Returns a DEFAULT_ARG.  */
26140
26141 static tree
26142 cp_parser_save_nsdmi (cp_parser* parser)
26143 {
26144   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26145 }
26146
26147 /* Parse a template-argument-list, as well as the trailing ">" (but
26148    not the opening "<").  See cp_parser_template_argument_list for the
26149    return value.  */
26150
26151 static tree
26152 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26153 {
26154   tree arguments;
26155   tree saved_scope;
26156   tree saved_qualifying_scope;
26157   tree saved_object_scope;
26158   bool saved_greater_than_is_operator_p;
26159   int saved_unevaluated_operand;
26160   int saved_inhibit_evaluation_warnings;
26161
26162   /* [temp.names]
26163
26164      When parsing a template-id, the first non-nested `>' is taken as
26165      the end of the template-argument-list rather than a greater-than
26166      operator.  */
26167   saved_greater_than_is_operator_p
26168     = parser->greater_than_is_operator_p;
26169   parser->greater_than_is_operator_p = false;
26170   /* Parsing the argument list may modify SCOPE, so we save it
26171      here.  */
26172   saved_scope = parser->scope;
26173   saved_qualifying_scope = parser->qualifying_scope;
26174   saved_object_scope = parser->object_scope;
26175   /* We need to evaluate the template arguments, even though this
26176      template-id may be nested within a "sizeof".  */
26177   saved_unevaluated_operand = cp_unevaluated_operand;
26178   cp_unevaluated_operand = 0;
26179   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26180   c_inhibit_evaluation_warnings = 0;
26181   /* Parse the template-argument-list itself.  */
26182   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26183       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26184     arguments = NULL_TREE;
26185   else
26186     arguments = cp_parser_template_argument_list (parser);
26187   /* Look for the `>' that ends the template-argument-list. If we find
26188      a '>>' instead, it's probably just a typo.  */
26189   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26190     {
26191       if (cxx_dialect != cxx98)
26192         {
26193           /* In C++0x, a `>>' in a template argument list or cast
26194              expression is considered to be two separate `>'
26195              tokens. So, change the current token to a `>', but don't
26196              consume it: it will be consumed later when the outer
26197              template argument list (or cast expression) is parsed.
26198              Note that this replacement of `>' for `>>' is necessary
26199              even if we are parsing tentatively: in the tentative
26200              case, after calling
26201              cp_parser_enclosed_template_argument_list we will always
26202              throw away all of the template arguments and the first
26203              closing `>', either because the template argument list
26204              was erroneous or because we are replacing those tokens
26205              with a CPP_TEMPLATE_ID token.  The second `>' (which will
26206              not have been thrown away) is needed either to close an
26207              outer template argument list or to complete a new-style
26208              cast.  */
26209           cp_token *token = cp_lexer_peek_token (parser->lexer);
26210           token->type = CPP_GREATER;
26211         }
26212       else if (!saved_greater_than_is_operator_p)
26213         {
26214           /* If we're in a nested template argument list, the '>>' has
26215             to be a typo for '> >'. We emit the error message, but we
26216             continue parsing and we push a '>' as next token, so that
26217             the argument list will be parsed correctly.  Note that the
26218             global source location is still on the token before the
26219             '>>', so we need to say explicitly where we want it.  */
26220           cp_token *token = cp_lexer_peek_token (parser->lexer);
26221           error_at (token->location, "%<>>%> should be %<> >%> "
26222                     "within a nested template argument list");
26223
26224           token->type = CPP_GREATER;
26225         }
26226       else
26227         {
26228           /* If this is not a nested template argument list, the '>>'
26229             is a typo for '>'. Emit an error message and continue.
26230             Same deal about the token location, but here we can get it
26231             right by consuming the '>>' before issuing the diagnostic.  */
26232           cp_token *token = cp_lexer_consume_token (parser->lexer);
26233           error_at (token->location,
26234                     "spurious %<>>%>, use %<>%> to terminate "
26235                     "a template argument list");
26236         }
26237     }
26238   else
26239     cp_parser_skip_to_end_of_template_parameter_list (parser);
26240   /* The `>' token might be a greater-than operator again now.  */
26241   parser->greater_than_is_operator_p
26242     = saved_greater_than_is_operator_p;
26243   /* Restore the SAVED_SCOPE.  */
26244   parser->scope = saved_scope;
26245   parser->qualifying_scope = saved_qualifying_scope;
26246   parser->object_scope = saved_object_scope;
26247   cp_unevaluated_operand = saved_unevaluated_operand;
26248   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26249
26250   return arguments;
26251 }
26252
26253 /* MEMBER_FUNCTION is a member function, or a friend.  If default
26254    arguments, or the body of the function have not yet been parsed,
26255    parse them now.  */
26256
26257 static void
26258 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26259 {
26260   timevar_push (TV_PARSE_INMETH);
26261   /* If this member is a template, get the underlying
26262      FUNCTION_DECL.  */
26263   if (DECL_FUNCTION_TEMPLATE_P (member_function))
26264     member_function = DECL_TEMPLATE_RESULT (member_function);
26265
26266   /* There should not be any class definitions in progress at this
26267      point; the bodies of members are only parsed outside of all class
26268      definitions.  */
26269   gcc_assert (parser->num_classes_being_defined == 0);
26270   /* While we're parsing the member functions we might encounter more
26271      classes.  We want to handle them right away, but we don't want
26272      them getting mixed up with functions that are currently in the
26273      queue.  */
26274   push_unparsed_function_queues (parser);
26275
26276   /* Make sure that any template parameters are in scope.  */
26277   maybe_begin_member_template_processing (member_function);
26278
26279   /* If the body of the function has not yet been parsed, parse it
26280      now.  */
26281   if (DECL_PENDING_INLINE_P (member_function))
26282     {
26283       tree function_scope;
26284       cp_token_cache *tokens;
26285
26286       /* The function is no longer pending; we are processing it.  */
26287       tokens = DECL_PENDING_INLINE_INFO (member_function);
26288       DECL_PENDING_INLINE_INFO (member_function) = NULL;
26289       DECL_PENDING_INLINE_P (member_function) = 0;
26290
26291       /* If this is a local class, enter the scope of the containing
26292          function.  */
26293       function_scope = current_function_decl;
26294       if (function_scope)
26295         push_function_context ();
26296
26297       /* Push the body of the function onto the lexer stack.  */
26298       cp_parser_push_lexer_for_tokens (parser, tokens);
26299
26300       /* Let the front end know that we going to be defining this
26301          function.  */
26302       start_preparsed_function (member_function, NULL_TREE,
26303                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
26304
26305       /* Don't do access checking if it is a templated function.  */
26306       if (processing_template_decl)
26307         push_deferring_access_checks (dk_no_check);
26308
26309       /* #pragma omp declare reduction needs special parsing.  */
26310       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26311         {
26312           parser->lexer->in_pragma = true;
26313           cp_parser_omp_declare_reduction_exprs (member_function, parser);
26314           finish_function (/*inline*/2);
26315           cp_check_omp_declare_reduction (member_function);
26316         }
26317       else
26318         /* Now, parse the body of the function.  */
26319         cp_parser_function_definition_after_declarator (parser,
26320                                                         /*inline_p=*/true);
26321
26322       if (processing_template_decl)
26323         pop_deferring_access_checks ();
26324
26325       /* Leave the scope of the containing function.  */
26326       if (function_scope)
26327         pop_function_context ();
26328       cp_parser_pop_lexer (parser);
26329     }
26330
26331   /* Remove any template parameters from the symbol table.  */
26332   maybe_end_member_template_processing ();
26333
26334   /* Restore the queue.  */
26335   pop_unparsed_function_queues (parser);
26336   timevar_pop (TV_PARSE_INMETH);
26337 }
26338
26339 /* If DECL contains any default args, remember it on the unparsed
26340    functions queue.  */
26341
26342 static void
26343 cp_parser_save_default_args (cp_parser* parser, tree decl)
26344 {
26345   tree probe;
26346
26347   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26348        probe;
26349        probe = TREE_CHAIN (probe))
26350     if (TREE_PURPOSE (probe))
26351       {
26352         cp_default_arg_entry entry = {current_class_type, decl};
26353         vec_safe_push (unparsed_funs_with_default_args, entry);
26354         break;
26355       }
26356 }
26357
26358 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26359    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
26360    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
26361    from the parameter-type-list.  */
26362
26363 static tree
26364 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26365                                       tree default_arg, tree parmtype)
26366 {
26367   cp_token_cache *tokens;
26368   tree parsed_arg;
26369   bool dummy;
26370
26371   if (default_arg == error_mark_node)
26372     return error_mark_node;
26373
26374   /* Push the saved tokens for the default argument onto the parser's
26375      lexer stack.  */
26376   tokens = DEFARG_TOKENS (default_arg);
26377   cp_parser_push_lexer_for_tokens (parser, tokens);
26378
26379   start_lambda_scope (decl);
26380
26381   /* Parse the default argument.  */
26382   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26383   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26384     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26385
26386   finish_lambda_scope ();
26387
26388   if (parsed_arg == error_mark_node)
26389     cp_parser_skip_to_end_of_statement (parser);
26390
26391   if (!processing_template_decl)
26392     {
26393       /* In a non-template class, check conversions now.  In a template,
26394          we'll wait and instantiate these as needed.  */
26395       if (TREE_CODE (decl) == PARM_DECL)
26396         parsed_arg = check_default_argument (parmtype, parsed_arg,
26397                                              tf_warning_or_error);
26398       else
26399         parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26400     }
26401
26402   /* If the token stream has not been completely used up, then
26403      there was extra junk after the end of the default
26404      argument.  */
26405   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26406     {
26407       if (TREE_CODE (decl) == PARM_DECL)
26408         cp_parser_error (parser, "expected %<,%>");
26409       else
26410         cp_parser_error (parser, "expected %<;%>");
26411     }
26412
26413   /* Revert to the main lexer.  */
26414   cp_parser_pop_lexer (parser);
26415
26416   return parsed_arg;
26417 }
26418
26419 /* FIELD is a non-static data member with an initializer which we saved for
26420    later; parse it now.  */
26421
26422 static void
26423 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26424 {
26425   tree def;
26426
26427   maybe_begin_member_template_processing (field);
26428
26429   push_unparsed_function_queues (parser);
26430   def = cp_parser_late_parse_one_default_arg (parser, field,
26431                                               DECL_INITIAL (field),
26432                                               NULL_TREE);
26433   pop_unparsed_function_queues (parser);
26434
26435   maybe_end_member_template_processing ();
26436
26437   DECL_INITIAL (field) = def;
26438 }
26439
26440 /* FN is a FUNCTION_DECL which may contains a parameter with an
26441    unparsed DEFAULT_ARG.  Parse the default args now.  This function
26442    assumes that the current scope is the scope in which the default
26443    argument should be processed.  */
26444
26445 static void
26446 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26447 {
26448   bool saved_local_variables_forbidden_p;
26449   tree parm, parmdecl;
26450
26451   /* While we're parsing the default args, we might (due to the
26452      statement expression extension) encounter more classes.  We want
26453      to handle them right away, but we don't want them getting mixed
26454      up with default args that are currently in the queue.  */
26455   push_unparsed_function_queues (parser);
26456
26457   /* Local variable names (and the `this' keyword) may not appear
26458      in a default argument.  */
26459   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26460   parser->local_variables_forbidden_p = true;
26461
26462   push_defarg_context (fn);
26463
26464   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26465          parmdecl = DECL_ARGUMENTS (fn);
26466        parm && parm != void_list_node;
26467        parm = TREE_CHAIN (parm),
26468          parmdecl = DECL_CHAIN (parmdecl))
26469     {
26470       tree default_arg = TREE_PURPOSE (parm);
26471       tree parsed_arg;
26472       vec<tree, va_gc> *insts;
26473       tree copy;
26474       unsigned ix;
26475
26476       if (!default_arg)
26477         continue;
26478
26479       if (TREE_CODE (default_arg) != DEFAULT_ARG)
26480         /* This can happen for a friend declaration for a function
26481            already declared with default arguments.  */
26482         continue;
26483
26484       parsed_arg
26485         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26486                                                 default_arg,
26487                                                 TREE_VALUE (parm));
26488       if (parsed_arg == error_mark_node)
26489         {
26490           continue;
26491         }
26492
26493       TREE_PURPOSE (parm) = parsed_arg;
26494
26495       /* Update any instantiations we've already created.  */
26496       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26497            vec_safe_iterate (insts, ix, &copy); ix++)
26498         TREE_PURPOSE (copy) = parsed_arg;
26499     }
26500
26501   pop_defarg_context ();
26502
26503   /* Make sure no default arg is missing.  */
26504   check_default_args (fn);
26505
26506   /* Restore the state of local_variables_forbidden_p.  */
26507   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26508
26509   /* Restore the queue.  */
26510   pop_unparsed_function_queues (parser);
26511 }
26512
26513 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26514
26515      sizeof ... ( identifier )
26516
26517    where the 'sizeof' token has already been consumed.  */
26518
26519 static tree
26520 cp_parser_sizeof_pack (cp_parser *parser)
26521 {
26522   /* Consume the `...'.  */
26523   cp_lexer_consume_token (parser->lexer);
26524   maybe_warn_variadic_templates ();
26525
26526   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26527   if (paren)
26528     cp_lexer_consume_token (parser->lexer);
26529   else
26530     permerror (cp_lexer_peek_token (parser->lexer)->location,
26531                "%<sizeof...%> argument must be surrounded by parentheses");
26532
26533   cp_token *token = cp_lexer_peek_token (parser->lexer);
26534   tree name = cp_parser_identifier (parser);
26535   if (name == error_mark_node)
26536     return error_mark_node;
26537   /* The name is not qualified.  */
26538   parser->scope = NULL_TREE;
26539   parser->qualifying_scope = NULL_TREE;
26540   parser->object_scope = NULL_TREE;
26541   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26542   if (expr == error_mark_node)
26543     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26544                                  token->location);
26545   if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26546     expr = TREE_TYPE (expr);
26547   else if (TREE_CODE (expr) == CONST_DECL)
26548     expr = DECL_INITIAL (expr);
26549   expr = make_pack_expansion (expr);
26550   PACK_EXPANSION_SIZEOF_P (expr) = true;
26551
26552   if (paren)
26553     cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26554
26555   return expr;
26556 }
26557
26558 /* Parse the operand of `sizeof' (or a similar operator).  Returns
26559    either a TYPE or an expression, depending on the form of the
26560    input.  The KEYWORD indicates which kind of expression we have
26561    encountered.  */
26562
26563 static tree
26564 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26565 {
26566   tree expr = NULL_TREE;
26567   const char *saved_message;
26568   char *tmp;
26569   bool saved_integral_constant_expression_p;
26570   bool saved_non_integral_constant_expression_p;
26571
26572   /* If it's a `...', then we are computing the length of a parameter
26573      pack.  */
26574   if (keyword == RID_SIZEOF
26575       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26576     return cp_parser_sizeof_pack (parser);
26577
26578   /* Types cannot be defined in a `sizeof' expression.  Save away the
26579      old message.  */
26580   saved_message = parser->type_definition_forbidden_message;
26581   /* And create the new one.  */
26582   tmp = concat ("types may not be defined in %<",
26583                 IDENTIFIER_POINTER (ridpointers[keyword]),
26584                 "%> expressions", NULL);
26585   parser->type_definition_forbidden_message = tmp;
26586
26587   /* The restrictions on constant-expressions do not apply inside
26588      sizeof expressions.  */
26589   saved_integral_constant_expression_p
26590     = parser->integral_constant_expression_p;
26591   saved_non_integral_constant_expression_p
26592     = parser->non_integral_constant_expression_p;
26593   parser->integral_constant_expression_p = false;
26594
26595   /* Do not actually evaluate the expression.  */
26596   ++cp_unevaluated_operand;
26597   ++c_inhibit_evaluation_warnings;
26598   /* If it's a `(', then we might be looking at the type-id
26599      construction.  */
26600   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26601     {
26602       tree type = NULL_TREE;
26603
26604       /* We can't be sure yet whether we're looking at a type-id or an
26605          expression.  */
26606       cp_parser_parse_tentatively (parser);
26607       /* Note: as a GNU Extension, compound literals are considered
26608          postfix-expressions as they are in C99, so they are valid
26609          arguments to sizeof.  See comment in cp_parser_cast_expression
26610          for details.  */
26611       if (cp_parser_compound_literal_p (parser))
26612         cp_parser_simulate_error (parser);
26613       else
26614         {
26615           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26616           parser->in_type_id_in_expr_p = true;
26617           /* Look for the type-id.  */
26618           type = cp_parser_type_id (parser);
26619           /* Look for the closing `)'.  */
26620           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26621           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26622         }
26623
26624       /* If all went well, then we're done.  */
26625       if (cp_parser_parse_definitely (parser))
26626         {
26627           cp_decl_specifier_seq decl_specs;
26628
26629           /* Build a trivial decl-specifier-seq.  */
26630           clear_decl_specs (&decl_specs);
26631           decl_specs.type = type;
26632
26633           /* Call grokdeclarator to figure out what type this is.  */
26634           expr = grokdeclarator (NULL,
26635                                  &decl_specs,
26636                                  TYPENAME,
26637                                  /*initialized=*/0,
26638                                  /*attrlist=*/NULL);
26639         }
26640     }
26641
26642   /* If the type-id production did not work out, then we must be
26643      looking at the unary-expression production.  */
26644   if (!expr)
26645     expr = cp_parser_unary_expression (parser);
26646
26647   /* Go back to evaluating expressions.  */
26648   --cp_unevaluated_operand;
26649   --c_inhibit_evaluation_warnings;
26650
26651   /* Free the message we created.  */
26652   free (tmp);
26653   /* And restore the old one.  */
26654   parser->type_definition_forbidden_message = saved_message;
26655   parser->integral_constant_expression_p
26656     = saved_integral_constant_expression_p;
26657   parser->non_integral_constant_expression_p
26658     = saved_non_integral_constant_expression_p;
26659
26660   return expr;
26661 }
26662
26663 /* If the current declaration has no declarator, return true.  */
26664
26665 static bool
26666 cp_parser_declares_only_class_p (cp_parser *parser)
26667 {
26668   /* If the next token is a `;' or a `,' then there is no
26669      declarator.  */
26670   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26671           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26672 }
26673
26674 /* Update the DECL_SPECS to reflect the storage class indicated by
26675    KEYWORD.  */
26676
26677 static void
26678 cp_parser_set_storage_class (cp_parser *parser,
26679                              cp_decl_specifier_seq *decl_specs,
26680                              enum rid keyword,
26681                              cp_token *token)
26682 {
26683   cp_storage_class storage_class;
26684
26685   if (parser->in_unbraced_linkage_specification_p)
26686     {
26687       error_at (token->location, "invalid use of %qD in linkage specification",
26688                 ridpointers[keyword]);
26689       return;
26690     }
26691   else if (decl_specs->storage_class != sc_none)
26692     {
26693       decl_specs->conflicting_specifiers_p = true;
26694       return;
26695     }
26696
26697   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26698       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26699       && decl_specs->gnu_thread_keyword_p)
26700     {
26701       pedwarn (decl_specs->locations[ds_thread], 0,
26702                 "%<__thread%> before %qD", ridpointers[keyword]);
26703     }
26704
26705   switch (keyword)
26706     {
26707     case RID_AUTO:
26708       storage_class = sc_auto;
26709       break;
26710     case RID_REGISTER:
26711       storage_class = sc_register;
26712       break;
26713     case RID_STATIC:
26714       storage_class = sc_static;
26715       break;
26716     case RID_EXTERN:
26717       storage_class = sc_extern;
26718       break;
26719     case RID_MUTABLE:
26720       storage_class = sc_mutable;
26721       break;
26722     default:
26723       gcc_unreachable ();
26724     }
26725   decl_specs->storage_class = storage_class;
26726   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26727
26728   /* A storage class specifier cannot be applied alongside a typedef 
26729      specifier. If there is a typedef specifier present then set 
26730      conflicting_specifiers_p which will trigger an error later
26731      on in grokdeclarator. */
26732   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26733     decl_specs->conflicting_specifiers_p = true;
26734 }
26735
26736 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
26737    is true, the type is a class or enum definition.  */
26738
26739 static void
26740 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26741                               tree type_spec,
26742                               cp_token *token,
26743                               bool type_definition_p)
26744 {
26745   decl_specs->any_specifiers_p = true;
26746
26747   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26748      (with, for example, in "typedef int wchar_t;") we remember that
26749      this is what happened.  In system headers, we ignore these
26750      declarations so that G++ can work with system headers that are not
26751      C++-safe.  */
26752   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26753       && !type_definition_p
26754       && (type_spec == boolean_type_node
26755           || type_spec == char16_type_node
26756           || type_spec == char32_type_node
26757           || type_spec == wchar_type_node)
26758       && (decl_specs->type
26759           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26760           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26761           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26762           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26763     {
26764       decl_specs->redefined_builtin_type = type_spec;
26765       set_and_check_decl_spec_loc (decl_specs,
26766                                    ds_redefined_builtin_type_spec,
26767                                    token);
26768       if (!decl_specs->type)
26769         {
26770           decl_specs->type = type_spec;
26771           decl_specs->type_definition_p = false;
26772           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26773         }
26774     }
26775   else if (decl_specs->type)
26776     decl_specs->multiple_types_p = true;
26777   else
26778     {
26779       decl_specs->type = type_spec;
26780       decl_specs->type_definition_p = type_definition_p;
26781       decl_specs->redefined_builtin_type = NULL_TREE;
26782       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26783     }
26784 }
26785
26786 /* True iff TOKEN is the GNU keyword __thread.  */
26787
26788 static bool
26789 token_is__thread (cp_token *token)
26790 {
26791   gcc_assert (token->keyword == RID_THREAD);
26792   return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26793 }
26794
26795 /* Set the location for a declarator specifier and check if it is
26796    duplicated.
26797
26798    DECL_SPECS is the sequence of declarator specifiers onto which to
26799    set the location.
26800
26801    DS is the single declarator specifier to set which location  is to
26802    be set onto the existing sequence of declarators.
26803
26804    LOCATION is the location for the declarator specifier to
26805    consider.  */
26806
26807 static void
26808 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26809                              cp_decl_spec ds, cp_token *token)
26810 {
26811   gcc_assert (ds < ds_last);
26812
26813   if (decl_specs == NULL)
26814     return;
26815
26816   source_location location = token->location;
26817
26818   if (decl_specs->locations[ds] == 0)
26819     {
26820       decl_specs->locations[ds] = location;
26821       if (ds == ds_thread)
26822         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26823     }
26824   else
26825     {
26826       if (ds == ds_long)
26827         {
26828           if (decl_specs->locations[ds_long_long] != 0)
26829             error_at (location,
26830                       "%<long long long%> is too long for GCC");
26831           else
26832             {
26833               decl_specs->locations[ds_long_long] = location;
26834               pedwarn_cxx98 (location,
26835                              OPT_Wlong_long, 
26836                              "ISO C++ 1998 does not support %<long long%>");
26837             }
26838         }
26839       else if (ds == ds_thread)
26840         {
26841           bool gnu = token_is__thread (token);
26842           if (gnu != decl_specs->gnu_thread_keyword_p)
26843             error_at (location,
26844                       "both %<__thread%> and %<thread_local%> specified");
26845           else
26846             error_at (location, "duplicate %qD", token->u.value);
26847         }
26848       else
26849         {
26850           static const char *const decl_spec_names[] = {
26851             "signed",
26852             "unsigned",
26853             "short",
26854             "long",
26855             "const",
26856             "volatile",
26857             "restrict",
26858             "inline",
26859             "virtual",
26860             "explicit",
26861             "friend",
26862             "typedef",
26863             "using",
26864             "constexpr",
26865             "__complex"
26866           };
26867           error_at (location,
26868                     "duplicate %qs", decl_spec_names[ds]);
26869         }
26870     }
26871 }
26872
26873 /* Return true iff the declarator specifier DS is present in the
26874    sequence of declarator specifiers DECL_SPECS.  */
26875
26876 bool
26877 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26878                           cp_decl_spec ds)
26879 {
26880   gcc_assert (ds < ds_last);
26881
26882   if (decl_specs == NULL)
26883     return false;
26884
26885   return decl_specs->locations[ds] != 0;
26886 }
26887
26888 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26889    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
26890
26891 static bool
26892 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26893 {
26894   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26895 }
26896
26897 /* Issue an error message indicating that TOKEN_DESC was expected.
26898    If KEYWORD is true, it indicated this function is called by
26899    cp_parser_require_keword and the required token can only be
26900    a indicated keyword. */
26901
26902 static void
26903 cp_parser_required_error (cp_parser *parser,
26904                           required_token token_desc,
26905                           bool keyword)
26906 {
26907   switch (token_desc)
26908     {
26909       case RT_NEW:
26910         cp_parser_error (parser, "expected %<new%>");
26911         return;
26912       case RT_DELETE:
26913         cp_parser_error (parser, "expected %<delete%>");
26914         return;
26915       case RT_RETURN:
26916         cp_parser_error (parser, "expected %<return%>");
26917         return;
26918       case RT_WHILE:
26919         cp_parser_error (parser, "expected %<while%>");
26920         return;
26921       case RT_EXTERN:
26922         cp_parser_error (parser, "expected %<extern%>");
26923         return;
26924       case RT_STATIC_ASSERT:
26925         cp_parser_error (parser, "expected %<static_assert%>");
26926         return;
26927       case RT_DECLTYPE:
26928         cp_parser_error (parser, "expected %<decltype%>");
26929         return;
26930       case RT_OPERATOR:
26931         cp_parser_error (parser, "expected %<operator%>");
26932         return;
26933       case RT_CLASS:
26934         cp_parser_error (parser, "expected %<class%>");
26935         return;
26936       case RT_TEMPLATE:
26937         cp_parser_error (parser, "expected %<template%>");
26938         return;
26939       case RT_NAMESPACE:
26940         cp_parser_error (parser, "expected %<namespace%>");
26941         return;
26942       case RT_USING:
26943         cp_parser_error (parser, "expected %<using%>");
26944         return;
26945       case RT_ASM:
26946         cp_parser_error (parser, "expected %<asm%>");
26947         return;
26948       case RT_TRY:
26949         cp_parser_error (parser, "expected %<try%>");
26950         return;
26951       case RT_CATCH:
26952         cp_parser_error (parser, "expected %<catch%>");
26953         return;
26954       case RT_THROW:
26955         cp_parser_error (parser, "expected %<throw%>");
26956         return;
26957       case RT_LABEL:
26958         cp_parser_error (parser, "expected %<__label__%>");
26959         return;
26960       case RT_AT_TRY:
26961         cp_parser_error (parser, "expected %<@try%>");
26962         return;
26963       case RT_AT_SYNCHRONIZED:
26964         cp_parser_error (parser, "expected %<@synchronized%>");
26965         return;
26966       case RT_AT_THROW:
26967         cp_parser_error (parser, "expected %<@throw%>");
26968         return;
26969       case RT_TRANSACTION_ATOMIC:
26970         cp_parser_error (parser, "expected %<__transaction_atomic%>");
26971         return;
26972       case RT_TRANSACTION_RELAXED:
26973         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26974         return;
26975       default:
26976         break;
26977     }
26978   if (!keyword)
26979     {
26980       switch (token_desc)
26981         {
26982           case RT_SEMICOLON:
26983             cp_parser_error (parser, "expected %<;%>");
26984             return;
26985           case RT_OPEN_PAREN:
26986             cp_parser_error (parser, "expected %<(%>");
26987             return;
26988           case RT_CLOSE_BRACE:
26989             cp_parser_error (parser, "expected %<}%>");
26990             return;
26991           case RT_OPEN_BRACE:
26992             cp_parser_error (parser, "expected %<{%>");
26993             return;
26994           case RT_CLOSE_SQUARE:
26995             cp_parser_error (parser, "expected %<]%>");
26996             return;
26997           case RT_OPEN_SQUARE:
26998             cp_parser_error (parser, "expected %<[%>");
26999             return;
27000           case RT_COMMA:
27001             cp_parser_error (parser, "expected %<,%>");
27002             return;
27003           case RT_SCOPE:
27004             cp_parser_error (parser, "expected %<::%>");
27005             return;
27006           case RT_LESS:
27007             cp_parser_error (parser, "expected %<<%>");
27008             return;
27009           case RT_GREATER:
27010             cp_parser_error (parser, "expected %<>%>");
27011             return;
27012           case RT_EQ:
27013             cp_parser_error (parser, "expected %<=%>");
27014             return;
27015           case RT_ELLIPSIS:
27016             cp_parser_error (parser, "expected %<...%>");
27017             return;
27018           case RT_MULT:
27019             cp_parser_error (parser, "expected %<*%>");
27020             return;
27021           case RT_COMPL:
27022             cp_parser_error (parser, "expected %<~%>");
27023             return;
27024           case RT_COLON:
27025             cp_parser_error (parser, "expected %<:%>");
27026             return;
27027           case RT_COLON_SCOPE:
27028             cp_parser_error (parser, "expected %<:%> or %<::%>");
27029             return;
27030           case RT_CLOSE_PAREN:
27031             cp_parser_error (parser, "expected %<)%>");
27032             return;
27033           case RT_COMMA_CLOSE_PAREN:
27034             cp_parser_error (parser, "expected %<,%> or %<)%>");
27035             return;
27036           case RT_PRAGMA_EOL:
27037             cp_parser_error (parser, "expected end of line");
27038             return;
27039           case RT_NAME:
27040             cp_parser_error (parser, "expected identifier");
27041             return;
27042           case RT_SELECT:
27043             cp_parser_error (parser, "expected selection-statement");
27044             return;
27045           case RT_INTERATION:
27046             cp_parser_error (parser, "expected iteration-statement");
27047             return;
27048           case RT_JUMP:
27049             cp_parser_error (parser, "expected jump-statement");
27050             return;
27051           case RT_CLASS_KEY:
27052             cp_parser_error (parser, "expected class-key");
27053             return;
27054           case RT_CLASS_TYPENAME_TEMPLATE:
27055             cp_parser_error (parser,
27056                  "expected %<class%>, %<typename%>, or %<template%>");
27057             return;
27058           default:
27059             gcc_unreachable ();
27060         }
27061     }
27062   else
27063     gcc_unreachable ();
27064 }
27065
27066
27067
27068 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
27069    issue an error message indicating that TOKEN_DESC was expected.
27070
27071    Returns the token consumed, if the token had the appropriate type.
27072    Otherwise, returns NULL.  */
27073
27074 static cp_token *
27075 cp_parser_require (cp_parser* parser,
27076                    enum cpp_ttype type,
27077                    required_token token_desc)
27078 {
27079   if (cp_lexer_next_token_is (parser->lexer, type))
27080     return cp_lexer_consume_token (parser->lexer);
27081   else
27082     {
27083       /* Output the MESSAGE -- unless we're parsing tentatively.  */
27084       if (!cp_parser_simulate_error (parser))
27085         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27086       return NULL;
27087     }
27088 }
27089
27090 /* An error message is produced if the next token is not '>'.
27091    All further tokens are skipped until the desired token is
27092    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
27093
27094 static void
27095 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27096 {
27097   /* Current level of '< ... >'.  */
27098   unsigned level = 0;
27099   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
27100   unsigned nesting_depth = 0;
27101
27102   /* Are we ready, yet?  If not, issue error message.  */
27103   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27104     return;
27105
27106   /* Skip tokens until the desired token is found.  */
27107   while (true)
27108     {
27109       /* Peek at the next token.  */
27110       switch (cp_lexer_peek_token (parser->lexer)->type)
27111         {
27112         case CPP_LESS:
27113           if (!nesting_depth)
27114             ++level;
27115           break;
27116
27117         case CPP_RSHIFT:
27118           if (cxx_dialect == cxx98)
27119             /* C++0x views the `>>' operator as two `>' tokens, but
27120                C++98 does not. */
27121             break;
27122           else if (!nesting_depth && level-- == 0)
27123             {
27124               /* We've hit a `>>' where the first `>' closes the
27125                  template argument list, and the second `>' is
27126                  spurious.  Just consume the `>>' and stop; we've
27127                  already produced at least one error.  */
27128               cp_lexer_consume_token (parser->lexer);
27129               return;
27130             }
27131           /* Fall through for C++0x, so we handle the second `>' in
27132              the `>>'.  */
27133
27134         case CPP_GREATER:
27135           if (!nesting_depth && level-- == 0)
27136             {
27137               /* We've reached the token we want, consume it and stop.  */
27138               cp_lexer_consume_token (parser->lexer);
27139               return;
27140             }
27141           break;
27142
27143         case CPP_OPEN_PAREN:
27144         case CPP_OPEN_SQUARE:
27145           ++nesting_depth;
27146           break;
27147
27148         case CPP_CLOSE_PAREN:
27149         case CPP_CLOSE_SQUARE:
27150           if (nesting_depth-- == 0)
27151             return;
27152           break;
27153
27154         case CPP_EOF:
27155         case CPP_PRAGMA_EOL:
27156         case CPP_SEMICOLON:
27157         case CPP_OPEN_BRACE:
27158         case CPP_CLOSE_BRACE:
27159           /* The '>' was probably forgotten, don't look further.  */
27160           return;
27161
27162         default:
27163           break;
27164         }
27165
27166       /* Consume this token.  */
27167       cp_lexer_consume_token (parser->lexer);
27168     }
27169 }
27170
27171 /* If the next token is the indicated keyword, consume it.  Otherwise,
27172    issue an error message indicating that TOKEN_DESC was expected.
27173
27174    Returns the token consumed, if the token had the appropriate type.
27175    Otherwise, returns NULL.  */
27176
27177 static cp_token *
27178 cp_parser_require_keyword (cp_parser* parser,
27179                            enum rid keyword,
27180                            required_token token_desc)
27181 {
27182   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27183
27184   if (token && token->keyword != keyword)
27185     {
27186       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
27187       return NULL;
27188     }
27189
27190   return token;
27191 }
27192
27193 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27194    function-definition.  */
27195
27196 static bool
27197 cp_parser_token_starts_function_definition_p (cp_token* token)
27198 {
27199   return (/* An ordinary function-body begins with an `{'.  */
27200           token->type == CPP_OPEN_BRACE
27201           /* A ctor-initializer begins with a `:'.  */
27202           || token->type == CPP_COLON
27203           /* A function-try-block begins with `try'.  */
27204           || token->keyword == RID_TRY
27205           /* A function-transaction-block begins with `__transaction_atomic'
27206              or `__transaction_relaxed'.  */
27207           || token->keyword == RID_TRANSACTION_ATOMIC
27208           || token->keyword == RID_TRANSACTION_RELAXED
27209           /* The named return value extension begins with `return'.  */
27210           || token->keyword == RID_RETURN);
27211 }
27212
27213 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27214    definition.  */
27215
27216 static bool
27217 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27218 {
27219   cp_token *token;
27220
27221   token = cp_lexer_peek_token (parser->lexer);
27222   return (token->type == CPP_OPEN_BRACE
27223           || (token->type == CPP_COLON
27224               && !parser->colon_doesnt_start_class_def_p));
27225 }
27226
27227 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27228    C++0x) ending a template-argument.  */
27229
27230 static bool
27231 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27232 {
27233   cp_token *token;
27234
27235   token = cp_lexer_peek_token (parser->lexer);
27236   return (token->type == CPP_COMMA 
27237           || token->type == CPP_GREATER
27238           || token->type == CPP_ELLIPSIS
27239           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27240 }
27241
27242 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27243    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
27244
27245 static bool
27246 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27247                                                      size_t n)
27248 {
27249   cp_token *token;
27250
27251   token = cp_lexer_peek_nth_token (parser->lexer, n);
27252   if (token->type == CPP_LESS)
27253     return true;
27254   /* Check for the sequence `<::' in the original code. It would be lexed as
27255      `[:', where `[' is a digraph, and there is no whitespace before
27256      `:'.  */
27257   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27258     {
27259       cp_token *token2;
27260       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27261       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27262         return true;
27263     }
27264   return false;
27265 }
27266
27267 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27268    or none_type otherwise.  */
27269
27270 static enum tag_types
27271 cp_parser_token_is_class_key (cp_token* token)
27272 {
27273   switch (token->keyword)
27274     {
27275     case RID_CLASS:
27276       return class_type;
27277     case RID_STRUCT:
27278       return record_type;
27279     case RID_UNION:
27280       return union_type;
27281
27282     default:
27283       return none_type;
27284     }
27285 }
27286
27287 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27288    or none_type otherwise or if the token is null.  */
27289
27290 static enum tag_types
27291 cp_parser_token_is_type_parameter_key (cp_token* token)
27292 {
27293   if (!token)
27294     return none_type;
27295
27296   switch (token->keyword)
27297     {
27298     case RID_CLASS:
27299       return class_type;
27300     case RID_TYPENAME:
27301       return typename_type;
27302
27303     default:
27304       return none_type;
27305     }
27306 }
27307
27308 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
27309
27310 static void
27311 cp_parser_check_class_key (enum tag_types class_key, tree type)
27312 {
27313   if (type == error_mark_node)
27314     return;
27315   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27316     {
27317       if (permerror (input_location, "%qs tag used in naming %q#T",
27318                      class_key == union_type ? "union"
27319                      : class_key == record_type ? "struct" : "class",
27320                      type))
27321         inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27322                 "%q#T was previously declared here", type);
27323     }
27324 }
27325
27326 /* Issue an error message if DECL is redeclared with different
27327    access than its original declaration [class.access.spec/3].
27328    This applies to nested classes and nested class templates.
27329    [class.mem/1].  */
27330
27331 static void
27332 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27333 {
27334   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
27335     return;
27336
27337   if ((TREE_PRIVATE (decl)
27338        != (current_access_specifier == access_private_node))
27339       || (TREE_PROTECTED (decl)
27340           != (current_access_specifier == access_protected_node)))
27341     error_at (location, "%qD redeclared with different access", decl);
27342 }
27343
27344 /* Look for the `template' keyword, as a syntactic disambiguator.
27345    Return TRUE iff it is present, in which case it will be
27346    consumed.  */
27347
27348 static bool
27349 cp_parser_optional_template_keyword (cp_parser *parser)
27350 {
27351   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27352     {
27353       /* In C++98 the `template' keyword can only be used within templates;
27354          outside templates the parser can always figure out what is a
27355          template and what is not.  In C++11,  per the resolution of DR 468,
27356          `template' is allowed in cases where it is not strictly necessary.  */
27357       if (!processing_template_decl
27358           && pedantic && cxx_dialect == cxx98)
27359         {
27360           cp_token *token = cp_lexer_peek_token (parser->lexer);
27361           pedwarn (token->location, OPT_Wpedantic,
27362                    "in C++98 %<template%> (as a disambiguator) is only "
27363                    "allowed within templates");
27364           /* If this part of the token stream is rescanned, the same
27365              error message would be generated.  So, we purge the token
27366              from the stream.  */
27367           cp_lexer_purge_token (parser->lexer);
27368           return false;
27369         }
27370       else
27371         {
27372           /* Consume the `template' keyword.  */
27373           cp_lexer_consume_token (parser->lexer);
27374           return true;
27375         }
27376     }
27377   return false;
27378 }
27379
27380 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
27381    set PARSER->SCOPE, and perform other related actions.  */
27382
27383 static void
27384 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27385 {
27386   struct tree_check *check_value;
27387
27388   /* Get the stored value.  */
27389   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27390   /* Set the scope from the stored value.  */
27391   parser->scope = saved_checks_value (check_value);
27392   parser->qualifying_scope = check_value->qualifying_scope;
27393   parser->object_scope = NULL_TREE;
27394 }
27395
27396 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
27397    encounter the end of a block before what we were looking for.  */
27398
27399 static bool
27400 cp_parser_cache_group (cp_parser *parser,
27401                        enum cpp_ttype end,
27402                        unsigned depth)
27403 {
27404   while (true)
27405     {
27406       cp_token *token = cp_lexer_peek_token (parser->lexer);
27407
27408       /* Abort a parenthesized expression if we encounter a semicolon.  */
27409       if ((end == CPP_CLOSE_PAREN || depth == 0)
27410           && token->type == CPP_SEMICOLON)
27411         return true;
27412       /* If we've reached the end of the file, stop.  */
27413       if (token->type == CPP_EOF
27414           || (end != CPP_PRAGMA_EOL
27415               && token->type == CPP_PRAGMA_EOL))
27416         return true;
27417       if (token->type == CPP_CLOSE_BRACE && depth == 0)
27418         /* We've hit the end of an enclosing block, so there's been some
27419            kind of syntax error.  */
27420         return true;
27421
27422       /* Consume the token.  */
27423       cp_lexer_consume_token (parser->lexer);
27424       /* See if it starts a new group.  */
27425       if (token->type == CPP_OPEN_BRACE)
27426         {
27427           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27428           /* In theory this should probably check end == '}', but
27429              cp_parser_save_member_function_body needs it to exit
27430              after either '}' or ')' when called with ')'.  */
27431           if (depth == 0)
27432             return false;
27433         }
27434       else if (token->type == CPP_OPEN_PAREN)
27435         {
27436           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27437           if (depth == 0 && end == CPP_CLOSE_PAREN)
27438             return false;
27439         }
27440       else if (token->type == CPP_PRAGMA)
27441         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27442       else if (token->type == end)
27443         return false;
27444     }
27445 }
27446
27447 /* Like above, for caching a default argument or NSDMI.  Both of these are
27448    terminated by a non-nested comma, but it can be unclear whether or not a
27449    comma is nested in a template argument list unless we do more parsing.
27450    In order to handle this ambiguity, when we encounter a ',' after a '<'
27451    we try to parse what follows as a parameter-declaration-list (in the
27452    case of a default argument) or a member-declarator (in the case of an
27453    NSDMI).  If that succeeds, then we stop caching.  */
27454
27455 static tree
27456 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27457 {
27458   unsigned depth = 0;
27459   int maybe_template_id = 0;
27460   cp_token *first_token;
27461   cp_token *token;
27462   tree default_argument;
27463
27464   /* Add tokens until we have processed the entire default
27465      argument.  We add the range [first_token, token).  */
27466   first_token = cp_lexer_peek_token (parser->lexer);
27467   if (first_token->type == CPP_OPEN_BRACE)
27468     {
27469       /* For list-initialization, this is straightforward.  */
27470       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27471       token = cp_lexer_peek_token (parser->lexer);
27472     }
27473   else while (true)
27474     {
27475       bool done = false;
27476
27477       /* Peek at the next token.  */
27478       token = cp_lexer_peek_token (parser->lexer);
27479       /* What we do depends on what token we have.  */
27480       switch (token->type)
27481         {
27482           /* In valid code, a default argument must be
27483              immediately followed by a `,' `)', or `...'.  */
27484         case CPP_COMMA:
27485           if (depth == 0 && maybe_template_id)
27486             {
27487               /* If we've seen a '<', we might be in a
27488                  template-argument-list.  Until Core issue 325 is
27489                  resolved, we don't know how this situation ought
27490                  to be handled, so try to DTRT.  We check whether
27491                  what comes after the comma is a valid parameter
27492                  declaration list.  If it is, then the comma ends
27493                  the default argument; otherwise the default
27494                  argument continues.  */
27495               bool error = false;
27496               cp_token *peek;
27497
27498               /* Set ITALP so cp_parser_parameter_declaration_list
27499                  doesn't decide to commit to this parse.  */
27500               bool saved_italp = parser->in_template_argument_list_p;
27501               parser->in_template_argument_list_p = true;
27502
27503               cp_parser_parse_tentatively (parser);
27504
27505               if (nsdmi)
27506                 {
27507                   /* Parse declarators until we reach a non-comma or
27508                      somthing that cannot be an initializer.
27509                      Just checking whether we're looking at a single
27510                      declarator is insufficient.  Consider:
27511                        int var = tuple<T,U>::x;
27512                      The template parameter 'U' looks exactly like a
27513                      declarator.  */
27514                   do
27515                     {
27516                       int ctor_dtor_or_conv_p;
27517                       cp_lexer_consume_token (parser->lexer);
27518                       cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27519                                             &ctor_dtor_or_conv_p,
27520                                             /*parenthesized_p=*/NULL,
27521                                             /*member_p=*/true,
27522                                             /*friend_p=*/false);
27523                       peek = cp_lexer_peek_token (parser->lexer);
27524                       if (cp_parser_error_occurred (parser))
27525                         break;
27526                     }
27527                   while (peek->type == CPP_COMMA);
27528                   /* If we met an '=' or ';' then the original comma
27529                      was the end of the NSDMI.  Otherwise assume
27530                      we're still in the NSDMI.  */
27531                   error = (peek->type != CPP_EQ
27532                            && peek->type != CPP_SEMICOLON);
27533                 }
27534               else
27535                 {
27536                   cp_lexer_consume_token (parser->lexer);
27537                   begin_scope (sk_function_parms, NULL_TREE);
27538                   cp_parser_parameter_declaration_list (parser, &error);
27539                   pop_bindings_and_leave_scope ();
27540                 }
27541               if (!cp_parser_error_occurred (parser) && !error)
27542                 done = true;
27543               cp_parser_abort_tentative_parse (parser);
27544
27545               parser->in_template_argument_list_p = saved_italp;
27546               break;
27547             }
27548         case CPP_CLOSE_PAREN:
27549         case CPP_ELLIPSIS:
27550           /* If we run into a non-nested `;', `}', or `]',
27551              then the code is invalid -- but the default
27552              argument is certainly over.  */
27553         case CPP_SEMICOLON:
27554         case CPP_CLOSE_BRACE:
27555         case CPP_CLOSE_SQUARE:
27556           if (depth == 0
27557               /* Handle correctly int n = sizeof ... ( p );  */
27558               && token->type != CPP_ELLIPSIS)
27559             done = true;
27560           /* Update DEPTH, if necessary.  */
27561           else if (token->type == CPP_CLOSE_PAREN
27562                    || token->type == CPP_CLOSE_BRACE
27563                    || token->type == CPP_CLOSE_SQUARE)
27564             --depth;
27565           break;
27566
27567         case CPP_OPEN_PAREN:
27568         case CPP_OPEN_SQUARE:
27569         case CPP_OPEN_BRACE:
27570           ++depth;
27571           break;
27572
27573         case CPP_LESS:
27574           if (depth == 0)
27575             /* This might be the comparison operator, or it might
27576                start a template argument list.  */
27577             ++maybe_template_id;
27578           break;
27579
27580         case CPP_RSHIFT:
27581           if (cxx_dialect == cxx98)
27582             break;
27583           /* Fall through for C++0x, which treats the `>>'
27584              operator like two `>' tokens in certain
27585              cases.  */
27586
27587         case CPP_GREATER:
27588           if (depth == 0)
27589             {
27590               /* This might be an operator, or it might close a
27591                  template argument list.  But if a previous '<'
27592                  started a template argument list, this will have
27593                  closed it, so we can't be in one anymore.  */
27594               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27595               if (maybe_template_id < 0)
27596                 maybe_template_id = 0;
27597             }
27598           break;
27599
27600           /* If we run out of tokens, issue an error message.  */
27601         case CPP_EOF:
27602         case CPP_PRAGMA_EOL:
27603           error_at (token->location, "file ends in default argument");
27604           return error_mark_node;
27605
27606         case CPP_NAME:
27607         case CPP_SCOPE:
27608           /* In these cases, we should look for template-ids.
27609              For example, if the default argument is
27610              `X<int, double>()', we need to do name lookup to
27611              figure out whether or not `X' is a template; if
27612              so, the `,' does not end the default argument.
27613
27614              That is not yet done.  */
27615           break;
27616
27617         default:
27618           break;
27619         }
27620
27621       /* If we've reached the end, stop.  */
27622       if (done)
27623         break;
27624
27625       /* Add the token to the token block.  */
27626       token = cp_lexer_consume_token (parser->lexer);
27627     }
27628
27629   /* Create a DEFAULT_ARG to represent the unparsed default
27630      argument.  */
27631   default_argument = make_node (DEFAULT_ARG);
27632   DEFARG_TOKENS (default_argument)
27633     = cp_token_cache_new (first_token, token);
27634   DEFARG_INSTANTIATIONS (default_argument) = NULL;
27635
27636   return default_argument;
27637 }
27638
27639 /* Begin parsing tentatively.  We always save tokens while parsing
27640    tentatively so that if the tentative parsing fails we can restore the
27641    tokens.  */
27642
27643 static void
27644 cp_parser_parse_tentatively (cp_parser* parser)
27645 {
27646   /* Enter a new parsing context.  */
27647   parser->context = cp_parser_context_new (parser->context);
27648   /* Begin saving tokens.  */
27649   cp_lexer_save_tokens (parser->lexer);
27650   /* In order to avoid repetitive access control error messages,
27651      access checks are queued up until we are no longer parsing
27652      tentatively.  */
27653   push_deferring_access_checks (dk_deferred);
27654 }
27655
27656 /* Commit to the currently active tentative parse.  */
27657
27658 static void
27659 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27660 {
27661   cp_parser_context *context;
27662   cp_lexer *lexer;
27663
27664   /* Mark all of the levels as committed.  */
27665   lexer = parser->lexer;
27666   for (context = parser->context; context->next; context = context->next)
27667     {
27668       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27669         break;
27670       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27671       while (!cp_lexer_saving_tokens (lexer))
27672         lexer = lexer->next;
27673       cp_lexer_commit_tokens (lexer);
27674     }
27675 }
27676
27677 /* Commit to the topmost currently active tentative parse.
27678
27679    Note that this function shouldn't be called when there are
27680    irreversible side-effects while in a tentative state.  For
27681    example, we shouldn't create a permanent entry in the symbol
27682    table, or issue an error message that might not apply if the
27683    tentative parse is aborted.  */
27684
27685 static void
27686 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27687 {
27688   cp_parser_context *context = parser->context;
27689   cp_lexer *lexer = parser->lexer;
27690
27691   if (context)
27692     {
27693       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27694         return;
27695       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27696
27697       while (!cp_lexer_saving_tokens (lexer))
27698         lexer = lexer->next;
27699       cp_lexer_commit_tokens (lexer);
27700     }
27701 }
27702
27703 /* Abort the currently active tentative parse.  All consumed tokens
27704    will be rolled back, and no diagnostics will be issued.  */
27705
27706 static void
27707 cp_parser_abort_tentative_parse (cp_parser* parser)
27708 {
27709   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27710               || errorcount > 0);
27711   cp_parser_simulate_error (parser);
27712   /* Now, pretend that we want to see if the construct was
27713      successfully parsed.  */
27714   cp_parser_parse_definitely (parser);
27715 }
27716
27717 /* Stop parsing tentatively.  If a parse error has occurred, restore the
27718    token stream.  Otherwise, commit to the tokens we have consumed.
27719    Returns true if no error occurred; false otherwise.  */
27720
27721 static bool
27722 cp_parser_parse_definitely (cp_parser* parser)
27723 {
27724   bool error_occurred;
27725   cp_parser_context *context;
27726
27727   /* Remember whether or not an error occurred, since we are about to
27728      destroy that information.  */
27729   error_occurred = cp_parser_error_occurred (parser);
27730   /* Remove the topmost context from the stack.  */
27731   context = parser->context;
27732   parser->context = context->next;
27733   /* If no parse errors occurred, commit to the tentative parse.  */
27734   if (!error_occurred)
27735     {
27736       /* Commit to the tokens read tentatively, unless that was
27737          already done.  */
27738       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27739         cp_lexer_commit_tokens (parser->lexer);
27740
27741       pop_to_parent_deferring_access_checks ();
27742     }
27743   /* Otherwise, if errors occurred, roll back our state so that things
27744      are just as they were before we began the tentative parse.  */
27745   else
27746     {
27747       cp_lexer_rollback_tokens (parser->lexer);
27748       pop_deferring_access_checks ();
27749     }
27750   /* Add the context to the front of the free list.  */
27751   context->next = cp_parser_context_free_list;
27752   cp_parser_context_free_list = context;
27753
27754   return !error_occurred;
27755 }
27756
27757 /* Returns true if we are parsing tentatively and are not committed to
27758    this tentative parse.  */
27759
27760 static bool
27761 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27762 {
27763   return (cp_parser_parsing_tentatively (parser)
27764           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27765 }
27766
27767 /* Returns nonzero iff an error has occurred during the most recent
27768    tentative parse.  */
27769
27770 static bool
27771 cp_parser_error_occurred (cp_parser* parser)
27772 {
27773   return (cp_parser_parsing_tentatively (parser)
27774           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27775 }
27776
27777 /* Returns nonzero if GNU extensions are allowed.  */
27778
27779 static bool
27780 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27781 {
27782   return parser->allow_gnu_extensions_p;
27783 }
27784 \f
27785 /* Objective-C++ Productions */
27786
27787
27788 /* Parse an Objective-C expression, which feeds into a primary-expression
27789    above.
27790
27791    objc-expression:
27792      objc-message-expression
27793      objc-string-literal
27794      objc-encode-expression
27795      objc-protocol-expression
27796      objc-selector-expression
27797
27798   Returns a tree representation of the expression.  */
27799
27800 static cp_expr
27801 cp_parser_objc_expression (cp_parser* parser)
27802 {
27803   /* Try to figure out what kind of declaration is present.  */
27804   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27805
27806   switch (kwd->type)
27807     {
27808     case CPP_OPEN_SQUARE:
27809       return cp_parser_objc_message_expression (parser);
27810
27811     case CPP_OBJC_STRING:
27812       kwd = cp_lexer_consume_token (parser->lexer);
27813       return objc_build_string_object (kwd->u.value);
27814
27815     case CPP_KEYWORD:
27816       switch (kwd->keyword)
27817         {
27818         case RID_AT_ENCODE:
27819           return cp_parser_objc_encode_expression (parser);
27820
27821         case RID_AT_PROTOCOL:
27822           return cp_parser_objc_protocol_expression (parser);
27823
27824         case RID_AT_SELECTOR:
27825           return cp_parser_objc_selector_expression (parser);
27826
27827         default:
27828           break;
27829         }
27830     default:
27831       error_at (kwd->location,
27832                 "misplaced %<@%D%> Objective-C++ construct",
27833                 kwd->u.value);
27834       cp_parser_skip_to_end_of_block_or_statement (parser);
27835     }
27836
27837   return error_mark_node;
27838 }
27839
27840 /* Parse an Objective-C message expression.
27841
27842    objc-message-expression:
27843      [ objc-message-receiver objc-message-args ]
27844
27845    Returns a representation of an Objective-C message.  */
27846
27847 static tree
27848 cp_parser_objc_message_expression (cp_parser* parser)
27849 {
27850   tree receiver, messageargs;
27851
27852   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27853   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
27854   receiver = cp_parser_objc_message_receiver (parser);
27855   messageargs = cp_parser_objc_message_args (parser);
27856   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27857   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27858
27859   tree result = objc_build_message_expr (receiver, messageargs);
27860
27861   /* Construct a location e.g.
27862        [self func1:5]
27863        ^~~~~~~~~~~~~~
27864      ranging from the '[' to the ']', with the caret at the start.  */
27865   location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27866   protected_set_expr_location (result, combined_loc);
27867
27868   return result;
27869 }
27870
27871 /* Parse an objc-message-receiver.
27872
27873    objc-message-receiver:
27874      expression
27875      simple-type-specifier
27876
27877   Returns a representation of the type or expression.  */
27878
27879 static tree
27880 cp_parser_objc_message_receiver (cp_parser* parser)
27881 {
27882   tree rcv;
27883
27884   /* An Objective-C message receiver may be either (1) a type
27885      or (2) an expression.  */
27886   cp_parser_parse_tentatively (parser);
27887   rcv = cp_parser_expression (parser);
27888
27889   /* If that worked out, fine.  */
27890   if (cp_parser_parse_definitely (parser))
27891     return rcv;
27892
27893   cp_parser_parse_tentatively (parser);
27894   rcv = cp_parser_simple_type_specifier (parser,
27895                                          /*decl_specs=*/NULL,
27896                                          CP_PARSER_FLAGS_NONE);
27897
27898   if (cp_parser_parse_definitely (parser))
27899     return objc_get_class_reference (rcv);
27900   
27901   cp_parser_error (parser, "objective-c++ message receiver expected");
27902   return error_mark_node;
27903 }
27904
27905 /* Parse the arguments and selectors comprising an Objective-C message.
27906
27907    objc-message-args:
27908      objc-selector
27909      objc-selector-args
27910      objc-selector-args , objc-comma-args
27911
27912    objc-selector-args:
27913      objc-selector [opt] : assignment-expression
27914      objc-selector-args objc-selector [opt] : assignment-expression
27915
27916    objc-comma-args:
27917      assignment-expression
27918      objc-comma-args , assignment-expression
27919
27920    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27921    selector arguments and TREE_VALUE containing a list of comma
27922    arguments.  */
27923
27924 static tree
27925 cp_parser_objc_message_args (cp_parser* parser)
27926 {
27927   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27928   bool maybe_unary_selector_p = true;
27929   cp_token *token = cp_lexer_peek_token (parser->lexer);
27930
27931   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27932     {
27933       tree selector = NULL_TREE, arg;
27934
27935       if (token->type != CPP_COLON)
27936         selector = cp_parser_objc_selector (parser);
27937
27938       /* Detect if we have a unary selector.  */
27939       if (maybe_unary_selector_p
27940           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27941         return build_tree_list (selector, NULL_TREE);
27942
27943       maybe_unary_selector_p = false;
27944       cp_parser_require (parser, CPP_COLON, RT_COLON);
27945       arg = cp_parser_assignment_expression (parser);
27946
27947       sel_args
27948         = chainon (sel_args,
27949                    build_tree_list (selector, arg));
27950
27951       token = cp_lexer_peek_token (parser->lexer);
27952     }
27953
27954   /* Handle non-selector arguments, if any. */
27955   while (token->type == CPP_COMMA)
27956     {
27957       tree arg;
27958
27959       cp_lexer_consume_token (parser->lexer);
27960       arg = cp_parser_assignment_expression (parser);
27961
27962       addl_args
27963         = chainon (addl_args,
27964                    build_tree_list (NULL_TREE, arg));
27965
27966       token = cp_lexer_peek_token (parser->lexer);
27967     }
27968
27969   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27970     {
27971       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27972       return build_tree_list (error_mark_node, error_mark_node);
27973     }
27974
27975   return build_tree_list (sel_args, addl_args);
27976 }
27977
27978 /* Parse an Objective-C encode expression.
27979
27980    objc-encode-expression:
27981      @encode objc-typename
27982
27983    Returns an encoded representation of the type argument.  */
27984
27985 static cp_expr
27986 cp_parser_objc_encode_expression (cp_parser* parser)
27987 {
27988   tree type;
27989   cp_token *token;
27990   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27991
27992   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
27993   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27994   token = cp_lexer_peek_token (parser->lexer);
27995   type = complete_type (cp_parser_type_id (parser));
27996   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27997
27998   if (!type)
27999     {
28000       error_at (token->location, 
28001                 "%<@encode%> must specify a type as an argument");
28002       return error_mark_node;
28003     }
28004
28005   /* This happens if we find @encode(T) (where T is a template
28006      typename or something dependent on a template typename) when
28007      parsing a template.  In that case, we can't compile it
28008      immediately, but we rather create an AT_ENCODE_EXPR which will
28009      need to be instantiated when the template is used.
28010   */
28011   if (dependent_type_p (type))
28012     {
28013       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28014       TREE_READONLY (value) = 1;
28015       return value;
28016     }
28017
28018
28019   /* Build a location of the form:
28020        @encode(int)
28021        ^~~~~~~~~~~~
28022      with caret==start at the @ token, finishing at the close paren.  */
28023   location_t combined_loc
28024     = make_location (start_loc, start_loc,
28025                      cp_lexer_previous_token (parser->lexer)->location);
28026
28027   return cp_expr (objc_build_encode_expr (type), combined_loc);
28028 }
28029
28030 /* Parse an Objective-C @defs expression.  */
28031
28032 static tree
28033 cp_parser_objc_defs_expression (cp_parser *parser)
28034 {
28035   tree name;
28036
28037   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
28038   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28039   name = cp_parser_identifier (parser);
28040   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28041
28042   return objc_get_class_ivars (name);
28043 }
28044
28045 /* Parse an Objective-C protocol expression.
28046
28047   objc-protocol-expression:
28048     @protocol ( identifier )
28049
28050   Returns a representation of the protocol expression.  */
28051
28052 static tree
28053 cp_parser_objc_protocol_expression (cp_parser* parser)
28054 {
28055   tree proto;
28056   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28057
28058   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
28059   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28060   proto = cp_parser_identifier (parser);
28061   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28062
28063   /* Build a location of the form:
28064        @protocol(prot)
28065        ^~~~~~~~~~~~~~~
28066      with caret==start at the @ token, finishing at the close paren.  */
28067   location_t combined_loc
28068     = make_location (start_loc, start_loc,
28069                      cp_lexer_previous_token (parser->lexer)->location);
28070   tree result = objc_build_protocol_expr (proto);
28071   protected_set_expr_location (result, combined_loc);
28072   return result;
28073 }
28074
28075 /* Parse an Objective-C selector expression.
28076
28077    objc-selector-expression:
28078      @selector ( objc-method-signature )
28079
28080    objc-method-signature:
28081      objc-selector
28082      objc-selector-seq
28083
28084    objc-selector-seq:
28085      objc-selector :
28086      objc-selector-seq objc-selector :
28087
28088   Returns a representation of the method selector.  */
28089
28090 static tree
28091 cp_parser_objc_selector_expression (cp_parser* parser)
28092 {
28093   tree sel_seq = NULL_TREE;
28094   bool maybe_unary_selector_p = true;
28095   cp_token *token;
28096   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28097
28098   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
28099   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28100   token = cp_lexer_peek_token (parser->lexer);
28101
28102   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28103          || token->type == CPP_SCOPE)
28104     {
28105       tree selector = NULL_TREE;
28106
28107       if (token->type != CPP_COLON
28108           || token->type == CPP_SCOPE)
28109         selector = cp_parser_objc_selector (parser);
28110
28111       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28112           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28113         {
28114           /* Detect if we have a unary selector.  */
28115           if (maybe_unary_selector_p)
28116             {
28117               sel_seq = selector;
28118               goto finish_selector;
28119             }
28120           else
28121             {
28122               cp_parser_error (parser, "expected %<:%>");
28123             }
28124         }
28125       maybe_unary_selector_p = false;
28126       token = cp_lexer_consume_token (parser->lexer);
28127
28128       if (token->type == CPP_SCOPE)
28129         {
28130           sel_seq
28131             = chainon (sel_seq,
28132                        build_tree_list (selector, NULL_TREE));
28133           sel_seq
28134             = chainon (sel_seq,
28135                        build_tree_list (NULL_TREE, NULL_TREE));
28136         }
28137       else
28138         sel_seq
28139           = chainon (sel_seq,
28140                      build_tree_list (selector, NULL_TREE));
28141
28142       token = cp_lexer_peek_token (parser->lexer);
28143     }
28144
28145  finish_selector:
28146   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28147
28148
28149   /* Build a location of the form:
28150        @selector(func)
28151        ^~~~~~~~~~~~~~~
28152      with caret==start at the @ token, finishing at the close paren.  */
28153   location_t combined_loc
28154     = make_location (loc, loc,
28155                      cp_lexer_previous_token (parser->lexer)->location);
28156   tree result = objc_build_selector_expr (combined_loc, sel_seq);
28157   /* TODO: objc_build_selector_expr doesn't always honor the location.  */
28158   protected_set_expr_location (result, combined_loc);
28159   return result;
28160 }
28161
28162 /* Parse a list of identifiers.
28163
28164    objc-identifier-list:
28165      identifier
28166      objc-identifier-list , identifier
28167
28168    Returns a TREE_LIST of identifier nodes.  */
28169
28170 static tree
28171 cp_parser_objc_identifier_list (cp_parser* parser)
28172 {
28173   tree identifier;
28174   tree list;
28175   cp_token *sep;
28176
28177   identifier = cp_parser_identifier (parser);
28178   if (identifier == error_mark_node)
28179     return error_mark_node;      
28180
28181   list = build_tree_list (NULL_TREE, identifier);
28182   sep = cp_lexer_peek_token (parser->lexer);
28183
28184   while (sep->type == CPP_COMMA)
28185     {
28186       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
28187       identifier = cp_parser_identifier (parser);
28188       if (identifier == error_mark_node)
28189         return list;
28190
28191       list = chainon (list, build_tree_list (NULL_TREE,
28192                                              identifier));
28193       sep = cp_lexer_peek_token (parser->lexer);
28194     }
28195   
28196   return list;
28197 }
28198
28199 /* Parse an Objective-C alias declaration.
28200
28201    objc-alias-declaration:
28202      @compatibility_alias identifier identifier ;
28203
28204    This function registers the alias mapping with the Objective-C front end.
28205    It returns nothing.  */
28206
28207 static void
28208 cp_parser_objc_alias_declaration (cp_parser* parser)
28209 {
28210   tree alias, orig;
28211
28212   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
28213   alias = cp_parser_identifier (parser);
28214   orig = cp_parser_identifier (parser);
28215   objc_declare_alias (alias, orig);
28216   cp_parser_consume_semicolon_at_end_of_statement (parser);
28217 }
28218
28219 /* Parse an Objective-C class forward-declaration.
28220
28221    objc-class-declaration:
28222      @class objc-identifier-list ;
28223
28224    The function registers the forward declarations with the Objective-C
28225    front end.  It returns nothing.  */
28226
28227 static void
28228 cp_parser_objc_class_declaration (cp_parser* parser)
28229 {
28230   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
28231   while (true)
28232     {
28233       tree id;
28234       
28235       id = cp_parser_identifier (parser);
28236       if (id == error_mark_node)
28237         break;
28238       
28239       objc_declare_class (id);
28240
28241       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28242         cp_lexer_consume_token (parser->lexer);
28243       else
28244         break;
28245     }
28246   cp_parser_consume_semicolon_at_end_of_statement (parser);
28247 }
28248
28249 /* Parse a list of Objective-C protocol references.
28250
28251    objc-protocol-refs-opt:
28252      objc-protocol-refs [opt]
28253
28254    objc-protocol-refs:
28255      < objc-identifier-list >
28256
28257    Returns a TREE_LIST of identifiers, if any.  */
28258
28259 static tree
28260 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28261 {
28262   tree protorefs = NULL_TREE;
28263
28264   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28265     {
28266       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
28267       protorefs = cp_parser_objc_identifier_list (parser);
28268       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28269     }
28270
28271   return protorefs;
28272 }
28273
28274 /* Parse a Objective-C visibility specification.  */
28275
28276 static void
28277 cp_parser_objc_visibility_spec (cp_parser* parser)
28278 {
28279   cp_token *vis = cp_lexer_peek_token (parser->lexer);
28280
28281   switch (vis->keyword)
28282     {
28283     case RID_AT_PRIVATE:
28284       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28285       break;
28286     case RID_AT_PROTECTED:
28287       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28288       break;
28289     case RID_AT_PUBLIC:
28290       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28291       break;
28292     case RID_AT_PACKAGE:
28293       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28294       break;
28295     default:
28296       return;
28297     }
28298
28299   /* Eat '@private'/'@protected'/'@public'.  */
28300   cp_lexer_consume_token (parser->lexer);
28301 }
28302
28303 /* Parse an Objective-C method type.  Return 'true' if it is a class
28304    (+) method, and 'false' if it is an instance (-) method.  */
28305
28306 static inline bool
28307 cp_parser_objc_method_type (cp_parser* parser)
28308 {
28309   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28310     return true;
28311   else
28312     return false;
28313 }
28314
28315 /* Parse an Objective-C protocol qualifier.  */
28316
28317 static tree
28318 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28319 {
28320   tree quals = NULL_TREE, node;
28321   cp_token *token = cp_lexer_peek_token (parser->lexer);
28322
28323   node = token->u.value;
28324
28325   while (node && identifier_p (node)
28326          && (node == ridpointers [(int) RID_IN]
28327              || node == ridpointers [(int) RID_OUT]
28328              || node == ridpointers [(int) RID_INOUT]
28329              || node == ridpointers [(int) RID_BYCOPY]
28330              || node == ridpointers [(int) RID_BYREF]
28331              || node == ridpointers [(int) RID_ONEWAY]))
28332     {
28333       quals = tree_cons (NULL_TREE, node, quals);
28334       cp_lexer_consume_token (parser->lexer);
28335       token = cp_lexer_peek_token (parser->lexer);
28336       node = token->u.value;
28337     }
28338
28339   return quals;
28340 }
28341
28342 /* Parse an Objective-C typename.  */
28343
28344 static tree
28345 cp_parser_objc_typename (cp_parser* parser)
28346 {
28347   tree type_name = NULL_TREE;
28348
28349   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28350     {
28351       tree proto_quals, cp_type = NULL_TREE;
28352
28353       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
28354       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28355
28356       /* An ObjC type name may consist of just protocol qualifiers, in which
28357          case the type shall default to 'id'.  */
28358       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28359         {
28360           cp_type = cp_parser_type_id (parser);
28361           
28362           /* If the type could not be parsed, an error has already
28363              been produced.  For error recovery, behave as if it had
28364              not been specified, which will use the default type
28365              'id'.  */
28366           if (cp_type == error_mark_node)
28367             {
28368               cp_type = NULL_TREE;
28369               /* We need to skip to the closing parenthesis as
28370                  cp_parser_type_id() does not seem to do it for
28371                  us.  */
28372               cp_parser_skip_to_closing_parenthesis (parser,
28373                                                      /*recovering=*/true,
28374                                                      /*or_comma=*/false,
28375                                                      /*consume_paren=*/false);
28376             }
28377         }
28378
28379       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28380       type_name = build_tree_list (proto_quals, cp_type);
28381     }
28382
28383   return type_name;
28384 }
28385
28386 /* Check to see if TYPE refers to an Objective-C selector name.  */
28387
28388 static bool
28389 cp_parser_objc_selector_p (enum cpp_ttype type)
28390 {
28391   return (type == CPP_NAME || type == CPP_KEYWORD
28392           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28393           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28394           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28395           || type == CPP_XOR || type == CPP_XOR_EQ);
28396 }
28397
28398 /* Parse an Objective-C selector.  */
28399
28400 static tree
28401 cp_parser_objc_selector (cp_parser* parser)
28402 {
28403   cp_token *token = cp_lexer_consume_token (parser->lexer);
28404
28405   if (!cp_parser_objc_selector_p (token->type))
28406     {
28407       error_at (token->location, "invalid Objective-C++ selector name");
28408       return error_mark_node;
28409     }
28410
28411   /* C++ operator names are allowed to appear in ObjC selectors.  */
28412   switch (token->type)
28413     {
28414     case CPP_AND_AND: return get_identifier ("and");
28415     case CPP_AND_EQ: return get_identifier ("and_eq");
28416     case CPP_AND: return get_identifier ("bitand");
28417     case CPP_OR: return get_identifier ("bitor");
28418     case CPP_COMPL: return get_identifier ("compl");
28419     case CPP_NOT: return get_identifier ("not");
28420     case CPP_NOT_EQ: return get_identifier ("not_eq");
28421     case CPP_OR_OR: return get_identifier ("or");
28422     case CPP_OR_EQ: return get_identifier ("or_eq");
28423     case CPP_XOR: return get_identifier ("xor");
28424     case CPP_XOR_EQ: return get_identifier ("xor_eq");
28425     default: return token->u.value;
28426     }
28427 }
28428
28429 /* Parse an Objective-C params list.  */
28430
28431 static tree
28432 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28433 {
28434   tree params = NULL_TREE;
28435   bool maybe_unary_selector_p = true;
28436   cp_token *token = cp_lexer_peek_token (parser->lexer);
28437
28438   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28439     {
28440       tree selector = NULL_TREE, type_name, identifier;
28441       tree parm_attr = NULL_TREE;
28442
28443       if (token->keyword == RID_ATTRIBUTE)
28444         break;
28445
28446       if (token->type != CPP_COLON)
28447         selector = cp_parser_objc_selector (parser);
28448
28449       /* Detect if we have a unary selector.  */
28450       if (maybe_unary_selector_p
28451           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28452         {
28453           params = selector; /* Might be followed by attributes.  */
28454           break;
28455         }
28456
28457       maybe_unary_selector_p = false;
28458       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28459         {
28460           /* Something went quite wrong.  There should be a colon
28461              here, but there is not.  Stop parsing parameters.  */
28462           break;
28463         }
28464       type_name = cp_parser_objc_typename (parser);
28465       /* New ObjC allows attributes on parameters too.  */
28466       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28467         parm_attr = cp_parser_attributes_opt (parser);
28468       identifier = cp_parser_identifier (parser);
28469
28470       params
28471         = chainon (params,
28472                    objc_build_keyword_decl (selector,
28473                                             type_name,
28474                                             identifier,
28475                                             parm_attr));
28476
28477       token = cp_lexer_peek_token (parser->lexer);
28478     }
28479
28480   if (params == NULL_TREE)
28481     {
28482       cp_parser_error (parser, "objective-c++ method declaration is expected");
28483       return error_mark_node;
28484     }
28485
28486   /* We allow tail attributes for the method.  */
28487   if (token->keyword == RID_ATTRIBUTE)
28488     {
28489       *attributes = cp_parser_attributes_opt (parser);
28490       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28491           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28492         return params;
28493       cp_parser_error (parser, 
28494                        "method attributes must be specified at the end");
28495       return error_mark_node;
28496     }
28497
28498   if (params == NULL_TREE)
28499     {
28500       cp_parser_error (parser, "objective-c++ method declaration is expected");
28501       return error_mark_node;
28502     }
28503   return params;
28504 }
28505
28506 /* Parse the non-keyword Objective-C params.  */
28507
28508 static tree
28509 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
28510                                        tree* attributes)
28511 {
28512   tree params = make_node (TREE_LIST);
28513   cp_token *token = cp_lexer_peek_token (parser->lexer);
28514   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
28515
28516   while (token->type == CPP_COMMA)
28517     {
28518       cp_parameter_declarator *parmdecl;
28519       tree parm;
28520
28521       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
28522       token = cp_lexer_peek_token (parser->lexer);
28523
28524       if (token->type == CPP_ELLIPSIS)
28525         {
28526           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
28527           *ellipsisp = true;
28528           token = cp_lexer_peek_token (parser->lexer);
28529           break;
28530         }
28531
28532       /* TODO: parse attributes for tail parameters.  */
28533       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28534       parm = grokdeclarator (parmdecl->declarator,
28535                              &parmdecl->decl_specifiers,
28536                              PARM, /*initialized=*/0,
28537                              /*attrlist=*/NULL);
28538
28539       chainon (params, build_tree_list (NULL_TREE, parm));
28540       token = cp_lexer_peek_token (parser->lexer);
28541     }
28542
28543   /* We allow tail attributes for the method.  */
28544   if (token->keyword == RID_ATTRIBUTE)
28545     {
28546       if (*attributes == NULL_TREE)
28547         {
28548           *attributes = cp_parser_attributes_opt (parser);
28549           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28550               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28551             return params;
28552         }
28553       else        
28554         /* We have an error, but parse the attributes, so that we can 
28555            carry on.  */
28556         *attributes = cp_parser_attributes_opt (parser);
28557
28558       cp_parser_error (parser, 
28559                        "method attributes must be specified at the end");
28560       return error_mark_node;
28561     }
28562
28563   return params;
28564 }
28565
28566 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
28567
28568 static void
28569 cp_parser_objc_interstitial_code (cp_parser* parser)
28570 {
28571   cp_token *token = cp_lexer_peek_token (parser->lexer);
28572
28573   /* If the next token is `extern' and the following token is a string
28574      literal, then we have a linkage specification.  */
28575   if (token->keyword == RID_EXTERN
28576       && cp_parser_is_pure_string_literal
28577          (cp_lexer_peek_nth_token (parser->lexer, 2)))
28578     cp_parser_linkage_specification (parser);
28579   /* Handle #pragma, if any.  */
28580   else if (token->type == CPP_PRAGMA)
28581     cp_parser_pragma (parser, pragma_objc_icode, NULL);
28582   /* Allow stray semicolons.  */
28583   else if (token->type == CPP_SEMICOLON)
28584     cp_lexer_consume_token (parser->lexer);
28585   /* Mark methods as optional or required, when building protocols.  */
28586   else if (token->keyword == RID_AT_OPTIONAL)
28587     {
28588       cp_lexer_consume_token (parser->lexer);
28589       objc_set_method_opt (true);
28590     }
28591   else if (token->keyword == RID_AT_REQUIRED)
28592     {
28593       cp_lexer_consume_token (parser->lexer);
28594       objc_set_method_opt (false);
28595     }
28596   else if (token->keyword == RID_NAMESPACE)
28597     cp_parser_namespace_definition (parser);
28598   /* Other stray characters must generate errors.  */
28599   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28600     {
28601       cp_lexer_consume_token (parser->lexer);
28602       error ("stray %qs between Objective-C++ methods",
28603              token->type == CPP_OPEN_BRACE ? "{" : "}");
28604     }
28605   /* Finally, try to parse a block-declaration, or a function-definition.  */
28606   else
28607     cp_parser_block_declaration (parser, /*statement_p=*/false);
28608 }
28609
28610 /* Parse a method signature.  */
28611
28612 static tree
28613 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28614 {
28615   tree rettype, kwdparms, optparms;
28616   bool ellipsis = false;
28617   bool is_class_method;
28618
28619   is_class_method = cp_parser_objc_method_type (parser);
28620   rettype = cp_parser_objc_typename (parser);
28621   *attributes = NULL_TREE;
28622   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28623   if (kwdparms == error_mark_node)
28624     return error_mark_node;
28625   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28626   if (optparms == error_mark_node)
28627     return error_mark_node;
28628
28629   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28630 }
28631
28632 static bool
28633 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28634 {
28635   tree tattr;  
28636   cp_lexer_save_tokens (parser->lexer);
28637   tattr = cp_parser_attributes_opt (parser);
28638   gcc_assert (tattr) ;
28639   
28640   /* If the attributes are followed by a method introducer, this is not allowed.
28641      Dump the attributes and flag the situation.  */
28642   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28643       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28644     return true;
28645
28646   /* Otherwise, the attributes introduce some interstitial code, possibly so
28647      rewind to allow that check.  */
28648   cp_lexer_rollback_tokens (parser->lexer);
28649   return false;  
28650 }
28651
28652 /* Parse an Objective-C method prototype list.  */
28653
28654 static void
28655 cp_parser_objc_method_prototype_list (cp_parser* parser)
28656 {
28657   cp_token *token = cp_lexer_peek_token (parser->lexer);
28658
28659   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28660     {
28661       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28662         {
28663           tree attributes, sig;
28664           bool is_class_method;
28665           if (token->type == CPP_PLUS)
28666             is_class_method = true;
28667           else
28668             is_class_method = false;
28669           sig = cp_parser_objc_method_signature (parser, &attributes);
28670           if (sig == error_mark_node)
28671             {
28672               cp_parser_skip_to_end_of_block_or_statement (parser);
28673               token = cp_lexer_peek_token (parser->lexer);
28674               continue;
28675             }
28676           objc_add_method_declaration (is_class_method, sig, attributes);
28677           cp_parser_consume_semicolon_at_end_of_statement (parser);
28678         }
28679       else if (token->keyword == RID_AT_PROPERTY)
28680         cp_parser_objc_at_property_declaration (parser);
28681       else if (token->keyword == RID_ATTRIBUTE 
28682                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28683         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
28684                     OPT_Wattributes, 
28685                     "prefix attributes are ignored for methods");
28686       else
28687         /* Allow for interspersed non-ObjC++ code.  */
28688         cp_parser_objc_interstitial_code (parser);
28689
28690       token = cp_lexer_peek_token (parser->lexer);
28691     }
28692
28693   if (token->type != CPP_EOF)
28694     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
28695   else
28696     cp_parser_error (parser, "expected %<@end%>");
28697
28698   objc_finish_interface ();
28699 }
28700
28701 /* Parse an Objective-C method definition list.  */
28702
28703 static void
28704 cp_parser_objc_method_definition_list (cp_parser* parser)
28705 {
28706   cp_token *token = cp_lexer_peek_token (parser->lexer);
28707
28708   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28709     {
28710       tree meth;
28711
28712       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28713         {
28714           cp_token *ptk;
28715           tree sig, attribute;
28716           bool is_class_method;
28717           if (token->type == CPP_PLUS)
28718             is_class_method = true;
28719           else
28720             is_class_method = false;
28721           push_deferring_access_checks (dk_deferred);
28722           sig = cp_parser_objc_method_signature (parser, &attribute);
28723           if (sig == error_mark_node)
28724             {
28725               cp_parser_skip_to_end_of_block_or_statement (parser);
28726               token = cp_lexer_peek_token (parser->lexer);
28727               continue;
28728             }
28729           objc_start_method_definition (is_class_method, sig, attribute,
28730                                         NULL_TREE);
28731
28732           /* For historical reasons, we accept an optional semicolon.  */
28733           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28734             cp_lexer_consume_token (parser->lexer);
28735
28736           ptk = cp_lexer_peek_token (parser->lexer);
28737           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
28738                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28739             {
28740               perform_deferred_access_checks (tf_warning_or_error);
28741               stop_deferring_access_checks ();
28742               meth = cp_parser_function_definition_after_declarator (parser,
28743                                                                      false);
28744               pop_deferring_access_checks ();
28745               objc_finish_method_definition (meth);
28746             }
28747         }
28748       /* The following case will be removed once @synthesize is
28749          completely implemented.  */
28750       else if (token->keyword == RID_AT_PROPERTY)
28751         cp_parser_objc_at_property_declaration (parser);
28752       else if (token->keyword == RID_AT_SYNTHESIZE)
28753         cp_parser_objc_at_synthesize_declaration (parser);
28754       else if (token->keyword == RID_AT_DYNAMIC)
28755         cp_parser_objc_at_dynamic_declaration (parser);
28756       else if (token->keyword == RID_ATTRIBUTE 
28757                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28758         warning_at (token->location, OPT_Wattributes,
28759                     "prefix attributes are ignored for methods");
28760       else
28761         /* Allow for interspersed non-ObjC++ code.  */
28762         cp_parser_objc_interstitial_code (parser);
28763
28764       token = cp_lexer_peek_token (parser->lexer);
28765     }
28766
28767   if (token->type != CPP_EOF)
28768     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
28769   else
28770     cp_parser_error (parser, "expected %<@end%>");
28771
28772   objc_finish_implementation ();
28773 }
28774
28775 /* Parse Objective-C ivars.  */
28776
28777 static void
28778 cp_parser_objc_class_ivars (cp_parser* parser)
28779 {
28780   cp_token *token = cp_lexer_peek_token (parser->lexer);
28781
28782   if (token->type != CPP_OPEN_BRACE)
28783     return;     /* No ivars specified.  */
28784
28785   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
28786   token = cp_lexer_peek_token (parser->lexer);
28787
28788   while (token->type != CPP_CLOSE_BRACE 
28789         && token->keyword != RID_AT_END && token->type != CPP_EOF)
28790     {
28791       cp_decl_specifier_seq declspecs;
28792       int decl_class_or_enum_p;
28793       tree prefix_attributes;
28794
28795       cp_parser_objc_visibility_spec (parser);
28796
28797       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28798         break;
28799
28800       cp_parser_decl_specifier_seq (parser,
28801                                     CP_PARSER_FLAGS_OPTIONAL,
28802                                     &declspecs,
28803                                     &decl_class_or_enum_p);
28804
28805       /* auto, register, static, extern, mutable.  */
28806       if (declspecs.storage_class != sc_none)
28807         {
28808           cp_parser_error (parser, "invalid type for instance variable");         
28809           declspecs.storage_class = sc_none;
28810         }
28811
28812       /* thread_local.  */
28813       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28814         {
28815           cp_parser_error (parser, "invalid type for instance variable");
28816           declspecs.locations[ds_thread] = 0;
28817         }
28818       
28819       /* typedef.  */
28820       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28821         {
28822           cp_parser_error (parser, "invalid type for instance variable");
28823           declspecs.locations[ds_typedef] = 0;
28824         }
28825
28826       prefix_attributes = declspecs.attributes;
28827       declspecs.attributes = NULL_TREE;
28828
28829       /* Keep going until we hit the `;' at the end of the
28830          declaration.  */
28831       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28832         {
28833           tree width = NULL_TREE, attributes, first_attribute, decl;
28834           cp_declarator *declarator = NULL;
28835           int ctor_dtor_or_conv_p;
28836
28837           /* Check for a (possibly unnamed) bitfield declaration.  */
28838           token = cp_lexer_peek_token (parser->lexer);
28839           if (token->type == CPP_COLON)
28840             goto eat_colon;
28841
28842           if (token->type == CPP_NAME
28843               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28844                   == CPP_COLON))
28845             {
28846               /* Get the name of the bitfield.  */
28847               declarator = make_id_declarator (NULL_TREE,
28848                                                cp_parser_identifier (parser),
28849                                                sfk_none);
28850
28851              eat_colon:
28852               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
28853               /* Get the width of the bitfield.  */
28854               width
28855                 = cp_parser_constant_expression (parser);
28856             }
28857           else
28858             {
28859               /* Parse the declarator.  */
28860               declarator
28861                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28862                                         &ctor_dtor_or_conv_p,
28863                                         /*parenthesized_p=*/NULL,
28864                                         /*member_p=*/false,
28865                                         /*friend_p=*/false);
28866             }
28867
28868           /* Look for attributes that apply to the ivar.  */
28869           attributes = cp_parser_attributes_opt (parser);
28870           /* Remember which attributes are prefix attributes and
28871              which are not.  */
28872           first_attribute = attributes;
28873           /* Combine the attributes.  */
28874           attributes = chainon (prefix_attributes, attributes);
28875
28876           if (width)
28877               /* Create the bitfield declaration.  */
28878               decl = grokbitfield (declarator, &declspecs,
28879                                    width,
28880                                    attributes);
28881           else
28882             decl = grokfield (declarator, &declspecs,
28883                               NULL_TREE, /*init_const_expr_p=*/false,
28884                               NULL_TREE, attributes);
28885
28886           /* Add the instance variable.  */
28887           if (decl != error_mark_node && decl != NULL_TREE)
28888             objc_add_instance_variable (decl);
28889
28890           /* Reset PREFIX_ATTRIBUTES.  */
28891           while (attributes && TREE_CHAIN (attributes) != first_attribute)
28892             attributes = TREE_CHAIN (attributes);
28893           if (attributes)
28894             TREE_CHAIN (attributes) = NULL_TREE;
28895
28896           token = cp_lexer_peek_token (parser->lexer);
28897
28898           if (token->type == CPP_COMMA)
28899             {
28900               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
28901               continue;
28902             }
28903           break;
28904         }
28905
28906       cp_parser_consume_semicolon_at_end_of_statement (parser);
28907       token = cp_lexer_peek_token (parser->lexer);
28908     }
28909
28910   if (token->keyword == RID_AT_END)
28911     cp_parser_error (parser, "expected %<}%>");
28912
28913   /* Do not consume the RID_AT_END, so it will be read again as terminating
28914      the @interface of @implementation.  */ 
28915   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28916     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
28917     
28918   /* For historical reasons, we accept an optional semicolon.  */
28919   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28920     cp_lexer_consume_token (parser->lexer);
28921 }
28922
28923 /* Parse an Objective-C protocol declaration.  */
28924
28925 static void
28926 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28927 {
28928   tree proto, protorefs;
28929   cp_token *tok;
28930
28931   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
28932   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28933     {
28934       tok = cp_lexer_peek_token (parser->lexer);
28935       error_at (tok->location, "identifier expected after %<@protocol%>");
28936       cp_parser_consume_semicolon_at_end_of_statement (parser);
28937       return;
28938     }
28939
28940   /* See if we have a forward declaration or a definition.  */
28941   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28942
28943   /* Try a forward declaration first.  */
28944   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28945     {
28946       while (true)
28947         {
28948           tree id;
28949           
28950           id = cp_parser_identifier (parser);
28951           if (id == error_mark_node)
28952             break;
28953           
28954           objc_declare_protocol (id, attributes);
28955           
28956           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28957             cp_lexer_consume_token (parser->lexer);
28958           else
28959             break;
28960         }
28961       cp_parser_consume_semicolon_at_end_of_statement (parser);
28962     }
28963
28964   /* Ok, we got a full-fledged definition (or at least should).  */
28965   else
28966     {
28967       proto = cp_parser_identifier (parser);
28968       protorefs = cp_parser_objc_protocol_refs_opt (parser);
28969       objc_start_protocol (proto, protorefs, attributes);
28970       cp_parser_objc_method_prototype_list (parser);
28971     }
28972 }
28973
28974 /* Parse an Objective-C superclass or category.  */
28975
28976 static void
28977 cp_parser_objc_superclass_or_category (cp_parser *parser, 
28978                                        bool iface_p,
28979                                        tree *super,
28980                                        tree *categ, bool *is_class_extension)
28981 {
28982   cp_token *next = cp_lexer_peek_token (parser->lexer);
28983
28984   *super = *categ = NULL_TREE;
28985   *is_class_extension = false;
28986   if (next->type == CPP_COLON)
28987     {
28988       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
28989       *super = cp_parser_identifier (parser);
28990     }
28991   else if (next->type == CPP_OPEN_PAREN)
28992     {
28993       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
28994
28995       /* If there is no category name, and this is an @interface, we
28996          have a class extension.  */
28997       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28998         {
28999           *categ = NULL_TREE;
29000           *is_class_extension = true;
29001         }
29002       else
29003         *categ = cp_parser_identifier (parser);
29004
29005       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29006     }
29007 }
29008
29009 /* Parse an Objective-C class interface.  */
29010
29011 static void
29012 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29013 {
29014   tree name, super, categ, protos;
29015   bool is_class_extension;
29016
29017   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
29018   name = cp_parser_identifier (parser);
29019   if (name == error_mark_node)
29020     {
29021       /* It's hard to recover because even if valid @interface stuff
29022          is to follow, we can't compile it (or validate it) if we
29023          don't even know which class it refers to.  Let's assume this
29024          was a stray '@interface' token in the stream and skip it.
29025       */
29026       return;
29027     }
29028   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29029                                          &is_class_extension);
29030   protos = cp_parser_objc_protocol_refs_opt (parser);
29031
29032   /* We have either a class or a category on our hands.  */
29033   if (categ || is_class_extension)
29034     objc_start_category_interface (name, categ, protos, attributes);
29035   else
29036     {
29037       objc_start_class_interface (name, super, protos, attributes);
29038       /* Handle instance variable declarations, if any.  */
29039       cp_parser_objc_class_ivars (parser);
29040       objc_continue_interface ();
29041     }
29042
29043   cp_parser_objc_method_prototype_list (parser);
29044 }
29045
29046 /* Parse an Objective-C class implementation.  */
29047
29048 static void
29049 cp_parser_objc_class_implementation (cp_parser* parser)
29050 {
29051   tree name, super, categ;
29052   bool is_class_extension;
29053
29054   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
29055   name = cp_parser_identifier (parser);
29056   if (name == error_mark_node)
29057     {
29058       /* It's hard to recover because even if valid @implementation
29059          stuff is to follow, we can't compile it (or validate it) if
29060          we don't even know which class it refers to.  Let's assume
29061          this was a stray '@implementation' token in the stream and
29062          skip it.
29063       */
29064       return;
29065     }
29066   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29067                                          &is_class_extension);
29068
29069   /* We have either a class or a category on our hands.  */
29070   if (categ)
29071     objc_start_category_implementation (name, categ);
29072   else
29073     {
29074       objc_start_class_implementation (name, super);
29075       /* Handle instance variable declarations, if any.  */
29076       cp_parser_objc_class_ivars (parser);
29077       objc_continue_implementation ();
29078     }
29079
29080   cp_parser_objc_method_definition_list (parser);
29081 }
29082
29083 /* Consume the @end token and finish off the implementation.  */
29084
29085 static void
29086 cp_parser_objc_end_implementation (cp_parser* parser)
29087 {
29088   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
29089   objc_finish_implementation ();
29090 }
29091
29092 /* Parse an Objective-C declaration.  */
29093
29094 static void
29095 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29096 {
29097   /* Try to figure out what kind of declaration is present.  */
29098   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29099
29100   if (attributes)
29101     switch (kwd->keyword)
29102       {
29103         case RID_AT_ALIAS:
29104         case RID_AT_CLASS:
29105         case RID_AT_END:
29106           error_at (kwd->location, "attributes may not be specified before"
29107                     " the %<@%D%> Objective-C++ keyword",
29108                     kwd->u.value);
29109           attributes = NULL;
29110           break;
29111         case RID_AT_IMPLEMENTATION:
29112           warning_at (kwd->location, OPT_Wattributes,
29113                       "prefix attributes are ignored before %<@%D%>",
29114                       kwd->u.value);
29115           attributes = NULL;
29116         default:
29117           break;
29118       }
29119
29120   switch (kwd->keyword)
29121     {
29122     case RID_AT_ALIAS:
29123       cp_parser_objc_alias_declaration (parser);
29124       break;
29125     case RID_AT_CLASS:
29126       cp_parser_objc_class_declaration (parser);
29127       break;
29128     case RID_AT_PROTOCOL:
29129       cp_parser_objc_protocol_declaration (parser, attributes);
29130       break;
29131     case RID_AT_INTERFACE:
29132       cp_parser_objc_class_interface (parser, attributes);
29133       break;
29134     case RID_AT_IMPLEMENTATION:
29135       cp_parser_objc_class_implementation (parser);
29136       break;
29137     case RID_AT_END:
29138       cp_parser_objc_end_implementation (parser);
29139       break;
29140     default:
29141       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29142                 kwd->u.value);
29143       cp_parser_skip_to_end_of_block_or_statement (parser);
29144     }
29145 }
29146
29147 /* Parse an Objective-C try-catch-finally statement.
29148
29149    objc-try-catch-finally-stmt:
29150      @try compound-statement objc-catch-clause-seq [opt]
29151        objc-finally-clause [opt]
29152
29153    objc-catch-clause-seq:
29154      objc-catch-clause objc-catch-clause-seq [opt]
29155
29156    objc-catch-clause:
29157      @catch ( objc-exception-declaration ) compound-statement
29158
29159    objc-finally-clause:
29160      @finally compound-statement
29161
29162    objc-exception-declaration:
29163      parameter-declaration
29164      '...'
29165
29166    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29167
29168    Returns NULL_TREE.
29169
29170    PS: This function is identical to c_parser_objc_try_catch_finally_statement
29171    for C.  Keep them in sync.  */   
29172
29173 static tree
29174 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29175 {
29176   location_t location;
29177   tree stmt;
29178
29179   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29180   location = cp_lexer_peek_token (parser->lexer)->location;
29181   objc_maybe_warn_exceptions (location);
29182   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29183      node, lest it get absorbed into the surrounding block.  */
29184   stmt = push_stmt_list ();
29185   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29186   objc_begin_try_stmt (location, pop_stmt_list (stmt));
29187
29188   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29189     {
29190       cp_parameter_declarator *parm;
29191       tree parameter_declaration = error_mark_node;
29192       bool seen_open_paren = false;
29193
29194       cp_lexer_consume_token (parser->lexer);
29195       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29196         seen_open_paren = true;
29197       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29198         {
29199           /* We have "@catch (...)" (where the '...' are literally
29200              what is in the code).  Skip the '...'.
29201              parameter_declaration is set to NULL_TREE, and
29202              objc_being_catch_clauses() knows that that means
29203              '...'.  */
29204           cp_lexer_consume_token (parser->lexer);
29205           parameter_declaration = NULL_TREE;
29206         }
29207       else
29208         {
29209           /* We have "@catch (NSException *exception)" or something
29210              like that.  Parse the parameter declaration.  */
29211           parm = cp_parser_parameter_declaration (parser, false, NULL);
29212           if (parm == NULL)
29213             parameter_declaration = error_mark_node;
29214           else
29215             parameter_declaration = grokdeclarator (parm->declarator,
29216                                                     &parm->decl_specifiers,
29217                                                     PARM, /*initialized=*/0,
29218                                                     /*attrlist=*/NULL);
29219         }
29220       if (seen_open_paren)
29221         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29222       else
29223         {
29224           /* If there was no open parenthesis, we are recovering from
29225              an error, and we are trying to figure out what mistake
29226              the user has made.  */
29227
29228           /* If there is an immediate closing parenthesis, the user
29229              probably forgot the opening one (ie, they typed "@catch
29230              NSException *e)".  Parse the closing parenthesis and keep
29231              going.  */
29232           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29233             cp_lexer_consume_token (parser->lexer);
29234           
29235           /* If these is no immediate closing parenthesis, the user
29236              probably doesn't know that parenthesis are required at
29237              all (ie, they typed "@catch NSException *e").  So, just
29238              forget about the closing parenthesis and keep going.  */
29239         }
29240       objc_begin_catch_clause (parameter_declaration);
29241       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29242       objc_finish_catch_clause ();
29243     }
29244   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29245     {
29246       cp_lexer_consume_token (parser->lexer);
29247       location = cp_lexer_peek_token (parser->lexer)->location;
29248       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29249          node, lest it get absorbed into the surrounding block.  */
29250       stmt = push_stmt_list ();
29251       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29252       objc_build_finally_clause (location, pop_stmt_list (stmt));
29253     }
29254
29255   return objc_finish_try_stmt ();
29256 }
29257
29258 /* Parse an Objective-C synchronized statement.
29259
29260    objc-synchronized-stmt:
29261      @synchronized ( expression ) compound-statement
29262
29263    Returns NULL_TREE.  */
29264
29265 static tree
29266 cp_parser_objc_synchronized_statement (cp_parser *parser)
29267 {
29268   location_t location;
29269   tree lock, stmt;
29270
29271   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29272
29273   location = cp_lexer_peek_token (parser->lexer)->location;
29274   objc_maybe_warn_exceptions (location);
29275   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29276   lock = cp_parser_expression (parser);
29277   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29278
29279   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29280      node, lest it get absorbed into the surrounding block.  */
29281   stmt = push_stmt_list ();
29282   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29283
29284   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29285 }
29286
29287 /* Parse an Objective-C throw statement.
29288
29289    objc-throw-stmt:
29290      @throw assignment-expression [opt] ;
29291
29292    Returns a constructed '@throw' statement.  */
29293
29294 static tree
29295 cp_parser_objc_throw_statement (cp_parser *parser)
29296 {
29297   tree expr = NULL_TREE;
29298   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29299
29300   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29301
29302   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29303     expr = cp_parser_expression (parser);
29304
29305   cp_parser_consume_semicolon_at_end_of_statement (parser);
29306
29307   return objc_build_throw_stmt (loc, expr);
29308 }
29309
29310 /* Parse an Objective-C statement.  */
29311
29312 static tree
29313 cp_parser_objc_statement (cp_parser * parser)
29314 {
29315   /* Try to figure out what kind of declaration is present.  */
29316   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29317
29318   switch (kwd->keyword)
29319     {
29320     case RID_AT_TRY:
29321       return cp_parser_objc_try_catch_finally_statement (parser);
29322     case RID_AT_SYNCHRONIZED:
29323       return cp_parser_objc_synchronized_statement (parser);
29324     case RID_AT_THROW:
29325       return cp_parser_objc_throw_statement (parser);
29326     default:
29327       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29328                kwd->u.value);
29329       cp_parser_skip_to_end_of_block_or_statement (parser);
29330     }
29331
29332   return error_mark_node;
29333 }
29334
29335 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
29336    look ahead to see if an objc keyword follows the attributes.  This
29337    is to detect the use of prefix attributes on ObjC @interface and 
29338    @protocol.  */
29339
29340 static bool
29341 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29342 {
29343   cp_lexer_save_tokens (parser->lexer);
29344   *attrib = cp_parser_attributes_opt (parser);
29345   gcc_assert (*attrib);
29346   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29347     {
29348       cp_lexer_commit_tokens (parser->lexer);
29349       return true;
29350     }
29351   cp_lexer_rollback_tokens (parser->lexer);
29352   return false;  
29353 }
29354
29355 /* This routine is a minimal replacement for
29356    c_parser_struct_declaration () used when parsing the list of
29357    types/names or ObjC++ properties.  For example, when parsing the
29358    code
29359
29360    @property (readonly) int a, b, c;
29361
29362    this function is responsible for parsing "int a, int b, int c" and
29363    returning the declarations as CHAIN of DECLs.
29364
29365    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
29366    similar parsing.  */
29367 static tree
29368 cp_parser_objc_struct_declaration (cp_parser *parser)
29369 {
29370   tree decls = NULL_TREE;
29371   cp_decl_specifier_seq declspecs;
29372   int decl_class_or_enum_p;
29373   tree prefix_attributes;
29374
29375   cp_parser_decl_specifier_seq (parser,
29376                                 CP_PARSER_FLAGS_NONE,
29377                                 &declspecs,
29378                                 &decl_class_or_enum_p);
29379
29380   if (declspecs.type == error_mark_node)
29381     return error_mark_node;
29382
29383   /* auto, register, static, extern, mutable.  */
29384   if (declspecs.storage_class != sc_none)
29385     {
29386       cp_parser_error (parser, "invalid type for property");
29387       declspecs.storage_class = sc_none;
29388     }
29389   
29390   /* thread_local.  */
29391   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29392     {
29393       cp_parser_error (parser, "invalid type for property");
29394       declspecs.locations[ds_thread] = 0;
29395     }
29396   
29397   /* typedef.  */
29398   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29399     {
29400       cp_parser_error (parser, "invalid type for property");
29401       declspecs.locations[ds_typedef] = 0;
29402     }
29403
29404   prefix_attributes = declspecs.attributes;
29405   declspecs.attributes = NULL_TREE;
29406
29407   /* Keep going until we hit the `;' at the end of the declaration. */
29408   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29409     {
29410       tree attributes, first_attribute, decl;
29411       cp_declarator *declarator;
29412       cp_token *token;
29413
29414       /* Parse the declarator.  */
29415       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29416                                          NULL, NULL, false, false);
29417
29418       /* Look for attributes that apply to the ivar.  */
29419       attributes = cp_parser_attributes_opt (parser);
29420       /* Remember which attributes are prefix attributes and
29421          which are not.  */
29422       first_attribute = attributes;
29423       /* Combine the attributes.  */
29424       attributes = chainon (prefix_attributes, attributes);
29425       
29426       decl = grokfield (declarator, &declspecs,
29427                         NULL_TREE, /*init_const_expr_p=*/false,
29428                         NULL_TREE, attributes);
29429
29430       if (decl == error_mark_node || decl == NULL_TREE)
29431         return error_mark_node;
29432       
29433       /* Reset PREFIX_ATTRIBUTES.  */
29434       while (attributes && TREE_CHAIN (attributes) != first_attribute)
29435         attributes = TREE_CHAIN (attributes);
29436       if (attributes)
29437         TREE_CHAIN (attributes) = NULL_TREE;
29438
29439       DECL_CHAIN (decl) = decls;
29440       decls = decl;
29441
29442       token = cp_lexer_peek_token (parser->lexer);
29443       if (token->type == CPP_COMMA)
29444         {
29445           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
29446           continue;
29447         }
29448       else
29449         break;
29450     }
29451   return decls;
29452 }
29453
29454 /* Parse an Objective-C @property declaration.  The syntax is:
29455
29456    objc-property-declaration:
29457      '@property' objc-property-attributes[opt] struct-declaration ;
29458
29459    objc-property-attributes:
29460     '(' objc-property-attribute-list ')'
29461
29462    objc-property-attribute-list:
29463      objc-property-attribute
29464      objc-property-attribute-list, objc-property-attribute
29465
29466    objc-property-attribute
29467      'getter' = identifier
29468      'setter' = identifier
29469      'readonly'
29470      'readwrite'
29471      'assign'
29472      'retain'
29473      'copy'
29474      'nonatomic'
29475
29476   For example:
29477     @property NSString *name;
29478     @property (readonly) id object;
29479     @property (retain, nonatomic, getter=getTheName) id name;
29480     @property int a, b, c;
29481
29482    PS: This function is identical to
29483    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
29484 static void 
29485 cp_parser_objc_at_property_declaration (cp_parser *parser)
29486 {
29487   /* The following variables hold the attributes of the properties as
29488      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
29489      seen.  When we see an attribute, we set them to 'true' (if they
29490      are boolean properties) or to the identifier (if they have an
29491      argument, ie, for getter and setter).  Note that here we only
29492      parse the list of attributes, check the syntax and accumulate the
29493      attributes that we find.  objc_add_property_declaration() will
29494      then process the information.  */
29495   bool property_assign = false;
29496   bool property_copy = false;
29497   tree property_getter_ident = NULL_TREE;
29498   bool property_nonatomic = false;
29499   bool property_readonly = false;
29500   bool property_readwrite = false;
29501   bool property_retain = false;
29502   tree property_setter_ident = NULL_TREE;
29503
29504   /* 'properties' is the list of properties that we read.  Usually a
29505      single one, but maybe more (eg, in "@property int a, b, c;" there
29506      are three).  */
29507   tree properties;
29508   location_t loc;
29509
29510   loc = cp_lexer_peek_token (parser->lexer)->location;
29511
29512   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
29513
29514   /* Parse the optional attribute list...  */
29515   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29516     {
29517       /* Eat the '('.  */
29518       cp_lexer_consume_token (parser->lexer);
29519
29520       while (true)
29521         {
29522           bool syntax_error = false;
29523           cp_token *token = cp_lexer_peek_token (parser->lexer);
29524           enum rid keyword;
29525
29526           if (token->type != CPP_NAME)
29527             {
29528               cp_parser_error (parser, "expected identifier");
29529               break;
29530             }
29531           keyword = C_RID_CODE (token->u.value);
29532           cp_lexer_consume_token (parser->lexer);
29533           switch (keyword)
29534             {
29535             case RID_ASSIGN:    property_assign = true;    break;
29536             case RID_COPY:      property_copy = true;      break;
29537             case RID_NONATOMIC: property_nonatomic = true; break;
29538             case RID_READONLY:  property_readonly = true;  break;
29539             case RID_READWRITE: property_readwrite = true; break;
29540             case RID_RETAIN:    property_retain = true;    break;
29541
29542             case RID_GETTER:
29543             case RID_SETTER:
29544               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29545                 {
29546                   if (keyword == RID_GETTER)
29547                     cp_parser_error (parser,
29548                                      "missing %<=%> (after %<getter%> attribute)");
29549                   else
29550                     cp_parser_error (parser,
29551                                      "missing %<=%> (after %<setter%> attribute)");
29552                   syntax_error = true;
29553                   break;
29554                 }
29555               cp_lexer_consume_token (parser->lexer); /* eat the = */
29556               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29557                 {
29558                   cp_parser_error (parser, "expected identifier");
29559                   syntax_error = true;
29560                   break;
29561                 }
29562               if (keyword == RID_SETTER)
29563                 {
29564                   if (property_setter_ident != NULL_TREE)
29565                     {
29566                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29567                       cp_lexer_consume_token (parser->lexer);
29568                     }
29569                   else
29570                     property_setter_ident = cp_parser_objc_selector (parser);
29571                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29572                     cp_parser_error (parser, "setter name must terminate with %<:%>");
29573                   else
29574                     cp_lexer_consume_token (parser->lexer);
29575                 }
29576               else
29577                 {
29578                   if (property_getter_ident != NULL_TREE)
29579                     {
29580                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29581                       cp_lexer_consume_token (parser->lexer);
29582                     }
29583                   else
29584                     property_getter_ident = cp_parser_objc_selector (parser);
29585                 }
29586               break;
29587             default:
29588               cp_parser_error (parser, "unknown property attribute");
29589               syntax_error = true;
29590               break;
29591             }
29592
29593           if (syntax_error)
29594             break;
29595
29596           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29597             cp_lexer_consume_token (parser->lexer);
29598           else
29599             break;
29600         }
29601
29602       /* FIXME: "@property (setter, assign);" will generate a spurious
29603          "error: expected â€˜)’ before â€˜,’ token".  This is because
29604          cp_parser_require, unlike the C counterpart, will produce an
29605          error even if we are in error recovery.  */
29606       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29607         {
29608           cp_parser_skip_to_closing_parenthesis (parser,
29609                                                  /*recovering=*/true,
29610                                                  /*or_comma=*/false,
29611                                                  /*consume_paren=*/true);
29612         }
29613     }
29614
29615   /* ... and the property declaration(s).  */
29616   properties = cp_parser_objc_struct_declaration (parser);
29617
29618   if (properties == error_mark_node)
29619     {
29620       cp_parser_skip_to_end_of_statement (parser);
29621       /* If the next token is now a `;', consume it.  */
29622       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29623         cp_lexer_consume_token (parser->lexer);
29624       return;
29625     }
29626
29627   if (properties == NULL_TREE)
29628     cp_parser_error (parser, "expected identifier");
29629   else
29630     {
29631       /* Comma-separated properties are chained together in
29632          reverse order; add them one by one.  */
29633       properties = nreverse (properties);
29634       
29635       for (; properties; properties = TREE_CHAIN (properties))
29636         objc_add_property_declaration (loc, copy_node (properties),
29637                                        property_readonly, property_readwrite,
29638                                        property_assign, property_retain,
29639                                        property_copy, property_nonatomic,
29640                                        property_getter_ident, property_setter_ident);
29641     }
29642   
29643   cp_parser_consume_semicolon_at_end_of_statement (parser);
29644 }
29645
29646 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
29647
29648    objc-synthesize-declaration:
29649      @synthesize objc-synthesize-identifier-list ;
29650
29651    objc-synthesize-identifier-list:
29652      objc-synthesize-identifier
29653      objc-synthesize-identifier-list, objc-synthesize-identifier
29654
29655    objc-synthesize-identifier
29656      identifier
29657      identifier = identifier
29658
29659   For example:
29660     @synthesize MyProperty;
29661     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29662
29663   PS: This function is identical to c_parser_objc_at_synthesize_declaration
29664   for C.  Keep them in sync.
29665 */
29666 static void 
29667 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29668 {
29669   tree list = NULL_TREE;
29670   location_t loc;
29671   loc = cp_lexer_peek_token (parser->lexer)->location;
29672
29673   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
29674   while (true)
29675     {
29676       tree property, ivar;
29677       property = cp_parser_identifier (parser);
29678       if (property == error_mark_node)
29679         {
29680           cp_parser_consume_semicolon_at_end_of_statement (parser);
29681           return;
29682         }
29683       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29684         {
29685           cp_lexer_consume_token (parser->lexer);
29686           ivar = cp_parser_identifier (parser);
29687           if (ivar == error_mark_node)
29688             {
29689               cp_parser_consume_semicolon_at_end_of_statement (parser);
29690               return;
29691             }
29692         }
29693       else
29694         ivar = NULL_TREE;
29695       list = chainon (list, build_tree_list (ivar, property));
29696       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29697         cp_lexer_consume_token (parser->lexer);
29698       else
29699         break;
29700     }
29701   cp_parser_consume_semicolon_at_end_of_statement (parser);
29702   objc_add_synthesize_declaration (loc, list);
29703 }
29704
29705 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
29706
29707    objc-dynamic-declaration:
29708      @dynamic identifier-list ;
29709
29710    For example:
29711      @dynamic MyProperty;
29712      @dynamic MyProperty, AnotherProperty;
29713
29714   PS: This function is identical to c_parser_objc_at_dynamic_declaration
29715   for C.  Keep them in sync.
29716 */
29717 static void 
29718 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29719 {
29720   tree list = NULL_TREE;
29721   location_t loc;
29722   loc = cp_lexer_peek_token (parser->lexer)->location;
29723
29724   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
29725   while (true)
29726     {
29727       tree property;
29728       property = cp_parser_identifier (parser);
29729       if (property == error_mark_node)
29730         {
29731           cp_parser_consume_semicolon_at_end_of_statement (parser);
29732           return;
29733         }
29734       list = chainon (list, build_tree_list (NULL, property));
29735       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29736         cp_lexer_consume_token (parser->lexer);
29737       else
29738         break;
29739     }
29740   cp_parser_consume_semicolon_at_end_of_statement (parser);
29741   objc_add_dynamic_declaration (loc, list);
29742 }
29743
29744 \f
29745 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
29746
29747 /* Returns name of the next clause.
29748    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29749    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
29750    returned and the token is consumed.  */
29751
29752 static pragma_omp_clause
29753 cp_parser_omp_clause_name (cp_parser *parser)
29754 {
29755   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29756
29757   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29758     result = PRAGMA_OACC_CLAUSE_AUTO;
29759   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29760     result = PRAGMA_OMP_CLAUSE_IF;
29761   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29762     result = PRAGMA_OMP_CLAUSE_DEFAULT;
29763   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29764     result = PRAGMA_OACC_CLAUSE_DELETE;
29765   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29766     result = PRAGMA_OMP_CLAUSE_PRIVATE;
29767   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29768     result = PRAGMA_OMP_CLAUSE_FOR;
29769   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29770     {
29771       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29772       const char *p = IDENTIFIER_POINTER (id);
29773
29774       switch (p[0])
29775         {
29776         case 'a':
29777           if (!strcmp ("aligned", p))
29778             result = PRAGMA_OMP_CLAUSE_ALIGNED;
29779           else if (!strcmp ("async", p))
29780             result = PRAGMA_OACC_CLAUSE_ASYNC;
29781           break;
29782         case 'c':
29783           if (!strcmp ("collapse", p))
29784             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29785           else if (!strcmp ("copy", p))
29786             result = PRAGMA_OACC_CLAUSE_COPY;
29787           else if (!strcmp ("copyin", p))
29788             result = PRAGMA_OMP_CLAUSE_COPYIN;
29789           else if (!strcmp ("copyout", p))
29790             result = PRAGMA_OACC_CLAUSE_COPYOUT;
29791           else if (!strcmp ("copyprivate", p))
29792             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29793           else if (!strcmp ("create", p))
29794             result = PRAGMA_OACC_CLAUSE_CREATE;
29795           break;
29796         case 'd':
29797           if (!strcmp ("defaultmap", p))
29798             result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29799           else if (!strcmp ("depend", p))
29800             result = PRAGMA_OMP_CLAUSE_DEPEND;
29801           else if (!strcmp ("device", p))
29802             result = PRAGMA_OMP_CLAUSE_DEVICE;
29803           else if (!strcmp ("deviceptr", p))
29804             result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29805           else if (!strcmp ("device_resident", p))
29806             result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29807           else if (!strcmp ("dist_schedule", p))
29808             result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29809           break;
29810         case 'f':
29811           if (!strcmp ("final", p))
29812             result = PRAGMA_OMP_CLAUSE_FINAL;
29813           else if (!strcmp ("firstprivate", p))
29814             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29815           else if (!strcmp ("from", p))
29816             result = PRAGMA_OMP_CLAUSE_FROM;
29817           break;
29818         case 'g':
29819           if (!strcmp ("gang", p))
29820             result = PRAGMA_OACC_CLAUSE_GANG;
29821           else if (!strcmp ("grainsize", p))
29822             result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29823           break;
29824         case 'h':
29825           if (!strcmp ("hint", p))
29826             result = PRAGMA_OMP_CLAUSE_HINT;
29827           else if (!strcmp ("host", p))
29828             result = PRAGMA_OACC_CLAUSE_HOST;
29829           break;
29830         case 'i':
29831           if (!strcmp ("inbranch", p))
29832             result = PRAGMA_OMP_CLAUSE_INBRANCH;
29833           else if (!strcmp ("independent", p))
29834             result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29835           else if (!strcmp ("is_device_ptr", p))
29836             result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29837           break;
29838         case 'l':
29839           if (!strcmp ("lastprivate", p))
29840             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29841           else if (!strcmp ("linear", p))
29842             result = PRAGMA_OMP_CLAUSE_LINEAR;
29843           else if (!strcmp ("link", p))
29844             result = PRAGMA_OMP_CLAUSE_LINK;
29845           break;
29846         case 'm':
29847           if (!strcmp ("map", p))
29848             result = PRAGMA_OMP_CLAUSE_MAP;
29849           else if (!strcmp ("mergeable", p))
29850             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29851           else if (flag_cilkplus && !strcmp ("mask", p))
29852             result = PRAGMA_CILK_CLAUSE_MASK;
29853           break;
29854         case 'n':
29855           if (!strcmp ("nogroup", p))
29856             result = PRAGMA_OMP_CLAUSE_NOGROUP;
29857           else if (!strcmp ("notinbranch", p))
29858             result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29859           else if (!strcmp ("nowait", p))
29860             result = PRAGMA_OMP_CLAUSE_NOWAIT;
29861           else if (flag_cilkplus && !strcmp ("nomask", p))
29862             result = PRAGMA_CILK_CLAUSE_NOMASK;
29863           else if (!strcmp ("num_gangs", p))
29864             result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29865           else if (!strcmp ("num_tasks", p))
29866             result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29867           else if (!strcmp ("num_teams", p))
29868             result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29869           else if (!strcmp ("num_threads", p))
29870             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29871           else if (!strcmp ("num_workers", p))
29872             result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29873           break;
29874         case 'o':
29875           if (!strcmp ("ordered", p))
29876             result = PRAGMA_OMP_CLAUSE_ORDERED;
29877           break;
29878         case 'p':
29879           if (!strcmp ("parallel", p))
29880             result = PRAGMA_OMP_CLAUSE_PARALLEL;
29881           else if (!strcmp ("present", p))
29882             result = PRAGMA_OACC_CLAUSE_PRESENT;
29883           else if (!strcmp ("present_or_copy", p)
29884                    || !strcmp ("pcopy", p))
29885             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29886           else if (!strcmp ("present_or_copyin", p)
29887                    || !strcmp ("pcopyin", p))
29888             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29889           else if (!strcmp ("present_or_copyout", p)
29890                    || !strcmp ("pcopyout", p))
29891             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29892           else if (!strcmp ("present_or_create", p)
29893                    || !strcmp ("pcreate", p))
29894             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29895           else if (!strcmp ("priority", p))
29896             result = PRAGMA_OMP_CLAUSE_PRIORITY;
29897           else if (!strcmp ("proc_bind", p))
29898             result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29899           break;
29900         case 'r':
29901           if (!strcmp ("reduction", p))
29902             result = PRAGMA_OMP_CLAUSE_REDUCTION;
29903           break;
29904         case 's':
29905           if (!strcmp ("safelen", p))
29906             result = PRAGMA_OMP_CLAUSE_SAFELEN;
29907           else if (!strcmp ("schedule", p))
29908             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29909           else if (!strcmp ("sections", p))
29910             result = PRAGMA_OMP_CLAUSE_SECTIONS;
29911           else if (!strcmp ("self", p))
29912             result = PRAGMA_OACC_CLAUSE_SELF;
29913           else if (!strcmp ("seq", p))
29914             result = PRAGMA_OACC_CLAUSE_SEQ;
29915           else if (!strcmp ("shared", p))
29916             result = PRAGMA_OMP_CLAUSE_SHARED;
29917           else if (!strcmp ("simd", p))
29918             result = PRAGMA_OMP_CLAUSE_SIMD;
29919           else if (!strcmp ("simdlen", p))
29920             result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29921           break;
29922         case 't':
29923           if (!strcmp ("taskgroup", p))
29924             result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29925           else if (!strcmp ("thread_limit", p))
29926             result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29927           else if (!strcmp ("threads", p))
29928             result = PRAGMA_OMP_CLAUSE_THREADS;
29929           else if (!strcmp ("tile", p))
29930             result = PRAGMA_OACC_CLAUSE_TILE;
29931           else if (!strcmp ("to", p))
29932             result = PRAGMA_OMP_CLAUSE_TO;
29933           break;
29934         case 'u':
29935           if (!strcmp ("uniform", p))
29936             result = PRAGMA_OMP_CLAUSE_UNIFORM;
29937           else if (!strcmp ("untied", p))
29938             result = PRAGMA_OMP_CLAUSE_UNTIED;
29939           else if (!strcmp ("use_device", p))
29940             result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29941           else if (!strcmp ("use_device_ptr", p))
29942             result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29943           break;
29944         case 'v':
29945           if (!strcmp ("vector", p))
29946             result = PRAGMA_OACC_CLAUSE_VECTOR;
29947           else if (!strcmp ("vector_length", p))
29948             result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29949           else if (flag_cilkplus && !strcmp ("vectorlength", p))
29950             result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29951           break;
29952         case 'w':
29953           if (!strcmp ("wait", p))
29954             result = PRAGMA_OACC_CLAUSE_WAIT;
29955           else if (!strcmp ("worker", p))
29956             result = PRAGMA_OACC_CLAUSE_WORKER;
29957           break;
29958         }
29959     }
29960
29961   if (result != PRAGMA_OMP_CLAUSE_NONE)
29962     cp_lexer_consume_token (parser->lexer);
29963
29964   return result;
29965 }
29966
29967 /* Validate that a clause of the given type does not already exist.  */
29968
29969 static void
29970 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29971                            const char *name, location_t location)
29972 {
29973   tree c;
29974
29975   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29976     if (OMP_CLAUSE_CODE (c) == code)
29977       {
29978         error_at (location, "too many %qs clauses", name);
29979         break;
29980       }
29981 }
29982
29983 /* OpenMP 2.5:
29984    variable-list:
29985      identifier
29986      variable-list , identifier
29987
29988    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29989    colon).  An opening parenthesis will have been consumed by the caller.
29990
29991    If KIND is nonzero, create the appropriate node and install the decl
29992    in OMP_CLAUSE_DECL and add the node to the head of the list.
29993
29994    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29995    return the list created.
29996
29997    COLON can be NULL if only closing parenthesis should end the list,
29998    or pointer to bool which will receive false if the list is terminated
29999    by closing parenthesis or true if the list is terminated by colon.  */
30000
30001 static tree
30002 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30003                                 tree list, bool *colon)
30004 {
30005   cp_token *token;
30006   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30007   if (colon)
30008     {
30009       parser->colon_corrects_to_scope_p = false;
30010       *colon = false;
30011     }
30012   while (1)
30013     {
30014       tree name, decl;
30015
30016       token = cp_lexer_peek_token (parser->lexer);
30017       if (kind != 0
30018           && current_class_ptr
30019           && cp_parser_is_keyword (token, RID_THIS))
30020         {
30021           decl = finish_this_expr ();
30022           if (TREE_CODE (decl) == NON_LVALUE_EXPR
30023               || CONVERT_EXPR_P (decl))
30024             decl = TREE_OPERAND (decl, 0);
30025           cp_lexer_consume_token (parser->lexer);
30026         }
30027       else
30028         {
30029           name = cp_parser_id_expression (parser, /*template_p=*/false,
30030                                           /*check_dependency_p=*/true,
30031                                           /*template_p=*/NULL,
30032                                           /*declarator_p=*/false,
30033                                           /*optional_p=*/false);
30034           if (name == error_mark_node)
30035             goto skip_comma;
30036
30037           decl = cp_parser_lookup_name_simple (parser, name, token->location);
30038           if (decl == error_mark_node)
30039             cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30040                                          token->location);
30041         }
30042       if (decl == error_mark_node)
30043         ;
30044       else if (kind != 0)
30045         {
30046           switch (kind)
30047             {
30048             case OMP_CLAUSE__CACHE_:
30049               /* The OpenACC cache directive explicitly only allows "array
30050                  elements or subarrays".  */
30051               if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30052                 {
30053                   error_at (token->location, "expected %<[%>");
30054                   decl = error_mark_node;
30055                   break;
30056                 }
30057               /* FALLTHROUGH.  */
30058             case OMP_CLAUSE_MAP:
30059             case OMP_CLAUSE_FROM:
30060             case OMP_CLAUSE_TO:
30061               while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30062                 {
30063                   location_t loc
30064                     = cp_lexer_peek_token (parser->lexer)->location;
30065                   cp_id_kind idk = CP_ID_KIND_NONE;
30066                   cp_lexer_consume_token (parser->lexer);
30067                   decl = convert_from_reference (decl);
30068                   decl
30069                     = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30070                                                               decl, false,
30071                                                               &idk, loc);
30072                 }
30073               /* FALLTHROUGH.  */
30074             case OMP_CLAUSE_DEPEND:
30075             case OMP_CLAUSE_REDUCTION:
30076               while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30077                 {
30078                   tree low_bound = NULL_TREE, length = NULL_TREE;
30079
30080                   parser->colon_corrects_to_scope_p = false;
30081                   cp_lexer_consume_token (parser->lexer);
30082                   if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30083                     low_bound = cp_parser_expression (parser);
30084                   if (!colon)
30085                     parser->colon_corrects_to_scope_p
30086                       = saved_colon_corrects_to_scope_p;
30087                   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30088                     length = integer_one_node;
30089                   else
30090                     {
30091                       /* Look for `:'.  */
30092                       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30093                         goto skip_comma;
30094                       if (!cp_lexer_next_token_is (parser->lexer,
30095                                                    CPP_CLOSE_SQUARE))
30096                         length = cp_parser_expression (parser);
30097                     }
30098                   /* Look for the closing `]'.  */
30099                   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30100                                           RT_CLOSE_SQUARE))
30101                     goto skip_comma;
30102
30103                   decl = tree_cons (low_bound, length, decl);
30104                 }
30105               break;
30106             default:
30107               break;
30108             }
30109
30110           tree u = build_omp_clause (token->location, kind);
30111           OMP_CLAUSE_DECL (u) = decl;
30112           OMP_CLAUSE_CHAIN (u) = list;
30113           list = u;
30114         }
30115       else
30116         list = tree_cons (decl, NULL_TREE, list);
30117
30118     get_comma:
30119       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30120         break;
30121       cp_lexer_consume_token (parser->lexer);
30122     }
30123
30124   if (colon)
30125     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30126
30127   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30128     {
30129       *colon = true;
30130       cp_parser_require (parser, CPP_COLON, RT_COLON);
30131       return list;
30132     }
30133
30134   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30135     {
30136       int ending;
30137
30138       /* Try to resync to an unnested comma.  Copied from
30139          cp_parser_parenthesized_expression_list.  */
30140     skip_comma:
30141       if (colon)
30142         parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30143       ending = cp_parser_skip_to_closing_parenthesis (parser,
30144                                                       /*recovering=*/true,
30145                                                       /*or_comma=*/true,
30146                                                       /*consume_paren=*/true);
30147       if (ending < 0)
30148         goto get_comma;
30149     }
30150
30151   return list;
30152 }
30153
30154 /* Similarly, but expect leading and trailing parenthesis.  This is a very
30155    common case for omp clauses.  */
30156
30157 static tree
30158 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30159 {
30160   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30161     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30162   return list;
30163 }
30164
30165 /* OpenACC 2.0:
30166    copy ( variable-list )
30167    copyin ( variable-list )
30168    copyout ( variable-list )
30169    create ( variable-list )
30170    delete ( variable-list )
30171    present ( variable-list )
30172    present_or_copy ( variable-list )
30173      pcopy ( variable-list )
30174    present_or_copyin ( variable-list )
30175      pcopyin ( variable-list )
30176    present_or_copyout ( variable-list )
30177      pcopyout ( variable-list )
30178    present_or_create ( variable-list )
30179      pcreate ( variable-list ) */
30180
30181 static tree
30182 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30183                             tree list)
30184 {
30185   enum gomp_map_kind kind;
30186   switch (c_kind)
30187     {
30188     case PRAGMA_OACC_CLAUSE_COPY:
30189       kind = GOMP_MAP_FORCE_TOFROM;
30190       break;
30191     case PRAGMA_OACC_CLAUSE_COPYIN:
30192       kind = GOMP_MAP_FORCE_TO;
30193       break;
30194     case PRAGMA_OACC_CLAUSE_COPYOUT:
30195       kind = GOMP_MAP_FORCE_FROM;
30196       break;
30197     case PRAGMA_OACC_CLAUSE_CREATE:
30198       kind = GOMP_MAP_FORCE_ALLOC;
30199       break;
30200     case PRAGMA_OACC_CLAUSE_DELETE:
30201       kind = GOMP_MAP_DELETE;
30202       break;
30203     case PRAGMA_OACC_CLAUSE_DEVICE:
30204       kind = GOMP_MAP_FORCE_TO;
30205       break;
30206     case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30207       kind = GOMP_MAP_DEVICE_RESIDENT;
30208       break;
30209     case PRAGMA_OACC_CLAUSE_HOST:
30210     case PRAGMA_OACC_CLAUSE_SELF:
30211       kind = GOMP_MAP_FORCE_FROM;
30212       break;
30213     case PRAGMA_OACC_CLAUSE_LINK:
30214       kind = GOMP_MAP_LINK;
30215       break;
30216     case PRAGMA_OACC_CLAUSE_PRESENT:
30217       kind = GOMP_MAP_FORCE_PRESENT;
30218       break;
30219     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30220       kind = GOMP_MAP_TOFROM;
30221       break;
30222     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30223       kind = GOMP_MAP_TO;
30224       break;
30225     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30226       kind = GOMP_MAP_FROM;
30227       break;
30228     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30229       kind = GOMP_MAP_ALLOC;
30230       break;
30231     default:
30232       gcc_unreachable ();
30233     }
30234   tree nl, c;
30235   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30236
30237   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30238     OMP_CLAUSE_SET_MAP_KIND (c, kind);
30239
30240   return nl;
30241 }
30242
30243 /* OpenACC 2.0:
30244    deviceptr ( variable-list ) */
30245
30246 static tree
30247 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30248 {
30249   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30250   tree vars, t;
30251
30252   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30253      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30254      variable-list must only allow for pointer variables.  */
30255   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30256   for (t = vars; t; t = TREE_CHAIN (t))
30257     {
30258       tree v = TREE_PURPOSE (t);
30259       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30260       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30261       OMP_CLAUSE_DECL (u) = v;
30262       OMP_CLAUSE_CHAIN (u) = list;
30263       list = u;
30264     }
30265
30266   return list;
30267 }
30268
30269 /* OpenACC 2.0:
30270    auto
30271    independent
30272    nohost
30273    seq */
30274
30275 static tree
30276 cp_parser_oacc_simple_clause (cp_parser * /* parser  */,
30277                               enum omp_clause_code code,
30278                               tree list, location_t location)
30279 {
30280   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30281   tree c = build_omp_clause (location, code);
30282   OMP_CLAUSE_CHAIN (c) = list;
30283   return c;
30284 }
30285
30286  /* OpenACC:
30287    num_gangs ( expression )
30288    num_workers ( expression )
30289    vector_length ( expression )  */
30290
30291 static tree
30292 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30293                                   const char *str, tree list)
30294 {
30295   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30296
30297   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30298     return list;
30299
30300   tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30301
30302   if (t == error_mark_node
30303       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30304     {
30305       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30306                                              /*or_comma=*/false,
30307                                              /*consume_paren=*/true);
30308       return list;
30309     }
30310
30311   check_no_duplicate_clause (list, code, str, loc);
30312
30313   tree c = build_omp_clause (loc, code);
30314   OMP_CLAUSE_OPERAND (c, 0) = t;
30315   OMP_CLAUSE_CHAIN (c) = list;
30316   return c;
30317 }
30318
30319 /* OpenACC:
30320
30321     gang [( gang-arg-list )]
30322     worker [( [num:] int-expr )]
30323     vector [( [length:] int-expr )]
30324
30325   where gang-arg is one of:
30326
30327     [num:] int-expr
30328     static: size-expr
30329
30330   and size-expr may be:
30331
30332     *
30333     int-expr
30334 */
30335
30336 static tree
30337 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30338                              const char *str, tree list)
30339 {
30340   const char *id = "num";
30341   cp_lexer *lexer = parser->lexer;
30342   tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30343   location_t loc = cp_lexer_peek_token (lexer)->location;
30344
30345   if (kind == OMP_CLAUSE_VECTOR)
30346     id = "length";
30347
30348   if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30349     {
30350       cp_lexer_consume_token (lexer);
30351
30352       do
30353         {
30354           cp_token *next = cp_lexer_peek_token (lexer);
30355           int idx = 0;
30356
30357           /* Gang static argument.  */
30358           if (kind == OMP_CLAUSE_GANG
30359               && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30360             {
30361               cp_lexer_consume_token (lexer);
30362
30363               if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30364                 goto cleanup_error;
30365
30366               idx = 1;
30367               if (ops[idx] != NULL)
30368                 {
30369                   cp_parser_error (parser, "too many %<static%> arguments");
30370                   goto cleanup_error;
30371                 }
30372
30373               /* Check for the '*' argument.  */
30374               if (cp_lexer_next_token_is (lexer, CPP_MULT)
30375                   && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30376                       || cp_lexer_nth_token_is (parser->lexer, 2,
30377                                                 CPP_CLOSE_PAREN)))
30378                 {
30379                   cp_lexer_consume_token (lexer);
30380                   ops[idx] = integer_minus_one_node;
30381
30382                   if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30383                     {
30384                       cp_lexer_consume_token (lexer);
30385                       continue;
30386                     }
30387                   else break;
30388                 }
30389             }
30390           /* Worker num: argument and vector length: arguments.  */
30391           else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30392                    && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30393                    && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30394             {
30395               cp_lexer_consume_token (lexer);  /* id  */
30396               cp_lexer_consume_token (lexer);  /* ':'  */
30397             }
30398
30399           /* Now collect the actual argument.  */
30400           if (ops[idx] != NULL_TREE)
30401             {
30402               cp_parser_error (parser, "unexpected argument");
30403               goto cleanup_error;
30404             }
30405
30406           tree expr = cp_parser_assignment_expression (parser, NULL, false,
30407                                                        false);
30408           if (expr == error_mark_node)
30409             goto cleanup_error;
30410
30411           mark_exp_read (expr);
30412           ops[idx] = expr;
30413
30414           if (kind == OMP_CLAUSE_GANG
30415               && cp_lexer_next_token_is (lexer, CPP_COMMA))
30416             {
30417               cp_lexer_consume_token (lexer);
30418               continue;
30419             }
30420           break;
30421         }
30422       while (1);
30423
30424       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30425         goto cleanup_error;
30426     }
30427
30428   check_no_duplicate_clause (list, kind, str, loc);
30429
30430   c = build_omp_clause (loc, kind);
30431
30432   if (ops[1])
30433     OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30434
30435   OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30436   OMP_CLAUSE_CHAIN (c) = list;
30437
30438   return c;
30439
30440  cleanup_error:
30441   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30442   return list;
30443 }
30444
30445 /* OpenACC 2.0:
30446    tile ( size-expr-list ) */
30447
30448 static tree
30449 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30450 {
30451   tree c, expr = error_mark_node;
30452   tree tile = NULL_TREE;
30453
30454   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30455
30456   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30457     return list;
30458
30459   do
30460     {
30461       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30462           && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30463               || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30464         {
30465           cp_lexer_consume_token (parser->lexer);
30466           expr = integer_minus_one_node;
30467         }
30468       else
30469         expr = cp_parser_assignment_expression (parser, NULL, false, false);
30470
30471       if (expr == error_mark_node)
30472         return list;
30473
30474       tile = tree_cons (NULL_TREE, expr, tile);
30475
30476       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30477         cp_lexer_consume_token (parser->lexer);
30478     }
30479   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30480
30481   /* Consume the trailing ')'.  */
30482   cp_lexer_consume_token (parser->lexer);
30483
30484   c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30485   tile = nreverse (tile);
30486   OMP_CLAUSE_TILE_LIST (c) = tile;
30487   OMP_CLAUSE_CHAIN (c) = list;
30488   return c;
30489 }
30490
30491 /* OpenACC 2.0
30492    Parse wait clause or directive parameters.  */
30493
30494 static tree
30495 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30496 {
30497   vec<tree, va_gc> *args;
30498   tree t, args_tree;
30499
30500   args = cp_parser_parenthesized_expression_list (parser, non_attr,
30501                                                   /*cast_p=*/false,
30502                                                   /*allow_expansion_p=*/true,
30503                                                   /*non_constant_p=*/NULL);
30504
30505   if (args == NULL || args->length () == 0)
30506     {
30507       cp_parser_error (parser, "expected integer expression before ')'");
30508       if (args != NULL)
30509         release_tree_vector (args);
30510       return list;
30511     }
30512
30513   args_tree = build_tree_list_vec (args);
30514
30515   release_tree_vector (args);
30516
30517   for (t = args_tree; t; t = TREE_CHAIN (t))
30518     {
30519       tree targ = TREE_VALUE (t);
30520
30521       if (targ != error_mark_node)
30522         {
30523           if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30524             error ("%<wait%> expression must be integral");
30525           else
30526             {
30527               tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30528
30529               mark_rvalue_use (targ);
30530               OMP_CLAUSE_DECL (c) = targ;
30531               OMP_CLAUSE_CHAIN (c) = list;
30532               list = c;
30533             }
30534         }
30535     }
30536
30537   return list;
30538 }
30539
30540 /* OpenACC:
30541    wait ( int-expr-list ) */
30542
30543 static tree
30544 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30545 {
30546   location_t location = cp_lexer_peek_token (parser->lexer)->location;
30547
30548   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30549     return list;
30550
30551   list = cp_parser_oacc_wait_list (parser, location, list);
30552
30553   return list;
30554 }
30555
30556 /* OpenMP 3.0:
30557    collapse ( constant-expression ) */
30558
30559 static tree
30560 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30561 {
30562   tree c, num;
30563   location_t loc;
30564   HOST_WIDE_INT n;
30565
30566   loc = cp_lexer_peek_token (parser->lexer)->location;
30567   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30568     return list;
30569
30570   num = cp_parser_constant_expression (parser);
30571
30572   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30573     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30574                                            /*or_comma=*/false,
30575                                            /*consume_paren=*/true);
30576
30577   if (num == error_mark_node)
30578     return list;
30579   num = fold_non_dependent_expr (num);
30580   if (!tree_fits_shwi_p (num)
30581       || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30582       || (n = tree_to_shwi (num)) <= 0
30583       || (int) n != n)
30584     {
30585       error_at (loc, "collapse argument needs positive constant integer expression");
30586       return list;
30587     }
30588
30589   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30590   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30591   OMP_CLAUSE_CHAIN (c) = list;
30592   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30593
30594   return c;
30595 }
30596
30597 /* OpenMP 2.5:
30598    default ( shared | none )
30599
30600    OpenACC 2.0
30601    default (none) */
30602
30603 static tree
30604 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30605                               location_t location, bool is_oacc)
30606 {
30607   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30608   tree c;
30609
30610   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30611     return list;
30612   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30613     {
30614       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30615       const char *p = IDENTIFIER_POINTER (id);
30616
30617       switch (p[0])
30618         {
30619         case 'n':
30620           if (strcmp ("none", p) != 0)
30621             goto invalid_kind;
30622           kind = OMP_CLAUSE_DEFAULT_NONE;
30623           break;
30624
30625         case 's':
30626           if (strcmp ("shared", p) != 0 || is_oacc)
30627             goto invalid_kind;
30628           kind = OMP_CLAUSE_DEFAULT_SHARED;
30629           break;
30630
30631         default:
30632           goto invalid_kind;
30633         }
30634
30635       cp_lexer_consume_token (parser->lexer);
30636     }
30637   else
30638     {
30639     invalid_kind:
30640       if (is_oacc)
30641         cp_parser_error (parser, "expected %<none%>");
30642       else
30643         cp_parser_error (parser, "expected %<none%> or %<shared%>");
30644     }
30645
30646   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30647     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30648                                            /*or_comma=*/false,
30649                                            /*consume_paren=*/true);
30650
30651   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30652     return list;
30653
30654   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30655   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30656   OMP_CLAUSE_CHAIN (c) = list;
30657   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30658
30659   return c;
30660 }
30661
30662 /* OpenMP 3.1:
30663    final ( expression ) */
30664
30665 static tree
30666 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30667 {
30668   tree t, c;
30669
30670   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30671     return list;
30672
30673   t = cp_parser_condition (parser);
30674
30675   if (t == error_mark_node
30676       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30677     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30678                                            /*or_comma=*/false,
30679                                            /*consume_paren=*/true);
30680
30681   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30682
30683   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30684   OMP_CLAUSE_FINAL_EXPR (c) = t;
30685   OMP_CLAUSE_CHAIN (c) = list;
30686
30687   return c;
30688 }
30689
30690 /* OpenMP 2.5:
30691    if ( expression )
30692
30693    OpenMP 4.5:
30694    if ( directive-name-modifier : expression )
30695
30696    directive-name-modifier:
30697      parallel | task | taskloop | target data | target | target update
30698      | target enter data | target exit data  */
30699
30700 static tree
30701 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30702                          bool is_omp)
30703 {
30704   tree t, c;
30705   enum tree_code if_modifier = ERROR_MARK;
30706
30707   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30708     return list;
30709
30710   if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30711     {
30712       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30713       const char *p = IDENTIFIER_POINTER (id);
30714       int n = 2;
30715
30716       if (strcmp ("parallel", p) == 0)
30717         if_modifier = OMP_PARALLEL;
30718       else if (strcmp ("task", p) == 0)
30719         if_modifier = OMP_TASK;
30720       else if (strcmp ("taskloop", p) == 0)
30721         if_modifier = OMP_TASKLOOP;
30722       else if (strcmp ("target", p) == 0)
30723         {
30724           if_modifier = OMP_TARGET;
30725           if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30726             {
30727               id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30728               p = IDENTIFIER_POINTER (id);
30729               if (strcmp ("data", p) == 0)
30730                 if_modifier = OMP_TARGET_DATA;
30731               else if (strcmp ("update", p) == 0)
30732                 if_modifier = OMP_TARGET_UPDATE;
30733               else if (strcmp ("enter", p) == 0)
30734                 if_modifier = OMP_TARGET_ENTER_DATA;
30735               else if (strcmp ("exit", p) == 0)
30736                 if_modifier = OMP_TARGET_EXIT_DATA;
30737               if (if_modifier != OMP_TARGET)
30738                 n = 3;
30739               else
30740                 {
30741                   location_t loc
30742                     = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30743                   error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30744                                  "or %<exit%>");
30745                   if_modifier = ERROR_MARK;
30746                 }
30747               if (if_modifier == OMP_TARGET_ENTER_DATA
30748                   || if_modifier == OMP_TARGET_EXIT_DATA)
30749                 {
30750                   if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30751                     {
30752                       id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30753                       p = IDENTIFIER_POINTER (id);
30754                       if (strcmp ("data", p) == 0)
30755                         n = 4;
30756                     }
30757                   if (n != 4)
30758                     {
30759                       location_t loc
30760                         = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30761                       error_at (loc, "expected %<data%>");
30762                       if_modifier = ERROR_MARK;
30763                     }
30764                 }
30765             }
30766         }
30767       if (if_modifier != ERROR_MARK)
30768         {
30769           if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30770             {
30771               while (n-- > 0)
30772                 cp_lexer_consume_token (parser->lexer);
30773             }
30774           else
30775             {
30776               if (n > 2)
30777                 {
30778                   location_t loc
30779                     = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30780                   error_at (loc, "expected %<:%>");
30781                 }
30782               if_modifier = ERROR_MARK;
30783             }
30784         }
30785     }
30786
30787   t = cp_parser_condition (parser);
30788
30789   if (t == error_mark_node
30790       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30791     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30792                                            /*or_comma=*/false,
30793                                            /*consume_paren=*/true);
30794
30795   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30796     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30797       {
30798         if (if_modifier != ERROR_MARK
30799             && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30800           {
30801             const char *p = NULL;
30802             switch (if_modifier)
30803               {
30804               case OMP_PARALLEL: p = "parallel"; break;
30805               case OMP_TASK: p = "task"; break;
30806               case OMP_TASKLOOP: p = "taskloop"; break;
30807               case OMP_TARGET_DATA: p = "target data"; break;
30808               case OMP_TARGET: p = "target"; break;
30809               case OMP_TARGET_UPDATE: p = "target update"; break;
30810               case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30811               case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30812               default: gcc_unreachable ();
30813               }
30814             error_at (location, "too many %<if%> clauses with %qs modifier",
30815                       p);
30816             return list;
30817           }
30818         else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30819           {
30820             if (!is_omp)
30821               error_at (location, "too many %<if%> clauses");
30822             else
30823               error_at (location, "too many %<if%> clauses without modifier");
30824             return list;
30825           }
30826         else if (if_modifier == ERROR_MARK
30827                  || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30828           {
30829             error_at (location, "if any %<if%> clause has modifier, then all "
30830                                 "%<if%> clauses have to use modifier");
30831             return list;
30832           }
30833       }
30834
30835   c = build_omp_clause (location, OMP_CLAUSE_IF);
30836   OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30837   OMP_CLAUSE_IF_EXPR (c) = t;
30838   OMP_CLAUSE_CHAIN (c) = list;
30839
30840   return c;
30841 }
30842
30843 /* OpenMP 3.1:
30844    mergeable */
30845
30846 static tree
30847 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30848                                 tree list, location_t location)
30849 {
30850   tree c;
30851
30852   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30853                              location);
30854
30855   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30856   OMP_CLAUSE_CHAIN (c) = list;
30857   return c;
30858 }
30859
30860 /* OpenMP 2.5:
30861    nowait */
30862
30863 static tree
30864 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30865                              tree list, location_t location)
30866 {
30867   tree c;
30868
30869   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30870
30871   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30872   OMP_CLAUSE_CHAIN (c) = list;
30873   return c;
30874 }
30875
30876 /* OpenMP 2.5:
30877    num_threads ( expression ) */
30878
30879 static tree
30880 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30881                                   location_t location)
30882 {
30883   tree t, c;
30884
30885   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30886     return list;
30887
30888   t = cp_parser_expression (parser);
30889
30890   if (t == error_mark_node
30891       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30892     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30893                                            /*or_comma=*/false,
30894                                            /*consume_paren=*/true);
30895
30896   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30897                              "num_threads", location);
30898
30899   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30900   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30901   OMP_CLAUSE_CHAIN (c) = list;
30902
30903   return c;
30904 }
30905
30906 /* OpenMP 4.5:
30907    num_tasks ( expression ) */
30908
30909 static tree
30910 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30911                                 location_t location)
30912 {
30913   tree t, c;
30914
30915   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30916     return list;
30917
30918   t = cp_parser_expression (parser);
30919
30920   if (t == error_mark_node
30921       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30922     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30923                                            /*or_comma=*/false,
30924                                            /*consume_paren=*/true);
30925
30926   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30927                              "num_tasks", location);
30928
30929   c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30930   OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30931   OMP_CLAUSE_CHAIN (c) = list;
30932
30933   return c;
30934 }
30935
30936 /* OpenMP 4.5:
30937    grainsize ( expression ) */
30938
30939 static tree
30940 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30941                                 location_t location)
30942 {
30943   tree t, c;
30944
30945   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30946     return list;
30947
30948   t = cp_parser_expression (parser);
30949
30950   if (t == error_mark_node
30951       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30952     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30953                                            /*or_comma=*/false,
30954                                            /*consume_paren=*/true);
30955
30956   check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30957                              "grainsize", location);
30958
30959   c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30960   OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30961   OMP_CLAUSE_CHAIN (c) = list;
30962
30963   return c;
30964 }
30965
30966 /* OpenMP 4.5:
30967    priority ( expression ) */
30968
30969 static tree
30970 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30971                                location_t location)
30972 {
30973   tree t, c;
30974
30975   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30976     return list;
30977
30978   t = cp_parser_expression (parser);
30979
30980   if (t == error_mark_node
30981       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30982     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30983                                            /*or_comma=*/false,
30984                                            /*consume_paren=*/true);
30985
30986   check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30987                              "priority", location);
30988
30989   c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30990   OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30991   OMP_CLAUSE_CHAIN (c) = list;
30992
30993   return c;
30994 }
30995
30996 /* OpenMP 4.5:
30997    hint ( expression ) */
30998
30999 static tree
31000 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31001                            location_t location)
31002 {
31003   tree t, c;
31004
31005   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31006     return list;
31007
31008   t = cp_parser_expression (parser);
31009
31010   if (t == error_mark_node
31011       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31012     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31013                                            /*or_comma=*/false,
31014                                            /*consume_paren=*/true);
31015
31016   check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31017
31018   c = build_omp_clause (location, OMP_CLAUSE_HINT);
31019   OMP_CLAUSE_HINT_EXPR (c) = t;
31020   OMP_CLAUSE_CHAIN (c) = list;
31021
31022   return c;
31023 }
31024
31025 /* OpenMP 4.5:
31026    defaultmap ( tofrom : scalar ) */
31027
31028 static tree
31029 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31030                                  location_t location)
31031 {
31032   tree c, id;
31033   const char *p;
31034
31035   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31036     return list;
31037
31038   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31039     {
31040       cp_parser_error (parser, "expected %<tofrom%>");
31041       goto out_err;
31042     }
31043   id = cp_lexer_peek_token (parser->lexer)->u.value;
31044   p = IDENTIFIER_POINTER (id);
31045   if (strcmp (p, "tofrom") != 0)
31046     {
31047       cp_parser_error (parser, "expected %<tofrom%>");
31048       goto out_err;
31049     }
31050   cp_lexer_consume_token (parser->lexer);
31051   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31052     goto out_err;
31053
31054   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31055     {
31056       cp_parser_error (parser, "expected %<scalar%>");
31057       goto out_err;
31058     }
31059   id = cp_lexer_peek_token (parser->lexer)->u.value;
31060   p = IDENTIFIER_POINTER (id);
31061   if (strcmp (p, "scalar") != 0)
31062     {
31063       cp_parser_error (parser, "expected %<scalar%>");
31064       goto out_err;
31065     }
31066   cp_lexer_consume_token (parser->lexer);
31067   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31068     goto out_err;
31069
31070   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31071                              location);
31072
31073   c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31074   OMP_CLAUSE_CHAIN (c) = list;
31075   return c;
31076
31077  out_err:
31078   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31079                                          /*or_comma=*/false,
31080                                          /*consume_paren=*/true);
31081   return list;
31082 }
31083
31084 /* OpenMP 2.5:
31085    ordered
31086
31087    OpenMP 4.5:
31088    ordered ( constant-expression ) */
31089
31090 static tree
31091 cp_parser_omp_clause_ordered (cp_parser *parser,
31092                               tree list, location_t location)
31093 {
31094   tree c, num = NULL_TREE;
31095   HOST_WIDE_INT n;
31096
31097   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31098                              "ordered", location);
31099
31100   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31101     {
31102       cp_lexer_consume_token (parser->lexer);
31103
31104       num = cp_parser_constant_expression (parser);
31105
31106       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31107         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31108                                                /*or_comma=*/false,
31109                                                /*consume_paren=*/true);
31110
31111       if (num == error_mark_node)
31112         return list;
31113       num = fold_non_dependent_expr (num);
31114       if (!tree_fits_shwi_p (num)
31115           || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31116           || (n = tree_to_shwi (num)) <= 0
31117           || (int) n != n)
31118         {
31119           error_at (location,
31120                     "ordered argument needs positive constant integer "
31121                     "expression");
31122           return list;
31123         }
31124     }
31125
31126   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31127   OMP_CLAUSE_ORDERED_EXPR (c) = num;
31128   OMP_CLAUSE_CHAIN (c) = list;
31129   return c;
31130 }
31131
31132 /* OpenMP 2.5:
31133    reduction ( reduction-operator : variable-list )
31134
31135    reduction-operator:
31136      One of: + * - & ^ | && ||
31137
31138    OpenMP 3.1:
31139
31140    reduction-operator:
31141      One of: + * - & ^ | && || min max
31142
31143    OpenMP 4.0:
31144
31145    reduction-operator:
31146      One of: + * - & ^ | && ||
31147      id-expression  */
31148
31149 static tree
31150 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31151 {
31152   enum tree_code code = ERROR_MARK;
31153   tree nlist, c, id = NULL_TREE;
31154
31155   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31156     return list;
31157
31158   switch (cp_lexer_peek_token (parser->lexer)->type)
31159     {
31160     case CPP_PLUS: code = PLUS_EXPR; break;
31161     case CPP_MULT: code = MULT_EXPR; break;
31162     case CPP_MINUS: code = MINUS_EXPR; break;
31163     case CPP_AND: code = BIT_AND_EXPR; break;
31164     case CPP_XOR: code = BIT_XOR_EXPR; break;
31165     case CPP_OR: code = BIT_IOR_EXPR; break;
31166     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31167     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31168     default: break;
31169     }
31170
31171   if (code != ERROR_MARK)
31172     cp_lexer_consume_token (parser->lexer);
31173   else
31174     {
31175       bool saved_colon_corrects_to_scope_p;
31176       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31177       parser->colon_corrects_to_scope_p = false;
31178       id = cp_parser_id_expression (parser, /*template_p=*/false,
31179                                     /*check_dependency_p=*/true,
31180                                     /*template_p=*/NULL,
31181                                     /*declarator_p=*/false,
31182                                     /*optional_p=*/false);
31183       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31184       if (identifier_p (id))
31185         {
31186           const char *p = IDENTIFIER_POINTER (id);
31187
31188           if (strcmp (p, "min") == 0)
31189             code = MIN_EXPR;
31190           else if (strcmp (p, "max") == 0)
31191             code = MAX_EXPR;
31192           else if (id == ansi_opname (PLUS_EXPR))
31193             code = PLUS_EXPR;
31194           else if (id == ansi_opname (MULT_EXPR))
31195             code = MULT_EXPR;
31196           else if (id == ansi_opname (MINUS_EXPR))
31197             code = MINUS_EXPR;
31198           else if (id == ansi_opname (BIT_AND_EXPR))
31199             code = BIT_AND_EXPR;
31200           else if (id == ansi_opname (BIT_IOR_EXPR))
31201             code = BIT_IOR_EXPR;
31202           else if (id == ansi_opname (BIT_XOR_EXPR))
31203             code = BIT_XOR_EXPR;
31204           else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31205             code = TRUTH_ANDIF_EXPR;
31206           else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31207             code = TRUTH_ORIF_EXPR;
31208           id = omp_reduction_id (code, id, NULL_TREE);
31209           tree scope = parser->scope;
31210           if (scope)
31211             id = build_qualified_name (NULL_TREE, scope, id, false);
31212           parser->scope = NULL_TREE;
31213           parser->qualifying_scope = NULL_TREE;
31214           parser->object_scope = NULL_TREE;
31215         }
31216       else
31217         {
31218           error ("invalid reduction-identifier");
31219          resync_fail:
31220           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31221                                                  /*or_comma=*/false,
31222                                                  /*consume_paren=*/true);
31223           return list;
31224         }
31225     }
31226
31227   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31228     goto resync_fail;
31229
31230   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31231                                           NULL);
31232   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31233     {
31234       OMP_CLAUSE_REDUCTION_CODE (c) = code;
31235       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31236     }
31237
31238   return nlist;
31239 }
31240
31241 /* OpenMP 2.5:
31242    schedule ( schedule-kind )
31243    schedule ( schedule-kind , expression )
31244
31245    schedule-kind:
31246      static | dynamic | guided | runtime | auto
31247
31248    OpenMP 4.5:
31249    schedule ( schedule-modifier : schedule-kind )
31250    schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31251
31252    schedule-modifier:
31253      simd
31254      monotonic
31255      nonmonotonic  */
31256
31257 static tree
31258 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31259 {
31260   tree c, t;
31261   int modifiers = 0, nmodifiers = 0;
31262
31263   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31264     return list;
31265
31266   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31267
31268   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31269     {
31270       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31271       const char *p = IDENTIFIER_POINTER (id);
31272       if (strcmp ("simd", p) == 0)
31273         OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31274       else if (strcmp ("monotonic", p) == 0)
31275         modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31276       else if (strcmp ("nonmonotonic", p) == 0)
31277         modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31278       else
31279         break;
31280       cp_lexer_consume_token (parser->lexer);
31281       if (nmodifiers++ == 0
31282           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31283         cp_lexer_consume_token (parser->lexer);
31284       else
31285         {
31286           cp_parser_require (parser, CPP_COLON, RT_COLON);
31287           break;
31288         }
31289     }
31290
31291   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31292     {
31293       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31294       const char *p = IDENTIFIER_POINTER (id);
31295
31296       switch (p[0])
31297         {
31298         case 'd':
31299           if (strcmp ("dynamic", p) != 0)
31300             goto invalid_kind;
31301           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31302           break;
31303
31304         case 'g':
31305           if (strcmp ("guided", p) != 0)
31306             goto invalid_kind;
31307           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31308           break;
31309
31310         case 'r':
31311           if (strcmp ("runtime", p) != 0)
31312             goto invalid_kind;
31313           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31314           break;
31315
31316         default:
31317           goto invalid_kind;
31318         }
31319     }
31320   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31321     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31322   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31323     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31324   else
31325     goto invalid_kind;
31326   cp_lexer_consume_token (parser->lexer);
31327
31328   if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31329                     | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31330       == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31331           | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31332     {
31333       error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31334                           "specified");
31335       modifiers = 0;
31336     }
31337
31338   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31339     {
31340       cp_token *token;
31341       cp_lexer_consume_token (parser->lexer);
31342
31343       token = cp_lexer_peek_token (parser->lexer);
31344       t = cp_parser_assignment_expression (parser);
31345
31346       if (t == error_mark_node)
31347         goto resync_fail;
31348       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31349         error_at (token->location, "schedule %<runtime%> does not take "
31350                   "a %<chunk_size%> parameter");
31351       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31352         error_at (token->location, "schedule %<auto%> does not take "
31353                   "a %<chunk_size%> parameter");
31354       else
31355         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31356
31357       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31358         goto resync_fail;
31359     }
31360   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31361     goto resync_fail;
31362
31363   OMP_CLAUSE_SCHEDULE_KIND (c)
31364     = (enum omp_clause_schedule_kind)
31365       (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31366
31367   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31368   OMP_CLAUSE_CHAIN (c) = list;
31369   return c;
31370
31371  invalid_kind:
31372   cp_parser_error (parser, "invalid schedule kind");
31373  resync_fail:
31374   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31375                                          /*or_comma=*/false,
31376                                          /*consume_paren=*/true);
31377   return list;
31378 }
31379
31380 /* OpenMP 3.0:
31381    untied */
31382
31383 static tree
31384 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31385                              tree list, location_t location)
31386 {
31387   tree c;
31388
31389   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31390
31391   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31392   OMP_CLAUSE_CHAIN (c) = list;
31393   return c;
31394 }
31395
31396 /* OpenMP 4.0:
31397    inbranch
31398    notinbranch */
31399
31400 static tree
31401 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31402                              tree list, location_t location)
31403 {
31404   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31405   tree c = build_omp_clause (location, code);
31406   OMP_CLAUSE_CHAIN (c) = list;
31407   return c;
31408 }
31409
31410 /* OpenMP 4.0:
31411    parallel
31412    for
31413    sections
31414    taskgroup */
31415
31416 static tree
31417 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31418                                  enum omp_clause_code code,
31419                                  tree list, location_t location)
31420 {
31421   tree c = build_omp_clause (location, code);
31422   OMP_CLAUSE_CHAIN (c) = list;
31423   return c;
31424 }
31425
31426 /* OpenMP 4.5:
31427    nogroup */
31428
31429 static tree
31430 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31431                               tree list, location_t location)
31432 {
31433   check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31434   tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31435   OMP_CLAUSE_CHAIN (c) = list;
31436   return c;
31437 }
31438
31439 /* OpenMP 4.5:
31440    simd
31441    threads */
31442
31443 static tree
31444 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31445                                   enum omp_clause_code code,
31446                                   tree list, location_t location)
31447 {
31448   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31449   tree c = build_omp_clause (location, code);
31450   OMP_CLAUSE_CHAIN (c) = list;
31451   return c;
31452 }
31453
31454 /* OpenMP 4.0:
31455    num_teams ( expression ) */
31456
31457 static tree
31458 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31459                                 location_t location)
31460 {
31461   tree t, c;
31462
31463   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31464     return list;
31465
31466   t = cp_parser_expression (parser);
31467
31468   if (t == error_mark_node
31469       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31470     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31471                                            /*or_comma=*/false,
31472                                            /*consume_paren=*/true);
31473
31474   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31475                              "num_teams", location);
31476
31477   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31478   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31479   OMP_CLAUSE_CHAIN (c) = list;
31480
31481   return c;
31482 }
31483
31484 /* OpenMP 4.0:
31485    thread_limit ( expression ) */
31486
31487 static tree
31488 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31489                                    location_t location)
31490 {
31491   tree t, c;
31492
31493   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31494     return list;
31495
31496   t = cp_parser_expression (parser);
31497
31498   if (t == error_mark_node
31499       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31500     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31501                                            /*or_comma=*/false,
31502                                            /*consume_paren=*/true);
31503
31504   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31505                              "thread_limit", location);
31506
31507   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31508   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31509   OMP_CLAUSE_CHAIN (c) = list;
31510
31511   return c;
31512 }
31513
31514 /* OpenMP 4.0:
31515    aligned ( variable-list )
31516    aligned ( variable-list : constant-expression )  */
31517
31518 static tree
31519 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31520 {
31521   tree nlist, c, alignment = NULL_TREE;
31522   bool colon;
31523
31524   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31525     return list;
31526
31527   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31528                                           &colon);
31529
31530   if (colon)
31531     {
31532       alignment = cp_parser_constant_expression (parser);
31533
31534       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31535         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31536                                                /*or_comma=*/false,
31537                                                /*consume_paren=*/true);
31538
31539       if (alignment == error_mark_node)
31540         alignment = NULL_TREE;
31541     }
31542
31543   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31544     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31545
31546   return nlist;
31547 }
31548
31549 /* OpenMP 4.0:
31550    linear ( variable-list )
31551    linear ( variable-list : expression )
31552
31553    OpenMP 4.5:
31554    linear ( modifier ( variable-list ) )
31555    linear ( modifier ( variable-list ) : expression ) */
31556
31557 static tree
31558 cp_parser_omp_clause_linear (cp_parser *parser, tree list, 
31559                              bool is_cilk_simd_fn, bool declare_simd)
31560 {
31561   tree nlist, c, step = integer_one_node;
31562   bool colon;
31563   enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31564
31565   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31566     return list;
31567
31568   if (!is_cilk_simd_fn
31569       && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31570     {
31571       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31572       const char *p = IDENTIFIER_POINTER (id);
31573
31574       if (strcmp ("ref", p) == 0)
31575         kind = OMP_CLAUSE_LINEAR_REF;
31576       else if (strcmp ("val", p) == 0)
31577         kind = OMP_CLAUSE_LINEAR_VAL;
31578       else if (strcmp ("uval", p) == 0)
31579         kind = OMP_CLAUSE_LINEAR_UVAL;
31580       if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31581         cp_lexer_consume_token (parser->lexer);
31582       else
31583         kind = OMP_CLAUSE_LINEAR_DEFAULT;
31584     }
31585
31586   if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31587     nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31588                                             &colon);
31589   else
31590     {
31591       nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31592       colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31593       if (colon)
31594         cp_parser_require (parser, CPP_COLON, RT_COLON);
31595       else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31596         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31597                                                /*or_comma=*/false,
31598                                                /*consume_paren=*/true);
31599     }
31600
31601   if (colon)
31602     {
31603       step = NULL_TREE;
31604       if (declare_simd
31605           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31606           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31607         {
31608           cp_token *token = cp_lexer_peek_token (parser->lexer);
31609           cp_parser_parse_tentatively (parser);
31610           step = cp_parser_id_expression (parser, /*template_p=*/false,
31611                                           /*check_dependency_p=*/true,
31612                                           /*template_p=*/NULL,
31613                                           /*declarator_p=*/false,
31614                                           /*optional_p=*/false);
31615           if (step != error_mark_node)
31616             step = cp_parser_lookup_name_simple (parser, step, token->location);
31617           if (step == error_mark_node)
31618             {
31619               step = NULL_TREE;
31620               cp_parser_abort_tentative_parse (parser);
31621             }
31622           else if (!cp_parser_parse_definitely (parser))
31623             step = NULL_TREE;
31624         }
31625       if (!step)
31626         step = cp_parser_expression (parser);
31627
31628       if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31629         {
31630           sorry ("using parameters for %<linear%> step is not supported yet");
31631           step = integer_one_node;
31632         }
31633       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31634         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31635                                                /*or_comma=*/false,
31636                                                /*consume_paren=*/true);
31637
31638       if (step == error_mark_node)
31639         return list;
31640     }
31641
31642   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31643     {
31644       OMP_CLAUSE_LINEAR_STEP (c) = step;
31645       OMP_CLAUSE_LINEAR_KIND (c) = kind;
31646     }
31647
31648   return nlist;
31649 }
31650
31651 /* OpenMP 4.0:
31652    safelen ( constant-expression )  */
31653
31654 static tree
31655 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31656                               location_t location)
31657 {
31658   tree t, c;
31659
31660   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31661     return list;
31662
31663   t = cp_parser_constant_expression (parser);
31664
31665   if (t == error_mark_node
31666       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31667     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31668                                            /*or_comma=*/false,
31669                                            /*consume_paren=*/true);
31670
31671   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31672
31673   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31674   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31675   OMP_CLAUSE_CHAIN (c) = list;
31676
31677   return c;
31678 }
31679
31680 /* OpenMP 4.0:
31681    simdlen ( constant-expression )  */
31682
31683 static tree
31684 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31685                               location_t location)
31686 {
31687   tree t, c;
31688
31689   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31690     return list;
31691
31692   t = cp_parser_constant_expression (parser);
31693
31694   if (t == error_mark_node
31695       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31696     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31697                                            /*or_comma=*/false,
31698                                            /*consume_paren=*/true);
31699
31700   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31701
31702   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31703   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31704   OMP_CLAUSE_CHAIN (c) = list;
31705
31706   return c;
31707 }
31708
31709 /* OpenMP 4.5:
31710    vec:
31711      identifier [+/- integer]
31712      vec , identifier [+/- integer]
31713 */
31714
31715 static tree
31716 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31717                                   tree list)
31718 {
31719   tree vec = NULL;
31720
31721   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31722     {
31723       cp_parser_error (parser, "expected identifier");
31724       return list;
31725     }
31726
31727   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31728     {
31729       location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31730       tree t, identifier = cp_parser_identifier (parser);
31731       tree addend = NULL;
31732
31733       if (identifier == error_mark_node)
31734         t = error_mark_node;
31735       else
31736         {
31737           t = cp_parser_lookup_name_simple
31738                 (parser, identifier,
31739                  cp_lexer_peek_token (parser->lexer)->location);
31740           if (t == error_mark_node)
31741             cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31742                                          id_loc);
31743         }
31744
31745       bool neg = false;
31746       if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31747         neg = true;
31748       else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31749         {
31750           addend = integer_zero_node;
31751           goto add_to_vector;
31752         }
31753       cp_lexer_consume_token (parser->lexer);
31754
31755       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31756         {
31757           cp_parser_error (parser, "expected integer");
31758           return list;
31759         }
31760
31761       addend = cp_lexer_peek_token (parser->lexer)->u.value;
31762       if (TREE_CODE (addend) != INTEGER_CST)
31763         {
31764           cp_parser_error (parser, "expected integer");
31765           return list;
31766         }
31767       cp_lexer_consume_token (parser->lexer);
31768
31769     add_to_vector:
31770       if (t != error_mark_node)
31771         {
31772           vec = tree_cons (addend, t, vec);
31773           if (neg)
31774             OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31775         }
31776
31777       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31778         break;
31779
31780       cp_lexer_consume_token (parser->lexer);
31781     }
31782
31783   if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31784     {
31785       tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31786       OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31787       OMP_CLAUSE_DECL (u) = nreverse (vec);
31788       OMP_CLAUSE_CHAIN (u) = list;
31789       return u;
31790     }
31791   return list;
31792 }
31793
31794 /* OpenMP 4.0:
31795    depend ( depend-kind : variable-list )
31796
31797    depend-kind:
31798      in | out | inout
31799
31800    OpenMP 4.5:
31801    depend ( source )
31802
31803    depend ( sink : vec ) */
31804
31805 static tree
31806 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31807 {
31808   tree nlist, c;
31809   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31810
31811   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31812     return list;
31813
31814   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31815     {
31816       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31817       const char *p = IDENTIFIER_POINTER (id);
31818
31819       if (strcmp ("in", p) == 0)
31820         kind = OMP_CLAUSE_DEPEND_IN;
31821       else if (strcmp ("inout", p) == 0)
31822         kind = OMP_CLAUSE_DEPEND_INOUT;
31823       else if (strcmp ("out", p) == 0)
31824         kind = OMP_CLAUSE_DEPEND_OUT;
31825       else if (strcmp ("source", p) == 0)
31826         kind = OMP_CLAUSE_DEPEND_SOURCE;
31827       else if (strcmp ("sink", p) == 0)
31828         kind = OMP_CLAUSE_DEPEND_SINK;
31829       else
31830         goto invalid_kind;
31831     }
31832   else
31833     goto invalid_kind;
31834
31835   cp_lexer_consume_token (parser->lexer);
31836
31837   if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31838     {
31839       c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31840       OMP_CLAUSE_DEPEND_KIND (c) = kind;
31841       OMP_CLAUSE_DECL (c) = NULL_TREE;
31842       OMP_CLAUSE_CHAIN (c) = list;
31843       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31844         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31845                                                /*or_comma=*/false,
31846                                                /*consume_paren=*/true);
31847       return c;
31848     }
31849
31850   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31851     goto resync_fail;
31852
31853   if (kind == OMP_CLAUSE_DEPEND_SINK)
31854     nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31855   else
31856     {
31857       nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31858                                               list, NULL);
31859
31860       for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31861         OMP_CLAUSE_DEPEND_KIND (c) = kind;
31862     }
31863   return nlist;
31864
31865  invalid_kind:
31866   cp_parser_error (parser, "invalid depend kind");
31867  resync_fail:
31868   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31869                                          /*or_comma=*/false,
31870                                          /*consume_paren=*/true);
31871   return list;
31872 }
31873
31874 /* OpenMP 4.0:
31875    map ( map-kind : variable-list )
31876    map ( variable-list )
31877
31878    map-kind:
31879      alloc | to | from | tofrom
31880
31881    OpenMP 4.5:
31882    map-kind:
31883      alloc | to | from | tofrom | release | delete
31884
31885    map ( always [,] map-kind: variable-list ) */
31886
31887 static tree
31888 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31889 {
31890   tree nlist, c;
31891   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31892   bool always = false;
31893
31894   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31895     return list;
31896
31897   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31898     {
31899       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31900       const char *p = IDENTIFIER_POINTER (id);
31901
31902       if (strcmp ("always", p) == 0)
31903         {
31904           int nth = 2;
31905           if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31906             nth++;
31907           if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31908                || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31909                    == RID_DELETE))
31910               && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31911                   == CPP_COLON))
31912             {
31913               always = true;
31914               cp_lexer_consume_token (parser->lexer);
31915               if (nth == 3)
31916                 cp_lexer_consume_token (parser->lexer);
31917             }
31918         }
31919     }
31920
31921   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31922       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31923     {
31924       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31925       const char *p = IDENTIFIER_POINTER (id);
31926
31927       if (strcmp ("alloc", p) == 0)
31928         kind = GOMP_MAP_ALLOC;
31929       else if (strcmp ("to", p) == 0)
31930         kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31931       else if (strcmp ("from", p) == 0)
31932         kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31933       else if (strcmp ("tofrom", p) == 0)
31934         kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31935       else if (strcmp ("release", p) == 0)
31936         kind = GOMP_MAP_RELEASE;
31937       else
31938         {
31939           cp_parser_error (parser, "invalid map kind");
31940           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31941                                                  /*or_comma=*/false,
31942                                                  /*consume_paren=*/true);
31943           return list;
31944         }
31945       cp_lexer_consume_token (parser->lexer);
31946       cp_lexer_consume_token (parser->lexer);
31947     }
31948   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31949            && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31950     {
31951       kind = GOMP_MAP_DELETE;
31952       cp_lexer_consume_token (parser->lexer);
31953       cp_lexer_consume_token (parser->lexer);
31954     }
31955
31956   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31957                                           NULL);
31958
31959   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31960     OMP_CLAUSE_SET_MAP_KIND (c, kind);
31961
31962   return nlist;
31963 }
31964
31965 /* OpenMP 4.0:
31966    device ( expression ) */
31967
31968 static tree
31969 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31970                              location_t location)
31971 {
31972   tree t, c;
31973
31974   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31975     return list;
31976
31977   t = cp_parser_expression (parser);
31978
31979   if (t == error_mark_node
31980       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31981     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31982                                            /*or_comma=*/false,
31983                                            /*consume_paren=*/true);
31984
31985   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31986                              "device", location);
31987
31988   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31989   OMP_CLAUSE_DEVICE_ID (c) = t;
31990   OMP_CLAUSE_CHAIN (c) = list;
31991
31992   return c;
31993 }
31994
31995 /* OpenMP 4.0:
31996    dist_schedule ( static )
31997    dist_schedule ( static , expression )  */
31998
31999 static tree
32000 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32001                                     location_t location)
32002 {
32003   tree c, t;
32004
32005   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32006     return list;
32007
32008   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32009
32010   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32011     goto invalid_kind;
32012   cp_lexer_consume_token (parser->lexer);
32013
32014   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32015     {
32016       cp_lexer_consume_token (parser->lexer);
32017
32018       t = cp_parser_assignment_expression (parser);
32019
32020       if (t == error_mark_node)
32021         goto resync_fail;
32022       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32023
32024       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32025         goto resync_fail;
32026     }
32027   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32028     goto resync_fail;
32029
32030   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32031                              location);
32032   OMP_CLAUSE_CHAIN (c) = list;
32033   return c;
32034
32035  invalid_kind:
32036   cp_parser_error (parser, "invalid dist_schedule kind");
32037  resync_fail:
32038   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32039                                          /*or_comma=*/false,
32040                                          /*consume_paren=*/true);
32041   return list;
32042 }
32043
32044 /* OpenMP 4.0:
32045    proc_bind ( proc-bind-kind )
32046
32047    proc-bind-kind:
32048      master | close | spread  */
32049
32050 static tree
32051 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32052                                 location_t location)
32053 {
32054   tree c;
32055   enum omp_clause_proc_bind_kind kind;
32056
32057   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32058     return list;
32059
32060   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32061     {
32062       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32063       const char *p = IDENTIFIER_POINTER (id);
32064
32065       if (strcmp ("master", p) == 0)
32066         kind = OMP_CLAUSE_PROC_BIND_MASTER;
32067       else if (strcmp ("close", p) == 0)
32068         kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32069       else if (strcmp ("spread", p) == 0)
32070         kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32071       else
32072         goto invalid_kind;
32073     }
32074   else
32075     goto invalid_kind;
32076
32077   cp_lexer_consume_token (parser->lexer);
32078   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32079     goto resync_fail;
32080
32081   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32082   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32083                              location);
32084   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32085   OMP_CLAUSE_CHAIN (c) = list;
32086   return c;
32087
32088  invalid_kind:
32089   cp_parser_error (parser, "invalid depend kind");
32090  resync_fail:
32091   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32092                                          /*or_comma=*/false,
32093                                          /*consume_paren=*/true);
32094   return list;
32095 }
32096
32097 /* OpenACC:
32098    async [( int-expr )] */
32099
32100 static tree
32101 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32102 {
32103   tree c, t;
32104   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32105
32106   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32107
32108   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32109     {
32110       cp_lexer_consume_token (parser->lexer);
32111
32112       t = cp_parser_expression (parser);
32113       if (t == error_mark_node
32114           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32115         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32116                                                 /*or_comma=*/false,
32117                                                 /*consume_paren=*/true);
32118     }
32119
32120   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32121
32122   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32123   OMP_CLAUSE_ASYNC_EXPR (c) = t;
32124   OMP_CLAUSE_CHAIN (c) = list;
32125   list = c;
32126
32127   return list;
32128 }
32129
32130 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
32131    is a bitmask in MASK.  Return the list of clauses found.  */
32132
32133 static tree
32134 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32135                            const char *where, cp_token *pragma_tok,
32136                            bool finish_p = true)
32137 {
32138   tree clauses = NULL;
32139   bool first = true;
32140
32141   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32142     {
32143       location_t here;
32144       pragma_omp_clause c_kind;
32145       omp_clause_code code;
32146       const char *c_name;
32147       tree prev = clauses;
32148
32149       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32150         cp_lexer_consume_token (parser->lexer);
32151
32152       here = cp_lexer_peek_token (parser->lexer)->location;
32153       c_kind = cp_parser_omp_clause_name (parser);
32154
32155       switch (c_kind)
32156         {
32157         case PRAGMA_OACC_CLAUSE_ASYNC:
32158           clauses = cp_parser_oacc_clause_async (parser, clauses);
32159           c_name = "async";
32160           break;
32161         case PRAGMA_OACC_CLAUSE_AUTO:
32162           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32163                                                  clauses, here);
32164           c_name = "auto";
32165           break;
32166         case PRAGMA_OACC_CLAUSE_COLLAPSE:
32167           clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32168           c_name = "collapse";
32169           break;
32170         case PRAGMA_OACC_CLAUSE_COPY:
32171           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32172           c_name = "copy";
32173           break;
32174         case PRAGMA_OACC_CLAUSE_COPYIN:
32175           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32176           c_name = "copyin";
32177           break;
32178         case PRAGMA_OACC_CLAUSE_COPYOUT:
32179           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32180           c_name = "copyout";
32181           break;
32182         case PRAGMA_OACC_CLAUSE_CREATE:
32183           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32184           c_name = "create";
32185           break;
32186         case PRAGMA_OACC_CLAUSE_DELETE:
32187           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32188           c_name = "delete";
32189           break;
32190         case PRAGMA_OMP_CLAUSE_DEFAULT:
32191           clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32192           c_name = "default";
32193           break;
32194         case PRAGMA_OACC_CLAUSE_DEVICE:
32195           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32196           c_name = "device";
32197           break;
32198         case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32199           clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32200           c_name = "deviceptr";
32201           break;
32202         case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32203           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32204           c_name = "device_resident";
32205           break;
32206         case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32207           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32208                                             clauses);
32209           c_name = "firstprivate";
32210           break;
32211         case PRAGMA_OACC_CLAUSE_GANG:
32212           c_name = "gang";
32213           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32214                                                  c_name, clauses);
32215           break;
32216         case PRAGMA_OACC_CLAUSE_HOST:
32217           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32218           c_name = "host";
32219           break;
32220         case PRAGMA_OACC_CLAUSE_IF:
32221           clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32222           c_name = "if";
32223           break;
32224         case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32225           clauses = cp_parser_oacc_simple_clause (parser,
32226                                                   OMP_CLAUSE_INDEPENDENT,
32227                                                   clauses, here);
32228           c_name = "independent";
32229           break;
32230         case PRAGMA_OACC_CLAUSE_LINK:
32231           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32232           c_name = "link";
32233           break;
32234         case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32235           code = OMP_CLAUSE_NUM_GANGS;
32236           c_name = "num_gangs";
32237           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32238                                                       clauses);
32239           break;
32240         case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32241           c_name = "num_workers";
32242           code = OMP_CLAUSE_NUM_WORKERS;
32243           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32244                                                       clauses);
32245           break;
32246         case PRAGMA_OACC_CLAUSE_PRESENT:
32247           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32248           c_name = "present";
32249           break;
32250         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32251           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32252           c_name = "present_or_copy";
32253           break;
32254         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32255           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32256           c_name = "present_or_copyin";
32257           break;
32258         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32259           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32260           c_name = "present_or_copyout";
32261           break;
32262         case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32263           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32264           c_name = "present_or_create";
32265           break;
32266         case PRAGMA_OACC_CLAUSE_PRIVATE:
32267           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32268                                             clauses);
32269           c_name = "private";
32270           break;
32271         case PRAGMA_OACC_CLAUSE_REDUCTION:
32272           clauses = cp_parser_omp_clause_reduction (parser, clauses);
32273           c_name = "reduction";
32274           break;
32275         case PRAGMA_OACC_CLAUSE_SELF:
32276           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32277           c_name = "self";
32278           break;
32279         case PRAGMA_OACC_CLAUSE_SEQ:
32280           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32281                                                  clauses, here);
32282           c_name = "seq";
32283           break;
32284         case PRAGMA_OACC_CLAUSE_TILE:
32285           clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32286           c_name = "tile";
32287           break;
32288         case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32289           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32290                                             clauses);
32291           c_name = "use_device";
32292           break;
32293         case PRAGMA_OACC_CLAUSE_VECTOR:
32294           c_name = "vector";
32295           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32296                                                  c_name, clauses);
32297           break;
32298         case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32299           c_name = "vector_length";
32300           code = OMP_CLAUSE_VECTOR_LENGTH;
32301           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32302                                                       clauses);
32303           break;
32304         case PRAGMA_OACC_CLAUSE_WAIT:
32305           clauses = cp_parser_oacc_clause_wait (parser, clauses);
32306           c_name = "wait";
32307           break;
32308         case PRAGMA_OACC_CLAUSE_WORKER:
32309           c_name = "worker";
32310           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32311                                                  c_name, clauses);
32312           break;
32313         default:
32314           cp_parser_error (parser, "expected %<#pragma acc%> clause");
32315           goto saw_error;
32316         }
32317
32318       first = false;
32319
32320       if (((mask >> c_kind) & 1) == 0)
32321         {
32322           /* Remove the invalid clause(s) from the list to avoid
32323              confusing the rest of the compiler.  */
32324           clauses = prev;
32325           error_at (here, "%qs is not valid for %qs", c_name, where);
32326         }
32327     }
32328
32329  saw_error:
32330   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32331
32332   if (finish_p)
32333     return finish_omp_clauses (clauses, false);
32334
32335   return clauses;
32336 }
32337
32338 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
32339    is a bitmask in MASK.  Return the list of clauses found; the result
32340    of clause default goes in *pdefault.  */
32341
32342 static tree
32343 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32344                            const char *where, cp_token *pragma_tok,
32345                            bool finish_p = true)
32346 {
32347   tree clauses = NULL;
32348   bool first = true;
32349   cp_token *token = NULL;
32350
32351   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32352     {
32353       pragma_omp_clause c_kind;
32354       const char *c_name;
32355       tree prev = clauses;
32356
32357       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32358         cp_lexer_consume_token (parser->lexer);
32359
32360       token = cp_lexer_peek_token (parser->lexer);
32361       c_kind = cp_parser_omp_clause_name (parser);
32362
32363       switch (c_kind)
32364         {
32365         case PRAGMA_OMP_CLAUSE_COLLAPSE:
32366           clauses = cp_parser_omp_clause_collapse (parser, clauses,
32367                                                    token->location);
32368           c_name = "collapse";
32369           break;
32370         case PRAGMA_OMP_CLAUSE_COPYIN:
32371           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32372           c_name = "copyin";
32373           break;
32374         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32375           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32376                                             clauses);
32377           c_name = "copyprivate";
32378           break;
32379         case PRAGMA_OMP_CLAUSE_DEFAULT:
32380           clauses = cp_parser_omp_clause_default (parser, clauses,
32381                                                   token->location, false);
32382           c_name = "default";
32383           break;
32384         case PRAGMA_OMP_CLAUSE_FINAL:
32385           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32386           c_name = "final";
32387           break;
32388         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32389           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32390                                             clauses);
32391           c_name = "firstprivate";
32392           break;
32393         case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32394           clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32395                                                     token->location);
32396           c_name = "grainsize";
32397           break;
32398         case PRAGMA_OMP_CLAUSE_HINT:
32399           clauses = cp_parser_omp_clause_hint (parser, clauses,
32400                                                token->location);
32401           c_name = "hint";
32402           break;
32403         case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32404           clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32405                                                      token->location);
32406           c_name = "defaultmap";
32407           break;
32408         case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32409           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32410                                             clauses);
32411           c_name = "use_device_ptr";
32412           break;
32413         case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32414           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32415                                             clauses);
32416           c_name = "is_device_ptr";
32417           break;
32418         case PRAGMA_OMP_CLAUSE_IF:
32419           clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32420                                              true);
32421           c_name = "if";
32422           break;
32423         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32424           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32425                                             clauses);
32426           c_name = "lastprivate";
32427           break;
32428         case PRAGMA_OMP_CLAUSE_MERGEABLE:
32429           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32430                                                     token->location);
32431           c_name = "mergeable";
32432           break;
32433         case PRAGMA_OMP_CLAUSE_NOWAIT:
32434           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32435           c_name = "nowait";
32436           break;
32437         case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32438           clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32439                                                     token->location);
32440           c_name = "num_tasks";
32441           break;
32442         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32443           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32444                                                       token->location);
32445           c_name = "num_threads";
32446           break;
32447         case PRAGMA_OMP_CLAUSE_ORDERED:
32448           clauses = cp_parser_omp_clause_ordered (parser, clauses,
32449                                                   token->location);
32450           c_name = "ordered";
32451           break;
32452         case PRAGMA_OMP_CLAUSE_PRIORITY:
32453           clauses = cp_parser_omp_clause_priority (parser, clauses,
32454                                                    token->location);
32455           c_name = "priority";
32456           break;
32457         case PRAGMA_OMP_CLAUSE_PRIVATE:
32458           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32459                                             clauses);
32460           c_name = "private";
32461           break;
32462         case PRAGMA_OMP_CLAUSE_REDUCTION:
32463           clauses = cp_parser_omp_clause_reduction (parser, clauses);
32464           c_name = "reduction";
32465           break;
32466         case PRAGMA_OMP_CLAUSE_SCHEDULE:
32467           clauses = cp_parser_omp_clause_schedule (parser, clauses,
32468                                                    token->location);
32469           c_name = "schedule";
32470           break;
32471         case PRAGMA_OMP_CLAUSE_SHARED:
32472           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32473                                             clauses);
32474           c_name = "shared";
32475           break;
32476         case PRAGMA_OMP_CLAUSE_UNTIED:
32477           clauses = cp_parser_omp_clause_untied (parser, clauses,
32478                                                  token->location);
32479           c_name = "untied";
32480           break;
32481         case PRAGMA_OMP_CLAUSE_INBRANCH:
32482         case PRAGMA_CILK_CLAUSE_MASK:
32483           clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32484                                                  clauses, token->location);
32485           c_name = "inbranch";
32486           break;
32487         case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32488         case PRAGMA_CILK_CLAUSE_NOMASK:
32489           clauses = cp_parser_omp_clause_branch (parser,
32490                                                  OMP_CLAUSE_NOTINBRANCH,
32491                                                  clauses, token->location);
32492           c_name = "notinbranch";
32493           break;
32494         case PRAGMA_OMP_CLAUSE_PARALLEL:
32495           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32496                                                      clauses, token->location);
32497           c_name = "parallel";
32498           if (!first)
32499             {
32500              clause_not_first:
32501               error_at (token->location, "%qs must be the first clause of %qs",
32502                         c_name, where);
32503               clauses = prev;
32504             }
32505           break;
32506         case PRAGMA_OMP_CLAUSE_FOR:
32507           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32508                                                      clauses, token->location);
32509           c_name = "for";
32510           if (!first)
32511             goto clause_not_first;
32512           break;
32513         case PRAGMA_OMP_CLAUSE_SECTIONS:
32514           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32515                                                      clauses, token->location);
32516           c_name = "sections";
32517           if (!first)
32518             goto clause_not_first;
32519           break;
32520         case PRAGMA_OMP_CLAUSE_TASKGROUP:
32521           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32522                                                      clauses, token->location);
32523           c_name = "taskgroup";
32524           if (!first)
32525             goto clause_not_first;
32526           break;
32527         case PRAGMA_OMP_CLAUSE_LINK:
32528           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32529           c_name = "to";
32530           break;
32531         case PRAGMA_OMP_CLAUSE_TO:
32532           if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32533             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32534                                               clauses);
32535           else
32536             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32537           c_name = "to";
32538           break;
32539         case PRAGMA_OMP_CLAUSE_FROM:
32540           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32541           c_name = "from";
32542           break;
32543         case PRAGMA_OMP_CLAUSE_UNIFORM:
32544           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32545                                             clauses);
32546           c_name = "uniform";
32547           break;
32548         case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32549           clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32550                                                     token->location);
32551           c_name = "num_teams";
32552           break;
32553         case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32554           clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32555                                                        token->location);
32556           c_name = "thread_limit";
32557           break;
32558         case PRAGMA_OMP_CLAUSE_ALIGNED:
32559           clauses = cp_parser_omp_clause_aligned (parser, clauses);
32560           c_name = "aligned";
32561           break;
32562         case PRAGMA_OMP_CLAUSE_LINEAR:
32563           {
32564             bool cilk_simd_fn = false, declare_simd = false;
32565             if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32566               cilk_simd_fn = true;
32567             else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32568               declare_simd = true;
32569             clauses = cp_parser_omp_clause_linear (parser, clauses,
32570                                                    cilk_simd_fn, declare_simd);
32571           }
32572           c_name = "linear";
32573           break;
32574         case PRAGMA_OMP_CLAUSE_DEPEND:
32575           clauses = cp_parser_omp_clause_depend (parser, clauses,
32576                                                  token->location);
32577           c_name = "depend";
32578           break;
32579         case PRAGMA_OMP_CLAUSE_MAP:
32580           clauses = cp_parser_omp_clause_map (parser, clauses);
32581           c_name = "map";
32582           break;
32583         case PRAGMA_OMP_CLAUSE_DEVICE:
32584           clauses = cp_parser_omp_clause_device (parser, clauses,
32585                                                  token->location);
32586           c_name = "device";
32587           break;
32588         case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32589           clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32590                                                         token->location);
32591           c_name = "dist_schedule";
32592           break;
32593         case PRAGMA_OMP_CLAUSE_PROC_BIND:
32594           clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32595                                                     token->location);
32596           c_name = "proc_bind";
32597           break;
32598         case PRAGMA_OMP_CLAUSE_SAFELEN:
32599           clauses = cp_parser_omp_clause_safelen (parser, clauses,
32600                                                   token->location);
32601           c_name = "safelen";
32602           break;
32603         case PRAGMA_OMP_CLAUSE_SIMDLEN:
32604           clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32605                                                   token->location);
32606           c_name = "simdlen";
32607           break;
32608         case PRAGMA_OMP_CLAUSE_NOGROUP:
32609           clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32610                                                   token->location);
32611           c_name = "nogroup";
32612           break;
32613         case PRAGMA_OMP_CLAUSE_THREADS:
32614           clauses
32615             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32616                                                 clauses, token->location);
32617           c_name = "threads";
32618           break;
32619         case PRAGMA_OMP_CLAUSE_SIMD:
32620           clauses
32621             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32622                                                 clauses, token->location);
32623           c_name = "simd";
32624           break;
32625         case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32626           clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32627           c_name = "simdlen";
32628           break;
32629         default:
32630           cp_parser_error (parser, "expected %<#pragma omp%> clause");
32631           goto saw_error;
32632         }
32633
32634       first = false;
32635
32636       if (((mask >> c_kind) & 1) == 0)
32637         {
32638           /* Remove the invalid clause(s) from the list to avoid
32639              confusing the rest of the compiler.  */
32640           clauses = prev;
32641           error_at (token->location, "%qs is not valid for %qs", c_name, where);
32642         }
32643     }
32644  saw_error:
32645   /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32646      no reason to skip to the end.  */
32647   if (!(flag_cilkplus && pragma_tok == NULL))
32648     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32649   if (finish_p)
32650     {
32651       if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32652         return finish_omp_clauses (clauses, false, true);
32653       else
32654         return finish_omp_clauses (clauses, true);
32655     }
32656   return clauses;
32657 }
32658
32659 /* OpenMP 2.5:
32660    structured-block:
32661      statement
32662
32663    In practice, we're also interested in adding the statement to an
32664    outer node.  So it is convenient if we work around the fact that
32665    cp_parser_statement calls add_stmt.  */
32666
32667 static unsigned
32668 cp_parser_begin_omp_structured_block (cp_parser *parser)
32669 {
32670   unsigned save = parser->in_statement;
32671
32672   /* Only move the values to IN_OMP_BLOCK if they weren't false.
32673      This preserves the "not within loop or switch" style error messages
32674      for nonsense cases like
32675         void foo() {
32676         #pragma omp single
32677           break;
32678         }
32679   */
32680   if (parser->in_statement)
32681     parser->in_statement = IN_OMP_BLOCK;
32682
32683   return save;
32684 }
32685
32686 static void
32687 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32688 {
32689   parser->in_statement = save;
32690 }
32691
32692 static tree
32693 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32694 {
32695   tree stmt = begin_omp_structured_block ();
32696   unsigned int save = cp_parser_begin_omp_structured_block (parser);
32697
32698   cp_parser_statement (parser, NULL_TREE, false, if_p);
32699
32700   cp_parser_end_omp_structured_block (parser, save);
32701   return finish_omp_structured_block (stmt);
32702 }
32703
32704 /* OpenMP 2.5:
32705    # pragma omp atomic new-line
32706      expression-stmt
32707
32708    expression-stmt:
32709      x binop= expr | x++ | ++x | x-- | --x
32710    binop:
32711      +, *, -, /, &, ^, |, <<, >>
32712
32713   where x is an lvalue expression with scalar type.
32714
32715    OpenMP 3.1:
32716    # pragma omp atomic new-line
32717      update-stmt
32718
32719    # pragma omp atomic read new-line
32720      read-stmt
32721
32722    # pragma omp atomic write new-line
32723      write-stmt
32724
32725    # pragma omp atomic update new-line
32726      update-stmt
32727
32728    # pragma omp atomic capture new-line
32729      capture-stmt
32730
32731    # pragma omp atomic capture new-line
32732      capture-block
32733
32734    read-stmt:
32735      v = x
32736    write-stmt:
32737      x = expr
32738    update-stmt:
32739      expression-stmt | x = x binop expr
32740    capture-stmt:
32741      v = expression-stmt
32742    capture-block:
32743      { v = x; update-stmt; } | { update-stmt; v = x; }
32744
32745    OpenMP 4.0:
32746    update-stmt:
32747      expression-stmt | x = x binop expr | x = expr binop x
32748    capture-stmt:
32749      v = update-stmt
32750    capture-block:
32751      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32752
32753   where x and v are lvalue expressions with scalar type.  */
32754
32755 static void
32756 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32757 {
32758   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32759   tree rhs1 = NULL_TREE, orig_lhs;
32760   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32761   bool structured_block = false;
32762   bool seq_cst = false;
32763
32764   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32765     {
32766       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32767       const char *p = IDENTIFIER_POINTER (id);
32768
32769       if (!strcmp (p, "seq_cst"))
32770         {
32771           seq_cst = true;
32772           cp_lexer_consume_token (parser->lexer);
32773           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32774               && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32775             cp_lexer_consume_token (parser->lexer);
32776         }
32777     }
32778   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32779     {
32780       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32781       const char *p = IDENTIFIER_POINTER (id);
32782
32783       if (!strcmp (p, "read"))
32784         code = OMP_ATOMIC_READ;
32785       else if (!strcmp (p, "write"))
32786         code = NOP_EXPR;
32787       else if (!strcmp (p, "update"))
32788         code = OMP_ATOMIC;
32789       else if (!strcmp (p, "capture"))
32790         code = OMP_ATOMIC_CAPTURE_NEW;
32791       else
32792         p = NULL;
32793       if (p)
32794         cp_lexer_consume_token (parser->lexer);
32795     }
32796   if (!seq_cst)
32797     {
32798       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32799           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32800         cp_lexer_consume_token (parser->lexer);
32801
32802       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32803         {
32804           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32805           const char *p = IDENTIFIER_POINTER (id);
32806
32807           if (!strcmp (p, "seq_cst"))
32808             {
32809               seq_cst = true;
32810               cp_lexer_consume_token (parser->lexer);
32811             }
32812         }
32813     }
32814   cp_parser_require_pragma_eol (parser, pragma_tok);
32815
32816   switch (code)
32817     {
32818     case OMP_ATOMIC_READ:
32819     case NOP_EXPR: /* atomic write */
32820       v = cp_parser_unary_expression (parser);
32821       if (v == error_mark_node)
32822         goto saw_error;
32823       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32824         goto saw_error;
32825       if (code == NOP_EXPR)
32826         lhs = cp_parser_expression (parser);
32827       else
32828         lhs = cp_parser_unary_expression (parser);
32829       if (lhs == error_mark_node)
32830         goto saw_error;
32831       if (code == NOP_EXPR)
32832         {
32833           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32834              opcode.  */
32835           code = OMP_ATOMIC;
32836           rhs = lhs;
32837           lhs = v;
32838           v = NULL_TREE;
32839         }
32840       goto done;
32841     case OMP_ATOMIC_CAPTURE_NEW:
32842       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32843         {
32844           cp_lexer_consume_token (parser->lexer);
32845           structured_block = true;
32846         }
32847       else
32848         {
32849           v = cp_parser_unary_expression (parser);
32850           if (v == error_mark_node)
32851             goto saw_error;
32852           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32853             goto saw_error;
32854         }
32855     default:
32856       break;
32857     }
32858
32859 restart:
32860   lhs = cp_parser_unary_expression (parser);
32861   orig_lhs = lhs;
32862   switch (TREE_CODE (lhs))
32863     {
32864     case ERROR_MARK:
32865       goto saw_error;
32866
32867     case POSTINCREMENT_EXPR:
32868       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32869         code = OMP_ATOMIC_CAPTURE_OLD;
32870       /* FALLTHROUGH */
32871     case PREINCREMENT_EXPR:
32872       lhs = TREE_OPERAND (lhs, 0);
32873       opcode = PLUS_EXPR;
32874       rhs = integer_one_node;
32875       break;
32876
32877     case POSTDECREMENT_EXPR:
32878       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32879         code = OMP_ATOMIC_CAPTURE_OLD;
32880       /* FALLTHROUGH */
32881     case PREDECREMENT_EXPR:
32882       lhs = TREE_OPERAND (lhs, 0);
32883       opcode = MINUS_EXPR;
32884       rhs = integer_one_node;
32885       break;
32886
32887     case COMPOUND_EXPR:
32888       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32889          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32890          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32891          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32892          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32893                                              (TREE_OPERAND (lhs, 1), 0), 0)))
32894             == BOOLEAN_TYPE)
32895        /* Undo effects of boolean_increment for post {in,de}crement.  */
32896        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32897       /* FALLTHRU */
32898     case MODIFY_EXPR:
32899       if (TREE_CODE (lhs) == MODIFY_EXPR
32900          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32901         {
32902           /* Undo effects of boolean_increment.  */
32903           if (integer_onep (TREE_OPERAND (lhs, 1)))
32904             {
32905               /* This is pre or post increment.  */
32906               rhs = TREE_OPERAND (lhs, 1);
32907               lhs = TREE_OPERAND (lhs, 0);
32908               opcode = NOP_EXPR;
32909               if (code == OMP_ATOMIC_CAPTURE_NEW
32910                   && !structured_block
32911                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32912                 code = OMP_ATOMIC_CAPTURE_OLD;
32913               break;
32914             }
32915         }
32916       /* FALLTHRU */
32917     default:
32918       switch (cp_lexer_peek_token (parser->lexer)->type)
32919         {
32920         case CPP_MULT_EQ:
32921           opcode = MULT_EXPR;
32922           break;
32923         case CPP_DIV_EQ:
32924           opcode = TRUNC_DIV_EXPR;
32925           break;
32926         case CPP_PLUS_EQ:
32927           opcode = PLUS_EXPR;
32928           break;
32929         case CPP_MINUS_EQ:
32930           opcode = MINUS_EXPR;
32931           break;
32932         case CPP_LSHIFT_EQ:
32933           opcode = LSHIFT_EXPR;
32934           break;
32935         case CPP_RSHIFT_EQ:
32936           opcode = RSHIFT_EXPR;
32937           break;
32938         case CPP_AND_EQ:
32939           opcode = BIT_AND_EXPR;
32940           break;
32941         case CPP_OR_EQ:
32942           opcode = BIT_IOR_EXPR;
32943           break;
32944         case CPP_XOR_EQ:
32945           opcode = BIT_XOR_EXPR;
32946           break;
32947         case CPP_EQ:
32948           enum cp_parser_prec oprec;
32949           cp_token *token;
32950           cp_lexer_consume_token (parser->lexer);
32951           cp_parser_parse_tentatively (parser);
32952           rhs1 = cp_parser_simple_cast_expression (parser);
32953           if (rhs1 == error_mark_node)
32954             {
32955               cp_parser_abort_tentative_parse (parser);
32956               cp_parser_simple_cast_expression (parser);
32957               goto saw_error;
32958             }
32959           token = cp_lexer_peek_token (parser->lexer);
32960           if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32961             {
32962               cp_parser_abort_tentative_parse (parser);
32963               cp_parser_parse_tentatively (parser);
32964               rhs = cp_parser_binary_expression (parser, false, true,
32965                                                  PREC_NOT_OPERATOR, NULL);
32966               if (rhs == error_mark_node)
32967                 {
32968                   cp_parser_abort_tentative_parse (parser);
32969                   cp_parser_binary_expression (parser, false, true,
32970                                                PREC_NOT_OPERATOR, NULL);
32971                   goto saw_error;
32972                 }
32973               switch (TREE_CODE (rhs))
32974                 {
32975                 case MULT_EXPR:
32976                 case TRUNC_DIV_EXPR:
32977                 case RDIV_EXPR:
32978                 case PLUS_EXPR:
32979                 case MINUS_EXPR:
32980                 case LSHIFT_EXPR:
32981                 case RSHIFT_EXPR:
32982                 case BIT_AND_EXPR:
32983                 case BIT_IOR_EXPR:
32984                 case BIT_XOR_EXPR:
32985                   if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32986                     {
32987                       if (cp_parser_parse_definitely (parser))
32988                         {
32989                           opcode = TREE_CODE (rhs);
32990                           rhs1 = TREE_OPERAND (rhs, 0);
32991                           rhs = TREE_OPERAND (rhs, 1);
32992                           goto stmt_done;
32993                         }
32994                       else
32995                         goto saw_error;
32996                     }
32997                   break;
32998                 default:
32999                   break;
33000                 }
33001               cp_parser_abort_tentative_parse (parser);
33002               if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33003                 {
33004                   rhs = cp_parser_expression (parser);
33005                   if (rhs == error_mark_node)
33006                     goto saw_error;
33007                   opcode = NOP_EXPR;
33008                   rhs1 = NULL_TREE;
33009                   goto stmt_done;
33010                 }
33011               cp_parser_error (parser,
33012                                "invalid form of %<#pragma omp atomic%>");
33013               goto saw_error;
33014             }
33015           if (!cp_parser_parse_definitely (parser))
33016             goto saw_error;
33017           switch (token->type)
33018             {
33019             case CPP_SEMICOLON:
33020               if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33021                 {
33022                   code = OMP_ATOMIC_CAPTURE_OLD;
33023                   v = lhs;
33024                   lhs = NULL_TREE;
33025                   lhs1 = rhs1;
33026                   rhs1 = NULL_TREE;
33027                   cp_lexer_consume_token (parser->lexer);
33028                   goto restart;
33029                 }
33030               else if (structured_block)
33031                 {
33032                   opcode = NOP_EXPR;
33033                   rhs = rhs1;
33034                   rhs1 = NULL_TREE;
33035                   goto stmt_done;
33036                 }
33037               cp_parser_error (parser,
33038                                "invalid form of %<#pragma omp atomic%>");
33039               goto saw_error;
33040             case CPP_MULT:
33041               opcode = MULT_EXPR;
33042               break;
33043             case CPP_DIV:
33044               opcode = TRUNC_DIV_EXPR;
33045               break;
33046             case CPP_PLUS:
33047               opcode = PLUS_EXPR;
33048               break;
33049             case CPP_MINUS:
33050               opcode = MINUS_EXPR;
33051               break;
33052             case CPP_LSHIFT:
33053               opcode = LSHIFT_EXPR;
33054               break;
33055             case CPP_RSHIFT:
33056               opcode = RSHIFT_EXPR;
33057               break;
33058             case CPP_AND:
33059               opcode = BIT_AND_EXPR;
33060               break;
33061             case CPP_OR:
33062               opcode = BIT_IOR_EXPR;
33063               break;
33064             case CPP_XOR:
33065               opcode = BIT_XOR_EXPR;
33066               break;
33067             default:
33068               cp_parser_error (parser,
33069                                "invalid operator for %<#pragma omp atomic%>");
33070               goto saw_error;
33071             }
33072           oprec = TOKEN_PRECEDENCE (token);
33073           gcc_assert (oprec != PREC_NOT_OPERATOR);
33074           if (commutative_tree_code (opcode))
33075             oprec = (enum cp_parser_prec) (oprec - 1);
33076           cp_lexer_consume_token (parser->lexer);
33077           rhs = cp_parser_binary_expression (parser, false, false,
33078                                              oprec, NULL);
33079           if (rhs == error_mark_node)
33080             goto saw_error;
33081           goto stmt_done;
33082           /* FALLTHROUGH */
33083         default:
33084           cp_parser_error (parser,
33085                            "invalid operator for %<#pragma omp atomic%>");
33086           goto saw_error;
33087         }
33088       cp_lexer_consume_token (parser->lexer);
33089
33090       rhs = cp_parser_expression (parser);
33091       if (rhs == error_mark_node)
33092         goto saw_error;
33093       break;
33094     }
33095 stmt_done:
33096   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33097     {
33098       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33099         goto saw_error;
33100       v = cp_parser_unary_expression (parser);
33101       if (v == error_mark_node)
33102         goto saw_error;
33103       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33104         goto saw_error;
33105       lhs1 = cp_parser_unary_expression (parser);
33106       if (lhs1 == error_mark_node)
33107         goto saw_error;
33108     }
33109   if (structured_block)
33110     {
33111       cp_parser_consume_semicolon_at_end_of_statement (parser);
33112       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33113     }
33114 done:
33115   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33116   if (!structured_block)
33117     cp_parser_consume_semicolon_at_end_of_statement (parser);
33118   return;
33119
33120  saw_error:
33121   cp_parser_skip_to_end_of_block_or_statement (parser);
33122   if (structured_block)
33123     {
33124       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33125         cp_lexer_consume_token (parser->lexer);
33126       else if (code == OMP_ATOMIC_CAPTURE_NEW)
33127         {
33128           cp_parser_skip_to_end_of_block_or_statement (parser);
33129           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33130             cp_lexer_consume_token (parser->lexer);
33131         }
33132     }
33133 }
33134
33135
33136 /* OpenMP 2.5:
33137    # pragma omp barrier new-line  */
33138
33139 static void
33140 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33141 {
33142   cp_parser_require_pragma_eol (parser, pragma_tok);
33143   finish_omp_barrier ();
33144 }
33145
33146 /* OpenMP 2.5:
33147    # pragma omp critical [(name)] new-line
33148      structured-block
33149
33150    OpenMP 4.5:
33151    # pragma omp critical [(name) [hint(expression)]] new-line
33152      structured-block  */
33153
33154 #define OMP_CRITICAL_CLAUSE_MASK                \
33155         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33156
33157 static tree
33158 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33159 {
33160   tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33161
33162   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33163     {
33164       cp_lexer_consume_token (parser->lexer);
33165
33166       name = cp_parser_identifier (parser);
33167
33168       if (name == error_mark_node
33169           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33170         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33171                                                /*or_comma=*/false,
33172                                                /*consume_paren=*/true);
33173       if (name == error_mark_node)
33174         name = NULL;
33175
33176       clauses = cp_parser_omp_all_clauses (parser,
33177                                            OMP_CRITICAL_CLAUSE_MASK,
33178                                            "#pragma omp critical", pragma_tok);
33179     }
33180   else
33181     cp_parser_require_pragma_eol (parser, pragma_tok);
33182
33183   stmt = cp_parser_omp_structured_block (parser, if_p);
33184   return c_finish_omp_critical (input_location, stmt, name, clauses);
33185 }
33186
33187 /* OpenMP 2.5:
33188    # pragma omp flush flush-vars[opt] new-line
33189
33190    flush-vars:
33191      ( variable-list ) */
33192
33193 static void
33194 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33195 {
33196   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33197     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33198   cp_parser_require_pragma_eol (parser, pragma_tok);
33199
33200   finish_omp_flush ();
33201 }
33202
33203 /* Helper function, to parse omp for increment expression.  */
33204
33205 static tree
33206 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33207 {
33208   tree cond = cp_parser_binary_expression (parser, false, true,
33209                                            PREC_NOT_OPERATOR, NULL);
33210   if (cond == error_mark_node
33211       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33212     {
33213       cp_parser_skip_to_end_of_statement (parser);
33214       return error_mark_node;
33215     }
33216
33217   switch (TREE_CODE (cond))
33218     {
33219     case GT_EXPR:
33220     case GE_EXPR:
33221     case LT_EXPR:
33222     case LE_EXPR:
33223       break;
33224     case NE_EXPR:
33225       if (code == CILK_SIMD || code == CILK_FOR)
33226         break;
33227       /* Fall through: OpenMP disallows NE_EXPR.  */
33228     default:
33229       return error_mark_node;
33230     }
33231
33232   /* If decl is an iterator, preserve LHS and RHS of the relational
33233      expr until finish_omp_for.  */
33234   if (decl
33235       && (type_dependent_expression_p (decl)
33236           || CLASS_TYPE_P (TREE_TYPE (decl))))
33237     return cond;
33238
33239   return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33240                             TREE_CODE (cond),
33241                             TREE_OPERAND (cond, 0), ERROR_MARK,
33242                             TREE_OPERAND (cond, 1), ERROR_MARK,
33243                             /*overload=*/NULL, tf_warning_or_error);
33244 }
33245
33246 /* Helper function, to parse omp for increment expression.  */
33247
33248 static tree
33249 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33250 {
33251   cp_token *token = cp_lexer_peek_token (parser->lexer);
33252   enum tree_code op;
33253   tree lhs, rhs;
33254   cp_id_kind idk;
33255   bool decl_first;
33256
33257   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33258     {
33259       op = (token->type == CPP_PLUS_PLUS
33260             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33261       cp_lexer_consume_token (parser->lexer);
33262       lhs = cp_parser_simple_cast_expression (parser);
33263       if (lhs != decl
33264           && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33265         return error_mark_node;
33266       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33267     }
33268
33269   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33270   if (lhs != decl
33271       && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33272     return error_mark_node;
33273
33274   token = cp_lexer_peek_token (parser->lexer);
33275   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33276     {
33277       op = (token->type == CPP_PLUS_PLUS
33278             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33279       cp_lexer_consume_token (parser->lexer);
33280       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33281     }
33282
33283   op = cp_parser_assignment_operator_opt (parser);
33284   if (op == ERROR_MARK)
33285     return error_mark_node;
33286
33287   if (op != NOP_EXPR)
33288     {
33289       rhs = cp_parser_assignment_expression (parser);
33290       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33291       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33292     }
33293
33294   lhs = cp_parser_binary_expression (parser, false, false,
33295                                      PREC_ADDITIVE_EXPRESSION, NULL);
33296   token = cp_lexer_peek_token (parser->lexer);
33297   decl_first = (lhs == decl
33298                 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33299   if (decl_first)
33300     lhs = NULL_TREE;
33301   if (token->type != CPP_PLUS
33302       && token->type != CPP_MINUS)
33303     return error_mark_node;
33304
33305   do
33306     {
33307       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33308       cp_lexer_consume_token (parser->lexer);
33309       rhs = cp_parser_binary_expression (parser, false, false,
33310                                          PREC_ADDITIVE_EXPRESSION, NULL);
33311       token = cp_lexer_peek_token (parser->lexer);
33312       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33313         {
33314           if (lhs == NULL_TREE)
33315             {
33316               if (op == PLUS_EXPR)
33317                 lhs = rhs;
33318               else
33319                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33320                                         tf_warning_or_error);
33321             }
33322           else
33323             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33324                                      ERROR_MARK, NULL, tf_warning_or_error);
33325         }
33326     }
33327   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33328
33329   if (!decl_first)
33330     {
33331       if ((rhs != decl
33332            && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33333           || op == MINUS_EXPR)
33334         return error_mark_node;
33335       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33336     }
33337   else
33338     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33339
33340   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33341 }
33342
33343 /* Parse the initialization statement of either an OpenMP for loop or
33344    a Cilk Plus for loop.
33345
33346    Return true if the resulting construct should have an
33347    OMP_CLAUSE_PRIVATE added to it.  */
33348
33349 static tree
33350 cp_parser_omp_for_loop_init (cp_parser *parser,
33351                              enum tree_code code,
33352                              tree &this_pre_body,
33353                              vec<tree, va_gc> *for_block,
33354                              tree &init,
33355                              tree &orig_init,
33356                              tree &decl,
33357                              tree &real_decl)
33358 {
33359   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33360     return NULL_TREE;
33361
33362   tree add_private_clause = NULL_TREE;
33363
33364   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33365
33366      init-expr:
33367      var = lb
33368      integer-type var = lb
33369      random-access-iterator-type var = lb
33370      pointer-type var = lb
33371   */
33372   cp_decl_specifier_seq type_specifiers;
33373
33374   /* First, try to parse as an initialized declaration.  See
33375      cp_parser_condition, from whence the bulk of this is copied.  */
33376
33377   cp_parser_parse_tentatively (parser);
33378   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33379                                 /*is_trailing_return=*/false,
33380                                 &type_specifiers);
33381   if (cp_parser_parse_definitely (parser))
33382     {
33383       /* If parsing a type specifier seq succeeded, then this
33384          MUST be a initialized declaration.  */
33385       tree asm_specification, attributes;
33386       cp_declarator *declarator;
33387
33388       declarator = cp_parser_declarator (parser,
33389                                          CP_PARSER_DECLARATOR_NAMED,
33390                                          /*ctor_dtor_or_conv_p=*/NULL,
33391                                          /*parenthesized_p=*/NULL,
33392                                          /*member_p=*/false,
33393                                          /*friend_p=*/false);
33394       attributes = cp_parser_attributes_opt (parser);
33395       asm_specification = cp_parser_asm_specification_opt (parser);
33396
33397       if (declarator == cp_error_declarator) 
33398         cp_parser_skip_to_end_of_statement (parser);
33399
33400       else 
33401         {
33402           tree pushed_scope, auto_node;
33403
33404           decl = start_decl (declarator, &type_specifiers,
33405                              SD_INITIALIZED, attributes,
33406                              /*prefix_attributes=*/NULL_TREE,
33407                              &pushed_scope);
33408
33409           auto_node = type_uses_auto (TREE_TYPE (decl));
33410           if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33411             {
33412               if (cp_lexer_next_token_is (parser->lexer, 
33413                                           CPP_OPEN_PAREN))
33414                 {
33415                   if (code != CILK_SIMD && code != CILK_FOR)
33416                     error ("parenthesized initialization is not allowed in "
33417                            "OpenMP %<for%> loop");
33418                   else
33419                     error ("parenthesized initialization is "
33420                            "not allowed in for-loop");
33421                 }
33422               else
33423                 /* Trigger an error.  */
33424                 cp_parser_require (parser, CPP_EQ, RT_EQ);
33425
33426               init = error_mark_node;
33427               cp_parser_skip_to_end_of_statement (parser);
33428             }
33429           else if (CLASS_TYPE_P (TREE_TYPE (decl))
33430                    || type_dependent_expression_p (decl)
33431                    || auto_node)
33432             {
33433               bool is_direct_init, is_non_constant_init;
33434
33435               init = cp_parser_initializer (parser,
33436                                             &is_direct_init,
33437                                             &is_non_constant_init);
33438
33439               if (auto_node)
33440                 {
33441                   TREE_TYPE (decl)
33442                     = do_auto_deduction (TREE_TYPE (decl), init,
33443                                          auto_node);
33444
33445                   if (!CLASS_TYPE_P (TREE_TYPE (decl))
33446                       && !type_dependent_expression_p (decl))
33447                     goto non_class;
33448                 }
33449                       
33450               cp_finish_decl (decl, init, !is_non_constant_init,
33451                               asm_specification,
33452                               LOOKUP_ONLYCONVERTING);
33453               orig_init = init;
33454               if (CLASS_TYPE_P (TREE_TYPE (decl)))
33455                 {
33456                   vec_safe_push (for_block, this_pre_body);
33457                   init = NULL_TREE;
33458                 }
33459               else
33460                 init = pop_stmt_list (this_pre_body);
33461               this_pre_body = NULL_TREE;
33462             }
33463           else
33464             {
33465               /* Consume '='.  */
33466               cp_lexer_consume_token (parser->lexer);
33467               init = cp_parser_assignment_expression (parser);
33468
33469             non_class:
33470               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33471                 init = error_mark_node;
33472               else
33473                 cp_finish_decl (decl, NULL_TREE,
33474                                 /*init_const_expr_p=*/false,
33475                                 asm_specification,
33476                                 LOOKUP_ONLYCONVERTING);
33477             }
33478
33479           if (pushed_scope)
33480             pop_scope (pushed_scope);
33481         }
33482     }
33483   else 
33484     {
33485       cp_id_kind idk;
33486       /* If parsing a type specifier sequence failed, then
33487          this MUST be a simple expression.  */
33488       if (code == CILK_FOR)
33489         error ("%<_Cilk_for%> allows expression instead of declaration only "
33490                "in C, not in C++");
33491       cp_parser_parse_tentatively (parser);
33492       decl = cp_parser_primary_expression (parser, false, false,
33493                                            false, &idk);
33494       cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33495       if (!cp_parser_error_occurred (parser)
33496           && decl
33497           && (TREE_CODE (decl) == COMPONENT_REF
33498               || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33499         {
33500           cp_parser_abort_tentative_parse (parser);
33501           cp_parser_parse_tentatively (parser);
33502           cp_token *token = cp_lexer_peek_token (parser->lexer);
33503           tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33504                                                /*check_dependency_p=*/true,
33505                                                /*template_p=*/NULL,
33506                                                /*declarator_p=*/false,
33507                                                /*optional_p=*/false);
33508           if (name != error_mark_node
33509               && last_tok == cp_lexer_peek_token (parser->lexer))
33510             {
33511               decl = cp_parser_lookup_name_simple (parser, name,
33512                                                    token->location);
33513               if (TREE_CODE (decl) == FIELD_DECL)
33514                 add_private_clause = omp_privatize_field (decl, false);
33515             }
33516           cp_parser_abort_tentative_parse (parser);
33517           cp_parser_parse_tentatively (parser);
33518           decl = cp_parser_primary_expression (parser, false, false,
33519                                                false, &idk);
33520         }
33521       if (!cp_parser_error_occurred (parser)
33522           && decl
33523           && DECL_P (decl)
33524           && CLASS_TYPE_P (TREE_TYPE (decl)))
33525         {
33526           tree rhs;
33527
33528           cp_parser_parse_definitely (parser);
33529           cp_parser_require (parser, CPP_EQ, RT_EQ);
33530           rhs = cp_parser_assignment_expression (parser);
33531           orig_init = rhs;
33532           finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33533                                                  decl, NOP_EXPR,
33534                                                  rhs,
33535                                                  tf_warning_or_error));
33536           if (!add_private_clause)
33537             add_private_clause = decl;
33538         }
33539       else
33540         {
33541           decl = NULL;
33542           cp_parser_abort_tentative_parse (parser);
33543           init = cp_parser_expression (parser);
33544           if (init)
33545             {
33546               if (TREE_CODE (init) == MODIFY_EXPR
33547                   || TREE_CODE (init) == MODOP_EXPR)
33548                 real_decl = TREE_OPERAND (init, 0);
33549             }
33550         }
33551     }
33552   return add_private_clause;
33553 }
33554
33555 /* Parse the restricted form of the for statement allowed by OpenMP.  */
33556
33557 static tree
33558 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33559                         tree *cclauses, bool *if_p)
33560 {
33561   tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33562   tree real_decl, initv, condv, incrv, declv;
33563   tree this_pre_body, cl, ordered_cl = NULL_TREE;
33564   location_t loc_first;
33565   bool collapse_err = false;
33566   int i, collapse = 1, ordered = 0, count, nbraces = 0;
33567   vec<tree, va_gc> *for_block = make_tree_vector ();
33568   auto_vec<tree, 4> orig_inits;
33569
33570   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33571     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33572       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33573     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33574              && OMP_CLAUSE_ORDERED_EXPR (cl))
33575       {
33576         ordered_cl = cl;
33577         ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33578       }
33579
33580   if (ordered && ordered < collapse)
33581     {
33582       error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33583                 "%<ordered%> clause parameter is less than %<collapse%>");
33584       OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33585         = build_int_cst (NULL_TREE, collapse);
33586       ordered = collapse;
33587     }
33588   if (ordered)
33589     {
33590       for (tree *pc = &clauses; *pc; )
33591         if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33592           {
33593             error_at (OMP_CLAUSE_LOCATION (*pc),
33594                       "%<linear%> clause may not be specified together "
33595                       "with %<ordered%> clause with a parameter");
33596             *pc = OMP_CLAUSE_CHAIN (*pc);
33597           }
33598         else
33599           pc = &OMP_CLAUSE_CHAIN (*pc);
33600     }
33601
33602   gcc_assert (collapse >= 1 && ordered >= 0);
33603   count = ordered ? ordered : collapse;
33604
33605   declv = make_tree_vec (count);
33606   initv = make_tree_vec (count);
33607   condv = make_tree_vec (count);
33608   incrv = make_tree_vec (count);
33609
33610   loc_first = cp_lexer_peek_token (parser->lexer)->location;
33611
33612   for (i = 0; i < count; i++)
33613     {
33614       int bracecount = 0;
33615       tree add_private_clause = NULL_TREE;
33616       location_t loc;
33617
33618       if (code != CILK_FOR
33619           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33620         {
33621           cp_parser_error (parser, "for statement expected");
33622           return NULL;
33623         }
33624       if (code == CILK_FOR
33625           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33626         {
33627           cp_parser_error (parser, "_Cilk_for statement expected");
33628           return NULL;
33629         }
33630       loc = cp_lexer_consume_token (parser->lexer)->location;
33631
33632       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33633         return NULL;
33634
33635       init = orig_init = decl = real_decl = NULL;
33636       this_pre_body = push_stmt_list ();
33637
33638       add_private_clause
33639         = cp_parser_omp_for_loop_init (parser, code,
33640                                        this_pre_body, for_block,
33641                                        init, orig_init, decl, real_decl);
33642
33643       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33644       if (this_pre_body)
33645         {
33646           this_pre_body = pop_stmt_list (this_pre_body);
33647           if (pre_body)
33648             {
33649               tree t = pre_body;
33650               pre_body = push_stmt_list ();
33651               add_stmt (t);
33652               add_stmt (this_pre_body);
33653               pre_body = pop_stmt_list (pre_body);
33654             }
33655           else
33656             pre_body = this_pre_body;
33657         }
33658
33659       if (decl)
33660         real_decl = decl;
33661       if (cclauses != NULL
33662           && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33663           && real_decl != NULL_TREE)
33664         {
33665           tree *c;
33666           for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33667             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33668                 && OMP_CLAUSE_DECL (*c) == real_decl)
33669               {
33670                 error_at (loc, "iteration variable %qD"
33671                           " should not be firstprivate", real_decl);
33672                 *c = OMP_CLAUSE_CHAIN (*c);
33673               }
33674             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33675                      && OMP_CLAUSE_DECL (*c) == real_decl)
33676               {
33677                 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
33678                 tree l = *c;
33679                 *c = OMP_CLAUSE_CHAIN (*c);
33680                 if (code == OMP_SIMD)
33681                   {
33682                     OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33683                     cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33684                   }
33685                 else
33686                   {
33687                     OMP_CLAUSE_CHAIN (l) = clauses;
33688                     clauses = l;
33689                   }
33690                 add_private_clause = NULL_TREE;
33691               }
33692             else
33693               {
33694                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33695                     && OMP_CLAUSE_DECL (*c) == real_decl)
33696                   add_private_clause = NULL_TREE;
33697                 c = &OMP_CLAUSE_CHAIN (*c);
33698               }
33699         }
33700
33701       if (add_private_clause)
33702         {
33703           tree c;
33704           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33705             {
33706               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33707                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33708                   && OMP_CLAUSE_DECL (c) == decl)
33709                 break;
33710               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33711                        && OMP_CLAUSE_DECL (c) == decl)
33712                 error_at (loc, "iteration variable %qD "
33713                           "should not be firstprivate",
33714                           decl);
33715               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33716                        && OMP_CLAUSE_DECL (c) == decl)
33717                 error_at (loc, "iteration variable %qD should not be reduction",
33718                           decl);
33719             }
33720           if (c == NULL)
33721             {
33722               if (code != OMP_SIMD)
33723                 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33724               else if (collapse == 1)
33725                 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33726               else
33727                 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33728               OMP_CLAUSE_DECL (c) = add_private_clause;
33729               c = finish_omp_clauses (c, true);
33730               if (c)
33731                 {
33732                   OMP_CLAUSE_CHAIN (c) = clauses;
33733                   clauses = c;
33734                   /* For linear, signal that we need to fill up
33735                      the so far unknown linear step.  */
33736                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33737                     OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33738                 }
33739             }
33740         }
33741
33742       cond = NULL;
33743       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33744         cond = cp_parser_omp_for_cond (parser, decl, code);
33745       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33746
33747       incr = NULL;
33748       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33749         {
33750           /* If decl is an iterator, preserve the operator on decl
33751              until finish_omp_for.  */
33752           if (real_decl
33753               && ((processing_template_decl
33754                    && (TREE_TYPE (real_decl) == NULL_TREE
33755                        || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33756                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33757             incr = cp_parser_omp_for_incr (parser, real_decl);
33758           else
33759             incr = cp_parser_expression (parser);
33760           if (!EXPR_HAS_LOCATION (incr))
33761             protected_set_expr_location (incr, input_location);
33762         }
33763
33764       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33765         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33766                                                /*or_comma=*/false,
33767                                                /*consume_paren=*/true);
33768
33769       TREE_VEC_ELT (declv, i) = decl;
33770       TREE_VEC_ELT (initv, i) = init;
33771       TREE_VEC_ELT (condv, i) = cond;
33772       TREE_VEC_ELT (incrv, i) = incr;
33773       if (orig_init)
33774         {
33775           orig_inits.safe_grow_cleared (i + 1);
33776           orig_inits[i] = orig_init;
33777         }
33778
33779       if (i == count - 1)
33780         break;
33781
33782       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33783          in between the collapsed for loops to be still considered perfectly
33784          nested.  Hopefully the final version clarifies this.
33785          For now handle (multiple) {'s and empty statements.  */
33786       cp_parser_parse_tentatively (parser);
33787       do
33788         {
33789           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33790             break;
33791           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33792             {
33793               cp_lexer_consume_token (parser->lexer);
33794               bracecount++;
33795             }
33796           else if (bracecount
33797                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33798             cp_lexer_consume_token (parser->lexer);
33799           else
33800             {
33801               loc = cp_lexer_peek_token (parser->lexer)->location;
33802               error_at (loc, "not enough collapsed for loops");
33803               collapse_err = true;
33804               cp_parser_abort_tentative_parse (parser);
33805               declv = NULL_TREE;
33806               break;
33807             }
33808         }
33809       while (1);
33810
33811       if (declv)
33812         {
33813           cp_parser_parse_definitely (parser);
33814           nbraces += bracecount;
33815         }
33816     }
33817
33818   if (nbraces)
33819     if_p = NULL;
33820
33821   /* Note that we saved the original contents of this flag when we entered
33822      the structured block, and so we don't need to re-save it here.  */
33823   if (code == CILK_SIMD || code == CILK_FOR)
33824     parser->in_statement = IN_CILK_SIMD_FOR;
33825   else
33826     parser->in_statement = IN_OMP_FOR;
33827
33828   /* Note that the grammar doesn't call for a structured block here,
33829      though the loop as a whole is a structured block.  */
33830   body = push_stmt_list ();
33831   cp_parser_statement (parser, NULL_TREE, false, if_p);
33832   body = pop_stmt_list (body);
33833
33834   if (declv == NULL_TREE)
33835     ret = NULL_TREE;
33836   else
33837     ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33838                           body, pre_body, &orig_inits, clauses);
33839
33840   while (nbraces)
33841     {
33842       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33843         {
33844           cp_lexer_consume_token (parser->lexer);
33845           nbraces--;
33846         }
33847       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33848         cp_lexer_consume_token (parser->lexer);
33849       else
33850         {
33851           if (!collapse_err)
33852             {
33853               error_at (cp_lexer_peek_token (parser->lexer)->location,
33854                         "collapsed loops not perfectly nested");
33855             }
33856           collapse_err = true;
33857           cp_parser_statement_seq_opt (parser, NULL);
33858           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33859             break;
33860         }
33861     }
33862
33863   while (!for_block->is_empty ())
33864     add_stmt (pop_stmt_list (for_block->pop ()));
33865   release_tree_vector (for_block);
33866
33867   return ret;
33868 }
33869
33870 /* Helper function for OpenMP parsing, split clauses and call
33871    finish_omp_clauses on each of the set of clauses afterwards.  */
33872
33873 static void
33874 cp_omp_split_clauses (location_t loc, enum tree_code code,
33875                       omp_clause_mask mask, tree clauses, tree *cclauses)
33876 {
33877   int i;
33878   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33879   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33880     if (cclauses[i])
33881       cclauses[i] = finish_omp_clauses (cclauses[i], true);
33882 }
33883
33884 /* OpenMP 4.0:
33885    #pragma omp simd simd-clause[optseq] new-line
33886      for-loop  */
33887
33888 #define OMP_SIMD_CLAUSE_MASK                                    \
33889         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)      \
33890         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
33891         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
33892         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
33893         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
33894         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
33895         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
33896         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33897
33898 static tree
33899 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33900                     char *p_name, omp_clause_mask mask, tree *cclauses,
33901                     bool *if_p)
33902 {
33903   tree clauses, sb, ret;
33904   unsigned int save;
33905   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33906
33907   strcat (p_name, " simd");
33908   mask |= OMP_SIMD_CLAUSE_MASK;
33909
33910   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33911                                        cclauses == NULL);
33912   if (cclauses)
33913     {
33914       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33915       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33916       tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33917                                 OMP_CLAUSE_ORDERED);
33918       if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33919         {
33920           error_at (OMP_CLAUSE_LOCATION (c),
33921                     "%<ordered%> clause with parameter may not be specified "
33922                     "on %qs construct", p_name);
33923           OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33924         }
33925     }
33926
33927   sb = begin_omp_structured_block ();
33928   save = cp_parser_begin_omp_structured_block (parser);
33929
33930   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33931
33932   cp_parser_end_omp_structured_block (parser, save);
33933   add_stmt (finish_omp_structured_block (sb));
33934
33935   return ret;
33936 }
33937
33938 /* OpenMP 2.5:
33939    #pragma omp for for-clause[optseq] new-line
33940      for-loop
33941
33942    OpenMP 4.0:
33943    #pragma omp for simd for-simd-clause[optseq] new-line
33944      for-loop  */
33945
33946 #define OMP_FOR_CLAUSE_MASK                                     \
33947         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
33948         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33949         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
33950         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
33951         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
33952         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)      \
33953         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)     \
33954         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
33955         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33956
33957 static tree
33958 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33959                    char *p_name, omp_clause_mask mask, tree *cclauses,
33960                    bool *if_p)
33961 {
33962   tree clauses, sb, ret;
33963   unsigned int save;
33964   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33965
33966   strcat (p_name, " for");
33967   mask |= OMP_FOR_CLAUSE_MASK;
33968   /* parallel for{, simd} disallows nowait clause, but for
33969      target {teams distribute ,}parallel for{, simd} it should be accepted.  */
33970   if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
33971     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33972   /* Composite distribute parallel for{, simd} disallows ordered clause.  */
33973   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33974     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33975
33976   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33977     {
33978       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33979       const char *p = IDENTIFIER_POINTER (id);
33980
33981       if (strcmp (p, "simd") == 0)
33982         {
33983           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33984           if (cclauses == NULL)
33985             cclauses = cclauses_buf;
33986
33987           cp_lexer_consume_token (parser->lexer);
33988           if (!flag_openmp)  /* flag_openmp_simd  */
33989             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33990                                        cclauses, if_p);
33991           sb = begin_omp_structured_block ();
33992           save = cp_parser_begin_omp_structured_block (parser);
33993           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33994                                     cclauses, if_p);
33995           cp_parser_end_omp_structured_block (parser, save);
33996           tree body = finish_omp_structured_block (sb);
33997           if (ret == NULL)
33998             return ret;
33999           ret = make_node (OMP_FOR);
34000           TREE_TYPE (ret) = void_type_node;
34001           OMP_FOR_BODY (ret) = body;
34002           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34003           SET_EXPR_LOCATION (ret, loc);
34004           add_stmt (ret);
34005           return ret;
34006         }
34007     }
34008   if (!flag_openmp)  /* flag_openmp_simd  */
34009     {
34010       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34011       return NULL_TREE;
34012     }
34013
34014   /* Composite distribute parallel for disallows linear clause.  */
34015   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34016     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34017
34018   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34019                                        cclauses == NULL);
34020   if (cclauses)
34021     {
34022       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34023       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34024     }
34025
34026   sb = begin_omp_structured_block ();
34027   save = cp_parser_begin_omp_structured_block (parser);
34028
34029   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34030
34031   cp_parser_end_omp_structured_block (parser, save);
34032   add_stmt (finish_omp_structured_block (sb));
34033
34034   return ret;
34035 }
34036
34037 /* OpenMP 2.5:
34038    # pragma omp master new-line
34039      structured-block  */
34040
34041 static tree
34042 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34043 {
34044   cp_parser_require_pragma_eol (parser, pragma_tok);
34045   return c_finish_omp_master (input_location,
34046                               cp_parser_omp_structured_block (parser, if_p));
34047 }
34048
34049 /* OpenMP 2.5:
34050    # pragma omp ordered new-line
34051      structured-block
34052
34053    OpenMP 4.5:
34054    # pragma omp ordered ordered-clauses new-line
34055      structured-block  */
34056
34057 #define OMP_ORDERED_CLAUSE_MASK                                 \
34058         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)      \
34059         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34060
34061 #define OMP_ORDERED_DEPEND_CLAUSE_MASK                          \
34062         (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34063
34064 static bool
34065 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34066                        enum pragma_context context, bool *if_p)
34067 {
34068   location_t loc = pragma_tok->location;
34069
34070   if (context != pragma_stmt && context != pragma_compound)
34071     {
34072       cp_parser_error (parser, "expected declaration specifiers");
34073       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34074       return false;
34075     }
34076
34077   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34078     {
34079       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34080       const char *p = IDENTIFIER_POINTER (id);
34081
34082       if (strcmp (p, "depend") == 0)
34083         {
34084           if (context == pragma_stmt)
34085             {
34086               error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34087                         "%<depend%> clause may only be used in compound "
34088                         "statements");
34089               cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34090               return false;
34091             }
34092           tree clauses
34093             = cp_parser_omp_all_clauses (parser,
34094                                          OMP_ORDERED_DEPEND_CLAUSE_MASK,
34095                                          "#pragma omp ordered", pragma_tok);
34096           c_finish_omp_ordered (loc, clauses, NULL_TREE);
34097           return false;
34098         }
34099     }
34100
34101   tree clauses
34102     = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34103                                  "#pragma omp ordered", pragma_tok);
34104   c_finish_omp_ordered (loc, clauses,
34105                         cp_parser_omp_structured_block (parser, if_p));
34106   return true;
34107 }
34108
34109 /* OpenMP 2.5:
34110
34111    section-scope:
34112      { section-sequence }
34113
34114    section-sequence:
34115      section-directive[opt] structured-block
34116      section-sequence section-directive structured-block  */
34117
34118 static tree
34119 cp_parser_omp_sections_scope (cp_parser *parser)
34120 {
34121   tree stmt, substmt;
34122   bool error_suppress = false;
34123   cp_token *tok;
34124
34125   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34126     return NULL_TREE;
34127
34128   stmt = push_stmt_list ();
34129
34130   if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34131       != PRAGMA_OMP_SECTION)
34132     {
34133       substmt = cp_parser_omp_structured_block (parser, NULL);
34134       substmt = build1 (OMP_SECTION, void_type_node, substmt);
34135       add_stmt (substmt);
34136     }
34137
34138   while (1)
34139     {
34140       tok = cp_lexer_peek_token (parser->lexer);
34141       if (tok->type == CPP_CLOSE_BRACE)
34142         break;
34143       if (tok->type == CPP_EOF)
34144         break;
34145
34146       if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34147         {
34148           cp_lexer_consume_token (parser->lexer);
34149           cp_parser_require_pragma_eol (parser, tok);
34150           error_suppress = false;
34151         }
34152       else if (!error_suppress)
34153         {
34154           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34155           error_suppress = true;
34156         }
34157
34158       substmt = cp_parser_omp_structured_block (parser, NULL);
34159       substmt = build1 (OMP_SECTION, void_type_node, substmt);
34160       add_stmt (substmt);
34161     }
34162   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34163
34164   substmt = pop_stmt_list (stmt);
34165
34166   stmt = make_node (OMP_SECTIONS);
34167   TREE_TYPE (stmt) = void_type_node;
34168   OMP_SECTIONS_BODY (stmt) = substmt;
34169
34170   add_stmt (stmt);
34171   return stmt;
34172 }
34173
34174 /* OpenMP 2.5:
34175    # pragma omp sections sections-clause[optseq] newline
34176      sections-scope  */
34177
34178 #define OMP_SECTIONS_CLAUSE_MASK                                \
34179         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34180         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34181         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
34182         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
34183         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34184
34185 static tree
34186 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34187                         char *p_name, omp_clause_mask mask, tree *cclauses)
34188 {
34189   tree clauses, ret;
34190   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34191
34192   strcat (p_name, " sections");
34193   mask |= OMP_SECTIONS_CLAUSE_MASK;
34194   if (cclauses)
34195     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34196
34197   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34198                                        cclauses == NULL);
34199   if (cclauses)
34200     {
34201       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34202       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34203     }
34204
34205   ret = cp_parser_omp_sections_scope (parser);
34206   if (ret)
34207     OMP_SECTIONS_CLAUSES (ret) = clauses;
34208
34209   return ret;
34210 }
34211
34212 /* OpenMP 2.5:
34213    # pragma omp parallel parallel-clause[optseq] new-line
34214      structured-block
34215    # pragma omp parallel for parallel-for-clause[optseq] new-line
34216      structured-block
34217    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34218      structured-block
34219
34220    OpenMP 4.0:
34221    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34222      structured-block */
34223
34224 #define OMP_PARALLEL_CLAUSE_MASK                                \
34225         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34226         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34227         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34228         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
34229         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
34230         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)       \
34231         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
34232         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)  \
34233         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34234
34235 static tree
34236 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34237                         char *p_name, omp_clause_mask mask, tree *cclauses,
34238                         bool *if_p)
34239 {
34240   tree stmt, clauses, block;
34241   unsigned int save;
34242   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34243
34244   strcat (p_name, " parallel");
34245   mask |= OMP_PARALLEL_CLAUSE_MASK;
34246   /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
34247   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34248       && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34249     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34250
34251   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34252     {
34253       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34254       if (cclauses == NULL)
34255         cclauses = cclauses_buf;
34256
34257       cp_lexer_consume_token (parser->lexer);
34258       if (!flag_openmp)  /* flag_openmp_simd  */
34259         return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34260                                   if_p);
34261       block = begin_omp_parallel ();
34262       save = cp_parser_begin_omp_structured_block (parser);
34263       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34264                                     if_p);
34265       cp_parser_end_omp_structured_block (parser, save);
34266       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34267                                   block);
34268       if (ret == NULL_TREE)
34269         return ret;
34270       OMP_PARALLEL_COMBINED (stmt) = 1;
34271       return stmt;
34272     }
34273   /* When combined with distribute, parallel has to be followed by for.
34274      #pragma omp target parallel is allowed though.  */
34275   else if (cclauses
34276            && (mask & (OMP_CLAUSE_MASK_1
34277                        << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34278     {
34279       error_at (loc, "expected %<for%> after %qs", p_name);
34280       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34281       return NULL_TREE;
34282     }
34283   else if (!flag_openmp)  /* flag_openmp_simd  */
34284     {
34285       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34286       return NULL_TREE;
34287     }
34288   else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34289     {
34290       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34291       const char *p = IDENTIFIER_POINTER (id);
34292       if (strcmp (p, "sections") == 0)
34293         {
34294           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34295           cclauses = cclauses_buf;
34296
34297           cp_lexer_consume_token (parser->lexer);
34298           block = begin_omp_parallel ();
34299           save = cp_parser_begin_omp_structured_block (parser);
34300           cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34301           cp_parser_end_omp_structured_block (parser, save);
34302           stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34303                                       block);
34304           OMP_PARALLEL_COMBINED (stmt) = 1;
34305           return stmt;
34306         }
34307     }
34308
34309   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34310                                        cclauses == NULL);
34311   if (cclauses)
34312     {
34313       cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34314       clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34315     }
34316
34317   block = begin_omp_parallel ();
34318   save = cp_parser_begin_omp_structured_block (parser);
34319   cp_parser_statement (parser, NULL_TREE, false, if_p);
34320   cp_parser_end_omp_structured_block (parser, save);
34321   stmt = finish_omp_parallel (clauses, block);
34322   return stmt;
34323 }
34324
34325 /* OpenMP 2.5:
34326    # pragma omp single single-clause[optseq] new-line
34327      structured-block  */
34328
34329 #define OMP_SINGLE_CLAUSE_MASK                                  \
34330         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34331         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34332         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)  \
34333         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34334
34335 static tree
34336 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34337 {
34338   tree stmt = make_node (OMP_SINGLE);
34339   TREE_TYPE (stmt) = void_type_node;
34340
34341   OMP_SINGLE_CLAUSES (stmt)
34342     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34343                                  "#pragma omp single", pragma_tok);
34344   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34345
34346   return add_stmt (stmt);
34347 }
34348
34349 /* OpenMP 3.0:
34350    # pragma omp task task-clause[optseq] new-line
34351      structured-block  */
34352
34353 #define OMP_TASK_CLAUSE_MASK                                    \
34354         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34355         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
34356         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
34357         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34358         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34359         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
34360         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
34361         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
34362         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34363         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34364
34365 static tree
34366 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34367 {
34368   tree clauses, block;
34369   unsigned int save;
34370
34371   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34372                                        "#pragma omp task", pragma_tok);
34373   block = begin_omp_task ();
34374   save = cp_parser_begin_omp_structured_block (parser);
34375   cp_parser_statement (parser, NULL_TREE, false, if_p);
34376   cp_parser_end_omp_structured_block (parser, save);
34377   return finish_omp_task (clauses, block);
34378 }
34379
34380 /* OpenMP 3.0:
34381    # pragma omp taskwait new-line  */
34382
34383 static void
34384 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34385 {
34386   cp_parser_require_pragma_eol (parser, pragma_tok);
34387   finish_omp_taskwait ();
34388 }
34389
34390 /* OpenMP 3.1:
34391    # pragma omp taskyield new-line  */
34392
34393 static void
34394 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34395 {
34396   cp_parser_require_pragma_eol (parser, pragma_tok);
34397   finish_omp_taskyield ();
34398 }
34399
34400 /* OpenMP 4.0:
34401    # pragma omp taskgroup new-line
34402      structured-block  */
34403
34404 static tree
34405 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34406 {
34407   cp_parser_require_pragma_eol (parser, pragma_tok);
34408   return c_finish_omp_taskgroup (input_location,
34409                                  cp_parser_omp_structured_block (parser,
34410                                                                  if_p));
34411 }
34412
34413
34414 /* OpenMP 2.5:
34415    # pragma omp threadprivate (variable-list) */
34416
34417 static void
34418 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34419 {
34420   tree vars;
34421
34422   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34423   cp_parser_require_pragma_eol (parser, pragma_tok);
34424
34425   finish_omp_threadprivate (vars);
34426 }
34427
34428 /* OpenMP 4.0:
34429    # pragma omp cancel cancel-clause[optseq] new-line  */
34430
34431 #define OMP_CANCEL_CLAUSE_MASK                                  \
34432         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
34433         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
34434         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
34435         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)    \
34436         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34437
34438 static void
34439 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34440 {
34441   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34442                                             "#pragma omp cancel", pragma_tok);
34443   finish_omp_cancel (clauses);
34444 }
34445
34446 /* OpenMP 4.0:
34447    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
34448
34449 #define OMP_CANCELLATION_POINT_CLAUSE_MASK                      \
34450         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
34451         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
34452         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
34453         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34454
34455 static void
34456 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34457 {
34458   tree clauses;
34459   bool point_seen = false;
34460
34461   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34462     {
34463       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34464       const char *p = IDENTIFIER_POINTER (id);
34465
34466       if (strcmp (p, "point") == 0)
34467         {
34468           cp_lexer_consume_token (parser->lexer);
34469           point_seen = true;
34470         }
34471     }
34472   if (!point_seen)
34473     {
34474       cp_parser_error (parser, "expected %<point%>");
34475       cp_parser_require_pragma_eol (parser, pragma_tok);
34476       return;
34477     }
34478
34479   clauses = cp_parser_omp_all_clauses (parser,
34480                                        OMP_CANCELLATION_POINT_CLAUSE_MASK,
34481                                        "#pragma omp cancellation point",
34482                                        pragma_tok);
34483   finish_omp_cancellation_point (clauses);
34484 }
34485
34486 /* OpenMP 4.0:
34487    #pragma omp distribute distribute-clause[optseq] new-line
34488      for-loop  */
34489
34490 #define OMP_DISTRIBUTE_CLAUSE_MASK                              \
34491         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34492         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34493         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
34494         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34495         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34496
34497 static tree
34498 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34499                           char *p_name, omp_clause_mask mask, tree *cclauses,
34500                           bool *if_p)
34501 {
34502   tree clauses, sb, ret;
34503   unsigned int save;
34504   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34505
34506   strcat (p_name, " distribute");
34507   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34508
34509   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34510     {
34511       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34512       const char *p = IDENTIFIER_POINTER (id);
34513       bool simd = false;
34514       bool parallel = false;
34515
34516       if (strcmp (p, "simd") == 0)
34517         simd = true;
34518       else
34519         parallel = strcmp (p, "parallel") == 0;
34520       if (parallel || simd)
34521         {
34522           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34523           if (cclauses == NULL)
34524             cclauses = cclauses_buf;
34525           cp_lexer_consume_token (parser->lexer);
34526           if (!flag_openmp)  /* flag_openmp_simd  */
34527             {
34528               if (simd)
34529                 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34530                                            cclauses, if_p);
34531               else
34532                 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34533                                                cclauses, if_p);
34534             }
34535           sb = begin_omp_structured_block ();
34536           save = cp_parser_begin_omp_structured_block (parser);
34537           if (simd)
34538             ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34539                                       cclauses, if_p);
34540           else
34541             ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34542                                           cclauses, if_p);
34543           cp_parser_end_omp_structured_block (parser, save);
34544           tree body = finish_omp_structured_block (sb);
34545           if (ret == NULL)
34546             return ret;
34547           ret = make_node (OMP_DISTRIBUTE);
34548           TREE_TYPE (ret) = void_type_node;
34549           OMP_FOR_BODY (ret) = body;
34550           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34551           SET_EXPR_LOCATION (ret, loc);
34552           add_stmt (ret);
34553           return ret;
34554         }
34555     }
34556   if (!flag_openmp)  /* flag_openmp_simd  */
34557     {
34558       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34559       return NULL_TREE;
34560     }
34561
34562   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34563                                        cclauses == NULL);
34564   if (cclauses)
34565     {
34566       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34567       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34568     }
34569
34570   sb = begin_omp_structured_block ();
34571   save = cp_parser_begin_omp_structured_block (parser);
34572
34573   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34574
34575   cp_parser_end_omp_structured_block (parser, save);
34576   add_stmt (finish_omp_structured_block (sb));
34577
34578   return ret;
34579 }
34580
34581 /* OpenMP 4.0:
34582    # pragma omp teams teams-clause[optseq] new-line
34583      structured-block  */
34584
34585 #define OMP_TEAMS_CLAUSE_MASK                                   \
34586         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34587         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34588         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
34589         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
34590         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)    \
34591         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34592         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34593
34594 static tree
34595 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34596                      char *p_name, omp_clause_mask mask, tree *cclauses,
34597                      bool *if_p)
34598 {
34599   tree clauses, sb, ret;
34600   unsigned int save;
34601   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34602
34603   strcat (p_name, " teams");
34604   mask |= OMP_TEAMS_CLAUSE_MASK;
34605
34606   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34607     {
34608       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34609       const char *p = IDENTIFIER_POINTER (id);
34610       if (strcmp (p, "distribute") == 0)
34611         {
34612           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34613           if (cclauses == NULL)
34614             cclauses = cclauses_buf;
34615
34616           cp_lexer_consume_token (parser->lexer);
34617           if (!flag_openmp)  /* flag_openmp_simd  */
34618             return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34619                                              cclauses, if_p);
34620           sb = begin_omp_structured_block ();
34621           save = cp_parser_begin_omp_structured_block (parser);
34622           ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34623                                           cclauses, if_p);
34624           cp_parser_end_omp_structured_block (parser, save);
34625           tree body = finish_omp_structured_block (sb);
34626           if (ret == NULL)
34627             return ret;
34628           clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34629           ret = make_node (OMP_TEAMS);
34630           TREE_TYPE (ret) = void_type_node;
34631           OMP_TEAMS_CLAUSES (ret) = clauses;
34632           OMP_TEAMS_BODY (ret) = body;
34633           OMP_TEAMS_COMBINED (ret) = 1;
34634           return add_stmt (ret);
34635         }
34636     }
34637   if (!flag_openmp)  /* flag_openmp_simd  */
34638     {
34639       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34640       return NULL_TREE;
34641     }
34642
34643   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34644                                        cclauses == NULL);
34645   if (cclauses)
34646     {
34647       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34648       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34649     }
34650
34651   tree stmt = make_node (OMP_TEAMS);
34652   TREE_TYPE (stmt) = void_type_node;
34653   OMP_TEAMS_CLAUSES (stmt) = clauses;
34654   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34655
34656   return add_stmt (stmt);
34657 }
34658
34659 /* OpenMP 4.0:
34660    # pragma omp target data target-data-clause[optseq] new-line
34661      structured-block  */
34662
34663 #define OMP_TARGET_DATA_CLAUSE_MASK                             \
34664         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34665         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34666         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34667         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34668
34669 static tree
34670 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34671 {
34672   tree clauses
34673     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34674                                  "#pragma omp target data", pragma_tok);
34675   int map_seen = 0;
34676   for (tree *pc = &clauses; *pc;)
34677     {
34678       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34679         switch (OMP_CLAUSE_MAP_KIND (*pc))
34680           {
34681           case GOMP_MAP_TO:
34682           case GOMP_MAP_ALWAYS_TO:
34683           case GOMP_MAP_FROM:
34684           case GOMP_MAP_ALWAYS_FROM:
34685           case GOMP_MAP_TOFROM:
34686           case GOMP_MAP_ALWAYS_TOFROM:
34687           case GOMP_MAP_ALLOC:
34688             map_seen = 3;
34689             break;
34690           case GOMP_MAP_FIRSTPRIVATE_POINTER:
34691           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34692           case GOMP_MAP_ALWAYS_POINTER:
34693             break;
34694           default:
34695             map_seen |= 1;
34696             error_at (OMP_CLAUSE_LOCATION (*pc),
34697                       "%<#pragma omp target data%> with map-type other "
34698                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34699                       "on %<map%> clause");
34700             *pc = OMP_CLAUSE_CHAIN (*pc);
34701             continue;
34702           }
34703       pc = &OMP_CLAUSE_CHAIN (*pc);
34704     }
34705
34706   if (map_seen != 3)
34707     {
34708       if (map_seen == 0)
34709         error_at (pragma_tok->location,
34710                   "%<#pragma omp target data%> must contain at least "
34711                   "one %<map%> clause");
34712       return NULL_TREE;
34713     }
34714
34715   tree stmt = make_node (OMP_TARGET_DATA);
34716   TREE_TYPE (stmt) = void_type_node;
34717   OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34718
34719   keep_next_level (true);
34720   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34721
34722   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34723   return add_stmt (stmt);
34724 }
34725
34726 /* OpenMP 4.5:
34727    # pragma omp target enter data target-enter-data-clause[optseq] new-line
34728      structured-block  */
34729
34730 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK                       \
34731         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34732         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34733         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34734         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34735         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34736
34737 static tree
34738 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34739                                  enum pragma_context context)
34740 {
34741   bool data_seen = false;
34742   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34743     {
34744       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34745       const char *p = IDENTIFIER_POINTER (id);
34746
34747       if (strcmp (p, "data") == 0)
34748         {
34749           cp_lexer_consume_token (parser->lexer);
34750           data_seen = true;
34751         }
34752     }
34753   if (!data_seen)
34754     {
34755       cp_parser_error (parser, "expected %<data%>");
34756       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34757       return NULL_TREE;
34758     }
34759
34760   if (context == pragma_stmt)
34761     {
34762       error_at (pragma_tok->location,
34763                 "%<#pragma omp target enter data%> may only be "
34764                 "used in compound statements");
34765       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34766       return NULL_TREE;
34767     }
34768
34769   tree clauses
34770     = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34771                                  "#pragma omp target enter data", pragma_tok);
34772   int map_seen = 0;
34773   for (tree *pc = &clauses; *pc;)
34774     {
34775       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34776         switch (OMP_CLAUSE_MAP_KIND (*pc))
34777           {
34778           case GOMP_MAP_TO:
34779           case GOMP_MAP_ALWAYS_TO:
34780           case GOMP_MAP_ALLOC:
34781             map_seen = 3;
34782             break;
34783           case GOMP_MAP_FIRSTPRIVATE_POINTER:
34784           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34785           case GOMP_MAP_ALWAYS_POINTER:
34786             break;
34787           default:
34788             map_seen |= 1;
34789             error_at (OMP_CLAUSE_LOCATION (*pc),
34790                       "%<#pragma omp target enter data%> with map-type other "
34791                       "than %<to%> or %<alloc%> on %<map%> clause");
34792             *pc = OMP_CLAUSE_CHAIN (*pc);
34793             continue;
34794           }
34795       pc = &OMP_CLAUSE_CHAIN (*pc);
34796     }
34797
34798   if (map_seen != 3)
34799     {
34800       if (map_seen == 0)
34801         error_at (pragma_tok->location,
34802                   "%<#pragma omp target enter data%> must contain at least "
34803                   "one %<map%> clause");
34804       return NULL_TREE;
34805     }
34806
34807   tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34808   TREE_TYPE (stmt) = void_type_node;
34809   OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34810   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34811   return add_stmt (stmt);
34812 }
34813
34814 /* OpenMP 4.5:
34815    # pragma omp target exit data target-enter-data-clause[optseq] new-line
34816      structured-block  */
34817
34818 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK                        \
34819         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34820         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34821         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34822         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34823         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34824
34825 static tree
34826 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34827                                 enum pragma_context context)
34828 {
34829   bool data_seen = false;
34830   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34831     {
34832       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34833       const char *p = IDENTIFIER_POINTER (id);
34834
34835       if (strcmp (p, "data") == 0)
34836         {
34837           cp_lexer_consume_token (parser->lexer);
34838           data_seen = true;
34839         }
34840     }
34841   if (!data_seen)
34842     {
34843       cp_parser_error (parser, "expected %<data%>");
34844       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34845       return NULL_TREE;
34846     }
34847
34848   if (context == pragma_stmt)
34849     {
34850       error_at (pragma_tok->location,
34851                 "%<#pragma omp target exit data%> may only be "
34852                 "used in compound statements");
34853       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34854       return NULL_TREE;
34855     }
34856
34857   tree clauses
34858     = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34859                                  "#pragma omp target exit data", pragma_tok);
34860   int map_seen = 0;
34861   for (tree *pc = &clauses; *pc;)
34862     {
34863       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34864         switch (OMP_CLAUSE_MAP_KIND (*pc))
34865           {
34866           case GOMP_MAP_FROM:
34867           case GOMP_MAP_ALWAYS_FROM:
34868           case GOMP_MAP_RELEASE:
34869           case GOMP_MAP_DELETE:
34870             map_seen = 3;
34871             break;
34872           case GOMP_MAP_FIRSTPRIVATE_POINTER:
34873           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34874           case GOMP_MAP_ALWAYS_POINTER:
34875             break;
34876           default:
34877             map_seen |= 1;
34878             error_at (OMP_CLAUSE_LOCATION (*pc),
34879                       "%<#pragma omp target exit data%> with map-type other "
34880                       "than %<from%>, %<release%> or %<delete%> on %<map%>"
34881                       " clause");
34882             *pc = OMP_CLAUSE_CHAIN (*pc);
34883             continue;
34884           }
34885       pc = &OMP_CLAUSE_CHAIN (*pc);
34886     }
34887
34888   if (map_seen != 3)
34889     {
34890       if (map_seen == 0)
34891         error_at (pragma_tok->location,
34892                   "%<#pragma omp target exit data%> must contain at least "
34893                   "one %<map%> clause");
34894       return NULL_TREE;
34895     }
34896
34897   tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34898   TREE_TYPE (stmt) = void_type_node;
34899   OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34900   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34901   return add_stmt (stmt);
34902 }
34903
34904 /* OpenMP 4.0:
34905    # pragma omp target update target-update-clause[optseq] new-line */
34906
34907 #define OMP_TARGET_UPDATE_CLAUSE_MASK                           \
34908         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)         \
34909         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
34910         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34911         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34912         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34913         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34914
34915 static bool
34916 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34917                              enum pragma_context context)
34918 {
34919   if (context == pragma_stmt)
34920     {
34921       error_at (pragma_tok->location,
34922                 "%<#pragma omp target update%> may only be "
34923                 "used in compound statements");
34924       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34925       return false;
34926     }
34927
34928   tree clauses
34929     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34930                                  "#pragma omp target update", pragma_tok);
34931   if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34932       && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34933     {
34934       error_at (pragma_tok->location,
34935                 "%<#pragma omp target update%> must contain at least one "
34936                 "%<from%> or %<to%> clauses");
34937       return false;
34938     }
34939
34940   tree stmt = make_node (OMP_TARGET_UPDATE);
34941   TREE_TYPE (stmt) = void_type_node;
34942   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34943   SET_EXPR_LOCATION (stmt, pragma_tok->location);
34944   add_stmt (stmt);
34945   return false;
34946 }
34947
34948 /* OpenMP 4.0:
34949    # pragma omp target target-clause[optseq] new-line
34950      structured-block  */
34951
34952 #define OMP_TARGET_CLAUSE_MASK                                  \
34953         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
34954         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
34955         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
34956         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
34957         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
34958         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
34959         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34960         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)   \
34961         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34962
34963 static bool
34964 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34965                       enum pragma_context context, bool *if_p)
34966 {
34967   tree *pc = NULL, stmt;
34968
34969   if (context != pragma_stmt && context != pragma_compound)
34970     {
34971       cp_parser_error (parser, "expected declaration specifiers");
34972       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34973       return false;
34974     }
34975
34976   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34977     {
34978       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34979       const char *p = IDENTIFIER_POINTER (id);
34980       enum tree_code ccode = ERROR_MARK;
34981
34982       if (strcmp (p, "teams") == 0)
34983         ccode = OMP_TEAMS;
34984       else if (strcmp (p, "parallel") == 0)
34985         ccode = OMP_PARALLEL;
34986       else if (strcmp (p, "simd") == 0)
34987         ccode = OMP_SIMD;
34988       if (ccode != ERROR_MARK)
34989         {
34990           tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34991           char p_name[sizeof ("#pragma omp target teams distribute "
34992                               "parallel for simd")];
34993
34994           cp_lexer_consume_token (parser->lexer);
34995           strcpy (p_name, "#pragma omp target");
34996           if (!flag_openmp)  /* flag_openmp_simd  */
34997             {
34998               tree stmt;
34999               switch (ccode)
35000                 {
35001                 case OMP_TEAMS:
35002                   stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35003                                               OMP_TARGET_CLAUSE_MASK,
35004                                               cclauses, if_p);
35005                   break;
35006                 case OMP_PARALLEL:
35007                   stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35008                                                  OMP_TARGET_CLAUSE_MASK,
35009                                                  cclauses, if_p);
35010                   break;
35011                 case OMP_SIMD:
35012                   stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35013                                              OMP_TARGET_CLAUSE_MASK,
35014                                              cclauses, if_p);
35015                   break;
35016                 default:
35017                   gcc_unreachable ();
35018                 }
35019               return stmt != NULL_TREE;
35020             }
35021           keep_next_level (true);
35022           tree sb = begin_omp_structured_block (), ret;
35023           unsigned save = cp_parser_begin_omp_structured_block (parser);
35024           switch (ccode)
35025             {
35026             case OMP_TEAMS:
35027               ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35028                                          OMP_TARGET_CLAUSE_MASK, cclauses,
35029                                          if_p);
35030               break;
35031             case OMP_PARALLEL:
35032               ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35033                                             OMP_TARGET_CLAUSE_MASK, cclauses,
35034                                             if_p);
35035               break;
35036             case OMP_SIMD:
35037               ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35038                                         OMP_TARGET_CLAUSE_MASK, cclauses,
35039                                         if_p);
35040               break;
35041             default:
35042               gcc_unreachable ();
35043             }
35044           cp_parser_end_omp_structured_block (parser, save);
35045           tree body = finish_omp_structured_block (sb);
35046           if (ret == NULL_TREE)
35047             return false;
35048           if (ccode == OMP_TEAMS && !processing_template_decl)
35049             {
35050               /* For combined target teams, ensure the num_teams and
35051                  thread_limit clause expressions are evaluated on the host,
35052                  before entering the target construct.  */
35053               tree c;
35054               for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35055                    c; c = OMP_CLAUSE_CHAIN (c))
35056                 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35057                      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35058                     && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35059                   {
35060                     tree expr = OMP_CLAUSE_OPERAND (c, 0);
35061                     expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35062                     if (expr == error_mark_node)
35063                       continue;
35064                     tree tmp = TARGET_EXPR_SLOT (expr);
35065                     add_stmt (expr);
35066                     OMP_CLAUSE_OPERAND (c, 0) = expr;
35067                     tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35068                                                 OMP_CLAUSE_FIRSTPRIVATE);
35069                     OMP_CLAUSE_DECL (tc) = tmp;
35070                     OMP_CLAUSE_CHAIN (tc)
35071                       = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35072                     cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35073                   }
35074             }
35075           tree stmt = make_node (OMP_TARGET);
35076           TREE_TYPE (stmt) = void_type_node;
35077           OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35078           OMP_TARGET_BODY (stmt) = body;
35079           OMP_TARGET_COMBINED (stmt) = 1;
35080           add_stmt (stmt);
35081           pc = &OMP_TARGET_CLAUSES (stmt);
35082           goto check_clauses;
35083         }
35084       else if (!flag_openmp)  /* flag_openmp_simd  */
35085         {
35086           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35087           return false;
35088         }
35089       else if (strcmp (p, "data") == 0)
35090         {
35091           cp_lexer_consume_token (parser->lexer);
35092           cp_parser_omp_target_data (parser, pragma_tok, if_p);
35093           return true;
35094         }
35095       else if (strcmp (p, "enter") == 0)
35096         {
35097           cp_lexer_consume_token (parser->lexer);
35098           cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35099           return false;
35100         }
35101       else if (strcmp (p, "exit") == 0)
35102         {
35103           cp_lexer_consume_token (parser->lexer);
35104           cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35105           return false;
35106         }
35107       else if (strcmp (p, "update") == 0)
35108         {
35109           cp_lexer_consume_token (parser->lexer);
35110           return cp_parser_omp_target_update (parser, pragma_tok, context);
35111         }
35112     }
35113
35114   stmt = make_node (OMP_TARGET);
35115   TREE_TYPE (stmt) = void_type_node;
35116
35117   OMP_TARGET_CLAUSES (stmt)
35118     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35119                                  "#pragma omp target", pragma_tok);
35120   pc = &OMP_TARGET_CLAUSES (stmt);
35121   keep_next_level (true);
35122   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35123
35124   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35125   add_stmt (stmt);
35126
35127 check_clauses:
35128   while (*pc)
35129     {
35130       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35131         switch (OMP_CLAUSE_MAP_KIND (*pc))
35132           {
35133           case GOMP_MAP_TO:
35134           case GOMP_MAP_ALWAYS_TO:
35135           case GOMP_MAP_FROM:
35136           case GOMP_MAP_ALWAYS_FROM:
35137           case GOMP_MAP_TOFROM:
35138           case GOMP_MAP_ALWAYS_TOFROM:
35139           case GOMP_MAP_ALLOC:
35140           case GOMP_MAP_FIRSTPRIVATE_POINTER:
35141           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35142           case GOMP_MAP_ALWAYS_POINTER:
35143             break;
35144           default:
35145             error_at (OMP_CLAUSE_LOCATION (*pc),
35146                       "%<#pragma omp target%> with map-type other "
35147                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35148                       "on %<map%> clause");
35149             *pc = OMP_CLAUSE_CHAIN (*pc);
35150             continue;
35151           }
35152       pc = &OMP_CLAUSE_CHAIN (*pc);
35153     }
35154   return true;
35155 }
35156
35157 /* OpenACC 2.0:
35158    # pragma acc cache (variable-list) new-line
35159 */
35160
35161 static tree
35162 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35163 {
35164   tree stmt, clauses;
35165
35166   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35167   clauses = finish_omp_clauses (clauses, false);
35168
35169   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35170
35171   stmt = make_node (OACC_CACHE);
35172   TREE_TYPE (stmt) = void_type_node;
35173   OACC_CACHE_CLAUSES (stmt) = clauses;
35174   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35175   add_stmt (stmt);
35176
35177   return stmt;
35178 }
35179
35180 /* OpenACC 2.0:
35181    # pragma acc data oacc-data-clause[optseq] new-line
35182      structured-block  */
35183
35184 #define OACC_DATA_CLAUSE_MASK                                           \
35185         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35186         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35187         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35188         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35189         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35190         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35191         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35192         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35193         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35194         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35195         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35196
35197 static tree
35198 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35199 {
35200   tree stmt, clauses, block;
35201   unsigned int save;
35202
35203   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35204                                         "#pragma acc data", pragma_tok);
35205
35206   block = begin_omp_parallel ();
35207   save = cp_parser_begin_omp_structured_block (parser);
35208   cp_parser_statement (parser, NULL_TREE, false, if_p);
35209   cp_parser_end_omp_structured_block (parser, save);
35210   stmt = finish_oacc_data (clauses, block);
35211   return stmt;
35212 }
35213
35214 /* OpenACC 2.0:
35215   # pragma acc host_data <clauses> new-line
35216   structured-block  */
35217
35218 #define OACC_HOST_DATA_CLAUSE_MASK                                      \
35219   ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35220
35221 static tree
35222 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35223 {
35224   tree stmt, clauses, block;
35225   unsigned int save;
35226
35227   clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35228                                         "#pragma acc host_data", pragma_tok);
35229
35230   block = begin_omp_parallel ();
35231   save = cp_parser_begin_omp_structured_block (parser);
35232   cp_parser_statement (parser, NULL_TREE, false, if_p);
35233   cp_parser_end_omp_structured_block (parser, save);
35234   stmt = finish_oacc_host_data (clauses, block);
35235   return stmt;
35236 }
35237
35238 /* OpenACC 2.0:
35239    # pragma acc declare oacc-data-clause[optseq] new-line
35240 */
35241
35242 #define OACC_DECLARE_CLAUSE_MASK                                        \
35243         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35244         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35245         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35246         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35247         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35248         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)     \
35249         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)                \
35250         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35251         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35252         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35253         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35254         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35255
35256 static tree
35257 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35258 {
35259   tree clauses, stmt;
35260   bool error = false;
35261
35262   clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35263                                         "#pragma acc declare", pragma_tok, true);
35264
35265
35266   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35267     {
35268       error_at (pragma_tok->location,
35269                 "no valid clauses specified in %<#pragma acc declare%>");
35270       return NULL_TREE;
35271     }
35272
35273   for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35274     {
35275       location_t loc = OMP_CLAUSE_LOCATION (t);
35276       tree decl = OMP_CLAUSE_DECL (t);
35277       if (!DECL_P (decl))
35278         {
35279           error_at (loc, "array section in %<#pragma acc declare%>");
35280           error = true;
35281           continue;
35282         }
35283       gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35284       switch (OMP_CLAUSE_MAP_KIND (t))
35285         {
35286         case GOMP_MAP_FORCE_ALLOC:
35287         case GOMP_MAP_FORCE_TO:
35288         case GOMP_MAP_FORCE_DEVICEPTR:
35289         case GOMP_MAP_DEVICE_RESIDENT:
35290           break;
35291
35292         case GOMP_MAP_POINTER:
35293           /* Generated by c_finish_omp_clauses from array sections;
35294              avoid spurious diagnostics.  */
35295           break;
35296
35297         case GOMP_MAP_LINK:
35298           if (!global_bindings_p ()
35299               && (TREE_STATIC (decl)
35300                || !DECL_EXTERNAL (decl)))
35301             {
35302               error_at (loc,
35303                         "%qD must be a global variable in"
35304                         "%<#pragma acc declare link%>",
35305                         decl);
35306               error = true;
35307               continue;
35308             }
35309           break;
35310
35311         default:
35312           if (global_bindings_p ())
35313             {
35314               error_at (loc, "invalid OpenACC clause at file scope");
35315               error = true;
35316               continue;
35317             }
35318           if (DECL_EXTERNAL (decl))
35319             {
35320               error_at (loc,
35321                         "invalid use of %<extern%> variable %qD "
35322                         "in %<#pragma acc declare%>", decl);
35323               error = true;
35324               continue;
35325             }
35326           else if (TREE_PUBLIC (decl))
35327             {
35328               error_at (loc,
35329                         "invalid use of %<global%> variable %qD "
35330                         "in %<#pragma acc declare%>", decl);
35331               error = true;
35332               continue;
35333             }
35334           break;
35335         }
35336
35337       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35338           || lookup_attribute ("omp declare target link",
35339                                DECL_ATTRIBUTES (decl)))
35340         {
35341           error_at (loc, "variable %qD used more than once with "
35342                     "%<#pragma acc declare%>", decl);
35343           error = true;
35344           continue;
35345         }
35346
35347       if (!error)
35348         {
35349           tree id;
35350
35351           if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35352             id = get_identifier ("omp declare target link");
35353           else
35354             id = get_identifier ("omp declare target");
35355
35356           DECL_ATTRIBUTES (decl)
35357                            = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35358           if (global_bindings_p ())
35359             {
35360               symtab_node *node = symtab_node::get (decl);
35361               if (node != NULL)
35362                 {
35363                   node->offloadable = 1;
35364                   if (ENABLE_OFFLOADING)
35365                     {
35366                       g->have_offload = true;
35367                       if (is_a <varpool_node *> (node))
35368                         vec_safe_push (offload_vars, decl);
35369                     }
35370                 }
35371             }
35372         }
35373     }
35374
35375   if (error || global_bindings_p ())
35376     return NULL_TREE;
35377
35378   stmt = make_node (OACC_DECLARE);
35379   TREE_TYPE (stmt) = void_type_node;
35380   OACC_DECLARE_CLAUSES (stmt) = clauses;
35381   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35382
35383   add_stmt (stmt);
35384
35385   return NULL_TREE;
35386 }
35387
35388 /* OpenACC 2.0:
35389    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35390
35391    or
35392
35393    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35394
35395    LOC is the location of the #pragma token.
35396 */
35397
35398 #define OACC_ENTER_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_COPYIN)              \
35402         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35403         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35404         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
35405         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35406
35407 #define OACC_EXIT_DATA_CLAUSE_MASK                                      \
35408         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35409         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35410         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35411         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE)              \
35412         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35413
35414 static tree
35415 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35416                                 bool enter)
35417 {
35418   tree stmt, clauses;
35419
35420   if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35421      || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35422     {
35423       cp_parser_error (parser, enter
35424                        ? "expected %<data%> in %<#pragma acc enter data%>"
35425                        : "expected %<data%> in %<#pragma acc exit data%>");
35426       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35427       return NULL_TREE;
35428     }
35429
35430   const char *p =
35431     IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35432   if (strcmp (p, "data") != 0)
35433     {
35434       cp_parser_error (parser, "invalid pragma");
35435       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35436       return NULL_TREE;
35437     }
35438
35439   cp_lexer_consume_token (parser->lexer);
35440
35441   if (enter)
35442     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35443                                          "#pragma acc enter data", pragma_tok);
35444   else
35445     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35446                                          "#pragma acc exit data", pragma_tok);
35447
35448   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35449     {
35450       error_at (pragma_tok->location,
35451                 "%<#pragma acc enter data%> has no data movement clause");
35452       return NULL_TREE;
35453     }
35454
35455   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35456   TREE_TYPE (stmt) = void_type_node;
35457   OMP_STANDALONE_CLAUSES (stmt) = clauses;
35458   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35459   add_stmt (stmt);
35460   return stmt;
35461 }
35462
35463 /* OpenACC 2.0:
35464    # pragma acc loop oacc-loop-clause[optseq] new-line
35465      structured-block  */
35466
35467 #define OACC_LOOP_CLAUSE_MASK                                           \
35468         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)            \
35469         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
35470         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
35471         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
35472         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
35473         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
35474         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)                \
35475         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)         \
35476         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)                 \
35477         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35478
35479 static tree
35480 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35481                      omp_clause_mask mask, tree *cclauses, bool *if_p)
35482 {
35483   bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35484
35485   strcat (p_name, " loop");
35486   mask |= OACC_LOOP_CLAUSE_MASK;
35487
35488   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35489                                              cclauses == NULL);
35490   if (cclauses)
35491     {
35492       clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35493       if (*cclauses)
35494         *cclauses = finish_omp_clauses (*cclauses, false);
35495       if (clauses)
35496         clauses = finish_omp_clauses (clauses, false);
35497     }
35498
35499   tree block = begin_omp_structured_block ();
35500   int save = cp_parser_begin_omp_structured_block (parser);
35501   tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35502   cp_parser_end_omp_structured_block (parser, save);
35503   add_stmt (finish_omp_structured_block (block));
35504
35505   return stmt;
35506 }
35507
35508 /* OpenACC 2.0:
35509    # pragma acc kernels oacc-kernels-clause[optseq] new-line
35510      structured-block
35511
35512    or
35513
35514    # pragma acc parallel oacc-parallel-clause[optseq] new-line
35515      structured-block
35516 */
35517
35518 #define OACC_KERNELS_CLAUSE_MASK                                        \
35519         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35520         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35521         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35522         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35523         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35524         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
35525         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35526         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35527         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35528         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35529         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35530         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35531         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
35532         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35533
35534 #define OACC_PARALLEL_CLAUSE_MASK                                       \
35535         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35536         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
35537         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
35538         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
35539         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
35540         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
35541         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
35542         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)        \
35543         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35544         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
35545         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
35546         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
35547         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
35548         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
35549         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
35550         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
35551         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
35552         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
35553         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
35554         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35555
35556 static tree
35557 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35558                                  char *p_name, bool *if_p)
35559 {
35560   omp_clause_mask mask;
35561   enum tree_code code;
35562   switch (cp_parser_pragma_kind (pragma_tok))
35563     {
35564     case PRAGMA_OACC_KERNELS:
35565       strcat (p_name, " kernels");
35566       mask = OACC_KERNELS_CLAUSE_MASK;
35567       code = OACC_KERNELS;
35568       break;
35569     case PRAGMA_OACC_PARALLEL:
35570       strcat (p_name, " parallel");
35571       mask = OACC_PARALLEL_CLAUSE_MASK;
35572       code = OACC_PARALLEL;
35573       break;
35574     default:
35575       gcc_unreachable ();
35576     }
35577
35578   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35579     {
35580       const char *p
35581         = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35582       if (strcmp (p, "loop") == 0)
35583         {
35584           cp_lexer_consume_token (parser->lexer);
35585           tree block = begin_omp_parallel ();
35586           tree clauses;
35587           cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35588                                if_p);
35589           return finish_omp_construct (code, block, clauses);
35590         }
35591     }
35592
35593   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35594
35595   tree block = begin_omp_parallel ();
35596   unsigned int save = cp_parser_begin_omp_structured_block (parser);
35597   cp_parser_statement (parser, NULL_TREE, false, if_p);
35598   cp_parser_end_omp_structured_block (parser, save);
35599   return finish_omp_construct (code, block, clauses);
35600 }
35601
35602 /* OpenACC 2.0:
35603    # pragma acc update oacc-update-clause[optseq] new-line
35604 */
35605
35606 #define OACC_UPDATE_CLAUSE_MASK                                         \
35607         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
35608         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)              \
35609         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)                \
35610         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
35611         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)                \
35612         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35613
35614 static tree
35615 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35616 {
35617   tree stmt, clauses;
35618
35619   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35620                                          "#pragma acc update", pragma_tok);
35621
35622   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35623     {
35624       error_at (pragma_tok->location,
35625                 "%<#pragma acc update%> must contain at least one "
35626                 "%<device%> or %<host%> or %<self%> clause");
35627       return NULL_TREE;
35628     }
35629
35630   stmt = make_node (OACC_UPDATE);
35631   TREE_TYPE (stmt) = void_type_node;
35632   OACC_UPDATE_CLAUSES (stmt) = clauses;
35633   SET_EXPR_LOCATION (stmt, pragma_tok->location);
35634   add_stmt (stmt);
35635   return stmt;
35636 }
35637
35638 /* OpenACC 2.0:
35639    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35640
35641    LOC is the location of the #pragma token.
35642 */
35643
35644 #define OACC_WAIT_CLAUSE_MASK                                   \
35645         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35646
35647 static tree
35648 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35649 {
35650   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35651   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35652
35653   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35654     list = cp_parser_oacc_wait_list (parser, loc, list);
35655
35656   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35657                                         "#pragma acc wait", pragma_tok);
35658
35659   stmt = c_finish_oacc_wait (loc, list, clauses);
35660   stmt = finish_expr_stmt (stmt);
35661
35662   return stmt;
35663 }
35664
35665 /* OpenMP 4.0:
35666    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
35667
35668 #define OMP_DECLARE_SIMD_CLAUSE_MASK                            \
35669         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
35670         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
35671         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
35672         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)      \
35673         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)     \
35674         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35675
35676 static void
35677 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35678                             enum pragma_context context)
35679 {
35680   bool first_p = parser->omp_declare_simd == NULL;
35681   cp_omp_declare_simd_data data;
35682   if (first_p)
35683     {
35684       data.error_seen = false;
35685       data.fndecl_seen = false;
35686       data.tokens = vNULL;
35687       parser->omp_declare_simd = &data;
35688     }
35689   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35690          && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35691     cp_lexer_consume_token (parser->lexer);
35692   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35693     parser->omp_declare_simd->error_seen = true;
35694   cp_parser_require_pragma_eol (parser, pragma_tok);
35695   struct cp_token_cache *cp
35696     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35697   parser->omp_declare_simd->tokens.safe_push (cp);
35698   if (first_p)
35699     {
35700       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35701         cp_parser_pragma (parser, context, NULL);
35702       switch (context)
35703         {
35704         case pragma_external:
35705           cp_parser_declaration (parser);
35706           break;
35707         case pragma_member:
35708           cp_parser_member_declaration (parser);
35709           break;
35710         case pragma_objc_icode:
35711           cp_parser_block_declaration (parser, /*statement_p=*/false);
35712           break;
35713         default:
35714           cp_parser_declaration_statement (parser);
35715           break;
35716         }
35717       if (parser->omp_declare_simd
35718           && !parser->omp_declare_simd->error_seen
35719           && !parser->omp_declare_simd->fndecl_seen)
35720         error_at (pragma_tok->location,
35721                   "%<#pragma omp declare simd%> not immediately followed by "
35722                   "function declaration or definition");
35723       data.tokens.release ();
35724       parser->omp_declare_simd = NULL;
35725     }
35726 }
35727
35728 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.  
35729    This function is modelled similar to the late parsing of omp declare 
35730    simd.  */
35731
35732 static tree
35733 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35734 {
35735   struct cp_token_cache *ce;
35736   cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35737   int ii = 0;
35738
35739   if (parser->omp_declare_simd != NULL
35740       || lookup_attribute ("simd", attrs))
35741     {
35742       error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35743              "used in the same function marked as a Cilk Plus SIMD-enabled "
35744              " function");
35745       parser->cilk_simd_fn_info->tokens.release ();
35746       XDELETE (parser->cilk_simd_fn_info);
35747       parser->cilk_simd_fn_info = NULL;
35748       return attrs;
35749     }
35750   if (!info->error_seen && info->fndecl_seen)
35751     {
35752       error ("vector attribute not immediately followed by a single function"
35753              " declaration or definition");
35754       info->error_seen = true;
35755     }
35756   if (info->error_seen)
35757     return attrs;
35758
35759   FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35760     {
35761       tree c, cl;
35762
35763       cp_parser_push_lexer_for_tokens (parser, ce);
35764       parser->lexer->in_pragma = true;
35765       cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35766                                       "SIMD-enabled functions attribute", 
35767                                       NULL);
35768       cp_parser_pop_lexer (parser);
35769       if (cl)
35770         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35771
35772       c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35773       TREE_CHAIN (c) = attrs;
35774       attrs = c;
35775
35776       c = build_tree_list (get_identifier ("omp declare simd"), cl);
35777       TREE_CHAIN (c) = attrs;
35778       if (processing_template_decl)
35779         ATTR_IS_DEPENDENT (c) = 1;
35780       attrs = c;
35781     }
35782   info->fndecl_seen = true;
35783   parser->cilk_simd_fn_info->tokens.release ();
35784   XDELETE (parser->cilk_simd_fn_info);
35785   parser->cilk_simd_fn_info = NULL;
35786   return attrs;
35787 }
35788
35789 /* Finalize #pragma omp declare simd clauses after direct declarator has
35790    been parsed, and put that into "omp declare simd" attribute.  */
35791
35792 static tree
35793 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35794 {
35795   struct cp_token_cache *ce;
35796   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35797   int i;
35798
35799   if (!data->error_seen && data->fndecl_seen)
35800     {
35801       error ("%<#pragma omp declare simd%> not immediately followed by "
35802              "a single function declaration or definition");
35803       data->error_seen = true;
35804       return attrs;
35805     }
35806   if (data->error_seen)
35807     return attrs;
35808
35809   FOR_EACH_VEC_ELT (data->tokens, i, ce)
35810     {
35811       tree c, cl;
35812
35813       cp_parser_push_lexer_for_tokens (parser, ce);
35814       parser->lexer->in_pragma = true;
35815       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35816       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35817       cp_lexer_consume_token (parser->lexer);
35818       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35819                                       "#pragma omp declare simd", pragma_tok);
35820       cp_parser_pop_lexer (parser);
35821       if (cl)
35822         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35823       c = build_tree_list (get_identifier ("omp declare simd"), cl);
35824       TREE_CHAIN (c) = attrs;
35825       if (processing_template_decl)
35826         ATTR_IS_DEPENDENT (c) = 1;
35827       attrs = c;
35828     }
35829
35830   data->fndecl_seen = true;
35831   return attrs;
35832 }
35833
35834
35835 /* OpenMP 4.0:
35836    # pragma omp declare target new-line
35837    declarations and definitions
35838    # pragma omp end declare target new-line
35839
35840    OpenMP 4.5:
35841    # pragma omp declare target ( extended-list ) new-line
35842
35843    # pragma omp declare target declare-target-clauses[seq] new-line  */
35844
35845 #define OMP_DECLARE_TARGET_CLAUSE_MASK                          \
35846         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
35847         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35848
35849 static void
35850 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35851 {
35852   tree clauses = NULL_TREE;
35853   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35854     clauses
35855       = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35856                                    "#pragma omp declare target", pragma_tok);
35857   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35858     {
35859       clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35860                                         clauses);
35861       clauses = finish_omp_clauses (clauses, true);
35862       cp_parser_require_pragma_eol (parser, pragma_tok);
35863     }
35864   else
35865     {
35866       cp_parser_require_pragma_eol (parser, pragma_tok);
35867       scope_chain->omp_declare_target_attribute++;
35868       return;
35869     }
35870   if (scope_chain->omp_declare_target_attribute)
35871     error_at (pragma_tok->location,
35872               "%<#pragma omp declare target%> with clauses in between "
35873               "%<#pragma omp declare target%> without clauses and "
35874               "%<#pragma omp end declare target%>");
35875   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35876     {
35877       tree t = OMP_CLAUSE_DECL (c), id;
35878       tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35879       tree at2 = lookup_attribute ("omp declare target link",
35880                                    DECL_ATTRIBUTES (t));
35881       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35882         {
35883           id = get_identifier ("omp declare target link");
35884           std::swap (at1, at2);
35885         }
35886       else
35887         id = get_identifier ("omp declare target");
35888       if (at2)
35889         {
35890           error_at (OMP_CLAUSE_LOCATION (c),
35891                     "%qD specified both in declare target %<link%> and %<to%>"
35892                     " clauses", t);
35893           continue;
35894         }
35895       if (!at1)
35896         {
35897           symtab_node *node = symtab_node::get (t);
35898           DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35899           if (node != NULL)
35900             {
35901               node->offloadable = 1;
35902               if (ENABLE_OFFLOADING)
35903                 {
35904                   g->have_offload = true;
35905                   if (is_a <varpool_node *> (node))
35906                     vec_safe_push (offload_vars, t);
35907                 }
35908             }
35909         }
35910     }
35911 }
35912
35913 static void
35914 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35915 {
35916   const char *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, "declare") == 0)
35923     {
35924       cp_lexer_consume_token (parser->lexer);
35925       p = "";
35926       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35927         {
35928           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35929           p = IDENTIFIER_POINTER (id);
35930         }
35931       if (strcmp (p, "target") == 0)
35932         cp_lexer_consume_token (parser->lexer);
35933       else
35934         {
35935           cp_parser_error (parser, "expected %<target%>");
35936           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35937           return;
35938         }
35939     }
35940   else
35941     {
35942       cp_parser_error (parser, "expected %<declare%>");
35943       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35944       return;
35945     }
35946   cp_parser_require_pragma_eol (parser, pragma_tok);
35947   if (!scope_chain->omp_declare_target_attribute)
35948     error_at (pragma_tok->location,
35949               "%<#pragma omp end declare target%> without corresponding "
35950               "%<#pragma omp declare target%>");
35951   else
35952     scope_chain->omp_declare_target_attribute--;
35953 }
35954
35955 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
35956    expression and optional initializer clause of
35957    #pragma omp declare reduction.  We store the expression(s) as
35958    either 3, 6 or 7 special statements inside of the artificial function's
35959    body.  The first two statements are DECL_EXPRs for the artificial
35960    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35961    expression that uses those variables.
35962    If there was any INITIALIZER clause, this is followed by further statements,
35963    the fourth and fifth statements are DECL_EXPRs for the artificial
35964    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
35965    constructor variant (first token after open paren is not omp_priv),
35966    then the sixth statement is a statement with the function call expression
35967    that uses the OMP_PRIV and optionally OMP_ORIG variable.
35968    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35969    to initialize the OMP_PRIV artificial variable and there is seventh
35970    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
35971
35972 static bool
35973 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35974 {
35975   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35976   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35977   type = TREE_TYPE (type);
35978   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35979   DECL_ARTIFICIAL (omp_out) = 1;
35980   pushdecl (omp_out);
35981   add_decl_expr (omp_out);
35982   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35983   DECL_ARTIFICIAL (omp_in) = 1;
35984   pushdecl (omp_in);
35985   add_decl_expr (omp_in);
35986   tree combiner;
35987   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35988
35989   keep_next_level (true);
35990   tree block = begin_omp_structured_block ();
35991   combiner = cp_parser_expression (parser);
35992   finish_expr_stmt (combiner);
35993   block = finish_omp_structured_block (block);
35994   add_stmt (block);
35995
35996   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35997     return false;
35998
35999   const char *p = "";
36000   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36001     {
36002       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36003       p = IDENTIFIER_POINTER (id);
36004     }
36005
36006   if (strcmp (p, "initializer") == 0)
36007     {
36008       cp_lexer_consume_token (parser->lexer);
36009       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36010         return false;
36011
36012       p = "";
36013       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36014         {
36015           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36016           p = IDENTIFIER_POINTER (id);
36017         }
36018
36019       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36020       DECL_ARTIFICIAL (omp_priv) = 1;
36021       pushdecl (omp_priv);
36022       add_decl_expr (omp_priv);
36023       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36024       DECL_ARTIFICIAL (omp_orig) = 1;
36025       pushdecl (omp_orig);
36026       add_decl_expr (omp_orig);
36027
36028       keep_next_level (true);
36029       block = begin_omp_structured_block ();
36030
36031       bool ctor = false;
36032       if (strcmp (p, "omp_priv") == 0)
36033         {
36034           bool is_direct_init, is_non_constant_init;
36035           ctor = true;
36036           cp_lexer_consume_token (parser->lexer);
36037           /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
36038           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36039               || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36040                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36041                      == CPP_CLOSE_PAREN
36042                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36043                      == CPP_CLOSE_PAREN))
36044             {
36045               finish_omp_structured_block (block);
36046               error ("invalid initializer clause");
36047               return false;
36048             }
36049           initializer = cp_parser_initializer (parser, &is_direct_init,
36050                                                &is_non_constant_init);
36051           cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36052                           NULL_TREE, LOOKUP_ONLYCONVERTING);
36053         }
36054       else
36055         {
36056           cp_parser_parse_tentatively (parser);
36057           tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36058                                                   /*check_dependency_p=*/true,
36059                                                   /*template_p=*/NULL,
36060                                                   /*declarator_p=*/false,
36061                                                   /*optional_p=*/false);
36062           vec<tree, va_gc> *args;
36063           if (fn_name == error_mark_node
36064               || cp_parser_error_occurred (parser)
36065               || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36066               || ((args = cp_parser_parenthesized_expression_list
36067                                 (parser, non_attr, /*cast_p=*/false,
36068                                  /*allow_expansion_p=*/true,
36069                                  /*non_constant_p=*/NULL)),
36070                   cp_parser_error_occurred (parser)))
36071             {
36072               finish_omp_structured_block (block);
36073               cp_parser_abort_tentative_parse (parser);
36074               cp_parser_error (parser, "expected id-expression (arguments)");
36075               return false;
36076             }
36077           unsigned int i;
36078           tree arg;
36079           FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36080             if (arg == omp_priv
36081                 || (TREE_CODE (arg) == ADDR_EXPR
36082                     && TREE_OPERAND (arg, 0) == omp_priv))
36083               break;
36084           cp_parser_abort_tentative_parse (parser);
36085           if (arg == NULL_TREE)
36086             error ("one of the initializer call arguments should be %<omp_priv%>"
36087                    " or %<&omp_priv%>");
36088           initializer = cp_parser_postfix_expression (parser, false, false, false,
36089                                                       false, NULL);
36090           finish_expr_stmt (initializer);
36091         }
36092
36093       block = finish_omp_structured_block (block);
36094       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36095       add_stmt (block);
36096
36097       if (ctor)
36098         add_decl_expr (omp_orig);
36099
36100       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36101         return false;
36102     }
36103
36104   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36105     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36106
36107   return true;
36108 }
36109
36110 /* OpenMP 4.0
36111    #pragma omp declare reduction (reduction-id : typename-list : expression) \
36112       initializer-clause[opt] new-line
36113
36114    initializer-clause:
36115       initializer (omp_priv initializer)
36116       initializer (function-name (argument-list))  */
36117
36118 static void
36119 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36120                                  enum pragma_context)
36121 {
36122   auto_vec<tree> types;
36123   enum tree_code reduc_code = ERROR_MARK;
36124   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36125   unsigned int i;
36126   cp_token *first_token;
36127   cp_token_cache *cp;
36128   int errs;
36129   void *p;
36130     
36131   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
36132   p = obstack_alloc (&declarator_obstack, 0);
36133
36134   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36135     goto fail;
36136
36137   switch (cp_lexer_peek_token (parser->lexer)->type)
36138     {
36139     case CPP_PLUS:
36140       reduc_code = PLUS_EXPR;
36141       break;
36142     case CPP_MULT:
36143       reduc_code = MULT_EXPR;
36144       break;
36145     case CPP_MINUS:
36146       reduc_code = MINUS_EXPR;
36147       break;
36148     case CPP_AND:
36149       reduc_code = BIT_AND_EXPR;
36150       break;
36151     case CPP_XOR:
36152       reduc_code = BIT_XOR_EXPR;
36153       break;
36154     case CPP_OR:
36155       reduc_code = BIT_IOR_EXPR;
36156       break;
36157     case CPP_AND_AND:
36158       reduc_code = TRUTH_ANDIF_EXPR;
36159       break;
36160     case CPP_OR_OR:
36161       reduc_code = TRUTH_ORIF_EXPR;
36162       break;
36163     case CPP_NAME:
36164       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36165       break;
36166     default:
36167       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36168                                "%<|%>, %<&&%>, %<||%> or identifier");
36169       goto fail;
36170     }
36171
36172   if (reduc_code != ERROR_MARK)
36173     cp_lexer_consume_token (parser->lexer);
36174
36175   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36176   if (reduc_id == error_mark_node)
36177     goto fail;
36178
36179   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36180     goto fail;
36181
36182   /* Types may not be defined in declare reduction type list.  */
36183   const char *saved_message;
36184   saved_message = parser->type_definition_forbidden_message;
36185   parser->type_definition_forbidden_message
36186     = G_("types may not be defined in declare reduction type list");
36187   bool saved_colon_corrects_to_scope_p;
36188   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36189   parser->colon_corrects_to_scope_p = false;
36190   bool saved_colon_doesnt_start_class_def_p;
36191   saved_colon_doesnt_start_class_def_p
36192     = parser->colon_doesnt_start_class_def_p;
36193   parser->colon_doesnt_start_class_def_p = true;
36194
36195   while (true)
36196     {
36197       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36198       type = cp_parser_type_id (parser);
36199       if (type == error_mark_node)
36200         ;
36201       else if (ARITHMETIC_TYPE_P (type)
36202                && (orig_reduc_id == NULL_TREE
36203                    || (TREE_CODE (type) != COMPLEX_TYPE
36204                        && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36205                                    "min") == 0
36206                            || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36207                                       "max") == 0))))
36208         error_at (loc, "predeclared arithmetic type %qT in "
36209                        "%<#pragma omp declare reduction%>", type);
36210       else if (TREE_CODE (type) == FUNCTION_TYPE
36211                || TREE_CODE (type) == METHOD_TYPE
36212                || TREE_CODE (type) == ARRAY_TYPE)
36213         error_at (loc, "function or array type %qT in "
36214                        "%<#pragma omp declare reduction%>", type);
36215       else if (TREE_CODE (type) == REFERENCE_TYPE)
36216         error_at (loc, "reference type %qT in "
36217                        "%<#pragma omp declare reduction%>", type);
36218       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36219         error_at (loc, "const, volatile or __restrict qualified type %qT in "
36220                        "%<#pragma omp declare reduction%>", type);
36221       else
36222         types.safe_push (type);
36223
36224       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36225         cp_lexer_consume_token (parser->lexer);
36226       else
36227         break;
36228     }
36229
36230   /* Restore the saved message.  */
36231   parser->type_definition_forbidden_message = saved_message;
36232   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36233   parser->colon_doesnt_start_class_def_p
36234     = saved_colon_doesnt_start_class_def_p;
36235
36236   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36237       || types.is_empty ())
36238     {
36239      fail:
36240       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36241       goto done;
36242     }
36243
36244   first_token = cp_lexer_peek_token (parser->lexer);
36245   cp = NULL;
36246   errs = errorcount;
36247   FOR_EACH_VEC_ELT (types, i, type)
36248     {
36249       tree fntype
36250         = build_function_type_list (void_type_node,
36251                                     cp_build_reference_type (type, false),
36252                                     NULL_TREE);
36253       tree this_reduc_id = reduc_id;
36254       if (!dependent_type_p (type))
36255         this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36256       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36257       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36258       DECL_ARTIFICIAL (fndecl) = 1;
36259       DECL_EXTERNAL (fndecl) = 1;
36260       DECL_DECLARED_INLINE_P (fndecl) = 1;
36261       DECL_IGNORED_P (fndecl) = 1;
36262       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36263       SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36264       DECL_ATTRIBUTES (fndecl)
36265         = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36266                      DECL_ATTRIBUTES (fndecl));
36267       if (processing_template_decl)
36268         fndecl = push_template_decl (fndecl);
36269       bool block_scope = false;
36270       tree block = NULL_TREE;
36271       if (current_function_decl)
36272         {
36273           block_scope = true;
36274           DECL_CONTEXT (fndecl) = global_namespace;
36275           if (!processing_template_decl)
36276             pushdecl (fndecl);
36277         }
36278       else if (current_class_type)
36279         {
36280           if (cp == NULL)
36281             {
36282               while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36283                      && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36284                 cp_lexer_consume_token (parser->lexer);
36285               if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36286                 goto fail;
36287               cp = cp_token_cache_new (first_token,
36288                                        cp_lexer_peek_nth_token (parser->lexer,
36289                                                                 2));
36290             }
36291           DECL_STATIC_FUNCTION_P (fndecl) = 1;
36292           finish_member_declaration (fndecl);
36293           DECL_PENDING_INLINE_INFO (fndecl) = cp;
36294           DECL_PENDING_INLINE_P (fndecl) = 1;
36295           vec_safe_push (unparsed_funs_with_definitions, fndecl);
36296           continue;
36297         }
36298       else
36299         {
36300           DECL_CONTEXT (fndecl) = current_namespace;
36301           pushdecl (fndecl);
36302         }
36303       if (!block_scope)
36304         start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36305       else
36306         block = begin_omp_structured_block ();
36307       if (cp)
36308         {
36309           cp_parser_push_lexer_for_tokens (parser, cp);
36310           parser->lexer->in_pragma = true;
36311         }
36312       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36313         {
36314           if (!block_scope)
36315             finish_function (0);
36316           else
36317             DECL_CONTEXT (fndecl) = current_function_decl;
36318           if (cp)
36319             cp_parser_pop_lexer (parser);
36320           goto fail;
36321         }
36322       if (cp)
36323         cp_parser_pop_lexer (parser);
36324       if (!block_scope)
36325         finish_function (0);
36326       else
36327         {
36328           DECL_CONTEXT (fndecl) = current_function_decl;
36329           block = finish_omp_structured_block (block);
36330           if (TREE_CODE (block) == BIND_EXPR)
36331             DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36332           else if (TREE_CODE (block) == STATEMENT_LIST)
36333             DECL_SAVED_TREE (fndecl) = block;
36334           if (processing_template_decl)
36335             add_decl_expr (fndecl);
36336         }
36337       cp_check_omp_declare_reduction (fndecl);
36338       if (cp == NULL && types.length () > 1)
36339         cp = cp_token_cache_new (first_token,
36340                                  cp_lexer_peek_nth_token (parser->lexer, 2));
36341       if (errs != errorcount)
36342         break;
36343     }
36344
36345   cp_parser_require_pragma_eol (parser, pragma_tok);
36346
36347  done:
36348   /* Free any declarators allocated.  */
36349   obstack_free (&declarator_obstack, p);
36350 }
36351
36352 /* OpenMP 4.0
36353    #pragma omp declare simd declare-simd-clauses[optseq] new-line
36354    #pragma omp declare reduction (reduction-id : typename-list : expression) \
36355       initializer-clause[opt] new-line
36356    #pragma omp declare target new-line  */
36357
36358 static void
36359 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36360                        enum pragma_context context)
36361 {
36362   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36363     {
36364       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36365       const char *p = IDENTIFIER_POINTER (id);
36366
36367       if (strcmp (p, "simd") == 0)
36368         {
36369           cp_lexer_consume_token (parser->lexer);
36370           cp_parser_omp_declare_simd (parser, pragma_tok,
36371                                       context);
36372           return;
36373         }
36374       cp_ensure_no_omp_declare_simd (parser);
36375       if (strcmp (p, "reduction") == 0)
36376         {
36377           cp_lexer_consume_token (parser->lexer);
36378           cp_parser_omp_declare_reduction (parser, pragma_tok,
36379                                            context);
36380           return;
36381         }
36382       if (!flag_openmp)  /* flag_openmp_simd  */
36383         {
36384           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36385           return;
36386         }
36387       if (strcmp (p, "target") == 0)
36388         {
36389           cp_lexer_consume_token (parser->lexer);
36390           cp_parser_omp_declare_target (parser, pragma_tok);
36391           return;
36392         }
36393     }
36394   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36395                            "or %<target%>");
36396   cp_parser_require_pragma_eol (parser, pragma_tok);
36397 }
36398
36399 /* OpenMP 4.5:
36400    #pragma omp taskloop taskloop-clause[optseq] new-line
36401      for-loop
36402
36403    #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36404      for-loop  */
36405
36406 #define OMP_TASKLOOP_CLAUSE_MASK                                \
36407         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
36408         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
36409         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36410         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
36411         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
36412         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)    \
36413         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)    \
36414         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)     \
36415         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
36416         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36417         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
36418         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
36419         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)      \
36420         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36421
36422 static tree
36423 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36424                         char *p_name, omp_clause_mask mask, tree *cclauses,
36425                         bool *if_p)
36426 {
36427   tree clauses, sb, ret;
36428   unsigned int save;
36429   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36430
36431   strcat (p_name, " taskloop");
36432   mask |= OMP_TASKLOOP_CLAUSE_MASK;
36433
36434   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36435     {
36436       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36437       const char *p = IDENTIFIER_POINTER (id);
36438
36439       if (strcmp (p, "simd") == 0)
36440         {
36441           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36442           if (cclauses == NULL)
36443             cclauses = cclauses_buf;
36444
36445           cp_lexer_consume_token (parser->lexer);
36446           if (!flag_openmp)  /* flag_openmp_simd  */
36447             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36448                                        cclauses, if_p);
36449           sb = begin_omp_structured_block ();
36450           save = cp_parser_begin_omp_structured_block (parser);
36451           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36452                                     cclauses, if_p);
36453           cp_parser_end_omp_structured_block (parser, save);
36454           tree body = finish_omp_structured_block (sb);
36455           if (ret == NULL)
36456             return ret;
36457           ret = make_node (OMP_TASKLOOP);
36458           TREE_TYPE (ret) = void_type_node;
36459           OMP_FOR_BODY (ret) = body;
36460           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36461           SET_EXPR_LOCATION (ret, loc);
36462           add_stmt (ret);
36463           return ret;
36464         }
36465     }
36466   if (!flag_openmp)  /* flag_openmp_simd  */
36467     {
36468       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36469       return NULL_TREE;
36470     }
36471
36472   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36473                                        cclauses == NULL);
36474   if (cclauses)
36475     {
36476       cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36477       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36478     }
36479
36480   sb = begin_omp_structured_block ();
36481   save = cp_parser_begin_omp_structured_block (parser);
36482
36483   ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36484                                 if_p);
36485
36486   cp_parser_end_omp_structured_block (parser, save);
36487   add_stmt (finish_omp_structured_block (sb));
36488
36489   return ret;
36490 }
36491
36492
36493 /* OpenACC 2.0:
36494    # pragma acc routine oacc-routine-clause[optseq] new-line
36495      function-definition
36496
36497    # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36498 */
36499
36500 #define OACC_ROUTINE_CLAUSE_MASK                                        \
36501         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
36502         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
36503         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
36504         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36505
36506
36507 /* Parse the OpenACC routine pragma.  This has an optional '( name )'
36508    component, which must resolve to a declared namespace-scope
36509    function.  The clauses are either processed directly (for a named
36510    function), or defered until the immediatley following declaration
36511    is parsed.  */
36512
36513 static void
36514 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36515                         enum pragma_context context)
36516 {
36517   bool first_p = parser->oacc_routine == NULL;
36518   location_t loc = pragma_tok->location;
36519   cp_omp_declare_simd_data data;
36520   if (first_p)
36521     {
36522       data.error_seen = false;
36523       data.fndecl_seen = false;
36524       data.tokens = vNULL;
36525       data.clauses = NULL_TREE;
36526       parser->oacc_routine = &data;
36527     }
36528
36529   tree decl = NULL_TREE;
36530   /* Create a dummy claue, to record location.  */
36531   tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36532
36533   if (context != pragma_external)
36534     {
36535       cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36536       parser->oacc_routine->error_seen = true;
36537       parser->oacc_routine = NULL;
36538       return;
36539     }
36540
36541   /* Look for optional '( name )'.  */
36542   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36543     {
36544       if (!first_p)
36545         {
36546           while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36547                  && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36548             cp_lexer_consume_token (parser->lexer);
36549           if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36550             parser->oacc_routine->error_seen = true;
36551           cp_parser_require_pragma_eol (parser, pragma_tok);
36552
36553           error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36554                     "%<#pragma acc routine%> not followed by a "
36555                     "function declaration or definition");
36556
36557           parser->oacc_routine->error_seen = true;
36558           return;
36559         }
36560
36561       cp_lexer_consume_token (parser->lexer);
36562       cp_token *token = cp_lexer_peek_token (parser->lexer);
36563
36564       /* We parse the name as an id-expression.  If it resolves to
36565          anything other than a non-overloaded function at namespace
36566          scope, it's an error.  */
36567       tree id = cp_parser_id_expression (parser,
36568                                          /*template_keyword_p=*/false,
36569                                          /*check_dependency_p=*/false,
36570                                          /*template_p=*/NULL,
36571                                          /*declarator_p=*/false,
36572                                          /*optional_p=*/false);
36573       decl = cp_parser_lookup_name_simple (parser, id, token->location);
36574       if (id != error_mark_node && decl == error_mark_node)
36575         cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36576                                      token->location);
36577
36578       if (decl == error_mark_node
36579           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36580         {
36581           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36582           parser->oacc_routine = NULL;
36583           return;
36584         }
36585
36586       /* Build a chain of clauses.  */
36587       parser->lexer->in_pragma = true;
36588       tree clauses = NULL_TREE;
36589       clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36590                                             "#pragma acc routine",
36591                                             cp_lexer_peek_token
36592                                             (parser->lexer));
36593
36594       /* Force clauses to be non-null, by attaching context to it.  */
36595       clauses = tree_cons (c_head, clauses, NULL_TREE);
36596
36597       if (decl && is_overloaded_fn (decl)
36598           && (TREE_CODE (decl) != FUNCTION_DECL
36599               || DECL_FUNCTION_TEMPLATE_P  (decl)))
36600         {
36601           error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36602           parser->oacc_routine = NULL;
36603           return;
36604         }
36605
36606       /* Perhaps we should use the same rule as declarations in different
36607          namespaces?  */
36608       if (!DECL_NAMESPACE_SCOPE_P (decl))
36609         {
36610           error_at (loc, "%<#pragma acc routine%> does not refer to a "
36611                     "namespace scope function");
36612           parser->oacc_routine = NULL;
36613           return;
36614         }
36615
36616       if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36617         {
36618           error_at (loc,
36619                     "%<#pragma acc routine%> does not refer to a function");
36620           parser->oacc_routine = NULL;
36621           return;
36622         }
36623
36624       data.clauses = clauses;
36625
36626       cp_finalize_oacc_routine (parser, decl, false);
36627       data.tokens.release ();
36628       parser->oacc_routine = NULL;
36629     }
36630   else
36631     {
36632       while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36633              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36634         cp_lexer_consume_token (parser->lexer);
36635       if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36636         parser->oacc_routine->error_seen = true;
36637       cp_parser_require_pragma_eol (parser, pragma_tok);
36638
36639       struct cp_token_cache *cp
36640         = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36641       parser->oacc_routine->tokens.safe_push (cp);
36642
36643       if (first_p)
36644         parser->oacc_routine->clauses = c_head;
36645
36646       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36647         cp_parser_pragma (parser, context, NULL);
36648
36649       if (first_p)
36650         {
36651           /* Create an empty list of clauses.  */
36652           parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36653                                                      NULL_TREE);
36654           cp_parser_declaration (parser);
36655
36656           if (parser->oacc_routine
36657               && !parser->oacc_routine->error_seen
36658               && !parser->oacc_routine->fndecl_seen)
36659             error_at (loc, "%<#pragma acc routine%> not followed by a "
36660                       "function declaration or definition");
36661
36662           data.tokens.release ();
36663           parser->oacc_routine = NULL;
36664         }
36665     }
36666 }
36667
36668 /* Finalize #pragma acc routine clauses after direct declarator has
36669    been parsed, and put that into "oacc function" attribute.  */
36670
36671 static tree
36672 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36673 {
36674   struct cp_token_cache *ce;
36675   cp_omp_declare_simd_data *data = parser->oacc_routine;
36676   tree cl, clauses = parser->oacc_routine->clauses;
36677   location_t loc;
36678
36679   loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36680   
36681   if ((!data->error_seen && data->fndecl_seen)
36682       || data->tokens.length () != 1)
36683     {
36684       error_at (loc, "%<#pragma acc routine%> not followed by a "
36685                 "function declaration or definition");
36686       data->error_seen = true;
36687       return attrs;
36688     }
36689   if (data->error_seen)
36690     return attrs;
36691
36692   ce = data->tokens[0];
36693
36694   cp_parser_push_lexer_for_tokens (parser, ce);
36695   parser->lexer->in_pragma = true;
36696   gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36697
36698   cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36699   cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36700                                   "#pragma acc routine", pragma_tok);
36701   cp_parser_pop_lexer (parser);
36702
36703   tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36704
36705   /* Force clauses to be non-null, by attaching context to it.  */
36706   parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36707
36708   data->fndecl_seen = true;
36709   return attrs;
36710 }
36711
36712 /* Apply any saved OpenACC routine clauses to a just-parsed
36713    declaration.  */
36714
36715 static void
36716 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36717 {
36718   if (__builtin_expect (parser->oacc_routine != NULL, 0))
36719     {
36720       tree clauses = parser->oacc_routine->clauses;
36721       location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36722
36723       if (parser->oacc_routine->error_seen)
36724         return;
36725       
36726       if (fndecl == error_mark_node)
36727         {
36728           parser->oacc_routine = NULL;
36729           return;
36730         }
36731
36732       if (TREE_CODE (fndecl) != FUNCTION_DECL)
36733         {
36734           cp_ensure_no_oacc_routine (parser);
36735           return;
36736         }
36737
36738       if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36739         {
36740           error_at (loc,
36741                     "%<#pragma acc routine%> not followed by a function "
36742                     "declaration or definition");
36743           parser->oacc_routine = NULL;
36744         }
36745           
36746       if (get_oacc_fn_attrib (fndecl))
36747         {
36748           error_at (loc, "%<#pragma acc routine%> already applied to %D",
36749                     fndecl);
36750           parser->oacc_routine = NULL;
36751         }
36752
36753       if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36754         {
36755           error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36756                     TREE_USED (fndecl) ? "use" : "definition");
36757           parser->oacc_routine = NULL;
36758         }
36759
36760       /* Process for function attrib  */
36761       tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36762       replace_oacc_fn_attrib (fndecl, dims);
36763       
36764       /* Add an "omp target" attribute.  */
36765       DECL_ATTRIBUTES (fndecl)
36766         = tree_cons (get_identifier ("omp declare target"),
36767                      NULL_TREE, DECL_ATTRIBUTES (fndecl));
36768     }
36769 }
36770
36771 /* Main entry point to OpenMP statement pragmas.  */
36772
36773 static void
36774 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36775 {
36776   tree stmt;
36777   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36778   omp_clause_mask mask (0);
36779
36780   switch (cp_parser_pragma_kind (pragma_tok))
36781     {
36782     case PRAGMA_OACC_ATOMIC:
36783       cp_parser_omp_atomic (parser, pragma_tok);
36784       return;
36785     case PRAGMA_OACC_CACHE:
36786       stmt = cp_parser_oacc_cache (parser, pragma_tok);
36787       break;
36788     case PRAGMA_OACC_DATA:
36789       stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36790       break;
36791     case PRAGMA_OACC_ENTER_DATA:
36792       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36793       break;
36794     case PRAGMA_OACC_EXIT_DATA:
36795       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36796       break;
36797     case PRAGMA_OACC_HOST_DATA:
36798       stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36799       break;
36800     case PRAGMA_OACC_KERNELS:
36801     case PRAGMA_OACC_PARALLEL:
36802       strcpy (p_name, "#pragma acc");
36803       stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36804                                               if_p);
36805       break;
36806     case PRAGMA_OACC_LOOP:
36807       strcpy (p_name, "#pragma acc");
36808       stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36809                                   if_p);
36810       break;
36811     case PRAGMA_OACC_UPDATE:
36812       stmt = cp_parser_oacc_update (parser, pragma_tok);
36813       break;
36814     case PRAGMA_OACC_WAIT:
36815       stmt = cp_parser_oacc_wait (parser, pragma_tok);
36816       break;
36817     case PRAGMA_OMP_ATOMIC:
36818       cp_parser_omp_atomic (parser, pragma_tok);
36819       return;
36820     case PRAGMA_OMP_CRITICAL:
36821       stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36822       break;
36823     case PRAGMA_OMP_DISTRIBUTE:
36824       strcpy (p_name, "#pragma omp");
36825       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36826                                        if_p);
36827       break;
36828     case PRAGMA_OMP_FOR:
36829       strcpy (p_name, "#pragma omp");
36830       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36831                                 if_p);
36832       break;
36833     case PRAGMA_OMP_MASTER:
36834       stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36835       break;
36836     case PRAGMA_OMP_PARALLEL:
36837       strcpy (p_name, "#pragma omp");
36838       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36839                                      if_p);
36840       break;
36841     case PRAGMA_OMP_SECTIONS:
36842       strcpy (p_name, "#pragma omp");
36843       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36844       break;
36845     case PRAGMA_OMP_SIMD:
36846       strcpy (p_name, "#pragma omp");
36847       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36848                                  if_p);
36849       break;
36850     case PRAGMA_OMP_SINGLE:
36851       stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36852       break;
36853     case PRAGMA_OMP_TASK:
36854       stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36855       break;
36856     case PRAGMA_OMP_TASKGROUP:
36857       stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36858       break;
36859     case PRAGMA_OMP_TASKLOOP:
36860       strcpy (p_name, "#pragma omp");
36861       stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36862                                      if_p);
36863       break;
36864     case PRAGMA_OMP_TEAMS:
36865       strcpy (p_name, "#pragma omp");
36866       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36867                                   if_p);
36868       break;
36869     default:
36870       gcc_unreachable ();
36871     }
36872
36873   protected_set_expr_location (stmt, pragma_tok->location);
36874 }
36875 \f
36876 /* Transactional Memory parsing routines.  */
36877
36878 /* Parse a transaction attribute.
36879
36880    txn-attribute:
36881         attribute
36882         [ [ identifier ] ]
36883
36884    We use this instead of cp_parser_attributes_opt for transactions to avoid
36885    the pedwarn in C++98 mode.  */
36886
36887 static tree
36888 cp_parser_txn_attribute_opt (cp_parser *parser)
36889 {
36890   cp_token *token;
36891   tree attr_name, attr = NULL;
36892
36893   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36894     return cp_parser_attributes_opt (parser);
36895
36896   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36897     return NULL_TREE;
36898   cp_lexer_consume_token (parser->lexer);
36899   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36900     goto error1;
36901
36902   token = cp_lexer_peek_token (parser->lexer);
36903   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36904     {
36905       token = cp_lexer_consume_token (parser->lexer);
36906
36907       attr_name = (token->type == CPP_KEYWORD
36908                    /* For keywords, use the canonical spelling,
36909                       not the parsed identifier.  */
36910                    ? ridpointers[(int) token->keyword]
36911                    : token->u.value);
36912       attr = build_tree_list (attr_name, NULL_TREE);
36913     }
36914   else
36915     cp_parser_error (parser, "expected identifier");
36916
36917   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36918  error1:
36919   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36920   return attr;
36921 }
36922
36923 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36924
36925    transaction-statement:
36926      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36927        compound-statement
36928      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36929 */
36930
36931 static tree
36932 cp_parser_transaction (cp_parser *parser, cp_token *token)
36933 {
36934   unsigned char old_in = parser->in_transaction;
36935   unsigned char this_in = 1, new_in;
36936   enum rid keyword = token->keyword;
36937   tree stmt, attrs, noex;
36938
36939   cp_lexer_consume_token (parser->lexer);
36940
36941   if (keyword == RID_TRANSACTION_RELAXED
36942       || keyword == RID_SYNCHRONIZED)
36943     this_in |= TM_STMT_ATTR_RELAXED;
36944   else
36945     {
36946       attrs = cp_parser_txn_attribute_opt (parser);
36947       if (attrs)
36948         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36949     }
36950
36951   /* Parse a noexcept specification.  */
36952   if (keyword == RID_ATOMIC_NOEXCEPT)
36953     noex = boolean_true_node;
36954   else if (keyword == RID_ATOMIC_CANCEL)
36955     {
36956       /* cancel-and-throw is unimplemented.  */
36957       sorry ("atomic_cancel");
36958       noex = NULL_TREE;
36959     }
36960   else
36961     noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36962
36963   /* Keep track if we're in the lexical scope of an outer transaction.  */
36964   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36965
36966   stmt = begin_transaction_stmt (token->location, NULL, this_in);
36967
36968   parser->in_transaction = new_in;
36969   cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36970   parser->in_transaction = old_in;
36971
36972   finish_transaction_stmt (stmt, NULL, this_in, noex);
36973
36974   return stmt;
36975 }
36976
36977 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36978
36979    transaction-expression:
36980      __transaction_atomic txn-noexcept-spec[opt] ( expression )
36981      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36982 */
36983
36984 static tree
36985 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36986 {
36987   unsigned char old_in = parser->in_transaction;
36988   unsigned char this_in = 1;
36989   cp_token *token;
36990   tree expr, noex;
36991   bool noex_expr;
36992   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36993
36994   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36995       || keyword == RID_TRANSACTION_RELAXED);
36996
36997   if (!flag_tm)
36998     error_at (loc,
36999               keyword == RID_TRANSACTION_RELAXED
37000               ? G_("%<__transaction_relaxed%> without transactional memory "
37001                   "support enabled")
37002               : G_("%<__transaction_atomic%> without transactional memory "
37003                    "support enabled"));
37004
37005   token = cp_parser_require_keyword (parser, keyword,
37006       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37007           : RT_TRANSACTION_RELAXED));
37008   gcc_assert (token != NULL);
37009
37010   if (keyword == RID_TRANSACTION_RELAXED)
37011     this_in |= TM_STMT_ATTR_RELAXED;
37012
37013   /* Set this early.  This might mean that we allow transaction_cancel in
37014      an expression that we find out later actually has to be a constexpr.
37015      However, we expect that cxx_constant_value will be able to deal with
37016      this; also, if the noexcept has no constexpr, then what we parse next
37017      really is a transaction's body.  */
37018   parser->in_transaction = this_in;
37019
37020   /* Parse a noexcept specification.  */
37021   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37022                                                true);
37023
37024   if (!noex || !noex_expr
37025       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37026     {
37027       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37028
37029       expr = cp_parser_expression (parser);
37030       expr = finish_parenthesized_expr (expr);
37031
37032       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37033     }
37034   else
37035     {
37036       /* The only expression that is available got parsed for the noexcept
37037          already.  noexcept is true then.  */
37038       expr = noex;
37039       noex = boolean_true_node;
37040     }
37041
37042   expr = build_transaction_expr (token->location, expr, this_in, noex);
37043   parser->in_transaction = old_in;
37044
37045   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37046     return error_mark_node;
37047
37048   return (flag_tm ? expr : error_mark_node);
37049 }
37050
37051 /* Parse a function-transaction-block.
37052
37053    function-transaction-block:
37054      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37055          function-body
37056      __transaction_atomic txn-attribute[opt] function-try-block
37057      __transaction_relaxed ctor-initializer[opt] function-body
37058      __transaction_relaxed function-try-block
37059 */
37060
37061 static bool
37062 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37063 {
37064   unsigned char old_in = parser->in_transaction;
37065   unsigned char new_in = 1;
37066   tree compound_stmt, stmt, attrs;
37067   bool ctor_initializer_p;
37068   cp_token *token;
37069
37070   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37071       || keyword == RID_TRANSACTION_RELAXED);
37072   token = cp_parser_require_keyword (parser, keyword,
37073       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37074           : RT_TRANSACTION_RELAXED));
37075   gcc_assert (token != NULL);
37076
37077   if (keyword == RID_TRANSACTION_RELAXED)
37078     new_in |= TM_STMT_ATTR_RELAXED;
37079   else
37080     {
37081       attrs = cp_parser_txn_attribute_opt (parser);
37082       if (attrs)
37083         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37084     }
37085
37086   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37087
37088   parser->in_transaction = new_in;
37089
37090   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37091     ctor_initializer_p = cp_parser_function_try_block (parser);
37092   else
37093     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37094       (parser, /*in_function_try_block=*/false);
37095
37096   parser->in_transaction = old_in;
37097
37098   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37099
37100   return ctor_initializer_p;
37101 }
37102
37103 /* Parse a __transaction_cancel statement.
37104
37105    cancel-statement:
37106      __transaction_cancel txn-attribute[opt] ;
37107      __transaction_cancel txn-attribute[opt] throw-expression ;
37108
37109    ??? Cancel and throw is not yet implemented.  */
37110
37111 static tree
37112 cp_parser_transaction_cancel (cp_parser *parser)
37113 {
37114   cp_token *token;
37115   bool is_outer = false;
37116   tree stmt, attrs;
37117
37118   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37119                                      RT_TRANSACTION_CANCEL);
37120   gcc_assert (token != NULL);
37121
37122   attrs = cp_parser_txn_attribute_opt (parser);
37123   if (attrs)
37124     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37125
37126   /* ??? Parse cancel-and-throw here.  */
37127
37128   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37129
37130   if (!flag_tm)
37131     {
37132       error_at (token->location, "%<__transaction_cancel%> without "
37133                 "transactional memory support enabled");
37134       return error_mark_node;
37135     }
37136   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37137     {
37138       error_at (token->location, "%<__transaction_cancel%> within a "
37139                 "%<__transaction_relaxed%>");
37140       return error_mark_node;
37141     }
37142   else if (is_outer)
37143     {
37144       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37145           && !is_tm_may_cancel_outer (current_function_decl))
37146         {
37147           error_at (token->location, "outer %<__transaction_cancel%> not "
37148                     "within outer %<__transaction_atomic%>");
37149           error_at (token->location,
37150                     "  or a %<transaction_may_cancel_outer%> function");
37151           return error_mark_node;
37152         }
37153     }
37154   else if (parser->in_transaction == 0)
37155     {
37156       error_at (token->location, "%<__transaction_cancel%> not within "
37157                 "%<__transaction_atomic%>");
37158       return error_mark_node;
37159     }
37160
37161   stmt = build_tm_abort_call (token->location, is_outer);
37162   add_stmt (stmt);
37163
37164   return stmt;
37165 }
37166 \f
37167 /* The parser.  */
37168
37169 static GTY (()) cp_parser *the_parser;
37170
37171 \f
37172 /* Special handling for the first token or line in the file.  The first
37173    thing in the file might be #pragma GCC pch_preprocess, which loads a
37174    PCH file, which is a GC collection point.  So we need to handle this
37175    first pragma without benefit of an existing lexer structure.
37176
37177    Always returns one token to the caller in *FIRST_TOKEN.  This is
37178    either the true first token of the file, or the first token after
37179    the initial pragma.  */
37180
37181 static void
37182 cp_parser_initial_pragma (cp_token *first_token)
37183 {
37184   tree name = NULL;
37185
37186   cp_lexer_get_preprocessor_token (NULL, first_token);
37187   if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37188     return;
37189
37190   cp_lexer_get_preprocessor_token (NULL, first_token);
37191   if (first_token->type == CPP_STRING)
37192     {
37193       name = first_token->u.value;
37194
37195       cp_lexer_get_preprocessor_token (NULL, first_token);
37196       if (first_token->type != CPP_PRAGMA_EOL)
37197         error_at (first_token->location,
37198                   "junk at end of %<#pragma GCC pch_preprocess%>");
37199     }
37200   else
37201     error_at (first_token->location, "expected string literal");
37202
37203   /* Skip to the end of the pragma.  */
37204   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37205     cp_lexer_get_preprocessor_token (NULL, first_token);
37206
37207   /* Now actually load the PCH file.  */
37208   if (name)
37209     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37210
37211   /* Read one more token to return to our caller.  We have to do this
37212      after reading the PCH file in, since its pointers have to be
37213      live.  */
37214   cp_lexer_get_preprocessor_token (NULL, first_token);
37215 }
37216
37217 /* Parses the grainsize pragma for the _Cilk_for statement.
37218    Syntax:
37219    #pragma cilk grainsize = <VALUE>.  */
37220
37221 static void
37222 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37223 {
37224   if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37225     {
37226       tree exp = cp_parser_binary_expression (parser, false, false,
37227                                               PREC_NOT_OPERATOR, NULL);
37228       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37229       if (!exp || exp == error_mark_node)
37230         {
37231           error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37232           return;
37233         }
37234
37235       /* Make sure the next token is _Cilk_for, it is invalid otherwise.  */
37236       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37237         cp_parser_cilk_for (parser, exp, if_p);
37238       else
37239         warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37240                     "%<#pragma cilk grainsize%> is not followed by "
37241                     "%<_Cilk_for%>");
37242       return;
37243     }
37244   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37245 }
37246
37247 /* Normal parsing of a pragma token.  Here we can (and must) use the
37248    regular lexer.  */
37249
37250 static bool
37251 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37252 {
37253   cp_token *pragma_tok;
37254   unsigned int id;
37255   tree stmt;
37256   bool ret;
37257
37258   pragma_tok = cp_lexer_consume_token (parser->lexer);
37259   gcc_assert (pragma_tok->type == CPP_PRAGMA);
37260   parser->lexer->in_pragma = true;
37261
37262   id = cp_parser_pragma_kind (pragma_tok);
37263   if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37264     cp_ensure_no_omp_declare_simd (parser);
37265   switch (id)
37266     {
37267     case PRAGMA_GCC_PCH_PREPROCESS:
37268       error_at (pragma_tok->location,
37269                 "%<#pragma GCC pch_preprocess%> must be first");
37270       break;
37271
37272     case PRAGMA_OMP_BARRIER:
37273       switch (context)
37274         {
37275         case pragma_compound:
37276           cp_parser_omp_barrier (parser, pragma_tok);
37277           return false;
37278         case pragma_stmt:
37279           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37280                     "used in compound statements");
37281           break;
37282         default:
37283           goto bad_stmt;
37284         }
37285       break;
37286
37287     case PRAGMA_OMP_FLUSH:
37288       switch (context)
37289         {
37290         case pragma_compound:
37291           cp_parser_omp_flush (parser, pragma_tok);
37292           return false;
37293         case pragma_stmt:
37294           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37295                     "used in compound statements");
37296           break;
37297         default:
37298           goto bad_stmt;
37299         }
37300       break;
37301
37302     case PRAGMA_OMP_TASKWAIT:
37303       switch (context)
37304         {
37305         case pragma_compound:
37306           cp_parser_omp_taskwait (parser, pragma_tok);
37307           return false;
37308         case pragma_stmt:
37309           error_at (pragma_tok->location,
37310                     "%<#pragma omp taskwait%> may only be "
37311                     "used in compound statements");
37312           break;
37313         default:
37314           goto bad_stmt;
37315         }
37316       break;
37317
37318     case PRAGMA_OMP_TASKYIELD:
37319       switch (context)
37320         {
37321         case pragma_compound:
37322           cp_parser_omp_taskyield (parser, pragma_tok);
37323           return false;
37324         case pragma_stmt:
37325           error_at (pragma_tok->location,
37326                     "%<#pragma omp taskyield%> may only be "
37327                     "used in compound statements");
37328           break;
37329         default:
37330           goto bad_stmt;
37331         }
37332       break;
37333
37334     case PRAGMA_OMP_CANCEL:
37335       switch (context)
37336         {
37337         case pragma_compound:
37338           cp_parser_omp_cancel (parser, pragma_tok);
37339           return false;
37340         case pragma_stmt:
37341           error_at (pragma_tok->location,
37342                     "%<#pragma omp cancel%> may only be "
37343                     "used in compound statements");
37344           break;
37345         default:
37346           goto bad_stmt;
37347         }
37348       break;
37349
37350     case PRAGMA_OMP_CANCELLATION_POINT:
37351       switch (context)
37352         {
37353         case pragma_compound:
37354           cp_parser_omp_cancellation_point (parser, pragma_tok);
37355           return false;
37356         case pragma_stmt:
37357           error_at (pragma_tok->location,
37358                     "%<#pragma omp cancellation point%> may only be "
37359                     "used in compound statements");
37360           break;
37361         default:
37362           goto bad_stmt;
37363         }
37364       break;
37365
37366     case PRAGMA_OMP_THREADPRIVATE:
37367       cp_parser_omp_threadprivate (parser, pragma_tok);
37368       return false;
37369
37370     case PRAGMA_OMP_DECLARE_REDUCTION:
37371       cp_parser_omp_declare (parser, pragma_tok, context);
37372       return false;
37373
37374     case PRAGMA_OACC_DECLARE:
37375       cp_parser_oacc_declare (parser, pragma_tok);
37376       return false;
37377
37378     case PRAGMA_OACC_ROUTINE:
37379       cp_parser_oacc_routine (parser, pragma_tok, context);
37380       return false;
37381
37382     case PRAGMA_OACC_ATOMIC:
37383     case PRAGMA_OACC_CACHE:
37384     case PRAGMA_OACC_DATA:
37385     case PRAGMA_OACC_ENTER_DATA:
37386     case PRAGMA_OACC_EXIT_DATA:
37387     case PRAGMA_OACC_HOST_DATA:
37388     case PRAGMA_OACC_KERNELS:
37389     case PRAGMA_OACC_PARALLEL:
37390     case PRAGMA_OACC_LOOP:
37391     case PRAGMA_OACC_UPDATE:
37392     case PRAGMA_OACC_WAIT:
37393     case PRAGMA_OMP_ATOMIC:
37394     case PRAGMA_OMP_CRITICAL:
37395     case PRAGMA_OMP_DISTRIBUTE:
37396     case PRAGMA_OMP_FOR:
37397     case PRAGMA_OMP_MASTER:
37398     case PRAGMA_OMP_PARALLEL:
37399     case PRAGMA_OMP_SECTIONS:
37400     case PRAGMA_OMP_SIMD:
37401     case PRAGMA_OMP_SINGLE:
37402     case PRAGMA_OMP_TASK:
37403     case PRAGMA_OMP_TASKGROUP:
37404     case PRAGMA_OMP_TASKLOOP:
37405     case PRAGMA_OMP_TEAMS:
37406       if (context != pragma_stmt && context != pragma_compound)
37407         goto bad_stmt;
37408       stmt = push_omp_privatization_clauses (false);
37409       cp_parser_omp_construct (parser, pragma_tok, if_p);
37410       pop_omp_privatization_clauses (stmt);
37411       return true;
37412
37413     case PRAGMA_OMP_ORDERED:
37414       stmt = push_omp_privatization_clauses (false);
37415       ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37416       pop_omp_privatization_clauses (stmt);
37417       return ret;
37418
37419     case PRAGMA_OMP_TARGET:
37420       stmt = push_omp_privatization_clauses (false);
37421       ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37422       pop_omp_privatization_clauses (stmt);
37423       return ret;
37424
37425     case PRAGMA_OMP_END_DECLARE_TARGET:
37426       cp_parser_omp_end_declare_target (parser, pragma_tok);
37427       return false;
37428
37429     case PRAGMA_OMP_SECTION:
37430       error_at (pragma_tok->location, 
37431                 "%<#pragma omp section%> may only be used in "
37432                 "%<#pragma omp sections%> construct");
37433       break;
37434
37435     case PRAGMA_IVDEP:
37436       {
37437         if (context == pragma_external)
37438           {
37439             error_at (pragma_tok->location,
37440                       "%<#pragma GCC ivdep%> must be inside a function");
37441             break;
37442           }
37443         cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37444         cp_token *tok;
37445         tok = cp_lexer_peek_token (the_parser->lexer);
37446         if (tok->type != CPP_KEYWORD
37447             || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37448                 && tok->keyword != RID_DO))
37449           {
37450             cp_parser_error (parser, "for, while or do statement expected");
37451             return false;
37452           }
37453         cp_parser_iteration_statement (parser, if_p, true);
37454         return true;
37455       }
37456
37457     case PRAGMA_CILK_SIMD:
37458       if (context == pragma_external)
37459         {
37460           error_at (pragma_tok->location,
37461                     "%<#pragma simd%> must be inside a function");
37462           break;
37463         }
37464       stmt = push_omp_privatization_clauses (false);
37465       cp_parser_cilk_simd (parser, pragma_tok, if_p);
37466       pop_omp_privatization_clauses (stmt);
37467       return true;
37468
37469     case PRAGMA_CILK_GRAINSIZE:
37470       if (context == pragma_external)
37471         {
37472           error_at (pragma_tok->location,
37473                     "%<#pragma cilk grainsize%> must be inside a function");
37474           break;
37475         }
37476
37477       /* Ignore the pragma if Cilk Plus is not enabled.  */
37478       if (flag_cilkplus)
37479         {
37480           cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37481           return true;
37482         }
37483       else
37484         {
37485           error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37486                     "%<#pragma cilk grainsize%>");
37487           break;
37488         }
37489
37490     default:
37491       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37492       c_invoke_pragma_handler (id);
37493       break;
37494
37495     bad_stmt:
37496       cp_parser_error (parser, "expected declaration specifiers");
37497       break;
37498     }
37499
37500   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37501   return false;
37502 }
37503
37504 /* The interface the pragma parsers have to the lexer.  */
37505
37506 enum cpp_ttype
37507 pragma_lex (tree *value, location_t *loc)
37508 {
37509   cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37510   enum cpp_ttype ret = tok->type;
37511
37512   *value = tok->u.value;
37513   if (loc)
37514     *loc = tok->location;
37515
37516   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37517     ret = CPP_EOF;
37518   else if (ret == CPP_STRING)
37519     *value = cp_parser_string_literal (the_parser, false, false);
37520   else
37521     {
37522       if (ret == CPP_KEYWORD)
37523         ret = CPP_NAME;
37524       cp_lexer_consume_token (the_parser->lexer);
37525     }
37526
37527   return ret;
37528 }
37529
37530 \f
37531 /* External interface.  */
37532
37533 /* Parse one entire translation unit.  */
37534
37535 void
37536 c_parse_file (void)
37537 {
37538   static bool already_called = false;
37539
37540   if (already_called)
37541     fatal_error (input_location,
37542                  "inter-module optimizations not implemented for C++");
37543   already_called = true;
37544
37545   the_parser = cp_parser_new ();
37546   push_deferring_access_checks (flag_access_control
37547                                 ? dk_no_deferred : dk_no_check);
37548   cp_parser_translation_unit (the_parser);
37549   the_parser = NULL;
37550 }
37551
37552 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's 
37553    vectorlength clause:
37554    Syntax:
37555    vectorlength ( constant-expression )  */
37556
37557 static tree
37558 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37559                                   bool is_simd_fn)
37560 {
37561   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37562   tree expr;
37563   /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37564      safelen clause.  Thus, vectorlength is represented as OMP 4.0
37565      safelen.  For SIMD-enabled function it is represented by OMP 4.0
37566      simdlen.  */
37567   if (!is_simd_fn)
37568     check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", 
37569                                loc);
37570   else
37571     check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37572                                loc);
37573
37574   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37575     return error_mark_node;
37576
37577   expr = cp_parser_constant_expression (parser);
37578   expr = maybe_constant_value (expr);
37579
37580   /* If expr == error_mark_node, then don't emit any errors nor
37581      create a clause.  if any of the above functions returns
37582      error mark node then they would have emitted an error message.  */
37583   if (expr == error_mark_node)
37584     ;
37585   else if (!TREE_TYPE (expr)
37586            || !TREE_CONSTANT (expr)
37587            || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37588     error_at (loc, "vectorlength must be an integer constant");
37589   else if (TREE_CONSTANT (expr)
37590            && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37591     error_at (loc, "vectorlength must be a power of 2");
37592   else 
37593     {
37594       tree c;
37595       if (!is_simd_fn)
37596         { 
37597           c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN); 
37598           OMP_CLAUSE_SAFELEN_EXPR (c) = expr; 
37599           OMP_CLAUSE_CHAIN (c) = clauses; 
37600           clauses = c;
37601         }
37602       else
37603         {
37604           c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37605           OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37606           OMP_CLAUSE_CHAIN (c) = clauses;
37607           clauses = c;
37608         }
37609     }
37610
37611   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37612     return error_mark_node;
37613   return clauses;
37614 }
37615
37616 /* Handles the Cilk Plus #pragma simd linear clause.
37617    Syntax:
37618    linear ( simd-linear-variable-list )
37619
37620    simd-linear-variable-list:
37621      simd-linear-variable
37622      simd-linear-variable-list , simd-linear-variable
37623
37624    simd-linear-variable:
37625      id-expression
37626      id-expression : simd-linear-step
37627
37628    simd-linear-step:
37629    conditional-expression */
37630
37631 static tree
37632 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37633 {
37634   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37635
37636   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37637     return clauses;
37638   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37639     {
37640       cp_parser_error (parser, "expected identifier");
37641       cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37642       return error_mark_node;
37643     }
37644
37645   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37646   parser->colon_corrects_to_scope_p = false;
37647   while (1)
37648     {
37649       cp_token *token = cp_lexer_peek_token (parser->lexer);
37650       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37651         {
37652           cp_parser_error (parser, "expected variable-name");
37653           clauses = error_mark_node;
37654           break;
37655         }
37656
37657       tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37658                                                false, false);
37659       tree decl = cp_parser_lookup_name_simple (parser, var_name,
37660                                                 token->location);
37661       if (decl == error_mark_node)
37662         {
37663           cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37664                                        token->location);
37665           clauses = error_mark_node;
37666         }
37667       else
37668         {
37669           tree e = NULL_TREE;
37670           tree step_size = integer_one_node;
37671
37672           /* If present, parse the linear step.  Otherwise, assume the default
37673              value of 1.  */
37674           if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37675             {
37676               cp_lexer_consume_token (parser->lexer);
37677
37678               e = cp_parser_assignment_expression (parser);
37679               e = maybe_constant_value (e);
37680
37681               if (e == error_mark_node)
37682                 {
37683                   /* If an error has occurred,  then the whole pragma is
37684                      considered ill-formed.  Thus, no reason to keep
37685                      parsing.  */
37686                   clauses = error_mark_node;
37687                   break;
37688                 }
37689               else if (type_dependent_expression_p (e)
37690                        || value_dependent_expression_p (e)
37691                        || (TREE_TYPE (e)
37692                            && INTEGRAL_TYPE_P (TREE_TYPE (e))
37693                            && (TREE_CONSTANT (e)
37694                                || DECL_P (e))))
37695                 step_size = e;
37696               else
37697                 cp_parser_error (parser,
37698                                  "step size must be an integer constant "
37699                                  "expression or an integer variable");
37700             }
37701
37702           /* Use the OMP_CLAUSE_LINEAR,  which has the same semantics.  */
37703           tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37704           OMP_CLAUSE_DECL (l) = decl;
37705           OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37706           OMP_CLAUSE_CHAIN (l) = clauses;
37707           clauses = l;
37708         }
37709       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37710         cp_lexer_consume_token (parser->lexer);
37711       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37712         break;
37713       else
37714         {
37715           error_at (cp_lexer_peek_token (parser->lexer)->location,
37716                     "expected %<,%> or %<)%> after %qE", decl);
37717           clauses = error_mark_node;
37718           break;
37719         }
37720     }
37721   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37722   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37723   return clauses;
37724 }
37725
37726 /* Returns the name of the next clause.  If the clause is not
37727    recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37728    token is not consumed.  Otherwise, the appropriate enum from the
37729    pragma_simd_clause is returned and the token is consumed.  */
37730
37731 static pragma_omp_clause
37732 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37733 {
37734   pragma_omp_clause clause_type;
37735   cp_token *token = cp_lexer_peek_token (parser->lexer);
37736
37737   if (token->keyword == RID_PRIVATE)
37738     clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37739   else if (!token->u.value || token->type != CPP_NAME)
37740     return PRAGMA_CILK_CLAUSE_NONE;
37741   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37742     clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37743   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37744     clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37745   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37746     clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37747   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37748     clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37749   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37750     clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37751   else
37752     return PRAGMA_CILK_CLAUSE_NONE;
37753
37754   cp_lexer_consume_token (parser->lexer);
37755   return clause_type;
37756 }
37757
37758 /* Parses all the #pragma simd clauses.  Returns a list of clauses found.  */
37759
37760 static tree
37761 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37762 {
37763   tree clauses = NULL_TREE;
37764
37765   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37766          && clauses != error_mark_node)
37767     {
37768       pragma_omp_clause c_kind;
37769       c_kind = cp_parser_cilk_simd_clause_name (parser);
37770       if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37771         clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37772       else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37773         clauses = cp_parser_cilk_simd_linear (parser, clauses);
37774       else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37775         /* Use the OpenMP 4.0 equivalent function.  */
37776         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37777       else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37778         /* Use the OpenMP 4.0 equivalent function.  */
37779         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37780                                           clauses);
37781       else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37782         /* Use the OMP 4.0 equivalent function.  */
37783         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37784                                           clauses);
37785       else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37786         /* Use the OMP 4.0 equivalent function.  */
37787         clauses = cp_parser_omp_clause_reduction (parser, clauses);
37788       else
37789         {
37790           clauses = error_mark_node;
37791           cp_parser_error (parser, "expected %<#pragma simd%> clause");
37792           break;
37793         }
37794     }
37795
37796   cp_parser_skip_to_pragma_eol (parser, pragma_token);
37797
37798   if (clauses == error_mark_node)
37799     return error_mark_node;
37800   else
37801     return c_finish_cilk_clauses (clauses);
37802 }
37803
37804 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops.  */
37805
37806 static void
37807 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37808 {
37809   tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37810
37811   if (clauses == error_mark_node)
37812     return;
37813
37814   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37815     {
37816       error_at (cp_lexer_peek_token (parser->lexer)->location,
37817                 "for statement expected");
37818       return;
37819     }
37820
37821   tree sb = begin_omp_structured_block ();
37822   int save = cp_parser_begin_omp_structured_block (parser);
37823   tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37824   if (ret)
37825     cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37826   cp_parser_end_omp_structured_block (parser, save);
37827   add_stmt (finish_omp_structured_block (sb));
37828 }
37829
37830 /* Main entry-point for parsing Cilk Plus _Cilk_for
37831    loops.  The return value is error_mark_node
37832    when errors happen and CILK_FOR tree on success.  */
37833
37834 static tree
37835 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37836 {
37837   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37838     gcc_unreachable ();
37839
37840   tree sb = begin_omp_structured_block ();
37841   int save = cp_parser_begin_omp_structured_block (parser);
37842
37843   tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37844   OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37845   OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37846   clauses = finish_omp_clauses (clauses, false);
37847
37848   tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37849   if (ret)
37850     cpp_validate_cilk_plus_loop (ret);
37851   else
37852     ret = error_mark_node;
37853
37854   cp_parser_end_omp_structured_block (parser, save);
37855   add_stmt (finish_omp_structured_block (sb));
37856   return ret;
37857 }
37858
37859 /* Create an identifier for a generic parameter type (a synthesized
37860    template parameter implied by `auto' or a concept identifier). */
37861
37862 static GTY(()) int generic_parm_count;
37863 static tree
37864 make_generic_type_name ()
37865 {
37866   char buf[32];
37867   sprintf (buf, "auto:%d", ++generic_parm_count);
37868   return get_identifier (buf);
37869 }
37870
37871 /* Predicate that behaves as is_auto_or_concept but matches the parent
37872    node of the generic type rather than the generic type itself.  This
37873    allows for type transformation in add_implicit_template_parms.  */
37874
37875 static inline bool
37876 tree_type_is_auto_or_concept (const_tree t)
37877 {
37878   return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37879 }
37880
37881 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37882    (creating a new template parameter list if necessary).  Returns the newly
37883    created template type parm.  */
37884
37885 static tree
37886 synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
37887 {
37888   gcc_assert (current_binding_level->kind == sk_function_parms);
37889
37890    /* Before committing to modifying any scope, if we're in an
37891       implicit template scope, and we're trying to synthesize a
37892       constrained parameter, try to find a previous parameter with
37893       the same name.  This is the same-type rule for abbreviated
37894       function templates.
37895
37896       NOTE: We can generate implicit parameters when tentatively
37897       parsing a nested name specifier, only to reject that parse
37898       later. However, matching the same template-id as part of a
37899       direct-declarator should generate an identical template
37900       parameter, so this rule will merge them. */
37901   if (parser->implicit_template_scope && constr)
37902     {
37903       tree t = parser->implicit_template_parms;
37904       while (t)
37905         {
37906           if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37907             {
37908               tree d = TREE_VALUE (t);
37909               if (TREE_CODE (d) == PARM_DECL)
37910                 /* Return the TEMPLATE_PARM_INDEX.  */
37911                 d = DECL_INITIAL (d);
37912               return d;
37913             }
37914           t = TREE_CHAIN (t);
37915         }
37916     }
37917
37918   /* We are either continuing a function template that already contains implicit
37919      template parameters, creating a new fully-implicit function template, or
37920      extending an existing explicit function template with implicit template
37921      parameters.  */
37922
37923   cp_binding_level *const entry_scope = current_binding_level;
37924
37925   bool become_template = false;
37926   cp_binding_level *parent_scope = 0;
37927
37928   if (parser->implicit_template_scope)
37929     {
37930       gcc_assert (parser->implicit_template_parms);
37931
37932       current_binding_level = parser->implicit_template_scope;
37933     }
37934   else
37935     {
37936       /* Roll back to the existing template parameter scope (in the case of
37937          extending an explicit function template) or introduce a new template
37938          parameter scope ahead of the function parameter scope (or class scope
37939          in the case of out-of-line member definitions).  The function scope is
37940          added back after template parameter synthesis below.  */
37941
37942       cp_binding_level *scope = entry_scope;
37943
37944       while (scope->kind == sk_function_parms)
37945         {
37946           parent_scope = scope;
37947           scope = scope->level_chain;
37948         }
37949       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37950         {
37951           /* If not defining a class, then any class scope is a scope level in
37952              an out-of-line member definition.  In this case simply wind back
37953              beyond the first such scope to inject the template parameter list.
37954              Otherwise wind back to the class being defined.  The latter can
37955              occur in class member friend declarations such as:
37956
37957                class A {
37958                  void foo (auto);
37959                };
37960                class B {
37961                  friend void A::foo (auto);
37962                };
37963
37964             The template parameter list synthesized for the friend declaration
37965             must be injected in the scope of 'B'.  This can also occur in
37966             erroneous cases such as:
37967
37968                struct A {
37969                  struct B {
37970                    void foo (auto);
37971                  };
37972                  void B::foo (auto) {}
37973                };
37974
37975             Here the attempted definition of 'B::foo' within 'A' is ill-formed
37976             but, nevertheless, the template parameter list synthesized for the
37977             declarator should be injected into the scope of 'A' as if the
37978             ill-formed template was specified explicitly.  */
37979
37980           while (scope->kind == sk_class && !scope->defining_class_p)
37981             {
37982               parent_scope = scope;
37983               scope = scope->level_chain;
37984             }
37985         }
37986
37987       current_binding_level = scope;
37988
37989       if (scope->kind != sk_template_parms
37990           || !function_being_declared_is_template_p (parser))
37991         {
37992           /* Introduce a new template parameter list for implicit template
37993              parameters.  */
37994
37995           become_template = true;
37996
37997           parser->implicit_template_scope
37998               = begin_scope (sk_template_parms, NULL);
37999
38000           ++processing_template_decl;
38001
38002           parser->fully_implicit_function_template_p = true;
38003           ++parser->num_template_parameter_lists;
38004         }
38005       else
38006         {
38007           /* Synthesize implicit template parameters at the end of the explicit
38008              template parameter list.  */
38009
38010           gcc_assert (current_template_parms);
38011
38012           parser->implicit_template_scope = scope;
38013
38014           tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38015           parser->implicit_template_parms
38016             = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38017         }
38018     }
38019
38020   /* Synthesize a new template parameter and track the current template
38021      parameter chain with implicit_template_parms.  */
38022
38023   tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38024   tree synth_id = make_generic_type_name ();
38025   tree synth_tmpl_parm;
38026   bool non_type = false;
38027
38028   if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38029     synth_tmpl_parm
38030       = finish_template_type_parm (class_type_node, synth_id);
38031   else if (TREE_CODE (proto) == TEMPLATE_DECL)
38032     synth_tmpl_parm
38033       = finish_constrained_template_template_parm (proto, synth_id);
38034   else
38035     {
38036       synth_tmpl_parm = copy_decl (proto);
38037       DECL_NAME (synth_tmpl_parm) = synth_id;
38038       non_type = true;
38039     }
38040
38041   // Attach the constraint to the parm before processing.
38042   tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38043   TREE_TYPE (node) = constr;
38044   tree new_parm
38045     = process_template_parm (parser->implicit_template_parms,
38046                              input_location,
38047                              node,
38048                              /*non_type=*/non_type,
38049                              /*param_pack=*/false);
38050
38051   // Chain the new parameter to the list of implicit parameters.
38052   if (parser->implicit_template_parms)
38053     parser->implicit_template_parms
38054       = TREE_CHAIN (parser->implicit_template_parms);
38055   else
38056     parser->implicit_template_parms = new_parm;
38057
38058   tree new_decl = getdecls ();
38059   if (non_type)
38060     /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
38061     new_decl = DECL_INITIAL (new_decl);
38062
38063   /* If creating a fully implicit function template, start the new implicit
38064      template parameter list with this synthesized type, otherwise grow the
38065      current template parameter list.  */
38066
38067   if (become_template)
38068     {
38069       parent_scope->level_chain = current_binding_level;
38070
38071       tree new_parms = make_tree_vec (1);
38072       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38073       current_template_parms = tree_cons (size_int (processing_template_decl),
38074                                           new_parms, current_template_parms);
38075     }
38076   else
38077     {
38078       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38079       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38080       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38081       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38082     }
38083
38084   // If the new parameter was constrained, we need to add that to the
38085   // constraints in the template parameter list.
38086   if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38087     {
38088       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38089       reqs = conjoin_constraints (reqs, req);
38090       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38091     }
38092
38093   current_binding_level = entry_scope;
38094
38095   return new_decl;
38096 }
38097
38098 /* Finish the declaration of a fully implicit function template.  Such a
38099    template has no explicit template parameter list so has not been through the
38100    normal template head and tail processing.  synthesize_implicit_template_parm
38101    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
38102    provided if the declaration is a class member such that its template
38103    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
38104    form is returned.  Otherwise NULL_TREE is returned. */
38105
38106 static tree
38107 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38108 {
38109   gcc_assert (parser->fully_implicit_function_template_p);
38110
38111   if (member_decl_opt && member_decl_opt != error_mark_node
38112       && DECL_VIRTUAL_P (member_decl_opt))
38113     {
38114       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38115                 "implicit templates may not be %<virtual%>");
38116       DECL_VIRTUAL_P (member_decl_opt) = false;
38117     }
38118
38119   if (member_decl_opt)
38120     member_decl_opt = finish_member_template_decl (member_decl_opt);
38121   end_template_decl ();
38122
38123   parser->fully_implicit_function_template_p = false;
38124   --parser->num_template_parameter_lists;
38125
38126   return member_decl_opt;
38127 }
38128
38129 #include "gt-cp-parser.h"